summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/sale_order.py
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-08-31 16:09:15 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-08-31 16:09:15 +0700
commitf5d3b28344f3b992377efbba556306b423bdf723 (patch)
tree75dfa7e7a661ba563d702c9b209b094508510f45 /indoteknik_custom/models/sale_order.py
parent4fec661573ebac160f0b928fbccc2ef9d6d1f969 (diff)
Add apply voucher function on sale order
Update sale order view
Diffstat (limited to 'indoteknik_custom/models/sale_order.py')
-rwxr-xr-xindoteknik_custom/models/sale_order.py53
1 files changed, 52 insertions, 1 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
+
+
+