from .. import controller from odoo import http from odoo.http import request from datetime import datetime class Promotion(controller.Controller): prefix = '/api/v1/' @http.route(prefix + 'program-line//stock', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() def get_promotion_stock(self, id): program_line = request.env['promotion.program.line'].browse(int(id)) if not program_line.id: return self.response(code=400, description='program not found') user_data = self.verify_user_token() limit_qty = program_line._res_limit_qty() remaining_qty = program_line._get_remaining_qty(user_data) percent_remaining = 0 if limit_qty['all'] > 0: percent_remaining = (limit_qty['all'] - remaining_qty['all']) / limit_qty['all'] * 100 return self.response({ 'limit_qty': limit_qty, 'remaining_qty': remaining_qty, 'used_percentage': percent_remaining, }) # @http.route(prefix + 'program-line', auth='public', methods=['GET', 'OPTIONS']) # @controller.Controller.must_authorized() # def get_promotion_type(self, **kw): # type = kw.get('type') # domain = [("active", "=", True)] # if type: # domain.append(("promotion_type", "=", type)) # program_lines = request.env['promotion.program.line'].search(domain, limit=100) # data = [{ # 'id': line.id, # 'name': line.name, # 'program': line.program_id.name, # 'promotion_type': line.promotion_type, # 'active': line.active, # 'description': line.description, # 'package_limit': line.package_limit, # 'package_limit_user': line.package_limit_user, # 'package_limit_trx': line.package_limit_trx, # 'price': line.price, # 'products': [{ # 'id': product.product_id.id, # 'name': product.product_id.name, # 'qty': product.qty # } for product in line.product_ids] # } for line in program_lines] # return self.response(data) @http.route(prefix + 'promotion/', auth='public', methods=['GET']) @controller.Controller.must_authorized() def get_promotion_by_id(self, **kw): base_url = request.env['ir.config_parameter'].get_param('web.base.url') 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)]) 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): 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), ('program_line.display_on_homepage', '=', True) ]) 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/', 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', '=', 'special_price'), ('program_id', '=', int(id)) ]) data = [] for line in program_lines: product = request.env['product.product'].v2_api_single_response(line.product_id) 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': line.calculate_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) } }) product.pop('parent', None) product.pop('price', None) product.pop('stock', None) data.append(product) return self.response(data)