summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/purchase_order.py
diff options
context:
space:
mode:
authorFIN-IT_AndriFP <it@fixcomart.co.id>2025-10-23 09:50:40 +0700
committerFIN-IT_AndriFP <it@fixcomart.co.id>2025-10-23 09:50:40 +0700
commit310f92c9b825a458618daa3b00581b790f8e009e (patch)
tree2b8957db596c86525c009ef5e62e23ccd6397f8f /indoteknik_custom/models/purchase_order.py
parent58a6ca75b9b1bb07ea958ab4bfb553140daeb8f8 (diff)
parent8d649f97dade329859b5770d1f3972cdd7233f97 (diff)
Merge branch 'odoo-backup' of https://bitbucket.org/altafixco/indoteknik-addons into pum-v3
Diffstat (limited to 'indoteknik_custom/models/purchase_order.py')
-rwxr-xr-xindoteknik_custom/models/purchase_order.py68
1 files changed, 35 insertions, 33 deletions
diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py
index b34ec926..e79417aa 100755
--- a/indoteknik_custom/models/purchase_order.py
+++ b/indoteknik_custom/models/purchase_order.py
@@ -1069,6 +1069,21 @@ class PurchaseOrder(models.Model):
) % order.name)
def button_confirm(self):
+ if self.env.user.id != 7 and not self.env.user.is_leader: # Pimpinan
+ if '/PJ/' in self.name:
+ low_margin_lines = self.order_sales_match_line.filtered(
+ lambda match: match.so_header_margin <= 15.0
+ )
+ price_change_detected = any(line.price_unit_before for line in self.order_line)
+ if low_margin_lines and price_change_detected:
+ # raise UserError("Matches SO terdapat item dengan header margin SO <= 15%. Approval Pimpinan diperlukan.")
+ raise UserError("Approval Pimpinan diperlukan jika terdapat perubahan Unit Price 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
res = super(PurchaseOrder, self).button_confirm()
@@ -1077,7 +1092,7 @@ class PurchaseOrder(models.Model):
self.check_different_vendor_so_po()
# self.check_data_vendor()
- if self.amount_untaxed >= 50000000 and not self.env.user.id == 21:
+ if self.amount_untaxed >= 50000000 and not self.env.user.id in (21, 7):
raise UserError("Hanya Rafly Hanggara yang bisa approve")
if not self.date_planned:
@@ -1405,63 +1420,50 @@ class PurchaseOrder(models.Model):
('product_id', '=', line.product_id.id),
('order_id', '=', line.purchase_order_id.id)
], limit=1)
- sale_order_line = line.sale_line_id
- if not sale_order_line:
- sale_order_line = self.env['sale.order.line'].search([
- ('product_id', '=', line.product_id.id),
- ('order_id', '=', line.sale_id.id)
- ], limit=1, order='price_reduce_taxexcl')
+ sale_order_line = line.sale_line_id or self.env['sale.order.line'].search([
+ ('product_id', '=', line.product_id.id),
+ ('order_id', '=', line.sale_id.id)
+ ], limit=1, order='price_reduce_taxexcl')
if sale_order_line and po_line:
- so_margin = (line.qty_po / line.qty_so) * sale_order_line.item_margin
+ qty_so = line.qty_so or 0
+ qty_po = line.qty_po or 0
+
+ # Hindari division by zero
+ so_margin = (qty_po / qty_so) * sale_order_line.item_margin if qty_so > 0 else 0
sum_so_margin += so_margin
- sales_price = sale_order_line.price_reduce_taxexcl * line.qty_po
+ sales_price = sale_order_line.price_reduce_taxexcl * qty_po
if sale_order_line.order_id.shipping_cost_covered == 'indoteknik':
- sales_price -= (sale_order_line.delivery_amt_line / sale_order_line.product_uom_qty) * line.qty_po
+ sales_price -= (sale_order_line.delivery_amt_line / sale_order_line.product_uom_qty) * qty_po
if sale_order_line.order_id.fee_third_party > 0:
- sales_price -= (sale_order_line.fee_third_party_line / sale_order_line.product_uom_qty) * line.qty_po
+ sales_price -= (sale_order_line.fee_third_party_line / sale_order_line.product_uom_qty) * qty_po
sum_sales_price += sales_price
-
purchase_price = po_line.price_subtotal
if po_line.ending_price > 0:
if po_line.taxes_id.id == 22:
- ending_price = po_line.ending_price / 1.11
- purchase_price = ending_price
+ purchase_price = po_line.ending_price / 1.11
else:
purchase_price = po_line.ending_price
if line.purchase_order_id.delivery_amount > 0:
- purchase_price += (po_line.delivery_amt_line / po_line.product_qty) * line.qty_po
+ purchase_price += (po_line.delivery_amt_line / po_line.product_qty) * qty_po
if line.purchase_order_id.delivery_amt > 0:
purchase_price += line.purchase_order_id.delivery_amt
+
real_item_margin = sales_price - purchase_price
sum_margin += real_item_margin
- if sum_so_margin != 0 and sum_sales_price != 0 and sum_margin != 0:
+ # 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
-
else:
- self.total_margin = 0
- self.total_percent_margin = 0
- self.total_so_margin = 0
- self.total_so_percent_margin = 0
-
-
- 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.total_margin = self.total_percent_margin = 0
+ self.total_so_margin = self.total_so_percent_margin = 0
- else:
- self.total_margin = 0
- self.total_percent_margin = 0
- self.total_so_margin = 0
- self.total_so_percent_margin = 0
def compute_amt_total_without_service(self):
for order in self: