summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/stock_picking.py
diff options
context:
space:
mode:
authortrisusilo48 <tri.susilo@altama.co.id>2025-01-14 09:31:44 +0700
committertrisusilo48 <tri.susilo@altama.co.id>2025-01-14 09:31:44 +0700
commit183cd9e68ea05e31c63000aeb992eb618edb02b5 (patch)
tree45d710db372c2a0eba437a0137cbb74d9935a7f2 /indoteknik_custom/models/stock_picking.py
parent63878bd84a6eb9094e702963d7c78fcd8dfa1808 (diff)
parent6dda865a1b3262ce78ed2db024fd03efb091d6a6 (diff)
Merge branch 'odoo-production' into feature/integrasi_biteship
# Conflicts: # indoteknik_custom/__manifest__.py # indoteknik_custom/models/__init__.py # indoteknik_custom/security/ir.model.access.csv
Diffstat (limited to 'indoteknik_custom/models/stock_picking.py')
-rw-r--r--indoteknik_custom/models/stock_picking.py218
1 files changed, 206 insertions, 12 deletions
diff --git a/indoteknik_custom/models/stock_picking.py b/indoteknik_custom/models/stock_picking.py
index e6506a0b..cd330aeb 100644
--- a/indoteknik_custom/models/stock_picking.py
+++ b/indoteknik_custom/models/stock_picking.py
@@ -6,10 +6,17 @@ from itertools import groupby
import pytz, requests, json, requests
from dateutil import parser
import datetime
-
+import hmac
+import hashlib
+import base64
+import requests
+import time
+import logging
+_logger = logging.getLogger(__name__)
class StockPicking(models.Model):
_inherit = 'stock.picking'
+ # check_product_lines = fields.One2many('check.product', 'picking_id', string='Check Product', auto_join=True)
is_internal_use = fields.Boolean('Internal Use', help='flag which is internal use or not')
account_id = fields.Many2one('account.account', string='Account')
efaktur_id = fields.Many2one('vit.efaktur', string='Faktur Pajak')
@@ -115,6 +122,19 @@ class StockPicking(models.Model):
], string='Status Reserve', readonly=True, tracking=True, help="The current state of the stock picking.")
notee = fields.Text(string="Note")
+ @api.model
+ def _compute_dokumen_tanda_terima(self):
+ for picking in self:
+ picking.dokumen_tanda_terima = picking.partner_id.dokumen_pengiriman
+
+ @api.model
+ def _compute_dokumen_pengiriman(self):
+ for picking in self:
+ picking.dokumen_pengiriman = picking.partner_id.dokumen_pengiriman_input
+
+ dokumen_tanda_terima = fields.Char(string='Dokumen Tanda Terima yang Diberikan Pada Saat Pengiriman Barang', readonly=True, compute=_compute_dokumen_tanda_terima)
+ dokumen_pengiriman = fields.Char(string='Dokumen yang Dibawa Saat Pengiriman Barang', readonly=True, compute=_compute_dokumen_pengiriman)
+
# Envio Tracking Section
envio_id = fields.Char(string="Envio ID", readonly=True)
envio_code = fields.Char(string="Envio Code", readonly=True)
@@ -134,6 +154,86 @@ class StockPicking(models.Model):
envio_latest_longitude = fields.Float(string="Log Longitude", readonly=True)
tracking_by = fields.Many2one('res.users', string='Tracking By', readonly=True, tracking=True)
+ # Lalamove Section
+ lalamove_order_id = fields.Char(string="Lalamove Order ID", copy=False)
+ lalamove_address = fields.Char(string="Lalamove Address")
+ lalamove_name = fields.Char(string="Lalamove Name")
+ lalamove_phone = fields.Char(string="Lalamove Phone")
+ lalamove_status = fields.Char(string="Lalamove Status")
+ lalamove_delivered_at = fields.Datetime(string="Lalamove Delivered At")
+ lalamove_data = fields.Text(string="Lalamove Data", readonly=True)
+ lalamove_image_url = fields.Char(string="Lalamove Image URL")
+ lalamove_image_html = fields.Html(string="Lalamove Image", compute="_compute_lalamove_image_html")
+
+ def _compute_lalamove_image_html(self):
+ for record in self:
+ if record.lalamove_image_url:
+ record.lalamove_image_html = f'<img src="{record.lalamove_image_url}" width="300" height="300"/>'
+ else:
+ record.lalamove_image_html = "No image available."
+
+ def action_fetch_lalamove_order(self):
+ pickings = self.env['stock.picking'].search([
+ ('picking_type_code', '=', 'outgoing'),
+ ('state', '=', 'done'),
+ ('carrier_id', '=', 9)
+ ])
+ for picking in pickings:
+ try:
+ order_id = picking.lalamove_order_id
+ apikey = self.env['ir.config_parameter'].sudo().get_param('lalamove.apikey')
+ secret = self.env['ir.config_parameter'].sudo().get_param('lalamove.secret')
+ market = self.env['ir.config_parameter'].sudo().get_param('lalamove.market', default='ID')
+
+ order_data = picking.get_lalamove_order(order_id, apikey, secret, market)
+ picking.lalamove_data = order_data
+ except Exception as e:
+ _logger.error(f"Error fetching Lalamove order for picking {picking.id}: {str(e)}")
+ continue
+
+ def get_lalamove_order(self, order_id, apikey, secret, market):
+ timestamp = str(int(time.time() * 1000))
+ message = f"{timestamp}\r\nGET\r\n/v3/orders/{order_id}\r\n\r\n"
+ signature = hmac.new(secret.encode('utf-8'), message.encode('utf-8'), hashlib.sha256).hexdigest()
+
+ headers = {
+ "Content-Type": "application/json",
+ "Authorization": f"hmac {apikey}:{timestamp}:{signature}",
+ "Market": market
+ }
+
+ url = f"https://rest.lalamove.com/v3/orders/{order_id}"
+ response = requests.get(url, headers=headers)
+
+ if response.status_code == 200:
+ data = response.json()
+ stops = data.get("data", {}).get("stops", [])
+
+ for stop in stops:
+ pod = stop.get("POD", {})
+ if pod.get("status") == "DELIVERED":
+ image_url = pod.get("image") # Sesuaikan jika key berbeda
+ self.lalamove_image_url = image_url
+
+ address = stop.get("address")
+ name = stop.get("name")
+ phone = stop.get("phone")
+ delivered_at = pod.get("deliveredAt")
+
+ delivered_at_dt = self._convert_to_datetime(delivered_at)
+
+ self.lalamove_address = address
+ self.lalamove_name = name
+ self.lalamove_phone = phone
+ self.lalamove_status = pod.get("status")
+ self.lalamove_delivered_at = delivered_at_dt
+ return data
+
+ raise UserError("No delivered data found in Lalamove response.")
+ else:
+ raise UserError(f"Error {response.status_code}: {response.text}")
+
+
def _convert_to_wib(self, date_str):
"""
Mengonversi string waktu ISO 8601 ke format waktu Indonesia (WIB)
@@ -661,13 +761,13 @@ class StockPicking(models.Model):
if not self.env.user.is_logistic_approver and self.env.context.get('active_model') == 'stock.picking':
if self.origin and 'Return of' in self.origin:
raise UserError("Button ini hanya untuk Logistik")
-
+
if self.picking_type_code == 'internal':
self.check_qty_done_stock()
if self._name != 'stock.picking':
return super(StockPicking, self).button_validate()
-
+
if not self.picking_code:
self.picking_code = self.env['ir.sequence'].next_by_code('stock.picking.code') or '0'
@@ -681,15 +781,10 @@ class StockPicking(models.Model):
raise UserError("Harus di Approve oleh Accounting")
if self.picking_type_id.id == 28 and not self.env.user.is_logistic_approver:
- raise UserError("Harus di Approve oleh Logistik")
+ raise UserError("Harus di Approve oleh Logistik")
if self.location_dest_id.id == 47 and not self.env.user.is_purchasing_manager:
- raise UserError("Transfer ke gudang selisih harus di approve Rafly Hanggara")
-
- # if self.group_id.sale_id:
- # if self.group_id.sale_id.payment_link_midtrans:
- # if self.group_id.sale_id.payment_status != 'settlement' and self.group_id.sale_id.state == 'draft':
- # raise UserError('Uang belum masuk (settlement), mohon konfirmasi ke sales atau finance')
+ raise UserError("Transfer ke gudang selisih harus di approve Rafly Hanggara")
if self.is_internal_use:
self.approval_status = 'approved'
@@ -707,7 +802,7 @@ class StockPicking(models.Model):
if not self.date_reserved:
current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
self.date_reserved = current_time
-
+
self.validation_minus_onhand_quantity()
self.responsible = self.env.user.id
res = super(StockPicking, self).button_validate()
@@ -715,6 +810,59 @@ class StockPicking(models.Model):
self.date_done = datetime.datetime.utcnow()
self.state_reserve = 'done'
return res
+
+
+ def send_mail_bills(self):
+ if self.picking_type_code == 'incoming' and self.purchase_id:
+ template = self.env.ref('indoteknik_custom.mail_template_invoice_po_document')
+ if template and self.purchase_id:
+ # Render email body
+ email_values = template.sudo().generate_email(
+ res_ids=[self.purchase_id.id],
+ fields=['body_html']
+ )
+ rendered_body = email_values.get(self.purchase_id.id, {}).get('body_html', '')
+
+ # Render report dengan XML ID
+ report = self.env.ref('purchase.action_report_purchase_order') # Gunakan XML ID laporan
+ if not report:
+ raise UserError("Laporan dengan XML ID 'purchase.action_report_purchase_order' tidak ditemukan.")
+
+ # Render laporan ke PDF
+ pdf_content, _ = report._render_qweb_pdf([self.purchase_id.id])
+ report_content = base64.b64encode(pdf_content).decode('utf-8')
+
+ # Kirim email menggunakan template
+ email_sent = template.sudo().send_mail(self.purchase_id.id, force_send=True)
+
+ if email_sent:
+ # Buat attachment untuk laporan
+ attachment = self.env['ir.attachment'].create({
+ 'name': self.purchase_id.name or "Laporan Invoice.pdf",
+ 'type': 'binary',
+ 'datas': report_content,
+ 'res_model': 'purchase.order',
+ 'res_id': self.purchase_id.id,
+ 'mimetype': 'application/pdf',
+ })
+
+ # Siapkan data untuk mail.compose.message
+ compose_values = {
+ 'subject': "Pengiriman Email Invoice",
+ 'body': rendered_body,
+ 'attachment_ids': [(4, attachment.id)],
+ 'res_id': self.purchase_id.id,
+ 'model': 'purchase.order',
+ }
+
+ # Buat mail.compose.message
+ compose_message = self.env['mail.compose.message'].create(compose_values)
+
+ # Kirim pesan melalui wizard
+ compose_message.action_send_mail()
+
+ return True
+
def action_cancel(self):
if not self.env.user.is_logistic_approver and self.env.context.get('active_model') == 'stock.picking':
if self.origin and 'Return of' in self.origin:
@@ -858,4 +1006,50 @@ class StockPicking(models.Model):
formatted_fastest_eta = fastest_eta.strftime(format_time_fastest)
formatted_longest_eta = longest_eta.strftime(format_time)
- return f'{formatted_fastest_eta} - {formatted_longest_eta}' \ No newline at end of file
+ return f'{formatted_fastest_eta} - {formatted_longest_eta}'
+
+# class CheckProduct(models.Model):
+# _name = 'check.product'
+# _description = 'Check Product'
+# _order = 'picking_id, id'
+
+# picking_id = fields.Many2one('stock.picking', string='Picking Reference', required=True, ondelete='cascade', index=True, copy=False)
+# product_id = fields.Many2one('product.product', string='Product')
+
+
+# @api.constrains('product_id')
+# def check_product_validity(self):
+# """
+# Validate if the product exists in the related stock.picking's move_ids_without_package
+# and ensure that the product's quantity does not exceed the available product_uom_qty.
+# """
+# for record in self:
+# if not record.picking_id or not record.product_id:
+# continue
+
+# # Filter move lines in the related picking for the selected product
+# moves = record.picking_id.move_ids_without_package.filtered(
+# lambda move: move.product_id.id == record.product_id.id
+# )
+
+# if not moves:
+# raise UserError((
+# "The product '%s' is not available in the related stock picking's moves. "
+# "Please check and try again."
+# ) % record.product_id.display_name)
+
+# # Calculate the total entries for the product in check.product for the same picking
+# product_entries_count = self.search_count([
+# ('picking_id', '=', record.picking_id.id),
+# ('product_id', '=', record.product_id.id)
+# ])
+
+# # Sum the product_uom_qty for all relevant moves
+# total_qty_in_moves = sum(moves.mapped('product_uom_qty'))
+
+# # Compare the count of entries against the available quantity
+# if product_entries_count > total_qty_in_moves:
+# raise UserError((
+# "The product '%s' exceeds the allowable quantity (%s) in the related stock picking's moves. "
+# "You can only add it %s times."
+# ) % (record.product_id.display_name, total_qty_in_moves, total_qty_in_moves))