diff --git a/account_spread_cost_revenue/__manifest__.py b/account_spread_cost_revenue/__manifest__.py index 84f6b719..4beffbb4 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 b87145a9..43c9c62f 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 ad7484e5..87d0184b 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 = 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 = 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 6be08116..6efd3cbe 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 9550b10b..4c61e939 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 74864d8a..850f6458 100644 --- a/account_spread_cost_revenue/tests/test_account_invoice_spread.py +++ b/account_spread_cost_revenue/tests/test_account_invoice_spread.py @@ -1,4 +1,4 @@ -# Copyright 2018 Onestein () +# Copyright 2018-2019 Onestein () # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). import datetime @@ -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') @@ -717,7 +720,6 @@ class TestAccountInvoiceSpread(common.TransactionCase): other_journal = self.env['account.journal'].create({ 'name': 'Other Journal', 'type': 'general', 'code': 'test2'}) - self.assertTrue(other_journal) with self.assertRaises(ValidationError): self.spread2.journal_id = other_journal diff --git a/account_spread_cost_revenue/tests/test_account_spread_cost_revenue.py b/account_spread_cost_revenue/tests/test_account_spread_cost_revenue.py index 502f3847..38d66e70 100644 --- a/account_spread_cost_revenue/tests/test_account_spread_cost_revenue.py +++ b/account_spread_cost_revenue/tests/test_account_spread_cost_revenue.py @@ -1,4 +1,4 @@ -# Copyright 2018 Onestein () +# Copyright 2018-2019 Onestein () # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). import datetime diff --git a/account_spread_cost_revenue/views/account_spread.xml b/account_spread_cost_revenue/views/account_spread.xml index fe40791b..f7c96366 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 @@ + diff --git a/account_spread_cost_revenue/views/res_company.xml b/account_spread_cost_revenue/views/res_company.xml index 00230e14..a93f40c1 100644 --- a/account_spread_cost_revenue/views/res_company.xml +++ b/account_spread_cost_revenue/views/res_company.xml @@ -8,15 +8,20 @@ - + - + + + + + + diff --git a/account_spread_cost_revenue/wizards/account_spread_invoice_line_link_wizard.py b/account_spread_cost_revenue/wizards/account_spread_invoice_line_link_wizard.py index d1784f8c..74212b5d 100644 --- a/account_spread_cost_revenue/wizards/account_spread_invoice_line_link_wizard.py +++ b/account_spread_cost_revenue/wizards/account_spread_invoice_line_link_wizard.py @@ -8,6 +8,24 @@ class AccountSpreadInvoiceLineLinkWizard(models.TransientModel): _name = 'account.spread.invoice.line.link.wizard' _description = 'Account Spread Invoice Line Link Wizard' + def _selection_spread_action_type(self): + base_selection = [ + ('template', 'Create from spread template'), + ('new', 'Create new spread board') + ] + if not self.env.context.get('allow_spread_planning'): + return base_selection + + link_selection = [ + ('link', 'Link to existing spread board'), + ] + return link_selection + base_selection + + def _selection_default_spread_action_type(self): + if not self.env.context.get('allow_spread_planning'): + return 'template' + return 'link' + invoice_line_id = fields.Many2one( 'account.invoice.line', string='Invoice Line', @@ -36,11 +54,9 @@ class AccountSpreadInvoiceLineLinkWizard(models.TransientModel): 'res.company', string='Company', required=True) - spread_action_type = fields.Selection([ - ('link', 'Link to existing spread board'), - ('template', 'Create from spread template'), - ('new', 'Create new spread board')], - default='link') + spread_action_type = fields.Selection( + selection=_selection_spread_action_type, + default=_selection_default_spread_action_type) template_id = fields.Many2one( 'account.spread.template', string='Spread Template')