summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-09-06 10:33:34 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-09-06 10:33:34 +0700
commitea48748650d1abe7b9c09f961eaa3762750e21be (patch)
tree6fabd4d2aff8d88fb76aa3fb4a10478975bd3c47 /indoteknik_api/controllers/api_v1
parenta8404a3ed4423dd7d86f8637ecbf35b7cc18a14c (diff)
Add voucher terms and conditions
Add voucher to sale order API
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
-rw-r--r--indoteknik_api/controllers/api_v1/sale_order.py22
-rw-r--r--indoteknik_api/controllers/api_v1/voucher.py33
2 files changed, 18 insertions, 37 deletions
diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py
index ecc6c771..ccecb55f 100644
--- a/indoteknik_api/controllers/api_v1/sale_order.py
+++ b/indoteknik_api/controllers/api_v1/sale_order.py
@@ -345,28 +345,8 @@ class SaleOrder(controller.Controller):
voucher_code = params['value']['voucher']
voucher = request.env['voucher'].search([('code', '=', voucher_code)])
if voucher:
- amount_untaxed = 0
- manufacture_ids = [x.id for x in voucher.manufacture_ids]
- for line in sale_order.order_line:
- manufacture_id = line.product_id.x_manufacture.id or False
- if len(manufacture_ids) == 0 or manufacture_id in manufacture_ids:
- amount_untaxed += line.price_subtotal
-
- voucher_discount = voucher.calculate_discount(amount_untaxed)
sale_order.voucher_id = voucher.id
- sale_order.amount_voucher_disc = voucher_discount
-
- for line in sale_order.order_line:
- manufacture_id = line.product_id.x_manufacture.id or False
- if len(manufacture_ids) > 0 and manufacture_id not in manufacture_ids:
- continue
- voucher_disc_line = line.price_subtotal / amount_untaxed * voucher_discount
- line.amount_voucher_disc = voucher_disc_line
-
- voucher_disc_item = voucher_disc_line / line.product_uom_qty
- voucher_disc_unit = line.price_unit - voucher_disc_item
-
- line.discount += (line.price_unit - voucher_disc_unit) / line.price_unit * 100
+ sale_order.apply_voucher()
cart_ids = [x['cart_id'] for x in products]
user_cart.browse(cart_ids).unlink()
diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py
index e19ae583..2ad61feb 100644
--- a/indoteknik_api/controllers/api_v1/voucher.py
+++ b/indoteknik_api/controllers/api_v1/voucher.py
@@ -26,34 +26,35 @@ class Voucher(controller.Controller):
parameter += [('visibility', 'in', visibility)]
vouchers = request.env['voucher'].get_active_voucher(parameter)
checkout = cart.get_user_checkout(user_id, source=source)
-
+ products = checkout['products']
+
+ order_line = []
+ for product in products:
+ order_line.append({
+ 'product_id': request.env['product.product'].browse(product['id']),
+ 'price': product['price']['price'],
+ 'discount': product['price']['discount_percentage'],
+ 'qty': product['quantity'],
+ 'subtotal': product['subtotal']
+ })
+
results = []
for voucher in vouchers:
- products = checkout['products']
-
- order_line = []
- for product in products:
- order_line.append({
- 'product_id': request.env['product.product'].browse(product['id']),
- 'price': product['price']['price'],
- 'discount': product['price']['discount_percentage'],
- 'qty': product['quantity'],
- 'subtotal': product['subtotal']
- })
+ if not voucher.can_used:
+ continue
+
voucher_info = voucher.apply(order_line)
+ voucher_tnc = voucher.terms_conditions or voucher.generate_tnc()
voucher_discount = voucher_info['discount']['all']
valid_order = voucher_info['valid_order']
- has_flashsale_products = any(product['has_flashsale'] for product in products)
can_apply = True if valid_order and voucher_discount > 0 else False
voucher_res = voucher.format()
voucher_res['can_apply'] = can_apply
- voucher_res['apply_status'] = ''
- voucher_res['has_flashsale_products'] = has_flashsale_products
- voucher_res['difference_to_apply'] = 0
voucher_res['discount_voucher'] = voucher_discount
+ voucher_res['terms_conditions'] = voucher_tnc
results.append(voucher_res)
sorted_results = sorted(results, key=lambda x: x['can_apply'], reverse=True)