From be09415741d5842bcba3cea1e75407801a8d3a6d Mon Sep 17 00:00:00 2001 From: Andrea Date: Fri, 5 Apr 2019 11:52:49 +0200 Subject: [PATCH] Add option to disable spread planning + code review --- account_spread_cost_revenue/__manifest__.py | 2 +- .../models/account_invoice_line.py | 4 ++- .../models/account_spread.py | 20 +++++++++++--- .../models/res_company.py | 6 +++++ .../readme/CONFIGURE.rst | 5 ++++ .../tests/test_account_invoice_spread.py | 13 +++++----- .../views/account_spread.xml | 11 ++++---- .../views/res_company.xml | 9 +++++-- ...account_spread_invoice_line_link_wizard.py | 26 +++++++++++++++---- 9 files changed, 72 insertions(+), 24 deletions(-) diff --git a/account_spread_cost_revenue/__manifest__.py b/account_spread_cost_revenue/__manifest__.py index caa3d132e..3e65d6a03 100644 --- a/account_spread_cost_revenue/__manifest__.py +++ b/account_spread_cost_revenue/__manifest__.py @@ -1,4 +1,4 @@ -# Copyright 2016-2018 Onestein () +# Copyright 2016-2019 Onestein () # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). { diff --git a/account_spread_cost_revenue/models/account_invoice_line.py b/account_spread_cost_revenue/models/account_invoice_line.py index b87145a9d..43c9c62f6 100644 --- a/account_spread_cost_revenue/models/account_invoice_line.py +++ b/account_spread_cost_revenue/models/account_invoice_line.py @@ -49,10 +49,12 @@ class AccountInvoiceLine(models.Model): # In case no spread board is linked to the invoice line # open the wizard to link them + company = self.invoice_id.company_id ctx = dict( self.env.context, default_invoice_line_id=self.id, - default_company_id=self.invoice_id.company_id.id, + default_company_id=company.id, + allow_spread_planning=company.allow_spread_planning, ) return { 'name': _('Link Invoice Line with Spread Board'), diff --git a/account_spread_cost_revenue/models/account_spread.py b/account_spread_cost_revenue/models/account_spread.py index 36dcf5b8a..80e40e48f 100644 --- a/account_spread_cost_revenue/models/account_spread.py +++ b/account_spread_cost_revenue/models/account_spread.py @@ -117,6 +117,9 @@ class AccountSpread(models.Model): display_create_all_moves = fields.Boolean( compute='_compute_display_create_all_moves', string='Display Button All Moves') + display_recompute_buttons = fields.Boolean( + compute='_compute_display_recompute_buttons', + string='Display Buttons Recompute') @api.model def default_get(self, fields): @@ -184,6 +187,14 @@ class AccountSpread(models.Model): else: spread.display_create_all_moves = False + @api.multi + def _compute_display_recompute_buttons(self): + for spread in self: + spread.display_recompute_buttons = True + if not spread.company_id.allow_spread_planning: + if spread.invoice_id.state == 'draft': + spread.display_recompute_buttons = False + @api.multi def _get_spread_entry_name(self, seq): """Use this method to customise the name of the accounting entry.""" @@ -258,7 +269,7 @@ class AccountSpread(models.Model): 'with selected invoice type')) @api.multi - def _compute_spread_period_duration(self): + def _get_spread_period_duration(self): """Converts the selected period_type to number of months.""" self.ensure_one() if self.period_type == 'year': @@ -277,7 +288,7 @@ class AccountSpread(models.Model): # if we already have some previous validated entries, # starting date is last entry + method period last_date = fields.Date.from_string(posted_line_ids[-1].date) - months = self._compute_spread_period_duration() + months = self._get_spread_period_duration() spread_date = last_date + relativedelta(months=months) else: spread_date = fields.Date.from_string(self.spread_date) @@ -289,7 +300,7 @@ class AccountSpread(models.Model): is used by "def _compute_spread_board()" method. """ self.ensure_one() - months = self._compute_spread_period_duration() + months = self._get_spread_period_duration() date = date + relativedelta(months=months) # get the last day of the month if month_day > 28: @@ -512,7 +523,8 @@ class AccountSpread(models.Model): if not line.move_id: line.create_move() - @api.multi + @api.depends( + 'debit_account_id.deprecated', 'credit_account_id.deprecated') def _compute_deprecated_accounts(self): for spread in self: debit_deprecated = bool(spread.debit_account_id.deprecated) diff --git a/account_spread_cost_revenue/models/res_company.py b/account_spread_cost_revenue/models/res_company.py index 6be08116d..6efd3cbe4 100644 --- a/account_spread_cost_revenue/models/res_company.py +++ b/account_spread_cost_revenue/models/res_company.py @@ -18,3 +18,9 @@ class ResCompany(models.Model): default_spread_expense_journal_id = fields.Many2one( 'account.journal', string='Expense Spread Journal') + + allow_spread_planning = fields.Boolean( + default=True, + help="Disable this option if you do not want to allow the " + "spreading before the invoice is validated.", + ) diff --git a/account_spread_cost_revenue/readme/CONFIGURE.rst b/account_spread_cost_revenue/readme/CONFIGURE.rst index 9550b10b1..4c61e9393 100644 --- a/account_spread_cost_revenue/readme/CONFIGURE.rst +++ b/account_spread_cost_revenue/readme/CONFIGURE.rst @@ -10,3 +10,8 @@ In the same *Account Spread* tab, you can also configure the Spread Balance Shee * the *Default Spread Account for Revenues*, * the *Default Spread Account for Expenses*. + + +This module by default allows the spreading even before the receipt of the invoice or when the invoice is still draft, +so that it is possible to work on the plan of the cost/revenue spreading. To disable this feature, on the form view of +the company disable the *Allow Spread Planning* option. diff --git a/account_spread_cost_revenue/tests/test_account_invoice_spread.py b/account_spread_cost_revenue/tests/test_account_invoice_spread.py index ef74f4c9d..5fe605dbc 100644 --- a/account_spread_cost_revenue/tests/test_account_invoice_spread.py +++ b/account_spread_cost_revenue/tests/test_account_invoice_spread.py @@ -123,6 +123,7 @@ class TestAccountInvoiceSpread(common.TransactionCase): wizard1 = Wizard.with_context( default_invoice_line_id=self.invoice_line.id, default_company_id=my_company.id, + allow_spread_planning=True, ).create({}) self.assertEqual(wizard1.invoice_line_id, self.invoice_line) @@ -144,7 +145,7 @@ class TestAccountInvoiceSpread(common.TransactionCase): self.assertEqual(wizard2.invoice_type, 'out_invoice') self.assertFalse(wizard2.spread_id) self.assertEqual(wizard2.company_id, my_company) - self.assertEqual(wizard2.spread_action_type, 'link') + self.assertEqual(wizard2.spread_action_type, 'template') self.assertFalse(wizard2.spread_account_id) self.assertFalse(wizard2.spread_journal_id) @@ -169,6 +170,7 @@ class TestAccountInvoiceSpread(common.TransactionCase): wizard1 = Wizard.with_context( default_invoice_line_id=self.invoice_line.id, default_company_id=my_company.id, + allow_spread_planning=True, ).create({}) self.assertEqual(wizard1.invoice_line_id, self.invoice_line) @@ -200,7 +202,7 @@ class TestAccountInvoiceSpread(common.TransactionCase): self.assertEqual(wizard2.invoice_type, 'out_invoice') self.assertFalse(wizard2.spread_id) self.assertEqual(wizard2.company_id, my_company) - self.assertEqual(wizard2.spread_action_type, 'link') + self.assertEqual(wizard2.spread_action_type, 'template') self.assertFalse(wizard2.spread_account_id) self.assertFalse(wizard2.spread_journal_id) @@ -221,6 +223,7 @@ class TestAccountInvoiceSpread(common.TransactionCase): wizard1 = Wizard.with_context( default_invoice_line_id=self.invoice_line.id, default_company_id=my_company.id, + allow_spread_planning=True, ).create({}) self.assertEqual(wizard1.spread_action_type, 'link') @@ -715,10 +718,8 @@ class TestAccountInvoiceSpread(common.TransactionCase): self.assertFalse(spread_ml.matched_credit_ids) self.assertFalse(spread_ml.full_reconcile_id) - other_journal = self.env['account.journal'].search([ - ('id', '!=', self.invoice_2.journal_id.id), - ], limit=1) - self.assertTrue(other_journal) + other_journal = self.env['account.journal'].create({ + 'name': 'Other Journal', 'type': 'general', 'code': 'test2'}) with self.assertRaises(ValidationError): self.spread2.journal_id = other_journal diff --git a/account_spread_cost_revenue/views/account_spread.xml b/account_spread_cost_revenue/views/account_spread.xml index 8a566bd29..f7c963668 100644 --- a/account_spread_cost_revenue/views/account_spread.xml +++ b/account_spread_cost_revenue/views/account_spread.xml @@ -6,8 +6,8 @@
-
@@ -28,6 +28,7 @@ + @@ -95,11 +96,11 @@ -