diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2023-07-01 11:30:17 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2023-07-01 11:30:17 +0700 |
| commit | fde265040065d233a0b7eee277defb3c472575a2 (patch) | |
| tree | 1ff4c348f2bbfc9de0ede5376215227222bf296c | |
| parent | 577fe5a01f3b782a2a441ad55f3cc721ba2e212c (diff) | |
api sla website
| -rw-r--r-- | indoteknik_api/controllers/api_v1/product.py | 50 | ||||
| -rwxr-xr-x | indoteknik_custom/__manifest__.py | 1 | ||||
| -rwxr-xr-x | indoteknik_custom/models/__init__.py | 1 | ||||
| -rw-r--r-- | indoteknik_custom/models/product_sla.py | 64 | ||||
| -rwxr-xr-x | indoteknik_custom/models/product_template.py | 11 | ||||
| -rwxr-xr-x | indoteknik_custom/security/ir.model.access.csv | 3 | ||||
| -rw-r--r-- | indoteknik_custom/views/product_sla.xml | 53 |
7 files changed, 176 insertions, 7 deletions
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/<id>', 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/<id>', 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 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<odoo> + <record id="product_sla_tree" model="ir.ui.view"> + <field name="name">product.sla.tree</field> + <field name="model">product.sla</field> + <field name="arch" type="xml"> + <tree create="false"> + <field name="product_variant_id"/> + <field name="avg_leadtime"/> + <field name="sla"/> + </tree> + </field> + </record> + + <record id="product_sla_form" model="ir.ui.view"> + <field name="name">product.sla.form</field> + <field name="model">product.sla</field> + <field name="arch" type="xml"> + <form> + <header> + <button name="generate_product_variant_id_sla" + string="Generate Product Sla" + type="object" + class="mr-2 oe_highlight"> + </button> + </header> + <sheet string="Product Sla"> + <group> + <group> + <field name="number"/> + <field name="product_variant_id"/> + <field name="avg_leadtime"/> + <field name="sla"/> + </group> + </group> + </sheet> + </form> + </field> + </record> + + <record id="product_sla_action" model="ir.actions.act_window"> + <field name="name">Product Sla</field> + <field name="type">ir.actions.act_window</field> + <field name="res_model">product.sla</field> + <field name="view_mode">tree,form</field> + </record> + + <menuitem id="menu_product_sla" + name="Product Sla" + action="product_sla_action" + parent="sale.product_menu_catalog" + sequence="7"/> +</odoo>
\ No newline at end of file |
