From db7481a490b87e3a1768112395bf096b93969562 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Wed, 7 Jun 2023 13:58:24 +0700 Subject: Add promotion program keyword and api homepage promotion --- indoteknik_api/controllers/api_v1/promotion.py | 66 +++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py index b137fe2e..5ee2acb1 100644 --- a/indoteknik_api/controllers/api_v1/promotion.py +++ b/indoteknik_api/controllers/api_v1/promotion.py @@ -1,7 +1,7 @@ from .. import controller from odoo import http from odoo.http import request -import ast +from datetime import datetime class Promotion(controller.Controller): @@ -26,4 +26,66 @@ class Promotion(controller.Controller): } return self.response(data) - \ No newline at end of file + + @http.route(prefix + 'promotion/home', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def v1_get_promotion_home(self): + current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + programs = request.env['promotion.program'].search([ + ('start_time', '<=', current_time), + ('end_time', '>=', current_time), + ]) + if not programs: + return self.response(None) + + data = [] + for program in programs: + data_program = { + 'id': program.id, + 'name': program.name, + 'banner': request.env['ir.attachment'].api_image('promotion.program', 'banner', program.id), + 'icon': request.env['ir.attachment'].api_image('promotion.program', 'icon', program.id) + } + data.append(data_program) + + return self.response(data) + + @http.route(prefix + 'promotion/home/', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def v1_get_promotion_home_detail(self, id): + program_lines = request.env['promotion.program.line'].search([ + ('display_on_homepage', '=', True), + ('promotion_type', '=', 'specific_product'), + ('program_id', '=', int(id)) + ]) + pricelist = self.user_pricelist() + data = [] + for line in program_lines: + product = request.env['product.product'].v2_api_single_response(line.product_id, pricelist=pricelist) + product_template = line.product_id.product_tmpl_id + + product.update({ + 'id': product['parent']['id'], + 'image': product['parent']['image'], + 'name': product['parent']['name'], + 'variant_total': len(product_template.product_variant_ids), + 'lowest_price': product['price'], + 'stock_total': product['stock'], + 'icon': { + 'top': request.env['ir.attachment'].api_image('promotion.program', 'icon_top', line.program_id.id), + 'bottom': request.env['ir.attachment'].api_image('promotion.program', 'icon_bottom', line.program_id.id) + } + }) + price = product['lowest_price']['price'] + if line.discount_type == 'percentage': + product['lowest_price']['discount_percentage'] = line.discount_amount + product['lowest_price']['price_discount'] = price - (price * line.discount_amount / 100) + if line.discount_type == 'fixed_price': + product['lowest_price']['price_discount'] = line.discount_amount + product['lowest_price']['discount_percentage'] = (price - line.discount_amount) / price * 100 + + product.pop('parent', None) + product.pop('price', None) + product.pop('stock', None) + data.append(product) + return self.response(data) \ No newline at end of file -- cgit v1.2.3 From 1f2995a85428ac4335123bd33d48ae17d3c9f36f Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 12 Jun 2023 08:22:10 +0700 Subject: Update promotion program feature --- indoteknik_api/controllers/api_v1/promotion.py | 52 +++++++++++++++++++++----- 1 file changed, 42 insertions(+), 10 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py index 5ee2acb1..991a3eb9 100644 --- a/indoteknik_api/controllers/api_v1/promotion.py +++ b/indoteknik_api/controllers/api_v1/promotion.py @@ -6,7 +6,8 @@ from datetime import datetime class Promotion(controller.Controller): prefix = '/api/v1/' - + + @http.route(prefix + 'promotion/', auth='public', methods=['GET']) @controller.Controller.must_authorized() def get_promotion_by_id(self, **kw): @@ -14,19 +15,21 @@ class Promotion(controller.Controller): id = kw.get('id') if not id: return self.response(code=400, description='id is required') - + data = {} id = int(id) - coupon_program = request.env['coupon.program'].search([('id', '=', id)]) + coupon_program = request.env['coupon.program'].search( + [('id', '=', id)]) if coupon_program: data = { 'banner': base_url + 'api/image/coupon.program/x_studio_banner_promo/' + str(coupon_program.id) if coupon_program.x_studio_banner_promo else '', 'image': base_url + 'api/image/coupon.program/x_studio_image_promo/' + str(coupon_program.id) if coupon_program.x_studio_image_promo else '', 'name': coupon_program.name, } - + return self.response(data) - + + @http.route(prefix + 'promotion/home', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() def v1_get_promotion_home(self): @@ -49,13 +52,14 @@ class Promotion(controller.Controller): data.append(data_program) return self.response(data) - + + @http.route(prefix + 'promotion/home/', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() def v1_get_promotion_home_detail(self, id): program_lines = request.env['promotion.program.line'].search([ ('display_on_homepage', '=', True), - ('promotion_type', '=', 'specific_product'), + ('promotion_type', '=', 'special_price'), ('program_id', '=', int(id)) ]) pricelist = self.user_pricelist() @@ -82,10 +86,38 @@ class Promotion(controller.Controller): product['lowest_price']['price_discount'] = price - (price * line.discount_amount / 100) if line.discount_type == 'fixed_price': product['lowest_price']['price_discount'] = line.discount_amount - product['lowest_price']['discount_percentage'] = (price - line.discount_amount) / price * 100 - + product['lowest_price']['discount_percentage'] = round((price - line.discount_amount) / price * 100) + product.pop('parent', None) product.pop('price', None) product.pop('stock', None) data.append(product) - return self.response(data) \ No newline at end of file + return self.response(data) + + + @http.route(prefix + 'promotion/product/', auth='public', methods=['GET', 'OPTIONS']) + def v1_get_promotion_by_product_id(self, id): + id = int(id) + current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + product = request.env['product.template'].search([('id', '=', id)]) + variant_ids = [variant.id for variant in product.product_variant_ids] + program_lines = request.env['promotion.program.line'].search([ + ('program_id.start_time', '<=', current_time), + ('program_id.end_time', '>=', current_time), + ('product_id.id', 'in', variant_ids) + ]) + + data = [] + ir_attachment = request.env['ir.attachment'] + for line in program_lines: + data.append({ + 'name': line.name, + 'image': ir_attachment.api_image('promotion.program.line', 'image', line.id), + 'type': line.promotion_type, + 'minimum_purchase_qty': line.minimum_purchase_qty, + 'applies_multiply': line.applies_multiply, + 'limit_qty': line.limit_qty, + 'limit_qty_user': line.limit_qty_user, + 'limit_qty_transaction': line.limit_qty_transaction, + }) + return self.response(data) -- cgit v1.2.3 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_api/controllers/api_v1/cart.py | 36 +++++++++++++------------ indoteknik_api/controllers/api_v1/sale_order.py | 20 ++++++++++++++ 2 files changed, 39 insertions(+), 17 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py index a8628432..0d54ecbc 100644 --- a/indoteknik_api/controllers/api_v1/cart.py +++ b/indoteknik_api/controllers/api_v1/cart.py @@ -5,32 +5,30 @@ from odoo.http import request class Cart(controller.Controller): prefix = '/api/v1/' + PREFIX_USER = prefix + 'user//' - @http.route(prefix + 'cart', auth='public', methods=['GET']) + @http.route(PREFIX_USER + 'cart', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() - def get_cart_by_user_id(self, **kw): - user_id = int(kw.get('user_id', 0)) + def get_cart_by_user_id(self, user_id, **kw): + user_id = int(user_id) limit = int(kw.get('limit', 0)) offset = int(kw.get('offset', 0)) query = [('user_id', '=', user_id)] carts = request.env['website.user.cart'].search(query, limit=limit, offset=offset, order='create_date desc') data = { 'product_total': request.env['website.user.cart'].search_count(query), - 'products': [] + 'products': [cart.get_product() for cart in carts] } - for cart in carts: - product = request.env['product.product'].api_single_response(cart.product_id) - product['template_id'] = cart.product_id.product_tmpl_id.id - product['quantity'] = cart.qty - data['products'].append(product) return self.response(data) - @http.route(prefix + 'cart/create-or-update', auth='public', methods=['POST'], csrf=False) + @http.route(PREFIX_USER + 'cart/create-or-update', auth='public', methods=['POST', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized() - def create_or_update_cart(self, **kw): - user_id = int(kw.get('user_id', 0)) + def create_or_update_cart(self, user_id, **kw): + user_id = int(user_id) product_id = int(kw.get('product_id', 0)) qty = int(kw.get('qty', 0)) + is_selected = int(kw.get('is_selected', False)) + program_line_id = int(kw.get('program_line_id', False)) if not user_id or not product_id or not qty: return self.response(code=400, description='user_id, product_id and qty is required') @@ -38,22 +36,26 @@ class Cart(controller.Controller): cart = request.env['website.user.cart'].search(query, limit=1) result = {} if cart: - cart.write({'qty': qty}) + data_to_update = {'qty': qty, 'is_selected': is_selected} + if program_line_id: + data_to_update['program_line_id'] = program_line_id + cart.write(data_to_update) result['id'] = cart.id else: create = request.env['website.user.cart'].create({ 'user_id': user_id, 'product_id': product_id, - 'qty': qty + 'qty': qty, + 'is_selected': is_selected }) result['id'] = create.id return self.response(result) - @http.route(prefix + 'cart', auth='public', methods=['DELETE'], csrf=False) + @http.route(PREFIX_USER + 'cart', auth='public', methods=['DELETE', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized() - def delete_cart_by_user_id(self, **kw): - user_id = int(kw.get('user_id', 0)) + def delete_cart_by_user_id(self, user_id, **kw): + user_id = int(user_id) query = [('user_id', '=', user_id)] product_ids = kw.get('product_ids') if product_ids: diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index 35361ba4..9df710ec 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -218,6 +218,26 @@ class SaleOrder(controller.Controller): data = sale_order.id return self.response(data) + @http.route(prefix + 'user//sale_order/checkout', auth='public', method=['GET', 'OPTIONS'], csrf=False) + @controller.Controller.must_authorized(private=True, private_key='user_id') + def get_user_checkout_so(self, user_id): + cart = request.env['website.user.cart'] + products = cart.get_product_by_user(user_id=user_id, selected=True) + total_purchase = sum(x['price']['price'] for x in products) + total_discount = sum((x['price']['price'] - x['price']['price_discount']) for x in products) + subtotal = total_purchase - total_discount + tax = round(subtotal * 0.11) + grand_total = subtotal + tax + result = { + 'total_purchase': total_purchase, + 'total_discount': total_discount, + 'subtotal': subtotal, + 'tax': tax, + 'grand_total': round(grand_total), + 'products': products + } + return self.response(result) + @http.route(PREFIX_PARTNER + 'sale_order/checkout', auth='public', method=['POST', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized(private=True, private_key='partner_id') def create_partner_sale_order(self, **kw): -- cgit v1.2.3 From 12c821732b31ff68dec7e0a7a8390466b4181692 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 16 Jun 2023 11:08:21 +0700 Subject: Fix add to cart api --- indoteknik_api/controllers/api_v1/cart.py | 4 +++- indoteknik_api/controllers/api_v1/sale_order.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py index 0d54ecbc..002b7442 100644 --- a/indoteknik_api/controllers/api_v1/cart.py +++ b/indoteknik_api/controllers/api_v1/cart.py @@ -27,9 +27,11 @@ class Cart(controller.Controller): user_id = int(user_id) product_id = int(kw.get('product_id', 0)) qty = int(kw.get('qty', 0)) - is_selected = int(kw.get('is_selected', False)) + is_selected = kw.get('selected', False) program_line_id = int(kw.get('program_line_id', False)) + if is_selected: + is_selected = True if is_selected == 'true' else False if not user_id or not product_id or not qty: return self.response(code=400, description='user_id, product_id and qty is required') query = [('user_id', '=', user_id), ('product_id', '=', product_id)] diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index 9df710ec..89e96206 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -266,7 +266,7 @@ class SaleOrder(controller.Controller): parameters = { 'warehouse_id': 8, 'carrier_id': 1, - 'sales_tax_id': 21, + 'sales_tax_id': 23, 'pricelist_id': product_pricelist_default_discount_id, 'payment_term_id': 26, 'team_id': 2, -- cgit v1.2.3 From 09458dba5519f01eadf5f9ca4f22a30dad98d5c7 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 19 Jun 2023 15:13:31 +0700 Subject: Add get cart count API --- indoteknik_api/controllers/api_v1/cart.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py index 002b7442..93d7b2f1 100644 --- a/indoteknik_api/controllers/api_v1/cart.py +++ b/indoteknik_api/controllers/api_v1/cart.py @@ -21,6 +21,14 @@ class Cart(controller.Controller): } return self.response(data) + @http.route(PREFIX_USER + 'cart/count', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def get_cart_count_by_user_id(self, user_id, **kw): + user_id = int(user_id) + query = [('user_id', '=', user_id)] + carts = request.env['website.user.cart'].search_count(query) + return self.response(carts) + @http.route(PREFIX_USER + 'cart/create-or-update', auth='public', methods=['POST', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized() def create_or_update_cart(self, user_id, **kw): -- cgit v1.2.3 From 4e9a5a025012afe63133524eeea8987b9af9e050 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 19 Jun 2023 15:13:55 +0700 Subject: Add response on get user checkout data API --- indoteknik_api/controllers/api_v1/sale_order.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index 89e96206..406ab577 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -223,17 +223,23 @@ class SaleOrder(controller.Controller): def get_user_checkout_so(self, user_id): cart = request.env['website.user.cart'] products = cart.get_product_by_user(user_id=user_id, selected=True) - total_purchase = sum(x['price']['price'] for x in products) - total_discount = sum((x['price']['price'] - x['price']['price_discount']) for x in products) + 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 self.response(result) -- cgit v1.2.3 From 329668e82ca9e4ebd2ee93d6670380abf6ed6377 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 22 Jun 2023 13:43:42 +0700 Subject: Add program line to order line relation, update remaining_qty response on get product promotion api, show order line on program line --- .../controllers/api_v1/product_variant.py | 12 ++++++++++ indoteknik_api/controllers/api_v1/promotion.py | 27 ---------------------- 2 files changed, 12 insertions(+), 27 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/product_variant.py b/indoteknik_api/controllers/api_v1/product_variant.py index 999ced6f..cec54fda 100644 --- a/indoteknik_api/controllers/api_v1/product_variant.py +++ b/indoteknik_api/controllers/api_v1/product_variant.py @@ -2,6 +2,7 @@ from .. import controller from odoo import http from odoo.http import request + class ProductVariant(controller.Controller): prefix = '/api/v1/' @@ -20,3 +21,14 @@ class ProductVariant(controller.Controller): return self.response(data) + @http.route(prefix + 'product_variant//promotions', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def get_product_variant_promotions(self, product_id): + product_id = int(product_id) + user_data = self.verify_user_token() + + program_line = request.env['promotion.program.line'] + program_lines = program_line.get_active_promotions(product_id) + program_lines = program_line.res_format(program_lines, user_data) + + return self.response(program_lines) diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py index 991a3eb9..948681fd 100644 --- a/indoteknik_api/controllers/api_v1/promotion.py +++ b/indoteknik_api/controllers/api_v1/promotion.py @@ -94,30 +94,3 @@ class Promotion(controller.Controller): data.append(product) return self.response(data) - - @http.route(prefix + 'promotion/product/', auth='public', methods=['GET', 'OPTIONS']) - def v1_get_promotion_by_product_id(self, id): - id = int(id) - current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') - product = request.env['product.template'].search([('id', '=', id)]) - variant_ids = [variant.id for variant in product.product_variant_ids] - program_lines = request.env['promotion.program.line'].search([ - ('program_id.start_time', '<=', current_time), - ('program_id.end_time', '>=', current_time), - ('product_id.id', 'in', variant_ids) - ]) - - data = [] - ir_attachment = request.env['ir.attachment'] - for line in program_lines: - data.append({ - 'name': line.name, - 'image': ir_attachment.api_image('promotion.program.line', 'image', line.id), - 'type': line.promotion_type, - 'minimum_purchase_qty': line.minimum_purchase_qty, - 'applies_multiply': line.applies_multiply, - 'limit_qty': line.limit_qty, - 'limit_qty_user': line.limit_qty_user, - 'limit_qty_transaction': line.limit_qty_transaction, - }) - return self.response(data) -- cgit v1.2.3 From 23014336a1fe1fe5ef54fad30cf6c3d9cc59b2d8 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 23 Jun 2023 11:20:52 +0700 Subject: Update response price di product promotion API, Membuat fungsi calculate price promotion, Refactor promotion homepage, Refactor fungsi calculate product price --- indoteknik_api/controllers/api_v1/product_variant.py | 3 ++- indoteknik_api/controllers/api_v1/promotion.py | 9 +-------- 2 files changed, 3 insertions(+), 9 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/product_variant.py b/indoteknik_api/controllers/api_v1/product_variant.py index cec54fda..06ce5d3c 100644 --- a/indoteknik_api/controllers/api_v1/product_variant.py +++ b/indoteknik_api/controllers/api_v1/product_variant.py @@ -26,9 +26,10 @@ class ProductVariant(controller.Controller): def get_product_variant_promotions(self, product_id): product_id = int(product_id) user_data = self.verify_user_token() + pricelist = self.user_pricelist() program_line = request.env['promotion.program.line'] program_lines = program_line.get_active_promotions(product_id) - program_lines = program_line.res_format(program_lines, user_data) + program_lines = program_lines.res_format(user=user_data, pricelist=pricelist) return self.response(program_lines) diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py index 948681fd..a3dbf0ba 100644 --- a/indoteknik_api/controllers/api_v1/promotion.py +++ b/indoteknik_api/controllers/api_v1/promotion.py @@ -73,20 +73,13 @@ class Promotion(controller.Controller): 'image': product['parent']['image'], 'name': product['parent']['name'], 'variant_total': len(product_template.product_variant_ids), - 'lowest_price': product['price'], + 'lowest_price': line.calculate_price(product['price']), 'stock_total': product['stock'], 'icon': { 'top': request.env['ir.attachment'].api_image('promotion.program', 'icon_top', line.program_id.id), 'bottom': request.env['ir.attachment'].api_image('promotion.program', 'icon_bottom', line.program_id.id) } }) - price = product['lowest_price']['price'] - if line.discount_type == 'percentage': - product['lowest_price']['discount_percentage'] = line.discount_amount - product['lowest_price']['price_discount'] = price - (price * line.discount_amount / 100) - if line.discount_type == 'fixed_price': - product['lowest_price']['price_discount'] = line.discount_amount - product['lowest_price']['discount_percentage'] = round((price - line.discount_amount) / price * 100) product.pop('parent', None) product.pop('price', None) -- 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_api/controllers/api_v1/cart.py | 7 ++++--- indoteknik_api/controllers/api_v1/product_variant.py | 3 +-- indoteknik_api/controllers/api_v1/promotion.py | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py index 93d7b2f1..035a40b7 100644 --- a/indoteknik_api/controllers/api_v1/cart.py +++ b/indoteknik_api/controllers/api_v1/cart.py @@ -10,14 +10,15 @@ class Cart(controller.Controller): @http.route(PREFIX_USER + 'cart', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() def get_cart_by_user_id(self, user_id, **kw): + user_cart = request.env['website.user.cart'] user_id = int(user_id) limit = int(kw.get('limit', 0)) offset = int(kw.get('offset', 0)) query = [('user_id', '=', user_id)] - carts = request.env['website.user.cart'].search(query, limit=limit, offset=offset, order='create_date desc') + carts = user_cart.search(query, limit=limit, offset=offset, order='create_date desc') data = { - 'product_total': request.env['website.user.cart'].search_count(query), - 'products': [cart.get_product() for cart in carts] + 'product_total': user_cart.search_count(query), + 'products': carts.get_products() } return self.response(data) diff --git a/indoteknik_api/controllers/api_v1/product_variant.py b/indoteknik_api/controllers/api_v1/product_variant.py index 06ce5d3c..8de4669e 100644 --- a/indoteknik_api/controllers/api_v1/product_variant.py +++ b/indoteknik_api/controllers/api_v1/product_variant.py @@ -26,10 +26,9 @@ class ProductVariant(controller.Controller): def get_product_variant_promotions(self, product_id): product_id = int(product_id) user_data = self.verify_user_token() - pricelist = self.user_pricelist() program_line = request.env['promotion.program.line'] program_lines = program_line.get_active_promotions(product_id) - program_lines = program_lines.res_format(user=user_data, pricelist=pricelist) + program_lines = program_lines.res_format(user=user_data) return self.response(program_lines) diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py index a3dbf0ba..68a23ef2 100644 --- a/indoteknik_api/controllers/api_v1/promotion.py +++ b/indoteknik_api/controllers/api_v1/promotion.py @@ -62,10 +62,10 @@ class Promotion(controller.Controller): ('promotion_type', '=', 'special_price'), ('program_id', '=', int(id)) ]) - pricelist = self.user_pricelist() + data = [] for line in program_lines: - product = request.env['product.product'].v2_api_single_response(line.product_id, pricelist=pricelist) + product = request.env['product.product'].v2_api_single_response(line.product_id) product_template = line.product_id.product_tmpl_id product.update({ -- cgit v1.2.3 From 0aaf2d3af0e5096514f8b72850f6a3d2901cfc3e Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Tue, 18 Jul 2023 11:56:33 +0700 Subject: refactor product real stock --- indoteknik_api/controllers/api_v1/product.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 8803bae3..8411ee25 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -18,11 +18,9 @@ class Product(controller.Controller): id = int(kw.get('id')) product = request.env['product.product'].search([('id', '=', id)], limit=1) product_sla = request.env['product.sla'].search([('product_variant_id', '=', id)], limit=1) + stock_vendor = request.env['stock.vendor'].search([('product_variant_id', '=', id)], limit=1) qty_available = product.qty_onhand_bandengan - qty_available -= 10 - if qty_available < 10: - qty_available = 0 qty = 0 sla_date = '-' @@ -33,8 +31,8 @@ class Product(controller.Controller): qty_altama = request.env['product.template'].get_stock_altama(product.default_code) qty_altama -= int(qty_altama * 0.1) qty_altama = math.ceil(float(qty_altama)) - qty = qty_altama - if qty_available > 10: + qty = qty_altama + if qty_available > 0: qty += qty_available sla_date = '1 Hari' elif qty_altama > 0: -- cgit v1.2.3 From e0102841e6e21c7b583f096914aa4ba1a28e1587 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Tue, 18 Jul 2023 14:00:34 +0700 Subject: Merge sale order voucher with promotion program --- indoteknik_api/controllers/api_v1/sale_order.py | 96 ++++++++++++------------- 1 file changed, 44 insertions(+), 52 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index 7c38d47d..a2e28cda 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -262,16 +262,10 @@ class SaleOrder(controller.Controller): @controller.Controller.must_authorized(private=True, private_key='partner_id') def create_partner_sale_order(self, **kw): config = request.env['ir.config_parameter'] - product_pricelist_default_discount_id = int( - config.get_param('product.pricelist.default_discount_id')) - product_pricelist_tier1 = int( - config.get_param('product.pricelist.tier1')) - product_pricelist_tier2 = int( - config.get_param('product.pricelist.tier2')) - product_pricelist_tier3 = int( - config.get_param('product.pricelist.tier3')) + product_pricelist_default_discount_id = int(config.get_param('product.pricelist.default_discount_id')) params = self.get_request_params(kw, { + 'user_id': ['number'], 'partner_id': ['number'], 'partner_shipping_id': ['required', 'number'], 'partner_invoice_id': ['required', 'number'], @@ -281,10 +275,9 @@ class SaleOrder(controller.Controller): 'type': [], 'delivery_amount': ['number', 'default:0'], 'carrier_id': [], - 'delivery_service_type': [], - 'voucher': [] + 'delivery_service_type': [] }) - + if not params['valid']: return self.response(code=400, description=params) @@ -316,51 +309,50 @@ class SaleOrder(controller.Controller): if params['value']['type'] == 'sale_order': parameters['approval_status'] = 'pengajuan1' sale_order = request.env['sale.order'].create([parameters]) - order_line = json.loads(params['value']['order_line']) parameters = [] - partner = request.env['res.partner'].browse( - params['value']['partner_id']) - partner_pricelist = partner.property_product_pricelist - for line in order_line: - product = request.env['product.product'].search( - [('id', '=', line['product_id'])], limit=1) - discount = product._get_website_disc(0) - - price_tier = False - pricelist = { - 'tier1': product._get_pricelist_tier1, - 'tier2': product._get_pricelist_tier2, - 'tier3': product._get_pricelist_tier3, - } - partner_pricelist_id = partner_pricelist.id if partner_pricelist else False - if partner_pricelist_id == product_pricelist_tier1: - price_tier = 'tier1' - if partner_pricelist_id == product_pricelist_tier2: - price_tier = 'tier2' - if partner_pricelist_id == product_pricelist_tier3: - price_tier = 'tier3' - - if price_tier: - price = pricelist[price_tier]() - discount_key = 'discount_%s' % price_tier - if price[discount_key] > 0: - discount = price[discount_key] - - flashsale = product._get_flashsale_price() - flashsale_discount = flashsale['flashsale_discount'] - if flashsale_discount > 0 and flashsale_discount > discount: - discount = flashsale_discount - - parameters.append({ + user_id = params['value']['user_id'] + user_cart = request.env['website.user.cart'] + products = user_cart.get_product_by_user(user_id=user_id, selected=True) + for product in products: + total_qty = product['quantity'] + price_unit = product['price']['price'] + price_discount =product['price']['discount_percentage'] + if product['program'] and product['program']['type']['value'] != 'special_price': + total_qty += sum(x['quantity'] for x in product['program']['items']) + price_unit = product['subtotal'] / total_qty + price_discount = 0 + + param = { 'company_id': 1, 'order_id': sale_order.id, - 'product_id': line['product_id'], - 'product_uom_qty': line['quantity'], - 'price_unit': product._get_website_price_exclude_tax(), - 'discount': discount - }) + 'price_unit': price_unit, + 'discount': price_discount + } + primary_product = { + **param, + 'product_id': product['id'], + 'product_uom_qty': product['quantity'] + } + if product['program']: + primary_product.update({ + 'program_line_id': product['program']['id'] + }) + parameters.append(primary_product) + + if not product['program']: + continue + + for item in product['program']['items']: + parameters.append({ + **param, + 'product_id': item['id'], + 'product_uom_qty': item['quantity'], + }) + + cart_ids = [x['cart_id'] for x in products] + user_cart.browse(cart_ids).unlink() request.env['sale.order.line'].create(parameters) amount_untaxed = sale_order.amount_untaxed @@ -380,7 +372,7 @@ class SaleOrder(controller.Controller): price = line.price_unit - voucher_discount_line line.price_unit = price line.amount_voucher_disc = voucher_discount_item * line.product_uom_qty - + return self.response({ 'id': sale_order.id, 'name': sale_order.name -- 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_api/controllers/api_v1/cart.py | 14 ++++++++------ indoteknik_api/controllers/api_v1/promotion.py | 6 ++++-- indoteknik_api/controllers/api_v1/sale_order.py | 3 ++- 3 files changed, 14 insertions(+), 9 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py index 035a40b7..0265ec57 100644 --- a/indoteknik_api/controllers/api_v1/cart.py +++ b/indoteknik_api/controllers/api_v1/cart.py @@ -37,7 +37,8 @@ class Cart(controller.Controller): product_id = int(kw.get('product_id', 0)) qty = int(kw.get('qty', 0)) is_selected = kw.get('selected', False) - program_line_id = int(kw.get('program_line_id', False)) + program_line_id = kw.get('program_line_id', False) + program_line_id = False if program_line_id == 'null' or not program_line_id else int(program_line_id) if is_selected: is_selected = True if is_selected == 'true' else False @@ -46,18 +47,19 @@ class Cart(controller.Controller): query = [('user_id', '=', user_id), ('product_id', '=', product_id)] cart = request.env['website.user.cart'].search(query, limit=1) result = {} + data_to_update = { + 'qty': qty, + 'is_selected': is_selected, + 'program_line_id': program_line_id + } if cart: - data_to_update = {'qty': qty, 'is_selected': is_selected} - if program_line_id: - data_to_update['program_line_id'] = program_line_id cart.write(data_to_update) result['id'] = cart.id else: create = request.env['website.user.cart'].create({ 'user_id': user_id, 'product_id': product_id, - 'qty': qty, - 'is_selected': is_selected + **data_to_update }) result['id'] = create.id diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py index 68a23ef2..f84b8c1c 100644 --- a/indoteknik_api/controllers/api_v1/promotion.py +++ b/indoteknik_api/controllers/api_v1/promotion.py @@ -18,8 +18,7 @@ class Promotion(controller.Controller): data = {} id = int(id) - coupon_program = request.env['coupon.program'].search( - [('id', '=', id)]) + coupon_program = request.env['coupon.program'].search([('id', '=', id)]) if coupon_program: data = { 'banner': base_url + 'api/image/coupon.program/x_studio_banner_promo/' + str(coupon_program.id) if coupon_program.x_studio_banner_promo else '', @@ -37,7 +36,9 @@ class Promotion(controller.Controller): programs = request.env['promotion.program'].search([ ('start_time', '<=', current_time), ('end_time', '>=', current_time), + ('program_line.display_on_homepage', '=', True) ]) + if not programs: return self.response(None) @@ -85,5 +86,6 @@ class Promotion(controller.Controller): product.pop('price', None) product.pop('stock', None) data.append(product) + return self.response(data) diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index a2e28cda..8135da33 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -275,7 +275,8 @@ class SaleOrder(controller.Controller): 'type': [], 'delivery_amount': ['number', 'default:0'], 'carrier_id': [], - 'delivery_service_type': [] + 'delivery_service_type': [], + 'voucher': [] }) if not params['valid']: -- cgit v1.2.3 From 6f898864f5e825a560cc92f2e1f1973defafba51 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 20 Jul 2023 09:09:31 +0700 Subject: Update voucher api can apply to public voucher --- indoteknik_api/controllers/api_v1/voucher.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 0990a1a0..f948183e 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -2,6 +2,7 @@ from .. import controller from odoo import http from odoo.http import request + class Voucher(controller.Controller): prefix = '/api/v1/' @@ -9,14 +10,14 @@ class Voucher(controller.Controller): @controller.Controller.must_authorized() def get_vouchers(self, **kw): code = kw.get('code') - visibility = 'public' + visibility = ['public'] parameter = [] if code: - visibility = 'private' + visibility.append('private') parameter += [('code', '=', code)] - parameter += [('visibility', '=', visibility)] + parameter += [('visibility', 'in', visibility)] vouchers = request.env['voucher'].get_active_voucher(parameter) data = vouchers.res_format() return self.response(data) -- 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_api/controllers/api_v1/sale_order.py | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index 8135da33..29649315 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -236,26 +236,7 @@ class SaleOrder(controller.Controller): @controller.Controller.must_authorized(private=True, private_key='user_id') def get_user_checkout_so(self, user_id): cart = request.env['website.user.cart'] - products = cart.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 - } + result = cart.get_user_checkout(user_id) return self.response(result) @http.route(PREFIX_PARTNER + 'sale_order/checkout', auth='public', method=['POST', 'OPTIONS'], csrf=False) -- cgit v1.2.3 From ed231d1cfa4e78b8f98c5406f88c1f985c0225a7 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 20 Jul 2023 10:43:13 +0700 Subject: Add response on vouccher api - can apply - discount voucher - difference to apply --- indoteknik_api/controllers/api_v1/voucher.py | 36 ++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index f948183e..5cdafba4 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -6,10 +6,12 @@ from odoo.http import request class Voucher(controller.Controller): prefix = '/api/v1/' - @http.route(prefix + 'voucher', auth='public', methods=['GET', 'OPTIONS']) - @controller.Controller.must_authorized() + @http.route(prefix + 'user//voucher', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized(private=True, private_key='user_id') def get_vouchers(self, **kw): + cart = request.env['website.user.cart'] code = kw.get('code') + user_id = kw.get('user_id') visibility = ['public'] parameter = [] @@ -19,5 +21,31 @@ class Voucher(controller.Controller): parameter += [('visibility', 'in', visibility)] vouchers = request.env['voucher'].get_active_voucher(parameter) - data = vouchers.res_format() - return self.response(data) + vouchers = vouchers.res_format() + checkout = cart.get_user_checkout(user_id) + + for voucher in vouchers: + subtotal = checkout['subtotal'] + min_purchase_amount = voucher['min_purchase_amount'] + max_discount_amount = voucher['max_discount_amount'] + discount_type = voucher['discount_type'] + discount_amount = voucher['discount_amount'] + can_apply = subtotal >= min_purchase_amount + difference_to_apply = 0 + if not can_apply: + difference_to_apply = min_purchase_amount - subtotal + + discount_voucher = 0 + if discount_type == 'fixed_price': + discount_voucher = discount_amount + if discount_type == 'percentage': + discount_voucher = subtotal * discount_amount / 100 + + if max_discount_amount > 0 and discount_voucher > max_discount_amount: + discount_voucher = max_discount_amount + + voucher['can_apply'] = can_apply + voucher['discount_voucher'] = discount_voucher + voucher['difference_to_apply'] = difference_to_apply + + return self.response(vouchers) -- 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_api/controllers/api_v1/sale_order.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index 29649315..88a72755 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -234,9 +234,11 @@ class SaleOrder(controller.Controller): @http.route(prefix + 'user//sale_order/checkout', auth='public', method=['GET', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized(private=True, private_key='user_id') - def get_user_checkout_so(self, user_id): + def get_user_checkout_so(self, user_id, **kw): cart = request.env['website.user.cart'] - result = cart.get_user_checkout(user_id) + voucher_code = kw.get('voucher') + voucher = request.env['voucher'].search([('code', '=', voucher_code)], limit=1) + result = cart.get_user_checkout(user_id, voucher) return self.response(result) @http.route(PREFIX_PARTNER + 'sale_order/checkout', auth='public', method=['POST', 'OPTIONS'], csrf=False) -- cgit v1.2.3 From 145b370e5b57206edd1b44400c9ed8a7a3d26c36 Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Thu, 20 Jul 2023 17:56:34 +0700 Subject: update api product sla --- indoteknik_api/controllers/api_v1/product.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 8411ee25..96b0742a 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -13,31 +13,37 @@ class Product(controller.Controller): prefix = '/api/v1/' @http.route(prefix + 'product_variant//stock', auth='public', methods=['GET', 'OPTIONS']) - @controller.Controller.must_authorized() + # @controller.Controller.must_authorized() def get_product_template_stock_by_id(self, **kw): id = int(kw.get('id')) product = request.env['product.product'].search([('id', '=', id)], limit=1) product_sla = request.env['product.sla'].search([('product_variant_id', '=', id)], limit=1) stock_vendor = request.env['stock.vendor'].search([('product_variant_id', '=', id)], limit=1) - + qty_available = product.qty_onhand_bandengan qty = 0 sla_date = '-' + qty_vendor = stock_vendor.quantity + qty_vendor -= int(qty_vendor * 0.1) + qty_vendor = math.ceil(float(qty_vendor)) + total_excell = qty_vendor + is_altama_product = product.x_manufacture.id in [10,122,89] if is_altama_product: try: qty_altama = request.env['product.template'].get_stock_altama(product.default_code) qty_altama -= int(qty_altama * 0.1) qty_altama = math.ceil(float(qty_altama)) - qty = qty_altama + total_adem = qty_altama if qty_available > 0: - qty += qty_available + qty = qty_available + total_adem sla_date = '1 Hari' - elif qty_altama > 0: + elif qty_altama > 0 or qty_vendor > 0: + qty = total_adem if qty_altama > 0 else total_excell sla_date = '2-4 Hari' - else: + else: sla_date = 'Indent' except: print('error') -- cgit v1.2.3 From 22298c2cfdb7d3de1c7a2e85e21444847b65d4ff Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Thu, 20 Jul 2023 17:57:28 +0700 Subject: add must_authorized in api product sla --- indoteknik_api/controllers/api_v1/product.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 96b0742a..4795f1fb 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -13,7 +13,7 @@ class Product(controller.Controller): prefix = '/api/v1/' @http.route(prefix + 'product_variant//stock', auth='public', methods=['GET', 'OPTIONS']) - # @controller.Controller.must_authorized() + @controller.Controller.must_authorized() def get_product_template_stock_by_id(self, **kw): id = int(kw.get('id')) product = request.env['product.product'].search([('id', '=', id)], limit=1) -- cgit v1.2.3 From 81175e09c6fa406ee3269265a91ca859659ef7fe Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Thu, 20 Jul 2023 18:01:30 +0700 Subject: add total_excell to condition --- indoteknik_api/controllers/api_v1/product.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 4795f1fb..87747988 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -38,7 +38,7 @@ class Product(controller.Controller): qty_altama = math.ceil(float(qty_altama)) total_adem = qty_altama if qty_available > 0: - qty = qty_available + total_adem + qty = qty_available + total_adem + total_excell sla_date = '1 Hari' elif qty_altama > 0 or qty_vendor > 0: qty = total_adem if qty_altama > 0 else total_excell -- cgit v1.2.3 From 70c667cb341efa30f29626183bb5dc25654714de Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Fri, 21 Jul 2023 08:20:16 +0700 Subject: update stock vendor --- indoteknik_api/controllers/api_v1/product.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 87747988..53ba7671 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -18,7 +18,7 @@ class Product(controller.Controller): id = int(kw.get('id')) product = request.env['product.product'].search([('id', '=', id)], limit=1) product_sla = request.env['product.sla'].search([('product_variant_id', '=', id)], limit=1) - stock_vendor = request.env['stock.vendor'].search([('product_variant_id', '=', id)], limit=1) + stock_vendor = self.env['stock.vendor'].get_stock_updated_last_7_days(id) qty_available = product.qty_onhand_bandengan -- cgit v1.2.3 From da2389775171d288b96958651a45f07865a2e014 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 21 Jul 2023 11:06:58 +0700 Subject: Update voucher use manufacture rules --- indoteknik_api/controllers/api_v1/voucher.py | 29 +++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 5cdafba4..96c52e1e 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -25,14 +25,36 @@ class Voucher(controller.Controller): checkout = cart.get_user_checkout(user_id) for voucher in vouchers: - subtotal = checkout['subtotal'] + apply_status = '' + products = checkout['products'] min_purchase_amount = voucher['min_purchase_amount'] max_discount_amount = voucher['max_discount_amount'] discount_type = voucher['discount_type'] discount_amount = voucher['discount_amount'] - can_apply = subtotal >= min_purchase_amount + can_apply = True difference_to_apply = 0 - if not can_apply: + + manufacture_ids = [x['id'] for x in voucher['manufactures']] + 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 + + if len(manufacture_ids) == 0 or manufacture_id in manufacture_ids: + purchase_amt = price * quantity + discount_amt = (price - price_discount) * quantity + subtotal += purchase_amt - discount_amt + has_match_manufacture = True + + if not has_match_manufacture: + can_apply = False + apply_status = 'UM' + elif subtotal < min_purchase_amount: + can_apply = False + apply_status = 'MPA' difference_to_apply = min_purchase_amount - subtotal discount_voucher = 0 @@ -45,6 +67,7 @@ class Voucher(controller.Controller): discount_voucher = max_discount_amount voucher['can_apply'] = can_apply + voucher['apply_status'] = apply_status voucher['discount_voucher'] = discount_voucher voucher['difference_to_apply'] = difference_to_apply -- cgit v1.2.3 From f58e8b41012f692b7995026b9869d7c07763b250 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 21 Jul 2023 11:20:34 +0700 Subject: Update voucher manufacture response --- indoteknik_api/controllers/api_v1/voucher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 96c52e1e..454961da 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -34,7 +34,7 @@ class Voucher(controller.Controller): can_apply = True difference_to_apply = 0 - manufacture_ids = [x['id'] for x in voucher['manufactures']] + manufacture_ids = voucher['manufacture_ids'] subtotal = 0 has_match_manufacture = False for product in products: @@ -42,7 +42,7 @@ class Voucher(controller.Controller): 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: purchase_amt = price * quantity discount_amt = (price - price_discount) * quantity -- cgit v1.2.3 From f4c02f0ca17c0c72314334c03417c2f54382dfa1 Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Fri, 21 Jul 2023 15:20:08 +0700 Subject: fix condition --- indoteknik_api/controllers/api_v1/product.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 53ba7671..3667a787 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -44,7 +44,7 @@ class Product(controller.Controller): qty = total_adem if qty_altama > 0 else total_excell sla_date = '2-4 Hari' else: - sla_date = 'Indent' + sla_date = '3-7 Hari' except: print('error') else: -- cgit v1.2.3 From c758c2c63243a01e090a7640839b2dd7ce9476be Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 21 Jul 2023 16:33:22 +0700 Subject: Update contribute pro-rate on create sale order API --- indoteknik_api/controllers/api_v1/sale_order.py | 32 +++++++++++++++---------- 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index 88a72755..6c878197 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -335,28 +335,36 @@ class SaleOrder(controller.Controller): 'product_uom_qty': item['quantity'], }) - cart_ids = [x['cart_id'] for x in products] - user_cart.browse(cart_ids).unlink() request.env['sale.order.line'].create(parameters) - amount_untaxed = sale_order.amount_untaxed - voucher = request.env['voucher'].search([ - ('code', '=', params['value']['voucher']) - ]) + 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 - total_qty = sum(line.product_uom_qty for line in sale_order.order_line) - voucher_discount_item = voucher_discount / total_qty 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_discount_line = line.price_subtotal / amount_untaxed * voucher_discount + line.amount_voucher_disc = voucher_discount_line + discount_decimal = line.discount / 100 - voucher_discount_line = voucher_discount_item / (1 - discount_decimal) - price = line.price_unit - voucher_discount_line - line.price_unit = price - line.amount_voucher_disc = voucher_discount_item * line.product_uom_qty + voucher_discount_item = voucher_discount_line / line.product_uom_qty + voucher_disc_before_line_disc = voucher_discount_item / (1 - discount_decimal) + line.price_unit -= voucher_disc_before_line_disc + cart_ids = [x['cart_id'] for x in products] + user_cart.browse(cart_ids).unlink() return self.response({ 'id': sale_order.id, 'name': sale_order.name -- cgit v1.2.3 From bc0c9e782140d82dc2147afb4a049c37141b081a Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 21 Jul 2023 17:03:15 +0700 Subject: Add exclude voucher pricelist API --- indoteknik_api/controllers/api_v1/voucher.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 454961da..a6a88cad 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -18,6 +18,9 @@ class Voucher(controller.Controller): if code: visibility.append('private') parameter += [('code', '=', code)] + user_pricelist = request.env.user_pricelist + if user_pricelist: + parameter += [('excl_pricelist_ids', 'not in', [user_pricelist.id])] parameter += [('visibility', 'in', visibility)] vouchers = request.env['voucher'].get_active_voucher(parameter) -- cgit v1.2.3 From 2a1179b22cd1b8ee9e3e31c157821dbbeb66195f Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Sat, 22 Jul 2023 11:12:14 +0700 Subject: fix bug api product sla stock vendor --- indoteknik_api/controllers/api_v1/product.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 3667a787..587773ee 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -13,12 +13,14 @@ class Product(controller.Controller): prefix = '/api/v1/' @http.route(prefix + 'product_variant//stock', auth='public', methods=['GET', 'OPTIONS']) - @controller.Controller.must_authorized() + # @controller.Controller.must_authorized() def get_product_template_stock_by_id(self, **kw): id = int(kw.get('id')) product = request.env['product.product'].search([('id', '=', id)], limit=1) product_sla = request.env['product.sla'].search([('product_variant_id', '=', id)], limit=1) - stock_vendor = self.env['stock.vendor'].get_stock_updated_last_7_days(id) + stock_vendor = request.env['stock.vendor'].search([('product_variant_id', '=', id)], limit=1) + + stock_vendor = stock_vendor.get_stock_updated_last_7_days() qty_available = product.qty_onhand_bandengan -- cgit v1.2.3 From 968f63a411f8cc9e190a261123e47604ce54c2b2 Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Mon, 24 Jul 2023 08:55:26 +0700 Subject: fix bug and refactor api product sla --- indoteknik_api/controllers/api_v1/product.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'indoteknik_api/controllers/api_v1') diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 587773ee..7ec6459b 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -13,32 +13,35 @@ class Product(controller.Controller): prefix = '/api/v1/' @http.route(prefix + 'product_variant//stock', auth='public', methods=['GET', 'OPTIONS']) - # @controller.Controller.must_authorized() + @controller.Controller.must_authorized() def get_product_template_stock_by_id(self, **kw): id = int(kw.get('id')) + date_7_days_ago = datetime.now() - timedelta(days=7) product = request.env['product.product'].search([('id', '=', id)], limit=1) product_sla = request.env['product.sla'].search([('product_variant_id', '=', id)], limit=1) - stock_vendor = request.env['stock.vendor'].search([('product_variant_id', '=', id)], limit=1) - - stock_vendor = stock_vendor.get_stock_updated_last_7_days() + stock_vendor = request.env['stock.vendor'].search([ + ('product_variant_id', '=', id), + ('write_date', '>=', date_7_days_ago.strftime("%Y-%m-%d %H:%M:%S")) + ], limit=1) qty_available = product.qty_onhand_bandengan qty = 0 sla_date = '-' - - qty_vendor = stock_vendor.quantity - qty_vendor -= int(qty_vendor * 0.1) - qty_vendor = math.ceil(float(qty_vendor)) - total_excell = qty_vendor is_altama_product = product.x_manufacture.id in [10,122,89] if is_altama_product: try: + # Qty Altama qty_altama = request.env['product.template'].get_stock_altama(product.default_code) qty_altama -= int(qty_altama * 0.1) qty_altama = math.ceil(float(qty_altama)) total_adem = qty_altama + # Qty Stock Vendor + qty_vendor = stock_vendor.quantity + qty_vendor -= int(qty_vendor * 0.1) + qty_vendor = math.ceil(float(qty_vendor)) + total_excell = qty_vendor if qty_available > 0: qty = qty_available + total_adem + total_excell sla_date = '1 Hari' -- cgit v1.2.3