From f5d3b28344f3b992377efbba556306b423bdf723 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 31 Aug 2023 16:09:15 +0700 Subject: Add apply voucher function on sale order Update sale order view --- indoteknik_custom/models/sale_order.py | 53 ++++++++++++++++++++++++++++- indoteknik_custom/models/sale_order_line.py | 1 + indoteknik_custom/views/sale_order.xml | 21 ++++++++++-- 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index a0bc54b8..f8c97258 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -73,7 +73,8 @@ class SaleOrder(models.Model): sppkp = fields.Char(string="SPPKP") npwp = fields.Char(string="NPWP") purchase_total = fields.Monetary(string='Purchase Total', compute='_compute_purchase_total') - voucher_id = fields.Many2one(comodel_name='voucher', string='Voucher') + voucher_id = fields.Many2one(comodel_name='voucher', string='Voucher', copy=False) + applied_voucher_id = fields.Many2one(comodel_name='voucher', string='Applied Voucher', copy=False) amount_voucher_disc = fields.Float(string='Voucher Discount') source_id = fields.Many2one('utm.source', 'Source', domain="[('id', 'in', [32, 59, 60, 61])]") @@ -375,4 +376,54 @@ class SaleOrder(models.Model): order.grand_total = order.delivery_amt + order.amount_total else: order.grand_total = order.amount_total + + def apply_voucher(self): + order_line = [] + for line in self.order_line: + order_line.append({ + 'product_id': line.product_id, + 'price': line.price_unit, + 'discount': line.discount, + 'qty': line.product_uom_qty, + 'subtotal': line.price_subtotal + }) + voucher = self.voucher_id.apply(order_line) + + for line in self.order_line: + line.initial_discount = line.discount + + voucher_type = voucher['type'] + used_total = voucher['total'][voucher_type] + used_discount = voucher['discount'][voucher_type] + + manufacture_id = line.product_id.x_manufacture.id + if voucher_type == 'brand': + used_total = used_total.get(manufacture_id) + used_discount = used_discount.get(manufacture_id) + + if not used_total or not used_discount: + continue + + line_contribution = line.price_subtotal / used_total + line_voucher = used_discount * line_contribution + line_voucher_item = line_voucher / line.product_uom_qty + line_discount_item = line.price_unit * line.discount / 100 + line_voucher_item + line_voucher_item = line_discount_item / line.price_unit * 100 + + line.amount_voucher_disc = line_voucher + line.discount = line_voucher_item + + self.amount_voucher_disc = voucher['discount']['all'] + self.applied_voucher_id = self.voucher_id + + def cancel_voucher(self): + self.applied_voucher_id = False + self.amount_voucher_disc = 0 + for line in self.order_line: + line.amount_voucher_disc = 0 + line.discount = line.initial_discount + line.initial_discount = False + + + diff --git a/indoteknik_custom/models/sale_order_line.py b/indoteknik_custom/models/sale_order_line.py index 4e1209cb..4ec48b55 100644 --- a/indoteknik_custom/models/sale_order_line.py +++ b/indoteknik_custom/models/sale_order_line.py @@ -6,6 +6,7 @@ class SaleOrderLine(models.Model): _inherit = 'sale.order.line' item_margin = fields.Float('Margin', compute='compute_item_margin', help="Total Margin in Sales Order Header") item_percent_margin = fields.Float('%Margin', compute='compute_item_margin', help="Total % Margin in Sales Order Header") + initial_discount = fields.Float('Initial Discount') vendor_id = fields.Many2one( 'res.partner', string='Vendor', readonly=True, change_default=True, index=True, tracking=1, diff --git a/indoteknik_custom/views/sale_order.xml b/indoteknik_custom/views/sale_order.xml index 14f3663f..bbfc9e3b 100755 --- a/indoteknik_custom/views/sale_order.xml +++ b/indoteknik_custom/views/sale_order.xml @@ -26,7 +26,17 @@ - + 1 @@ -49,6 +59,11 @@ + + + {'readonly': ['|', ('applied_voucher_id', '!=', False), ('state', 'in', ('done','cancel'))]} + + { @@ -75,7 +90,7 @@ - + @@ -85,7 +100,7 @@