From 31528dbdc6f90834205c929d59005aab8e284eab Mon Sep 17 00:00:00 2001 From: Marc Durepos Date: Mon, 26 Jun 2023 11:41:07 -0400 Subject: [PATCH] bemade_fsm: Site contacts and work order contacts added and unit tested. Need to do the UI and integration testing next. Re #37. --- __manifest__.py | 1 + models/equipment.py | 12 +++-- models/res_partner.py | 19 ++++++-- models/sale_order.py | 27 +++++++++++ models/task_template.py | 2 +- tests/__init__.py | 1 + tests/test_bemade_fsm_common.py | 4 +- tests/test_equipment.py | 2 + tests/test_fsm_contact_setting.py | 66 ++++++++++++++++++-------- tests/test_task_template.py | 1 + tests/test_task_template_sale_order.py | 1 + 11 files changed, 108 insertions(+), 28 deletions(-) diff --git a/__manifest__.py b/__manifest__.py index 0a0e459..bbc2493 100644 --- a/__manifest__.py +++ b/__manifest__.py @@ -35,6 +35,7 @@ 'industry_fsm', 'industry_fsm_sale', 'bemade_partner_root_ancestor', + 'mail', ], 'data': ['views/task_template_views.xml', 'views/equipment.xml', diff --git a/models/equipment.py b/models/equipment.py index b82c143..6ea99d6 100644 --- a/models/equipment.py +++ b/models/equipment.py @@ -20,6 +20,7 @@ class EquipmentType(models.Model): name = fields.Char(string='Intervention Name', required=True, translate=True) + class Equipment(models.Model): _name = 'bemade_fsm.equipment' _rec_name = 'complete_name' @@ -39,13 +40,14 @@ class Equipment(models.Model): "Waste water, Pure water") partner_id = fields.Many2one('res.partner', string="Owner", - compute="_compute_partner") + compute="_compute_partner", + search="_search_partner", ) description = fields.Text(string="Description", tracking=True) partner_location_id = fields.Many2one('res.partner', string="Physical Address", - tracking=True,) + tracking=True, ) location_notes = fields.Text(string="Physical Location Notes", tracking=True) @@ -58,6 +60,10 @@ class Equipment(models.Model): for rec in self: rec.partner_id = rec.partner_location_id and rec.partner_location_id.root_ancestor + @api.model + def _search_partner(self, operator, value): + return [('partner_location_id.root_ancestor', operator, value)] + @api.depends('pid_tag', 'name') def _compute_complete_name(self): for rec in self: @@ -87,4 +93,4 @@ class Equipment(models.Model): 'context': self.env.context, 'res_model': 'bemade_fsm.equipment', 'type': 'ir.actions.act_window', - } \ No newline at end of file + } diff --git a/models/res_partner.py b/models/res_partner.py index c9f1877..f5dba22 100644 --- a/models/res_partner.py +++ b/models/res_partner.py @@ -16,7 +16,8 @@ class Partner(models.Model): string='Site Equipment') is_site_contact = fields.Boolean(string='Is a site contact', - compute="_compute_is_site_contact") + compute="_compute_is_site_contact", + search="_search_is_site_contact",) site_ids = fields.Many2many(string='Work Sites', comodel_name='res.partner', @@ -33,6 +34,14 @@ class Partner(models.Model): domain=[('is_company', '=', False)], tracking=True) + work_order_contacts = fields.Many2many(string='Work Order Recipients', + comodel_name='res.partner', + relation='res_partner_work_order_contacts_rel', + column1='res_partner_id', + column2='work_order_contact_id', + domain=[('is_company', '=', False)], + tracking=True) + @api.depends('site_ids') def _compute_is_site_contact(self): for rec in self: @@ -41,5 +50,9 @@ class Partner(models.Model): @api.depends('equipment_ids') def _compute_equipment_count(self): for rec in self: - all_equipmemt_ids = self.env['bemade_fsm.equipment'].search([('partner_id', '=', rec.id)]) - rec.equipment_count = len(all_equipmemt_ids) + all_equipment_ids = self.env['bemade_fsm.equipment'].search([('partner_id', '=', rec.id)]) + rec.equipment_count = len(all_equipment_ids) + + @api.model + def _search_is_site_contact(self, operator, value): + return [('site_contacts', '!=', False)] diff --git a/models/sale_order.py b/models/sale_order.py index 0605e28..10b166b 100644 --- a/models/sale_order.py +++ b/models/sale_order.py @@ -1,6 +1,33 @@ from odoo import fields, models, api, _, Command +class SaleOrder(models.Model): + _inherit = 'sale.order' + + site_contacts = fields.Many2many(comodel_name='res.partner', + relation="sale_order_site_contacts_rel", + compute="_compute_default_contacts", + inverse="_inverse_default_contacts", + string='Site Contacts', + store=True) + + work_order_contacts = fields.Many2many(comodel_name='res.partner', + relation='sale_order_work_order_contacts_rel', + compute='_compute_default_contacts', + inverse='_inverse_default_contacts', + string='Work Order Recipients', + store=True) + + @api.depends('partner_id') + def _compute_default_contacts(self): + for rec in self: + rec.site_contacts = rec.partner_id.site_contacts + rec.work_order_contacts = rec.partner_id.work_order_contacts + + def _inverse_default_contacts(self): + pass + + class SaleOrderLine(models.Model): _inherit = 'sale.order.line' diff --git a/models/task_template.py b/models/task_template.py index f95b3c7..3f70518 100644 --- a/models/task_template.py +++ b/models/task_template.py @@ -3,7 +3,7 @@ from odoo import models, fields, api, _ class TaskTemplate(models.Model): _name = 'project.task.template' - + _description = "Template for new project tasks" @api.model def _current_company(self): return self.env.company diff --git a/tests/__init__.py b/tests/__init__.py index a66b934..fc3738e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -2,3 +2,4 @@ from . import test_bemade_fsm_common from . import test_task_template from . import test_task_template_sale_order from . import test_equipment +from . import test_fsm_contact_setting diff --git a/tests/test_bemade_fsm_common.py b/tests/test_bemade_fsm_common.py index 47942ac..4fcfe35 100644 --- a/tests/test_bemade_fsm_common.py +++ b/tests/test_bemade_fsm_common.py @@ -1,6 +1,8 @@ -from odoo.tests.common import TransactionCase +from odoo.tests.common import TransactionCase, tagged from odoo import Command + +@tagged("-at_install", "post_install") class FSMManagerUserTransactionCase(TransactionCase): @classmethod diff --git a/tests/test_equipment.py b/tests/test_equipment.py index 2ca0485..197938c 100644 --- a/tests/test_equipment.py +++ b/tests/test_equipment.py @@ -1,6 +1,8 @@ from odoo.tests.common import HttpCase, tagged from .test_bemade_fsm_common import FSMManagerUserTransactionCase + +@tagged("-at_install", "post_install") class TestEquipmentCommon(FSMManagerUserTransactionCase): @classmethod diff --git a/tests/test_fsm_contact_setting.py b/tests/test_fsm_contact_setting.py index a8874c2..7d32ecb 100644 --- a/tests/test_fsm_contact_setting.py +++ b/tests/test_fsm_contact_setting.py @@ -1,30 +1,56 @@ from odoo.tests import TransactionCase, HttpCase, tagged from odoo import Command -from test_bemade_fsm_common import FSMManagerUserTransactionCase +from .test_bemade_fsm_common import FSMManagerUserTransactionCase + +@tagged("-at_install", "post_install") class SaleOrderFSMContactsCase(FSMManagerUserTransactionCase): - def test_default_site_contacts(self): - # Make sure the SO pulls the defaults from the partner correctly - pass + @classmethod + def setUpClass(cls): + super().setUpClass() + Partner = cls.env['res.partner'] + cls.parent_co = Partner.create({ + 'name': 'Parent Co', + 'company_type': 'company', + }) + cls.contact_1 = Partner.create({ + 'name': 'Contact 1', + 'company_type': 'person', + 'parent_id': cls.parent_co.id, + }) + cls.contact_2 = Partner.create({ + 'name': 'Contact 2', + 'company_type': 'person', + 'parent_id': cls.parent_co.id, + }) - def test_default_billing_contacts(self): + def test_site_contacts(self): # Make sure the SO pulls the defaults from the partner correctly - pass + self.parent_co.write({'site_contacts': [Command.set([self.contact_1.id, self.contact_2.id])]}) + so = self.env['sale.order'].create({ + 'partner_id': self.parent_co.id, + }) + self.assertTrue(so.site_contacts == self.parent_co.site_contacts) + # Make sure updating the site contacts on the SO doesn't feed back to the partner + so.write({'site_contacts': [Command.set([self.contact_1.id])]}) + self.assertTrue(self.contact_1 in so.site_contacts) + self.assertTrue(self.contact_2 not in so.site_contacts) + self.assertTrue(so.site_contacts != so.partner_id.site_contacts and len(so.partner_id.site_contacts) == 2) def test_default_workorder_contacts(self): # Make sure the SO pulls the defaults from the partner correctly - pass - - def test_change_site_contacts(self): - # Make sure the SO contacts can be updated without feeding back to the partner - pass - - def test_change_workorder_contacts(self): - # Make sure the SO contacts can be updated without feeding back to the partner - pass - - def test_change_billing_contacts(self): - # Make sure the SO contacts can be updated without feedin back to the partner - pass - + self.parent_co.write({'work_order_contacts': [Command.set([self.contact_1.id, self.contact_2.id])]}) + so = self.env['sale.order'].create({ + 'partner_id': self.parent_co.id, + }) + self.assertTrue(self.contact_1 in self.parent_co.work_order_contacts) + self.assertTrue(self.contact_2 in self.parent_co.work_order_contacts) + self.assertTrue(self.contact_1 in so.work_order_contacts) + self.assertTrue(self.contact_2 in so.work_order_contacts) + # Make sure setting the work order contacts on the SO doesn't feed back to the partner + so.write({'work_order_contacts': [Command.set([self.contact_1.id])]}) + self.assertTrue(self.contact_1 in so.work_order_contacts) + self.assertTrue(self.contact_2 not in so.work_order_contacts) + self.assertTrue( + so.work_order_contacts != so.partner_id.work_order_contacts and len(so.partner_id.work_order_contacts) == 2) diff --git a/tests/test_task_template.py b/tests/test_task_template.py index bd46e0a..c0bce51 100644 --- a/tests/test_task_template.py +++ b/tests/test_task_template.py @@ -5,6 +5,7 @@ from odoo import Command from psycopg2.errors import ForeignKeyViolation +@tagged("-at_install", "post_install") class TestTaskTemplateCommon(FSMManagerUserTransactionCase): @classmethod diff --git a/tests/test_task_template_sale_order.py b/tests/test_task_template_sale_order.py index 176c222..e2fe05e 100644 --- a/tests/test_task_template_sale_order.py +++ b/tests/test_task_template_sale_order.py @@ -2,6 +2,7 @@ from .test_task_template import TestTaskTemplateCommon from odoo.tests.common import tagged +@tagged("-at_install", "post_install") class TestTaskTemplateSalesOrder(TestTaskTemplateCommon): @classmethod def setUpClass(cls):