diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-09-11 13:18:02 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-09-11 13:18:02 +0700 |
| commit | 1694e042acb29b476815d29f54f2ec95d37883be (patch) | |
| tree | d502c76d333cc4c0602e01a98eeb7fb7f0461c01 /indoteknik_api/controllers/api_v1 | |
| parent | 02c2304b242245250177fec6ab3c911d9acba781 (diff) | |
| parent | 9a630354c1d00e218595db9b2e8cd0b7df9ed97c (diff) | |
Merge branch 'feature/voucher-group' into production
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/sale_order.py | 22 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/stock_picking.py | 35 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/voucher.py | 84 |
3 files changed, 66 insertions, 75 deletions
diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index 4f6393a6..adc89f66 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -343,28 +343,8 @@ class SaleOrder(controller.Controller): voucher_code = params['value']['voucher'] voucher = request.env['voucher'].search([('code', '=', voucher_code)]) if voucher: - amount_untaxed = 0 - manufacture_ids = [x.id for x in voucher.manufacture_ids] - for line in sale_order.order_line: - manufacture_id = line.product_id.x_manufacture.id or False - if len(manufacture_ids) == 0 or manufacture_id in manufacture_ids: - amount_untaxed += line.price_subtotal - - voucher_discount = voucher.calculate_discount(amount_untaxed) sale_order.voucher_id = voucher.id - sale_order.amount_voucher_disc = voucher_discount - - for line in sale_order.order_line: - manufacture_id = line.product_id.x_manufacture.id or False - if len(manufacture_ids) > 0 and manufacture_id not in manufacture_ids: - continue - voucher_disc_line = line.price_subtotal / amount_untaxed * voucher_discount - line.amount_voucher_disc = voucher_disc_line - - voucher_disc_item = voucher_disc_line / line.product_uom_qty - voucher_disc_unit = line.price_unit - voucher_disc_item - - line.discount += (line.price_unit - voucher_disc_unit) / line.price_unit * 100 + sale_order.apply_voucher() cart_ids = [x['cart_id'] for x in products] user_cart.browse(cart_ids).unlink() diff --git a/indoteknik_api/controllers/api_v1/stock_picking.py b/indoteknik_api/controllers/api_v1/stock_picking.py index e0a60c98..74f4564a 100644 --- a/indoteknik_api/controllers/api_v1/stock_picking.py +++ b/indoteknik_api/controllers/api_v1/stock_picking.py @@ -13,6 +13,7 @@ class StockPicking(controller.Controller): get_params = self.get_request_params(kw, { 'partner_id': ['number'], 'q': [], + 'status': [], 'limit': ['default:0', 'number'], 'offset': ['default:0', 'number'] }) @@ -27,18 +28,35 @@ class StockPicking(controller.Controller): child_ids = request.env['res.partner'].browse(partner_id).get_child_ids() + pending_domain = [('driver_departure_date', '=', False), ('driver_arrival_date', '=', False)] + shipment_domain = [('driver_departure_date', '!=', False), ('driver_arrival_date', '=', False)] + completed_domain = [('driver_departure_date', '!=', False), ('driver_arrival_date', '!=', False)] + picking_model = request.env['stock.picking'] - default_domain = [('partner_id', 'in', child_ids), ('sale_id', '!=', False), ('origin', 'ilike', 'SO%'), ('state', '!=', 'cancel')] + default_domain = [ + ('partner_id', 'in', child_ids), + ('sale_id', '!=', False), + ('origin', 'ilike', 'SO%'), + ('state', '!=', 'cancel') + ] - domain = default_domain + domain = default_domain.copy() if params['q']: query_like = '%' + params['q'].replace(' ', '%') + '%' domain += ['|', '|', ('name', 'ilike', query_like), ('sale_id.client_order_ref', 'ilike', query_like), ('delivery_tracking_no', 'ilike', query_like)] + + if params['status'] == 'pending': + domain += pending_domain + elif params['status'] == 'shipment': + domain += shipment_domain + elif params['status'] == 'completed': + domain += completed_domain stock_pickings = picking_model.search(domain, offset=offset, limit=limit, order='create_date desc') res_pickings = [] for picking in stock_pickings: manifests = picking.get_manifests() + res_pickings.append({ 'id': picking.id, 'name': picking.name, @@ -50,21 +68,18 @@ class StockPicking(controller.Controller): 'client_order_ref': picking.sale_id.client_order_ref or '' }, 'delivered': picking.waybill_id.delivered or picking.driver_arrival_date != False, + 'status': picking.shipping_status, 'carrier_name': picking.carrier_id.name or '', 'last_manifest': next(iter(manifests), None) }) - pending_count = picking_model.search_count(default_domain + [('driver_departure_date', '=', False), ('driver_arrival_date', '=', False)]) - shipment_count = picking_model.search_count(default_domain + [('driver_departure_date', '!=', False), ('driver_arrival_date', '=', False)]) - completed_count = picking_model.search_count(default_domain + [('driver_departure_date', '!=', False), ('driver_arrival_date', '!=', False)]) - return self.response({ 'summary': { - 'pending_count': pending_count, - 'shipment_count': shipment_count, - 'completed_count': completed_count + 'pending_count': picking_model.search_count(default_domain + pending_domain), + 'shipment_count': picking_model.search_count(default_domain + shipment_domain), + 'completed_count': picking_model.search_count(default_domain + completed_domain) }, - 'picking_total': picking_model.search_count(default_domain), + 'picking_total': picking_model.search_count(domain), 'pickings': res_pickings }) diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 0b769ee7..dfe9ceba 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -1,6 +1,7 @@ from .. import controller from odoo import http from odoo.http import request +from bs4 import BeautifulSoup class Voucher(controller.Controller): @@ -11,7 +12,7 @@ class Voucher(controller.Controller): def get_vouchers(self, **kw): cart = request.env['website.user.cart'] code = kw.get('code') - user_id = kw.get('user_id') + user_id = int(kw.get('user_id', 0)) source = kw.get('source') visibility = ['public'] @@ -25,57 +26,52 @@ class Voucher(controller.Controller): parameter += [('visibility', 'in', visibility)] vouchers = request.env['voucher'].get_active_voucher(parameter) - vouchers = vouchers.res_format() checkout = cart.get_user_checkout(user_id, source=source) + products = checkout['products'] - for voucher in vouchers: - apply_status = '' - products = checkout['products'] - min_purchase_amount = voucher['min_purchase_amount'] - can_apply = False - difference_to_apply = 0 - - manufacture_ids = voucher['manufacture_ids'] - subtotal = 0 - has_match_manufacture = False - for product in products: - price = product['price']['price'] - price_discount = product['price']['price_discount'] - quantity = product['quantity'] - manufacture_id = product['manufacture']['id'] or False + user = request.env['res.users'].search([('id', '=', user_id)], limit=1) + if not user: + return self.response([]) - if len(manufacture_ids) == 0 or manufacture_id in manufacture_ids: - has_match_manufacture = True + order_line = [] + for product in products: + 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 - if product['has_flashsale']: - continue + partner_voucher_orders = [] + for order in voucher.order_ids: + if order.partner_id.id == user.partner_id.id: + 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'] - purchase_amt = price * quantity - discount_amt = (price - price_discount) * quantity - subtotal += purchase_amt - discount_amt + valid_order = voucher_info['valid_order'] - has_flashsale_products = any(product['has_flashsale'] for product in products) - if not has_match_manufacture: - apply_status = 'UM' # Unqualified Manufacture - elif subtotal < min_purchase_amount: - apply_status = 'MPA' # Minimum Purchase Amount - if has_flashsale_products: - apply_status += '-HF' # Has Flashsale - else: - can_apply = True + can_apply = True if valid_order and voucher_discount > 0 else False - if subtotal < min_purchase_amount: - difference_to_apply = min_purchase_amount - subtotal + voucher_res = voucher.format() + voucher_res['can_apply'] = can_apply + voucher_res['discount_voucher'] = voucher_discount - obj_voucher = request.env['voucher'].browse(voucher['id']) - discount_voucher = obj_voucher.calculate_discount(subtotal) + 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() - voucher['can_apply'] = can_apply - voucher['apply_status'] = apply_status - voucher['has_flashsale_products'] = has_flashsale_products - voucher['discount_voucher'] = discount_voucher - voucher['difference_to_apply'] = difference_to_apply + results.append(voucher_res) - sorted_vouchers = sorted(vouchers, key=lambda x: x['can_apply'], reverse=True) + sorted_results = sorted(results, key=lambda x: x['can_apply'], reverse=True) - return self.response(sorted_vouchers) + return self.response(sorted_results) |
