From add53c91a49d43b20438b14cf48aaececbabf2cd Mon Sep 17 00:00:00 2001 From: Miqdad Date: Thu, 5 Jun 2025 11:43:02 +0700 Subject: add before margin in order line --- indoteknik_custom/models/sale_order_line.py | 25 +++++++++++++++++++++++++ indoteknik_custom/views/sale_order.xml | 6 ++++++ 2 files changed, 31 insertions(+) diff --git a/indoteknik_custom/models/sale_order_line.py b/indoteknik_custom/models/sale_order_line.py index c8066961..da66465e 100644 --- a/indoteknik_custom/models/sale_order_line.py +++ b/indoteknik_custom/models/sale_order_line.py @@ -10,6 +10,8 @@ class SaleOrderLine(models.Model): help="Total Margin in Sales Order Header") item_percent_margin = fields.Float('%Margin', compute='compute_item_margin', help="Total % Margin in Sales Order Header") + item_percent_margin_before = fields.Float('%Margin Before', compute='_compute_item_percent_margin_before', + help="Total % Margin excluding third party in Sales Order Header") initial_discount = fields.Float('Initial Discount') vendor_id = fields.Many2one( 'res.partner', string='Vendor', readonly=True, @@ -134,6 +136,29 @@ class SaleOrderLine(models.Model): else: line.item_percent_margin_without_deduction = 0 + def _compute_item_percent_margin_before(self): + for line in self: + if not line.product_id or line.product_id.type == 'service' \ + or line.price_unit <= 0 or line.product_uom_qty <= 0 \ + or not line.vendor_id: + line.item_percent_margin_before = 0 + continue + + sales_price = line.price_reduce_taxexcl * line.product_uom_qty + + purchase_price = line.purchase_price + if line.purchase_tax_id and line.purchase_tax_id.price_include: + purchase_price = line.purchase_price / 1.11 + + purchase_price = purchase_price * line.product_uom_qty + + margin_before = sales_price - purchase_price + + if sales_price > 0: + line.item_percent_margin_before = round((margin_before / sales_price), 4) * 100 + else: + line.item_percent_margin_before = 0 + def compute_item_margin(self): for line in self: if not line.product_id or line.product_id.type == 'service' \ diff --git a/indoteknik_custom/views/sale_order.xml b/indoteknik_custom/views/sale_order.xml index 03c978a7..4b74825e 100755 --- a/indoteknik_custom/views/sale_order.xml +++ b/indoteknik_custom/views/sale_order.xml @@ -208,6 +208,11 @@