<commit_before># Copyright 2021 Studio73 - <NAME> <<EMAIL>>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import base64

from odoo import _, fields, models
from odoo.tools import float_compare

from .dhl_parcel_request import (
    DHL_PARCEL_DELIVERY_STATES_STATIC,
    DHL_PARCEL_INCOTERMS_STATIC,
    DhlParcelRequest,
)


class DeliveryCarrier(models.Model):
    _inherit = "delivery.carrier"

    def _compute_can_generate_return(self):
        super()._compute_can_generate_return()
        for carrier in self:
            if carrier.delivery_type == "dhl_parcel":
                carrier.can_generate_return = True

    delivery_type = fields.Selection(
        selection_add=[("dhl_parcel", "DHL Parcel")],
        ondelete={"dhl_parcel": "set default"},
    )
    dhl_parcel_customer_code = fields.Char(string="DHL Parcel customer code")
    dhl_parcel_incoterm = fields.Selection(
        string="DHL Parcel incoterms", selection=DHL_PARCEL_INCOTERMS_STATIC
    )
    dhl_parcel_product = fields.Selection(
        string="DHL Product",
        selection=[("B2B", "B2B Product"), ("B2C", "B2C Product")],
        help="If the product is not specified, it is considered B2B",
    )
    dhl_parcel_uid = fields.Char(string="DHL Parcel UID")
    dhl_parcel_password = fields.Char(string="DHL Parcel Password")
    dhl_parcel_last_end_day_report = fields.Binary(
        string="DHL Parcel last manual end day report"
    )
    dhl_parcel_last_end_day_report_name = fields.Char(string="Filename")
    dhl_parcel_label_format = fields.Selection(
        selection=[("PDF", "PDF"), ("ZPL", "ZPL"), ("EPL", "EPL")],
        default="PDF",
        string="Label format",
    )

    def dhl_parcel_get_tracking_link(self, picking):
        """Provide tracking link for the customer"""
        tracking_url = (
            "https://clientesparcel.dhl.es/seguimientoenvios/integra/"
            "SeguimientoDocumentos.aspx?codigo={}&anno={}&lang=sp"
        )
        return tracking_url.format(
            picking.carrier_tracking_ref, fields.Date.today().year
        )

    def _get_dhl_parcel_receiver_info(self, picking):
        partner = picking.partner_id
        return {
            "Name": partner.name or partner.parent_id.name or "",
            "Address": partner.street or "",
            "City": partner.city or "",
            "PostalCode": partner.zip or "",
            "Country": partner.country_id.code or "",
            "Phone": partner.phone or "",
            "Email": partner.email or "",
        }

    def _get_dhl_parcel_sender_info(self, picking):
        """Optional, if the sender information is not
        sent, they will be fetched from DHL web Service B2B.
        """
        partner = picking.picking_type_id.warehouse_id.partner_id
        return {
            "Name": partner.name or partner.parent_id.name or "",
            "Address": partner.street or "",
            "City": partner.city or "",
            "PostalCode": partner.zip or "",
            "Country": partner.country_id.code or "",
            "Phone": partner.phone or "",
            "Email": partner.email or "",
        }

    def _get_dhl_parcel_product(self, picking):
        product = self.dhl_parcel_product
        if product == "B2C" and picking.is_return_picking:
            product = "R2C"
        return product or "B2B"

    def _prepare_dhl_parcel_shipping(self, picking):
        """Convert picking values for dhl parcel api
        :param picking record with picking to send
        :returns dict values for the connector
        """
        self.ensure_one()
        # El peso del envío tiene que ser como mínimo 1 kilo o como máximo 99999 kilos
        weight = picking.shipping_weight
        if float_compare(weight, 1, precision_digits=3) == -1:
            weight = 1
        return {
            "Customer": self.dhl_parcel_customer_code,
            "Receiver": self._get_dhl_parcel_receiver_info(picking),
            "Sender": self._get_dhl_parcel_sender_info(picking),  # [optional]<commit_msg>[FIX] delivery_dhl_parcel: Round the weight of the shipment to avoid errors at the end of the day. TT37136<commit_after># Copyright 2021 Studio73 - <NAME> <<EMAIL>>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import base64

from odoo import _, fields, models
from odoo.tools import float_compare

from .dhl_parcel_request import (
    DHL_PARCEL_DELIVERY_STATES_STATIC,
    DHL_PARCEL_INCOTERMS_STATIC,
    DhlParcelRequest,
)


class DeliveryCarrier(models.Model):
    _inherit = "delivery.carrier"

    def _compute_can_generate_return(self):
        super()._compute_can_generate_return()
        for carrier in self:
            if carrier.delivery_type == "dhl_parcel":
                carrier.can_generate_return = True

    delivery_type = fields.Selection(
        selection_add=[("dhl_parcel", "DHL Parcel")],
        ondelete={"dhl_parcel": "set default"},
    )
    dhl_parcel_customer_code = fields.Char(string="DHL Parcel customer code")
    dhl_parcel_incoterm = fields.Selection(
        string="DHL Parcel incoterms", selection=DHL_PARCEL_INCOTERMS_STATIC
    )
    dhl_parcel_product = fields.Selection(
        string="DHL Product",
        selection=[("B2B", "B2B Product"), ("B2C", "B2C Product")],
        help="If the product is not specified, it is considered B2B",
    )
    dhl_parcel_uid = fields.Char(string="DHL Parcel UID")
    dhl_parcel_password = fields.Char(string="DHL Parcel Password")
    dhl_parcel_last_end_day_report = fields.Binary(
        string="DHL Parcel last manual end day report"
    )
    dhl_parcel_last_end_day_report_name = fields.Char(string="Filename")
    dhl_parcel_label_format = fields.Selection(
        selection=[("PDF", "PDF"), ("ZPL", "ZPL"), ("EPL", "EPL")],
        default="PDF",
        string="Label format",
    )

    def dhl_parcel_get_tracking_link(self, picking):
        """Provide tracking link for the customer"""
        tracking_url = (
            "https://clientesparcel.dhl.es/seguimientoenvios/integra/"
            "SeguimientoDocumentos.aspx?codigo={}&anno={}&lang=sp"
        )
        return tracking_url.format(
            picking.carrier_tracking_ref, fields.Date.today().year
        )

    def _get_dhl_parcel_receiver_info(self, picking):
        partner = picking.partner_id
        return {
            "Name": partner.name or partner.parent_id.name or "",
            "Address": partner.street or "",
            "City": partner.city or "",
            "PostalCode": partner.zip or "",
            "Country": partner.country_id.code or "",
            "Phone": partner.phone or "",
            "Email": partner.email or "",
        }

    def _get_dhl_parcel_sender_info(self, picking):
        """Optional, if the sender information is not
        sent, they will be fetched from DHL web Service B2B.
        """
        partner = picking.picking_type_id.warehouse_id.partner_id
        return {
            "Name": partner.name or partner.parent_id.name or "",
            "Address": partner.street or "",
            "City": partner.city or "",
            "PostalCode": partner.zip or "",
            "Country": partner.country_id.code or "",
            "Phone": partner.phone or "",
            "Email": partner.email or "",
        }

    def _get_dhl_parcel_product(self, picking):
        product = self.dhl_parcel_product
        if product == "B2C" and picking.is_return_picking:
            product = "R2C"
        return product or "B2B"

    def _prepare_dhl_parcel_shipping(self, picking):
        """Convert picking values for dhl parcel api
        :param picking record with picking to send
        :returns dict values for the connector
        """
        self.ensure_one()
        # El peso debe tener 2 decimales para evitar errores en el cierre del día
        weight = round(picking.shipping_weight, 2)
        # El peso del envío tiene que ser como mínimo 1 kilo o como máximo 99999 kilos
        if float_compare(weight, 1, precision_digits=2) == -1:
            weight = 1
        return {
            "Customer": self.dhl_parcel_customer_code,
            "Receiver": self._get_dhl_parcel_receiver_info(picking),
            "Sender": self._get_dhl_parcel_sender_info(picking),  # [optional]
{'fraction_non_alphanumeric': 0.1076904503199324, 'fraction_numerical': 0.007847398285645298, 'mean_word_length': 2.6638655462184873, 'pattern_counts': {'":': 38, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4255417', 'n_tokens_mistral': 2620, 'n_tokens_neox': 2543, 'n_words': 585}