From 4e90ef5afeb441650af5dee1fa7aeedabe47eac6 Mon Sep 17 00:00:00 2001 From: Antonio Espinosa Date: Wed, 27 Jan 2016 13:54:21 +0100 Subject: [PATCH] contract_payment_mode: Several fixes+imps: * Copy partner payment mode to contracts when installing * Filter payment modes for sales * Adding tests --- contract_payment_mode/README.rst | 1 + contract_payment_mode/__init__.py | 4 ++ contract_payment_mode/__openerp__.py | 1 + contract_payment_mode/hooks.py | 27 +++++++++++++ contract_payment_mode/models/contract.py | 5 +-- contract_payment_mode/tests/__init__.py | 5 +++ .../tests/test_contract_payment_init.py | 40 +++++++++++++++++++ 7 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 contract_payment_mode/hooks.py create mode 100644 contract_payment_mode/tests/__init__.py create mode 100644 contract_payment_mode/tests/test_contract_payment_init.py diff --git a/contract_payment_mode/README.rst b/contract_payment_mode/README.rst index 5d4ee46c..09e27ba1 100644 --- a/contract_payment_mode/README.rst +++ b/contract_payment_mode/README.rst @@ -34,6 +34,7 @@ Contributors ------------ * Ángel Moya +* Antonio Espinosa Maintainer diff --git a/contract_payment_mode/__init__.py b/contract_payment_mode/__init__.py index a0fdc10f..753e6cee 100644 --- a/contract_payment_mode/__init__.py +++ b/contract_payment_mode/__init__.py @@ -1,2 +1,6 @@ # -*- coding: utf-8 -*- +# © 2016 Antiun Ingenieria S.L. - Antonio Espinosa +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + from . import models +from .hooks import post_init_hook diff --git a/contract_payment_mode/__openerp__.py b/contract_payment_mode/__openerp__.py index 1eee7c8b..f9c12929 100644 --- a/contract_payment_mode/__openerp__.py +++ b/contract_payment_mode/__openerp__.py @@ -35,6 +35,7 @@ 'views/contract_view.xml', ], 'test': ['test/contract_payment_mode.yml'], + 'post_init_hook': 'post_init_hook', 'installable': True, 'auto_install': True, } diff --git a/contract_payment_mode/hooks.py b/contract_payment_mode/hooks.py new file mode 100644 index 00000000..5e68b6c3 --- /dev/null +++ b/contract_payment_mode/hooks.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- +# © 2016 Antiun Ingenieria S.L. - Antonio Espinosa +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp import api, SUPERUSER_ID +import logging + +_logger = logging.getLogger(__name__) + + +def post_init_hook(cr, registry): + """Copy payment mode from partner to the new field at contract.""" + with api.Environment.manage(): + env = api.Environment(cr, SUPERUSER_ID, {}) + m_contract = env['account.analytic.account'] + contracts = m_contract.search([ + ('type', '=', 'contract'), + ('payment_mode_id', '=', False), + ]) + if contracts: + _logger.info('Setting payment mode: %d contracts' % + len(contracts)) + for contract in contracts: + payment_mode = contract.partner_id.customer_payment_mode + if payment_mode: + contract.payment_mode_id = payment_mode.id + _logger.info('Setting payment mode: Done') diff --git a/contract_payment_mode/models/contract.py b/contract_payment_mode/models/contract.py index 8d1b31a5..0aa60e89 100644 --- a/contract_payment_mode/models/contract.py +++ b/contract_payment_mode/models/contract.py @@ -8,7 +8,7 @@ class AccountAnalyticAccount(models.Model): payment_mode_id = fields.Many2one( 'payment.mode', string='Payment Mode', - domain="[('type', '=', 'sale')]") + domain="[('sale_ok', '=', True)]") @api.multi def on_change_partner_id(self, partner_id, name): @@ -23,8 +23,7 @@ class AccountAnalyticAccount(models.Model): @api.model def _prepare_invoice_data(self, contract): invoice_vals = super(AccountAnalyticAccount, self).\ - _prepare_invoice_data( - contract) + _prepare_invoice_data(contract) if contract.payment_mode_id: invoice_vals['payment_mode_id'] = contract.payment_mode_id.id invoice_vals['partner_bank_id'] = \ diff --git a/contract_payment_mode/tests/__init__.py b/contract_payment_mode/tests/__init__.py new file mode 100644 index 00000000..be86a7bf --- /dev/null +++ b/contract_payment_mode/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2016 Antiun Ingenieria S.L. - Antonio Espinosa +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from . import test_contract_payment_init diff --git a/contract_payment_mode/tests/test_contract_payment_init.py b/contract_payment_mode/tests/test_contract_payment_init.py new file mode 100644 index 00000000..2056a162 --- /dev/null +++ b/contract_payment_mode/tests/test_contract_payment_init.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# © 2015 Antiun Ingenieria S.L. - Antonio Espinosa +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from openerp.tests.common import TransactionCase +from ..hooks import post_init_hook + + +class TestContractPaymentInit(TransactionCase): + # Use case : Prepare some data for current test case + def setUp(self): + super(TestContractPaymentInit, self).setUp() + self.payment_mode = self.env.ref('account_payment.payment_mode_1') + self.partner = self.env['res.partner'].create({ + 'name': 'Test contract partner', + 'customer_payment_mode': self.payment_mode.id, + }) + + def _contract_payment_mode_id(self, contract_id): + contract = self.env['account.analytic.account'].search([ + ('id', '=', contract_id), + ]) + return contract.payment_mode_id.id + + def test_post_init_hook(self): + contract = self.env['account.analytic.account'].create({ + 'name': 'Test contract', + 'type': 'contract', + 'partner_id': self.partner.id, + 'payment_mode_id': self.payment_mode.id, + }) + self.assertEqual(self._contract_payment_mode_id(contract.id), + self.payment_mode.id) + + contract.payment_mode_id = False + self.assertEqual(self._contract_payment_mode_id(contract.id), False) + + post_init_hook(self.cr, self.env) + self.assertEqual(self._contract_payment_mode_id(contract.id), + self.payment_mode.id)