diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2024-08-29 13:09:54 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2024-08-29 13:09:54 +0700 |
| commit | 0e9e58da406e62a72ce2ba18c6317e3de57963a1 (patch) | |
| tree | e71748bf819c9760400183029aaede2a00772a3a | |
| parent | f4c67f4f8ef9ffc9da3e0406cb08fffb6ba44fd4 (diff) | |
cr api voucher
| -rw-r--r-- | indoteknik_api/controllers/api_v1/sale_order.py | 16 | ||||
| -rw-r--r-- | indoteknik_custom/models/website_user_cart.py | 20 |
2 files changed, 27 insertions, 9 deletions
diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index b351bacc..f6417bf9 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -342,11 +342,21 @@ 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, **kw): - cart = request.env['website.user.cart'] + m_voucher = request.env['voucher'] + m_cart = request.env['website.user.cart'] + voucher_code = kw.get('voucher') + voucher_shipping_code = kw.get('voucher_shipping') source = kw.get('source') - voucher = request.env['voucher'].search([('code', '=', voucher_code)], limit=1) - result = cart.with_context(price_for="web").get_user_checkout(user_id, voucher, source) + + voucher = m_voucher.search([('code', '=', voucher_code)], limit=1) + voucher_shipping = m_voucher.search([('code', '=', voucher_shipping_code)], limit=1) + result = m_cart.with_context(price_for="web").get_user_checkout( + user_id, + voucher=voucher, + voucher_shipping=voucher_shipping, + source=source + ) 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 d9352abb..c233dfd7 100644 --- a/indoteknik_custom/models/website_user_cart.py +++ b/indoteknik_custom/models/website_user_cart.py @@ -91,7 +91,7 @@ class WebsiteUserCart(models.Model): products = carts.get_products() return products - def get_user_checkout(self, user_id, voucher=False, source=False): + def get_user_checkout(self, user_id, voucher=False, voucher_shipping=False, source=False): products = self.get_product_by_user(user_id=user_id, selected=True, source=source) total_purchase = 0 @@ -109,12 +109,12 @@ class WebsiteUserCart(models.Model): subtotal = total_purchase - total_discount discount_voucher = 0 - if voucher: - order_line = [] + discount_voucher_shipping = 0 + order_line = [] + + if voucher or voucher_shipping: for product in products: - if product['cart_type'] == 'promotion': - continue - + if product['cart_type'] == 'promotion': continue order_line.append({ 'product_id': self.env['product.product'].browse(product['id']), 'price': product['price']['price'], @@ -122,9 +122,16 @@ class WebsiteUserCart(models.Model): 'qty': product['quantity'], 'subtotal': product['subtotal'] }) + + if voucher: voucher_info = voucher.apply(order_line) discount_voucher = voucher_info['discount']['all'] subtotal -= discount_voucher + + if voucher_shipping: + voucher_shipping_info = voucher_shipping.apply(order_line) + discount_voucher_shipping = voucher_shipping_info['discount']['all'] + tax = round(subtotal * 0.11) grand_total = subtotal + tax total_weight = sum(x['weight'] * x['quantity'] for x in products) @@ -133,6 +140,7 @@ class WebsiteUserCart(models.Model): 'total_purchase': total_purchase, 'total_discount': total_discount, 'discount_voucher': discount_voucher, + 'discount_voucher_shipping': discount_voucher_shipping, 'subtotal': subtotal, 'tax': tax, 'grand_total': round(grand_total), |
