From b6d8ead9bb0725a072bc7e178d73120d904347f5 Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Mon, 25 Oct 2021 15:53:28 -0700 Subject: [PATCH] [IMP] delivery_ups_hibou: allow per-warehouse account, signature and insurance requirement --- delivery_ups_hibou/__manifest__.py | 3 +- delivery_ups_hibou/models/__init__.py | 1 + delivery_ups_hibou/models/delivery_ups.py | 71 +++++-- delivery_ups_hibou/models/stock.py | 9 + .../models/ups_request_patch.py | 189 +++++++++++++++++- delivery_ups_hibou/views/stock_views.xml | 15 ++ 6 files changed, 271 insertions(+), 17 deletions(-) create mode 100644 delivery_ups_hibou/models/stock.py create mode 100644 delivery_ups_hibou/views/stock_views.xml diff --git a/delivery_ups_hibou/__manifest__.py b/delivery_ups_hibou/__manifest__.py index 23a25160..9ccc78af 100644 --- a/delivery_ups_hibou/__manifest__.py +++ b/delivery_ups_hibou/__manifest__.py @@ -1,6 +1,6 @@ { 'name': 'Hibou UPS Shipping', - 'version': '11.0.1.1.0', + 'version': '11.0.1.2.0', 'category': 'Stock', 'author': "Hibou Corp.", 'license': 'OPL-1', @@ -10,6 +10,7 @@ 'delivery_hibou', ], 'data': [ + 'views/stock_views.xml', ], 'demo': [ ], diff --git a/delivery_ups_hibou/models/__init__.py b/delivery_ups_hibou/models/__init__.py index 102ebacd..ddfac4e5 100644 --- a/delivery_ups_hibou/models/__init__.py +++ b/delivery_ups_hibou/models/__init__.py @@ -1,4 +1,5 @@ # Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. from . import delivery_ups +from . import stock from . import ups_request_patch diff --git a/delivery_ups_hibou/models/delivery_ups.py b/delivery_ups_hibou/models/delivery_ups.py index c2e12a36..d8b046ea 100644 --- a/delivery_ups_hibou/models/delivery_ups.py +++ b/delivery_ups_hibou/models/delivery_ups.py @@ -12,6 +12,15 @@ _logger = logging.getLogger(__name__) class ProviderUPS(models.Model): _inherit = 'delivery.carrier' + def _get_ups_signature_required_type(self): + # '3' for Adult Sig. + return '2' + + def _get_ups_signature_required(self, order=None, picking=None): + if self.get_signature_required(order=order, picking=picking): + return self._get_ups_signature_required_type() + return False + def _get_ups_is_third_party(self, order=None, picking=None): third_party_account = self.get_third_party_account(order=order, picking=picking) if third_party_account: @@ -22,6 +31,16 @@ class ProviderUPS(models.Model): return True return False + def _get_main_ups_account_number(self, order=None, picking=None): + wh = None + if order: + wh = order.warehouse_id + if picking: + wh = picking.picking_type_id.warehouse_id + if wh and wh.ups_shipper_number: + return wh.ups_shipper_number + return self.ups_shipper_number + def _get_ups_account_number(self, order=None, picking=None): """ Common hook to customize what UPS Account number to use. @@ -35,6 +54,8 @@ class ProviderUPS(models.Model): return third_party_account.name if order and order.ups_carrier_account: return order.ups_carrier_account + if picking and picking.picking_type_id.warehouse_id.ups_shipper_number: + return picking.picking_type_id.warehouse_id.ups_shipper_number if picking and picking.sale_id.ups_carrier_account: return picking.sale_id.ups_carrier_account return self.ups_shipper_number @@ -42,14 +63,20 @@ class ProviderUPS(models.Model): def _get_ups_carrier_account(self, picking): # 3rd party billing should return False if not used. account = self._get_ups_account_number(picking=picking) - return account if account != self.ups_shipper_number else False + return account if account not in (self.ups_shipper_number, picking.picking_type_id.warehouse_id.ups_shipper_number) else False """ Overrides to use Hibou Delivery methods to get shipper etc. and to add 'transit_days' to result. """ def ups_rate_shipment(self, order): superself = self.sudo() - srm = UPSRequest(self.log_xml, superself.ups_username, superself.ups_passwd, superself.ups_shipper_number, superself.ups_access_number, self.prod_environment) + # Hibou Shipping + signature_required = superself._get_ups_signature_required(order=order) + insurance_value = superself.get_insurance_value(order=order) + currency = order.currency_id or order.company_id.currency_id + insurance_currency_code = currency.name + + srm = UPSRequest(self.log_xml, superself.ups_username, superself.ups_passwd, superself._get_main_ups_account_number(order=order), superself.ups_access_number, self.prod_environment) ResCurrency = self.env['res.currency'] max_weight = self.ups_default_packaging_id.max_weight packages = [] @@ -64,11 +91,14 @@ class ProviderUPS(models.Model): last_package_weight = total_weight % max_weight for seq in range(total_package): - packages.append(Package(self, max_weight)) + packages.append(Package(self, max_weight, + insurance_value=insurance_value, insurance_currency_code=insurance_currency_code, signature_required=signature_required)) if last_package_weight: - packages.append(Package(self, last_package_weight)) + packages.append(Package(self, last_package_weight, + insurance_value=insurance_value, insurance_currency_code=insurance_currency_code, signature_required=signature_required)) else: - packages.append(Package(self, total_weight)) + packages.append(Package(self, total_weight, + insurance_value=insurance_value, insurance_currency_code=insurance_currency_code, signature_required=signature_required)) shipment_info = { 'total_qty': total_qty # required when service type = 'UPS Worldwide Express Freight' @@ -131,13 +161,17 @@ class ProviderUPS(models.Model): def ups_send_shipping(self, pickings): res = [] superself = self.sudo() - srm = UPSRequest(self.log_xml, superself.ups_username, superself.ups_passwd, superself.ups_shipper_number, superself.ups_access_number, self.prod_environment) ResCurrency = self.env['res.currency'] for picking in pickings: + srm = UPSRequest(self.log_xml, superself.ups_username, superself.ups_passwd, superself._get_main_ups_account_number(picking=picking), superself.ups_access_number, self.prod_environment) # Hibou Delivery shipper_company = superself.get_shipper_company(picking=picking) shipper_warehouse = superself.get_shipper_warehouse(picking=picking) recipient = superself.get_recipient(picking=picking) + signature_required = superself._get_ups_signature_required(picking=picking) + insurance_value = superself.get_insurance_value(picking=picking) + currency = picking.sale_id.currency_id if picking.sale_id else picking.company_id.currency_id + insurance_currency_code = currency.name picking_packages = picking.package_ids package_carriers = picking_packages.mapped('carrier_id') @@ -153,7 +187,8 @@ class ProviderUPS(models.Model): if picking_packages: # Create all packages for package in picking_packages: - packages.append(Package(self, package.shipping_weight, quant_pack=package.packaging_id, name=package.name)) + packages.append(Package(self, package.shipping_weight, quant_pack=package.packaging_id, name=package.name, + insurance_value=insurance_value, insurance_currency_code=insurance_currency_code, signature_required=signature_required)) package_names.append(package.name) # what is the point of weight_bulk? @@ -244,12 +279,16 @@ class ProviderUPS(models.Model): def _ups_rate_shipment_multi_package(self, order=None, picking=None, package=None): superself = self.sudo() - srm = UPSRequest(self.log_xml, superself.ups_username, superself.ups_passwd, superself.ups_shipper_number, superself.ups_access_number, self.prod_environment) + srm = UPSRequest(self.log_xml, superself.ups_username, superself.ups_passwd, superself._get_main_ups_account_number(order=order, picking=picking), superself.ups_access_number, self.prod_environment) ResCurrency = self.env['res.currency'] max_weight = self.ups_default_packaging_id.max_weight + insurance_value = superself.get_insurance_value(order=order, picking=picking) + insurance_currency_code = 'USD' # will be overridden below, here for consistency + signature_required = superself._get_ups_signature_required(order=order, picking=picking) packages = [] if order: currency = order.currency_id + insurance_currency_code = currency.name company = order.company_id date_order = order.date_order or fields.Date.today() total_qty = 0 @@ -263,23 +302,27 @@ class ProviderUPS(models.Model): last_package_weight = total_weight % max_weight for seq in range(total_package): - packages.append(Package(self, max_weight)) + packages.append(Package(self, max_weight, insurance_value=insurance_value, insurance_currency_code=insurance_currency_code, signature_required=signature_required)) if last_package_weight: - packages.append(Package(self, last_package_weight)) + packages.append(Package(self, last_package_weight, insurance_value=insurance_value, insurance_currency_code=insurance_currency_code, signature_required=signature_required)) else: - packages.append(Package(self, total_weight)) + packages.append(Package(self, total_weight, insurance_value=insurance_value, insurance_currency_code=insurance_currency_code, signature_required=signature_required)) else: currency = picking.sale_id.currency_id if picking.sale_id else picking.company_id.currency_id + insurance_currency_code = currency.name company = picking.company_id date_order = picking.sale_id.date_order or fields.Date.today() if picking.sale_id else fields.Date.today() # Is total quantity the number of packages or the number of items being shipped? if package: total_qty = 1 - packages = [Package(self, package.shipping_weight)] - else: + packages = [Package(self, package.shipping_weight, insurance_value=insurance_value, insurance_currency_code=insurance_currency_code, signature_required=signature_required)] + elif picking.package_ids: # all packages.... total_qty = len(picking.package_ids) - packages = [Package(self, package.shipping_weight) for package in picking.package_ids.filtered(lambda p: not p.carrier_id)] + packages = [Package(self, package.shipping_weight, insurance_value=insurance_value, insurance_currency_code=insurance_currency_code, signature_required=signature_required) for package in picking.package_ids.filtered(lambda p: not p.carrier_id)] + else: + total_qty = 1 + packages.append(Package(self, picking.shipping_weight or picking.weight, insurance_value=insurance_value, insurance_currency_code=insurance_currency_code, signature_required=signature_required)) shipment_info = { 'total_qty': total_qty # required when service type = 'UPS Worldwide Express Freight' diff --git a/delivery_ups_hibou/models/stock.py b/delivery_ups_hibou/models/stock.py new file mode 100644 index 00000000..2c06bfed --- /dev/null +++ b/delivery_ups_hibou/models/stock.py @@ -0,0 +1,9 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + +from odoo import fields, models + + +class StockWarehouse(models.Model): + _inherit = 'stock.warehouse' + + ups_shipper_number = fields.Char(string='UPS Shipper Number') diff --git a/delivery_ups_hibou/models/ups_request_patch.py b/delivery_ups_hibou/models/ups_request_patch.py index 336f6a9f..df7de1c5 100644 --- a/delivery_ups_hibou/models/ups_request_patch.py +++ b/delivery_ups_hibou/models/ups_request_patch.py @@ -2,7 +2,7 @@ from os import path as os_path import suds -from odoo.addons.delivery_ups.models.ups_request import UPSRequest +from odoo.addons.delivery_ups.models.ups_request import UPSRequest, Package SUDS_VERSION = suds.__version__ @@ -107,7 +107,7 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from else: shipment.ShipmentServiceOptions = '' - shipment.ShipmentRatingOptions.NegotiatedRatesIndicator = 1 + shipment.ShipmentRatingOptions.NegotiatedRatesIndicator = '1' try: # Get rate using for provided detail @@ -205,5 +205,190 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from except IOError as e: return self.get_error_message('0', 'UPS Server Not Found:\n%s' % e) + +def patched_send_shipping(self, shipment_info, packages, shipper, ship_from, ship_to, packaging_type, service_type, saturday_delivery, cod_info=None, label_file_type='GIF', ups_carrier_account=False): + client = self._set_client(self.ship_wsdl, 'Ship', 'ShipmentRequest') + + request = client.factory.create('ns0:RequestType') + request.RequestOption = 'nonvalidate' + + namespace = 'ns3' + label = client.factory.create('{}:LabelSpecificationType'.format(namespace)) + + label.LabelImageFormat.Code = label_file_type + label.LabelImageFormat.Description = label_file_type + if label_file_type != 'GIF': + label.LabelStockSize.Height = '6' + label.LabelStockSize.Width = '4' + + shipment = client.factory.create('{}:ShipmentType'.format(namespace)) + shipment.Description = shipment_info.get('description') + + for package in self.set_package_detail(client, packages, packaging_type, namespace, ship_from, ship_to, cod_info): + shipment.Package.append(package) + + shipment.Shipper.AttentionName = shipper.name or '' + shipment.Shipper.Name = shipper.parent_id.name or shipper.name or '' + shipment.Shipper.Address.AddressLine = [l for l in [shipper.street or '', shipper.street2 or ''] if l] + shipment.Shipper.Address.City = shipper.city or '' + shipment.Shipper.Address.PostalCode = shipper.zip or '' + shipment.Shipper.Address.CountryCode = shipper.country_id.code or '' + if shipper.country_id.code in ('US', 'CA', 'IE'): + shipment.Shipper.Address.StateProvinceCode = shipper.state_id.code or '' + shipment.Shipper.ShipperNumber = self.shipper_number or '' + shipment.Shipper.Phone.Number = self._clean_phone_number(shipper.phone) + + shipment.ShipFrom.AttentionName = ship_from.name or '' + shipment.ShipFrom.Name = ship_from.parent_id.name or ship_from.name or '' + shipment.ShipFrom.Address.AddressLine = [l for l in [ship_from.street or '', ship_from.street2 or ''] if l] + shipment.ShipFrom.Address.City = ship_from.city or '' + shipment.ShipFrom.Address.PostalCode = ship_from.zip or '' + shipment.ShipFrom.Address.CountryCode = ship_from.country_id.code or '' + if ship_from.country_id.code in ('US', 'CA', 'IE'): + shipment.ShipFrom.Address.StateProvinceCode = ship_from.state_id.code or '' + shipment.ShipFrom.Phone.Number = self._clean_phone_number(ship_from.phone) + + shipment.ShipTo.AttentionName = ship_to.name or '' + shipment.ShipTo.Name = ship_to.parent_id.name or ship_to.name or '' + shipment.ShipTo.Address.AddressLine = [l for l in [ship_to.street or '', ship_to.street2 or ''] if l] + shipment.ShipTo.Address.City = ship_to.city or '' + shipment.ShipTo.Address.PostalCode = ship_to.zip or '' + shipment.ShipTo.Address.CountryCode = ship_to.country_id.code or '' + if ship_to.country_id.code in ('US', 'CA', 'IE'): + shipment.ShipTo.Address.StateProvinceCode = ship_to.state_id.code or '' + shipment.ShipTo.Phone.Number = self._clean_phone_number(shipment_info['phone']) + if not ship_to.commercial_partner_id.is_company: + shipment.ShipTo.Address.ResidentialAddressIndicator = suds.null() + + shipment.Service.Code = service_type or '' + shipment.Service.Description = 'Service Code' + if service_type == "96": + shipment.NumOfPiecesInShipment = int(shipment_info.get('total_qty')) + shipment.ShipmentRatingOptions.NegotiatedRatesIndicator = 1 + + # Shipments from US to CA or PR require extra info + if ship_from.country_id.code == 'US' and ship_to.country_id.code in ['CA', 'PR']: + shipment.InvoiceLineTotal.CurrencyCode = shipment_info.get('itl_currency_code') + shipment.InvoiceLineTotal.MonetaryValue = shipment_info.get('ilt_monetary_value') + + # set the default method for payment using shipper account + payment_info = client.factory.create('ns3:PaymentInformation') + shipcharge = client.factory.create('ns3:ShipmentCharge') + shipcharge.Type = '01' + + # Bill Recevier 'Bill My Account' + if ups_carrier_account: + shipcharge.BillReceiver.AccountNumber = ups_carrier_account + shipcharge.BillReceiver.Address.PostalCode = ship_to.zip + else: + shipcharge.BillShipper.AccountNumber = self.shipper_number or '' + + payment_info.ShipmentCharge = shipcharge + shipment.PaymentInformation = payment_info + + if saturday_delivery: + shipment.ShipmentServiceOptions.SaturdayDeliveryIndicator = saturday_delivery + else: + shipment.ShipmentServiceOptions = '' + + try: + response = client.service.ProcessShipment( + Request=request, Shipment=shipment, + LabelSpecification=label) + + # Check if shipment is not success then return reason for that + if response.Response.ResponseStatus.Code != "1": + return self.get_error_message(response.Response.ResponseStatus.Code, response.Response.ResponseStatus.Description) + + result = {} + result['label_binary_data'] = {} + for package in response.ShipmentResults.PackageResults: + result['label_binary_data'][package.TrackingNumber] = self.save_label(package.ShippingLabel.GraphicImage, label_file_type=label_file_type) + result['tracking_ref'] = response.ShipmentResults.ShipmentIdentificationNumber + result['currency_code'] = response.ShipmentResults.ShipmentCharges.TotalCharges.CurrencyCode + + # Some users are qualified to receive negotiated rates + negotiated_rate = 'NegotiatedRateCharges' in response.ShipmentResults and response.ShipmentResults.NegotiatedRateCharges.TotalCharge.MonetaryValue or None + + result['price'] = negotiated_rate or response.ShipmentResults.ShipmentCharges.TotalCharges.MonetaryValue + return result + + except suds.WebFault as e: + # childAtPath behaviour is changing at version 0.6 + prefix = '' + if SUDS_VERSION >= "0.6": + prefix = '/Envelope/Body/Fault' + return self.get_error_message(e.document.childAtPath(prefix + '/detail/Errors/ErrorDetail/PrimaryErrorCode/Code').getText(), + e.document.childAtPath(prefix + '/detail/Errors/ErrorDetail/PrimaryErrorCode/Description').getText()) + except IOError as e: + return self.get_error_message('0', 'UPS Server Not Found:\n%s' % e) + + +def patched_set_package_detail(self, client, packages, packaging_type, namespace, ship_from, ship_to, cod_info): + Packages = [] + for i, p in enumerate(packages): + package = client.factory.create('{}:PackageType'.format(namespace)) + if hasattr(package, 'Packaging'): + package.Packaging.Code = p.packaging_type or packaging_type or '' + elif hasattr(package, 'PackagingType'): + package.PackagingType.Code = p.packaging_type or packaging_type or '' + + # Hibou Insurance & Signature Requirement + if p.insurance_value: + package.PackageServiceOptions.DeclaredValue.MonetaryValue = p.insurance_value + package.PackageServiceOptions.DeclaredValue.CurrencyCode = p.insurance_currency_code + if p.signature_required: + package.PackageServiceOptions.DeliveryConfirmation.DCISType = p.signature_required + + if p.dimension_unit and any(p.dimension.values()): + package.Dimensions.UnitOfMeasurement.Code = p.dimension_unit or '' + package.Dimensions.Length = p.dimension['length'] or '' + package.Dimensions.Width = p.dimension['width'] or '' + package.Dimensions.Height = p.dimension['height'] or '' + + if cod_info: + package.PackageServiceOptions.COD.CODFundsCode = str(cod_info['funds_code']) + package.PackageServiceOptions.COD.CODAmount.MonetaryValue = cod_info['monetary_value'] + package.PackageServiceOptions.COD.CODAmount.CurrencyCode = cod_info['currency'] + + package.PackageWeight.UnitOfMeasurement.Code = p.weight_unit or '' + package.PackageWeight.Weight = p.weight or '' + + # Package and shipment reference text is only allowed for shipments within + # the USA and within Puerto Rico. This is a UPS limitation. + if (p.name and ship_from.country_id.code in ('US') and ship_to.country_id.code in ('US')): + reference_number = client.factory.create('ns3:ReferenceNumberType') + reference_number.Code = 'PM' + reference_number.Value = p.name + reference_number.BarCodeIndicator = p.name + package.ReferenceNumber = reference_number + + Packages.append(package) + return Packages + + UPSRequest.__init__ = patched__init__ UPSRequest.get_shipping_price = patched_get_shipping_price +UPSRequest.send_shipping = patched_send_shipping +UPSRequest.set_package_detail = patched_set_package_detail + + +def patched__init__2(self, carrier, weight, quant_pack=False, name='', + insurance_value=False, insurance_currency_code=False, signature_required=False): + self.weight = self._convert_weight(weight, carrier.ups_package_weight_unit) + self.weight_unit = carrier.ups_package_weight_unit + self.name = name + self.dimension_unit = carrier.ups_package_dimension_unit + if quant_pack: + self.dimension = {'length': quant_pack.length, 'width': quant_pack.width, 'height': quant_pack.height} + else: + self.dimension = {'length': carrier.ups_default_packaging_id.length, + 'width': carrier.ups_default_packaging_id.width, + 'height': carrier.ups_default_packaging_id.height} + self.packaging_type = quant_pack and quant_pack.shipper_package_code or False + self.insurance_value = insurance_value + self.insurance_currency_code = insurance_currency_code + self.signature_required = signature_required + + +Package.__init__ = patched__init__2 diff --git a/delivery_ups_hibou/views/stock_views.xml b/delivery_ups_hibou/views/stock_views.xml new file mode 100644 index 00000000..e74ebd99 --- /dev/null +++ b/delivery_ups_hibou/views/stock_views.xml @@ -0,0 +1,15 @@ + + + + + stock.warehouse + stock.warehouse + + + + + + + + +