diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-09-11 13:18:02 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-09-11 13:18:02 +0700 |
| commit | 1694e042acb29b476815d29f54f2ec95d37883be (patch) | |
| tree | d502c76d333cc4c0602e01a98eeb7fb7f0461c01 /indoteknik_api/controllers/api_v1/voucher.py | |
| parent | 02c2304b242245250177fec6ab3c911d9acba781 (diff) | |
| parent | 9a630354c1d00e218595db9b2e8cd0b7df9ed97c (diff) | |
Merge branch 'feature/voucher-group' into production
Diffstat (limited to 'indoteknik_api/controllers/api_v1/voucher.py')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/voucher.py | 84 |
1 files changed, 40 insertions, 44 deletions
diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 0b769ee7..dfe9ceba 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -1,6 +1,7 @@ from .. import controller from odoo import http from odoo.http import request +from bs4 import BeautifulSoup class Voucher(controller.Controller): @@ -11,7 +12,7 @@ class Voucher(controller.Controller): def get_vouchers(self, **kw): cart = request.env['website.user.cart'] code = kw.get('code') - user_id = kw.get('user_id') + user_id = int(kw.get('user_id', 0)) source = kw.get('source') visibility = ['public'] @@ -25,57 +26,52 @@ 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) + products = checkout['products'] - 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 - for product in products: - price = product['price']['price'] - price_discount = product['price']['price_discount'] - quantity = product['quantity'] - manufacture_id = product['manufacture']['id'] or False + user = request.env['res.users'].search([('id', '=', user_id)], limit=1) + if not user: + return self.response([]) - if len(manufacture_ids) == 0 or manufacture_id in manufacture_ids: - has_match_manufacture = True + 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: + if voucher.limit > 0 and voucher.count_order >= voucher.limit: + continue - if product['has_flashsale']: - continue + partner_voucher_orders = [] + for order in voucher.order_ids: + if order.partner_id.id == user.partner_id.id: + partner_voucher_orders.append(order) + + if voucher.limit_user > 0 and len(partner_voucher_orders) >= voucher.limit_user: + continue + + voucher_info = voucher.apply(order_line) + voucher_discount = voucher_info['discount']['all'] - purchase_amt = price * quantity - discount_amt = (price - price_discount) * quantity - subtotal += purchase_amt - discount_amt + 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 + can_apply = True if valid_order and voucher_discount > 0 else False - if subtotal < min_purchase_amount: - difference_to_apply = min_purchase_amount - subtotal + voucher_res = voucher.format() + voucher_res['can_apply'] = can_apply + voucher_res['discount_voucher'] = voucher_discount - obj_voucher = request.env['voucher'].browse(voucher['id']) - discount_voucher = obj_voucher.calculate_discount(subtotal) + cleaned_tnc = BeautifulSoup(voucher.terms_conditions or '', "html.parser").get_text() + voucher_res['terms_conditions'] = voucher.terms_conditions if cleaned_tnc else voucher.generate_tnc() - 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 + 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) |
