summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-07-20 11:20:21 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-07-20 11:20:21 +0700
commit3c9ef63acb42298d948ee86407d9a5ca67004246 (patch)
tree87dfe77c6ebf6b3aaf8e3f336432947e502487e8
parented231d1cfa4e78b8f98c5406f88c1f985c0225a7 (diff)
Add discount_voucher on get checkout data API
-rw-r--r--indoteknik_api/controllers/api_v1/sale_order.py6
-rw-r--r--indoteknik_custom/models/website_user_cart.py7
2 files changed, 10 insertions, 3 deletions
diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py
index 29649315..88a72755 100644
--- a/indoteknik_api/controllers/api_v1/sale_order.py
+++ b/indoteknik_api/controllers/api_v1/sale_order.py
@@ -234,9 +234,11 @@ class SaleOrder(controller.Controller):
@http.route(prefix + 'user/<user_id>/sale_order/checkout', auth='public', method=['GET', 'OPTIONS'], csrf=False)
@controller.Controller.must_authorized(private=True, private_key='user_id')
- def get_user_checkout_so(self, user_id):
+ def get_user_checkout_so(self, user_id, **kw):
cart = request.env['website.user.cart']
- result = cart.get_user_checkout(user_id)
+ voucher_code = kw.get('voucher')
+ voucher = request.env['voucher'].search([('code', '=', voucher_code)], limit=1)
+ result = cart.get_user_checkout(user_id, voucher)
return self.response(result)
@http.route(PREFIX_PARTNER + 'sale_order/checkout', auth='public', method=['POST', 'OPTIONS'], csrf=False)
diff --git a/indoteknik_custom/models/website_user_cart.py b/indoteknik_custom/models/website_user_cart.py
index 802dbd37..29bf4291 100644
--- a/indoteknik_custom/models/website_user_cart.py
+++ b/indoteknik_custom/models/website_user_cart.py
@@ -45,17 +45,22 @@ class WebsiteUserCart(models.Model):
products = carts.get_products()
return products
- def get_user_checkout(self, user_id):
+ def get_user_checkout(self, user_id, voucher=False):
products = self.get_product_by_user(user_id=user_id, selected=True)
total_purchase = sum(x['price']['price'] * x['quantity'] for x in products)
total_discount = sum((x['price']['price'] - x['price']['price_discount']) * x['quantity'] for x in products)
subtotal = total_purchase - total_discount
+ discount_voucher = 0
+ if voucher:
+ discount_voucher = voucher.calculate_discount(subtotal)
+ subtotal -= discount_voucher
tax = round(subtotal * 0.11)
grand_total = subtotal + tax
total_weight = sum(x['weight'] * x['quantity'] for x in products)
result = {
'total_purchase': total_purchase,
'total_discount': total_discount,
+ 'discount_voucher': discount_voucher,
'subtotal': subtotal,
'tax': tax,
'grand_total': round(grand_total),