From a0868bc1ec601e075a78f7ee4dd5b4d1d9542376 Mon Sep 17 00:00:00 2001 From: yhu-odoo Date: Fri, 18 Sep 2020 14:15:04 +0000 Subject: [PATCH] [IMP] (mrp,stock)_account: using correct account when unbuild a MO Currently when unbuild a MO, we create journal entries the same as receiving components (add credit to input account) and deliver finished product (add debit to output account). But since the unbuild is just a revesal of the MO, the journal entries should alse be revesals. In this commit, when unbuild a MO, for the journal entry about the component change, we add credit to the output account, and for the journal entry of the finished product change, we add debit to input account make sure the journal entries are also reversals. Task 2312828 PR #58150 closes odoo/odoo#60730 X-original-commit: 83a3230706e1e2f4eaf9718156647821341d192b Signed-off-by: Arnold Moyaux --- addons/mrp_account/models/__init__.py | 1 + addons/mrp_account/models/stock_move.py | 12 ++++++++++++ addons/stock_account/models/stock_move.py | 15 ++++++++++----- 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 addons/mrp_account/models/stock_move.py diff --git a/addons/mrp_account/models/__init__.py b/addons/mrp_account/models/__init__.py index 348a288235b..520901ce500 100644 --- a/addons/mrp_account/models/__init__.py +++ b/addons/mrp_account/models/__init__.py @@ -3,3 +3,4 @@ from . import mrp_workcenter from . import mrp_production from . import product +from . import stock_move diff --git a/addons/mrp_account/models/stock_move.py b/addons/mrp_account/models/stock_move.py new file mode 100644 index 00000000000..f5c7f9be12b --- /dev/null +++ b/addons/mrp_account/models/stock_move.py @@ -0,0 +1,12 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo import api, fields, models, _ + + +class StockMove(models.Model): + _inherit = "stock.move" + + def _is_returned(self, valued_type): + if self.unbuild_id and self.unbuild_id.mo_id: # unbuilding a MO + return True + return super()._is_returned(valued_type) diff --git a/addons/stock_account/models/stock_move.py b/addons/stock_account/models/stock_move.py index 6f9c34c49b5..cb11e71fd57 100644 --- a/addons/stock_account/models/stock_move.py +++ b/addons/stock_account/models/stock_move.py @@ -473,11 +473,11 @@ class StockMove(models.Model): company_from = self._is_out() and self.mapped('move_line_ids.location_id.company_id') or False company_to = self._is_in() and self.mapped('move_line_ids.location_dest_id.company_id') or False + journal_id, acc_src, acc_dest, acc_valuation = self._get_accounting_data_for_valuation() # Create Journal Entry for products arriving in the company; in case of routes making the link between several # warehouse of the same company, the transit location belongs to this company, so we don't need to create accounting entries if self._is_in(): - journal_id, acc_src, acc_dest, acc_valuation = self._get_accounting_data_for_valuation() - if location_from and location_from.usage == 'customer': # goods returned from customer + if self._is_returned(valued_type='in'): self.with_company(company_to)._create_account_move_line(acc_dest, acc_valuation, journal_id, qty, description, svl_id, cost) else: self.with_company(company_to)._create_account_move_line(acc_src, acc_valuation, journal_id, qty, description, svl_id, cost) @@ -485,15 +485,13 @@ class StockMove(models.Model): # Create Journal Entry for products leaving the company if self._is_out(): cost = -1 * cost - journal_id, acc_src, acc_dest, acc_valuation = self._get_accounting_data_for_valuation() - if location_to and location_to.usage == 'supplier': # goods returned to supplier + if self._is_returned(valued_type='out'): self.with_company(company_from)._create_account_move_line(acc_valuation, acc_src, journal_id, qty, description, svl_id, cost) else: self.with_company(company_from)._create_account_move_line(acc_valuation, acc_dest, journal_id, qty, description, svl_id, cost) if self.company_id.anglo_saxon_accounting: # Creates an account entry from stock_input to stock_output on a dropship move. https://github.com/odoo/odoo/issues/12687 - journal_id, acc_src, acc_dest, acc_valuation = self._get_accounting_data_for_valuation() if self._is_dropshipped(): if cost > 0: self.with_company(self.company_id)._create_account_move_line(acc_src, acc_valuation, journal_id, qty, description, svl_id, cost) @@ -516,3 +514,10 @@ class StockMove(models.Model): to the way they mix stock moves with invoices. """ return self.env['account.move'] + + def _is_returned(self, valued_type): + self.ensure_one() + if valued_type == 'in': + return self.location_id and self.location_id.usage == 'customer' # goods returned from customer + if valued_type == 'out': + return self.location_dest_id and self.location_dest_id.usage == 'supplier' # goods returned to supplier