Browse Source

[12.0][IMP] - check invoice line vals before assignment

[FIX] - Fix unit tests.
pull/207/head
sbejaoui 6 years ago
parent
commit
42a70ddaac
  1. 9
      contract/models/contract.py
  2. 27
      contract/models/contract_line.py
  3. 32
      contract/tests/test_contract.py

9
contract/models/contract.py

@ -48,8 +48,7 @@ class AccountAnalyticAccount(models.Model):
compute='_compute_date_end', string='Date End', store=True
)
payment_term_id = fields.Many2one(
comodel_name='account.payment.term',
string='Payment Terms',
comodel_name='account.payment.term', string='Payment Terms'
)
@api.depends('recurring_invoice_line_ids.date_end')
@ -330,8 +329,12 @@ class AccountAnalyticAccount(models.Model):
invoice_values = contract._prepare_invoice(date_ref)
for line in contract_lines:
invoice_values.setdefault('invoice_line_ids', [])
invoice_line_values = line._prepare_invoice_line(
invoice_id=False
)
if invoice_line_values:
invoice_values['invoice_line_ids'].append(
(0, 0, line._prepare_invoice_line(invoice_id=False))
(0, 0, invoice_line_values)
)
invoices_values.append(invoice_values)
contract_lines._update_recurring_next_date()

27
contract/models/contract_line.py

@ -385,31 +385,18 @@ class AccountAnalyticInvoiceLine(models.Model):
new_date = old_date + self.get_relative_delta(
rec.recurring_rule_type, rec.recurring_interval
)
if rec.recurring_rule_type == 'monthlylastday':
rec.last_date_invoiced = (
old_date
if rec.date_end and old_date < rec.date_end
else rec.date_end
)
last_date_invoiced = old_date
elif rec.recurring_invoicing_type == 'post-paid':
rec.last_date_invoiced = (
old_date - relativedelta(days=1)
if rec.date_end and old_date < rec.date_end
else rec.date_end
)
last_date_invoiced = old_date - relativedelta(days=1)
elif rec.recurring_invoicing_type == 'pre-paid':
rec.last_date_invoiced = (
new_date - relativedelta(days=1)
if rec.date_end and new_date < rec.date_end
else rec.date_end
)
if (
rec.last_date_invoiced
and rec.last_date_invoiced == rec.date_end
):
last_date_invoiced = new_date - relativedelta(days=1)
if rec.date_end and last_date_invoiced >= rec.date_end:
rec.last_date_invoiced = rec.date_end
rec.recurring_next_date = False
else:
rec.last_date_invoiced = last_date_invoiced
rec.recurring_next_date = new_date
@api.multi

32
contract/tests/test_contract.py

@ -95,9 +95,8 @@ class TestContractBase(common.SavepointCase):
'recurring_rule_type': 'monthly',
'recurring_interval': 1,
'date_start': '2018-01-01',
'date_end': '2019-01-01',
'recurring_next_date': '2018-01-15',
'is_auto_renew': True,
'is_auto_renew': False,
}
cls.acct_line = cls.env['account.analytic.invoice.line'].create(
cls.line_vals
@ -112,7 +111,6 @@ class TestContract(TestContractBase):
vals = self.line_vals.copy()
del vals['contract_id']
del vals['date_start']
del vals['date_end']
vals['contract_id'] = self.template.id
vals.update(overrides)
return self.env['account.analytic.contract.line'].create(vals)
@ -652,9 +650,7 @@ class TestContract(TestContractBase):
def test_date_end(self):
"""recurring next date for a contract is the min for all lines"""
self.assertEqual(self.acct_line.date_end, to_date('2019-01-01'))
self.acct_line.date_end = '2018-01-01'
self.assertEqual(self.acct_line.date_end, to_date('2018-01-01'))
self.acct_line.copy()
self.acct_line.write({'date_end': False, 'is_auto_renew': False})
self.assertFalse(self.contract.date_end)
@ -1247,7 +1243,9 @@ class TestContract(TestContractBase):
)
def test_search_contract_line_to_renew(self):
self.acct_line.write({'date_end': fields.Date.today()})
self.acct_line.write(
{'date_end': fields.Date.today(), 'is_auto_renew': True}
)
line_1 = self.acct_line.copy(
{'date_end': fields.Date.today() + relativedelta(months=1)}
)
@ -1268,13 +1266,29 @@ class TestContract(TestContractBase):
)
def test_renew(self):
date_start = fields.Date.today() - relativedelta(months=9)
date_end = (
date_start + relativedelta(months=12) - relativedelta(days=1)
)
self.acct_line.write(
{
'is_auto_renew': True,
'date_start': date_start,
'recurring_next_date': date_start,
'date_end': fields.Date.today(),
}
)
self.acct_line._onchange_is_auto_renew()
self.assertEqual(self.acct_line.date_end, to_date('2018-12-31'))
self.assertEqual(self.acct_line.date_end, date_end)
new_line = self.acct_line.renew()
self.assertFalse(self.acct_line.is_auto_renew)
self.assertTrue(new_line.is_auto_renew)
self.assertEqual(new_line.date_start, to_date('2019-01-01'))
self.assertEqual(new_line.date_end, to_date('2019-12-31'))
self.assertEqual(
new_line.date_start, date_start + relativedelta(months=12)
)
self.assertEqual(
new_line.date_end, date_end + relativedelta(months=12)
)
def test_cron_recurring_create_invoice(self):
self.acct_line.date_start = '2018-01-01'

Loading…
Cancel
Save