Esempio n. 1
0
    def _get_ups_packages(self):
        """
        Return UPS Packages XML
        """
        UPSConfiguration = Pool().get('ups.configuration')

        ups_config = UPSConfiguration(1)

        package_type = RatingService.packaging_type(
            Code=self.ups_package_type
        )

        weight = self._get_package_weight(ups_config.weight_uom).quantize(
            Decimal('.01'), rounding=ROUND_UP
        )
        package_weight = RatingService.package_weight_type(
            Weight=str(weight),
            Code=ups_config.weight_uom_code,
        )
        package_service_options = RatingService.package_service_options_type(
            RatingService.insured_value_type(MonetaryValue='0')
        )
        package_container = RatingService.package_type(
            package_type,
            package_weight,
            package_service_options
        )
        return [package_container]
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)
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. 4
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. 5
0
 def setUp(self):
     """Initialise a ShipmentConfirm and ShipmentAccept class each.
     """
     logging.disable(logging.DEBUG)
     self.shipper_number = os.environ['UPS_SHIPPER_NO']
     self.rating_api = RatingService(
         os.environ['UPS_LICENSE_NO'],
         os.environ['UPS_USER_ID'],
         os.environ['UPS_PASSWORD'],
         True            # Test must be performed in sandbox anyway
     )
Esempio n. 6
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)
def get_rating_service(params):
    return RatingService(
        params.get("ups_license"),
        params.get("ups_user_name"),
        params.get("ups_password"),
        params.get("ups_mode"),                        # sandbox for testing purpose set as True for production set it to False
    )
Esempio n. 8
0
    def _get_ups_packages(self):
        """
        Return UPS Packages XML
        """
        carrier = self.carrier

        package_type = RatingService.packaging_type(Code=self.ups_package_type)

        package_weight = RatingService.package_weight_type(
            Weight="%.2f" % self._get_package_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")
        )
        package_container = RatingService.package_type(package_type, package_weight, package_service_options)
        return [package_container]
Esempio n. 9
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. 10
0
    def _get_ups_packages(self):
        """
        Return UPS Packages XML
        """
        carrier = self.carrier

        package_type = RatingService.packaging_type(Code=self.ups_package_type)

        package_weight = RatingService.package_weight_type(
            Weight="%.2f" % self._get_package_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'))
        package_container = RatingService.package_type(
            package_type, package_weight, package_service_options)
        return [package_container]
Esempio n. 11
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. 12
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. 13
0
    def _get_ups_packages(self):
        """
        Return UPS Packages XML
        """
        UPSConfiguration = Pool().get('ups.configuration')

        ups_config = UPSConfiguration(1)

        package_type = RatingService.packaging_type(Code=self.ups_package_type)

        weight = self._get_package_weight(ups_config.weight_uom).quantize(
            Decimal('.01'), rounding=ROUND_UP)
        package_weight = RatingService.package_weight_type(
            Weight=str(weight),
            Code=ups_config.weight_uom_code,
        )
        package_service_options = RatingService.package_service_options_type(
            RatingService.insured_value_type(MonetaryValue='0'))
        package_container = RatingService.package_type(
            package_type, package_weight, package_service_options)
        return [package_container]
Esempio n. 14
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)
Esempio n. 15
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. 16
0
    def get_ups_package_container_rate(self):
        """
        Return UPS package container for a single package
        """
        Uom = Pool().get('product.uom')

        shipment = self.shipment
        carrier = shipment.carrier

        if self.box_type:
            code = self.box_type.code
            length = self.box_type.length
            height = self.box_type.height
            width = self.box_type.width
            dimensions_symbol = self.box_type.distance_unit and \
                self.box_type.distance_unit.symbol.upper()
        else:
            code = '02'
            length = self.length
            height = self.height
            width = self.width
            dimensions_symbol = self.distance_unit and \
                self.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 the box type is 'Customer Supplied Package'
        if code == '02' and 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)

        package_container = RatingService.package_type(*args)
        return package_container
Esempio n. 17
0
class TestRatingPackage(unittest.TestCase):
    """
    Test the :class:`RatingService`
    """

    @classmethod
    def setUpClass(self):
        """Check if the variables for initialising the test case is available
        in the environment"""
        assert 'UPS_LICENSE_NO' in os.environ, \
            "UPS_LICENSE_NO not given. Hint:Use export UPS_LICENSE_NO=<number>"
        assert 'UPS_SHIPPER_NO' in os.environ, \
            "UPS_SHIPPER_NO not given. Hint:Use export UPS_SHIPPER_NO=<number>"
        assert 'UPS_USER_ID' in os.environ, \
            "UPS_USER_ID not given. Hint:Use export UPS_USER_ID=<user_id>"
        assert 'UPS_PASSWORD' in os.environ, \
            "UPS_PASSWORD not given. Hint:Use export UPS_PASSWORD=<password>"

    def setUp(self):
        """Initialise a ShipmentConfirm and ShipmentAccept class each.
        """
        logging.disable(logging.DEBUG)
        self.shipper_number = os.environ['UPS_SHIPPER_NO']
        self.rating_api = RatingService(
            os.environ['UPS_LICENSE_NO'],
            os.environ['UPS_USER_ID'],
            os.environ['UPS_PASSWORD'],
            True            # Test must be performed in sandbox anyway
        )

    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)

    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)

    @unittest.expectedFailure
    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)