summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/promotion_program_line.py
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-06-22 13:43:42 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-06-22 13:43:42 +0700
commit329668e82ca9e4ebd2ee93d6670380abf6ed6377 (patch)
tree4180e03acdd15f33654a6cf3d5e8f856ac677848 /indoteknik_custom/models/promotion_program_line.py
parent4e9a5a025012afe63133524eeea8987b9af9e050 (diff)
Add program line to order line relation, update remaining_qty response on get product promotion api, show order line on program line
Diffstat (limited to 'indoteknik_custom/models/promotion_program_line.py')
-rw-r--r--indoteknik_custom/models/promotion_program_line.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/indoteknik_custom/models/promotion_program_line.py b/indoteknik_custom/models/promotion_program_line.py
index b6c9a532..8d80bfb0 100644
--- a/indoteknik_custom/models/promotion_program_line.py
+++ b/indoteknik_custom/models/promotion_program_line.py
@@ -1,4 +1,5 @@
from odoo import fields, models, api
+from datetime import datetime
class PromotionProgramLine(models.Model):
@@ -34,6 +35,7 @@ class PromotionProgramLine(models.Model):
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):
@@ -68,3 +70,52 @@ class PromotionProgramLine(models.Model):
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, line, user):
+ remaining_qty = {
+ 'all': line.limit_qty,
+ 'user': line.limit_qty_user,
+ 'transaction': line.limit_qty_transaction
+ }
+ for order in line.order_line_ids:
+ parent_order = order.order_id
+ if parent_order.state != 'cancel':
+ remaining_qty['all'] -= order.product_uom_qty
+ if 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 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': line.promotion_type,
+ 'minimum_purchase_qty': line.minimum_purchase_qty,
+ 'applies_multiply': line.applies_multiply,
+ 'limit_qty': {
+ 'all': line.limit_qty,
+ 'user': line.limit_qty_user,
+ 'transaction': line.limit_qty_transaction,
+ },
+ 'remaining_qty': self.get_remaining_qty(line, user),
+ })
+ return data
+