1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
from odoo import fields, models, api
from datetime import datetime
class PromotionProgramLine(models.Model):
_name = "promotion.program.line"
name = fields.Char(string="Name")
image = fields.Binary(string="Image")
product_id = fields.Many2one(
comodel_name="product.product", string="Product Variant")
program_id = fields.Many2one(
comodel_name="promotion.program", string="Program")
discount_type = fields.Selection(selection=[
("percentage", "Percentage"),
("fixed_price", "Fixed Price"),
], string="Discount Type")
discount_amount = fields.Float(string="Discount Amount")
promotion_type = fields.Selection(selection=[
("special_price", "Special Price"),
("bundling", "Bundling"),
("discount_loading", "Discount Loading"),
("merchandise", "Merchandise")
], string="Promotion Type")
minimum_purchase_qty = fields.Integer(
string="Minimum Purchase Qty", help="Minimum Qty to applied discount loading")
applies_multiply = fields.Boolean(
string="Applies Multiply", help="Is applies multiply")
limit_qty = fields.Integer(
string="Limit Qty", help="Limit Qty product in promotion")
limit_qty_user = fields.Integer(
string="Limit Qty / User", help="Limit Qty per User")
limit_qty_transaction = fields.Integer(
string="Limit Qty / Transaction", help="Limit Qty per Transaction")
line_free_item = fields.One2many(
comodel_name="promotion.program.free_item", inverse_name="line_id", string="Line Free Item")
display_on_homepage = fields.Boolean(string="Display on Homepage")
order_line_ids = fields.One2many('sale.order.line', 'program_line_id')
@api.onchange('product_id')
def _onchange_product_id(self):
if self.product_id and not self.name:
self.name = self.product_id.display_name
self._discount_loading_auto()
@api.onchange('promotion_type')
def onchange_promotion_type(self):
self._discount_loading_auto()
def _discount_loading_auto(self):
program_line = self
product = program_line.product_id
promotion_type = program_line.promotion_type
if promotion_type != 'discount_loading' or not product:
return
line = self.browse(self.ids)
line.product_id = self.product_id.id
line.promotion_type = self.promotion_type
product_added = False
line_free_item = program_line.line_free_item
for line in line_free_item:
if line.product_id.id == product.id:
product_added = True
continue
line.unlink()
if not product_added:
data = {'product_id': product.id,
'qty': 1, 'line_id': program_line.id}
line_free_item.create(data)
def get_active_promotions(self, product_id):
current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
return self.search([
('program_id.start_time', '<=', current_time),
('program_id.end_time', '>=', current_time),
('product_id', '=', product_id)
])
def _get_remaining_qty(self, user):
remaining_qty = {
'all': self.limit_qty,
'user': self.limit_qty_user,
'transaction': self.limit_qty_transaction
}
for order in self.order_line_ids:
parent_order = order.order_id
if parent_order.state != 'cancel':
remaining_qty['all'] -= order.product_uom_qty
if user and parent_order.partner_id.id == user['partner_id']:
remaining_qty['user'] -= order.product_uom_qty
if remaining_qty['all'] < remaining_qty['user']:
remaining_qty['user'] = remaining_qty['all']
if remaining_qty['user'] < remaining_qty['transaction']:
remaining_qty['transaction'] = remaining_qty['user']
return remaining_qty
def _get_remaining_time(self):
calculate_time = self.program_id.end_time - datetime.now()
return round(calculate_time.total_seconds())
def res_format(self, lines, user):
ir_attachment = self.env['ir.attachment']
data = []
for line in lines:
data.append({
'id': line.id,
'name': line.name,
'image': ir_attachment.api_image('promotion.program.line', 'image', line.id),
'type': {
'value': line.promotion_type,
'label': dict(self._fields['promotion_type'].selection).get(line.promotion_type)
},
'minimum_purchase_qty': line.minimum_purchase_qty,
'applies_multiply': line.applies_multiply,
'remaining_time': line._get_remaining_time(),
'limit_qty': {
'all': line.limit_qty,
'user': line.limit_qty_user,
'transaction': line.limit_qty_transaction,
},
'remaining_qty': line._get_remaining_qty(user),
})
return data
|