From bc6bc85f455c4b8bc9f73b779b521faa5fcdcf96 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 15 Jun 2023 16:48:29 +0700 Subject: Update user cart model and API --- indoteknik_custom/models/website_user_cart.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'indoteknik_custom/models/website_user_cart.py') diff --git a/indoteknik_custom/models/website_user_cart.py b/indoteknik_custom/models/website_user_cart.py index 8046469f..dcd9fa5a 100644 --- a/indoteknik_custom/models/website_user_cart.py +++ b/indoteknik_custom/models/website_user_cart.py @@ -5,6 +5,27 @@ class WebsiteUserCart(models.Model): _name = 'website.user.cart' _rec_name = 'user_id' - user_id = fields.Many2one('res.users', string='User', help="User ID yang terdaftar di table res.users") - product_id = fields.Many2one('product.product', string='Product', help="Product yang terdaftar di table product.product") + user_id = fields.Many2one('res.users', string='User') + product_id = fields.Many2one('product.product', string='Product') + program_line_id = fields.Many2one('promotion.program.line', string='Program', help="Apply program") qty = fields.Float(string='Quantity', digits='Product Unit of Measure') + is_selected = fields.Boolean(string='Selected?', digits='Is selected to process checkout') + + def get_product(self): + product_product = self.env['product.product'] + product = product_product.v2_api_single_response(self.product_id) + product['quantity'] = self.qty + product['subtotal'] = self.qty * product['price']['price_discount'] + product['selected'] = self.is_selected + return product + + def get_product_by_user(self, user_id, selected = False): + user_id = int(user_id) + parameters = [('user_id', '=', user_id)] + if selected: + parameters.append(('is_selected', '=', True)) + carts = self.search(parameters) + products = [] + for cart in carts: + products.append(cart.get_product()) + return products \ No newline at end of file -- cgit v1.2.3 From 3ed54712ca9856f3be937f8325db030d0796532e Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 23 Jun 2023 14:19:38 +0700 Subject: Refactor pricelist on product price --- indoteknik_custom/models/website_user_cart.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'indoteknik_custom/models/website_user_cart.py') diff --git a/indoteknik_custom/models/website_user_cart.py b/indoteknik_custom/models/website_user_cart.py index dcd9fa5a..388151ab 100644 --- a/indoteknik_custom/models/website_user_cart.py +++ b/indoteknik_custom/models/website_user_cart.py @@ -12,12 +12,22 @@ class WebsiteUserCart(models.Model): is_selected = fields.Boolean(string='Selected?', digits='Is selected to process checkout') def get_product(self): + user_data = { + 'partner_id': self.user_id.partner_id.id, + 'user_id': self.user_id.id + } product_product = self.env['product.product'] product = product_product.v2_api_single_response(self.product_id) product['quantity'] = self.qty product['subtotal'] = self.qty * product['price']['price_discount'] product['selected'] = self.is_selected + product['program'] = None + if self.program_line_id: + product['program'] = self.program_line_id.res_format_cart(user_data) return product + + def get_products(self): + return [x.get_product() for x in self] def get_product_by_user(self, user_id, selected = False): user_id = int(user_id) @@ -25,7 +35,5 @@ class WebsiteUserCart(models.Model): if selected: parameters.append(('is_selected', '=', True)) carts = self.search(parameters) - products = [] - for cart in carts: - products.append(cart.get_product()) + products = carts.get_products() return products \ No newline at end of file -- cgit v1.2.3 From d966917a5ba95074b6773f49fcb2c3c924296029 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Tue, 18 Jul 2023 16:38:29 +0700 Subject: Fix lost merge voucher with promotion program --- indoteknik_custom/models/website_user_cart.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'indoteknik_custom/models/website_user_cart.py') diff --git a/indoteknik_custom/models/website_user_cart.py b/indoteknik_custom/models/website_user_cart.py index 388151ab..a8cde228 100644 --- a/indoteknik_custom/models/website_user_cart.py +++ b/indoteknik_custom/models/website_user_cart.py @@ -16,14 +16,21 @@ class WebsiteUserCart(models.Model): 'partner_id': self.user_id.partner_id.id, 'user_id': self.user_id.id } - product_product = self.env['product.product'] - product = product_product.v2_api_single_response(self.product_id) + product = self.product_id.v2_api_single_response(self.product_id) + product['cart_id'] = self.id product['quantity'] = self.qty product['subtotal'] = self.qty * product['price']['price_discount'] product['selected'] = self.is_selected product['program'] = None + product['can_buy'] = True if self.program_line_id: - product['program'] = self.program_line_id.res_format_cart(user_data) + product['program'] = self.program_line_id.res_format_cart(user=user_data, quantity=self.qty) + + if product['program']: + if self.qty < product['program']['minimum_purchase_qty'] or self.qty > product['program']['remaining_qty']['transaction']: + product['can_buy'] = False + product['price'] = product['program']['price'] + return product def get_products(self): -- cgit v1.2.3 From 7b76facc6b7fdbff83a98a1b3251f1d3dad48937 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 20 Jul 2023 10:42:32 +0700 Subject: Add get_user_checkout on user cart --- indoteknik_custom/models/website_user_cart.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'indoteknik_custom/models/website_user_cart.py') diff --git a/indoteknik_custom/models/website_user_cart.py b/indoteknik_custom/models/website_user_cart.py index a8cde228..802dbd37 100644 --- a/indoteknik_custom/models/website_user_cart.py +++ b/indoteknik_custom/models/website_user_cart.py @@ -43,4 +43,27 @@ class WebsiteUserCart(models.Model): parameters.append(('is_selected', '=', True)) carts = self.search(parameters) products = carts.get_products() - return products \ No newline at end of file + return products + + def get_user_checkout(self, user_id): + products = self.get_product_by_user(user_id=user_id, selected=True) + total_purchase = sum(x['price']['price'] * x['quantity'] for x in products) + total_discount = sum((x['price']['price'] - x['price']['price_discount']) * x['quantity'] for x in products) + subtotal = total_purchase - total_discount + tax = round(subtotal * 0.11) + grand_total = subtotal + tax + total_weight = sum(x['weight'] * x['quantity'] for x in products) + result = { + 'total_purchase': total_purchase, + 'total_discount': total_discount, + 'subtotal': subtotal, + 'tax': tax, + 'grand_total': round(grand_total), + 'total_weight': { + 'kg': total_weight, + 'g': total_weight * 1000 + }, + 'has_product_without_weight': any(not product.get('weight') or product.get('weight') == 0 for product in products), + 'products': products + } + return result \ No newline at end of file -- cgit v1.2.3 From 3c9ef63acb42298d948ee86407d9a5ca67004246 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 20 Jul 2023 11:20:21 +0700 Subject: Add discount_voucher on get checkout data API --- indoteknik_custom/models/website_user_cart.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'indoteknik_custom/models/website_user_cart.py') diff --git a/indoteknik_custom/models/website_user_cart.py b/indoteknik_custom/models/website_user_cart.py index 802dbd37..29bf4291 100644 --- a/indoteknik_custom/models/website_user_cart.py +++ b/indoteknik_custom/models/website_user_cart.py @@ -45,17 +45,22 @@ class WebsiteUserCart(models.Model): products = carts.get_products() return products - def get_user_checkout(self, user_id): + def get_user_checkout(self, user_id, voucher=False): products = self.get_product_by_user(user_id=user_id, selected=True) total_purchase = sum(x['price']['price'] * x['quantity'] for x in products) total_discount = sum((x['price']['price'] - x['price']['price_discount']) * x['quantity'] for x in products) subtotal = total_purchase - total_discount + discount_voucher = 0 + if voucher: + discount_voucher = voucher.calculate_discount(subtotal) + subtotal -= discount_voucher tax = round(subtotal * 0.11) grand_total = subtotal + tax total_weight = sum(x['weight'] * x['quantity'] for x in products) result = { 'total_purchase': total_purchase, 'total_discount': total_discount, + 'discount_voucher': discount_voucher, 'subtotal': subtotal, 'tax': tax, 'grand_total': round(grand_total), -- cgit v1.2.3