From c2b5ab68a84bf830f64e9c4e39f92b24c085c7e6 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Thu, 1 Jun 2023 17:12:25 +0200 Subject: calculate cart sum --- db/functions.sql | 42 +++++++++++++++++++++++++++++++++--------- public/admin-order.php | 7 +++++-- public/admin.css | 9 +++++---- public/cart.css | 2 ++ public/cart.php | 12 +++++++++++- public/global.css | 1 + public/index.php | 10 ++++++++++ 7 files changed, 67 insertions(+), 16 deletions(-) diff --git a/db/functions.sql b/db/functions.sql index 995e7e9..5bbba54 100644 --- a/db/functions.sql +++ b/db/functions.sql @@ -1,30 +1,54 @@ -drop function if exists webs.cart; -drop function if exists webs.add_to_cart; +use webs; +drop function if exists cart; +drop function if exists add_to_cart; delimiter $$ -create function webs.cart(user_id int) -- get current order for user_id (cart order id) +create function cart(user_id int) -- get current order for user_id (cart order id) returns int begin - set @order_id = (select id from webs.order where status = 1 and user = user_id); + set @order_id = (select id from `order` where status = 1 and user = user_id); if @order_id is not null then return @order_id; end if; - insert into webs.order (`user`) values (user_id); - set @order_id = (select id from webs.order where status = 1 and user = user_id); + insert into `order` (`user`) values (user_id); + set @order_id = (select id from `order` where status = 1 and user = user_id); return @order_id; end$$ -create function webs.add_to_cart(product_id int, user_id int) +create function add_to_cart(product_id int, user_id int) returns boolean begin - set @orderproduct_id = (select id from webs.orderproduct where product = product_id and `order` = cart(user_id)); + set @orderproduct_id = (select id from orderproduct where product = product_id and `order` = cart(user_id)); if @orderproduct_id is not null then update orderproduct set count = count + 1 where id = @orderproduct_id; return true; end if; - insert into webs.orderproduct (`product`, `count`, `order`) values (product_id, 1, cart(user_id)); + insert into orderproduct (`product`, `count`, `order`) values (product_id, 1, cart(user_id)); return false; end$$ +create function cart_sum(order_id int) -- get cart sum for order +returns int +begin + return ( + with precalc + as ( + select + product.price, + orderproduct.count, + promotion.count_buff, + promotion.price_buff, + floor(orderproduct.count / promotion.count_buff) * promotion.count_buff as qualify_count, + mod(orderproduct.count, promotion.count_buff) as remainder_count + from orderproduct + join product on product.id = orderproduct.product + join promotion on promotion.product = product.id + where `order` = order_id + ) + select sum(price * qualify_count * price_buff + price * remainder_count) + from precalc + ); +end$$ + delimiter ; diff --git a/public/admin-order.php b/public/admin-order.php index debde66..8bb7fc0 100644 --- a/public/admin-order.php +++ b/public/admin-order.php @@ -14,6 +14,7 @@ } while (false); ?> subtotal, 2, ',', '.'); echo <<<"EOF"
@@ -21,6 +22,7 @@ function order_template($order) { $order->id $order->user_name $order->product_count + $subtotal - $item->price + $price EOF; } @@ -86,8 +87,17 @@ EOF; EOF; while ($product = $res->fetch_object()) item_template($product); + + $statement = $cursor->prepare("select cart_sum(cart(?)) as `sum`"); + $statement->bind_param("i", $user_id); + if (!$statement->execute()) break; + $res = $statement->get_result(); + $subtotal = number_format($res->fetch_object()->sum, 2, ',', '.'); echo <<<"EOF"
+
+ Subtotaal: $subtotal +
diff --git a/public/global.css b/public/global.css index 2a23250..a34b503 100644 --- a/public/global.css +++ b/public/global.css @@ -102,3 +102,4 @@ input.buttonstyle[type=number] { cursor: unset; } } .skipafter { margin-bottom: 16px; } +.alignright { text-align: right; } diff --git a/public/index.php b/public/index.php index 0fd0ff2..fecf624 100644 --- a/public/index.php +++ b/public/index.php @@ -20,6 +20,16 @@ function promobuff2str($price_buff, $count_buff) {
+ +
+ +
+
+ EOF; + } while (false); ?>

hier zijn de aanbiedingen