From 6ec25a483fbe8e56056bde55ae9bd79398525c6b Mon Sep 17 00:00:00 2001 From: AjmalCybro Date: Sat, 16 Dec 2023 14:43:20 +0530 Subject: [PATCH] Dec 16 : [UPDT] Updated 'margin_product_sale_invoice' --- margin_product_sale_invoice/__manifest__.py | 3 +- .../models/__init__.py | 1 + .../models/account_move.py | 18 +++++++-- .../models/product_product.py | 7 ++-- .../models/product_template.py | 40 +++++++++++++++++++ .../models/sale_order.py | 21 ++++++++-- .../views/product_template_views.xml | 14 +++++++ 7 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 margin_product_sale_invoice/models/product_template.py create mode 100644 margin_product_sale_invoice/views/product_template_views.xml diff --git a/margin_product_sale_invoice/__manifest__.py b/margin_product_sale_invoice/__manifest__.py index 1374a11c8..d7c22c972 100755 --- a/margin_product_sale_invoice/__manifest__.py +++ b/margin_product_sale_invoice/__manifest__.py @@ -21,7 +21,7 @@ ############################################################################# { 'name': 'Margin on Products, Sales & Invoices', - 'version': '16.0.1.0.0', + 'version': '16.0.1.0.3', 'summary': 'Providing margin on products, sales and invoices', 'sequence': 4, 'description': """This module is developed to help to provide the @@ -39,6 +39,7 @@ ], 'data': [ 'report/sale_report_views.xml', + 'views/product_template_views.xml', 'views/product_product_views.xml', 'views/sale_order_views.xml', 'views/account_move_views.xml', diff --git a/margin_product_sale_invoice/models/__init__.py b/margin_product_sale_invoice/models/__init__.py index cd54d9bf0..69fc4cba7 100755 --- a/margin_product_sale_invoice/models/__init__.py +++ b/margin_product_sale_invoice/models/__init__.py @@ -20,5 +20,6 @@ # ############################################################################# from . import product_product +from . import product_template from . import sale_order from . import account_move diff --git a/margin_product_sale_invoice/models/account_move.py b/margin_product_sale_invoice/models/account_move.py index f0f4205bf..48d97295b 100755 --- a/margin_product_sale_invoice/models/account_move.py +++ b/margin_product_sale_invoice/models/account_move.py @@ -27,7 +27,7 @@ class AccountMoveLine(models.Model): _inherit = 'account.move.line' cost_price_amount = fields.Float(string='Cost', - related="product_id.standard_price", + compute='compute_cost_price_amount', store=True, help='Field for the cost') margin_amount = fields.Float(string='Margin Amount', compute='compute_margin_amount', @@ -42,13 +42,25 @@ class AccountMoveLine(models.Model): if record.price_unit and record.cost_price_amount: record.margin_amount = record.price_unit - record.cost_price_amount + @api.depends('product_id') + def compute_cost_price_amount(self): + for record in self: + converted_amount = record.env.company.currency_id._convert( + from_amount=record.product_id.standard_price, + to_currency=record.currency_id, company=record.company_id, + date=fields.Date.today() + ) + record.cost_price_amount = converted_amount + class AccountMove(models.Model): """This class extends for margin on the invoices""" _inherit = 'account.move' - margin_percent_amount = fields.Float(string='Margin %', help='Field margin amount in percentage') - margin_amount_total = fields.Float(string='Margin Amount', help='Field displays total margin amount') + margin_percent_amount = fields.Float(string='Margin %', + help='Field margin amount in percentage') + margin_amount_total = fields.Float(string='Margin Amount', + help='Field displays total margin amount') def action_post(self): """Method for setting the margin amount and margin percent""" diff --git a/margin_product_sale_invoice/models/product_product.py b/margin_product_sale_invoice/models/product_product.py index 0360a394f..f2b05da0e 100755 --- a/margin_product_sale_invoice/models/product_product.py +++ b/margin_product_sale_invoice/models/product_product.py @@ -26,12 +26,13 @@ class ProductProduct(models.Model): """This class represents margin on products""" _inherit = 'product.product' - margin_percent_product = fields.Float(string='Margin %', compute='compute_margin', store=True, - help='Field to store the margin in percentage of the product') + margin_percent_product = fields.Float(string='Margin %', + compute='compute_margin', store=True, + help='Field to store the margin in' + ' percentage of the product') @api.depends('list_price', 'standard_price') def compute_margin(self): - """Method to compute the margin of the product.""" self.margin_percent_product = 0 for record in self: if record.list_price and record.standard_price: diff --git a/margin_product_sale_invoice/models/product_template.py b/margin_product_sale_invoice/models/product_template.py new file mode 100644 index 000000000..93c7106f8 --- /dev/null +++ b/margin_product_sale_invoice/models/product_template.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +############################################################################# +# +# Cybrosys Technologies Pvt. Ltd. +# +# Copyright (C) 2023-TODAY Cybrosys Technologies() +# Author: Cybrosys Techno Solutions() +# +# You can modify it under the terms of the GNU LESSER +# GENERAL PUBLIC LICENSE (LGPL v3), Version 3. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU LESSER GENERAL PUBLIC LICENSE (LGPL v3) for more details. +# +# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE +# (LGPL v3) along with this program. +# If not, see . +# +############################################################################# +from odoo import api, fields, models + + +class ProductTemplate(models.Model): + """This class represents margin on products""" + _inherit = 'product.template' + + margin_percent_product = fields.Float(string='Margin %', + compute='compute_margin', store=True, + help='Field to store the margin in' + ' percentage of the product') + + @api.depends('list_price', 'standard_price') + def compute_margin(self): + """Method to compute the margin of the product.""" + self.margin_percent_product = 0 + for record in self: + if record.list_price and record.standard_price: + record.margin_percent_product = ( record.list_price - record.standard_price) / record.list_price diff --git a/margin_product_sale_invoice/models/sale_order.py b/margin_product_sale_invoice/models/sale_order.py index 5e0352fed..fbeeedd62 100755 --- a/margin_product_sale_invoice/models/sale_order.py +++ b/margin_product_sale_invoice/models/sale_order.py @@ -27,7 +27,7 @@ class SaleOrderLine(models.Model): _inherit = 'sale.order.line' cost_price_sale = fields.Float(string='Cost', - related='product_id.standard_price', + compute='compute_cost_price_amount', store=True, help='Field for cost price') margin_amount_sale = fields.Float(string='Margin Amount', compute='compute_margin_amount', @@ -42,6 +42,16 @@ class SaleOrderLine(models.Model): if record.product_id: record.cost_price_sale = record.product_id.standard_price + @api.depends('product_id') + def compute_cost_price_amount(self): + for record in self: + converted_amount = record.product_id.currency_id._convert( + from_amount=record.product_id.standard_price, + to_currency=record.env.company.currency_id, company=record.company_id, + date=fields.Date.today() + ) + record.cost_price_sale = converted_amount + @api.depends('product_id') def compute_margin_amount(self): """ Compute the margin amount of the line. """ @@ -55,8 +65,10 @@ class SaleOrder(models.Model): """This class is used to display margin on sale order.""" _inherit = 'sale.order' - margin_percent_sale = fields.Float(string='Margin %', help='Field for margin in percentage') - margin_amount_sale_total = fields.Float(string='Margin Amount', help='Field for Margin amount in total ') + margin_percent_sale = fields.Float(string='Margin %', + help='Field for margin in percentage') + margin_amount_sale_total = fields.Float(string='Margin Amount', + help='Field for Margin amount in total ') def action_confirm(self): """Method for confirm sale order and set values to margin and margin @@ -64,6 +76,7 @@ class SaleOrder(models.Model): res = super(SaleOrder, self).action_confirm() for record in self: if record.order_line.product_id: - record.margin_amount_sale_total = sum(record.order_line.mapped('margin_amount_sale')) + record.margin_amount_sale_total = sum( + record.order_line.mapped('margin_amount_sale')) record.margin_percent_sale = record.margin_amount_sale_total / record.amount_total return res diff --git a/margin_product_sale_invoice/views/product_template_views.xml b/margin_product_sale_invoice/views/product_template_views.xml new file mode 100644 index 000000000..5e4d54fba --- /dev/null +++ b/margin_product_sale_invoice/views/product_template_views.xml @@ -0,0 +1,14 @@ + + + + + product.template.view.form.inherit.margin.product.sale.invoice + product.template + + + + + + + +