From 89caeae2a83683d612a7423dc5b940967e6758b8 Mon Sep 17 00:00:00 2001 From: Samuel Date: Tue, 17 Dec 2024 13:04:23 +0100 Subject: [PATCH] [FIX] delivery_deliverea: improve code, refactor, change m2m to o2m delivera_parameters_ids --- .../migrations/16.0.1.0.2/post-migrate.py | 19 +++ .../models/carrier_deliverea_parameter.py | 1 + .../models/carrier_deliverea_service.py | 4 +- delivery_deliverea/models/delivery_carrier.py | 114 +++++++++++------- 4 files changed, 94 insertions(+), 44 deletions(-) create mode 100644 delivery_deliverea/migrations/16.0.1.0.2/post-migrate.py diff --git a/delivery_deliverea/migrations/16.0.1.0.2/post-migrate.py b/delivery_deliverea/migrations/16.0.1.0.2/post-migrate.py new file mode 100644 index 0000000000..d1eee16fd2 --- /dev/null +++ b/delivery_deliverea/migrations/16.0.1.0.2/post-migrate.py @@ -0,0 +1,19 @@ +def migrate(cr, version): + cr.execute( + """ + SELECT service_id, parameter_id + FROM carrier_deliverea_service_carrier_deliverea_parameter_rel + """ + ) + m2m_data = cr.fetchall() + + # Insertar los datos en la nueva estructura One2many + for service_id, parameter_id in m2m_data: + cr.execute( + """ + UPDATE carrier_deliverea_parameter + SET service_id = %s + WHERE id = %s + """, + (service_id, parameter_id), + ) diff --git a/delivery_deliverea/models/carrier_deliverea_parameter.py b/delivery_deliverea/models/carrier_deliverea_parameter.py index e779e7a408..11c0fbd1ec 100644 --- a/delivery_deliverea/models/carrier_deliverea_parameter.py +++ b/delivery_deliverea/models/carrier_deliverea_parameter.py @@ -16,3 +16,4 @@ class CarrierDeliveaParameter(models.Model): ("unsupported", "Unsupported"), ], ) + service_id = fields.Many2one("carrier.deliverea.service") diff --git a/delivery_deliverea/models/carrier_deliverea_service.py b/delivery_deliverea/models/carrier_deliverea_service.py index 6161aa4d6f..d97e51f774 100644 --- a/delivery_deliverea/models/carrier_deliverea_service.py +++ b/delivery_deliverea/models/carrier_deliverea_service.py @@ -8,7 +8,9 @@ class CarrierDelivereaService(models.Model): name = fields.Char() description = fields.Char() - deliverea_parameters = fields.Many2many(comodel_name="carrier.deliverea.parameter") + deliverea_parameters_ids = fields.One2many( + "carrier.deliverea.parameter", "service_id" + ) carrier_code = fields.Char() deliverea_distribution_center_id = fields.Many2one( comodel_name="deliverea.distribution.center", diff --git a/delivery_deliverea/models/delivery_carrier.py b/delivery_deliverea/models/delivery_carrier.py index 7a765d8daa..0829ac25a8 100644 --- a/delivery_deliverea/models/delivery_carrier.py +++ b/delivery_deliverea/models/delivery_carrier.py @@ -165,6 +165,49 @@ def deliverea_get_services_cron(self, extra_domain=None): self.env.cr.rollback() return True + def manage_deliverea_params(self, service_id, service_parameter): + for parameter in service_parameter.get("parameters", []): + parameter_name = parameter.get("name") + parameter_type = parameter.get("necessity").get("type") + parameter_id = self.env["carrier.deliverea.parameter"].search( + # change the search domain for manage type changes + [ + ("name", "=", parameter_name), + # ("type", "=", parameter_type), + ("service_id", "=", service_id.id), + ] + ) + if not parameter_id: + self.env["carrier.deliverea.parameter"].create( + { + "name": parameter_name, + "type": parameter_type, + "service_id": service_id.id, + } + ) + else: + if parameter_id.type != parameter_type: + parameter_id.write({"type": parameter_type}) + self.deliverea_check_parameters(parameter_id) + + def _create_service( + self, carrier_code, service_code, service_parameter, active_service + ): + return self.env["carrier.deliverea.service"].create( + { + "name": (carrier_code or "").upper() + + " " + + (service_parameter.get("name", "") or (service_code or "")), + "code": service_code, + "description": service_parameter.get("description") or "", + "carrier_code": carrier_code, + "deliverea_distribution_center_id": ( + self.deliverea_distribution_center_id.id + ), + "active": active_service, + } + ) + def deliverea_get_services(self): deliverea_request = DelivereaRequest(self) if not self.deliverea_distribution_center_id: @@ -173,16 +216,18 @@ def deliverea_get_services(self): self.deliverea_distribution_center_id.uuid ) for carrier in carriers.get("data"): + carrier_code = carrier.get("code") services = deliverea_request.get_carrier_detail( distribution_center_id=self.deliverea_distribution_center_id.uuid, - carrier_code=carrier.get("code"), + carrier_code=carrier_code, cost_center=carrier.get("costCenters")[0].get("code"), ) for service in services.get("services"): + service_code = service.get("code") service_id = self.env["carrier.deliverea.service"].search( [ - ("carrier_code", "=", carrier.get("code")), - ("code", "=", service.get("code")), + ("carrier_code", "=", carrier_code), + ("code", "=", service_code), ("active", "in", [True, False]), ] ) @@ -194,56 +239,22 @@ def deliverea_get_services(self): continue services_parameter = ( deliverea_request.get_carrier_services_integrations( - carrier.get("code"), services.get("integrationCode") + carrier_code, services.get("integrationCode") ) ) service_parameter = next( ( item for item in services_parameter.get("services") - if item.get("code") == service.get("code") + if item.get("code") == service_code ), {}, ) if not service_id: - service_id = self.env["carrier.deliverea.service"].create( - { - "name": carrier.get("code", "").upper() - + " " - + ( - service_parameter.get("name", "") - or service.get("code", "") - ), - "code": service.get("code"), - "description": service_parameter.get("description") or "", - "carrier_code": carrier.get("code"), - "deliverea_distribution_center_id": ( - self.deliverea_distribution_center_id.id - ), - "active": active_service, - } - ) - for parameter in service_parameter.get("parameters", []): - parameter_id = self.env["carrier.deliverea.parameter"].search( - [ - ("name", "=", parameter.get("name")), - ("type", "=", parameter.get("necessity").get("type")), - ] + self._create_service( + carrier_code, service_code, service_parameter, active_service ) - if not parameter_id: - parameter_id = self.env["carrier.deliverea.parameter"].create( - { - "name": parameter.get("name"), - "type": parameter.get("necessity").get("type"), - } - ) - if parameter_id.id not in service_id.deliverea_parameters.ids: - service_id.deliverea_parameters = [ - ( - 4, - parameter_id.id, - ) - ] + self.manage_deliverea_params(service_id, service_parameter) def _delete_empty_values(self, values): delete = [] @@ -358,7 +369,7 @@ def _get_service_attributes(self, carrier, service): "hideSender": carrier.deliverea_hide_sender, "insuranceValue": "0.0 EUR", } - for parameter in service.deliverea_parameters: + for parameter in service.deliverea_parameters_ids: if parameter.name in values.keys(): if parameter.type in ("ignored", "unsupported"): del values[parameter.name] @@ -538,3 +549,20 @@ def deliverea_tracking_state_update(self, picking): picking.date_delivered = datetime.strftime( datetime.now(), DEFAULT_SERVER_DATETIME_FORMAT ) + + def deliverea_check_parameters(self, parameter): + # this function is for check the parameters and auto check the checkbox + parameters_key = { + "notificationViaSMS": "deliverea_notifications_sms", + "notificationViaEmail": "deliverea_notifications_email", + "saturdayDelivery": "deliverea_saturday_delivery", + "hideSender": "deliverea_hide_sender", + "returnLabel": "deliverea_return_label", + "returnProofOfDelivery": "deliverea_return_proof_delivery", + } + param = parameters_key.get(parameter.name) + if param: + if parameter.type == "unsupported": + self[param] = False + elif parameter.type == "required": + self[param] = True