def get_ups_rating_request(delivery_note, params):
    # prepate the ups rating request
    dn = delivery_note

    packing_slips = [row.packing_slip for row in dn.packing_slip_details]
    if not packing_slips:
        frappe.throw("Can not find the linked Packing Slip ...")

    ship_from_address_name = params.get("default_warehouse")
    shipper_number = params.get("shipper_number")
    package_type = ups_packages.get(params.get("package_type"))
    ship_to_params = {
        "customer":dn.customer,
        "contact_display":dn.contact_display,
        "contact_mobile":dn.contact_mobile
    }

    shipment = E.Shipment(
                    Helper.get_shipper(params),
                    Helper.get_ship_to_address(ship_to_params, dn.shipping_address_name,),
                    Helper.get_ship_from_address(params, ship_from_address_name),
                    RatingService.service_type(Code='03'),
                )
    packages = Helper.get_packages(packing_slips, package_type)
    shipment.extend(packages)

    rating_request = RatingService.rating_request_type(
        shipment,
    )

    return rating_request
Esempio n. 2
0
    def test_negotiated_rate_fetching(self):
        """
        Test the rate fetching with negotiated rates.
        This will fail if your shipper number is not eligible for negotaited
        rates
        """
        rating_request = RatingService.rating_request_type(
            E.Shipment(
                Helper.get_shipper(self.shipper_number, "US"),
                Helper.get_ship_to("US"),
                Helper.get_ship_from("US"),
                RatingService.service_type(Code='03'),    # UPS Ground
                Helper.get_package("US", package_type_code="00"),
                RatingService.rate_information_type(negotiated=True)
            ),
        )
        response = self.rating_api.request(rating_request)

        self.assertTrue(
            hasattr(response.RatedShipment, 'NegotiatedRates')
        )
        self.assertTrue(
            response.RatedShipment.RatedPackage.TotalCharges.MonetaryValue
        )
        print etree.tostring(response, pretty_print=True)
Esempio n. 3
0
    def _get_rate_request_xml(self, carrier, carrier_service):

        packages = self._get_ups_packages_rate()

        from_address = self._get_ship_from_address()

        shipment_args = [
            from_address.to_ups_shipper(carrier=carrier),  # Shipper
            self.delivery_address.to_ups_to_address(),      # Ship to
            from_address.to_ups_from_address(),   # Ship from

        ]
        shipment_args.extend(packages)
        if carrier.ups_negotiated_rates:
            shipment_args.append(
                RatingService.rate_information_type(negotiated=True)
            )

        if carrier_service:
            # TODO: handle ups_saturday_delivery
            shipment_args.append(
                RatingService.service_type(Code=self.carrier_service.code)
            )
            request_option = E.RequestOption('Rate')
        else:
            request_option = E.RequestOption('Shop')

        return RatingService.rating_request_type(
            E.Shipment(*shipment_args), RequestOption=request_option
        )
Esempio n. 4
0
    def _get_rate_request_xml(self, carrier, carrier_service):
        SaleConfiguration = Pool().get("sale.configuration")
        Uom = Pool().get("product.uom")
        config = SaleConfiguration(1)

        code = length = width = height = dimensions_symbol = None
        box_type = config.ups_box_type
        if box_type:
            code = box_type.code
            length = box_type.length
            height = box_type.height
            width = box_type.width
            dimensions_symbol = box_type.distance_unit and box_type.distance_unit.symbol.upper()

        package_type = RatingService.packaging_type(Code=code)

        package_weight = RatingService.package_weight_type(
            Weight="%.2f" % Uom.compute_qty(self.weight_uom, self.weight, carrier.ups_weight_uom),
            Code=carrier.ups_weight_uom_code,
        )
        package_service_options = RatingService.package_service_options_type(
            RatingService.insured_value_type(MonetaryValue="0")
        )

        args = [package_type, package_weight, package_service_options]

        # Only send dimensions if box type has all information
        if length and width and height and dimensions_symbol:
            package_dimensions = RatingService.dimensions_type(
                Code=dimensions_symbol, Length=str(length), Width=str(width), Height=str(height)
            )
            args.append(package_dimensions)

        shipment_args = [RatingService.package_type(*args)]

        from_address = self._get_ship_from_address()

        shipment_args.extend(
            [
                from_address.to_ups_shipper(carrier=carrier),  # Shipper
                self.shipment_address.to_ups_to_address(),  # Ship to
                from_address.to_ups_from_address(),  # Ship from
            ]
        )

        if carrier.ups_negotiated_rates:
            shipment_args.append(RatingService.rate_information_type(negotiated=True))

        if carrier_service:
            # TODO: handle ups_saturday_delivery
            shipment_args.append(RatingService.service_type(Code=carrier_service.code))
            request_option = E.RequestOption("Rate")
        else:
            request_option = E.RequestOption("Shop")

        return RatingService.rating_request_type(E.Shipment(*shipment_args), RequestOption=request_option)
Esempio n. 5
0
    def test_rate_fetching(self):
        "Test the rate fetching"
        rating_request = RatingService.rating_request_type(
            E.Shipment(
                Helper.get_shipper(self.shipper_number, "US"),
                Helper.get_ship_to("US"),
                Helper.get_ship_from("US"),
                RatingService.service_type(Code='03'),    # UPS Ground
                Helper.get_package("US", package_type_code="00")
            ),
        )
        response = self.rating_api.request(rating_request)

        self.assertTrue(
            response.RatedShipment.RatedPackage.TotalCharges.MonetaryValue
        )
        print etree.tostring(response, pretty_print=True)
Esempio n. 6
0
    def _get_rate_request_xml(self, mode='rate'):
        """
        Return the E builder object with the rate fetching request

        :param mode: 'rate' - to fetch rate of current shipment and selected
                              package type
                     'shop' - to get a rates list
        """
        UPSConfiguration = Pool().get('ups.configuration')

        ups_config = UPSConfiguration(1)

        assert mode in ('rate', 'shop'), "Mode should be 'rate' or 'shop'"

        if mode == 'rate' and not self.ups_service_type:
            self.raise_user_error('ups_service_type_missing')

        shipment_args = self._get_ups_packages()

        shipment_args.extend([
            self.warehouse.address.to_ups_shipper(),        # Shipper
            self.shipment_address.to_ups_to_address(),      # Ship to
            self._get_ship_from_address().to_ups_from_address(),   # Ship from

        ])

        if ups_config.negotiated_rates:
            shipment_args.append(
                RatingService.rate_information_type(negotiated=True)
            )

        if mode == 'rate':
            # TODO: handle ups_saturday_delivery
            shipment_args.append(
                RatingService.service_type(Code=self.ups_service_type.code)
            )
            request_option = E.RequestOption('Rate')
        else:
            request_option = E.RequestOption('Shop')

        return RatingService.rating_request_type(
            E.Shipment(*shipment_args), RequestOption=request_option
        )
Esempio n. 7
0
    def _get_rate_request_xml(self, mode='rate'):
        """
        Return the E builder object with the rate fetching request

        :param mode: 'rate' - to fetch rate of current shipment and selected
                              package type
                     'shop' - to get a rates list
        """
        carrier = self.carrier

        assert mode in ('rate', 'shop'), "Mode should be 'rate' or 'shop'"

        if mode == 'rate' and not self.ups_service_type:
            self.raise_user_error('ups_service_type_missing')

        shipment_args = self._get_ups_packages()

        from_address = self._get_ship_from_address()

        shipment_args.extend([
            from_address.to_ups_shipper(carrier=carrier),  # Shipper
            self.shipment_address.to_ups_to_address(),      # Ship to
            from_address.to_ups_from_address(),   # Ship from

        ])

        if carrier.ups_negotiated_rates:
            shipment_args.append(
                RatingService.rate_information_type(negotiated=True)
            )

        if mode == 'rate':
            # TODO: handle ups_saturday_delivery
            shipment_args.append(
                RatingService.service_type(Code=self.ups_service_type.code)
            )
            request_option = E.RequestOption('Rate')
        else:
            request_option = E.RequestOption('Shop')

        return RatingService.rating_request_type(
            E.Shipment(*shipment_args), RequestOption=request_option
        )
Esempio n. 8
0
    def test_rate_chart_fetching(self):
        "Test the rate fetching"
        rating_request = RatingService.rating_request_type(
            E.Shipment(
                Helper.get_shipper(self.shipper_number, "US"),
                Helper.get_ship_to("US"),
                Helper.get_ship_from("US"),
                Helper.get_package("US", package_type_code="00"),
            ),
        )
        response = self.rating_api.request(rating_request)

        self.assertTrue(
            len([s for s in response.iterchildren(tag='RatedShipment')]) > 1
        )

        self.assertTrue(
            response.RatedShipment.RatedPackage.TotalCharges.MonetaryValue
        )
        print etree.tostring(response, pretty_print=True)
Esempio n. 9
0
    def _get_rate_request_xml(self, mode="rate"):
        """
        Return the E builder object with the rate fetching request

        :param mode: 'rate' - to fetch rate of current shipment and selected
                              package type
                     'shop' - to get a rates list
        """
        carrier = self.carrier

        assert mode in ("rate", "shop"), "Mode should be 'rate' or 'shop'"

        if mode == "rate" and not self.ups_service_type:
            self.raise_user_error("ups_service_type_missing")

        shipment_args = self._get_ups_packages()

        shipment_args.extend(
            [
                self.warehouse.address.to_ups_shipper(carrier=carrier),  # Shipper
                self.shipment_address.to_ups_to_address(),  # Ship to
                self._get_ship_from_address().to_ups_from_address(),  # Ship from
            ]
        )

        if carrier.ups_negotiated_rates:
            shipment_args.append(RatingService.rate_information_type(negotiated=True))

        if mode == "rate":
            # TODO: handle ups_saturday_delivery
            shipment_args.append(RatingService.service_type(Code=self.ups_service_type.code))
            request_option = E.RequestOption("Rate")
        else:
            request_option = E.RequestOption("Shop")

        return RatingService.rating_request_type(E.Shipment(*shipment_args), RequestOption=request_option)