From 78c8326a608db2d5b96fa34ffc78b6a9513aee31 Mon Sep 17 00:00:00 2001 From: Carlos Dauden Date: Wed, 28 Mar 2018 09:58:45 +0200 Subject: [PATCH] [IMP] contract: Add extend existing invoice possibility --- contract/__manifest__.py | 2 +- contract/models/account_analytic_account.py | 26 ++++++++++++++++++--- contract/tests/test_contract.py | 13 +++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/contract/__manifest__.py b/contract/__manifest__.py index 1512a6f9..b5343ee7 100644 --- a/contract/__manifest__.py +++ b/contract/__manifest__.py @@ -8,7 +8,7 @@ { 'name': 'Contracts Management - Recurring', - 'version': '11.0.2.0.0', + 'version': '11.0.2.0.1', 'category': 'Contract Management', 'license': 'AGPL-3', 'author': "OpenERP SA, " diff --git a/contract/models/account_analytic_account.py b/contract/models/account_analytic_account.py index 09b99d49..31ccf503 100644 --- a/contract/models/account_analytic_account.py +++ b/contract/models/account_analytic_account.py @@ -236,10 +236,30 @@ class AccountAnalyticAccount(models.Model): return invoice._convert_to_write(invoice._cache) @api.multi - def _create_invoice(self): + def _prepare_invoice_update(self, invoice): + vals = self._prepare_invoice() + update_vals = { + 'contract_id': self.id, + 'date_invoice': vals.get('date_invoice', False), + 'reference': ' '.join(filter(None, [ + invoice.reference, vals.get('reference')])), + 'origin': ' '.join(filter(None, [ + invoice.origin, vals.get('origin')])), + } + return update_vals + + @api.multi + def _create_invoice(self, invoice=False): + """ + :param invoice: If not False add lines to this invoice + :return: invoice created or updated + """ self.ensure_one() - invoice_vals = self._prepare_invoice() - invoice = self.env['account.invoice'].create(invoice_vals) + if invoice and invoice.state == 'draft': + invoice.update(self._prepare_invoice_update(invoice)) + else: + invoice = self.env['account.invoice'].create( + self._prepare_invoice()) for line in self.recurring_invoice_line_ids: invoice_line_vals = self._prepare_invoice_line(line, invoice.id) if invoice_line_vals: diff --git a/contract/tests/test_contract.py b/contract/tests/test_contract.py index 3c11073b..eaef5f5f 100644 --- a/contract/tests/test_contract.py +++ b/contract/tests/test_contract.py @@ -303,3 +303,16 @@ class TestContract(TestContractBase): self.assertTrue(self.contract.create_invoice_visibility) self.contract.date_end = '2016-01-01' self.assertFalse(self.contract.create_invoice_visibility) + + def test_extend_invoice(self): + AccountInvoice = self.env['account.invoice'] + self.contract.recurring_create_invoice() + invoice = AccountInvoice.search( + [('contract_id', '=', self.contract.id)]) + invoice.origin = 'Orig Invoice' + self.contract._create_invoice(invoice) + self.assertEqual(invoice.origin, 'Orig Invoice Test Contract') + invoice_count = AccountInvoice.search_count( + [('contract_id', '=', self.contract.id)]) + self.assertEqual(invoice_count, 1) + self.assertEqual(len(invoice.invoice_line_ids), 2)