summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2025-02-26 13:46:53 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2025-02-26 13:46:53 +0700
commitc459ff9c8b326929e748bb35b3dffe1cc9248e8a (patch)
treeb5b5a943e5a9a567cae4ad7c121dbf5f84fd0455
parent6a5a2a2666c33ffafd610df882491d86918468bb (diff)
margin per line on po
-rwxr-xr-xindoteknik_custom/models/purchase_order_line.py60
-rwxr-xr-xindoteknik_custom/views/purchase_order.xml4
2 files changed, 48 insertions, 16 deletions
diff --git a/indoteknik_custom/models/purchase_order_line.py b/indoteknik_custom/models/purchase_order_line.py
index 587a09a1..033469b8 100755
--- a/indoteknik_custom/models/purchase_order_line.py
+++ b/indoteknik_custom/models/purchase_order_line.py
@@ -321,32 +321,60 @@ class PurchaseOrderLine(models.Model):
def compute_item_margin(self):
sum_so_margin = sum_sales_price = sum_margin = 0
+
for line in self:
- if not line.product_id or line.product_id.type == 'service' or not self.order_id.sale_order_id:
+ product = line.product_id
+ order = line.order_id
+
+ # Skip jika tidak ada product_id, produk adalah service, atau tidak ada purchase order terkait
+ if not product or product.type == 'service' or not order:
line.so_item_margin = 0
line.so_item_percent_margin = 0
line.item_margin = 0
line.item_percent_margin = 0
continue
- sale_order_line = self.env['sale.order.line'].search(
- [('product_id', '=', line.product_id.id),
- ('order_id', '=', line.order_id.sale_order_id.id)], limit=1, order='price_reduce_taxexcl')
- line.so_item_margin = sale_order_line.item_margin
- line.so_item_percent_margin = sale_order_line.item_percent_margin
- sum_so_margin += sale_order_line.item_margin
- sales_price = sale_order_line.price_reduce_taxexcl * sale_order_line.product_uom_qty
- if sale_order_line.order_id.shipping_cost_covered == 'indoteknik':
- sales_price -= sale_order_line.delivery_amt_line
- if sale_order_line.order_id.fee_third_party > 0:
- sales_price -= sale_order_line.fee_third_party_line
- sum_sales_price += sales_price
+
+ # Cari semua sale.order.line terkait dengan purchase.order melalui tabel purchase.order.sales.match
+ sales_matches = self.env['purchase.order.sales.match'].search([
+ ('purchase_order_id', '=', order.id),
+ ('product_id', '=', product.id)
+ ])
+
+ total_sales_price = total_margin = total_qty_so = 0
+ for match in sales_matches:
+ sale_order_line = match.sale_line_id
+
+ # Hitung harga jual setelah mempertimbangkan biaya tambahan
+ sales_price = sale_order_line.price_reduce_taxexcl * match.qty_so
+ if sale_order_line.order_id.shipping_cost_covered == 'indoteknik':
+ sales_price -= sale_order_line.delivery_amt_line
+ if sale_order_line.order_id.fee_third_party > 0:
+ sales_price -= sale_order_line.fee_third_party_line
+
+ total_sales_price += sales_price
+ total_margin += sale_order_line.item_margin
+ total_qty_so += match.qty_so
+
+ # Set margin berdasarkan total dari semua sales order yang terkait
+ line.so_item_margin = total_margin
+ line.so_item_percent_margin = (total_margin / total_sales_price) * 100 if total_sales_price else 0
+
+ sum_so_margin += total_margin
+ sum_sales_price += total_sales_price
+
+ # Hitung harga pembelian dengan mempertimbangkan biaya pengiriman
purchase_price = line.price_subtotal
- if line.order_id.delivery_amount > 0:
+ if order.delivery_amount > 0:
purchase_price += line.delivery_amt_line
- real_item_margin = sales_price - purchase_price
- real_item_percent_margin = round((real_item_margin/sales_price), 2) * 100
+
+ # Hitung margin dan persentase margin
+ real_item_margin = total_sales_price - purchase_price
+ real_item_percent_margin = (real_item_margin / total_sales_price) * 100 if total_sales_price else 0
+
+ # Set nilai margin ke dalam line
line.item_margin = real_item_margin
line.item_percent_margin = real_item_percent_margin
+
sum_margin += real_item_margin
def compute_delivery_amt_line(self):
diff --git a/indoteknik_custom/views/purchase_order.xml b/indoteknik_custom/views/purchase_order.xml
index 0d3d5cc2..a57bd467 100755
--- a/indoteknik_custom/views/purchase_order.xml
+++ b/indoteknik_custom/views/purchase_order.xml
@@ -109,6 +109,10 @@
<field name="price_vendor" attrs="{'readonly': 1}" optional="hide"/>
</field>
<field name="price_subtotal" position="after">
+ <field name="so_item_margin" attrs="{'readonly': 1}" optional="hide"/>
+ <field name="so_item_percent_margin" attrs="{'readonly': 1}" optional="hide"/>
+ <field name="item_margin" attrs="{'readonly': 1}" optional="hide"/>
+ <field name="item_percent_margin" attrs="{'readonly': 1}" optional="hide"/>
<field name="so_line_id" attrs="{'readonly': 1}" optional="hide"/>
<field name="so_id" attrs="{'readonly': 1}" optional="hide"/>
<field name="indent" optional="hide"/>