diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-06-12 08:22:10 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-06-12 08:22:10 +0700 |
| commit | 1f2995a85428ac4335123bd33d48ae17d3c9f36f (patch) | |
| tree | 42d8558ac317fcb3b158b52fe01205c35b885c64 /indoteknik_api/controllers/api_v1 | |
| parent | db7481a490b87e3a1768112395bf096b93969562 (diff) | |
Update promotion program feature
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/promotion.py | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py index 5ee2acb1..991a3eb9 100644 --- a/indoteknik_api/controllers/api_v1/promotion.py +++ b/indoteknik_api/controllers/api_v1/promotion.py @@ -6,7 +6,8 @@ from datetime import datetime class Promotion(controller.Controller): prefix = '/api/v1/' - + + @http.route(prefix + 'promotion/<id>', auth='public', methods=['GET']) @controller.Controller.must_authorized() def get_promotion_by_id(self, **kw): @@ -14,19 +15,21 @@ class Promotion(controller.Controller): id = kw.get('id') if not id: return self.response(code=400, description='id is required') - + data = {} id = int(id) - coupon_program = request.env['coupon.program'].search([('id', '=', id)]) + coupon_program = request.env['coupon.program'].search( + [('id', '=', id)]) if coupon_program: data = { 'banner': base_url + 'api/image/coupon.program/x_studio_banner_promo/' + str(coupon_program.id) if coupon_program.x_studio_banner_promo else '', 'image': base_url + 'api/image/coupon.program/x_studio_image_promo/' + str(coupon_program.id) if coupon_program.x_studio_image_promo else '', 'name': coupon_program.name, } - + return self.response(data) - + + @http.route(prefix + 'promotion/home', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() def v1_get_promotion_home(self): @@ -49,13 +52,14 @@ class Promotion(controller.Controller): 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'), + ('promotion_type', '=', 'special_price'), ('program_id', '=', int(id)) ]) pricelist = self.user_pricelist() @@ -82,10 +86,38 @@ class Promotion(controller.Controller): 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['lowest_price']['discount_percentage'] = round((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 + return self.response(data) + + + @http.route(prefix + 'promotion/product/<id>', auth='public', methods=['GET', 'OPTIONS']) + def v1_get_promotion_by_product_id(self, id): + id = int(id) + current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + product = request.env['product.template'].search([('id', '=', id)]) + variant_ids = [variant.id for variant in product.product_variant_ids] + program_lines = request.env['promotion.program.line'].search([ + ('program_id.start_time', '<=', current_time), + ('program_id.end_time', '>=', current_time), + ('product_id.id', 'in', variant_ids) + ]) + + data = [] + ir_attachment = request.env['ir.attachment'] + for line in program_lines: + data.append({ + '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': line.limit_qty, + 'limit_qty_user': line.limit_qty_user, + 'limit_qty_transaction': line.limit_qty_transaction, + }) + return self.response(data) |
