summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/purchase_order_line.py
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-09-28 16:25:01 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-09-28 16:25:01 +0700
commit597d6458234a14bd20e7fba8a0ceb07168423d2a (patch)
treed3af413c17daa60486fa52ed63d582e3ac66e5da /indoteknik_custom/models/purchase_order_line.py
parent009be5919cb5835f98f5882f69b3a7652e8ec658 (diff)
Update purchase_order.py, purchase_order_line.py, and 3 more files...
Diffstat (limited to 'indoteknik_custom/models/purchase_order_line.py')
-rwxr-xr-xindoteknik_custom/models/purchase_order_line.py86
1 files changed, 52 insertions, 34 deletions
diff --git a/indoteknik_custom/models/purchase_order_line.py b/indoteknik_custom/models/purchase_order_line.py
index 4229fcea..fcfab1da 100755
--- a/indoteknik_custom/models/purchase_order_line.py
+++ b/indoteknik_custom/models/purchase_order_line.py
@@ -5,6 +5,18 @@ from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
class PurchaseOrderLine(models.Model):
_inherit = 'purchase.order.line'
+ item_margin = fields.Float(
+ 'Margin', compute='compute_item_margin',
+ help="Total Margin in Sales Order Header")
+ item_percent_margin = fields.Float(
+ 'Margin%', compute='compute_item_margin',
+ help="Total % Margin in Sales Order Header")
+ so_item_margin = fields.Float(
+ 'SO Margin', compute='compute_item_margin',
+ help="Total Margin in Sales Order Header")
+ so_item_percent_margin = fields.Float(
+ 'SO Margin%', compute='compute_item_margin',
+ help="Total % Margin in Sales Order Header")
# Override method from addons/purchase/models/purchase.py
@api.onchange('product_qty', 'product_uom')
@@ -27,37 +39,43 @@ 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' or not line.order_id.sale_order_id.id:
- # 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')
- #
- # est_purchase_price = sale_order_line.purchase_price
- # real_purchase_price = line.price_unit
- # real_tax = 0
- # real_tax_amount = 0
- # count_real_tax = 0
- # for tax in line.taxes_id:
- # count_real_tax += 1
- # real_tax = tax
- # real_tax_amount += tax.amount
- # if sale_order_line.purchase_tax_id.amount != real_tax_amount or count_real_tax > 1:
- # raise UserError("Beda tax amount dengan Sales")
- #
- # # 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
+ 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':
+ 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')
+ # 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