diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-06-22 13:43:42 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-06-22 13:43:42 +0700 |
| commit | 329668e82ca9e4ebd2ee93d6670380abf6ed6377 (patch) | |
| tree | 4180e03acdd15f33654a6cf3d5e8f856ac677848 | |
| parent | 4e9a5a025012afe63133524eeea8987b9af9e050 (diff) | |
Add program line to order line relation, update remaining_qty response on get product promotion api, show order line on program line
| -rw-r--r-- | indoteknik_api/controllers/api_v1/product_variant.py | 12 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/promotion.py | 27 | ||||
| -rw-r--r-- | indoteknik_custom/models/promotion_program_line.py | 51 | ||||
| -rwxr-xr-x | indoteknik_custom/models/sale_order.py | 1 | ||||
| -rw-r--r-- | indoteknik_custom/views/promotion_program_line.xml | 15 | ||||
| -rwxr-xr-x | 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/<product_id>/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/<id>', 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 @@ <page string="Line Free Item" name="line_free_items" - attrs="{'invisible': [('promotion_type', '=', 'specific_product')]}" + attrs="{'invisible': [('promotion_type', '=', 'special_price')]}" > <field name="line_free_item" /> </page> + <page + string="Order Line" + name="order_line" + > + <field name="order_line_ids" readonly="1"> + <tree> + <field name="order_id" /> + <field name="name" /> + <field name="product_id" /> + <field name="product_uom_qty" /> + </tree> + </field> + </page> </notebook> </sheet> </form> 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 @@ "/> <field name="purchase_tax_id" attrs="{'readonly': [('parent.approval_status', '!=', False)]}" domain="[('type_tax_use','=','purchase')]"/> <field name="item_percent_margin"/> + <field name="program_line_id" optional="1" /> </xpath> <xpath expr="//form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='product_id']" position="before"> <field name="line_no" readonly="1"/> |
