summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-09-26 13:29:17 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-09-26 13:29:17 +0700
commit18a08e93126100d572a56e4deced21434082bd72 (patch)
treeb56da2069dfa32cd2719178100b1cf9068cb9915 /indoteknik_custom/models
parent6c2d70c8ba9e5ff2c3432a1752f3fbd50d8bec30 (diff)
Update sale_order.py and sale_order.xml - calculate margin with input purchase price
Diffstat (limited to 'indoteknik_custom/models')
-rwxr-xr-xindoteknik_custom/models/sale_order.py36
1 files changed, 19 insertions, 17 deletions
diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py
index 08e7281e..4d1550aa 100755
--- a/indoteknik_custom/models/sale_order.py
+++ b/indoteknik_custom/models/sale_order.py
@@ -95,7 +95,7 @@ class SaleOrder(models.Model):
total_margin += line.item_margin
order.total_margin = total_margin
if order.amount_untaxed > 0:
- total_percent_margin = round((total_margin / order.amount_untaxed), 4) * 100
+ total_percent_margin = round((total_margin / order.amount_untaxed), 2) * 100
order.total_percent_margin = total_percent_margin
def compute_count_line_product(self):
@@ -126,12 +126,7 @@ class SaleOrderLine(models.Model):
item_percent_margin = fields.Float(
'%Margin', compute='compute_item_margin',
help="Total % Margin in Sales Order Header")
- vendor = fields.Many2one(
- 'res.partner', string='Vendor', readonly=True,
- states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
- required=True, change_default=True, index=True, tracking=1,
- domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",)
- vendor = fields.Many2one(
+ vendor_id = fields.Many2one(
'res.partner', string='Vendor', readonly=True,
states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
change_default=True, index=True, tracking=1,
@@ -142,17 +137,24 @@ class SaleOrderLine(models.Model):
def compute_item_margin(self):
for line in self:
- if not line.product_id or line.price_unit <= 0 or line.product_uom_qty <= 0:
+ 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 or not line.purchase_tax_id:
line.item_margin = 0
line.item_percent_margin = 0
continue
- subtotal_untaxed = line.price_subtotal
- purchase_pricelist = self.env['purchase.pricelist'].search(
- [('product_id', '=', line.product_id.id)], limit=1, order='product_price')
- purchase_pricelist_untaxed = 0
- if purchase_pricelist.product_price > 0:
- purchase_pricelist_untaxed = purchase_pricelist.product_price / 1.11
- margin_per_item = subtotal_untaxed - (purchase_pricelist_untaxed * line.product_uom_qty)
+ # calculate margin without tax
+ sales_price = line.price_reduce_taxexcl * line.product_uom_qty
+ # minus with delivery if covered by indoteknik
+ if line.order_id.shipping_cost_covered:
+ sales_price -= round((line.order_id.delivery_amount / line.order_id.count_line_product), 2)
+
+ purchase_price = line.purchase_price
+ if line.purchase_tax_id.price_include:
+ purchase_price = line.purchase_price / 1.11
+
+ purchase_price = purchase_price * line.product_uom_qty
+ margin_per_item = sales_price - purchase_price
line.item_margin = margin_per_item
- if subtotal_untaxed > 0:
- line.item_percent_margin = round((margin_per_item / subtotal_untaxed), 4) * 100
+ # if sales_price > 0:
+ line.item_percent_margin = round((margin_per_item / sales_price), 2) * 100