From 2420c640f18e39bfe912d1bdd6325f0acd46a73c Mon Sep 17 00:00:00 2001 From: Arnold Moyaux Date: Tue, 27 Dec 2022 13:39:06 +0000 Subject: [PATCH] [FIX] purchase, purchase_stock: add price difference account Version 16.0 removed the price different account following this pull request #99411 This decision has been made because it was only used anymore by standard cost method and real time valuation. We thought that standard was not a valid accounting method and we didn't want to maintain code for it. But: - Standard could be valid if you manualy complete the accounting entries by yourself (e.g. employees/machines cost in mrp). - It's also valid if you record the difference between standard price and vendo price (price diff) If you want to have an estimation of your cogs (benefits and loss) during an accounting period. People just do a manual correction at the end but they have a real time reporting on the situation. It's a too big regression to be acceptable. We apologize and reintroduce it for 16.0 and future version. This PR reintroduce the fields in purchase and the fix module in 16.0 is not needed anymore. closes odoo/odoo#108736 Signed-off-by: William Henrotin (whe) --- addons/purchase_stock/__manifest__.py | 1 + .../purchase_stock/models/account_invoice.py | 22 ++++++++++++---- addons/purchase_stock/models/product.py | 14 ++++++++++ addons/purchase_stock/views/product_views.xml | 26 +++++++++++++++++++ 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 addons/purchase_stock/views/product_views.xml diff --git a/addons/purchase_stock/__manifest__.py b/addons/purchase_stock/__manifest__.py index da0cbe122e6..23753cd2887 100644 --- a/addons/purchase_stock/__manifest__.py +++ b/addons/purchase_stock/__manifest__.py @@ -19,6 +19,7 @@ 'views/res_config_settings_views.xml', 'views/res_partner_views.xml', 'views/stock_lot_views.xml', + 'views/product_views.xml', 'report/purchase_report_views.xml', 'report/purchase_report_templates.xml', 'report/report_stock_forecasted.xml', diff --git a/addons/purchase_stock/models/account_invoice.py b/addons/purchase_stock/models/account_invoice.py index d8b135005ac..b89cb564fbc 100644 --- a/addons/purchase_stock/models/account_invoice.py +++ b/addons/purchase_stock/models/account_invoice.py @@ -42,8 +42,13 @@ class AccountMove(models.Model): continue # Retrieve accounts needed to generate the price difference. - accounts = line.product_id.product_tmpl_id.get_product_accounts(fiscal_pos=move.fiscal_position_id) - debit_expense_account = accounts['expense'] + if line.product_id.cost_method == 'standard': + debit_expense_account = line.product_id.property_account_creditor_price_difference \ + or line.product_id.categ_id.property_account_creditor_price_difference_categ + debit_expense_account = line.move_id.fiscal_position_id.map_account(debit_expense_account) + else: + accounts = line.product_id.product_tmpl_id.get_product_accounts(fiscal_pos=move.fiscal_position_id) + debit_expense_account = accounts['expense'] if not debit_expense_account: continue @@ -70,14 +75,21 @@ class AccountMove(models.Model): valuation_price_unit = line.product_id.uom_id._compute_price(valuation_price_unit, line.product_uom_id) else: - continue - + price_unit = line.product_id.uom_id._compute_price(line.product_id.standard_price, line.product_uom_id) + price_unit = -price_unit if line.move_id.move_type == 'in_refund' else price_unit + valuation_price_unit = line.company_currency_id._convert( + price_unit, move.currency_id, + move.company_id, fields.Date.today(), round=False + ) price_unit = line._get_gross_unit_price() price_unit_val_dif = price_unit - valuation_price_unit # If there are some valued moves, we only consider their quantity already used - relevant_qty = line._get_out_and_not_invoiced_qty(valuation_stock_moves) + if line.product_id.cost_method == 'standard': + relevant_qty = line.quantity + else: + relevant_qty = line._get_out_and_not_invoiced_qty(valuation_stock_moves) price_subtotal = relevant_qty * price_unit_val_dif # We consider there is a price difference if the subtotal is not zero. In case a diff --git a/addons/purchase_stock/models/product.py b/addons/purchase_stock/models/product.py index f93667c834d..bb54de04479 100644 --- a/addons/purchase_stock/models/product.py +++ b/addons/purchase_stock/models/product.py @@ -5,10 +5,24 @@ from odoo import api, fields, models from odoo.osv import expression +class ProductCategory(models.Model): + _inherit = "product.category" + + property_account_creditor_price_difference_categ = fields.Many2one( + 'account.account', string="Price Difference Account", + company_dependent=True, + help="This account will be used to value price difference between purchase price and accounting cost.") + + class ProductTemplate(models.Model): _name = 'product.template' _inherit = 'product.template' + property_account_creditor_price_difference = fields.Many2one( + 'account.account', string="Price Difference Account", company_dependent=True, + help="This account is used in automated inventory valuation to "\ + "record the price difference between a purchase order and its related vendor bill when validating this vendor bill.") + @api.model def _get_buy_route(self): buy_route = self.env.ref('purchase_stock.route_warehouse0_buy', raise_if_not_found=False) diff --git a/addons/purchase_stock/views/product_views.xml b/addons/purchase_stock/views/product_views.xml new file mode 100644 index 00000000000..d45cfdea783 --- /dev/null +++ b/addons/purchase_stock/views/product_views.xml @@ -0,0 +1,26 @@ + + + + product.category.property.form.inherit.purchase.stock + product.category + + + + + + + + + + product.normal.form.inherit.purchase.stock + product.template + + + + + + + + \ No newline at end of file