summaryrefslogtreecommitdiff
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
parent2c0bb838b406503aa6a10cc0c21d474429246e18 (diff)
Add voucher model, view, and api
-rw-r--r--indoteknik_api/controllers/api_v1/__init__.py1
-rw-r--r--indoteknik_api/controllers/api_v1/voucher.py22
-rwxr-xr-xindoteknik_custom/__manifest__.py1
-rwxr-xr-xindoteknik_custom/models/__init__.py1
-rwxr-xr-xindoteknik_custom/models/sale_order.py1
-rw-r--r--indoteknik_custom/models/voucher.py66
-rwxr-xr-xindoteknik_custom/security/ir.model.access.csv3
-rwxr-xr-xindoteknik_custom/views/voucher.xml85
8 files changed, 179 insertions, 1 deletions
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 @@
+<odoo>
+ <data>
+ <record id="voucher_action" model="ir.actions.act_window">
+ <field name="name">Voucher</field>
+ <field name="res_model">voucher</field>
+ <field name="view_mode">tree,form</field>
+ </record>
+
+ <record id="voucher_tree" model="ir.ui.view">
+ <field name="name">Voucher</field>
+ <field name="model">voucher</field>
+ <field name="arch" type="xml">
+ <tree>
+ <field name="name"/>
+ <field name="code"/>
+ <field name="description"/>
+ </tree>
+ </field>
+ </record>
+
+ <record id="voucher_form" model="ir.ui.view">
+ <field name="name">Voucher</field>
+ <field name="model">voucher</field>
+ <field name="arch" type="xml">
+ <form>
+ <sheet>
+ <group>
+ <group>
+ <field name="image" widget="image" width="120"/>
+ <field name="name" required="1" />
+ <field name="description" placeholder="Insert short description..." />
+ </group>
+ <group string="Rules">
+ <field name="code" required="1" />
+ <field name="visibility" required="1" />
+ <field name="start_time" required="1"/>
+ <field name="end_time" required="1"/>
+ </group>
+ <group string="Discount Settings">
+ <field name="min_purchase_amount" widget="monetary" required="1" />
+ <field name="discount_type" required="1" />
+
+ <label for="max_discount_amount" string="Discount Amount" />
+ <div class="d-flex align-items-center">
+ <span
+ class="mr-1 font-weight-bold"
+ attrs="{'invisible': [('discount_type', '!=', 'fixed_price')]}"
+ >
+ Rp
+ </span>
+ <field class="mb-0" name="discount_amount" required="1" />
+ <span
+ class="ml-1 font-weight-bold"
+ attrs="{'invisible': [('discount_type', '!=', 'percentage')]}"
+ >
+ %
+ </span>
+ </div>
+
+ <field name="max_discount_amount" widget="monetary" required="1" attrs="{'invisible': [('discount_type', '!=', 'percentage')]}"/>
+ </group>
+ </group>
+ <notebook>
+ <page name="order_page" string="Orders">
+ <field name="order_ids" readonly="1">
+ <tree>
+ <field name="name" />
+ <field name="amount_total" />
+ </tree>
+ </field>
+ </page>
+ </notebook>
+ </sheet>
+ </form>
+ </field>
+ </record>
+
+ <menuitem id="voucher"
+ name="Voucher"
+ parent="website_sale.menu_catalog"
+ sequence="1"
+ action="voucher_action"
+ />
+ </data>
+</odoo> \ No newline at end of file