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_api/controllers/api_v1/__init__.py | 1 + indoteknik_api/controllers/api_v1/voucher.py | 22 +++++++ 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 ++++++++++++++++++++++++++ 8 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 indoteknik_api/controllers/api_v1/voucher.py create mode 100644 indoteknik_custom/models/voucher.py create mode 100755 indoteknik_custom/views/voucher.xml diff --git a/indoteknik_api/controllers/api_v1/__init__.py b/indoteknik_api/controllers/api_v1/__init__.py index 2afefb34..65bcf926 100644 --- a/indoteknik_api/controllers/api_v1/__init__.py +++ b/indoteknik_api/controllers/api_v1/__init__.py @@ -25,3 +25,4 @@ from . import content from . import midtrans from . import wati from . import courier +from . import voucher \ No newline at end of file diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py new file mode 100644 index 00000000..0990a1a0 --- /dev/null +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -0,0 +1,22 @@ +from .. import controller +from odoo import http +from odoo.http import request + +class Voucher(controller.Controller): + prefix = '/api/v1/' + + @http.route(prefix + 'voucher', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def get_vouchers(self, **kw): + code = kw.get('code') + visibility = 'public' + + parameter = [] + if code: + visibility = 'private' + parameter += [('code', '=', code)] + + parameter += [('visibility', '=', visibility)] + vouchers = request.env['voucher'].get_active_voucher(parameter) + data = vouchers.res_format() + return self.response(data) 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