diff options
| author | HafidBuroiroh <hafidburoiroh09@gmail.com> | 2026-03-06 14:50:49 +0700 |
|---|---|---|
| committer | HafidBuroiroh <hafidburoiroh09@gmail.com> | 2026-03-06 14:50:49 +0700 |
| commit | 9bcbad313a3101a10658e51f2475f8dfd6414c55 (patch) | |
| tree | bb8d5fa714960e1dbf1ec5b4869c21abf942f818 /indoteknik_custom/models/purchase_order.py | |
| parent | 5c20bda1e65d72088af744481abf0784d30b710e (diff) | |
| parent | 1d61c5c2f29270d2d1e9c84e887e9c94416d9027 (diff) | |
Merge branch 'odoo-backup' of https://bitbucket.org/altafixco/indoteknik-addons into refund_system
Diffstat (limited to 'indoteknik_custom/models/purchase_order.py')
| -rwxr-xr-x | indoteknik_custom/models/purchase_order.py | 352 |
1 files changed, 339 insertions, 13 deletions
diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py index 35fa79a8..244575ae 100755 --- a/indoteknik_custom/models/purchase_order.py +++ b/indoteknik_custom/models/purchase_order.py @@ -8,6 +8,8 @@ import io import base64 from odoo.tools import lazy_property import socket +import requests +import json try: from odoo.tools.misc import xlsxwriter @@ -53,6 +55,7 @@ class PurchaseOrder(models.Model): total_so_percent_margin = fields.Float( 'SO Margin%', compute='compute_total_margin', help="Total % Margin in Sales Order Header") + amount_cashback = fields.Float('Cashback', compute = 'compute_total_margin', help = 'Total Cashback brand Altama') amount_total_without_service = fields.Float('AmtTotalWithoutService', compute='compute_amt_total_without_service') summary_qty_po = fields.Float('Total Qty', compute='_compute_summary_qty') summary_qty_receipt = fields.Float('Summary Qty Receipt', compute='_compute_summary_qty') @@ -124,7 +127,308 @@ class PurchaseOrder(models.Model): ) overseas_po = fields.Boolean(string='PO Luar Negeri?', tracking=3, help='Centang jika PO untuk pembelian luar negeri') + order_altama_id = fields.Integer('Req Order Altama', copy=False) + soo_number = fields.Char('SOO Number', copy=False) + soo_price = fields.Float('SOO Price', copy=False) + soo_discount = fields.Float('SOO Discount', copy=False) + soo_tax = fields.Float('SOO Tax', copy=False) + + def _get_altama_token(self, source='auto'): + ICP = self.env['ir.config_parameter'].sudo() + TokenLog = self.env['token.log'].sudo() + + token_url = ICP.get_param('token.adempiere.altama') + client_id = ICP.get_param('client.adempiere.altama') + client_secret = ICP.get_param('secret_key.adempiere.altama') + + active_token = TokenLog.search([ + ('is_active', '=', True), + ('token_from', '=', 'Adempiere Altama'), + ('expires_at', '>', datetime.utcnow()), + ], limit=1, order='id desc') + + if active_token: + return active_token.token + + headers = { + "Authorization": "Basic " + base64.b64encode(f"{client_id}:{client_secret}".encode()).decode(), + "Content-Type": "application/x-www-form-urlencoded", + } + data = {"grant_type": "client_credentials"} + + response = requests.post(token_url, data=data, headers=headers, timeout=15) + if response.status_code == 200: + result = response.json() + token = result.get("access_token") + expires_in = result.get("expires_in", 3600) + expiry_time = datetime.utcnow() + timedelta(seconds=expires_in - 60) + + TokenLog.search([ + ('token_from', '=', 'Adempiere Altama'), + ('is_active', '=', True), + ]).write({'is_active': False}) + + TokenLog.create({ + 'token': token, + 'expires_at': expiry_time, + 'is_active': True, + 'created_by': self.env.user.id if self.env.user else None, + 'source': source, + 'token_from': 'Adempiere Altama', + }) + + return token + + else: + raise Exception(f"Gagal ambil token: {response.status_code} - {response.text}") + + def action_create_order_altama(self): + ICP = self.env['ir.config_parameter'].sudo() + for order in self: + try: + token = self._get_altama_token(source='manual') + url = ICP.get_param('endpoint.create.order.adempiere.altama') + headers = { + "Authorization": f"Bearer {token}", + "Content-Type": "application/json", + } + + payload = { + "date_po": order.date_approve.strftime("%Y%m%d%H%M%S"), + "no_po": order.name, + "details": [ + { + "item_code": line.product_id.default_code or "", + "price": line.price_unit, + "qty": line.product_qty, + } + for line in order.order_line + ], + } + + response = requests.post(url, json=payload, headers=headers, timeout=20) + + try: + result = response.json() + except json.JSONDecodeError: + raise Exception(f"Response bukan JSON valid: {response.text}") + + if response.status_code == 200 and result.get("code") == "00": + contents = result.get("contents", {}) + if isinstance(contents, dict): + order.order_altama_id = contents.get("req_id") + else: + order.order_altama_id = contents.get("req_id") + + elif response.status_code == 404: + raise Exception("URL endpoint gak ditemukan (404). Pastikan path-nya benar di Altama API.") + elif response.status_code == 401: + token = self._get_altama_token(source='auto') + headers["Authorization"] = f"Bearer {token}" + response = requests.post(url, json=payload, headers=headers, timeout=20) + elif response.status_code not in (200, 201): + raise Exception(f"Gagal kirim ke Altama: {response.status_code} - {response.text}") + + self.message_post(body=f"✅ PO berhasil dikirim ke Altama!\nResponse: {json.dumps(result, indent=2)}") + + except Exception as e: + self.message_post(body=f"❌ Gagal kirim ke Altama:<br/><pre>{str(e)}</pre>") + + + def action_get_order_altama(self): + ICP = self.env['ir.config_parameter'].sudo() + + for order in self: + try: + # ============================ + # Get Token + # ============================ + token = self._get_altama_token(source='manual') + + url = ICP.get_param('endpoint.get.order.adempiere.altama') + if not url: + raise Exception("Parameter 'endpoint.adempiere.altama' belum diset di System Parameters.") + + headers = { + "Authorization": f"Bearer {token}", + "Content-Type": "application/json", + } + + params = { + "orderreq_id": order.order_altama_id or 0, + } + + # ============================ + # Request ke API + # ============================ + response = requests.get(url, headers=headers, params=params, timeout=20) + + if response.status_code == 401: + token = self._get_altama_token(source='auto') + headers["Authorization"] = f"Bearer {token}" + response = requests.get(url, headers=headers, params=params, timeout=20) + if response.status_code not in (200, 201): + raise Exception(f"Gagal ambil data dari Altama: {response.status_code} - {response.text}") + + data = response.json() + + # ============================ + # Extract Data + # ============================ + contents_root = data.get("contents", {}) + contents_list = contents_root.get("contents", []) + + if not isinstance(contents_list, list): + raise Exception("Format data contents dari Altama tidak sesuai (expected list).") + + order.message_post( + body=f"✅ Berhasil ambil data dari Altama. Ditemukan {len(contents_list)} record." + ) + + # ===================================================== + # LOOP MAIN DATA + # ===================================================== + for item in contents_list: + + req_id = item.get("req_id") + no_po = item.get("no_po") + list_item_po = item.get("list_Item_po", []) + list_soo = item.get("list_soo", []) + + # ============================ + # Isi Data SOO Ke Order + # ============================ + soo_numbers = [s.get("no_soo") for s in list_soo if s.get("no_soo")] + unique_soo = list(set(soo_numbers)) + if len(unique_soo) == 1: + order.soo_number = unique_soo[0] + if not order.picking_ids.number_soo: + # order.picking_ids[0].number_soo = unique_soo[0] + for picking in order.picking_ids: + picking.number_soo = unique_soo[0] + elif len(unique_soo) > 1: + order.soo_number = ", ".join(unique_soo) + if not order.picking_ids.number_soo: + # order.picking_ids[0].number_soo = ", ".join(unique_soo) + for picking in order.picking_ids: + picking.number_soo = ", ".join(unique_soo) + else: + order.soo_number = False + + if list_soo: + first_soo = list_soo[0] + order.soo_price = first_soo.get("totalprice") + order.soo_discount = first_soo.get("diskon") + order.soo_tax = first_soo.get("ppn") + + order.order_altama_id = req_id + + # ============================ + # Update Order Lines + # ============================ + for item_line in list_item_po: + + line = order.order_line.filtered( + lambda l: l.product_id.default_code == item_line.get("item_code") + ) + + if line: + line.write({ + "description": item_line.get("description", ""), + "altama_ordered": item_line.get("qtyordered", 0), + "altama_delivered": item_line.get("qtydelivered", 0), + "altama_invoiced": item_line.get("qtyinvoiced", 0), + "docstatus_altama": item_line.get("docstatus", ""), + }) + + # ===================================================== + # BUILD HTML TABLES FOR CHATTER + # ===================================================== + + # ---- SOO TABLE ---- + soo_rows = "" + for s in list_soo: + soo_rows += f""" + <tr> + <td>{s.get('no_soo')}</td> + <td>{s.get('totalprice')}</td> + <td>{s.get('diskon')}</td> + <td>{s.get('ppn')}</td> + </tr> + """ + + soo_table = f""" + <table style="width:100%; border-collapse: collapse; margin-top: 10px;"> + <thead> + <tr style="background:#f1f1f1;"> + <th style="border:1px solid #ccc; padding:6px;">SOO Number</th> + <th style="border:1px solid #ccc; padding:6px;">Total Price</th> + <th style="border:1px solid #ccc; padding:6px;">Diskon</th> + <th style="border:1px solid #ccc; padding:6px;">PPN</th> + </tr> + </thead> + <tbody> + {soo_rows or '<tr><td colspan="4">Tidak ada data SOO</td></tr>'} + </tbody> + </table> + """ + + # ---- ITEM PO TABLE ---- + po_rows = "" + for l in list_item_po: + + desc = l.get("description") or "" + + # Flag: row error kalau description tidak mulai dengan SOO/ + is_error = desc and not desc.startswith("SOO/") + + # Style row merah + row_style = "color:red; font-weight:bold;" if is_error else "" + + po_rows += f""" + <tr style="{row_style}"> + <td>{l.get('item_code')}</td> + <td>{desc}</td> + <td>{l.get('qtyordered')}</td> + <td>{l.get('qtydelivered')}</td> + <td>{l.get('qtyinvoiced')}</td> + <td>{l.get('docstatus')}</td> + </tr> + """ + + + po_table = f""" + <table style="width:100%; border-collapse: collapse; margin-top: 10px;"> + <thead> + <tr style="background:#f1f1f1;"> + <th style="border:1px solid #ccc; padding:6px;">Item Code</th> + <th style="border:1px solid #ccc; padding:6px;">Description</th> + <th style="border:1px solid #ccc; padding:6px;">Ordered</th> + <th style="border:1px solid #ccc; padding:6px;">Delivered</th> + <th style="border:1px solid #ccc; padding:6px;">Invoiced</th> + <th style="border:1px solid #ccc; padding:6px;">Status</th> + </tr> + </thead> + <tbody> + {po_rows or '<tr><td colspan="6">Tidak ada item PO</td></tr>'} + </tbody> + </table> + """ + + # ---- POST TO CHATTER ---- + order.message_post( + body=f""" + <b>📦 Data SOO</b><br/>{soo_table} + <br/><br/> + <b>📦 Data Item PO</b><br/>{po_table} + """ + ) + + except Exception as e: + order.message_post( + body=f"❌ Gagal ambil data dari Altama:<br/><pre>{str(e)}</pre>" + ) @staticmethod def is_local_env(): hostname = socket.gethostname().lower() @@ -631,7 +935,8 @@ class PurchaseOrder(models.Model): date_done = self.date_approve - day_extension = int(self.payment_term_id.line_ids.days) + # day_extension = int(self.payment_term_id.line_ids.days) + day_extension = int(max(self.payment_term_id.line_ids.mapped('days'), default=0)) payment_schedule = date_done + timedelta(days=day_extension) if payment_schedule.weekday() == 0: @@ -1086,17 +1391,6 @@ class PurchaseOrder(models.Model): if self.order_sales_match_line: if self.total_percent_margin <= 15.0: raise UserError("Approval Pimpinan diperlukan jika terdapat perubahan Unit Price pada PO Line dan Memiliki Margin <= 15%") - else: - low_margin_match_so = self.order_sales_match_line.filtered( - lambda match: match.so_header_margin <= 15.0 - ) - if low_margin_match_so: - raise UserError("Approval Pimpinan diperlukan jika pada PO Line yang Matches SO item memiliki header margin SO <= 15%") - # else: - # is_po_manual = '/A/' not in self.name and '/MO/' not in self.name - # if is_po_manual: - # if not self.order_sales_match_line: - # raise UserError("Tidak ada matches SO, Approval Pimpinan diperlukan.") self._check_assets_note() # self._check_payment_term() # check payment term @@ -1152,6 +1446,9 @@ class PurchaseOrder(models.Model): send_email = True break + # if self.partner_id.id == 5571 and not self.revisi_po: + # self.action_create_order_altama() + if send_email: if self.is_local_env(): _logger.warning("📪 Local environment detected — skip sending email reminders.") @@ -1169,6 +1466,7 @@ class PurchaseOrder(models.Model): self.calculate_line_no() self.approve_by = self.env.user.id + # override date planned added with two days # leadtime = self.partner_id.leadtime # delta_time = current_time + timedelta(days=leadtime) @@ -1186,6 +1484,8 @@ class PurchaseOrder(models.Model): # if len(self) == 1: # _logger.info("Redirecting ke BU") # return self.action_view_related_bu() + if self.partner_id.id == 5571 and not self.revisi_po: + self.action_create_order_altama() return res @@ -1421,19 +1721,36 @@ class PurchaseOrder(models.Model): real_item_margin = sales_price - purchase_price sum_margin += real_item_margin + cashback_amount = 0 + if self.partner_id.id == 5571: + cashback_percent = line.product_id.x_manufacture.cashback_percent or 0.0 + if cashback_percent > 0: + cashback_amount = purchase_price * cashback_percent + purchase_price -= cashback_amount + + # line.amount_cashback = cashback_amount + if sum_so_margin != 0 and sum_sales_price != 0 and sum_margin != 0: self.total_so_margin = sum_so_margin self.total_so_percent_margin = round((sum_so_margin / sum_sales_price), 2) * 100 self.total_margin = sum_margin self.total_percent_margin = round((sum_margin / sum_sales_price), 2) * 100 + self.amount_cashback = 0 + elif self.partner_id.id == 5571 and sum_so_margin != 0 and sum_sales_price != 0 and sum_margin != 0 and cashback_amount != 0: + self.total_so_margin = sum_so_margin + self.total_so_percent_margin = round((sum_so_margin / sum_sales_price), 2) * 100 + self.total_margin = sum_margin + self.total_percent_margin = round((sum_margin / sum_sales_price), 2) * 100 + self.amount_cashback = cashback_amount else: self.total_margin = 0 self.total_percent_margin = 0 self.total_so_margin = 0 self.total_so_percent_margin = 0 + self.amount_cashback = 0 def compute_total_margin_from_apo(self): - sum_so_margin = sum_sales_price = sum_margin = 0 + sum_so_margin = sum_sales_price = sum_margin = cashback_amount = 0 for line in self.order_sales_match_line: po_line = self.env['purchase.order.line'].search([ ('product_id', '=', line.product_id.id), @@ -1470,18 +1787,27 @@ class PurchaseOrder(models.Model): if line.purchase_order_id.delivery_amt > 0: purchase_price += line.purchase_order_id.delivery_amt + if self.partner_id.id == 5571: + cashback_percent = line.product_id.x_manufacture.cashback_percent or 0.0 + if cashback_percent > 0: + cashback_amount = purchase_price * cashback_percent + purchase_price -= cashback_amount + real_item_margin = sales_price - purchase_price sum_margin += real_item_margin + self.amount_cashback = cashback_amount # Akumulasi hasil akhir if sum_sales_price != 0: self.total_so_margin = sum_so_margin self.total_so_percent_margin = round((sum_so_margin / sum_sales_price), 2) * 100 self.total_margin = sum_margin self.total_percent_margin = round((sum_margin / sum_sales_price), 2) * 100 + self.amount_cashback = cashback_amount else: self.total_margin = self.total_percent_margin = 0 self.total_so_margin = self.total_so_percent_margin = 0 + self.amount_cashback = 0 def compute_amt_total_without_service(self): |
