From 68c2e896a684e1dc68b4f01da38a444fe76bceac Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 10 Jul 2023 14:34:55 +0700 Subject: Add voucher model, view, and api --- indoteknik_custom/__manifest__.py | 1 + indoteknik_custom/models/__init__.py | 1 + indoteknik_custom/models/sale_order.py | 1 + indoteknik_custom/models/voucher.py | 66 ++++++++++++++++++++ indoteknik_custom/security/ir.model.access.csv | 3 +- indoteknik_custom/views/voucher.xml | 85 ++++++++++++++++++++++++++ 6 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 indoteknik_custom/models/voucher.py create mode 100755 indoteknik_custom/views/voucher.xml (limited to 'indoteknik_custom') diff --git a/indoteknik_custom/__manifest__.py b/indoteknik_custom/__manifest__.py index 79f6e195..5b0c4e41 100755 --- a/indoteknik_custom/__manifest__.py +++ b/indoteknik_custom/__manifest__.py @@ -77,6 +77,7 @@ 'views/brand_vendor.xml', 'views/requisition.xml', 'views/landedcost.xml', + 'views/voucher.xml', 'report/report.xml', 'report/report_banner_banner.xml', 'report/report_banner_banner2.xml', diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 166d43ad..3a27072c 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -64,3 +64,4 @@ from . import brand_vendor from . import manufacturing from . import requisition from . import account_move_due_extension +from . import voucher \ No newline at end of file diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 5a3cada9..e54b9ae2 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -86,6 +86,7 @@ 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') def _compute_purchase_total(self): for order in self: diff --git a/indoteknik_custom/models/voucher.py b/indoteknik_custom/models/voucher.py new file mode 100644 index 00000000..4ca13bd3 --- /dev/null +++ b/indoteknik_custom/models/voucher.py @@ -0,0 +1,66 @@ +from odoo import models, fields +from datetime import datetime + + +class Voucher(models.Model): + _name = 'voucher' + + name = fields.Char(string='Name') + image = fields.Binary(string='Image') + code = fields.Char(string='Code', help='Kode voucher yang akan berlaku untuk pengguna') + description = fields.Text(string='Description') + discount_amount = fields.Integer(string='Discount Amount') + discount_type = fields.Selection( + selection=[ + ('percentage', 'Percentage'), + ('fixed_price', 'Fixed Price'), + ], + string='Discount Type', + help='Select the type of discount:\n' + '- Percentage: Persentage dari total harga.\n' + '- Fixed Price: Jumlah tetap yang dikurangi dari harga total.' + ) + visibility = fields.Selection( + selection=[ + ('public', 'Public'), + ('private', 'Private') + ], + string='Visibility', + help='Select the visibility:\n' + '- Public: Ditampilkan kepada seluruh pengguna.\n' + '- Private: Tidak ditampilkan kepada seluruh pengguna.' + ) + start_time = fields.Datetime(string='Start Time') + end_time = fields.Datetime(string='End Time') + min_purchase_amount = fields.Integer(string='Min. Purchase Amount', help='Nominal minimum untuk dapat menggunakan voucher. Isi 0 jika tidak ada minimum purchase amount') + 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 res_format(self): + datas = [voucher.format() for voucher in self] + return datas + + def format(self): + data = { + 'name': self.name, + 'code': self.code, + 'description': self.description, + 'discount_amount': self.discount_amount, + 'discount_type': self.discount_type, + 'remaining_time': self._get_remaining_time(), + 'min_purchase_amount': self.min_purchase_amount + } + return data + + def _get_remaining_time(self): + calculate_time = self.end_time - datetime.now() + return round(calculate_time.total_seconds()) + + def get_active_voucher(self, parameter): + current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + parameter += [ + ('start_time', '<=', current_time), + ('end_time', '>=', current_time), + ] + vouchers = self.search(parameter) + return vouchers \ No newline at end of file diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index df820053..7cacf2bb 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -53,4 +53,5 @@ access_rajaongkir_kurir,access.rajaongkir.kurir,model_rajaongkir_kurir,,1,1,1,1 access_brand_vendor,access.brand.vendor,model_brand_vendor,,1,1,1,1 access_requisition,access.requisition,model_requisition,,1,1,1,1 access_requisition_line,access.requisition.line,model_requisition_line,,1,1,1,1 -access_requisition_purchase_match,access.requisition.purchase.match,model_requisition_purchase_match,,1,1,1,1 \ No newline at end of file +access_requisition_purchase_match,access.requisition.purchase.match,model_requisition_purchase_match,,1,1,1,1 +access_voucher,access.voucher,model_voucher,,1,1,1,1 \ No newline at end of file diff --git a/indoteknik_custom/views/voucher.xml b/indoteknik_custom/views/voucher.xml new file mode 100755 index 00000000..32c15588 --- /dev/null +++ b/indoteknik_custom/views/voucher.xml @@ -0,0 +1,85 @@ + + + + Voucher + voucher + tree,form + + + + Voucher + voucher + + + + + + + + + + + Voucher + voucher + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+
+ + +
+
\ No newline at end of file -- cgit v1.2.3 From 8ae3fbf012539d286a5de3deb3d7c500920c2257 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 10 Jul 2023 15:00:17 +0700 Subject: Update voucher api response --- indoteknik_custom/models/voucher.py | 5 ++++- indoteknik_custom/views/voucher.xml | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'indoteknik_custom') diff --git a/indoteknik_custom/models/voucher.py b/indoteknik_custom/models/voucher.py index 4ca13bd3..c97e54fa 100644 --- a/indoteknik_custom/models/voucher.py +++ b/indoteknik_custom/models/voucher.py @@ -41,13 +41,16 @@ class Voucher(models.Model): return datas def format(self): + ir_attachment = self.env['ir.attachment'] data = { + 'id': self.id, + 'image': ir_attachment.api_image('voucher', 'image', self.id), 'name': self.name, 'code': self.code, 'description': self.description, 'discount_amount': self.discount_amount, 'discount_type': self.discount_type, - 'remaining_time': self._get_remaining_time(), + 'remaining_time': self.end_time.strftime('%d-%m-%Y'), 'min_purchase_amount': self.min_purchase_amount } return data diff --git a/indoteknik_custom/views/voucher.xml b/indoteknik_custom/views/voucher.xml index 32c15588..cd42586e 100755 --- a/indoteknik_custom/views/voucher.xml +++ b/indoteknik_custom/views/voucher.xml @@ -36,6 +36,7 @@ + -- cgit v1.2.3 From 30ee32fcb482d325dd4a8db09983112a81fdb448 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 10 Jul 2023 15:28:00 +0700 Subject: Update format voucher api response --- indoteknik_custom/models/voucher.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'indoteknik_custom') diff --git a/indoteknik_custom/models/voucher.py b/indoteknik_custom/models/voucher.py index c97e54fa..e1497d9a 100644 --- a/indoteknik_custom/models/voucher.py +++ b/indoteknik_custom/models/voucher.py @@ -42,6 +42,8 @@ class Voucher(models.Model): def format(self): ir_attachment = self.env['ir.attachment'] + discount_type = self.discount_type + max_discount_amount = self.max_discount_amount if discount_type == 'percentage' else 0 data = { 'id': self.id, 'image': ir_attachment.api_image('voucher', 'image', self.id), @@ -49,9 +51,10 @@ class Voucher(models.Model): 'code': self.code, 'description': self.description, 'discount_amount': self.discount_amount, - 'discount_type': self.discount_type, + 'discount_type': discount_type, 'remaining_time': self.end_time.strftime('%d-%m-%Y'), - 'min_purchase_amount': self.min_purchase_amount + 'min_purchase_amount': self.min_purchase_amount, + 'max_discount_amount': max_discount_amount, } return data -- cgit v1.2.3 From e1af064ea7c6be76e96719382f4de8245b89dc5f Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Mon, 10 Jul 2023 15:30:53 +0700 Subject: Add start time on voucher api --- indoteknik_custom/models/voucher.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'indoteknik_custom') diff --git a/indoteknik_custom/models/voucher.py b/indoteknik_custom/models/voucher.py index e1497d9a..6a166cf3 100644 --- a/indoteknik_custom/models/voucher.py +++ b/indoteknik_custom/models/voucher.py @@ -52,7 +52,8 @@ class Voucher(models.Model): 'description': self.description, 'discount_amount': self.discount_amount, 'discount_type': discount_type, - 'remaining_time': self.end_time.strftime('%d-%m-%Y'), + 'start_time': self.start_time.strftime('%d-%m-%Y'), + 'end_time': self.end_time.strftime('%d-%m-%Y'), 'min_purchase_amount': self.min_purchase_amount, 'max_discount_amount': max_discount_amount, } -- cgit v1.2.3 From 94224e3174bf1370ae8e4ced3271d78beba83cfb Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Tue, 11 Jul 2023 10:32:54 +0700 Subject: Update get active voucher order by lowest min_purchase_amount --- indoteknik_custom/models/voucher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indoteknik_custom') diff --git a/indoteknik_custom/models/voucher.py b/indoteknik_custom/models/voucher.py index 6a166cf3..23285b3a 100644 --- a/indoteknik_custom/models/voucher.py +++ b/indoteknik_custom/models/voucher.py @@ -69,5 +69,5 @@ class Voucher(models.Model): ('start_time', '<=', current_time), ('end_time', '>=', current_time), ] - vouchers = self.search(parameter) + vouchers = self.search(parameter, order='min_purchase_amount ASC') return vouchers \ No newline at end of file -- cgit v1.2.3 From 7753f4b1ebf4514863c05d3a92d8c4496d9b344a Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Wed, 12 Jul 2023 08:56:19 +0700 Subject: Update voucher time text --- indoteknik_custom/models/voucher.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'indoteknik_custom') diff --git a/indoteknik_custom/models/voucher.py b/indoteknik_custom/models/voucher.py index 23285b3a..00192cec 100644 --- a/indoteknik_custom/models/voucher.py +++ b/indoteknik_custom/models/voucher.py @@ -1,5 +1,5 @@ from odoo import models, fields -from datetime import datetime +from datetime import datetime, timedelta class Voucher(models.Model): @@ -52,13 +52,28 @@ class Voucher(models.Model): 'description': self.description, 'discount_amount': self.discount_amount, 'discount_type': discount_type, - 'start_time': self.start_time.strftime('%d-%m-%Y'), - 'end_time': self.end_time.strftime('%d-%m-%Y'), + 'remaining_time': self._res_remaining_time(), 'min_purchase_amount': self.min_purchase_amount, 'max_discount_amount': max_discount_amount, } return data + def _res_remaining_time(self): + seconds = self._get_remaining_time() + remaining_time = timedelta(seconds=seconds) + hours = remaining_time.seconds // 3600 + minutes = remaining_time.seconds // 60 + if remaining_time.days > 0: + time = remaining_time.days + unit = 'hari' + elif hours > 0: + time = hours + unit = 'jam' + else: + time = minutes + unit = 'menit' + return f'{time} {unit}' + def _get_remaining_time(self): calculate_time = self.end_time - datetime.now() return round(calculate_time.total_seconds()) -- cgit v1.2.3 From 5d101afe46c1c1bce87ec2f7e8f18d040bbbc7d3 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Wed, 12 Jul 2023 11:33:04 +0700 Subject: Add apply voucher on checkout api --- indoteknik_custom/models/voucher.py | 20 ++++++++++++++++++++ indoteknik_custom/views/sale_order.xml | 1 + 2 files changed, 21 insertions(+) (limited to 'indoteknik_custom') 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 @@ + -- cgit v1.2.3