diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-06-07 13:58:24 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-06-07 13:58:24 +0700 |
| commit | db7481a490b87e3a1768112395bf096b93969562 (patch) | |
| tree | 4b62a1028b3db4748245a91a033817302deb0dff /indoteknik_api/controllers/api_v1 | |
| parent | 06a4478d69975b8a6eb3d228fa88708448b40a0e (diff) | |
Add promotion program keyword and api homepage promotion
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/promotion.py | 66 |
1 files changed, 64 insertions, 2 deletions
diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py index b137fe2e..5ee2acb1 100644 --- a/indoteknik_api/controllers/api_v1/promotion.py +++ b/indoteknik_api/controllers/api_v1/promotion.py @@ -1,7 +1,7 @@ from .. import controller from odoo import http from odoo.http import request -import ast +from datetime import datetime class Promotion(controller.Controller): @@ -26,4 +26,66 @@ class Promotion(controller.Controller): } return self.response(data) -
\ No newline at end of file + + @http.route(prefix + 'promotion/home', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def v1_get_promotion_home(self): + current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + programs = request.env['promotion.program'].search([ + ('start_time', '<=', current_time), + ('end_time', '>=', current_time), + ]) + if not programs: + return self.response(None) + + data = [] + for program in programs: + data_program = { + 'id': program.id, + 'name': program.name, + 'banner': request.env['ir.attachment'].api_image('promotion.program', 'banner', program.id), + 'icon': request.env['ir.attachment'].api_image('promotion.program', 'icon', program.id) + } + data.append(data_program) + + return self.response(data) + + @http.route(prefix + 'promotion/home/<id>', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def v1_get_promotion_home_detail(self, id): + program_lines = request.env['promotion.program.line'].search([ + ('display_on_homepage', '=', True), + ('promotion_type', '=', 'specific_product'), + ('program_id', '=', int(id)) + ]) + pricelist = self.user_pricelist() + data = [] + for line in program_lines: + product = request.env['product.product'].v2_api_single_response(line.product_id, pricelist=pricelist) + product_template = line.product_id.product_tmpl_id + + product.update({ + 'id': product['parent']['id'], + 'image': product['parent']['image'], + 'name': product['parent']['name'], + 'variant_total': len(product_template.product_variant_ids), + 'lowest_price': product['price'], + 'stock_total': product['stock'], + 'icon': { + 'top': request.env['ir.attachment'].api_image('promotion.program', 'icon_top', line.program_id.id), + 'bottom': request.env['ir.attachment'].api_image('promotion.program', 'icon_bottom', line.program_id.id) + } + }) + price = product['lowest_price']['price'] + if line.discount_type == 'percentage': + product['lowest_price']['discount_percentage'] = line.discount_amount + product['lowest_price']['price_discount'] = price - (price * line.discount_amount / 100) + if line.discount_type == 'fixed_price': + product['lowest_price']['price_discount'] = line.discount_amount + product['lowest_price']['discount_percentage'] = (price - line.discount_amount) / price * 100 + + product.pop('parent', None) + product.pop('price', None) + product.pop('stock', None) + data.append(product) + return self.response(data)
\ No newline at end of file |
