from bs4 import BeautifulSoup from odoo import http from odoo.http import request from .. import controller class Voucher(controller.Controller): PREFIX_API = '/api/v1/' @http.route(PREFIX_API + 'voucher', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() def get_vouchers(self, **kw): vouchers = request.env['voucher'].get_active_voucher([('visibility', 'in', ['public'])]) vouchers = vouchers.res_format() return self.response(vouchers) @http.route(PREFIX_API + 'user//voucher', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized(private=True, private_key='user_id') def get_vouchers_by_user_id(self, **kw): cart = request.env['website.user.cart'] code = kw.get('code') type = kw.get('type') user_id = int(kw.get('user_id', 0)) partner_id = int(kw.get('partner_id', 0)) source = kw.get('source') visibility = ['public'] user = request.env['res.users'].search([('id', '=', user_id)], limit=1) if not user: return self.response([]) domain = [] if code: visibility.append('private') domain += [('code', 'ilike', code)] user_pricelist = request.env.context.get('user_pricelist') if user_pricelist: domain += [('excl_pricelist_ids', 'not in', [user_pricelist.id])] if type: type = type.split(',') domain += [('apply_type', 'in', type)] if partner_id: partner = request.env['res.partner'].search([('id', '=', partner_id)], limit=1) main_parent = partner.get_main_parent() if main_parent and main_parent.company_type: domain += [('account_type', 'in', ['all',main_parent.company_type])] # domain += [('account_type', 'in', main_parent.company_type)] domain += [('visibility', 'in', visibility)] vouchers = request.env['voucher'].get_active_voucher(domain) checkout = cart.get_user_checkout(user_id, source=source) products = checkout['products'] order_line = [] for product in products: if product['cart_type'] == 'promotion': continue order_line.append({ 'product_id': request.env['product.product'].browse(product['id']), 'price': product['price']['price'], 'discount': product['price']['discount_percentage'], 'qty': product['quantity'], 'subtotal': product['subtotal'] }) results = [] for voucher in vouchers: if voucher.limit > 0 and voucher.count_order >= voucher.limit: continue partner_voucher_orders = [] for order in voucher.order_ids: if order.partner_id.id == user.partner_id.id and order.state != 'cancel' and (order.payment_status or order.payment_status is None): partner_voucher_orders.append(order) if voucher.limit_user > 0 and len(partner_voucher_orders) >= voucher.limit_user: continue voucher_info = voucher.apply(order_line) voucher_discount = voucher_info['discount']['all'] valid_order = voucher_info['valid_order'] can_apply = True if valid_order and voucher_discount > 0 else False voucher_res = voucher.format() voucher_res['can_apply'] = can_apply voucher_res['discount_voucher'] = voucher_discount cleaned_tnc = BeautifulSoup(voucher.terms_conditions or '', "html.parser").get_text() voucher_res['terms_conditions'] = voucher.terms_conditions if cleaned_tnc else voucher.generate_tnc() results.append(voucher_res) sorted_results = sorted(results, key=lambda x: x['can_apply'], reverse=True) return self.response(sorted_results) def get_user_by_email(self, email): return request.env['res.users'].search([ ('login', '=', email), ('active', 'in', [True, False]) ])