Example #1
0
    def create_track_request(
            self, payload: T.tracking_request) -> List[Track.TrackRequest]:
        Request_ = Common.RequestType(
            TransactionReference=Common.TransactionReferenceType(
                TransactionIdentifier="TransactionIdentifier"))

        Request_.add_RequestOption(1)

        TrackRequests_ = [
            Track.TrackRequest(Request=Request_, InquiryNumber=number)
            for number in payload.tracking_numbers
        ]

        return TrackRequests_
Example #2
0
 def create_freight_ship_request(
         self, payload: T.ShipmentRequest) -> FShip.FreightShipRequest:
     services = [
         ShippingServiceCode[svc] for svc in payload.shipment.services
         if svc in ShippingServiceCode.__members__
     ]
     payer = T.Party(
         **payload.shipment.extra.get("Payer")) if "Payer" else None
     return FShip.FreightShipRequest(
         Request=Common.RequestType(
             RequestOption=payload.shipment.extra.get("RequestOption")
             or "1",
             SubVersion=None,
             TransactionReference=Common.TransactionReferenceType(
                 CustomerContext=", ".join(payload.shipment.references),
                 TransactionIdentifier=payload.shipment.extra.get(
                     "TransactionIdentifier"),
             ),
         ),
         Shipment=FShip.ShipmentType(
             ShipFrom=FShip.ShipFromType(
                 Name=payload.shipper.company_name,
                 TaxIdentificationNumber=payload.shipper.tax_id,
                 TaxIDType=None,
                 TariffPoint=None,
                 Address=FShip.FreightShipAddressType(
                     AddressLine=payload.shipper.address_lines,
                     City=payload.shipper.city,
                     StateProvinceCode=payload.shipper.state_code,
                     Town=None,
                     PostalCode=payload.shipper.postal_code,
                     CountryCode=payload.shipper.country_code,
                 ),
                 AttentionName=payload.shipper.person_name,
                 Phone=FShip.FreightShipPhoneType(
                     Number=payload.shipper.phone_number,
                     Extension=payload.shipper.extra.get("Extension"),
                 ) if payload.shipper.phone_number is not None else None,
                 FaxNumber=payload.shipper.extra.get("FaxNumber"),
                 EMailAddress=payload.shipper.email_address,
             ),
             ShipperNumber=payload.shipper.account_number,
             ShipTo=FShip.ShipToType(
                 Name=payload.recipient.company_name,
                 TaxIdentificationNumber=payload.recipient.tax_id,
                 Address=FShip.FreightShipAddressType(
                     AddressLine=payload.recipient.address_lines,
                     City=payload.recipient.city,
                     StateProvinceCode=payload.recipient.state_code,
                     Town=None,
                     PostalCode=payload.recipient.postal_code,
                     CountryCode=payload.recipient.country_code,
                 ),
                 TariffPoint=None,
                 AttentionName=payload.recipient.person_name,
                 Phone=FShip.PhoneType(
                     Number=payload.recipient.phone_number,
                     Extension=payload.recipient.extra.get("Extension"),
                 ) if payload.recipient.phone_number is not None else None,
                 FaxNumber=payload.recipient.extra.get("FaxNumber"),
                 EMailAddress=payload.recipient.email_address,
             ),
             PaymentInformation=FShip.PaymentInformationType(
                 Payer=FShip.PayerType(
                     Name=payer.company_name,
                     Address=FShip.FreightShipAddressType(
                         AddressLine=payer.address_lines,
                         City=payer.city,
                         StateProvinceCode=payer.state_code,
                         Town=None,
                         PostalCode=payer.postal_code,
                         CountryCode=payer.country_code,
                     ),
                     ShipperNumber=payer.account_number
                     or payload.shipment.payment_account_number,
                     AttentionName=payer.person_name,
                     Phone=FShip.FreightShipPhoneType(
                         Number=payer.phone_number,
                         Extension=payer.extra.get("Extension"),
                     ) if payer.phone_number is not None else None,
                     FaxNumber=payer.extra.get("FaxNumber"),
                     EMailAddress=payer.email_address,
                 ) if payer is not None else None,
                 ShipmentBillingOption=(
                     lambda option: FShip.ShipCodeDescriptionType(
                         Code=option.get("Code"),
                         Description=option.get("Description"),
                     ))(payload.shipment.extra.get("ShipmentBillingOption"))
                 if "ShipmentBillingOption" in payload.shipment.extra else
                 None,
             ),
             ManufactureInformation=None,
             Service=FShip.ShipCodeDescriptionType(
                 Code=services[0].value) if len(services) > 0 else None,
             HandlingUnitOne=(lambda unit: FShip.HandlingUnitType(
                 Quantity=unit.get("Quantity"),
                 Type=FShip.ShipCodeDescriptionType(
                     Code=unit.get("Type").get("Code"),
                     Description=unit.get("Type").get("Description"),
                 ) if "Type" in unit else None,
             ))(payload.shipment.extra.get("HandlingUnitOne"))
             if "HandlingUnitOne" in payload.shipment.extra else None,
             HandlingUnitTwo=(lambda unit: FShip.HandlingUnitType(
                 Quantity=unit.get("Quantity"), Type=unit.get("Type")))(
                     payload.shipment.extra.get("HandlingUnitTwo"))
             if "HandlingUnitTwo" in payload.shipment.extra else None,
             ExistingShipmentID=None,
             HandlingInstructions=None,
             DeliveryInstructions=None,
             PickupInstructions=None,
             SpecialInstructions=None,
             ShipmentTotalWeight=None,
             Commodity=[
                 FShip.CommodityType(
                     CommodityID=pkg.id,
                     Description=pkg.description,
                     Weight=FShip.WeightType(
                         UnitOfMeasurement=FShip.
                         FreightShipUnitOfMeasurementType(
                             Code=WeightUnit[
                                 payload.shipment.weight_unit].value,
                             Description=None,
                         ),
                         Value=pkg.weight,
                     ),
                     Dimensions=FShip.DimensionsType(
                         UnitOfMeasurement=FShip.
                         FreightShipUnitOfMeasurementType(
                             Code=DimensionUnit[
                                 payload.shipment.dimension_unit].value,
                             Description=None,
                         ),
                         Length=pkg.length,
                         Width=pkg.width,
                         Height=pkg.height,
                     ) if any(
                         (pkg.length, pkg.width, pkg.height)) else None,
                     NumberOfPieces=pkg.quantity,
                     PackagingType=FShip.ShipCodeDescriptionType(
                         Code=PackagingType[pkg.packaging_type].value,
                         Description=None,
                     ) if pkg.packaging_type != None else None,
                     DangerousGoodsIndicator=None,
                     CommodityValue=FShip.CommodityValueType(
                         CurrencyCode=pkg.value_amount,
                         MonetaryValue=pkg.value_currency,
                     ) if any(
                         (pkg.value_amount, pkg.value_currency)) else None,
                     FreightClass=pkg.extra.get("FreightClass"),
                     NMFCCommodityCode=None,
                     NMFCCommodity=None,
                 ) for pkg in payload.shipment.items
             ],
             Reference=None,
             ShipmentServiceOptions=None,
             PickupRequest=None,
             Documents=None,
             ITNNumber=None,
             TaxID=None,
             MovementReferenceNumber=None,
             EICNumberAndStatement=None,
             TimeInTransitIndicator=None,
             HandlingUnits=None,
             DensityEligibleIndicator=None,
         ),
     )
Example #3
0
 def create_package_ship_request(
         self, payload: T.ShipmentRequest) -> PShip.ShipmentRequest:
     services = [
         ShippingServiceCode[svc] for svc in payload.shipment.services
         if svc in ShippingServiceCode.__members__
     ]
     return PShip.ShipmentRequest(
         Request=Common.RequestType(
             RequestOption=payload.shipment.extra.get("RequestOption")
             or ["validate"],
             SubVersion=None,
             TransactionReference=Common.TransactionReferenceType(
                 CustomerContext=", ".join(payload.shipment.references),
                 TransactionIdentifier=payload.shipment.extra.get(
                     "TransactionIdentifier"),
             ),
         ),
         Shipment=PShip.ShipmentType(
             Description=payload.shipment.extra.get("Description"),
             ReturnService=None,
             DocumentsOnlyIndicator=""
             if payload.shipment.is_document else None,
             Shipper=PShip.ShipperType(
                 Name=payload.shipper.company_name,
                 AttentionName=payload.shipper.person_name,
                 CompanyDisplayableName=payload.shipper.extra.get(
                     "CompanyDisplayableName"),
                 TaxIdentificationNumber=payload.shipper.tax_id,
                 TaxIDType=None,
                 Phone=PShip.ShipPhoneType(
                     Number=payload.shipper.phone_number,
                     Extension=payload.shipper.extra.get("Extension"),
                 ) if payload.shipper.phone_number is not None else None,
                 ShipperNumber=payload.shipper.account_number,
                 FaxNumber=payload.shipper.extra.get("FaxNumber"),
                 EMailAddress=payload.shipper.email_address,
                 Address=PShip.ShipAddressType(
                     AddressLine=payload.shipper.address_lines,
                     City=payload.shipper.city,
                     StateProvinceCode=payload.shipper.state_code,
                     PostalCode=payload.shipper.postal_code,
                     CountryCode=payload.shipper.country_code,
                 ),
             ),
             ShipTo=PShip.ShipToType(
                 Name=payload.recipient.company_name,
                 AttentionName=payload.recipient.person_name,
                 CompanyDisplayableName=payload.recipient.extra.get(
                     "CompanyDisplayableName"),
                 TaxIdentificationNumber=payload.recipient.tax_id,
                 TaxIDType=None,
                 Phone=PShip.ShipPhoneType(
                     Number=payload.recipient.phone_number,
                     Extension=payload.recipient.extra.get("Extension"),
                 ) if payload.recipient.phone_number is not None else None,
                 FaxNumber=payload.recipient.extra.get("FaxNumber"),
                 EMailAddress=payload.recipient.email_address,
                 Address=PShip.ShipAddressType(
                     AddressLine=payload.recipient.address_lines,
                     City=payload.recipient.city,
                     StateProvinceCode=payload.recipient.state_code,
                     PostalCode=payload.recipient.postal_code,
                     CountryCode=payload.recipient.country_code,
                 ),
                 LocationID=None,
             ),
             AlternateDeliveryAddress=(
                 lambda alternate: PShip.AlternateDeliveryAddressType(
                     Name=alternate.company_name,
                     AttentionName=alternate.person_name,
                     UPSAccessPointID=alternate.extra.get("UPSAccessPointID"
                                                          ),
                     Address=PShip.ShipAddressType(
                         AddressLine=alternate.address_lines,
                         City=alternate.city,
                         StateProvinceCode=alternate.state_code,
                         PostalCode=alternate.postal_code,
                         CountryCode=alternate.country_code,
                     ),
                 )
             )(T.Party(
                 payload.shipment.extra.get("AlternateDeliveryAddress"))) if
             "AlternateDeliveryAddress" in payload.shipment.extra else None,
             ShipFrom=(lambda shipFrom: PShip.ShipFromType(
                 Name=shipFrom.company_name,
                 AttentionName=shipFrom.person_name,
                 CompanyDisplayableName=shipFrom.extra.get(
                     "CompanyDisplayableName"),
                 TaxIdentificationNumber=shipFrom.tax_id,
                 TaxIDType=None,
                 Phone=PShip.ShipPhoneType(
                     Number=shipFrom.phone_number,
                     Extension=shipFrom.extra.get("Extension"),
                 ) if shipFrom.phone_number is not None else None,
                 FaxNumber=shipFrom.extra.get("FaxNumber"),
                 EMailAddress=shipFrom.email_address,
                 Address=PShip.ShipAddressType(
                     AddressLine=shipFrom.address_lines,
                     City=shipFrom.city,
                     StateProvinceCode=shipFrom.state_code,
                     PostalCode=shipFrom.postal_code,
                     CountryCode=shipFrom.country_code,
                 ),
             ))(T.Party(**payload.shipment.extra.get("ShipFrom")))
             if "ShipFrom" in payload.shipment.extra else None,
             PaymentInformation=PShip.PaymentInfoType(
                 ShipmentCharge=[
                     PShip.ShipmentChargeType(
                         Type=payload.shipment.extra.get("ShipmentCharge").
                         get("Type") if "ShipmentCharge"
                         in payload.shipment.extra else None,
                         BillShipper=PShip.BillShipperType(
                             AccountNumber=payload.shipment.
                             payment_account_number
                             or payload.shipper.account_number,
                             CreditCard=(lambda card: PShip.CreditCardType(
                                 Type=card.get("Type"),
                                 Number=card.get("Number"),
                                 ExpirationDate=card.get("ExpirationDate"),
                                 SecurityCode=card.get("Type"),
                                 Address
                                 =(lambda address: PShip.ShipAddressType(
                                     AddressLine=address.address_lines,
                                     City=address.city,
                                     StateProvinceCode=address.state_code,
                                     PostalCode=address.postal_code,
                                     CountryCode=address.country_code,
                                 ))(T.Party(**card.get("Address")))
                                 if "Address" in card else None,
                             ))(payload.shipment.extra.get("CreditCard"))
                             if "CreditCard" in payload.shipment.extra else
                             None,
                             AlternatePaymentMethod=payload.shipment.
                             payment_type,
                         )
                         if payload.shipment.paid_by == "SENDER" else None,
                         BillReceiver=PShip.BillReceiverType(
                             AccountNumber=payload.recipient.account_number,
                             Address=PShip.BillReceiverAddressType(
                                 PostalCode=payload.recipient.postal_code),
                         ) if not payload.shipment.paid_by else None,
                         BillThirdParty=PShip.BillThirdPartyChargeType(
                             AccountNumber=payload.shipment.
                             payment_account_number,
                             Address=PShip.BillReceiverAddressType(
                                 PostalCode=payload.shipment.extra.get(
                                     "payor_postal_code")),
                         ) if payload.shipment.paid_by == "THIRD_PARTY" else
                         None,
                         ConsigneeBilledIndicator=None,
                     )
                 ],
                 SplitDutyVATIndicator=None,
             ),
             FRSPaymentInformation=None,
             FreightShipmentInformation=None,
             GoodsNotInFreeCirculationIndicator=None,
             ShipmentRatingOptions=None,
             MovementReferenceNumber=None,
             ReferenceNumber=None,
             Service=PShip.ServiceType(
                 Code=services[0].value) if len(services) > 0 else None,
             InvoiceLineTotal=None,
             NumOfPiecesInShipment=payload.shipment.total_items,
             USPSEndorsement=None,
             MILabelCN22Indicator=None,
             SubClassification=None,
             CostCenter=None,
             PackageID=None,
             IrregularIndicator=None,
             ShipmentIndicationType=None,
             MIDualReturnShipmentKey=None,
             MIDualReturnShipmentIndicator=None,
             RatingMethodRequestedIndicator=None,
             TaxInformationIndicator=None,
             PromotionalDiscountInformation=None,
             ShipmentServiceOptions=None,
             Package=[
                 PShip.PackageType(
                     Description=pkg.description,
                     Packaging=PShip.PackagingType(
                         Code=ShippingPackagingType[
                             pkg.packaging_type].value,
                         Description=None,
                     ) if pkg.packaging_type != None else None,
                     Dimensions=PShip.DimensionsType(
                         UnitOfMeasurement=PShip.ShipUnitOfMeasurementType(
                             Code=DimensionUnit[
                                 payload.shipment.dimension_unit].value,
                             Description=None,
                         ),
                         Length=pkg.length,
                         Width=pkg.width,
                         Height=pkg.height,
                     ),
                     DimWeight=pkg.extra.get("DimWeight"),
                     PackageWeight=PShip.PackageWeightType(
                         UnitOfMeasurement=PShip.ShipUnitOfMeasurementType(
                             Code=WeightUnit[
                                 payload.shipment.weight_unit].value,
                             Description=None,
                         ),
                         Weight=pkg.weight,
                     ),
                     LargePackageIndicator=pkg.extra.get(
                         "LargePackageIndicator"),
                     ReferenceNumber=None,
                     AdditionalHandlingIndicator=None,
                     PackageServiceOptions=None,
                     Commodity=None,
                     HazMatPackageInformation=None,
                 ) for pkg in payload.shipment.items
             ],
         ),
         LabelSpecification=PShip.LabelSpecificationType(
             LabelImageFormat=PShip.LabelImageFormatType(
                 Code=payload.shipment.label.format,
                 Description=payload.shipment.label.format,
             ),
             HTTPUserAgent=payload.shipment.label.extra.get(
                 "HTTPUserAgent"),
             LabelStockSize=None,
             Instruction=payload.shipment.label.extra.get("Instruction"),
         ) if payload.shipment.label is not None else None,
         ReceiptSpecification=None,
     )
Example #4
0
def freight_rate_request(
        payload: RateRequest,
        settings: Settings) -> Serializable[FreightRateRequest]:
    packages = Packages(payload.parcels, PackagePresets)
    service = ([
        RatingServiceCode[svc]
        for svc in payload.services if svc in RatingServiceCode.__members__
    ] + [RatingServiceCode.ups_freight_ltl_guaranteed])[0]
    request = FreightRateRequest(
        Request=common.RequestType(
            TransactionReference=common.TransactionReferenceType(
                TransactionIdentifier="TransactionIdentifier"),
            RequestOption=[1],
        ),
        ShipFrom=ShipFromType(
            Name=payload.shipper.company_name,
            Address=AddressType(
                AddressLine=concat_str(payload.shipper.address_line1,
                                       payload.shipper.address_line2),
                City=payload.shipper.city,
                PostalCode=payload.shipper.postal_code,
                CountryCode=payload.shipper.country_code,
                StateProvinceCode=payload.shipper.state_code,
            ),
            AttentionName=payload.shipper.person_name,
        ),
        ShipTo=ShipToType(
            Name=payload.recipient.company_name,
            Address=AddressType(
                AddressLine=concat_str(payload.recipient.address_line1,
                                       payload.recipient.address_line2),
                City=payload.recipient.city,
                PostalCode=payload.recipient.postal_code,
                CountryCode=payload.recipient.country_code,
                StateProvinceCode=payload.recipient.state_code,
            ),
            AttentionName=payload.recipient.person_name,
        ),
        PaymentInformation=None,
        Service=RateCodeDescriptionType(Code=service.value, Description=None),
        HandlingUnitOne=HandlingUnitType(
            Quantity=1, Type=RateCodeDescriptionType(Code="SKD")),
        ShipmentServiceOptions=ShipmentServiceOptionsType(
            PickupOptions=PickupOptionsType(WeekendPickupIndicator="")),
        DensityEligibleIndicator="",
        AdjustedWeightIndicator="",
        HandlingUnitWeight=None,
        PickupRequest=None,
        GFPOptions=None,
        TimeInTransitIndicator="",
        Commodity=[
            CommodityType(
                Description=package.parcel.description or "...",
                Weight=WeightType(
                    UnitOfMeasurement=UnitOfMeasurementType(
                        Code=UPSWeightUnit[package.weight_unit].value),
                    Value=package.weight.value,
                ),
                Dimensions=DimensionsType(
                    UnitOfMeasurement=UnitOfMeasurementType(
                        Code=package.dimension_unit),
                    Width=package.width.value,
                    Height=package.height.value,
                    Length=package.length.value,
                ),
                NumberOfPieces=None,
                PackagingType=RateCodeDescriptionType(
                    Code=FreightPackagingType[package.packaging_type
                                              or "small_box"].value,
                    Description=None,
                ),
                FreightClass=50,
            ) for package in packages
        ],
    )
    return Serializable(
        create_envelope(header_content=settings.Security,
                        body_content=request),
        _request_serializer,
    )
Example #5
0
def freight_ship_request(
    payload: ShipmentRequest, settings: Settings
) -> Serializable[FreightShipRequest]:
    dimension_unit = DimensionUnit[payload.parcel.dimension_unit or "IN"]
    weight_unit = WeightUnit[payload.parcel.weight_unit or "LB"]
    options = Options(payload.options)
    service = ShippingServiceCode[payload.service].value
    freight_class = FreightClass[
        payload.options.get("ups_freight_class", "ups_freight_class_50")
    ].value

    request = FreightShipRequest(
        Request=common.RequestType(
            RequestOption="1",
            SubVersion=None,
            TransactionReference=common.TransactionReferenceType(
                CustomerContext=payload.reference, TransactionIdentifier=None
            ),
        ),
        Shipment=ShipmentType(
            ShipFrom=ShipFromType(
                Name=payload.shipper.company_name,
                TaxIdentificationNumber=payload.shipper.federal_tax_id,
                TaxIDType=None,
                TariffPoint=None,
                Address=FreightShipAddressType(
                    AddressLine=concat_str(
                        payload.shipper.address_line1, payload.shipper.address_line2
                    ),
                    City=payload.shipper.city,
                    StateProvinceCode=payload.shipper.state_code,
                    Town=None,
                    PostalCode=payload.shipper.postal_code,
                    CountryCode=payload.shipper.country_code,
                ),
                AttentionName=payload.shipper.person_name,
                Phone=FreightShipPhoneType(
                    Number=payload.shipper.phone_number, Extension=None
                )
                if payload.shipper.phone_number is not None
                else None,
                FaxNumber=None,
                EMailAddress=payload.shipper.email,
            ),
            ShipperNumber=settings.account_number,
            ShipTo=ShipToType(
                Name=payload.recipient.company_name,
                TaxIdentificationNumber=payload.recipient.federal_tax_id,
                Address=FreightShipAddressType(
                    AddressLine=concat_str(
                        payload.recipient.address_line1,
                        payload.recipient.address_line2,
                    ),
                    City=payload.recipient.city,
                    StateProvinceCode=payload.recipient.state_code,
                    Town=None,
                    PostalCode=payload.recipient.postal_code,
                    CountryCode=payload.recipient.country_code,
                ),
                TariffPoint=None,
                AttentionName=payload.recipient.person_name,
                Phone=PhoneType(Number=payload.recipient.phone_number, Extension=None)
                if payload.recipient.phone_number is not None
                else None,
                FaxNumber=None,
                EMailAddress=payload.recipient.email,
            ),
            PaymentInformation=None,
            ManufactureInformation=None,
            Service=ShipCodeDescriptionType(Code=service)
            if service is not None
            else None,
            HandlingUnitOne=None,
            HandlingUnitTwo=None,
            ExistingShipmentID=None,
            HandlingInstructions=None,
            DeliveryInstructions=None,
            PickupInstructions=None,
            SpecialInstructions=None,
            ShipmentTotalWeight=None,
            Commodity=[
                CommodityType(
                    CommodityID=payload.parcel.id,
                    Description=payload.parcel.description,
                    Weight=WeightType(
                        UnitOfMeasurement=FreightShipUnitOfMeasurementType(
                            Code=UPSWeightUnit[weight_unit.name].value
                        ),
                        Value=Weight(payload.parcel.weight, weight_unit).value,
                    ),
                    Dimensions=DimensionsType(
                        UnitOfMeasurement=FreightShipUnitOfMeasurementType(
                            Code=dimension_unit.value
                        ),
                        Width=Dimension(payload.parcel.width, dimension_unit).value,
                        Height=Dimension(payload.parcel.height, dimension_unit).value,
                        Length=Dimension(payload.parcel.length, dimension_unit).value,
                    )
                    if any(
                        [
                            payload.parcel.width,
                            payload.parcel.height,
                            payload.parcel.length,
                        ]
                    )
                    else None,
                    NumberOfPieces=None,
                    PackagingType=None,
                    DangerousGoodsIndicator=None,
                    CommodityValue=None,
                    FreightClass=freight_class,
                    NMFCCommodityCode=None,
                    NMFCCommodity=None,
                )
            ],
            Reference=None,
            ShipmentServiceOptions=ShipmentServiceOptionsType(
                EMailInformation=[
                    EMailNotificationType(
                        EMailAddress=options.notification.email
                        or payload.shipper.email,
                        EventType=NOTIFICATION_EVENT_TYPES,
                    )
                ]
                if options.notification
                else None,
                PickupOptions=None,
                DeliveryOptions=None,
                OverSeasLeg=None,
                COD=CODType(
                    CODValue=CODValueType(
                        CurrencyCode=options.currency or "USD",
                        MonetaryValue=options.cash_on_delivery.amount,
                    ),
                    CODPaymentMethod=None,
                    CODBillingOption=None,
                    RemitTo=None,
                )
                if options.cash_on_delivery
                else None,
                DangerousGoods=None,
                SortingAndSegregating=None,
                DeclaredValue=None,
                ExcessDeclaredValue=None,
                CustomsValue=None,
                DeliveryDutiesPaidIndicator=None,
                DeliveryDutiesUnpaidIndicator=None,
                HandlingCharge=None,
                CustomsClearanceIndicator=None,
                FreezableProtectionIndicator=None,
                ExtremeLengthIndicator=None,
                LinearFeet=None,
            )
            if options.has_content
            else None,
            PickupRequest=None,
            Documents=None,
            ITNNumber=None,
            TaxID=None,
            MovementReferenceNumber=None,
            EICNumberAndStatement=None,
            TimeInTransitIndicator=None,
            HandlingUnits=None,
            DensityEligibleIndicator=None,
        ),
    )
    return Serializable(
        create_envelope(header_content=settings.Security, body_content=request),
        _request_serializer,
    )
Example #6
0
def shipment_request(
    payload: ShipmentRequest, settings: Settings
) -> Serializable[UPSShipmentRequest]:
    packages = Packages(payload.parcels, PackagePresets)
    is_document = all([parcel.is_document for parcel in payload.parcels])
    package_description = (packages[0].parcel.description if len(packages) == 1 else None)
    options = Options(payload.options)
    service = ShippingServiceCode[payload.service].value

    if (("freight" in service) or ("ground" in service)) and (
        packages.weight.value is None
    ):
        raise FieldError({"parcel.weight": FieldErrorCode.required})

    charges: Dict[str, Payment] = {
        "01": payload.payment,
        "02": payload.customs.duty if payload.customs is not None else None,
    }
    mps_packaging = ShippingPackagingType.your_packaging.value if len(packages) > 1 else None

    request = UPSShipmentRequest(
        Request=common.RequestType(
            RequestOption=["validate"],
            SubVersion=None,
            TransactionReference=common.TransactionReferenceType(
                CustomerContext=payload.reference, TransactionIdentifier=None
            ),
        ),
        Shipment=ShipmentType(
            Description=package_description,
            DocumentsOnlyIndicator="" if is_document else None,
            Shipper=ShipperType(
                Name=payload.shipper.company_name,
                AttentionName=payload.shipper.person_name,
                CompanyDisplayableName=None,
                TaxIdentificationNumber=payload.shipper.federal_tax_id,
                TaxIDType=None,
                Phone=(
                    ShipPhoneType(Number=payload.shipper.phone_number, Extension=None)
                    if payload.shipper.phone_number is not None
                    else None
                ),
                ShipperNumber=settings.account_number,
                FaxNumber=None,
                EMailAddress=payload.shipper.email,
                Address=ShipAddressType(
                    AddressLine=concat_str(
                        payload.shipper.address_line1, payload.shipper.address_line2
                    ),
                    City=payload.shipper.city,
                    StateProvinceCode=payload.shipper.state_code,
                    PostalCode=payload.shipper.postal_code,
                    CountryCode=payload.shipper.country_code,
                ),
            ),
            ShipTo=ShipToType(
                Name=payload.recipient.company_name,
                AttentionName=payload.recipient.person_name,
                CompanyDisplayableName=None,
                TaxIdentificationNumber=payload.recipient.federal_tax_id,
                TaxIDType=None,
                Phone=(
                    ShipPhoneType(Number=payload.recipient.phone_number, Extension=None)
                    if payload.recipient.phone_number is not None
                    else None
                ),
                FaxNumber=None,
                EMailAddress=payload.recipient.email,
                Address=ShipAddressType(
                    AddressLine=concat_str(
                        payload.recipient.address_line1,
                        payload.recipient.address_line2,
                    ),
                    City=payload.recipient.city,
                    StateProvinceCode=payload.recipient.state_code,
                    PostalCode=payload.recipient.postal_code,
                    CountryCode=payload.recipient.country_code,
                ),
            ),
            PaymentInformation=PaymentInfoType(
                ShipmentCharge=[
                    ShipmentChargeType(
                        Type=charge_type,
                        BillShipper=BillShipperType(
                            AccountNumber=settings.account_number,
                            CreditCard=CreditCardType(
                                Type=payment.credit_card.type,
                                Number=payment.credit_card.number,
                                ExpirationDate=(
                                    f"{payment.credit_card.expiry_year}{payment.credit_card.expiry_month}"
                                ),
                                SecurityCode=payment.credit_card.security_code,
                                Address=CreditCardAddressType(
                                    AddressLine=concat_str(
                                        payload.shipper.address_line1,
                                        payload.shipper.address_line2,
                                    ),
                                    City=payload.shipper.city,
                                    StateProvinceCode=payload.shipper.state_code,
                                    PostalCode=payload.payment.credit_card.postal_code
                                    or payload.shipper.postal_code,
                                    CountryCode=payload.shipper.country_code,
                                ),
                            )
                            if payment.credit_card is not None
                            else None,
                            AlternatePaymentMethod=None,
                        )
                        if payment.paid_by == PaymentType.sender.name
                        else None,
                        BillReceiver=BillReceiverType(
                            AccountNumber=payment.account_number,
                            Address=BillReceiverAddressType(
                                PostalCode=payload.recipient.postal_code
                            ),
                        )
                        if payment.paid_by == PaymentType.recipient.name
                        else None,
                        BillThirdParty=BillThirdPartyChargeType(
                            AccountNumber=payment.account_number,
                        )
                        if payment.paid_by == PaymentType.third_party.name
                        else None,
                        ConsigneeBilledIndicator=None,
                    )
                    for charge_type, payment in charges.items()
                    if payment is not None
                ],
                SplitDutyVATIndicator=None,
            )
            if any(charges.values())
            else None,
            Service=(ServiceType(Code=service) if service is not None else None),
            ShipmentServiceOptions=ShipmentServiceOptionsType(
                COD=CODType(
                    CODFundsCode=None,
                    CODAmount=CurrencyMonetaryType(
                        CurrencyCode=options.currency or "USD",
                        MonetaryValue=options.cash_on_delivery.amount,
                    ),
                )
                if options.cash_on_delivery
                else None,
                Notification=[
                    NotificationType(
                        NotificationCode=event,
                        EMail=EmailDetailsType(
                            EMailAddress=[
                                options.notification.email or payload.shipper.email
                            ],
                        ),
                        VoiceMessage=None,
                        TextMessage=None,
                        Locale=None,
                    )
                    for event in [8]
                ]
                if options.notification
                else None,
            )
            if any([options.cash_on_delivery, options.notification])
            else None,
            Package=[
                PackageType(
                    Description=package.parcel.description,
                    Packaging=PackagingType(
                        Code=mps_packaging or ShippingPackagingType[package.packaging_type or "your_packaging"].value
                    ),
                    Dimensions=DimensionsType(
                        UnitOfMeasurement=ShipUnitOfMeasurementType(
                            Code=package.dimension_unit.value,
                        ),
                        Length=package.length.value,
                        Width=package.width.value,
                        Height=package.height.value,
                    ),
                    PackageWeight=PackageWeightType(
                        UnitOfMeasurement=ShipUnitOfMeasurementType(
                            Code=UPSWeightUnit[package.weight_unit.name].value,
                        ),
                        Weight=package.weight.value,
                    ),
                )
                for package in packages
            ],
        ),
        LabelSpecification=LabelSpecificationType(
            LabelImageFormat=LabelImageFormatType(
                Code='GIF',
                Description=None
            ),
            HTTPUserAgent=None,
            LabelStockSize=None,
            Instruction=None,
            CharacterSet=None
        ),
        ReceiptSpecification=None,
    )
    return Serializable(
        create_envelope(header_content=settings.Security, body_content=request),
        _request_serializer,
    )
Example #7
0
 def create_package_rate_request(
     self, payload: T.ShipmentRequest
 ) -> PRate.RateRequest:
     service = (
         [
             RatingServiceCode[svc]
             for svc in payload.shipment.services
             if svc in RatingServiceCode.__members__
         ]
         + [RatingServiceCode.UPS_Worldwide_Express]
     )[0]
     payment_details_provided = (
         all((payload.shipment.paid_by, payload.shipment.payment_account_number))
         or (
             payload.shipment.paid_by == "SENDER"
             and payload.shipper.account_number != None
         )
         or (
             payload.shipment.paid_by == "RECIPIENT"
             and payload.recipient.account_number != None
         )
     )
     is_negotiated_rate = any(
         (payload.shipment.payment_account_number, payload.shipper.account_number)
     )
     service_options = [
         opt
         for opt in payload.shipment.options
         if opt.code in ServiceOption.__members__
     ]
     rating_options = [
         opt
         for opt in payload.shipment.options
         if opt.code in RatingOption.__members__
     ]
     return PRate.RateRequest(
         Request=Common.RequestType(
             RequestOption=payload.shipment.extra.get("RequestOption") or ["Rate"],
             SubVersion=None,
             TransactionReference=Common.TransactionReferenceType(
                 CustomerContext=", ".join(payload.shipment.references),
                 TransactionIdentifier=payload.shipment.extra.get(
                     "TransactionIdentifier"
                 ),
             ),
         ),
         PickupType=None,
         CustomerClassification=None,
         Shipment=PRate.ShipmentType(
             OriginRecordTransactionTimestamp=None,
             Shipper=PRate.ShipperType(
                 Name=payload.shipper.company_name,
                 ShipperNumber=payload.shipper.account_number,
                 Address=PRate.ShipAddressType(
                     AddressLine=payload.shipper.address_lines,
                     City=payload.shipper.city,
                     StateProvinceCode=payload.shipper.state_code,
                     PostalCode=payload.shipper.postal_code,
                     CountryCode=payload.shipper.country_code,
                 ),
             ),
             ShipTo=PRate.ShipToType(
                 Name=payload.recipient.company_name,
                 Address=PRate.ShipToAddressType(
                     AddressLine=payload.recipient.address_lines,
                     City=payload.recipient.city,
                     StateProvinceCode=payload.recipient.state_code,
                     PostalCode=payload.recipient.postal_code,
                     CountryCode=payload.recipient.country_code,
                     ResidentialAddressIndicator=None,
                 ),
             ),
             ShipFrom=(
                 lambda shipFrom: PRate.ShipFromType(
                     Name=shipFrom.company_name,
                     Address=PRate.ShipAddressType(
                         AddressLine=shipFrom.address_lines,
                         City=shipFrom.city,
                         StateProvinceCode=shipFrom.state_code,
                         PostalCode=shipFrom.postal_code,
                         CountryCode=shipFrom.country_code,
                     ),
                 )
             )(T.Party(**payload.shipment.extra.get("ShipFrom")))
             if "ShipFrom" in payload.shipment.extra
             else None,
             AlternateDeliveryAddress=(
                 lambda alternate: PRate.AlternateDeliveryAddressType(
                     Name=alternate.company_name,
                     Address=PRate.ShipAddressType(
                         AddressLine=alternate.address_lines,
                         City=alternate.city,
                         StateProvinceCode=alternate.state_code,
                         PostalCode=alternate.postal_code,
                         CountryCode=alternate.country_code,
                     ),
                 )
             )(T.Party(**payload.shipment.extra.get("AlternateDeliveryAddress")))
             if "AlternateDeliveryAddress" in payload.shipment.extra
             else None,
             ShipmentIndicationType=None,
             PaymentDetails=PRate.PaymentDetailsType(
                 ShipmentCharge=[
                     PRate.ShipmentChargeType(
                         Type=None,
                         BillShipper=PRate.BillShipperChargeType(
                             AccountNumber=payload.shipment.payment_account_number
                             or payload.shipper.account_number
                         )
                         if payload.shipment.paid_by == "SENDER"
                         else None,
                         BillReceiver=PRate.BillReceiverChargeType(
                             AccountNumber=payload.shipment.payment_account_number
                             or payload.recipient.account_number,
                             Address=PRate.BillReceiverAddressType(
                                 PostalCode=payload.recipient.postal_code
                             ),
                         )
                         if payload.shipment.paid_by == "RECIPIENT"
                         else None,
                         BillThirdParty=PRate.BillThirdPartyChargeType(
                             AccountNumber=payload.shipment.payment_account_number,
                             Address=PRate.BillReceiverAddressType(
                                 PostalCode=payload.shipment.extra.get(
                                     "payor_postal_code"
                                 )
                             ),
                         )
                         if payload.shipment.paid_by == "THIRD_PARTY"
                         else None,
                         ConsigneeBilledIndicator=None,
                     )
                 ],
                 SplitDutyVATIndicator=None,
             )
             if payment_details_provided
             else None,
             FRSPaymentInformation=None,
             FreightShipmentInformation=None,
             GoodsNotInFreeCirculationIndicator=None,
             Service=PRate.UOMCodeDescriptionType(
                 Code=service.value, Description=None
             ),
             NumOfPieces=payload.shipment.total_items,
             ShipmentTotalWeight=payload.shipment.total_weight,
             DocumentsOnlyIndicator="" if payload.shipment.is_document else None,
             Package=[
                 PRate.PackageType(
                     PackagingType=PRate.UOMCodeDescriptionType(
                         Code=RatingPackagingType[pkg.packaging_type or "BOX"].value,
                         Description=None,
                     ),
                     Dimensions=PRate.DimensionsType(
                         UnitOfMeasurement=PRate.UOMCodeDescriptionType(
                             Code=DimensionUnit[
                                 payload.shipment.dimension_unit
                             ].value,
                             Description=None,
                         ),
                         Length=pkg.length,
                         Width=pkg.width,
                         Height=pkg.height,
                     ),
                     DimWeight=pkg.extra.get("DimWeight"),
                     PackageWeight=PRate.PackageWeightType(
                         UnitOfMeasurement=PRate.UOMCodeDescriptionType(
                             Code=WeightUnit[payload.shipment.weight_unit].value,
                             Description=None,
                         ),
                         Weight=pkg.weight,
                     ),
                     Commodity=None,
                     PackageServiceOptions=None,
                     AdditionalHandlingIndicator=None,
                 )
                 for pkg in payload.shipment.items
             ],
             ShipmentServiceOptions=PRate.ShipmentServiceOptionsType(
                 SaturdayDeliveryIndicator=next(
                     (
                         ""
                         for o in service_options
                         if o.code == "SaturdayDeliveryIndicator"
                     ),
                     None,
                 ),
                 AccessPointCOD=(
                     lambda option: PRate.ShipmentServiceOptionsAccessPointCODType(
                         CurrencyCode=option.value.get("CurrencyCode"),
                         MonetaryValue=option.value.get("MonetaryValue"),
                     )
                     if option != None
                     else None
                 )(
                     next(
                         (o for o in service_options if o.code == "AccessPointCOD"),
                         None,
                     )
                 ),
                 DeliverToAddresseeOnlyIndicator=next(
                     (
                         ""
                         for o in service_options
                         if o.code == "DeliverToAddresseeOnlyIndicator"
                     ),
                     None,
                 ),
                 DirectDeliveryOnlyIndicator=next(
                     (
                         ""
                         for o in service_options
                         if o.code == "DirectDeliveryOnlyIndicator"
                     ),
                     None,
                 ),
                 COD=(
                     lambda option: PRate.CODType(
                         CODFundsCode=option.value.get("CODFundsCode"),
                         CODAmount=option.value.get("CODAmount"),
                     )
                     if option != None
                     else None
                 )(next((o for o in service_options if o.code == "COD"), None)),
                 DeliveryConfirmation=(
                     lambda option: PRate.DeliveryConfirmationType(
                         DCISType=option.value.get("DCISType")
                     )
                     if option != None
                     else None
                 )(
                     next(
                         (
                             o
                             for o in service_options
                             if o.code == "DeliveryConfirmation"
                         ),
                         None,
                     )
                 ),
                 ReturnOfDocumentIndicator=next(
                     (
                         ""
                         for o in service_options
                         if o.code == "ReturnOfDocumentIndicator"
                     ),
                     None,
                 ),
                 UPScarbonneutralIndicator=next(
                     (
                         ""
                         for o in service_options
                         if o.code == "UPScarbonneutralIndicator"
                     ),
                     None,
                 ),
                 CertificateOfOriginIndicator=next(
                     (
                         ""
                         for o in service_options
                         if o.code == "CertificateOfOriginIndicator"
                     ),
                     None,
                 ),
                 PickupOptions=(
                     lambda option: PRate.PickupOptionsType(
                         LiftGateAtPickupIndicator=option.value.get(
                             "LiftGateAtPickupIndicator"
                         ),
                         HoldForPickupIndicator=option.value.get(
                             "HoldForPickupIndicator"
                         ),
                     )
                     if option != None
                     else None
                 )(
                     next(
                         (o for o in service_options if o.code == "PickupOptions"),
                         None,
                     )
                 ),
                 DeliveryOptions=(
                     lambda option: PRate.DeliveryOptionsType(
                         LiftGateAtDeliveryIndicator=option.value.get(
                             "LiftGateAtDeliveryIndicator"
                         ),
                         DropOffAtUPSFacilityIndicator=option.value.get(
                             "DropOffAtUPSFacilityIndicator"
                         ),
                     )
                     if option != None
                     else None
                 )(
                     next(
                         (o for o in service_options if o.code == "DeliveryOptions"),
                         None,
                     )
                 ),
                 RestrictedArticles=None,
                 ShipperExportDeclarationIndicator=next(
                     (
                         ""
                         for o in service_options
                         if o.code == "ShipperExportDeclarationIndicator"
                     ),
                     None,
                 ),
                 CommercialInvoiceRemovalIndicator=next(
                     (
                         ""
                         for o in service_options
                         if o.code == "CommercialInvoiceRemovalIndicator"
                     ),
                     None,
                 ),
                 ImportControl=None,
                 ReturnService=None,
                 SDLShipmentIndicator=next(
                     (
                         ""
                         for o in service_options
                         if o.code == "SDLShipmentIndicator"
                     ),
                     None,
                 ),
                 EPRAIndicator=next(
                     ("" for o in service_options if o.code == "EPRAIndicator"), None
                 ),
             )
             if len(service_options) > 0
             else None,
             ShipmentRatingOptions=PRate.ShipmentRatingOptionsType(
                 NegotiatedRatesIndicator="" if is_negotiated_rate else None,
                 FRSShipmentIndicator=""
                 if any([o.code == "FRSShipmentIndicator" for o in rating_options])
                 else None,
                 RateChartIndicator=""
                 if any([o.code == "RateChartIndicator" for o in rating_options])
                 else None,
                 UserLevelDiscountIndicator=""
                 if (
                     any(
                         [
                             o.code == "UserLevelDiscountIndicator"
                             for o in rating_options
                         ]
                     )
                     and is_negotiated_rate
                 )
                 else None,
             )
             if len(rating_options) > 0 or is_negotiated_rate
             else None,
             InvoiceLineTotal=None,
             RatingMethodRequestedIndicator=None,
             TaxInformationIndicator=None,
             PromotionalDiscountInformation=None,
             DeliveryTimeInformation=None,
         ),
     )
Example #8
0
 def create_freight_rate_request(
     self, payload: T.ShipmentRequest
 ) -> Rate.FreightRateRequest:
     service = (
         [
             RatingServiceCode[svc]
             for svc in payload.shipment.services
             if svc in RatingServiceCode.__members__
         ]
         + [RatingServiceCode.UPS_Freight_LTL_Guaranteed]
     )[0]
     return Rate.FreightRateRequest(
         Request=Common.RequestType(
             TransactionReference=Common.TransactionReferenceType(
                 TransactionIdentifier="TransactionIdentifier"
             ),
             RequestOption=[1],
         ),
         ShipFrom=Rate.ShipFromType(
             Name=payload.shipper.company_name,
             Address=Rate.AddressType(
                 AddressLine=payload.shipper.address_lines,
                 City=payload.shipper.city,
                 PostalCode=payload.shipper.postal_code,
                 CountryCode=payload.shipper.country_code,
                 StateProvinceCode=payload.shipper.state_code
             ),
             AttentionName=payload.shipper.person_name,
         ),
         ShipTo=Rate.ShipToType(
             Name=payload.recipient.company_name,
             Address=Rate.AddressType(
                 AddressLine=payload.recipient.address_lines,
                 City=payload.recipient.city,
                 PostalCode=payload.recipient.postal_code,
                 CountryCode=payload.recipient.country_code,
                 StateProvinceCode=payload.recipient.state_code
             ),
             AttentionName=payload.recipient.person_name,
         ),
         PaymentInformation=Rate.PaymentInformationType(
             Payer=Rate.PayerType(
                 Name=payload.shipment.payment_country_code
                 or payload.shipper.country_code,
                 Address=Rate.AddressType(
                     City=payload.shipper.city,
                     PostalCode=payload.shipper.postal_code,
                     CountryCode=payload.shipper.country_code,
                     AddressLine=payload.shipper.address_lines,
                 ),
                 ShipperNumber=payload.shipper.account_number
                 or payload.shipment.payment_account_number,
             ),
             ShipmentBillingOption=Rate.RateCodeDescriptionType(Code=10),
         ),
         Service=Rate.RateCodeDescriptionType(Code=service.value, Description=None),
         HandlingUnitOne=Rate.HandlingUnitType(
             Quantity=1, Type=Rate.RateCodeDescriptionType(Code="SKD")
         ),
         ShipmentServiceOptions=Rate.ShipmentServiceOptionsType(
             PickupOptions=Rate.PickupOptionsType(WeekendPickupIndicator="")
         ),
         DensityEligibleIndicator="",
         AdjustedWeightIndicator="",
         HandlingUnitWeight=Rate.HandlingUnitWeightType(
             Value=1,
             UnitOfMeasurement=Rate.UnitOfMeasurementType(
                 Code=WeightUnit[payload.shipment.weight_unit].value
             ),
         ),
         PickupRequest=None,
         GFPOptions=Rate.OnCallInformationType(),
         TimeInTransitIndicator="",
         Commodity=[
             Rate.CommodityType(
                 Description=c.description or "...",
                 Weight=Rate.WeightType(
                     UnitOfMeasurement=Rate.UnitOfMeasurementType(
                         Code=WeightUnit[payload.shipment.weight_unit].value
                     ),
                     Value=c.weight,
                 ),
                 Dimensions=Rate.DimensionsType(
                     UnitOfMeasurement=Rate.UnitOfMeasurementType(
                         Code=DimensionUnit[payload.shipment.dimension_unit].value
                     ),
                     Width=c.width,
                     Height=c.height,
                     Length=c.length,
                 ),
                 NumberOfPieces=len(payload.shipment.items),
                 PackagingType=Rate.RateCodeDescriptionType(
                     Code=PackagingType[c.packaging_type or "BOX"].value,
                     Description=None,
                 ),
                 FreightClass=50,
             )
             for c in payload.shipment.items
         ],
     )