diff options
| -rw-r--r-- | indoteknik_api/controllers/api_v1/product.py | 46 | ||||
| -rwxr-xr-x | indoteknik_custom/__manifest__.py | 1 | ||||
| -rwxr-xr-x | indoteknik_custom/models/__init__.py | 2 | ||||
| -rw-r--r-- | indoteknik_custom/models/product_sla.py | 87 | ||||
| -rwxr-xr-x | indoteknik_custom/models/product_template.py | 66 | ||||
| -rw-r--r-- | indoteknik_custom/models/token_storage.py | 9 | ||||
| -rwxr-xr-x | indoteknik_custom/security/ir.model.access.csv | 4 | ||||
| -rw-r--r-- | indoteknik_custom/views/product_sla.xml | 46 |
8 files changed, 258 insertions, 3 deletions
diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py index 54b039c7..3908ecc2 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,47 @@ _logger = logging.getLogger(__name__) class Product(controller.Controller): prefix = '/api/v1/' + @http.route(prefix + '/api/v1/product_variant/<id>/stock', auth='public', methods=['GET', 'OPTIONS']) + def get_product_template_stock_by_id(self, **kw): + id = kw.get('id') + product = request.env['product.product'].search([('id', '=', id)], limit=1) + product_sla = request.env['product.sla'].search([('product_variant_id', '=', id)], limit=1) + + qty_available = product.qty_onhand_bandengan + qty_available -= 10 + if qty_available < 10: + qty_available = 0 + + qty = 0 + sla_date = '-' + + is_altama_product = product.x_manufacture.id in [10,122,89] + qty_altama = request.env['product.template'].get_stock_altama(product.default_code) + qty_altama -= int(qty_altama * 0.1) + qty_altama = math.ceil(float(qty_altama)) + if is_altama_product: + if qty_available > 10: + qty = qty_altama + qty_available + sla_date = '1 Hari' + else: + if qty_altama > 0: + qty = qty_altama + sla_date = '2-4 Hari' + else: + sla_date = 'Indent' + else: + if qty_available > 0: + qty = qty_available + sla_date = product_sla.sla or '-' + + 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 +96,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 +132,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 79f6e195..11921285 100755 --- a/indoteknik_custom/__manifest__.py +++ b/indoteknik_custom/__manifest__.py @@ -77,6 +77,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 166d43ad..4af46dae 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -63,4 +63,6 @@ from . import procurement_monitoring_detail from . import brand_vendor from . import manufacturing from . import requisition +from . import token_storage +from . import product_sla from . import account_move_due_extension diff --git a/indoteknik_custom/models/product_sla.py b/indoteknik_custom/models/product_sla.py new file mode 100644 index 00000000..f969502f --- /dev/null +++ b/indoteknik_custom/models/product_sla.py @@ -0,0 +1,87 @@ +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' + _rec_name = 'product_variant_id' + + 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) + version = fields.Integer(string="Version", compute="_compute_version") + + def _compute_version(self): + for sla in self: + sla.version = sla.product_variant_id.sla_version + + def generate_product_variant_id_sla(self, limit=5000): + # Filter produk non-Altama + products = self.env['product.product'].search([ + ('x_manufacture', 'not in', [10, 122, 89]), + ('location_id', '=', 57), + ('stock_move_ids', '!=', False), + ], order='sla_version asc', limit=limit) + + i = 1 + for product in products: + _logger.info(f'Product SLA: {i}/{len(products)}') + i += 1 + product.sla_version += 1 + + product_sla = self.search([('product_variant_id', '=', product.id)], limit=1) + if not product_sla: + product_sla = self.env['product.sla'].create({ + 'product_variant_id': product.id, + }) + + product_sla.generate_product_sla() + + def generate_product_sla(self): + self.avg_leadtime = '-' + self.sla = '-' + + product = self.product_variant_id + + qty_available = 0 + qty_available = product.qty_onhand_bandengan + qty_available -= 10 + + if qty_available < 10: + qty_available = 0 + + if qty_available > 10: + self.sla = '1 Hari' + + query = [ + ('product_id', '=', product.id), + ('picking_id', '!=', False), + ('picking_id.location_id', '=', 57), + ('picking_id.state', 'not in', ['cancel']) + ] + picking = self.env['stock.move.line'].search(query) + leadtimes=[] + for stock in picking: + 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(avg_leadtime) + self.avg_leadtime = rounded_leadtime + if rounded_leadtime >= 1 and rounded_leadtime <= 5: + self.sla = '3-6 Hari' + elif rounded_leadtime >= 6 and rounded_leadtime <= 10: + self.sla = '4-12 Hari' + elif rounded_leadtime >= 11: + self.sla = 'Indent'
\ No newline at end of file diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index a4f8f031..1a83b702 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -2,6 +2,8 @@ from odoo import fields, models, api from datetime import datetime, timedelta from odoo.exceptions import AccessError, UserError, ValidationError import logging +import requests +import json _logger = logging.getLogger(__name__) @@ -190,7 +192,70 @@ class ProductTemplate(models.Model): product.product_rating = rate product.last_calculate_rating = current_time + def _get_stock_website(self): + qty = self._get_stock_altama() + print(qty) + 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)] + token_data = self.env['token.storage'].search(query, order='expired_date desc',limit=1) + if not token_data: + token_data = self._get_new_token_altama() + token = token_data['access_token'] + else: + token = token_data.access_token + + url = "https://erpapi.altama.co.id/erp/api/stock/buffer/btob" + auth = "Bearer "+token + headers = { + 'Content-Type': 'application/json', + 'Authorization': auth, + } + json_data = { + 'type_search': 'Item_code', + '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: + 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" + auth = 'Basic SW5kb3Rla25pa19DbGllbnQ6Vm1iZExER1ZUS3RuVlRQdkU1MXRvRzdiTW51TE1WRVI=' + headers = { + 'Content-Type': 'application/x-www-form-urlencoded', + 'Authorization': auth, + } + data = { + 'grant_type': 'client_credentials', + } + + response = requests.post(url, headers=headers, data=data).json() + lookup_json = json.dumps(response, indent=4, sort_keys=True) + token = json.loads(lookup_json)['access_token'] + expires_in = json.loads(lookup_json)['expires_in'] + + current_time = datetime.now() + delta_time = current_time + timedelta(seconds=int(expires_in)) + + current_time = current_time.strftime('%Y-%m-%d %H:%M:%S') + delta_time = delta_time.strftime('%Y-%m-%d %H:%M:%S') + + values = { + 'source': 'altama', + 'access_token': token, + 'expires_in': expires_in, + 'expired_date': delta_time, + } + self.env['token.storage'].create([values]) + return values class ProductProduct(models.Model): @@ -213,6 +278,7 @@ class ProductProduct(models.Model): material = fields.Char(string='Material') qty_onhand_bandengan = fields.Float(string='Qty Incoming Bandengan', compute='_get_qty_onhand_bandengan') qty_incoming_bandengan = fields.Float(string='Qty Incoming Bandengan', compute='_get_qty_incoming_bandengan') + sla_version = fields.Integer(string="SLA Version", default=0) def _get_qty_incoming_bandengan(self): for product in self: diff --git a/indoteknik_custom/models/token_storage.py b/indoteknik_custom/models/token_storage.py new file mode 100644 index 00000000..0eef7f20 --- /dev/null +++ b/indoteknik_custom/models/token_storage.py @@ -0,0 +1,9 @@ +from odoo import fields, models + +class TokenStorage(models.Model): + + _name = 'token.storage' + source = fields.Char(string='Source') + access_token = fields.Char(string='Access Token') + expires_in = fields.Integer(string='Expires In') + expired_date = fields.Datetime(string='Expired Date') diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index df820053..a470fa8f 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -53,4 +53,6 @@ access_rajaongkir_kurir,access.rajaongkir.kurir,model_rajaongkir_kurir,,1,1,1,1 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
\ No newline at end of file +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 +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..f91cf97c --- /dev/null +++ b/indoteknik_custom/views/product_sla.xml @@ -0,0 +1,46 @@ +<?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> + <sheet string="Product Sla"> + <group> + <group> + <field name="product_variant_id"/> + <field name="avg_leadtime"/> + <field name="sla"/> + <field name="version"/> + </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 |
