diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-09-11 13:18:02 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-09-11 13:18:02 +0700 |
| commit | 1694e042acb29b476815d29f54f2ec95d37883be (patch) | |
| tree | d502c76d333cc4c0602e01a98eeb7fb7f0461c01 /indoteknik_custom/models/sale_order.py | |
| parent | 02c2304b242245250177fec6ab3c911d9acba781 (diff) | |
| parent | 9a630354c1d00e218595db9b2e8cd0b7df9ed97c (diff) | |
Merge branch 'feature/voucher-group' into production
Diffstat (limited to 'indoteknik_custom/models/sale_order.py')
| -rwxr-xr-x | indoteknik_custom/models/sale_order.py | 75 |
1 files changed, 74 insertions, 1 deletions
diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 65132fda..8317e1fd 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])]") estimated_arrival_days = fields.Integer('Estimated Arrival Days', default=0) @@ -377,3 +378,75 @@ class SaleOrder(models.Model): else: order.grand_total = order.amount_total + def action_apply_voucher(self): + for order in self.order_line: + if order.program_line_id: + raise UserError('Voucher tidak dapat digabung dengan promotion program') + + voucher = self.voucher_id + if voucher.limit > 0 and voucher.count_order >= voucher.limit: + raise UserError('Voucher tidak dapat digunakan karena sudah habis digunakan') + + partner_voucher_orders = [] + for order in voucher.order_ids: + if order.partner_id.id == self.partner_id.id: + partner_voucher_orders.append(order) + + if voucher.limit_user > 0 and len(partner_voucher_orders) >= voucher.limit_user: + raise UserError('Voucher tidak dapat digunakan karena Customer ini sudah menghabiskan kuota voucher') + + if self.pricelist_id.id in [x.id for x in voucher.excl_pricelist_ids]: + raise UserError('Voucher tidak dapat digunakan karena pricelist ini tidak berlaku pada voucher') + + self.apply_voucher() + + 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 + + + + |
