diff --git a/odoo/addons/dsrpt_address_book/models/__init__.py b/odoo/addons/dsrpt_address_book/models/__init__.py index dd7f1ad..acdc756 100644 --- a/odoo/addons/dsrpt_address_book/models/__init__.py +++ b/odoo/addons/dsrpt_address_book/models/__init__.py @@ -2,6 +2,7 @@ from . import dsrpt_communication_type from . import dsrpt_contact +from . import dsrpt_contact_address from . import dsrpt_contact_communication from . import contact_event from . import contact_source diff --git a/odoo/addons/dsrpt_address_book/models/dsrpt_contact.py b/odoo/addons/dsrpt_address_book/models/dsrpt_contact.py index 823e1e6..f7e08ac 100644 --- a/odoo/addons/dsrpt_address_book/models/dsrpt_contact.py +++ b/odoo/addons/dsrpt_address_book/models/dsrpt_contact.py @@ -38,6 +38,11 @@ class Contact(models.Model): 'contact_id', string='Events' ) + address_ids = fields.One2many( + 'dsrpt.contact.address', + 'contact_id', + string='Addresses' + ) # call_ids moved to dsrpt_calls module to avoid circular dependencies # Computed fields diff --git a/odoo/addons/dsrpt_address_book/models/dsrpt_contact_address.py b/odoo/addons/dsrpt_address_book/models/dsrpt_contact_address.py new file mode 100644 index 0000000..574969b --- /dev/null +++ b/odoo/addons/dsrpt_address_book/models/dsrpt_contact_address.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- + +from odoo import fields, models + + +class ContactAddress(models.Model): + _name = "dsrpt.contact.address" + _description = "Contact Address" + _order = "id desc" + _rec_name = "description" + + contact_id = fields.Many2one("dsrpt.contact", required=True, ondelete="cascade", index=True) + description = fields.Char(required=True) + latitude = fields.Float(digits=(10, 6)) + longitude = fields.Float(digits=(10, 6)) diff --git a/odoo/addons/dsrpt_address_book/security/ir.model.access.csv b/odoo/addons/dsrpt_address_book/security/ir.model.access.csv index 45dba25..5cf8188 100644 --- a/odoo/addons/dsrpt_address_book/security/ir.model.access.csv +++ b/odoo/addons/dsrpt_address_book/security/ir.model.access.csv @@ -5,6 +5,8 @@ access_dsrpt_communication_type_user,dsrpt.communication.type.user,model_dsrpt_c access_dsrpt_communication_type_admin,dsrpt.communication.type.admin,model_dsrpt_communication_type,group_dsrpt_address_book_admin,1,1,1,1 access_dsrpt_contact_communication_user,dsrpt.contact.communication.user,model_dsrpt_contact_communication,group_dsrpt_address_book_user,1,1,1,1 access_dsrpt_contact_communication_admin,dsrpt.contact.communication.admin,model_dsrpt_contact_communication,group_dsrpt_address_book_admin,1,1,1,1 +access_dsrpt_contact_address_user,dsrpt.contact.address.user,model_dsrpt_contact_address,group_dsrpt_address_book_user,1,1,1,1 +access_dsrpt_contact_address_admin,dsrpt.contact.address.admin,model_dsrpt_contact_address,group_dsrpt_address_book_admin,1,1,1,1 access_contact_event_user,contact.event.user,model_contact_event,group_dsrpt_address_book_user,1,1,1,0 access_contact_event_admin,contact.event.admin,model_contact_event,group_dsrpt_address_book_admin,1,1,1,1 access_contact_source_user,contact.source.user,model_contact_source,group_dsrpt_address_book_user,1,1,1,0 diff --git a/odoo/addons/dsrpt_address_book/views/dsrpt_contact_views.xml b/odoo/addons/dsrpt_address_book/views/dsrpt_contact_views.xml index 2e5ab26..5fbb4e9 100644 --- a/odoo/addons/dsrpt_address_book/views/dsrpt_contact_views.xml +++ b/odoo/addons/dsrpt_address_book/views/dsrpt_contact_views.xml @@ -96,7 +96,18 @@ - + + + + + + + + + + + + @@ -107,7 +118,7 @@ - + diff --git a/odoo/addons/dsrpt_repair_work_orders/models/work_order.py b/odoo/addons/dsrpt_repair_work_orders/models/work_order.py index 06ef749..95b1a1d 100644 --- a/odoo/addons/dsrpt_repair_work_orders/models/work_order.py +++ b/odoo/addons/dsrpt_repair_work_orders/models/work_order.py @@ -10,10 +10,20 @@ class RepairWorkOrder(models.Model): name = fields.Char(default="New", copy=False, readonly=True, tracking=True) contact_id = fields.Many2one("dsrpt.contact", required=True, tracking=True) - service_address = fields.Char(tracking=True) - service_latitude = fields.Float(digits=(10, 6), tracking=True) - service_longitude = fields.Float(digits=(10, 6), tracking=True) - zone_id = fields.Many2one("repair.fsm.zone", string="FSM Zone", tracking=True) + contact_address_id = fields.Many2one( + "dsrpt.contact.address", + string="Address", + tracking=True, + domain="[('contact_id', '=', contact_id)]", + ) + zone_id = fields.Many2one( + "repair.fsm.zone", + string="FSM Zone", + compute="_compute_zone_id", + store=True, + readonly=True, + tracking=True, + ) description = fields.Text(tracking=True) requested_datetime = fields.Datetime(default=fields.Datetime.now, tracking=True) scheduled_datetime = fields.Datetime(tracking=True) @@ -42,15 +52,7 @@ class RepairWorkOrder(models.Model): for vals in vals_list: if vals.get("name", "New") == "New": vals["name"] = self.env["ir.sequence"].next_by_code("repair.work.order") or "New" - records = super().create(vals_list) - for record, vals in zip(records, vals_list): - if vals.get("zone_id"): - continue - if record._has_service_point(): - zone = record._find_zone_for_point(record.service_latitude, record.service_longitude) - if zone and record.zone_id != zone: - record.zone_id = zone.id - return records + return super().create(vals_list) def _group_expand_states(self, states, domain, order): return [key for key, _label in self._fields["state"].selection] @@ -61,44 +63,30 @@ class RepairWorkOrder(models.Model): rec.total_time_hours = sum(rec.time_line_ids.mapped("hours")) rec.total_material_cost = sum(rec.material_line_ids.mapped("subtotal")) - def _has_service_point(self): - self.ensure_one() - return self.service_latitude is not False and self.service_longitude is not False - def _find_zone_for_point(self, latitude, longitude): - zones = self.env["repair.fsm.zone"].search([("state", "=", "active")]) + if latitude is False or longitude is False or latitude is None or longitude is None: + return self.env["repair.fsm.zone"] + zone_domain = [("state", "=", "active")] if "state" in self.env["repair.fsm.zone"]._fields else [] + zones = self.env["repair.fsm.zone"].search(zone_domain) for zone in zones: if zone.contains_point(latitude, longitude): return zone return self.env["repair.fsm.zone"] - @api.onchange("service_latitude", "service_longitude") - def _onchange_service_coordinates(self): + @api.depends("contact_address_id.latitude", "contact_address_id.longitude") + def _compute_zone_id(self): for rec in self: - if rec._has_service_point(): - zone = rec._find_zone_for_point(rec.service_latitude, rec.service_longitude) - if zone: - rec.zone_id = zone + if not rec.contact_address_id: + rec.zone_id = False + continue + zone = rec._find_zone_for_point(rec.contact_address_id.latitude, rec.contact_address_id.longitude) + rec.zone_id = zone.id if zone else False - def action_detect_zone(self): + @api.onchange("contact_id") + def _onchange_contact_id(self): for rec in self: - if not rec._has_service_point(): - raise ValidationError("Service coordinates are required to detect FSM Zone.") - zone = rec._find_zone_for_point(rec.service_latitude, rec.service_longitude) - if not zone: - raise ValidationError("No active FSM Zone contains this point.") - rec.zone_id = zone.id - return True - - def write(self, vals): - result = super().write(vals) - if ("service_latitude" in vals or "service_longitude" in vals) and "zone_id" not in vals: - for rec in self: - if rec._has_service_point(): - zone = rec._find_zone_for_point(rec.service_latitude, rec.service_longitude) - if zone and rec.zone_id != zone: - super(RepairWorkOrder, rec).write({"zone_id": zone.id}) - return result + if rec.contact_address_id and rec.contact_address_id.contact_id != rec.contact_id: + rec.contact_address_id = False def action_confirm(self): self.write({"state": "confirmed"}) diff --git a/odoo/addons/dsrpt_repair_work_orders/views/repair_work_order_view_form.xml b/odoo/addons/dsrpt_repair_work_orders/views/repair_work_order_view_form.xml index 349db80..788a359 100644 --- a/odoo/addons/dsrpt_repair_work_orders/views/repair_work_order_view_form.xml +++ b/odoo/addons/dsrpt_repair_work_orders/views/repair_work_order_view_form.xml @@ -8,7 +8,6 @@