diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-07-18 13:59:18 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-07-18 13:59:18 +0700 |
| commit | 45fd501a53c6997bf74d5927d7c0eecf387caa51 (patch) | |
| tree | fa20bdff3e95d943b0f4846b9746767f87f70f31 /indoteknik_api | |
| parent | 999725ea036840d74c7fdeebbd3aefac772bd8d3 (diff) | |
| parent | d418bd8dd84b91b9dc031819cfa9a2446e77acd2 (diff) | |
Merge branch 'origin/feature/promotion-program' into feature/voucher-cart
Diffstat (limited to 'indoteknik_api')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/cart.py | 51 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/product_variant.py | 12 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/promotion.py | 70 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/sale_order.py | 28 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v2/product.py | 4 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v2/product_variant.py | 4 | ||||
| -rw-r--r-- | indoteknik_api/controllers/controller.py | 1 | ||||
| -rw-r--r-- | indoteknik_api/models/product_product.py | 66 | ||||
| -rw-r--r-- | indoteknik_api/models/product_template.py | 6 |
9 files changed, 172 insertions, 70 deletions
diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py index a8628432..035a40b7 100644 --- a/indoteknik_api/controllers/api_v1/cart.py +++ b/indoteknik_api/controllers/api_v1/cart.py @@ -5,55 +5,68 @@ from odoo.http import request class Cart(controller.Controller): prefix = '/api/v1/' + PREFIX_USER = prefix + 'user/<user_id>/' - @http.route(prefix + 'cart', auth='public', methods=['GET']) + @http.route(PREFIX_USER + 'cart', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() - def get_cart_by_user_id(self, **kw): - user_id = int(kw.get('user_id', 0)) + def get_cart_by_user_id(self, user_id, **kw): + user_cart = request.env['website.user.cart'] + user_id = int(user_id) limit = int(kw.get('limit', 0)) offset = int(kw.get('offset', 0)) query = [('user_id', '=', user_id)] - carts = request.env['website.user.cart'].search(query, limit=limit, offset=offset, order='create_date desc') + carts = user_cart.search(query, limit=limit, offset=offset, order='create_date desc') data = { - 'product_total': request.env['website.user.cart'].search_count(query), - 'products': [] + 'product_total': user_cart.search_count(query), + 'products': carts.get_products() } - for cart in carts: - product = request.env['product.product'].api_single_response(cart.product_id) - product['template_id'] = cart.product_id.product_tmpl_id.id - product['quantity'] = cart.qty - data['products'].append(product) return self.response(data) - @http.route(prefix + 'cart/create-or-update', auth='public', methods=['POST'], csrf=False) + @http.route(PREFIX_USER + 'cart/count', auth='public', methods=['GET', 'OPTIONS']) @controller.Controller.must_authorized() - def create_or_update_cart(self, **kw): - user_id = int(kw.get('user_id', 0)) + def get_cart_count_by_user_id(self, user_id, **kw): + user_id = int(user_id) + query = [('user_id', '=', user_id)] + carts = request.env['website.user.cart'].search_count(query) + return self.response(carts) + + @http.route(PREFIX_USER + 'cart/create-or-update', auth='public', methods=['POST', 'OPTIONS'], csrf=False) + @controller.Controller.must_authorized() + def create_or_update_cart(self, user_id, **kw): + user_id = int(user_id) product_id = int(kw.get('product_id', 0)) qty = int(kw.get('qty', 0)) + is_selected = kw.get('selected', False) + program_line_id = int(kw.get('program_line_id', False)) + if is_selected: + is_selected = True if is_selected == 'true' else False if not user_id or not product_id or not qty: return self.response(code=400, description='user_id, product_id and qty is required') query = [('user_id', '=', user_id), ('product_id', '=', product_id)] cart = request.env['website.user.cart'].search(query, limit=1) result = {} if cart: - cart.write({'qty': qty}) + data_to_update = {'qty': qty, 'is_selected': is_selected} + if program_line_id: + data_to_update['program_line_id'] = program_line_id + cart.write(data_to_update) result['id'] = cart.id else: create = request.env['website.user.cart'].create({ 'user_id': user_id, 'product_id': product_id, - 'qty': qty + 'qty': qty, + 'is_selected': is_selected }) result['id'] = create.id return self.response(result) - @http.route(prefix + 'cart', auth='public', methods=['DELETE'], csrf=False) + @http.route(PREFIX_USER + 'cart', auth='public', methods=['DELETE', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized() - def delete_cart_by_user_id(self, **kw): - user_id = int(kw.get('user_id', 0)) + def delete_cart_by_user_id(self, user_id, **kw): + user_id = int(user_id) query = [('user_id', '=', user_id)] product_ids = kw.get('product_ids') if product_ids: diff --git a/indoteknik_api/controllers/api_v1/product_variant.py b/indoteknik_api/controllers/api_v1/product_variant.py index 999ced6f..8de4669e 100644 --- a/indoteknik_api/controllers/api_v1/product_variant.py +++ b/indoteknik_api/controllers/api_v1/product_variant.py @@ -2,6 +2,7 @@ from .. import controller from odoo import http from odoo.http import request + class ProductVariant(controller.Controller): prefix = '/api/v1/' @@ -20,3 +21,14 @@ class ProductVariant(controller.Controller): return self.response(data) + @http.route(prefix + 'product_variant/<product_id>/promotions', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized() + def get_product_variant_promotions(self, product_id): + product_id = int(product_id) + user_data = self.verify_user_token() + + program_line = request.env['promotion.program.line'] + program_lines = program_line.get_active_promotions(product_id) + program_lines = program_lines.res_format(user=user_data) + + return self.response(program_lines) diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py index b137fe2e..68a23ef2 100644 --- a/indoteknik_api/controllers/api_v1/promotion.py +++ b/indoteknik_api/controllers/api_v1/promotion.py @@ -1,12 +1,13 @@ from .. import controller from odoo import http from odoo.http import request -import ast +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,16 +15,75 @@ 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): + 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', '=', '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) -
\ No newline at end of file + diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index 713b3bff..7c38d47d 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -231,7 +231,33 @@ class SaleOrder(controller.Controller): sale_order.state = 'cancel' data = sale_order.id return self.response(data) - + + @http.route(prefix + 'user/<user_id>/sale_order/checkout', auth='public', method=['GET', 'OPTIONS'], csrf=False) + @controller.Controller.must_authorized(private=True, private_key='user_id') + def get_user_checkout_so(self, user_id): + cart = request.env['website.user.cart'] + products = cart.get_product_by_user(user_id=user_id, selected=True) + total_purchase = sum(x['price']['price'] * x['quantity'] for x in products) + total_discount = sum((x['price']['price'] - x['price']['price_discount']) * x['quantity'] for x in products) + subtotal = total_purchase - total_discount + tax = round(subtotal * 0.11) + grand_total = subtotal + tax + total_weight = sum(x['weight'] * x['quantity'] for x in products) + result = { + 'total_purchase': total_purchase, + 'total_discount': total_discount, + 'subtotal': subtotal, + 'tax': tax, + 'grand_total': round(grand_total), + 'total_weight': { + 'kg': total_weight, + 'g': total_weight * 1000 + }, + 'has_product_without_weight': any(not product.get('weight') or product.get('weight') == 0 for product in products), + 'products': products + } + return self.response(result) + @http.route(PREFIX_PARTNER + 'sale_order/checkout', auth='public', method=['POST', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized(private=True, private_key='partner_id') def create_partner_sale_order(self, **kw): diff --git a/indoteknik_api/controllers/api_v2/product.py b/indoteknik_api/controllers/api_v2/product.py index d9b43bda..19a97dec 100644 --- a/indoteknik_api/controllers/api_v2/product.py +++ b/indoteknik_api/controllers/api_v2/product.py @@ -12,12 +12,10 @@ class V2Product(controller.Controller): if not id: return self.response(code=400, description='id is required') - pricelist = self.user_pricelist() - data = [] id = [int(x) for x in id.split(',')] product_templates = request.env['product.template'].search([('id', 'in', id)]) if product_templates: - data = [request.env['product.template'].v2_api_single_response(x, pricelist=pricelist, with_detail='DEFAULT') for x in product_templates] + data = [request.env['product.template'].v2_api_single_response(x, with_detail='DEFAULT') for x in product_templates] return self.response(data)
\ No newline at end of file diff --git a/indoteknik_api/controllers/api_v2/product_variant.py b/indoteknik_api/controllers/api_v2/product_variant.py index 8a5bbeb1..b74e4936 100644 --- a/indoteknik_api/controllers/api_v2/product_variant.py +++ b/indoteknik_api/controllers/api_v2/product_variant.py @@ -12,13 +12,11 @@ class V2ProductVariant(controller.Controller): if not id: return self.response(code=400, description='id is required') - pricelist = self.user_pricelist() - data = [] id = [int(x) for x in id.split(',')] product_products = request.env['product.product'].search([('id', 'in', id)]) if product_products: - data = [request.env['product.product'].v2_api_single_response(x, pricelist=pricelist) for x in product_products] + data = [request.env['product.product'].v2_api_single_response(x) for x in product_products] return self.response(data) diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py index 0fcf4814..1e9f01ee 100644 --- a/indoteknik_api/controllers/controller.py +++ b/indoteknik_api/controllers/controller.py @@ -21,6 +21,7 @@ class Controller(http.Controller): def inner_wrapper(*args, **kwargs): self = args[0] auth = self.authenticate() + request.env.user_pricelist = self.user_pricelist() if not auth: return self.unauthorized_response() if private: diff --git a/indoteknik_api/models/product_product.py b/indoteknik_api/models/product_product.py index edf95a58..03742d69 100644 --- a/indoteknik_api/models/product_product.py +++ b/indoteknik_api/models/product_product.py @@ -29,20 +29,41 @@ class ProductProduct(models.Model): } return data - def v2_api_single_response(self, product_product, pricelist=False): + def v2_api_single_response(self, product_product): + product_template = product_product.product_tmpl_id + data = { + 'id': product_product.id, + 'parent': { + 'id': product_template.id, + 'name': product_template.name, + 'image': self.env['ir.attachment'].api_image('product.template', 'image_256', product_template.id), + }, + 'code': product_product.default_code or '', + 'name': product_product.display_name, + 'price': product_product.calculate_website_price(), + 'stock': product_product.qty_stock_vendor, + 'weight': product_product.weight, + 'attributes': [x.name for x in product_product.product_template_attribute_value_ids], + 'manufacture' : self.api_manufacture(product_product) + } + return data + + def calculate_website_price(self): + pricelist = self.env.user_pricelist + config = self.env['ir.config_parameter'] product_pricelist_tier1 = int(config.get_param('product.pricelist.tier1')) product_pricelist_tier2 = int(config.get_param('product.pricelist.tier2')) product_pricelist_tier3 = int(config.get_param('product.pricelist.tier3')) - discount_percentage = product_product._get_website_disc(0) - price_discount = product_product._get_website_price_after_disc_and_tax() + discount_percentage = self._get_website_disc(0) + price_discount = self._get_website_price_after_disc_and_tax() price_tier = False pricelists = { - 'tier1': product_product._get_pricelist_tier1, - 'tier2': product_product._get_pricelist_tier2, - 'tier3': product_product._get_pricelist_tier3, + 'tier1': self._get_pricelist_tier1, + 'tier2': self._get_pricelist_tier2, + 'tier3': self._get_pricelist_tier3, } pricelist_id = pricelist.id if pricelist else False if pricelist_id == product_pricelist_tier1: price_tier = 'tier1' @@ -56,36 +77,11 @@ class ProductProduct(models.Model): if price[discount_key] > 0: discount_percentage = price[discount_key] if price[price_key] > 0: price_discount = price[price_key] - flashsale = product_product._get_flashsale_price() - flashsale_price = flashsale['flashsale_price'] - flashsale_discount = flashsale['flashsale_discount'] - if flashsale_price > 0 and flashsale_price < price_discount: - price_discount = flashsale_price - discount_percentage = flashsale_discount - - stock = product_product.qty_stock_vendor - stock = stock if stock > 0 else 1 - product_template = product_product.product_tmpl_id - data = { - 'id': product_product.id, - 'parent': { - 'id': product_template.id, - 'name': product_template.name, - 'image': self.env['ir.attachment'].api_image('product.template', 'image_256', product_template.id), - }, - 'code': product_product.default_code or '', - 'name': product_product.display_name, - 'price': { - 'price': product_product._get_website_price_exclude_tax(), - 'discount_percentage': discount_percentage, - 'price_discount': price_discount - }, - 'stock': stock, - 'weight': product_product.weight, - 'attributes': [x.name for x in product_product.product_template_attribute_value_ids], - 'manufacture' : self.api_manufacture(product_product) + return { + 'price': self._get_website_price_exclude_tax(), + 'discount_percentage': discount_percentage, + 'price_discount': price_discount } - return data def api_manufacture(self, product_template): if product_template.x_manufacture: diff --git a/indoteknik_api/models/product_template.py b/indoteknik_api/models/product_template.py index 1a345967..68ab79c2 100644 --- a/indoteknik_api/models/product_template.py +++ b/indoteknik_api/models/product_template.py @@ -51,9 +51,7 @@ class ProductTemplate(models.Model): data.update(data_with_detail) return data - def v2_api_single_response(self, product_template, pricelist=False, with_detail=''): - product_pricelist_default_discount_id = self.env['ir.config_parameter'].get_param('product.pricelist.default_discount_id') - product_pricelist_default_discount_id = int(product_pricelist_default_discount_id) + def v2_api_single_response(self, product_template, with_detail=''): data = { 'id': product_template.id, 'image': self.env['ir.attachment'].api_image('product.template', 'image_128', product_template.id), @@ -67,7 +65,7 @@ class ProductTemplate(models.Model): } if with_detail != '': - variants = [self.env['product.product'].v2_api_single_response(variant, pricelist=pricelist) for variant in product_template.product_variant_ids] + variants = [self.env['product.product'].v2_api_single_response(variant) for variant in product_template.product_variant_ids] lowest_price = variants[0]['price'] for variant in variants: if variant["price"]["price_discount"] < lowest_price["price_discount"]: |
