diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-08-31 16:09:15 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-08-31 16:09:15 +0700 |
| commit | f5d3b28344f3b992377efbba556306b423bdf723 (patch) | |
| tree | 75dfa7e7a661ba563d702c9b209b094508510f45 | |
| parent | 4fec661573ebac160f0b928fbccc2ef9d6d1f969 (diff) | |
Add apply voucher function on sale order
Update sale order view
| -rwxr-xr-x | indoteknik_custom/models/sale_order.py | 53 | ||||
| -rw-r--r-- | indoteknik_custom/models/sale_order_line.py | 1 | ||||
| -rwxr-xr-x | 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 @@ <field name="delivery_amt"/> <field name="fee_third_party"/> <field name="total_percent_margin"/> - <field name="voucher_id" readonly="1" /> + <label for="voucher_id"/> + <div class="o_row"> + <field name="voucher_id" id="voucher_id" attrs="{'readonly': ['|', ('state', 'not in', ['draft', 'sent']), ('applied_voucher_id', '!=', False)]}"/> + <field name="applied_voucher_id" invisible="1" /> + <button name="apply_voucher" type="object" string="Apply" confirm="Anda yakin untuk menggunakan voucher?" help="Apply the selected voucher" class="btn-link mb-1 px-0" icon="fa-plus" + attrs="{'invisible': ['|', '|', ('voucher_id', '=', False), ('state', 'not in', ['draft', 'sent']), ('applied_voucher_id', '!=', False)]}" + /> + <button name="cancel_voucher" type="object" string="Cancel" confirm="Anda yakin untuk membatalkan penggunaan voucher?" help="Cancel applied voucher" class="btn-link mb-1 px-0" icon="fa-times" + attrs="{'invisible': ['|', ('applied_voucher_id', '=', False), ('state', 'not in', ['draft','sent'])]}" + /> + </div> </field> <field name="source_id" position="attributes"> <attribute name="invisible">1</attribute> @@ -49,6 +59,11 @@ <field name="date_doc_kirim" readonly="1"/> <field name="notification" readonly="1"/> </field> + <xpath expr="//form/sheet/notebook/page/field[@name='order_line']" position="attributes"> + <attribute name="attrs"> + {'readonly': ['|', ('applied_voucher_id', '!=', False), ('state', 'in', ('done','cancel'))]} + </attribute> + </xpath> <xpath expr="//form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='price_unit']" position="attributes"> <attribute name="attrs"> { @@ -75,7 +90,7 @@ <field name="item_percent_margin"/> <field name="note_procurement" optional="hide"/> <field name="vendor_subtotal" optional="hide"/> - <field name="amount_voucher_disc" string="Voucher" optional="hide"/> + <field name="amount_voucher_disc" string="Voucher" readonly="1" optional="hide"/> <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"> @@ -85,7 +100,7 @@ <field name="grand_total"/> <label for="amount_voucher_disc" string="Voucher" /> <div> - <field class="mb-0" name="amount_voucher_disc" string="Voucher"/> + <field class="mb-0" name="amount_voucher_disc" string="Voucher" readonly="1"/> <div class="text-right mb-2"><small>*Hanya informasi</small></div> </div> <field name="total_margin"/> |
