From a8404a3ed4423dd7d86f8637ecbf35b7cc18a14c Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 31 Aug 2023 16:09:27 +0700 Subject: Update get voucher api --- indoteknik_api/controllers/api_v1/voucher.py | 66 ++++++++++------------------ 1 file changed, 23 insertions(+), 43 deletions(-) (limited to 'indoteknik_api/controllers/api_v1/voucher.py') 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) -- cgit v1.2.3 From ea48748650d1abe7b9c09f961eaa3762750e21be Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Wed, 6 Sep 2023 10:33:34 +0700 Subject: Add voucher terms and conditions Add voucher to sale order API --- indoteknik_api/controllers/api_v1/voucher.py | 33 ++++++++++++++-------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'indoteknik_api/controllers/api_v1/voucher.py') 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) -- cgit v1.2.3 From 6077556118dc28824c73baf157c75792ab75fa16 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 7 Sep 2023 15:53:53 +0700 Subject: Add filter on get voucher API --- indoteknik_api/controllers/api_v1/voucher.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'indoteknik_api/controllers/api_v1/voucher.py') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 2ad61feb..e176803d 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -11,7 +11,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'] @@ -28,6 +28,10 @@ class Voucher(controller.Controller): checkout = cart.get_user_checkout(user_id, source=source) products = checkout['products'] + user = request.env['res.users'].search([('id', '=', user_id)], limit=1) + if not user: + return self.response([]) + order_line = [] for product in products: order_line.append({ @@ -40,7 +44,15 @@ class Voucher(controller.Controller): results = [] for voucher in vouchers: - if not voucher.can_used: + if voucher.limit > 0 and voucher.count_order >= voucher.limit: + 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) -- cgit v1.2.3 From 5c5c0bbcb7ad09a8951b8ee2800b73e7a2ab8c8d Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 7 Sep 2023 17:12:02 +0700 Subject: Update clean term condition when empty and generate default on get voucher API --- indoteknik_api/controllers/api_v1/voucher.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'indoteknik_api/controllers/api_v1/voucher.py') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index e176803d..a76d57e6 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): @@ -56,7 +57,6 @@ class Voucher(controller.Controller): 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'] @@ -66,7 +66,10 @@ class Voucher(controller.Controller): voucher_res = voucher.format() voucher_res['can_apply'] = can_apply voucher_res['discount_voucher'] = voucher_discount - voucher_res['terms_conditions'] = voucher_tnc + + cleaned_tnc = BeautifulSoup(voucher.terms_conditions, "html.parser").get_text() + voucher_res['terms_conditions'] = voucher.terms_conditions if cleaned_tnc else voucher.generate_tnc() + results.append(voucher_res) sorted_results = sorted(results, key=lambda x: x['can_apply'], reverse=True) -- cgit v1.2.3 From 9a630354c1d00e218595db9b2e8cd0b7df9ed97c Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 11 Sep 2023 13:10:24 +0700 Subject: Fix feature - Fix voucher API - Add field shipping status on stock picking --- indoteknik_api/controllers/api_v1/voucher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indoteknik_api/controllers/api_v1/voucher.py') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index a76d57e6..dfe9ceba 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -67,7 +67,7 @@ class Voucher(controller.Controller): voucher_res['can_apply'] = can_apply voucher_res['discount_voucher'] = voucher_discount - cleaned_tnc = BeautifulSoup(voucher.terms_conditions, "html.parser").get_text() + 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() results.append(voucher_res) -- cgit v1.2.3