diff --git a/contract/__openerp__.py b/contract/__openerp__.py index ec8eb58a..dc59a8ef 100644 --- a/contract/__openerp__.py +++ b/contract/__openerp__.py @@ -6,7 +6,7 @@ { 'name': 'Contracts Management recurring', - 'version': '9.0.1.2.1', + 'version': '9.0.1.3.0', 'category': 'Contract Management', 'license': 'AGPL-3', 'author': "OpenERP SA," @@ -22,6 +22,7 @@ 'data/contract_template.xml', 'views/contract.xml', 'views/account_invoice_view.xml', + 'views/res_partner_view.xml', ], 'installable': True, } diff --git a/contract/models/__init__.py b/contract/models/__init__.py index 8deef410..229ebac5 100644 --- a/contract/models/__init__.py +++ b/contract/models/__init__.py @@ -4,3 +4,4 @@ from . import contract from . import invoice +from . import res_partner diff --git a/contract/models/res_partner.py b/contract/models/res_partner.py new file mode 100644 index 00000000..c5dee078 --- /dev/null +++ b/contract/models/res_partner.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Carlos Dauden +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from openerp import api, fields, models + + +class ResPartner(models.Model): + _inherit = 'res.partner' + + contract_count = fields.Integer( + compute='_compute_contract_count', + string='# of Contracts', + ) + contract_ids = fields.One2many( + comodel_name='account.analytic.account', + inverse_name='partner_id', + string='Contracts', + ) + + @api.multi + @api.depends('contract_ids') + def _compute_contract_count(self): + contract_data = self.env['account.analytic.account'].read_group( + domain=[('partner_id', 'child_of', self.ids), + ('recurring_invoices', '=', True)], + fields=['partner_id'], + groupby=['partner_id']) + # read to keep the child/parent relation while aggregating the + # read_group result in the loop + partner_child_ids = self.read(['child_ids']) + mapped_data = dict([ + (m['partner_id'][0], m['partner_id_count']) for m in contract_data + ]) + for partner in self: + # let's obtain the partner id and all its child ids from the read + # up there + partner_ids = filter( + lambda r: r['id'] == partner.id, partner_child_ids)[0] + partner_ids = ([partner_ids.get('id')] + + partner_ids.get('child_ids')) + # then we can sum for all the partner's child + partner.contract_count = sum( + mapped_data.get(child, 0) for child in partner_ids) diff --git a/contract/tests/test_contract.py b/contract/tests/test_contract.py index f2710433..7d49cf5a 100644 --- a/contract/tests/test_contract.py +++ b/contract/tests/test_contract.py @@ -11,7 +11,10 @@ class TestContract(SavepointCase): @classmethod def setUpClass(cls): super(TestContract, cls).setUpClass() - cls.partner = cls.env.ref('base.res_partner_2') + cls.partner = cls.env['res.partner'].create({ + 'name': 'Partner test', + 'customer': True, + }) cls.product = cls.env.ref('product.product_product_2') cls.product.description_sale = 'Test description sale' cls.contract = cls.env['account.analytic.account'].create({ @@ -42,10 +45,13 @@ class TestContract(SavepointCase): self.assertIn('uom_id', res['domain']) self.contract_line.price_unit = 100.0 + self.assertEqual(self.partner.contract_count, 1) self.contract.partner_id = False with self.assertRaises(ValidationError): self.contract.recurring_create_invoice() + self.assertEqual(self.partner.contract_count, 0) self.contract.partner_id = self.partner.id + self.assertEqual(self.partner.contract_count, 1) new_invoice = self.contract.recurring_create_invoice() self.assertTrue(new_invoice) diff --git a/contract/views/contract.xml b/contract/views/contract.xml index 50189f70..6d62f45c 100644 --- a/contract/views/contract.xml +++ b/contract/views/contract.xml @@ -3,14 +3,16 @@ - {'search_default_contract_id': - [active_id], - 'default_contract_id': active_id} - Invoices account.invoice - - + + { + 'search_default_contract_id': [active_id], + 'default_contract_id': active_id} + @@ -90,6 +92,10 @@ account.analytic.account + + + diff --git a/contract/views/res_partner_view.xml b/contract/views/res_partner_view.xml new file mode 100644 index 00000000..3b2d92bc --- /dev/null +++ b/contract/views/res_partner_view.xml @@ -0,0 +1,31 @@ + + + + + Contracts + account.analytic.account + form + tree,form + { + 'search_default_partner_id': active_id, + 'search_default_recurring_invoices': 1} + + + + + res.partner.view.contract + res.partner + + + +
+ +
+
+
+ +