summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortrisusilo48 <tri.susilo@altama.co.id>2025-01-28 09:46:48 +0700
committertrisusilo48 <tri.susilo@altama.co.id>2025-01-28 09:46:48 +0700
commit2644e259339cd921babf49218aadbdcedb0c8937 (patch)
tree64e0c1c5daa37ecc222c912522419a1b6193c381
parent464292a0eb2b9353f499a541991e4d88a76c2d82 (diff)
change product sla
-rw-r--r--indoteknik_api/controllers/api_v1/product.py72
-rw-r--r--indoteknik_custom/models/product_sla.py114
-rw-r--r--indoteknik_custom/views/product_sla.xml5
3 files changed, 100 insertions, 91 deletions
diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py
index 7570015f..5d564ff9 100644
--- a/indoteknik_api/controllers/api_v1/product.py
+++ b/indoteknik_api/controllers/api_v1/product.py
@@ -34,31 +34,34 @@ class Product(controller.Controller):
categories.reverse()
return self.response(categories, headers=[('Cache-Control', 'max-age=3600, public')])
- @http.route(prefix + 'product/variants/sla', auth='none', type='json', csrf=False, cors='*', methods=['GET', 'OPTIONS'])
+ @http.route(prefix + 'product/variants/sla', auth='public', methods=['GET', 'OPTIONS'])
@controller.Controller.must_authorized()
- def get_product_template_sla_by_id(self, **kw):
- json_raw = json.loads(request.httprequest.data)
+ def get_product_template_sla_by_id(self, **kwargs):
+ body_params = kwargs.get('ids')
- ids = json_raw.get('ids')
- ids = list(map(int, ids))
+ if not body_params:
+ return self.response('Failed', code=400, description='id is required')
- if not ids or not isinstance(ids, list):
- return ({'status' : 'Failed','message': 'Parameter "ids" harus berupa list dan tidak boleh kosong.'})
+ ids = [int(id.strip()) for id in body_params.split(',') if id.strip().isdigit()]
- sla_days = 0
+ sla_duration = 0
+ sla_unit = 'Hari'
+ include_instant = True
products = request.env['product.product'].search([('id', 'in', ids)])
if len(products) < 1:
- return ({
- 'status' : 'Failed',
- 'message' : 'Produk Tidak Di Temukan.'
- })
+ return self.response(
+ 'Failed',
+ code=400,
+ description='Produk Tidak Di Temukan.'
+ )
product_slas = request.env['product.sla'].search([('product_variant_id', 'in', ids)])
if len(product_slas) < 1:
- return ({
- 'status' : 'Failed',
- 'message' : 'Produk Tidak Di Temukan.'
- })
+ return self.response(
+ 'Failed',
+ code=400,
+ description='SLA Tidak Di Temukan.'
+ )
# Mapping SLA untuk mempermudah lookup
sla_map = {sla.product_variant_id.id: sla for sla in product_slas}
@@ -66,24 +69,20 @@ class Product(controller.Controller):
for product in products:
product_sla = sla_map.get(product.id)
if product_sla:
- sla_days = max(sla_days, product_sla.sla_vendor_id.duration)
+ sla_duration = max(sla_duration, int(product_sla.sla))
+ sla_unit = product_sla.sla_vendor_id.unit
if product.qty_free_bandengan < 1 :
if product_sla.sla_vendor_id.unit != 'jam':
- return ({
- 'status' : 'Success',
- 'data' : [{
- 'include_instant': False,
- 'sla_days': sla_days
- }],
- })
+ include_instant = False
+ break
+
# Jika semua loop selesai tanpa include_instant menjadi False
- return ({
- 'status' : 'Success',
- 'data' : [{
- 'include_instant': True,
- 'sla_days': sla_days
- }],
- })
+ return self.response({
+ 'include_instant': include_instant,
+ 'sla_duration': sla_duration,
+ 'sla_unit': sla_unit
+ }
+ )
@http.route(prefix + 'product_variant/<id>/stock', auth='public', methods=['GET', 'OPTIONS'])
@controller.Controller.must_authorized()
@@ -98,18 +97,12 @@ class Product(controller.Controller):
('product_variant_id', '=', id),
('write_date', '>=', date_7_days_ago.strftime("%Y-%m-%d %H:%M:%S"))
], limit=1)
-
- include_instant = False
qty_available = product.qty_free_bandengan
- if qty_available > 0 :
- include_instant = True
- else :
+ if qty_available < 1 :
qty_available = 0
- if product_sla.sla_vendor_id.unit == 'jam':
- include_instant = True
qty = 0
sla_date = '-'
@@ -150,8 +143,7 @@ class Product(controller.Controller):
data = {
'qty': qty,
- 'sla_date': sla_date,
- 'can_instant': include_instant
+ 'sla_date': sla_date
}
return self.response(data, headers=[('Cache-Control', 'max-age=600, private')])
diff --git a/indoteknik_custom/models/product_sla.py b/indoteknik_custom/models/product_sla.py
index 988aa78f..04ad2ffd 100644
--- a/indoteknik_custom/models/product_sla.py
+++ b/indoteknik_custom/models/product_sla.py
@@ -14,51 +14,53 @@ class ProductSla(models.Model):
product_variant_id = fields.Many2one('product.product',string='Product')
sla_vendor_id = fields.Many2one('vendor.sla',string='Vendor', readonly=True)
sla_vendor_duration = fields.Char(string='AVG Leadtime', related='sla_vendor_id.duration_unit')
- avg_leadtime = fields.Char(string='AVG Leadtime', readonly=True)
- leadtime = fields.Char(string='Leadtime', readonly=True)
+ sla_logistic = fields.Char(string='SLA Logistic', readonly=True)
+ sla_logistic_unit = fields.Selection(
+ [('jam', 'Jam'),('hari', 'Hari')],
+ string="SLA Logistic Time"
+ )
+ sla_logistic_duration_unit = fields.Char(string="SLA Logistic Duration (Unit)")
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
-
+ def generate_product_variant_id_sla(self, limit=500):
+ offset = 0
+ # while True:
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)
+ ('active', '=', True),
+ ('sale_ok', '=', True),
+ ], order='sla_version asc', limit=limit, offset=offset)
+
+ # if not products:
+ # break
- i = 1
for product in products:
- _logger.info(f'Product SLA: {i}/{len(products)}')
- i += 1
- product.sla_version += 1
+ _logger.info(f'Memproses SLA untuk produk ID {product.id}, versi {product.sla_version}')
product_sla = self.search([('product_variant_id', '=', product.id)], limit=1)
- print(product_sla.id, product.id)
if not product_sla:
- product_sla = self.env['product.sla'].create({
- 'product_variant_id': product.id,
- })
-
+ product_sla = self.create({'product_variant_id': product.id})
+
product_sla.generate_product_sla()
+ # Tandai produk sebagai sudah diproses
+ product.sla_version += 1
+
+ offset += limit
+
+
def generate_product_sla(self):
- self.avg_leadtime = '-'
- self.sla = '-'
+ # self.sla_logistic = '-'
+ # self.sla_logistic_duration_unit = '-'
+ # self.sla = '-'
product = self.product_variant_id
-
- # qty_available = 0
- # qty_available = product.qty_onhand_bandengan
-
-
- # if qty_available > 0:
- # self.sla = 1
q_vendor = [
('product_id', '=', product.id),
@@ -68,6 +70,8 @@ class ProductSla(models.Model):
vendor = self.env['purchase.pricelist'].search(q_vendor)
vendor_duration = 0
+
+ #SLA Vendor
if vendor:
vendor_sla = self.env['vendor.sla'].search([('id_vendor', '=', vendor.vendor_id.id)], limit=1)
sla_vendor = int(vendor_sla.duration) if vendor_sla else 0
@@ -78,28 +82,40 @@ class ProductSla(models.Model):
vendor_duration = vendor_sla.duration * 60
self.sla_vendor_id = vendor_sla.id if vendor_sla else False
+ #SLA Logistik selalu 1 hari
+ estimation_sla = (1 * 24 * 60) + vendor_duration
+ estimation_sla_days = estimation_sla / (24 * 60)
+ self.sla = math.ceil(estimation_sla_days)
+ self.sla_logistic = int(1)
+ self.sla_logistic_unit = 'hari'
+ self.sla_logistic_duration_unit = '1 hari'
+ else:
+ self.unlink()
+ else:
+ self.unlink()
+
- 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)
+ # 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_do_ready = stock.picking_id.date_reserved
- if date_delivered and date_do_ready:
- leadtime = date_delivered - date_do_ready
- leadtime_in_days = leadtime.days
- leadtimes.append(leadtime_in_days)
+ # leadtimes=[]
+ # for stock in picking:
+ # date_delivered = stock.picking_id.driver_departure_date
+ # date_do_ready = stock.picking_id.date_reserved
+ # if date_delivered and date_do_ready:
+ # leadtime = date_delivered - date_do_ready
+ # 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)
- estimation_sla = ((rounded_leadtime * 24 * 60) + vendor_duration)/2
- estimation_sla_days = estimation_sla / (24 * 60)
- self.sla = math.ceil(estimation_sla_days)
- self.avg_leadtime = int(rounded_leadtime) \ No newline at end of file
+ # if len(leadtimes) > 0:
+ # avg_leadtime = sum(leadtimes) / len(leadtimes)
+ # rounded_leadtime = math.ceil(avg_leadtime)
+ # estimation_sla = ((rounded_leadtime * 24 * 60) + vendor_duration)/2
+ # estimation_sla_days = estimation_sla / (24 * 60)
+ # self.sla = math.ceil(estimation_sla_days)
+ # self.avg_leadtime = int(rounded_leadtime) \ No newline at end of file
diff --git a/indoteknik_custom/views/product_sla.xml b/indoteknik_custom/views/product_sla.xml
index 3722ef3d..9179730f 100644
--- a/indoteknik_custom/views/product_sla.xml
+++ b/indoteknik_custom/views/product_sla.xml
@@ -8,7 +8,7 @@
<field name="product_variant_id"/>
<field name="sla_vendor_id" string="Name Vendor"/>
<field name="sla_vendor_duration" string="SLA Vendor"/>
- <field name="avg_leadtime"/>
+ <field name="sla_logistic_duration_unit" string="SLA Logistic"/>
<field name="sla"/>
</tree>
</field>
@@ -23,7 +23,8 @@
<group>
<group>
<field name="product_variant_id"/>
- <field name="avg_leadtime"/>
+ <field name="sla_logistic"/>
+ <field name="sla_logistic_unit"/>
<field name="sla"/>
<field name="version"/>
</group>