summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-09-29 09:38:13 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-09-29 09:38:13 +0700
commit7f1d89998467de2b2519bc17dffb5f9c2723c230 (patch)
tree5fcd28dd2af8d1931a525d48662dfc06592ba4de
parent4eff0ccadaade5964fec515a90bb0fc4c915b917 (diff)
Update purchase_order.py and purchase_order_line.py
-rwxr-xr-xindoteknik_custom/models/purchase_order.py35
-rwxr-xr-xindoteknik_custom/models/purchase_order_line.py13
2 files changed, 31 insertions, 17 deletions
diff --git a/indoteknik_custom/models/purchase_order.py b/indoteknik_custom/models/purchase_order.py
index a3226083..ff07d32c 100755
--- a/indoteknik_custom/models/purchase_order.py
+++ b/indoteknik_custom/models/purchase_order.py
@@ -163,11 +163,38 @@ class PurchaseOrder(models.Model):
return res
def compute_total_margin(self):
- if not self.order_line or not self.sale_order_id:
+ sum_so_margin = sum_sales_price = sum_margin = 0
+ for line in self.order_line:
+ 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')
+ 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 -= round(
+ (sale_order_line.order_id.delivery_amt / sale_order_line.order_id.count_line_product), 2)
+ sum_sales_price += sales_price
+ purchase_price = line.price_subtotal
+ if line.order_id.delivery_amount > 0:
+ purchase_price += round((line.order_id.delivery_amount / line.order_id.count_line_product), 2)
+ 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:
+ 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
- return
- for line in self.order_line:
- line.compute_item_margin()
+ # if not self.order_line or not self.sale_order_id:
+ # self.total_margin = 0
+ # self.total_percent_margin = 0
+ # self.total_so_margin = 0
+ # self.total_so_percent_margin = 0
+ # return
+ # for line in self.order_line:
+ # line.compute_item_margin()
diff --git a/indoteknik_custom/models/purchase_order_line.py b/indoteknik_custom/models/purchase_order_line.py
index 4965b507..19c2c8fa 100755
--- a/indoteknik_custom/models/purchase_order_line.py
+++ b/indoteknik_custom/models/purchase_order_line.py
@@ -51,31 +51,18 @@ class PurchaseOrderLine(models.Model):
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')
- # calculate margin so and real margin
- # margin so compare est purchase price vs sales price
- # real margin compare purchase price vs sales price
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
- # minus with delivery if covered by indoteknik
if sale_order_line.order_id.shipping_cost_covered == 'indoteknik':
sales_price -= round((sale_order_line.order_id.delivery_amt / sale_order_line.order_id.count_line_product), 2)
sum_sales_price += sales_price
-
purchase_price = line.price_subtotal
if line.order_id.delivery_amount > 0:
purchase_price += round((line.order_id.delivery_amount / line.order_id.count_line_product), 2)
- # add with delivery amount in sales price or purchase price if any
real_item_margin = sales_price - purchase_price
real_item_percent_margin = round((real_item_margin/sales_price), 2) * 100
line.item_margin = real_item_margin
line.item_percent_margin = real_item_percent_margin
sum_margin += real_item_margin
-
- if sum_so_margin > 0 and sum_sales_price > 0 and sum_margin > 0:
- self.order_id.total_so_margin = sum_so_margin
- self.order_id.total_so_percent_margin = round((sum_so_margin / sum_sales_price), 2) * 100
- self.order_id.total_margin = sum_margin
- self.order_id.total_percent_margin = round((sum_margin / sum_sales_price), 2) * 100