2
0

Add option to disable spread planning + code review

This commit is contained in:
Andrea 2019-04-05 12:04:42 +02:00 committed by Andrea Stirpe
parent 5d7390183d
commit b60c1ab8e4
10 changed files with 69 additions and 20 deletions

View File

@ -1,4 +1,4 @@
# Copyright 2016-2018 Onestein (<https://www.onestein.eu>)
# Copyright 2016-2019 Onestein (<https://www.onestein.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{

View File

@ -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'),

View File

@ -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)

View File

@ -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.",
)

View File

@ -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.

View File

@ -1,4 +1,4 @@
# Copyright 2018 Onestein (<https://www.onestein.eu>)
# Copyright 2018-2019 Onestein (<https://www.onestein.eu>)
# 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

View File

@ -1,4 +1,4 @@
# Copyright 2018 Onestein (<https://www.onestein.eu>)
# Copyright 2018-2019 Onestein (<https://www.onestein.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
import datetime

View File

@ -6,8 +6,8 @@
<field name="arch" type="xml">
<form>
<header>
<button name="compute_spread_board" type="object" string="Recalculate unposted lines" class="oe_highlight" attrs="{'invisible': [('debit_account_id', '=', False)]}" />
<button name="action_recalculate_spread" type="object" string="Recalculate entire spread" attrs="{'invisible': [('debit_account_id', '=', False)]}" groups="account.group_account_manager" />
<button name="compute_spread_board" type="object" string="Recalculate unposted lines" class="oe_highlight" attrs="{'invisible': ['|',('debit_account_id', '=', False),('display_recompute_buttons', '=', False)]}" />
<button name="action_recalculate_spread" type="object" string="Recalculate entire spread" attrs="{'invisible': ['|',('debit_account_id', '=', False),('display_recompute_buttons', '=', False)]}" groups="account.group_account_manager" />
<button name="action_undo_spread" type="object" string="Undo spread" attrs="{'invisible': [('line_ids', '=', [])]}" groups="account.group_account_manager" />
<button name="action_unlink_invoice_line" type="object" string="Unlink Invoice Line" attrs="{'invisible': [('invoice_line_id', '=', False)]}" groups="account.group_account_manager" />
</header>
@ -28,6 +28,7 @@
<group name="spread_definitions">
<field name="template_id"/>
<field name="invoice_type" attrs="{'readonly':[('invoice_line_id','!=',False)]}"/>
<field name="display_recompute_buttons" invisible="1"/>
</group>
</group>
<group name="accounts">

View File

@ -8,15 +8,20 @@
<xpath expr="//notebook">
<page name="account_spread_cost_revenue" string="Account Spread" groups="account.group_account_manager">
<group>
<group string="Default Spread Accounts">
<group string="Default Spread Accounts" name="default_spread_accounts">
<field name="default_spread_revenue_account_id" />
<field name="default_spread_expense_account_id" />
</group>
<group string="Default Spread Journals">
<group string="Default Spread Journals" name="default_spread_journals">
<field name="default_spread_revenue_journal_id" />
<field name="default_spread_expense_journal_id" />
</group>
</group>
<group name="spreading_options">
<group>
<field name="allow_spread_planning"/>
</group>
</group>
</page>
</xpath>
</field>

View File

@ -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')