[IMP+FIX] contract: Several things

- Add failing test for next invoice date before the last date invoiced
- raise an error when next invoice date before the last date invoiced
- Add note field to contract
- add new option: create_new_line_at_contract_line_renew
  Add a company config option to decide whether to create or to extend contract
  line at renew action
- extend contract line at renewal
- improve code: unify methods argument _renew_create_line and _renew_extend_line
This commit is contained in:
sbejaoui
2019-12-21 14:05:15 +01:00
committed by Pedro M. Baeza
parent 990ea36348
commit ddd6bfb967
80 changed files with 3623 additions and 305 deletions

View File

@@ -9,3 +9,6 @@ from . import contract_line
from . import account_invoice
from . import account_invoice_line
from . import res_partner
from . import contract_tag
from . import res_company
from . import res_config_settings

View File

@@ -91,6 +91,8 @@ class ContractContract(models.Model):
string='Commercial Entity',
index=True
)
tag_ids = fields.Many2many(comodel_name="contract.tag", string="Tags")
note = fields.Text(string="Notes")
@api.multi
def _inverse_partner_id(self):

View File

@@ -569,7 +569,9 @@ class ContractLine(models.Model):
% line.name
)
@api.constrains('date_start', 'date_end', 'last_date_invoiced')
@api.constrains(
'date_start', 'date_end', 'last_date_invoiced', 'recurring_next_date'
)
def _check_last_date_invoiced(self):
for rec in self.filtered('last_date_invoiced'):
if rec.date_start and rec.date_start > rec.last_date_invoiced:
@@ -588,6 +590,17 @@ class ContractLine(models.Model):
)
% rec.name
)
if (
rec.recurring_next_date
and rec.recurring_next_date <= rec.last_date_invoiced
):
raise ValidationError(
_(
"You can't have the next invoice date before the date "
"of last invoice for the contract line '%s'"
)
% rec.name
)
@api.constrains('recurring_next_date')
def _check_recurring_next_date_recurring_invoices(self):
@@ -1156,25 +1169,43 @@ class ContractLine(models.Model):
}
@api.multi
def _get_renewal_dates(self):
def _get_renewal_new_date_end(self):
self.ensure_one()
date_start = self.date_end + relativedelta(days=1)
date_end = self._get_first_date_end(
date_start, self.auto_renew_rule_type, self.auto_renew_interval
)
return date_start, date_end
return date_end
@api.multi
def _renew_create_line(self, date_end):
self.ensure_one()
date_start = self.date_end + relativedelta(days=1)
is_auto_renew = self.is_auto_renew
self.stop(self.date_end, post_message=False)
new_line = self.plan_successor(
date_start, date_end, is_auto_renew, post_message=False
)
new_line._onchange_date_start()
return new_line
@api.multi
def _renew_extend_line(self, date_end):
self.ensure_one()
self.date_end = date_end
return self
@api.multi
def renew(self):
res = self.env['contract.line']
for rec in self:
is_auto_renew = rec.is_auto_renew
rec.stop(rec.date_end, post_message=False)
date_start, date_end = rec._get_renewal_dates()
new_line = rec.plan_successor(
date_start, date_end, is_auto_renew, post_message=False
)
new_line._onchange_date_start()
company = rec.contract_id.company_id
date_end = rec._get_renewal_new_date_end()
date_start = rec.date_end + relativedelta(days=1)
if company.create_new_line_at_contract_line_renew:
new_line = rec._renew_create_line(date_end)
else:
new_line = rec._renew_extend_line(date_end)
res |= new_line
msg = _(
"""Contract line for <strong>{product}</strong>

View File

@@ -0,0 +1,17 @@
# Copyright 2019 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import fields, models
class ResCompany(models.Model):
_inherit = 'res.company'
create_new_line_at_contract_line_renew = fields.Boolean(
string="Create New Line At Contract Line Renew",
help="If checked, a new line will be generated at contract line renew "
"and linked to the original one as successor. The default "
"behavior is to extend the end date of the contract by a new "
"subscription period",
)

View File

@@ -0,0 +1,19 @@
# Copyright 2019 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import fields, models
class ResConfigSettings(models.TransientModel):
_inherit = 'res.config.settings'
create_new_line_at_contract_line_renew = fields.Boolean(
related="company_id.create_new_line_at_contract_line_renew",
readonly=False,
string="Create New Line At Contract Line Renew",
help="If checked, a new line will be generated at contract line renew "
"and linked to the original one as successor. The default "
"behavior is to extend the end date of the contract by a new "
"subscription period",
)