Skip to content

Commit

Permalink
imrprove DHL shipment creation mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
danh91 committed Nov 8, 2018
1 parent 1005a03 commit afdedc6
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 202 deletions.
4 changes: 2 additions & 2 deletions purplship/mappers/dhl/dhl_mapper/partials/rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def create_dct_request(self, payload: T.shipment_request) -> Req.DCTRequest:
BkgDetails=ReqType.BkgDetailsType(
PaymentCountryCode=payload.shipment.payment_country_code or "CA",
NetworkTypeCode=payload.shipment.extra.get('NetworkTypeCode') or "AL",
WeightUnit=payload.shipment.weight_unit or "LB",
DimensionUnit=payload.shipment.dimension_unit or "IN",
WeightUnit=payload.shipment.weight_unit or "KG",
DimensionUnit=payload.shipment.dimension_unit or "CM",
ReadyTime=time.strftime("PT%HH%MM"),
Date=time.strftime("%Y-%m-%d"),
IsDutiable="Y" if is_dutiable else "N",
Expand Down
265 changes: 135 additions & 130 deletions purplship/mappers/dhl/dhl_mapper/partials/shipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,131 +48,118 @@ def _extract_shipment(self, shipmentResponseNode) -> T.ShipmentDetails:
)

def create_dhlshipment_request(self, payload: T.shipment_request) -> ShipReq.ShipmentRequest:
Request_ = self.init_request()
Request_.MetaData = MetaData(SoftwareName="3PV", SoftwareVersion="1.0")

Billing_ = ShipReq.Billing(
ShipperAccountNumber=payload.shipper.account_number or self.client.account_number,
BillingAccountNumber=payload.shipment.payment_account_number,
ShippingPaymentType=payload.shipment.paid_by,
DutyAccountNumber=payload.shipment.duty_payment_account,
DutyPaymentType=payload.shipment.duty_paid_by
)

Consignee_ = ShipReq.Consignee(
CompanyName=payload.recipient.company_name,
PostalCode=payload.recipient.postal_code,
CountryCode=payload.recipient.country_code,
City=payload.recipient.city,
CountryName=payload.recipient.country_name,
Division=payload.recipient.state,
DivisionCode=payload.recipient.state_code
is_dutiable = payload.shipment.declared_value != None
default_packaging_type = "FLY" if payload.shipment.is_document else "BOX"
extra_services = (
[payload.shipment.service_type] +
payload.shipment.extra_services +
([] if not payload.shipment.insured_amount else ["I"])
)

if any([payload.recipient.person_name, payload.recipient.email_address]):
Consignee_.Contact = ShipReq.Contact(
PersonName=payload.recipient.person_name,
PhoneNumber=payload.recipient.phone_number,
Email=payload.recipient.email_address,
FaxNumber=payload.recipient.extra.get('FaxNumber'),
Telex=payload.recipient.extra.get('Telex'),
PhoneExtension=payload.recipient.extra.get('PhoneExtension'),
MobilePhoneNumber=payload.recipient.extra.get('MobilePhoneNumber')
)

[Consignee_.add_AddressLine(line)
for line in payload.recipient.address_lines]

Shipper_ = ShipReq.Shipper(
ShipperID=payload.shipment.extra.get('ShipperID') or Billing_.ShipperAccountNumber,
RegisteredAccount=payload.shipment.extra.get('ShipperID') or Billing_.ShipperAccountNumber,
CompanyName=payload.shipper.company_name,
PostalCode=payload.shipper.postal_code,
CountryCode=payload.shipper.country_code,
City=payload.shipper.city,
CountryName=payload.shipper.country_name,
Division=payload.shipper.state,
DivisionCode=payload.shipper.state_code
)

if any([payload.shipper.person_name, payload.shipper.email_address]):
Shipper_.Contact = ShipReq.Contact(
PersonName=payload.shipper.person_name,
PhoneNumber=payload.shipper.phone_number,
Email=payload.shipper.email_address,
FaxNumber=payload.shipper.extra.get('FaxNumber'),
Telex=payload.shipper.extra.get('Telex'),
PhoneExtension=payload.shipper.extra.get('PhoneExtension'),
MobilePhoneNumber=payload.shipper.extra.get('MobilePhoneNumber')
)

[Shipper_.add_AddressLine(line)
for line in payload.shipper.address_lines]

Pieces_ = ShipReq.Pieces()
for p in payload.shipment.items:
Pieces_.add_Piece(ShipReq.Piece(
PieceID=p.id,
PackageType=p.packaging_type,
Weight=p.weight,
DimWeight=p.extra.get('DimWeight'),
Height=p.height,
Width=p.width,
Depth=p.length,
PieceContents=p.content
))

"""
Get PackageType from extra when implementing multi carrier,
Get weight from total_weight if specified otherwise calculated from items weight sum
"""
ShipmentDetails_ = ShipReq.ShipmentDetails(
NumberOfPieces=len(payload.shipment.items),
Pieces=Pieces_,
Weight=payload.shipment.total_weight or sum([p.weight for p in payload.shipment.items]),
CurrencyCode=payload.shipment.currency or "USD",
WeightUnit=(payload.shipment.weight_unit or "LB")[0],
DimensionUnit=(payload.shipment.dimension_unit or "IN")[0],
Date=payload.shipment.date or time.strftime('%Y-%m-%d'),
PackageType=payload.shipment.packaging_type or payload.shipment.extra.get('PackageType'),
IsDutiable= "N" if payload.shipment.is_document else "Y",
InsuredAmount=payload.shipment.insured_amount,
DoorTo=payload.shipment.extra.get('DoorTo'),
GlobalProductCode=payload.shipment.extra.get('GlobalProductCode'),
LocalProductCode=payload.shipment.extra.get('LocalProductCode'),
Contents=payload.shipment.extra.get('Contents') or "..."
)
Request_ = self.init_request()
Request_.MetaData = MetaData(SoftwareName="3PV", SoftwareVersion="1.0")

ShipmentRequest_ = ShipReq.ShipmentRequest(
return ShipReq.ShipmentRequest(
schemaVersion="6.1",
Request=Request_,
RegionCode=payload.shipment.extra.get('RegionCode') or "AM",
RequestedPickupTime=payload.shipment.extra.get('RequestedPickupTime') or "Y",
LanguageCode=payload.shipment.extra.get('LanguageCode') or "en",
PiecesEnabled=payload.shipment.extra.get('PiecesEnabled') or "Y",
LatinResponseInd=None,
Billing=ShipReq.Billing(
ShipperAccountNumber=payload.shipper.account_number,
BillingAccountNumber=payload.shipment.payment_account_number,
ShippingPaymentType=payload.shipment.paid_by,
DutyAccountNumber=payload.shipment.duty_payment_account,
DutyPaymentType=payload.shipment.duty_paid_by
),
Consignee=ShipReq.Consignee(
CompanyName=payload.recipient.company_name,
SuiteDepartmentName=None,
AddressLine=payload.recipient.address_lines,
City=payload.recipient.city,
Division=payload.recipient.state,
DivisionCode=payload.recipient.state_code,
PostalCode=payload.recipient.postal_code,
CountryCode=payload.recipient.country_code,
CountryName=payload.recipient.country_name,
FederalTaxId=payload.shipper.tax_id,
StateTaxId=None,
Contact=ShipReq.Contact(
PersonName=payload.recipient.person_name,
PhoneNumber=payload.recipient.phone_number,
Email=payload.recipient.email_address,
FaxNumber=payload.recipient.extra.get('FaxNumber'),
Telex=payload.recipient.extra.get('Telex'),
PhoneExtension=payload.recipient.extra.get('PhoneExtension'),
MobilePhoneNumber=payload.recipient.extra.get('MobilePhoneNumber')
) if any([payload.recipient.person_name, payload.recipient.phone_number, payload.recipient.email_address]) else None,
Suburb=None
),
Commodity=[
ShipReq.Commodity(
CommodityCode=c.code,
CommodityName=c.description
) for c in payload.shipment.items
],
NewShipper=payload.shipment.extra.get('NewShipper'),
Billing=Billing_,
Consignee=Consignee_,
Shipper=Shipper_,
ShipmentDetails=ShipmentDetails_,
EProcShip=payload.shipment.extra.get('EProcShip')
)

if payload.shipment.label is not None:
DocImages_ = ShipReq.DocImages()
Image_ = None if 'Image' not in payload.shipment.label.extra else b64decode(
payload.shipment.label.extra.get('Image') + '=' * (-len(payload.shipment.label.extra.get('Image')) % 4)
)
DocImages_.add_DocImage(ShipReq.DocImage(
Type=payload.shipment.label.type,
ImageFormat=payload.shipment.label.format,
Image=Image_
))
ShipmentRequest_.DocImages = DocImages_

if ShipmentDetails_.IsDutiable == "Y":
ShipmentRequest_.Dutiable = ShipReq.Dutiable(
DeclaredCurrency=ShipmentDetails_.CurrencyCode,
Shipper=ShipReq.Shipper(
ShipperID=payload.shipper.account_number,
RegisteredAccount=payload.shipper.account_number,
AddressLine=payload.shipper.address_lines,
CompanyName=payload.shipper.company_name,
PostalCode=payload.shipper.postal_code,
CountryCode=payload.shipper.country_code,
City=payload.shipper.city,
CountryName=payload.shipper.country_name,
Division=payload.shipper.state,
DivisionCode=payload.shipper.state_code,
Contact=ShipReq.Contact(
PersonName=payload.shipper.person_name,
PhoneNumber=payload.shipper.phone_number,
Email=payload.shipper.email_address,
FaxNumber=payload.shipper.extra.get('FaxNumber'),
Telex=payload.shipper.extra.get('Telex'),
PhoneExtension=payload.shipper.extra.get('PhoneExtension'),
MobilePhoneNumber=payload.shipper.extra.get('MobilePhoneNumber')
) if any([payload.shipper.person_name, payload.shipper.phone_number, payload.shipper.email_address]) else None
),
ShipmentDetails=ShipReq.ShipmentDetails(
NumberOfPieces=len(payload.shipment.items),
Pieces=(lambda pieces:
(
pieces,
[
pieces.add_Piece(ShipReq.Piece(
PieceID=p.id,
PackageType=p.packaging_type or default_packaging_type,
Weight=p.weight,
DimWeight=p.extra.get('DimWeight'),
Height=p.height,
Width=p.width,
Depth=p.length,
PieceContents=p.content
)) for p in payload.shipment.items
]
)[0]
)(ShipReq.Pieces()),
Weight=payload.shipment.total_weight or sum([p.weight for p in payload.shipment.items]),
CurrencyCode=payload.shipment.currency or "USD",
WeightUnit=(payload.shipment.weight_unit or "KG")[0],
DimensionUnit=(payload.shipment.dimension_unit or "CM")[0],
Date=payload.shipment.date or time.strftime('%Y-%m-%d'),
PackageType=payload.shipment.packaging_type or payload.shipment.extra.get('PackageType'),
IsDutiable= "N" if payload.shipment.is_document else "Y",
InsuredAmount=payload.shipment.insured_amount,
DoorTo=payload.shipment.extra.get('DoorTo'),
GlobalProductCode=payload.shipment.extra.get('GlobalProductCode'),
LocalProductCode=payload.shipment.extra.get('LocalProductCode'),
Contents=payload.shipment.extra.get('Contents') or "..."
),
EProcShip=payload.shipment.extra.get('EProcShip'),
Dutiable=ShipReq.Dutiable(
DeclaredCurrency=payload.shipment.currency or "USD",
DeclaredValue=payload.shipment.declared_value,
TermsOfTrade=payload.shipment.customs.terms_of_trade,
ScheduleB=payload.shipment.customs.extra.get('ScheduleB'),
Expand All @@ -181,19 +168,37 @@ def create_dhlshipment_request(self, payload: T.shipment_request) -> ShipReq.Shi
ShipperIDType=payload.shipment.customs.extra.get('ShipperIDType'),
ImportLicense=payload.shipment.customs.extra.get('ImportLicense'),
ConsigneeEIN=payload.shipment.customs.extra.get('ConsigneeEIN')
)

[ShipmentRequest_.add_SpecialService(
ShipReq.SpecialService(SpecialServiceType=service)
) for service in (payload.shipment.extra_services + [payload.shipment.service_type])]

[ShipmentRequest_.add_Commodity(
ShipReq.Commodity(CommodityCode=c.code, CommodityName=c.description)
) for c in payload.shipment.items]

[ShipmentRequest_.add_Reference(
ShipReq.Reference(ReferenceID=r)
) for r in payload.shipment.references]

return ShipmentRequest_
) if is_dutiable else None,
ExportDeclaration=None,
Reference=[
ShipReq.Reference(
ReferenceID=r
) for r in payload.shipment.references
],
SpecialService=[
ShipReq.SpecialService(
SpecialServiceType=service
) for service in extra_services
] if len(extra_services) > 0 else None,
LabelImageFormat=payload.shipment.label.format if payload.shipment.label != None else None,
DocImages=(lambda images:
(
images,
[
images.add_DocImage(ShipReq.DocImage(
Type=doc.type,
ImageFormat=doc.format,
Image=b64decode(
doc.image + '=' * (-len(doc.image) % 4)
)
)) for doc in payload.shipment.doc_images
]
)[0]
)(ShipReq.DocImages()) if len(payload.shipment.doc_images) > 0 else None,
RequestArchiveDoc=None,
NumberOfArchiveDoc=None,
Label=None,
ODDLinkReq=None,
DGs=None
)

Loading

0 comments on commit afdedc6

Please sign in to comment.