summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/purchase_order_line.py
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-09-22 13:46:48 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-09-22 13:46:48 +0700
commit60fffef5ee0c91ba58a056823fc2feff6241a303 (patch)
tree9135ec67b5f509d74787e8869d4b11b60e39d7fb /indoteknik_custom/models/purchase_order_line.py
parentb5c15f8ee9171ade6f3d11415a383b0a9f936110 (diff)
add calculate margin including delivery fee in purchase order
Diffstat (limited to 'indoteknik_custom/models/purchase_order_line.py')
-rwxr-xr-xindoteknik_custom/models/purchase_order_line.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/indoteknik_custom/models/purchase_order_line.py b/indoteknik_custom/models/purchase_order_line.py
index b4be9ffc..24afcab6 100755
--- a/indoteknik_custom/models/purchase_order_line.py
+++ b/indoteknik_custom/models/purchase_order_line.py
@@ -4,6 +4,15 @@ from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
+ sales_price = fields.Float(
+ 'Sales Price', compute='compute_item_margin',
+ help="Sales Price in Purchase Order Line")
+ item_margin = fields.Float(
+ 'Total Margin', compute='compute_item_margin',
+ help="Total Margin in Purchase Order Line")
+ item_percent_margin = fields.Float(
+ 'Total Percent Margin', compute='compute_item_margin',
+ help="Total % Margin in Purchase Order Line")
# Override method from addons/purchase/models/purchase.py
@api.onchange('product_qty', 'product_uom')
@@ -25,3 +34,26 @@ class PurchaseOrderLine(models.Model):
self.price_unit = price_unit
return res
+
+ 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 or line.product_id.type == 'service':
+ 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')
+
+ # untaxed sales price
+ sales_price = sale_order_line.price_reduce_taxexcl * sale_order_line.product_uom_qty
+ # must add expedition price in sales_price, expedition payed by customer?
+ # sales_price -= round((line.order_id.sale_order_id.delivery_amount / line.order_id.sale_order_id.count_line_product), 2)
+ # untaxed purchase price
+ purchase_price = line.price_subtotal
+ # must add expedition price in purchase_price as expenses
+ purchase_price += round((line.order_id.delivery_amount / line.order_id.count_line_product), 2)
+ margin_per_item = sales_price - purchase_price
+ line.item_margin = margin_per_item
+ if sales_price > 0:
+ line.item_percent_margin = round((margin_per_item / sales_price), 2) * 100 \ No newline at end of file