summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-07-10 14:34:55 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-07-10 14:34:55 +0700
commit68c2e896a684e1dc68b4f01da38a444fe76bceac (patch)
treeb706fb7df382c718590ff346469fe11522333688 /indoteknik_custom/models
parent2c0bb838b406503aa6a10cc0c21d474429246e18 (diff)
Add voucher model, view, and api
Diffstat (limited to 'indoteknik_custom/models')
-rwxr-xr-xindoteknik_custom/models/__init__.py1
-rwxr-xr-xindoteknik_custom/models/sale_order.py1
-rw-r--r--indoteknik_custom/models/voucher.py66
3 files changed, 68 insertions, 0 deletions
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