Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][FIX] delivery_deliverea: improve code, refactor, change m2m to o2m d… #919

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions delivery_deliverea/migrations/16.0.1.0.2/post-migrate.py
Original file line number Diff line number Diff line change
@@ -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),
)
1 change: 1 addition & 0 deletions delivery_deliverea/models/carrier_deliverea_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ class CarrierDeliveaParameter(models.Model):
("unsupported", "Unsupported"),
],
)
service_id = fields.Many2one("carrier.deliverea.service")
4 changes: 3 additions & 1 deletion delivery_deliverea/models/carrier_deliverea_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
114 changes: 71 additions & 43 deletions delivery_deliverea/models/delivery_carrier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]),
]
)
Expand All @@ -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 = []
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
Loading