[IMP] delivery_purolator: add service types and decouple api from delivery.carrier

H10820
This commit is contained in:
Cedric Collins
2022-08-31 17:37:10 -05:00
committed by Jared Kipe
parent a4626f2ef0
commit b5c6f89970
2 changed files with 97 additions and 20 deletions

View File

@@ -2,15 +2,82 @@ from odoo import fields, models, _
from .purolator_services import PurolatorClient from .purolator_services import PurolatorClient
PUROLATOR_SERVICES = [
('PurolatorExpress9AM', 'Purolator Express 9AM'),
('PurolatorExpress10:30AM', 'Purolator Express 10:30AM'),
('PurolatorExpress12PM', 'Purolator Express 12PM'),
('PurolatorExpress', 'Purolator Express'),
('PurolatorExpressEvening', 'Purolator Express Evening'),
('PurolatorExpressEnvelope9AM', 'Purolator Express Envelope 9AM'),
('PurolatorExpressEnvelope10:30AM', 'Purolator Express Envelope 10:30AM'),
('PurolatorExpressEnvelope12PM', 'Purolator Express Envelope 12PM'),
('PurolatorExpressEnvelope', 'Purolator Express Envelope'),
('PurolatorExpressEnvelopeEvening', 'Purolator Express Envelope Evening'),
('PurolatorExpressPack9AM', 'Purolator Express Pack 9AM'),
('PurolatorExpressPack10:30AM', 'Purolator Express Pack 10:30AM'),
('PurolatorExpressPack12PM', 'Purolator Express Pack 12PM'),
('PurolatorExpressPack', 'Purolator Express Pack'),
('PurolatorExpressPackEvening', 'Purolator Express Pack Evening'),
('PurolatorExpressBox9AM', 'Purolator Express Box 9AM'),
('PurolatorExpressBox10:30AM', 'Purolator Express Box 10:30AM'),
('PurolatorExpressBox12PM', 'Purolator Express Box 12PM'),
('PurolatorExpressBox', 'Purolator Express Box'),
('PurolatorExpressBoxEvening', 'Purolator Express Box Evening'),
('PurolatorGround', 'Purolator Ground'),
('PurolatorGround9AM', 'Purolator Ground 9AM'),
('PurolatorGround10:30AM', 'Purolator Ground 10:30AM'),
('PurolatorGroundEvening', 'Purolator Ground Evening'),
('PurolatorQuickShip', 'Purolator Quick Ship'),
('PurolatorQuickShipEnvelope', 'Purolator Quick Ship Envelope'),
('PurolatorQuickShipPack', 'Purolator Quick Ship Pack'),
('PurolatorQuickShipBox', 'Purolator Quick Ship Box'),
('PurolatorExpressU.S.', 'Purolator Express U.S.'),
('PurolatorExpressU.S.9AM', 'Purolator Express U.S. 9AM'),
('PurolatorExpressU.S.10:30AM', 'Purolator Express U.S. 10:30AM'),
('PurolatorExpressU.S.12:00', 'Purolator Express U.S. 12:00'),
('PurolatorExpressEnvelopeU.S.', 'Purolator Express Envelope U.S.'),
('PurolatorExpressU.S.Envelope9AM', 'Purolator Express U.S. Envelope 9AM'),
('PurolatorExpressU.S.Envelope10:30AM', 'Purolator Express U.S. Envelope 10:30AM'),
('PurolatorExpressU.S.Envelope12:00', 'Purolator Express U.S. Envelope 12:00'),
('PurolatorExpressPackU.S.', 'Purolator Express Pack U.S.'),
('PurolatorExpressU.S.Pack9AM', 'Purolator Express U.S. Pack 9AM'),
('PurolatorExpressU.S.Pack10:30AM', 'Purolator Express U.S. Pack 10:30AM'),
('PurolatorExpressU.S.Pack12:00', 'Purolator Express U.S. Pack 12:00'),
('PurolatorExpressBoxU.S.', 'Purolator Express Box U.S.'),
('PurolatorExpressU.S.Box9AM', 'Purolator Express U.S. Box 9AM'),
('PurolatorExpressU.S.Box10:30AM', 'Purolator Express U.S. Box 10:30AM'),
('PurolatorExpressU.S.Box12:00', 'Purolator Express U.S. Box 12:00'),
('PurolatorGroundU.S.', 'Purolator Ground U.S.'),
('PurolatorExpressInternational', 'Purolator Express International'),
('PurolatorExpressInternational9AM', 'Purolator Express International 9AM'),
('PurolatorExpressInternational10:30AM', 'Purolator Express International 10:30AM'),
('PurolatorExpressInternational12:00', 'Purolator Express International 12:00'),
('PurolatorExpressEnvelopeInternational', 'Purolator Express Envelope International'),
('PurolatorExpressInternationalEnvelope9AM', 'Purolator Express International Envelope 9AM'),
('PurolatorExpressInternationalEnvelope10:30AM', 'Purolator Express International Envelope 10:30AM'),
('PurolatorExpressInternationalEnvelope12:00', 'Purolator Express International Envelope 12:00'),
('PurolatorExpressPackInternational', 'Purolator Express Pack International'),
('PurolatorExpressInternationalPack9AM', 'Purolator Express International Pack 9AM'),
('PurolatorExpressInternationalPack10:30AM', 'Purolator Express International Pack 10:30AM'),
('PurolatorExpressInternationalPack12:00', 'Purolator Express International Pack 12:00'),
('PurolatorExpressBoxInternational', 'Purolator Express Box International'),
('PurolatorExpressInternationalBox9AM', 'Purolator Express International Box 9AM'),
('PurolatorExpressInternationalBox10:30AM', 'Purolator Express International Box 10:30AM'),
('PurolatorExpressInternationalBox12:00', 'Purolator Express International Box 12:00'),
]
class ProviderPurolator(models.Model): class ProviderPurolator(models.Model):
_inherit = 'delivery.carrier' _inherit = 'delivery.carrier'
delivery_type = fields.Selection(selection_add=[('purolator', 'Purolator')], ondelete={'purolator': 'cascade'}) delivery_type = fields.Selection(selection_add=[('purolator', 'Purolator')],
ondelete={'purolator': lambda recs: recs.write({'delivery_type': 'fixed', 'fixed_price': 0})})
purolator_api_key = fields.Char(string='Purolator API Key', groups='base.group_system') purolator_api_key = fields.Char(string='Purolator API Key', groups='base.group_system')
purolator_password = fields.Char(string='Purolator Password', groups='base.group_system') purolator_password = fields.Char(string='Purolator Password', groups='base.group_system')
purolator_activation_key = fields.Char(string='Purolator Activation Key', groups='base.group_system') purolator_activation_key = fields.Char(string='Purolator Activation Key', groups='base.group_system')
purolator_account_number = fields.Char(string='Purolator Account Number', groups='base.group_system') purolator_account_number = fields.Char(string='Purolator Account Number', groups='base.group_system')
purolator_service_type = fields.Selection([('PurolatorExpress', 'PurolatorExpress')], default='PurolatorExpress') purolator_service_type = fields.Selection(selection=PUROLATOR_SERVICES,
default='PurolatorGround')
def purolator_rate_shipment(self, order): def purolator_rate_shipment(self, order):
# sudoself = self.sudo() # sudoself = self.sudo()
@@ -24,7 +91,13 @@ class ProviderPurolator(models.Model):
} }
weight_uom_id = self.env['product.template']._get_weight_uom_id_from_ir_config_parameter() weight_uom_id = self.env['product.template']._get_weight_uom_id_from_ir_config_parameter()
weight = weight_uom_id._compute_quantity(order._get_estimated_weight(), self.env.ref('uom.product_uom_lb'), round=False) weight = weight_uom_id._compute_quantity(order._get_estimated_weight(), self.env.ref('uom.product_uom_lb'), round=False)
client = PurolatorClient(self) client = PurolatorClient(
self.purolator_api_key,
self.purolator_password,
self.purolator_activation_key,
self.purolator_account_number,
self.prod_environment,
)
res = client.get_quick_estimate(sender.zip, receiver_address, 'CustomerPackaging', weight) res = client.get_quick_estimate(sender.zip, receiver_address, 'CustomerPackaging', weight)
if res['error']: if res['error']:
return { return {
@@ -33,9 +106,17 @@ class ProviderPurolator(models.Model):
'error_message': _(res['error']), 'error_message': _(res['error']),
'warning_message': False, 'warning_message': False,
} }
shipment = list(filter(lambda s: s['ServiceID'] == self.purolator_service_type, res['shipments']))
if not shipment:
return {
'success': False,
'price': 0.0,
'error_message': _('No rate found matching service: %s') % self.purolator_service_type,
'warning_message': False,
}
return { return {
'success': True, 'success': True,
'price': res['price'], 'price': shipment[0]['TotalPrice'],
'error_message': False, 'error_message': False,
'warning_message': False, 'warning_message': False,
} }

View File

@@ -9,16 +9,13 @@ _logger = logging.getLogger(__name__)
class PurolatorClient(object): class PurolatorClient(object):
def __init__(self, carrier): def __init__(self, api_key, password, activation_key, account_number, is_prod):
if carrier.delivery_type != 'purolator': self.api_key = api_key
raise UserError('Invalid carrier: %s' % carrier.name) self.password = password
self.api_key = carrier.purolator_api_key self.activation_key = activation_key
self.password = carrier.purolator_password self.account_number = account_number
self.activation_key = carrier.purolator_activation_key
self.account_number = carrier.purolator_account_number
self.service_type = carrier.purolator_service_type
self._wsdl_base = "https://devwebservices.purolator.com" self._wsdl_base = "https://devwebservices.purolator.com"
if carrier.prod_environment: if is_prod:
self._wsdl_base = "https://webservices.purolator.com" self._wsdl_base = "https://webservices.purolator.com"
session = Session() session = Session()
@@ -50,7 +47,7 @@ class PurolatorClient(object):
'PostalCode': string} 'PostalCode': string}
:param package_type: string :param package_type: string
:param total_weight: float (in pounds) :param total_weight: float (in pounds)
:returns: dict :returns: dict {'shipments': list, 'error': string or False}
""" """
client = self._get_client('/EWS/V2/Estimating/EstimatingService.asmx?wsdl') client = self._get_client('/EWS/V2/Estimating/EstimatingService.asmx?wsdl')
response = client.service.GetQuickEstimate( response = client.service.GetQuickEstimate(
@@ -67,17 +64,16 @@ class PurolatorClient(object):
errors = response['body']['ResponseInformation']['Errors'] errors = response['body']['ResponseInformation']['Errors']
if errors: if errors:
return { return {
'price': 0.0, 'shipments': False,
'error': '\n'.join(['%s: %s' % (error['Code'], error['Description']) for error in errors['Error']]), 'error': '\n'.join(['%s: %s' % (error['Code'], error['Description']) for error in errors['Error']]),
} }
shipments = response['body']['ShipmentEstimates']['ShipmentEstimate'] shipments = response['body']['ShipmentEstimates']['ShipmentEstimate']
shipment = list(filter(lambda s: s['ServiceID'] == self.service_type, shipments)) if shipments:
if shipment:
return { return {
'price': shipment[0]['TotalPrice'], 'shipments': shipments,
'error': False, 'error': False,
} }
return { return {
'price': 0.0, 'shipments': False,
'error': 'Purolator ServiceID not found', 'error': 'Purolator service did not return any matching rates.',
} }