From 60fffef5ee0c91ba58a056823fc2feff6241a303 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Thu, 22 Sep 2022 13:46:48 +0700 Subject: add calculate margin including delivery fee in purchase order --- indoteknik_custom/models/purchase_order_line.py | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'indoteknik_custom/models/purchase_order_line.py') 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 -- cgit v1.2.3 From f38c1291c45beebd9c44939aee1adbf46cdbd7dd Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Sat, 24 Sep 2022 10:19:24 +0700 Subject: skip calculate margin if not have sale order --- indoteknik_custom/models/purchase_order_line.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indoteknik_custom/models/purchase_order_line.py') diff --git a/indoteknik_custom/models/purchase_order_line.py b/indoteknik_custom/models/purchase_order_line.py index 24afcab6..55381c6d 100755 --- a/indoteknik_custom/models/purchase_order_line.py +++ b/indoteknik_custom/models/purchase_order_line.py @@ -37,7 +37,7 @@ class PurchaseOrderLine(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 or line.product_id.type == 'service': + 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 -- cgit v1.2.3 From 7ae5b8b7baafb9ed302c3c3e54f3f44664651c06 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Mon, 26 Sep 2022 18:25:41 +0700 Subject: Update purchase_order.py, purchase_order_line.py, and 2 more files... --- indoteknik_custom/models/purchase_order_line.py | 68 +++++++++++++------------ 1 file changed, 36 insertions(+), 32 deletions(-) (limited to 'indoteknik_custom/models/purchase_order_line.py') diff --git a/indoteknik_custom/models/purchase_order_line.py b/indoteknik_custom/models/purchase_order_line.py index 55381c6d..4229fcea 100755 --- a/indoteknik_custom/models/purchase_order_line.py +++ b/indoteknik_custom/models/purchase_order_line.py @@ -1,18 +1,10 @@ -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' - 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') @@ -35,25 +27,37 @@ 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') - - # 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): + # 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 -- cgit v1.2.3 From 597d6458234a14bd20e7fba8a0ceb07168423d2a Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Wed, 28 Sep 2022 16:25:01 +0700 Subject: Update purchase_order.py, purchase_order_line.py, and 3 more files... --- indoteknik_custom/models/purchase_order_line.py | 86 +++++++++++++++---------- 1 file changed, 52 insertions(+), 34 deletions(-) (limited to 'indoteknik_custom/models/purchase_order_line.py') 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 -- cgit v1.2.3 From ac37a854362dbaaf8a88431ea5ab077a37f3c5f9 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Wed, 28 Sep 2022 17:17:44 +0700 Subject: Update purchase_order.py and purchase_order_line.py --- indoteknik_custom/models/purchase_order_line.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'indoteknik_custom/models/purchase_order_line.py') diff --git a/indoteknik_custom/models/purchase_order_line.py b/indoteknik_custom/models/purchase_order_line.py index fcfab1da..4965b507 100755 --- a/indoteknik_custom/models/purchase_order_line.py +++ b/indoteknik_custom/models/purchase_order_line.py @@ -42,7 +42,7 @@ class PurchaseOrderLine(models.Model): 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': + 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 -- cgit v1.2.3 From 7f1d89998467de2b2519bc17dffb5f9c2723c230 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Thu, 29 Sep 2022 09:38:13 +0700 Subject: Update purchase_order.py and purchase_order_line.py --- indoteknik_custom/models/purchase_order_line.py | 13 ------------- 1 file changed, 13 deletions(-) (limited to 'indoteknik_custom/models/purchase_order_line.py') 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 -- cgit v1.2.3 From 1e40f3b647d6825779503858bb2ed2d0f8a4b184 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Thu, 29 Sep 2022 19:06:49 +0700 Subject: Update purchase_order.py, purchase_order_line.py, and 2 more files... --- indoteknik_custom/models/purchase_order_line.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'indoteknik_custom/models/purchase_order_line.py') diff --git a/indoteknik_custom/models/purchase_order_line.py b/indoteknik_custom/models/purchase_order_line.py index 19c2c8fa..bd758055 100755 --- a/indoteknik_custom/models/purchase_order_line.py +++ b/indoteknik_custom/models/purchase_order_line.py @@ -17,6 +17,7 @@ class PurchaseOrderLine(models.Model): 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') @@ -56,13 +57,22 @@ class PurchaseOrderLine(models.Model): 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) + 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 += round((line.order_id.delivery_amount / line.order_id.count_line_product), 2) + 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 -- cgit v1.2.3