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 ------------ indoteknik_custom/models/promotion_program_line.py | 51 ++++++++++++++++++++++ indoteknik_custom/models/sale_order.py | 1 + indoteknik_custom/views/promotion_program_line.xml | 15 ++++++- indoteknik_custom/views/sale_order.xml | 1 + 6 files changed, 79 insertions(+), 28 deletions(-) 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) diff --git a/indoteknik_custom/models/promotion_program_line.py b/indoteknik_custom/models/promotion_program_line.py index b6c9a532..8d80bfb0 100644 --- a/indoteknik_custom/models/promotion_program_line.py +++ b/indoteknik_custom/models/promotion_program_line.py @@ -1,4 +1,5 @@ from odoo import fields, models, api +from datetime import datetime class PromotionProgramLine(models.Model): @@ -34,6 +35,7 @@ class PromotionProgramLine(models.Model): line_free_item = fields.One2many( comodel_name="promotion.program.free_item", inverse_name="line_id", string="Line Free Item") display_on_homepage = fields.Boolean(string="Display on Homepage") + order_line_ids = fields.One2many('sale.order.line', 'program_line_id') @api.onchange('product_id') def _onchange_product_id(self): @@ -68,3 +70,52 @@ class PromotionProgramLine(models.Model): data = {'product_id': product.id, 'qty': 1, 'line_id': program_line.id} line_free_item.create(data) + + def get_active_promotions(self, product_id): + current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + return self.search([ + ('program_id.start_time', '<=', current_time), + ('program_id.end_time', '>=', current_time), + ('product_id', '=', product_id) + ]) + + def get_remaining_qty(self, line, user): + remaining_qty = { + 'all': line.limit_qty, + 'user': line.limit_qty_user, + 'transaction': line.limit_qty_transaction + } + for order in line.order_line_ids: + parent_order = order.order_id + if parent_order.state != 'cancel': + remaining_qty['all'] -= order.product_uom_qty + if parent_order.partner_id.id == user['partner_id']: + remaining_qty['user'] -= order.product_uom_qty + + if remaining_qty['all'] < remaining_qty['user']: + remaining_qty['user'] = remaining_qty['all'] + if remaining_qty['user'] < remaining_qty['transaction']: + remaining_qty['transaction'] = remaining_qty['user'] + + return remaining_qty + + def res_format(self, lines, user): + ir_attachment = self.env['ir.attachment'] + data = [] + for line in lines: + data.append({ + 'id': line.id, + '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': { + 'all': line.limit_qty, + 'user': line.limit_qty_user, + 'transaction': line.limit_qty_transaction, + }, + 'remaining_qty': self.get_remaining_qty(line, user), + }) + return data + diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index cbc6a60a..f8826fab 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -459,6 +459,7 @@ class SaleOrderLine(models.Model): delivery_amt_line = fields.Float('DeliveryAmtLine', compute='compute_delivery_amt_line') fee_third_party_line = fields.Float('FeeThirdPartyLine', compute='compute_fee_third_party_line', default=0) line_no = fields.Integer('No', default=0, copy=False) + program_line_id = fields.Many2one('promotion.program.line', 'Program Line') def compute_item_margin(self): for line in self: diff --git a/indoteknik_custom/views/promotion_program_line.xml b/indoteknik_custom/views/promotion_program_line.xml index e15bb0d7..5963d0dd 100644 --- a/indoteknik_custom/views/promotion_program_line.xml +++ b/indoteknik_custom/views/promotion_program_line.xml @@ -58,10 +58,23 @@ + + + + + + + + + + diff --git a/indoteknik_custom/views/sale_order.xml b/indoteknik_custom/views/sale_order.xml index ea13c954..83278ca8 100755 --- a/indoteknik_custom/views/sale_order.xml +++ b/indoteknik_custom/views/sale_order.xml @@ -60,6 +60,7 @@ "/> + -- cgit v1.2.3