From fde265040065d233a0b7eee277defb3c472575a2 Mon Sep 17 00:00:00 2001 From: Azka Nathan Date: Sat, 1 Jul 2023 11:30:17 +0700 Subject: api sla website --- indoteknik_api/controllers/api_v1/product.py | 50 +++++++++++++++++++- indoteknik_custom/__manifest__.py | 1 + indoteknik_custom/models/__init__.py | 1 + indoteknik_custom/models/product_sla.py | 64 ++++++++++++++++++++++++++ indoteknik_custom/models/product_template.py | 11 +++-- indoteknik_custom/security/ir.model.access.csv | 3 +- indoteknik_custom/views/product_sla.xml | 53 +++++++++++++++++++++ 7 files changed, 176 insertions(+), 7 deletions(-) create mode 100644 indoteknik_custom/models/product_sla.py create mode 100644 indoteknik_custom/views/product_sla.xml diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 54b039c7..8dea4c28 100644 --- a/indoteknik_api/controllers/api_v1/product.py +++ b/indoteknik_api/controllers/api_v1/product.py @@ -4,6 +4,7 @@ from odoo.http import request from datetime import datetime, timedelta import ast import logging +import math _logger = logging.getLogger(__name__) @@ -11,6 +12,51 @@ _logger = logging.getLogger(__name__) class Product(controller.Controller): prefix = '/api/v1/' + @http.route(prefix + 'product/template/stock/', auth='public', methods=['GET', 'OPTIONS']) + def get_product_template_stock_by_id(self, **kw): + id = kw.get('id') + stock_location_id = 57 # Ubah dengan location_id yang diinginkan + template = request.env['product.product'].search([('id', '=', id)], limit=1) + stock = template.qty_available + item_code = template.default_code + qty_altama = request.env['product.template'].get_stock_altama(item_code) + qty_altama -= int(qty_altama * 0.1) + rounded_qty_altama = math.ceil(float(qty_altama)) + qty_b2b_altama = rounded_qty_altama + + qty = 0 + sla_date = '-' + qty_available = 0 + + for stock in template.stock_quant_ids: + if stock.location_id.id == 57: + qty_available += stock.quantity + + if qty_available == 0: + if qty_b2b_altama > 0: + qty = qty_b2b_altama + sla_date = '2-4 Hari' + else: + qty = 0 + sla_date = 'Indent' + elif qty_available > 10: + if template.x_manufacture.id == 10 or template.x_manufacture.id == 122 or template.x_manufacture.id == 89: + qty = qty_available + qty_b2b_altama + else: + qty = qty_available + sla_date = '1 Hari' + elif qty_available < 10: + qty = 0 + sla_date = '-' + + data = { + 'qty': qty, + 'sla_date': sla_date, + } + + return self.response(data, headers=[('Cache-Control', 'max-age=180, private')]) + + @http.route(prefix + 'product/template/price/', auth='public', methods=['GET', 'OPTIONS']) def get_product_template_price_by_id(self, **kw): if not self.authenticate(): @@ -54,7 +100,7 @@ class Product(controller.Controller): if not self.authenticate(): return self.response(code=401, description='Unauthorized') id = kw.get('id') - partner_id = int(kw.get('partner_id', 0)) + partner_id = int(kw.get('partner_id', 0)) product_product = request.env['product.product'].search([('id', '=', id)], limit=1) data = { @@ -90,7 +136,7 @@ class Product(controller.Controller): # ('create_date', '>=', delta_time), ] new_products = request.env['product.template'].search(query_products, order='create_date desc', limit=limit_new_products) - brands = [] + brands = [] for product in new_products: brands.append(product.x_manufacture) brands = list(dict.fromkeys(brands)) diff --git a/indoteknik_custom/__manifest__.py b/indoteknik_custom/__manifest__.py index eaa5390a..bba38a53 100755 --- a/indoteknik_custom/__manifest__.py +++ b/indoteknik_custom/__manifest__.py @@ -76,6 +76,7 @@ 'views/brand_vendor.xml', 'views/requisition.xml', 'views/landedcost.xml', + 'views/product_sla.xml', 'report/report.xml', 'report/report_banner_banner.xml', 'report/report_banner_banner2.xml', diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 4cc2433f..8fd33b4a 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -64,3 +64,4 @@ from . import brand_vendor from . import manufacturing from . import requisition from . import token_storage +from . import product_sla diff --git a/indoteknik_custom/models/product_sla.py b/indoteknik_custom/models/product_sla.py new file mode 100644 index 00000000..00df2fa2 --- /dev/null +++ b/indoteknik_custom/models/product_sla.py @@ -0,0 +1,64 @@ +from odoo import models, api, fields +from odoo.exceptions import AccessError, UserError, ValidationError +from datetime import timedelta, date +import logging +import math +_logger = logging.getLogger(__name__) + + +class ProductSla(models.Model): + _name = 'product.sla' + _description = 'Product Sla' + product_variant_id = fields.Many2one('product.product',string='Product') + avg_leadtime = fields.Char(string='AVG Leadtime', readonly=True) + leadtime = fields.Char(string='Leadtime', readonly=True) + sla = fields.Char(string='SLA', readonly=True) + + def generate_product_variant_id_sla(self): + # Filter produk non-Altama + products = self.env['stock.move.line'].search([ + # ('product_id', '=', self.product_variant_id.id), + ('product_id.x_manufacture', 'not in', [10,122,89]), + ('picking_id.driver_departure_date', '!=', False) + ]) + + for product in products: + sla = self.env['product.sla'].create({ + 'product_variant_id': product.product_id.id, + }) + + sla.generate_product_sla() + + def generate_product_sla(self): + for sla in self: + query = [ + ('product_id', '=', sla.product_variant_id.id), + ('picking_id', '!=', False), + ('picking_id.state', '=', 'done'), + ] + picking = self.env['stock.move.line'].search(query) + leadtimes=[] + for stock in picking: + if not stock.picking_id.driver_departure_date: + continue + date_delivered = stock.picking_id.driver_departure_date + date_so_confirmed = stock.picking_id.sale_id.date_order + if date_delivered and date_so_confirmed: + leadtime = date_delivered - date_so_confirmed + leadtime_in_days = leadtime.days + leadtimes.append(leadtime_in_days) + if len(leadtimes) > 0: + avg_leadtime = sum(leadtimes)/len(leadtimes) + rounded_leadtime = math.ceil(float(avg_leadtime)) + sla.avg_leadtime = rounded_leadtime + + if picking.picking_id.location_id == 57 and picking.picking_id.qty_available > 10: + sla.sla = '1 Hari' + elif sla.avg_leadtime and float(sla.avg_leadtime) >= 1 and float(sla.avg_leadtime) <= 5: + sla.sla = '3-6 Hari' + elif sla.avg_leadtime and float(sla.avg_leadtime) >= 6 and float(sla.avg_leadtime) <= 10: + sla.sla = '4-12 Hari' + elif sla.avg_leadtime and float(sla.avg_leadtime) >= 11: + sla.sla = 'Indent' + else: + sla.sla = '-' \ No newline at end of file diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index 5fbac2e3..8247cd7e 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -196,7 +196,7 @@ class ProductTemplate(models.Model): qty = self._get_stock_altama() print(qty) - def _get_stock_altama(self): + def get_stock_altama(self, item_code): current_time = datetime.now() current_time = current_time.strftime('%Y-%m-%d %H:%M:%S') query = [('source', '=', 'altama'), ('expired_date', '>', current_time)] @@ -215,13 +215,16 @@ class ProductTemplate(models.Model): } json_data = { 'type_search': 'Item_code', - 'search_key':['RSG100-3'], + 'search_key':[item_code], } response = requests.post(url, headers=headers, json=json_data) datas = json.loads(response.text)['data'] + qty = 0 for data in datas: - qty = data['availability'] - return qty or 0 + availability = int(data['availability']) # Mengonversi ke tipe data int + qty += availability # Mengakumulasi qty dari setiap data + + return qty def _get_new_token_altama(self): url = "https://kc.altama.co.id/realms/altama/protocol/openid-connect/token" diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index fb8f48f1..d809f3c4 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -52,4 +52,5 @@ access_brand_vendor,access.brand.vendor,model_brand_vendor,,1,1,1,1 access_requisition,access.requisition,model_requisition,,1,1,1,1 access_requisition_line,access.requisition.line,model_requisition_line,,1,1,1,1 access_requisition_purchase_match,access.requisition.purchase.match,model_requisition_purchase_match,,1,1,1,1 -access_token_storage,access.token_storage,model_token_storage,,1,1,1,1 \ No newline at end of file +access_token_storage,access.token_storage,model_token_storage,,1,1,1,1 +access_product_sla,access.product_sla,model_product_sla,,1,1,1,1 \ No newline at end of file diff --git a/indoteknik_custom/views/product_sla.xml b/indoteknik_custom/views/product_sla.xml new file mode 100644 index 00000000..378a977b --- /dev/null +++ b/indoteknik_custom/views/product_sla.xml @@ -0,0 +1,53 @@ + + + + product.sla.tree + product.sla + + + + + + + + + + + product.sla.form + product.sla + +
+
+ +
+ + + + + + + + + + +
+
+
+ + + Product Sla + ir.actions.act_window + product.sla + tree,form + + + +
\ No newline at end of file -- cgit v1.2.3