summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-09-25 14:50:43 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-09-25 14:50:43 +0700
commit50b5bd7bd984ef108e8bd324440050a222d8262f (patch)
treefbd5f818fa3fbe716c41c73def6b104e8d124c36
parentdae4a3bf266ba4c19b1ba1d11c52ed9e19259b7c (diff)
Fix promotion program feature
-rw-r--r--indoteknik_custom/models/promotion/__init__.py3
-rw-r--r--indoteknik_custom/models/promotion/promotion_keyword.py8
-rw-r--r--indoteknik_custom/models/promotion/promotion_program.py1
-rw-r--r--indoteknik_custom/models/promotion/promotion_program_line.py32
-rw-r--r--indoteknik_custom/models/promotion/sale_order.py34
-rw-r--r--indoteknik_custom/models/promotion/sale_order_promotion.py1
-rwxr-xr-xindoteknik_custom/security/ir.model.access.csv1
-rw-r--r--indoteknik_custom/views/promotion/promotion_program.xml1
-rw-r--r--indoteknik_custom/views/promotion/promotion_program_line.xml18
-rwxr-xr-xindoteknik_custom/views/sale_order.xml1
10 files changed, 78 insertions, 22 deletions
diff --git a/indoteknik_custom/models/promotion/__init__.py b/indoteknik_custom/models/promotion/__init__.py
index 631df8f3..1e15d714 100644
--- a/indoteknik_custom/models/promotion/__init__.py
+++ b/indoteknik_custom/models/promotion/__init__.py
@@ -4,4 +4,5 @@ from . import promotion_product
from . import promotion_free_product
from . import sale_order_promotion
from . import sale_order_line
-from . import sale_order \ No newline at end of file
+from . import sale_order
+from . import promotion_keyword \ No newline at end of file
diff --git a/indoteknik_custom/models/promotion/promotion_keyword.py b/indoteknik_custom/models/promotion/promotion_keyword.py
new file mode 100644
index 00000000..a4bf90b6
--- /dev/null
+++ b/indoteknik_custom/models/promotion/promotion_keyword.py
@@ -0,0 +1,8 @@
+from odoo import models, fields
+
+
+class PromotionKeyword(models.Model):
+ _name = 'promotion.keyword'
+
+ name = fields.Char('Name')
+ promotion_id = fields.Many2one('promotion.program', 'Program') \ No newline at end of file
diff --git a/indoteknik_custom/models/promotion/promotion_program.py b/indoteknik_custom/models/promotion/promotion_program.py
index 90220509..29aaa753 100644
--- a/indoteknik_custom/models/promotion/promotion_program.py
+++ b/indoteknik_custom/models/promotion/promotion_program.py
@@ -16,3 +16,4 @@ class PromotionProgram(models.Model):
("login_user", "Login User")
])
program_line = fields.One2many('promotion.program.line', 'program_id', 'Program line')
+ keyword_ids = fields.One2many('promotion.keyword', 'promotion_id', 'Keywords')
diff --git a/indoteknik_custom/models/promotion/promotion_program_line.py b/indoteknik_custom/models/promotion/promotion_program_line.py
index a16f426d..34a0fbb2 100644
--- a/indoteknik_custom/models/promotion/promotion_program_line.py
+++ b/indoteknik_custom/models/promotion/promotion_program_line.py
@@ -7,7 +7,7 @@ class PromotionProgramLine(models.Model):
program_id = fields.Many2one('promotion.program', 'Program')
name = fields.Char('Name')
- type = fields.Selection([
+ promotion_type = fields.Selection([
("special_price", "Special Price"),
("bundling", "Bundling"),
("discount_loading", "Discount Loading"),
@@ -23,6 +23,11 @@ class PromotionProgramLine(models.Model):
free_product_ids = fields.One2many('promotion.free_product', 'program_line_id', 'Free Product')
price = fields.Float('Price')
+ discount_type = fields.Selection([
+ ("percentage", "Percentage"),
+ ("fixed", "Fixed")
+ ], 'Discount Type')
+ discount_amount = fields.Float('Discount Amount')
order_promotion_ids = fields.One2many('sale.order.promotion', 'program_line_id', 'Promotions')
@@ -62,10 +67,10 @@ class PromotionProgramLine(models.Model):
return remaining_qty
- def _res_type(self):
+ def _res_promotion_type(self):
return {
- 'value': self.type,
- 'label': dict(self._fields['type'].selection).get(self.type)
+ 'value': self.promotion_type,
+ 'label': dict(self._fields['promotion_type'].selection).get(self.promotion_type)
}
def _get_remaining_time(self):
@@ -97,7 +102,7 @@ class PromotionProgramLine(models.Model):
'name': self.name,
'image': ir_attachment.api_image(self._name, 'image', self.id),
'remaining_time': self._get_remaining_time(),
- 'type': self._res_type(),
+ 'promotion_type': self._res_promotion_type(),
'limit_qty': limit_qty,
'remaining_qty': remaining_qty,
'used_percentage': percent_remaining,
@@ -111,4 +116,19 @@ class PromotionProgramLine(models.Model):
'weight': weight
}
- return response \ No newline at end of file
+ if self.promotion_type == 'special_price':
+ response['price'] = self._calc_special_price_price(products[0])
+
+ return response
+
+ def _calc_special_price_price(self, product):
+ result = product['price']
+ price = result['price']
+ if self.discount_type == 'percentage':
+ result['discount_percentage'] = self.discount_amount
+ result['price_discount'] = price * (1 - self.discount_amount / 100)
+ elif self.discount_type == 'fixed':
+ final_price = price - self.discount_amount
+ result['price_discount'] = final_price
+ result['discount_percentage'] = (price - final_price) / price * 100
+ return result \ No newline at end of file
diff --git a/indoteknik_custom/models/promotion/sale_order.py b/indoteknik_custom/models/promotion/sale_order.py
index abbcb64b..42e972d0 100644
--- a/indoteknik_custom/models/promotion/sale_order.py
+++ b/indoteknik_custom/models/promotion/sale_order.py
@@ -21,7 +21,7 @@ class SaleOrder(models.Model):
free_products = promotion['free_products']
all_products = products.copy()
- if promotion['type']['value'] == 'merchandise':
+ if promotion['promotion_type']['value'] == 'merchandise':
iu_products += filter(lambda x: x['qty'] > 0, free_products)
else:
all_products = self._merge_promotion_products(promotion)
@@ -31,6 +31,21 @@ class SaleOrder(models.Model):
promotion_used_price = 0
for index, product in enumerate(all_products):
+ order_line_default = {
+ 'company_id': 1,
+ 'order_id': self.id,
+ 'order_promotion_id': line.id,
+ 'product_id': product['id'],
+ 'product_uom_qty': product['qty'],
+ }
+ if promotion['promotion_type']['value'] == 'special_price':
+ order_lines.append({
+ **order_line_default,
+ 'price_unit': product['price']['price'],
+ 'discount': product['price']['discount_percentage']
+ })
+ continue
+
product_price = product['price']['price']
contrib_decimal = product_price * product['qty'] / promotion_amt_total
@@ -46,19 +61,24 @@ class SaleOrder(models.Model):
promotion_used_price += product_subtotal
order_lines.append({
- 'company_id': 1,
- 'order_id': self.id,
+ **order_line_default,
'price_unit': product_price,
- 'discount': contrib_disc_unit,
- 'product_id': product['id'],
- 'product_uom_qty': product['qty'],
- 'order_promotion_id': line.id
+ 'discount': contrib_disc_unit
})
self.env['sale.order.line'].create(order_lines)
self._create_promotion_program_iu_docs(iu_products)
def _merge_promotion_products(self, promotion):
+ """
+ Merges the products and free products from a promotion into a single dictionary.
+
+ Parameters:
+ promotion (dict): A dictionary representing a promotion with 'products' and 'free_products' keys.
+
+ Returns:
+ list: A list containing the merged products from the promotion.
+ """
merged_products = {}
for product in promotion['products'] + promotion['free_products']:
diff --git a/indoteknik_custom/models/promotion/sale_order_promotion.py b/indoteknik_custom/models/promotion/sale_order_promotion.py
index 3527f9e6..a3cd8d01 100644
--- a/indoteknik_custom/models/promotion/sale_order_promotion.py
+++ b/indoteknik_custom/models/promotion/sale_order_promotion.py
@@ -3,6 +3,7 @@ from odoo import models, fields
class SaleOrderPromotion(models.Model):
_name = 'sale.order.promotion'
+ _rec_name = "program_line_id"
order_id = fields.Many2one('sale.order', 'Order')
order_line_ids = fields.One2many('sale.order.line', 'order_promotion_id', 'Order Line')
diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv
index 21bbe093..da92f81f 100755
--- a/indoteknik_custom/security/ir.model.access.csv
+++ b/indoteknik_custom/security/ir.model.access.csv
@@ -55,6 +55,7 @@ access_promotion_program,access.promotion.program,model_promotion_program,,1,1,1
access_promotion_program_line,access.promotion.program.line,model_promotion_program_line,,1,1,1,1
access_promotion_product,access.promotion.product,model_promotion_product,,1,1,1,1
access_promotion_free_product,access.promotion.free_product,model_promotion_free_product,,1,1,1,1
+access_promotion_keyword,access.promotion.keyword,model_promotion_keyword,,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
diff --git a/indoteknik_custom/views/promotion/promotion_program.xml b/indoteknik_custom/views/promotion/promotion_program.xml
index 99fbd878..f8432d8a 100644
--- a/indoteknik_custom/views/promotion/promotion_program.xml
+++ b/indoteknik_custom/views/promotion/promotion_program.xml
@@ -22,6 +22,7 @@
<group>
<field name="name" />
<field name="banner" widget="image" height="160" />
+ <field name="keyword_ids" widget="many2many_tags" />
</group>
<group>
<field name="start_time" />
diff --git a/indoteknik_custom/views/promotion/promotion_program_line.xml b/indoteknik_custom/views/promotion/promotion_program_line.xml
index 6fdf0a9a..db6d5252 100644
--- a/indoteknik_custom/views/promotion/promotion_program_line.xml
+++ b/indoteknik_custom/views/promotion/promotion_program_line.xml
@@ -5,7 +5,7 @@
<field name="arch" type="xml">
<tree>
<field name="name" />
- <field name="type" />
+ <field name="promotion_type" />
<field name="product_ids" />
<field name="free_product_ids" />
<field name="price" />
@@ -22,30 +22,32 @@
<group>
<group>
<field name="name" />
- <field name="type" />
+ <field name="promotion_type" />
<field name="image" widget="image" height="160" />
</group>
<group>
<field name="package_limit" />
<field name="package_limit_user" />
<field name="package_limit_trx" />
- <field name="price" />
+ <field name="price" attrs="{'invisible': [('promotion_type', '=', 'special_price')]}" />
+ <field name="discount_type" attrs="{'invisible': [('promotion_type', '!=', 'special_price')]}" />
+ <field name="discount_amount" attrs="{'invisible': [('promotion_type', '!=', 'special_price')]}" />
</group>
</group>
<notebook>
<page string="Product Required" name="product_required">
<field name="product_ids">
<tree editable="top">
- <field name="product_id" />
- <field name="qty" />
+ <field name="product_id" required="1" />
+ <field name="qty" required="1" />
</tree>
</field>
</page>
- <page string="Free Product" name="free_product">
+ <page string="Free Product" name="free_product" attrs="{'invisible': [('promotion_type', '=', 'special_price')]}">
<field name="free_product_ids">
<tree editable="top">
- <field name="product_id" />
- <field name="qty" />
+ <field name="product_id" required="1" />
+ <field name="qty" required="1" />
</tree>
</field>
</page>
diff --git a/indoteknik_custom/views/sale_order.xml b/indoteknik_custom/views/sale_order.xml
index 282e4024..04d4b854 100755
--- a/indoteknik_custom/views/sale_order.xml
+++ b/indoteknik_custom/views/sale_order.xml
@@ -91,6 +91,7 @@
<field name="note_procurement" optional="hide"/>
<field name="vendor_subtotal" optional="hide"/>
<field name="amount_voucher_disc" string="Voucher" readonly="1" optional="hide"/>
+ <field name="order_promotion_id" string="Promotion" readonly="1" optional="hide"/>
</xpath>
<xpath expr="//form/sheet/notebook/page/field[@name='order_line']/tree/field[@name='product_id']" position="before">
<field name="line_no" readonly="1" optional="hide"/>