diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2022-10-04 13:42:30 +0700 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2022-10-04 13:42:30 +0700 |
| commit | c4f30a0164ada85a9a4a25cdd03ee7c4e42774a0 (patch) | |
| tree | 30d246e2cdf3cb1925d88a039926fcd06f2ee82c /indoteknik_custom/models/purchase_order_line.py | |
| parent | d1bc570eae2818bc4b535840f2eb3061b99ca98b (diff) | |
| parent | 1d6351cbab4df3a5f5ceff92c53560437d7b40a4 (diff) | |
Merge commit '1d6351cbab4df3a5f5ceff92c53560437d7b40a4'
Diffstat (limited to 'indoteknik_custom/models/purchase_order_line.py')
| -rwxr-xr-x | indoteknik_custom/models/purchase_order_line.py | 53 |
1 files changed, 52 insertions, 1 deletions
diff --git a/indoteknik_custom/models/purchase_order_line.py b/indoteknik_custom/models/purchase_order_line.py index b4be9ffc..bd758055 100755 --- a/indoteknik_custom/models/purchase_order_line.py +++ b/indoteknik_custom/models/purchase_order_line.py @@ -1,9 +1,23 @@ -from odoo import fields, models, api +from odoo import fields, models, api, _ +from odoo.exceptions import AccessError, UserError, ValidationError 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") + delivery_amt_line = fields.Float('DeliveryAmtLine', compute='compute_delivery_amt_line') # Override method from addons/purchase/models/purchase.py @api.onchange('product_qty', 'product_uom') @@ -25,3 +39,40 @@ class PurchaseOrderLine(models.Model): self.price_unit = price_unit return res + + 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' or not self.order_id.sale_order_id: + 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') + 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 + if sale_order_line.order_id.shipping_cost_covered == 'indoteknik': + sales_price -= sale_order_line.delivery_amt_line + sum_sales_price += sales_price + purchase_price = line.price_subtotal + if line.order_id.delivery_amount > 0: + purchase_price += line.delivery_amt_line + 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 + + def compute_delivery_amt_line(self): + for line in self: + if line.product_id.type == 'product': + contribution = round((line.price_total / line.order_id.amount_total_without_service), 2) + delivery_amt = line.order_id.delivery_amount + line.delivery_amt_line = delivery_amt * contribution + else: + line.delivery_amt_line = 0 |
