diff --git a/addons/account/wizard/account_automatic_entry_wizard.py b/addons/account/wizard/account_automatic_entry_wizard.py index 1aa9c634a1f..6f0a7a6ee49 100644 --- a/addons/account/wizard/account_automatic_entry_wizard.py +++ b/addons/account/wizard/account_automatic_entry_wizard.py @@ -248,6 +248,59 @@ class AutomaticEntryWizard(models.TransientModel): 'line_ids': [(0, 0, line) for line in line_vals], }] + def _get_move_line_dict_vals_change_period(self, aml, date): + # account.move.line data + accrual_account = self.revenue_accrual_account if self.account_type == 'income' else self.expense_accrual_account + reported_debit = aml.company_id.currency_id.round((self.percentage / 100) * aml.debit) + reported_credit = aml.company_id.currency_id.round((self.percentage / 100) * aml.credit) + reported_amount_currency = aml.currency_id.round((self.percentage / 100) * aml.amount_currency) + + if date == 'new_date': + return [ + (0, 0, { + 'name': aml.name or '', + 'debit': reported_debit, + 'credit': reported_credit, + 'amount_currency': reported_amount_currency, + 'currency_id': aml.currency_id.id, + 'account_id': aml.account_id.id, + 'partner_id': aml.partner_id.id, + 'analytic_distribution': aml.analytic_distribution, + }), + (0, 0, { + 'name': self._format_strings(_('{percent}% recognized on {new_date}'), aml.move_id), + 'debit': reported_credit, + 'credit': reported_debit, + 'amount_currency': -reported_amount_currency, + 'currency_id': aml.currency_id.id, + 'account_id': accrual_account.id, + 'partner_id': aml.partner_id.id, + 'analytic_distribution': aml.analytic_distribution, + }), + ] + return [ + (0, 0, { + 'name': aml.name or '', + 'debit': reported_credit, + 'credit': reported_debit, + 'amount_currency': -reported_amount_currency, + 'currency_id': aml.currency_id.id, + 'account_id': aml.account_id.id, + 'partner_id': aml.partner_id.id, + 'analytic_distribution': aml.analytic_distribution, + }), + (0, 0, { + 'name': self._format_strings(_('{percent}% recognized on {new_date}'), aml.move_id), + 'debit': reported_debit, + 'credit': reported_credit, + 'amount_currency': reported_amount_currency, + 'currency_id': aml.currency_id.id, + 'account_id': accrual_account.id, + 'partner_id': aml.partner_id.id, + 'analytic_distribution': aml.analytic_distribution, + }), + ] + def _get_move_dict_vals_change_period(self): reference_move = self.env['account.move'].new({'journal_id': self.journal_id.id, 'move_type': 'entry'}) @@ -256,7 +309,6 @@ class AutomaticEntryWizard(models.TransientModel): return reference_move._get_accounting_date(aml.date, aml.move_id._affect_tax_report()) # set the change_period account on the selected journal items - accrual_account = self.revenue_accrual_account if self.account_type == 'income' else self.expense_accrual_account move_data = {'new_date': { 'currency_id': self.journal_id.currency_id.id or self.journal_id.company_id.currency_id.id, @@ -281,55 +333,8 @@ class AutomaticEntryWizard(models.TransientModel): # compute the account.move.lines and the total amount per move for aml in self.move_line_ids: - # account.move.line data - reported_debit = aml.company_id.currency_id.round((self.percentage / 100) * aml.debit) - reported_credit = aml.company_id.currency_id.round((self.percentage / 100) * aml.credit) - reported_amount_currency = aml.currency_id.round((self.percentage / 100) * aml.amount_currency) - - move_data['new_date']['line_ids'] += [ - (0, 0, { - 'name': aml.name or '', - 'debit': reported_debit, - 'credit': reported_credit, - 'amount_currency': reported_amount_currency, - 'currency_id': aml.currency_id.id, - 'account_id': aml.account_id.id, - 'partner_id': aml.partner_id.id, - 'analytic_distribution': aml.analytic_distribution, - }), - (0, 0, { - 'name': self._format_strings(_('{percent}% recognized on {new_date}'), aml.move_id), - 'debit': reported_credit, - 'credit': reported_debit, - 'amount_currency': -reported_amount_currency, - 'currency_id': aml.currency_id.id, - 'account_id': accrual_account.id, - 'partner_id': aml.partner_id.id, - 'analytic_distribution': aml.analytic_distribution, - }), - ] - move_data[get_lock_safe_date(aml)]['line_ids'] += [ - (0, 0, { - 'name': aml.name or '', - 'debit': reported_credit, - 'credit': reported_debit, - 'amount_currency': -reported_amount_currency, - 'currency_id': aml.currency_id.id, - 'account_id': aml.account_id.id, - 'partner_id': aml.partner_id.id, - 'analytic_distribution': aml.analytic_distribution, - }), - (0, 0, { - 'name': self._format_strings(_('{percent}% to recognize on {new_date}'), aml.move_id), - 'debit': reported_debit, - 'credit': reported_credit, - 'amount_currency': reported_amount_currency, - 'currency_id': aml.currency_id.id, - 'account_id': accrual_account.id, - 'partner_id': aml.partner_id.id, - 'analytic_distribution': aml.analytic_distribution, - }), - ] + for date in ('new_date', get_lock_safe_date(aml)): + move_data[date]['line_ids'] += self._get_move_line_dict_vals_change_period(aml, date) move_vals = [m for m in move_data.values()] return move_vals diff --git a/addons/account_fleet/__init__.py b/addons/account_fleet/__init__.py index dc5e6b693d1..2ae6446f9dc 100644 --- a/addons/account_fleet/__init__.py +++ b/addons/account_fleet/__init__.py @@ -2,3 +2,4 @@ # Part of Odoo. See LICENSE file for full copyright and licensing details. from . import models +from . import wizard diff --git a/addons/account_fleet/tests/__init__.py b/addons/account_fleet/tests/__init__.py new file mode 100644 index 00000000000..ec31b2f37b4 --- /dev/null +++ b/addons/account_fleet/tests/__init__.py @@ -0,0 +1 @@ +from . import test_account_fleet diff --git a/addons/account_fleet/tests/test_account_fleet.py b/addons/account_fleet/tests/test_account_fleet.py new file mode 100644 index 00000000000..7a1d8878231 --- /dev/null +++ b/addons/account_fleet/tests/test_account_fleet.py @@ -0,0 +1,43 @@ +from freezegun import freeze_time +from odoo.addons.account.tests.common import AccountTestInvoicingCommon +from odoo.tests import tagged + +@tagged('post_install', '-at_install') +class TestAccountFleet(AccountTestInvoicingCommon): + + @freeze_time('2021-09-15') + def test_transfer_wizard_vehicle_info_propagation(self): + brand = self.env["fleet.vehicle.model.brand"].create({ + "name": "Audi", + }) + model = self.env["fleet.vehicle.model"].create({ + "brand_id": brand.id, + "name": "A3", + }) + car_1 = self.env["fleet.vehicle"].create({ + "model_id": model.id, + "plan_to_change_car": False + }) + + bill = self.init_invoice('in_invoice', products=self.product_a, invoice_date='2021-09-01', post=False) + bill.invoice_line_ids.write({'vehicle_id': car_1.id}) + bill.action_post() + + context = {'active_model': 'account.move.line', 'active_ids': bill.invoice_line_ids.ids} + expense_account = self.company_data['default_account_expense'] + wizard = self.env['account.automatic.entry.wizard'].with_context(context).create({ + 'action': 'change_period', + 'date': '2021-09-10', + 'percentage': 60, + 'journal_id': self.company_data['default_journal_misc'].id, + 'expense_accrual_account': expense_account.id, + 'revenue_accrual_account': self.env['account.account'].create({ + 'name': 'Accrual Revenue Account', + 'code': '765432', + 'account_type': 'expense', + 'reconcile': True, + }).id, + }) + result_action = wizard.do_action() + transfer_moves = self.env['account.move'].search(result_action['domain']) + self.assertEqual(transfer_moves.line_ids.filtered(lambda l: l.account_id == expense_account).vehicle_id, car_1, "Vehicle info is missing") diff --git a/addons/account_fleet/wizard/__init__.py b/addons/account_fleet/wizard/__init__.py new file mode 100644 index 00000000000..40ac0e0f5fc --- /dev/null +++ b/addons/account_fleet/wizard/__init__.py @@ -0,0 +1 @@ +from . import account_automatic_entry_wizard diff --git a/addons/account_fleet/wizard/account_automatic_entry_wizard.py b/addons/account_fleet/wizard/account_automatic_entry_wizard.py new file mode 100644 index 00000000000..34bed3a2bb4 --- /dev/null +++ b/addons/account_fleet/wizard/account_automatic_entry_wizard.py @@ -0,0 +1,12 @@ +from odoo import models + +class AutomaticEntryWizard(models.TransientModel): + _inherit = 'account.automatic.entry.wizard' + + def _get_move_line_dict_vals_change_period(self, aml, date): + res = super()._get_move_line_dict_vals_change_period(aml, date) + if aml.vehicle_id: + for move_line_data in res: + if move_line_data[2]['account_id'] == aml.account_id.id: + move_line_data[2]['vehicle_id'] = aml.vehicle_id.id + return res