From 6c22269284811346002724d7a58d588be20861f5 Mon Sep 17 00:00:00 2001 From: cubells Date: Thu, 1 Jun 2017 22:59:15 +0200 Subject: [PATCH] [9.0][FIX] contract_payment_mode: Payment mode unknow. Improve tests --- contract/models/contract.py | 5 +- contract/tests/test_contract.py | 67 ++++++++++--------- .../models/account_analytic_analysis.py | 23 +++---- .../test_contract_invoice_merge_by_partner.py | 6 +- contract_payment_mode/models/contract.py | 12 ++-- .../tests/test_contract_payment.py | 7 +- 6 files changed, 54 insertions(+), 66 deletions(-) diff --git a/contract/models/contract.py b/contract/models/contract.py index ebd61df6..9ef2e51d 100644 --- a/contract/models/contract.py +++ b/contract/models/contract.py @@ -264,6 +264,7 @@ class AccountAnalyticAccount(models.Model): @api.multi def recurring_create_invoice(self): + invoices = self.env['account.invoice'] for contract in self: old_date = fields.Date.from_string( contract.recurring_next_date or fields.Date.today()) @@ -277,11 +278,11 @@ class AccountAnalyticAccount(models.Model): 'force_company': contract.company_id.id, }) # Re-read contract with correct company - contract.with_context(ctx)._create_invoice() + invoices |= contract.with_context(ctx)._create_invoice() contract.write({ 'recurring_next_date': new_date.strftime('%Y-%m-%d') }) - return True + return invoices @api.model def cron_recurring_create_invoice(self): diff --git a/contract/tests/test_contract.py b/contract/tests/test_contract.py index a04b8511..c0147a32 100644 --- a/contract/tests/test_contract.py +++ b/contract/tests/test_contract.py @@ -3,30 +3,31 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from openerp.exceptions import ValidationError -from openerp.tests.common import TransactionCase +from openerp.tests.common import SavepointCase -class TestContract(TransactionCase): +class TestContract(SavepointCase): # Use case : Prepare some data for current test case - def setUp(self): - super(TestContract, self).setUp() - self.partner = self.env.ref('base.res_partner_2') - self.product = self.env.ref('product.product_product_2') - self.product.description_sale = 'Test description sale' - self.contract = self.env['account.analytic.account'].create({ + @classmethod + def setUpClass(cls): + super(TestContract, cls).setUpClass() + cls.partner = cls.env.ref('base.res_partner_2') + cls.product = cls.env.ref('product.product_product_2') + cls.product.description_sale = 'Test description sale' + cls.contract = cls.env['account.analytic.account'].create({ 'name': 'Test Contract', - 'partner_id': self.partner.id, - 'pricelist_id': self.partner.property_product_pricelist.id, + 'partner_id': cls.partner.id, + 'pricelist_id': cls.partner.property_product_pricelist.id, 'recurring_invoices': True, 'date_start': '2016-02-15', 'recurring_next_date': '2016-02-29', }) - self.contract_line = self.env['account.analytic.invoice.line'].create({ - 'analytic_account_id': self.contract.id, - 'product_id': self.product.id, + cls.contract_line = cls.env['account.analytic.invoice.line'].create({ + 'analytic_account_id': cls.contract.id, + 'product_id': cls.product.id, 'name': 'Services from #START# to #END#', 'quantity': 1, - 'uom_id': self.product.uom_id.id, + 'uom_id': cls.product.uom_id.id, 'price_unit': 100, 'discount': 50, }) @@ -46,16 +47,13 @@ class TestContract(TransactionCase): self.contract.recurring_create_invoice() self.contract.partner_id = self.partner.id - self.contract.recurring_create_invoice() - self.invoice_monthly = self.env['account.invoice'].search( - [('contract_id', '=', self.contract.id)]) - self.assertTrue(self.invoice_monthly) + new_invoice = self.contract.recurring_create_invoice() + self.assertTrue(new_invoice) self.assertEqual(self.contract.recurring_next_date, '2016-03-29') - self.inv_line = self.invoice_monthly.invoice_line_ids[0] + self.inv_line = new_invoice.invoice_line_ids[0] self.assertAlmostEqual(self.inv_line.price_subtotal, 50.0) - self.assertEqual(self.contract.partner_id.user_id, - self.invoice_monthly.user_id) + self.assertEqual(self.contract.partner_id.user_id, new_invoice.user_id) def test_contract_daily(self): self.contract.recurring_next_date = '2016-02-29' @@ -71,20 +69,16 @@ class TestContract(TransactionCase): self.contract.recurring_next_date = '2016-02-29' self.contract.recurring_rule_type = 'weekly' self.contract.recurring_invoicing_type = 'post-paid' - self.contract.recurring_create_invoice() - invoices_weekly = self.env['account.invoice'].search( - [('contract_id', '=', self.contract.id)]) - self.assertTrue(invoices_weekly) + new_invoice = self.contract.recurring_create_invoice() + self.assertTrue(new_invoice) self.assertEqual( self.contract.recurring_next_date, '2016-03-07') def test_contract_yearly(self): self.contract.recurring_next_date = '2016-02-29' self.contract.recurring_rule_type = 'yearly' - self.contract.recurring_create_invoice() - invoices_weekly = self.env['account.invoice'].search( - [('contract_id', '=', self.contract.id)]) - self.assertTrue(invoices_weekly) + new_invoice = self.contract.recurring_create_invoice() + self.assertTrue(new_invoice) self.assertEqual( self.contract.recurring_next_date, '2017-02-28') @@ -92,12 +86,19 @@ class TestContract(TransactionCase): self.contract.recurring_next_date = '2016-02-29' self.contract.recurring_invoicing_type = 'post-paid' self.contract.recurring_rule_type = 'monthlylastday' - self.contract.recurring_create_invoice() - invoices_monthly_lastday = self.env['account.invoice'].search( - [('contract_id', '=', self.contract.id)]) - self.assertTrue(invoices_monthly_lastday) + new_invoice = self.contract.recurring_create_invoice() + self.assertTrue(new_invoice) self.assertEqual(self.contract.recurring_next_date, '2016-03-31') + def test_contract_monthly_lastday_prepaid(self): + self.contract.recurring_next_date = '2016-02-25' + self.contract.recurring_invoicing_type = 'pre-paid' + self.contract.recurring_rule_type = 'monthlylastday' + self.contract.recurring_create_invoice() + new_invoice = self.contract.recurring_create_invoice() + self.assertTrue(new_invoice) + self.assertEqual(new_invoice.date_invoice, '2016-03-31') + def test_onchange_partner_id(self): self.contract._onchange_partner_id() self.assertEqual(self.contract.pricelist_id, diff --git a/contract_invoice_merge_by_partner/models/account_analytic_analysis.py b/contract_invoice_merge_by_partner/models/account_analytic_analysis.py index 4b29a9cc..47f72730 100644 --- a/contract_invoice_merge_by_partner/models/account_analytic_analysis.py +++ b/contract_invoice_merge_by_partner/models/account_analytic_analysis.py @@ -4,7 +4,7 @@ # Copyright 2017 Vicent Cubells # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). -from openerp import api, fields, models +from openerp import api, models class AccountAnalyticAccount(models.Model): @@ -12,23 +12,16 @@ class AccountAnalyticAccount(models.Model): @api.multi def recurring_create_invoice(self): - contracts = self.search( - [('recurring_next_date', '<=', fields.Date.today()), - ('account_type', '=', 'normal'), - ('recurring_invoices', '=', True)] - ) - res = super(AccountAnalyticAccount, self).recurring_create_invoice() - if not contracts: - return res - invoices = self.env['account.invoice'].search([ - ('contract_id', 'in', contracts.ids) - ]) - invoices2unlink = self.env['account.invoice'] + invoices = super( + AccountAnalyticAccount, self).recurring_create_invoice() + invoices_info = {} + invoices2unlink = AccountInvoice = self.env['account.invoice'] for partner in invoices.mapped('partner_id'): invoices2merge = invoices.filtered( lambda x: x.partner_id == partner) if partner.contract_invoice_merge and len(invoices2merge) > 1: - invoices2merge.do_merge() + invoices_info.update(invoices2merge.do_merge()) invoices2unlink += invoices2merge + invoices -= invoices2unlink invoices2unlink.unlink() - return True + return invoices | AccountInvoice.browse(invoices_info.keys()) diff --git a/contract_invoice_merge_by_partner/tests/test_contract_invoice_merge_by_partner.py b/contract_invoice_merge_by_partner/tests/test_contract_invoice_merge_by_partner.py index 6fb8f33b..efae1fa8 100644 --- a/contract_invoice_merge_by_partner/tests/test_contract_invoice_merge_by_partner.py +++ b/contract_invoice_merge_by_partner/tests/test_contract_invoice_merge_by_partner.py @@ -32,8 +32,6 @@ class TestContractInvoiceMergeByPartner(common.SavepointCase): }) def test_invoices_merged(self): - res = self.env['account.analytic.account'].recurring_create_invoice() - self.assertEqual(res, True) self.contract1.write({ 'recurring_invoices': True, 'recurring_rule_type': 'monthly', @@ -51,9 +49,7 @@ class TestContractInvoiceMergeByPartner(common.SavepointCase): contracts = self.env['account.analytic.account'].search([ ('partner_id', '=', self.partner.id) ]) - contracts.recurring_create_invoice() - invoices = self.env['account.invoice'].search( - [('partner_id', '=', self.partner.id)]) + invoices = contracts.recurring_create_invoice() inv_draft = invoices.filtered(lambda x: x.state == 'draft') self.assertEqual(len(inv_draft), 1) inv_cancel = invoices.filtered(lambda x: x.state == 'cancel') diff --git a/contract_payment_mode/models/contract.py b/contract_payment_mode/models/contract.py index 60ab6684..e7936e63 100644 --- a/contract_payment_mode/models/contract.py +++ b/contract_payment_mode/models/contract.py @@ -16,12 +16,10 @@ class AccountAnalyticAccount(models.Model): if self.partner_id.customer_payment_mode_id: self.payment_mode_id = self.partner_id.customer_payment_mode_id.id - @api.model - def _prepare_invoice_data(self, contract): + @api.multi + def _prepare_invoice(self): invoice_vals = super(AccountAnalyticAccount, self)._prepare_invoice() - if contract.payment_mode_id: - invoice_vals['payment_mode_id'] = contract.payment_mode_id.id - invoice_vals['partner_bank_id'] = ( - contract.partner_id.bank_ids[:1].id - ) + if self.payment_mode_id: + invoice_vals['payment_mode_id'] = self.payment_mode_id.id + invoice_vals['partner_bank_id'] = self.partner_id.bank_ids[:1].id return invoice_vals diff --git a/contract_payment_mode/tests/test_contract_payment.py b/contract_payment_mode/tests/test_contract_payment.py index d5c41106..fda49fe2 100644 --- a/contract_payment_mode/tests/test_contract_payment.py +++ b/contract_payment_mode/tests/test_contract_payment.py @@ -69,14 +69,13 @@ class TestContractPaymentInit(common.SavepointCase): 'uom_id': self.product.uom_id.id, })] }) - res = self.contract._prepare_invoice_data(self.contract) - self.assertEqual(res.get('partner_id'), self.contract.partner_id.id) - self.assertEqual(res.get('payment_mode_id'), - self.contract.payment_mode_id.id) self.contract.recurring_create_invoice() new_invoice = self.env['account.invoice'].search([ ('contract_id', '=', self.contract.id) ]) + self.assertEqual(new_invoice.partner_id, self.contract.partner_id) + self.assertEqual(new_invoice.payment_mode_id, + self.contract.payment_mode_id) self.assertEqual(len(new_invoice.ids), 1) self.contract.recurring_create_invoice() self.assertEqual(self.contract.payment_mode_id,