diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2022-10-05 15:19:27 +0700 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2022-10-05 15:19:27 +0700 |
| commit | 14cb30c3f2fb8b15baaf32ab9fca7bb9bda73845 (patch) | |
| tree | e4039e13e934db6ee148f766882bb82af8db1d7a /indoteknik_api/controllers | |
| parent | d1bc570eae2818bc4b535840f2eb3061b99ca98b (diff) | |
Update struktur API and fitur API flash sale
Diffstat (limited to 'indoteknik_api/controllers')
| -rw-r--r-- | indoteknik_api/controllers/__init__.py | 3 | ||||
| -rw-r--r-- | indoteknik_api/controllers/controller.py | 48 | ||||
| -rw-r--r-- | indoteknik_api/controllers/product_controller.py | 142 | ||||
| -rw-r--r-- | indoteknik_api/controllers/sale_order_controller.py | 110 |
4 files changed, 303 insertions, 0 deletions
diff --git a/indoteknik_api/controllers/__init__.py b/indoteknik_api/controllers/__init__.py new file mode 100644 index 00000000..4c317fdc --- /dev/null +++ b/indoteknik_api/controllers/__init__.py @@ -0,0 +1,3 @@ +from . import controller +from . import product_controller +from . import sale_order_controller diff --git a/indoteknik_api/controllers/controller.py b/indoteknik_api/controllers/controller.py new file mode 100644 index 00000000..fb5f1fce --- /dev/null +++ b/indoteknik_api/controllers/controller.py @@ -0,0 +1,48 @@ +import datetime +import base64 +import json + +from odoo import http +from odoo.http import request +from pytz import timezone + + +class Controller(http.Controller): + def authenticate(self, kw): + db = kw.get('db') + username = kw.get('username') + password = kw.get('password') + request.session.authenticate(db, username, password) + + def time_to_str(self, object, format): + time = '' + if isinstance(object, datetime.datetime): + time = object.astimezone(timezone('Asia/Jakarta')).strftime(format) + return time + + def response(self, data=[], code=200, description='OK'): + response = { + 'status': { + 'code': code, + 'description': description + } + } + if code == 200: + response.update({'result': data}) + + response = json.dumps(response) + return request.make_response(response, [('Content-Type', 'application/json')]) + + def search_with_api_params(self, model: str, kw, domain=[]): + """ To search data by default API Params if exist """ + limit = kw.get('limit', 0) + offset = kw.get('offset', 0) + order = kw.get('order', '') + + return request.env[model].search(domain, limit=int(limit), offset=int(offset), order=order) + + @http.route('/api/image/<model>/<field>/<id>', auth='public', methods=['GET']) + def get_image(self, model, field, id): + model = request.env[model].sudo().search([('id', '=', id)], limit=1) + image = model[field] if model[field] else '' + return request.make_response(base64.b64decode(image), [('Content-Type', 'image/jpg')]) diff --git a/indoteknik_api/controllers/product_controller.py b/indoteknik_api/controllers/product_controller.py new file mode 100644 index 00000000..a127b790 --- /dev/null +++ b/indoteknik_api/controllers/product_controller.py @@ -0,0 +1,142 @@ +from datetime import datetime +from . import controller +from odoo import http +from odoo.http import request + + +class ProductController(controller.Controller): + prefix_url = '/api/product' + + # TODO: separate function for get manufacture and promotion by product_id + @http.route(prefix_url + '/search', auth='public', methods=['GET']) + def search_product(self, **kw): + self.authenticate(kw) + base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url') + + query = kw.get('query') + if not query: + return self.response(code=400, description='Field query is required') + + query = '%' + query.replace(' ', '%') + '%' + domain = [ + ('sale_ok', '=', True), + '|', + ('default_code', 'ilike', query), + ('name', 'ilike', query) + ] + + manufactures = kw.get('manufactures') + if manufactures: + manufactures = [int(x) for x in manufactures.split(',')] + domain.append(('x_manufacture', 'in', manufactures)) + + categories = kw.get('categories') + if categories: + categories = [int(x) for x in categories.split(',')] + domain.append(('public_categ_ids', 'child_of', categories)) + + product_variants = request.env['product.product'].search(domain) + product_variant_ids = [x.id for x in product_variants] + + domain = [('product_variant_ids', 'in', product_variant_ids)] + product_templates = self.search_with_api_params('product.template', kw, domain) + data = { + 'total_records': len(request.env['product.template'].search(domain)), + 'products': [] + } + for product_template in product_templates: + discount_price = 0 + price = product_template.web_price + if price > 0: + if product_template.taxes_id: + if not product_template.taxes_id.price_include: + price += (price * product_template.taxes_id.amount / 100) + else: + price += (price * 11 / 100) + + promotion = self.get_promotion_by_product(product_template) + if len(promotion) > 0: + discount_price = price - (price * promotion['discount_percentage'] / 100) + + manufacture = self.get_manufacture_by_product(product_template) + stock = self.get_stock_by_product(product_template) + + # TODO: remove price and discount_price if old indoteknik not use + data['products'].append({ + 'id': product_template.id, + 'image': base_url + 'api/image/product.template/image_128/' + str(product_template.id) if product_template.image_128 else '', + 'name': product_template.name, + # 'lowest_price': request.env['product.pricelist'].get_lowest_product_variant_price(product_template.id, 4), + 'price': price, + 'discount_price': discount_price, + 'total_variant': len(product_template.product_variant_ids), + 'stock': stock, + 'manufacture': manufacture, + 'promotion': promotion, + }) + return self.response(data) + + @http.route(prefix_url + '/flash_sale', auth='public', methods=['GET']) + def get_flash_sale_product(self, **kw): + self.authenticate(kw) + base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url') + product_pricelist_default = request.env['ir.config_parameter'].sudo().get_param('product.pricelist.default') + active_flash_sale = request.env['product.pricelist'].get_active_flash_sale() + data = {} + if active_flash_sale: + flash_sale = { + 'banner': base_url + 'api/image/product.pricelist/banner/' + str(active_flash_sale.id) if active_flash_sale.banner else '', + 'duration': (active_flash_sale.end_date - datetime.now()).total_seconds(), + 'products': [] + } + product_pricelist_item = request.env['product.pricelist.item'].search([('pricelist_id', '=', active_flash_sale.id)]) + product_variant_ids = [x.product_id.id for x in product_pricelist_item] + product_templates = self.search_with_api_params('product.template', kw, [('product_variant_ids', 'in', product_variant_ids)]) + for product in product_templates: + flash_sale['products'].append({ + 'id': product.id, + 'name': product.name, + 'image': base_url + 'api/image/product.template/image_128/' + str(product.id) if product.image_128 else '', + 'lowest_price': request.env['product.pricelist'].get_lowest_product_variant_price(product.id, int(product_pricelist_default)), + 'stock': self.get_stock_by_product(product), + 'total_variant': len(product.product_variant_ids), + 'manufacture': self.get_manufacture_by_product(product), + 'promotion': self.get_promotion_by_product(product), + }) + data.update({'flash_sale': flash_sale}) + return self.response(data) + + def get_stock_by_product(self, product_template: object): + stock = 0 + for product_variant in product_template.product_variant_ids: + stock += product_variant.qty_stock_vendor + return stock + + def get_manufacture_by_product(self, product_template: object): + manufacture = {} + if product_template.x_manufacture: + manufacture.update({ + 'id': product_template.x_manufacture.id, + 'name': product_template.x_manufacture.x_name, + }) + return manufacture + + def get_promotion_by_product(self, product_template: object): + base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url') + promotion = {} + if product_template.x_manufacture: + domain = [ + ('rule_products_domain', 'ilike', product_template.x_manufacture.x_name), + ('active', '=', True) + ] + coupon_program = request.env['coupon.program'].search(domain, limit=1) + if coupon_program: + icon_1 = (base_url + 'api/image/coupon.program/x_studio_field_Ifopn/' + str(coupon_program.id)) if coupon_program.x_studio_field_Ifopn else '' + icon_2 = (base_url + 'api/image/coupon.program/x_studio_field_2Ul77/' + str(coupon_program.id)) if coupon_program.x_studio_field_2Ul77 else '' + promotion.update({ + 'name': coupon_program.name, + 'discount_percentage': coupon_program.discount_percentage, + 'icon_1': icon_1, + 'icon_2': icon_2 + }) + return promotion
\ No newline at end of file diff --git a/indoteknik_api/controllers/sale_order_controller.py b/indoteknik_api/controllers/sale_order_controller.py new file mode 100644 index 00000000..741d4bf8 --- /dev/null +++ b/indoteknik_api/controllers/sale_order_controller.py @@ -0,0 +1,110 @@ +from . import controller +from odoo import http +from odoo.http import request + + +class SaleOrderController(controller.Controller): + @http.route('/api/sale_order/invoiced', auth='public', methods=['GET']) + def get_sale_order_invoiced_by_partner_id(self, **kw): + self.authenticate(kw) + partner_id = kw.get('partner_id') + if not partner_id: + return self.response(code=400, description='Field partner_id is required') + + partner_id = int(partner_id) + # Get company member by partner_id + parent_partner_id = request.env['res.partner'].search([('id', '=', partner_id)], limit=1).parent_id.id + partner_childs = request.env['res.partner'].search([('parent_id', '=', int(parent_partner_id))]) + partner_child_ids = [v['id'] for v in partner_childs] + [partner_id] + + # Get sale order by company member and invoiced + data = [] + default_domain = [ + ('partner_id', 'in', partner_child_ids), + '|', + ('invoice_status', '=', 'invoiced'), + ('invoice_status', '=', 'to_invoice') + ] + sale_orders = self.search_with_api_params('sale.order', kw, default_domain) + for sale_order in sale_orders: + pickings = [] + for picking in sale_order.picking_ids: + if picking.state in ['confirmed', 'assigned', 'done']: + pickings.append({ + 'id': picking.id, + 'name': picking.name, + 'delivery_address': picking.partner_id.street, + 'delivery_tracking_no': picking.delivery_tracking_no, + 'delivery_status': picking.delivery_status + }) + + data.append({ + 'id': sale_order.id, + 'name': sale_order.name, + 'amount_total': sale_order.amount_total, + 'salesperson': sale_order.user_id.name, + 'date_order': self.time_to_str(sale_order.date_order, '%d/%m/%Y'), + 'pickings': pickings, + 'access_token': sale_order.access_token + }) + return self.response(data) + + @http.route('/api/sale_order/invoiced/detail', auth='public', methods=['GET']) + def get_sale_order_invoiced_detail_by_partner(self, **kw): + self.authenticate(kw) + + id = kw.get('id') + partner_id = kw.get('partner_id') + if not id: + return self.response(code=400, description='Field id is required') + if not partner_id: + return self.response(code=400, description='Field partner_id is required') + + default_domain = [ + ('id', '=', id), + '|', + ('invoice_status', '=', 'invoiced'), + ('invoice_status', '=', 'to_invoice') + ] + parent_partner_id = request.env['res.partner'].search([('id', '=', int(partner_id))], limit=1).parent_id.id + partner_childs = request.env['res.partner'].search([('parent_id', '=', int(parent_partner_id))]) + partner_child_ids = [v['id'] for v in partner_childs] + [int(partner_id)] + default_domain.append(('partner_id', 'in', partner_child_ids)) + + sale_order = self.search_with_api_params('sale.order', kw, default_domain) + orders = [] + for order in sale_order.order_line: + orders.append({ + 'name': order.name, + 'product_qty': order.product_qty, + 'price_unit': order.price_unit, + 'price_tax': order.price_tax, + 'price_total': order.price_total, + 'price_subtotal': order.price_subtotal, + 'tax': order.tax_id.name, + 'discount': order.discount, + }) + + data = { + 'id': sale_order.id, + 'name': sale_order.name, + 'carrier': sale_order.carrier_id.name, + 'partner': { + 'id': sale_order.partner_id.id, + 'name': sale_order.partner_id.name, + 'mobile': sale_order.partner_id.mobile, + 'email': sale_order.partner_id.email + }, + 'delivery_address': sale_order.partner_shipping_id.street, + 'delivery_method': sale_order.carrier_id.name, + 'payment_term': sale_order.payment_term_id.name, + 'salesperson': sale_order.user_id.name, + 'date_order': self.time_to_str(sale_order.date_order, '%d/%m/%Y %H:%M:%S'), + 'note': sale_order.note, + 'amount_untaxed': sale_order.amount_untaxed, + 'amount_tax': sale_order.amount_tax, + 'amount_total': sale_order.amount_total, + 'orders': orders + } + + return self.response(data) |
