diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-07-12 11:33:04 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-07-12 11:33:04 +0700 |
| commit | 5d101afe46c1c1bce87ec2f7e8f18d040bbbc7d3 (patch) | |
| tree | 71a2065a8f6584352e766723df0cedd61a546e96 /indoteknik_custom | |
| parent | 7753f4b1ebf4514863c05d3a92d8c4496d9b344a (diff) | |
Add apply voucher on checkout api
Diffstat (limited to 'indoteknik_custom')
| -rw-r--r-- | indoteknik_custom/models/voucher.py | 20 | ||||
| -rwxr-xr-x | indoteknik_custom/views/sale_order.xml | 1 |
2 files changed, 21 insertions, 0 deletions
diff --git a/indoteknik_custom/models/voucher.py b/indoteknik_custom/models/voucher.py index 00192cec..b327df6d 100644 --- a/indoteknik_custom/models/voucher.py +++ b/indoteknik_custom/models/voucher.py @@ -4,7 +4,9 @@ from datetime import datetime, timedelta class Voucher(models.Model): _name = 'voucher' + _rec_name = 'display_name' + display_name = fields.Char(string='Display Name', compute='_compute_display_name') name = fields.Char(string='Name') image = fields.Binary(string='Image') code = fields.Char(string='Code', help='Kode voucher yang akan berlaku untuk pengguna') @@ -36,6 +38,10 @@ class Voucher(models.Model): max_discount_amount = fields.Integer(string='Max. Discount Amount', help='Max nominal terhadap persentase diskon') order_ids = fields.One2many('sale.order', 'voucher_id', string='Order') + def _compute_display_name(self): + for voucher in self: + voucher.display_name = f'{voucher.name} ({voucher.code})' + def res_format(self): datas = [voucher.format() for voucher in self] return datas @@ -78,6 +84,20 @@ class Voucher(models.Model): calculate_time = self.end_time - datetime.now() return round(calculate_time.total_seconds()) + def calculate_discount(self, price): + if price < self.min_purchase_amount: + return 0 + + if self.discount_type == 'fixed_price': + return self.discount_amount + + if self.discount_type == 'percentage': + discount = price * self.discount_amount / 100 + max_disc = self.max_discount_amount + return max_disc if discount > max_disc else discount + + return 0 + def get_active_voucher(self, parameter): current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') parameter += [ diff --git a/indoteknik_custom/views/sale_order.xml b/indoteknik_custom/views/sale_order.xml index 70e3392f..b55fefff 100755 --- a/indoteknik_custom/views/sale_order.xml +++ b/indoteknik_custom/views/sale_order.xml @@ -26,6 +26,7 @@ <field name="delivery_amt"/> <field name="fee_third_party"/> <field name="total_percent_margin"/> + <field name="voucher_id" /> </field> <field name="analytic_account_id" position="after"> <field name="customer_type" attrs="{'required': ['|', ('create_date', '>', '2023-06-28'), ('create_date', '=', False)]}"/> |
