diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-08-31 16:09:27 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-08-31 16:09:27 +0700 |
| commit | a8404a3ed4423dd7d86f8637ecbf35b7cc18a14c (patch) | |
| tree | 58a5dbfc17d2a3e328ca2efd5b7ac0ed8b976b79 /indoteknik_api/controllers/api_v1 | |
| parent | f5d3b28344f3b992377efbba556306b423bdf723 (diff) | |
Update get voucher api
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/voucher.py | 66 |
1 files changed, 23 insertions, 43 deletions
diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 0b769ee7..e19ae583 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -25,57 +25,37 @@ class Voucher(controller.Controller): parameter += [('visibility', 'in', visibility)] vouchers = request.env['voucher'].get_active_voucher(parameter) - vouchers = vouchers.res_format() checkout = cart.get_user_checkout(user_id, source=source) + results = [] for voucher in vouchers: - apply_status = '' products = checkout['products'] - min_purchase_amount = voucher['min_purchase_amount'] - can_apply = False - difference_to_apply = 0 - manufacture_ids = voucher['manufacture_ids'] - subtotal = 0 - has_match_manufacture = False + order_line = [] for product in products: - price = product['price']['price'] - price_discount = product['price']['price_discount'] - quantity = product['quantity'] - manufacture_id = product['manufacture']['id'] or False - - if len(manufacture_ids) == 0 or manufacture_id in manufacture_ids: - has_match_manufacture = True - - if product['has_flashsale']: - continue - - purchase_amt = price * quantity - discount_amt = (price - price_discount) * quantity - subtotal += purchase_amt - discount_amt - + 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'] + }) + voucher_info = voucher.apply(order_line) + voucher_discount = voucher_info['discount']['all'] + + valid_order = voucher_info['valid_order'] has_flashsale_products = any(product['has_flashsale'] for product in products) - if not has_match_manufacture: - apply_status = 'UM' # Unqualified Manufacture - elif subtotal < min_purchase_amount: - apply_status = 'MPA' # Minimum Purchase Amount - if has_flashsale_products: - apply_status += '-HF' # Has Flashsale - else: - can_apply = True - - if subtotal < min_purchase_amount: - difference_to_apply = min_purchase_amount - subtotal - obj_voucher = request.env['voucher'].browse(voucher['id']) - discount_voucher = obj_voucher.calculate_discount(subtotal) + can_apply = True if valid_order and voucher_discount > 0 else False - voucher['can_apply'] = can_apply - voucher['apply_status'] = apply_status - voucher['has_flashsale_products'] = has_flashsale_products - voucher['discount_voucher'] = discount_voucher - voucher['difference_to_apply'] = difference_to_apply + 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 + results.append(voucher_res) - sorted_vouchers = sorted(vouchers, key=lambda x: x['can_apply'], reverse=True) + sorted_results = sorted(results, key=lambda x: x['can_apply'], reverse=True) - return self.response(sorted_vouchers) + return self.response(sorted_results) |
