From a6cd94d4aa89941aaacd0428f9f28b47b6467345 Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Thu, 29 Oct 2020 14:15:20 -0700 Subject: [PATCH 1/7] [MOV] delivery_ups_hibou: from hibou-suite-enterprise:12.0 --- delivery_ups_hibou/__init__.py | 1 + delivery_ups_hibou/__manifest__.py | 18 ++ delivery_ups_hibou/models/__init__.py | 2 + delivery_ups_hibou/models/delivery_ups.py | 218 ++++++++++++++++++ .../models/ups_request_patch.py | 112 +++++++++ 5 files changed, 351 insertions(+) create mode 100644 delivery_ups_hibou/__init__.py create mode 100644 delivery_ups_hibou/__manifest__.py create mode 100644 delivery_ups_hibou/models/__init__.py create mode 100644 delivery_ups_hibou/models/delivery_ups.py create mode 100644 delivery_ups_hibou/models/ups_request_patch.py diff --git a/delivery_ups_hibou/__init__.py b/delivery_ups_hibou/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/delivery_ups_hibou/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/delivery_ups_hibou/__manifest__.py b/delivery_ups_hibou/__manifest__.py new file mode 100644 index 00000000..67931660 --- /dev/null +++ b/delivery_ups_hibou/__manifest__.py @@ -0,0 +1,18 @@ +{ + 'name': 'Hibou UPS Shipping', + 'version': '12.0.1.0.0', + 'category': 'Stock', + 'author': "Hibou Corp.", + 'license': 'AGPL-3', + 'website': 'https://hibou.io/', + 'depends': [ + 'delivery_ups', + 'delivery_hibou', + ], + 'data': [ + ], + 'demo': [ + ], + 'installable': True, + 'application': False, + } diff --git a/delivery_ups_hibou/models/__init__.py b/delivery_ups_hibou/models/__init__.py new file mode 100644 index 00000000..735db17b --- /dev/null +++ b/delivery_ups_hibou/models/__init__.py @@ -0,0 +1,2 @@ +from . import delivery_ups +from . import ups_request_patch diff --git a/delivery_ups_hibou/models/delivery_ups.py b/delivery_ups_hibou/models/delivery_ups.py new file mode 100644 index 00000000..8285cdd6 --- /dev/null +++ b/delivery_ups_hibou/models/delivery_ups.py @@ -0,0 +1,218 @@ +from odoo import api, fields, models, _ +from odoo.exceptions import UserError, ValidationError +from odoo.addons.delivery_ups.models.ups_request import UPSRequest, Package +from odoo.tools import pdf + + +class ProviderUPS(models.Model): + _inherit = 'delivery.carrier' + + 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: + if not third_party_account.delivery_type == 'ups': + raise ValidationError('Non-UPS Shipping Account indicated during UPS shipment.') + return True + if order and self.ups_bill_my_account and order.ups_carrier_account: + return True + return False + + def _get_ups_account_number(self, order=None, picking=None): + """ + Common hook to customize what UPS Account number to use. + :return: UPS Account Number + """ + # Provided by Hibou Odoo Suite `delivery_hibou` + third_party_account = self.get_third_party_account(order=order, picking=picking) + if third_party_account: + if not third_party_account.delivery_type == 'ups': + raise ValidationError('Non-UPS Shipping Account indicated during UPS shipment.') + return third_party_account.name + if order and order.ups_carrier_account: + return order.ups_carrier_account + if picking and picking.sale_id.ups_carrier_account: + return picking.sale_id.ups_carrier_account + return self.ups_shipper_number + + 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 + + """ + 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) + ResCurrency = self.env['res.currency'] + max_weight = self.ups_default_packaging_id.max_weight + packages = [] + total_qty = 0 + total_weight = 0 + for line in order.order_line.filtered(lambda line: not line.is_delivery): + total_qty += line.product_uom_qty + total_weight += line.product_id.weight * line.product_qty + + if max_weight and total_weight > max_weight: + total_package = int(total_weight / max_weight) + last_package_weight = total_weight % max_weight + + for seq in range(total_package): + packages.append(Package(self, max_weight)) + if last_package_weight: + packages.append(Package(self, last_package_weight)) + else: + packages.append(Package(self, total_weight)) + + shipment_info = { + 'total_qty': total_qty # required when service type = 'UPS Worldwide Express Freight' + } + + if self.ups_cod: + cod_info = { + 'currency': order.partner_id.country_id.currency_id.name, + 'monetary_value': order.amount_total, + 'funds_code': self.ups_cod_funds_code, + } + else: + cod_info = None + + # Hibou Delivery + shipper_company = self.get_shipper_company(order=order) + shipper_warehouse = self.get_shipper_warehouse(order=order) + recipient = self.get_recipient(order=order) + date_planned = None + if self.env.context.get('date_planned'): + date_planned = self.env.context.get('date_planned') + + check_value = srm.check_required_value(shipper_company, shipper_warehouse, recipient, order=order) + if check_value: + return {'success': False, + 'price': 0.0, + 'error_message': check_value, + 'warning_message': False} + + ups_service_type = order.ups_service_type or self.ups_default_service_type + result = srm.get_shipping_price( + shipment_info=shipment_info, packages=packages, shipper=shipper_company, ship_from=shipper_warehouse, + ship_to=recipient, packaging_type=self.ups_default_packaging_id.shipper_package_code, service_type=ups_service_type, + saturday_delivery=self.ups_saturday_delivery, cod_info=cod_info, date_planned=date_planned) + + if result.get('error_message'): + return {'success': False, + 'price': 0.0, + 'error_message': _('Error:\n%s') % result['error_message'], + 'warning_message': False} + + if order.currency_id.name == result['currency_code']: + price = float(result['price']) + else: + quote_currency = ResCurrency.search([('name', '=', result['currency_code'])], limit=1) + price = quote_currency._convert( + float(result['price']), order.currency_id, order.company_id, order.date_order or fields.Date.today()) + + # Hibou Delivery + if self._get_ups_is_third_party(order=order): + # Don't show delivery amount, if ups bill my account option is true + price = 0.0 + + return {'success': True, + 'price': price, + 'transit_days': result.get('transit_days', 0), + 'error_message': False, + 'warning_message': False} + + 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: + # Hibou Delivery + shipper_company = superself.get_shipper_company(picking=picking) + shipper_warehouse = superself.get_shipper_warehouse(picking=picking) + recipient = superself.get_recipient(picking=picking) + + packages = [] + package_names = [] + if picking.package_ids: + # Create all packages + for package in picking.package_ids: + packages.append(Package(self, package.shipping_weight, quant_pack=package.packaging_id, name=package.name)) + package_names.append(package.name) + # Create one package with the rest (the content that is not in a package) + if picking.weight_bulk: + packages.append(Package(self, picking.weight_bulk)) + + invoice_line_total = 0 + for move in picking.move_lines: + invoice_line_total += picking.company_id.currency_id.round(move.product_id.lst_price * move.product_qty) + + shipment_info = { + 'description': superself.get_order_name(picking=picking), + 'total_qty': sum(sml.qty_done for sml in picking.move_line_ids), + 'ilt_monetary_value': '%d' % invoice_line_total, + 'itl_currency_code': self.env.user.company_id.currency_id.name, + 'phone': recipient.mobile or recipient.phone, + } + if picking.sale_id and picking.sale_id.carrier_id != picking.carrier_id: + ups_service_type = picking.carrier_id.ups_default_service_type or picking.ups_service_type or superself.ups_default_service_type + else: + ups_service_type = picking.ups_service_type or superself.ups_default_service_type + + # Hibou Delivery + ups_carrier_account = superself._get_ups_carrier_account(picking) + + if picking.carrier_id.ups_cod: + cod_info = { + 'currency': picking.partner_id.country_id.currency_id.name, + 'monetary_value': picking.sale_id.amount_total, + 'funds_code': superself.ups_cod_funds_code, + } + else: + cod_info = None + + check_value = srm.check_required_value(shipper_company, shipper_warehouse, recipient, picking=picking) + if check_value: + raise UserError(check_value) + + package_type = picking.package_ids and picking.package_ids[0].packaging_id.shipper_package_code or self.ups_default_packaging_id.shipper_package_code + result = srm.send_shipping( + shipment_info=shipment_info, packages=packages, shipper=shipper_company, ship_from=shipper_warehouse, + ship_to=recipient, packaging_type=package_type, service_type=ups_service_type, label_file_type=self.ups_label_file_type, ups_carrier_account=ups_carrier_account, + saturday_delivery=picking.carrier_id.ups_saturday_delivery, cod_info=cod_info) + if result.get('error_message'): + raise UserError(result['error_message']) + + order = picking.sale_id + company = order.company_id or picking.company_id or self.env.user.company_id + currency_order = picking.sale_id.currency_id + if not currency_order: + currency_order = picking.company_id.currency_id + + if currency_order.name == result['currency_code']: + price = float(result['price']) + else: + quote_currency = ResCurrency.search([('name', '=', result['currency_code'])], limit=1) + price = quote_currency._convert( + float(result['price']), currency_order, company, order.date_order or fields.Date.today()) + + package_labels = [] + for track_number, label_binary_data in result.get('label_binary_data').items(): + package_labels = package_labels + [(track_number, label_binary_data)] + + carrier_tracking_ref = "+".join([pl[0] for pl in package_labels]) + logmessage = _("Shipment created into UPS
" + "Tracking Numbers: %s
" + "Packages: %s") % (carrier_tracking_ref, ','.join(package_names)) + if superself.ups_label_file_type != 'GIF': + attachments = [('LabelUPS-%s.%s' % (pl[0], superself.ups_label_file_type), pl[1]) for pl in package_labels] + if superself.ups_label_file_type == 'GIF': + attachments = [('LabelUPS.pdf', pdf.merge_pdf([pl[1] for pl in package_labels]))] + picking.message_post(body=logmessage, attachments=attachments) + shipping_data = { + 'exact_price': price, + 'tracking_number': carrier_tracking_ref} + res = res + [shipping_data] + return res diff --git a/delivery_ups_hibou/models/ups_request_patch.py b/delivery_ups_hibou/models/ups_request_patch.py new file mode 100644 index 00000000..3104662e --- /dev/null +++ b/delivery_ups_hibou/models/ups_request_patch.py @@ -0,0 +1,112 @@ +import suds +from odoo.addons.delivery_ups.models.ups_request import UPSRequest + +SUDS_VERSION = suds.__version__ + + +def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from, ship_to, packaging_type, service_type, + saturday_delivery, cod_info, date_planned=False): + client = self._set_client(self.rate_wsdl, 'Rate', 'RateRequest') + + request = client.factory.create('ns0:RequestType') + request.RequestOption = 'Rate' + + classification = client.factory.create('ns2:CodeDescriptionType') + classification.Code = '00' # Get rates for the shipper account + classification.Description = 'Get rates for the shipper account' + + namespace = 'ns2' + shipment = client.factory.create('{}:ShipmentType'.format(namespace)) + + # Hibou Delivery + if date_planned: + if not isinstance(date_planned, str): + date_planned = str(date_planned) + shipment.DeliveryTimeInformation = client.factory.create('{}:TimeInTransitRequestType'.format(namespace)) + shipment.DeliveryTimeInformation.Pickup = client.factory.create('{}:PickupType'.format(namespace)) + shipment.DeliveryTimeInformation.Pickup.Date = date_planned.split(' ')[0] + # End + + for package in self.set_package_detail(client, packages, packaging_type, namespace, ship_from, ship_to, cod_info): + shipment.Package.append(package) + + shipment.Shipper.Name = shipper.name or '' + shipment.Shipper.Address.AddressLine = [shipper.street or '', shipper.street2 or ''] + 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 = shipper.phone or '' + + shipment.ShipFrom.Name = ship_from.name or '' + shipment.ShipFrom.Address.AddressLine = [ship_from.street or '', ship_from.street2 or ''] + 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 = ship_from.phone or '' + + shipment.ShipTo.Name = ship_to.name or '' + shipment.ShipTo.Address.AddressLine = [ship_to.street or '', ship_to.street2 or ''] + 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 = ship_to.phone or '' + 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.NumOfPieces = int(shipment_info.get('total_qty')) + + if saturday_delivery: + shipment.ShipmentServiceOptions.SaturdayDeliveryIndicator = saturday_delivery + else: + shipment.ShipmentServiceOptions = '' + + shipment.ShipmentRatingOptions.NegotiatedRatesIndicator = 1 + + try: + # Get rate using for provided detail + response = client.service.ProcessRate(Request=request, CustomerClassification=classification, Shipment=shipment) + + # Check if ProcessRate 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['currency_code'] = response.RatedShipment[0].TotalCharges.CurrencyCode + + # Some users are qualified to receive negotiated rates + negotiated_rate = 'NegotiatedRateCharges' in response.RatedShipment[0] and response.RatedShipment[ + 0].NegotiatedRateCharges.TotalCharge.MonetaryValue or None + + result['price'] = negotiated_rate or response.RatedShipment[0].TotalCharges.MonetaryValue + + # Hibou Delivery + if hasattr(response.RatedShipment[0], 'GuaranteedDelivery') and hasattr(response.RatedShipment[0].GuaranteedDelivery, 'BusinessDaysInTransit'): + result['transit_days'] = int(response.RatedShipment[0].GuaranteedDelivery.BusinessDaysInTransit) + # End + + 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) + + +UPSRequest.get_shipping_price = patched_get_shipping_price From 8494686d0ff9df51363dcd558f63b0bede76d4dc Mon Sep 17 00:00:00 2001 From: Cedric Collins Date: Mon, 12 Apr 2021 16:55:46 -0500 Subject: [PATCH 2/7] [IMP] delivery_hibou,delivery_ups_hibou,stock_delivery_planner: add ups_rate_shipment_multi H3455 --- delivery_ups_hibou/models/delivery_ups.py | 121 ++++++++++++++++++ .../models/ups_request_patch.py | 74 ++++++++--- 2 files changed, 175 insertions(+), 20 deletions(-) diff --git a/delivery_ups_hibou/models/delivery_ups.py b/delivery_ups_hibou/models/delivery_ups.py index 8285cdd6..245f29ba 100644 --- a/delivery_ups_hibou/models/delivery_ups.py +++ b/delivery_ups_hibou/models/delivery_ups.py @@ -2,6 +2,8 @@ from odoo import api, fields, models, _ from odoo.exceptions import UserError, ValidationError from odoo.addons.delivery_ups.models.ups_request import UPSRequest, Package from odoo.tools import pdf +import logging +_logger = logging.getLogger(__name__) class ProviderUPS(models.Model): @@ -216,3 +218,122 @@ class ProviderUPS(models.Model): 'tracking_number': carrier_tracking_ref} res = res + [shipping_data] return res + + def ups_rate_shipment_multi(self, order=None, picking=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) + ResCurrency = self.env['res.currency'] + max_weight = self.ups_default_packaging_id.max_weight + packages = [] + if order: + currency = order.currency_id + company = order.company_id + date_order = order.date_order or fields.Date.today() + total_qty = 0 + total_weight = 0 + for line in order.order_line.filtered(lambda line: not line.is_delivery): + total_qty += line.product_uom_qty + total_weight += line.product_id.weight * line.product_qty + + if max_weight and total_weight > max_weight: + total_package = int(total_weight / max_weight) + last_package_weight = total_weight % max_weight + + for seq in range(total_package): + packages.append(Package(self, max_weight)) + if last_package_weight: + packages.append(Package(self, last_package_weight)) + else: + packages.append(Package(self, total_weight)) + else: + currency = picking.sale_id.currency_id if picking.sale_id else picking.company_id.currency_id + 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? + total_qty = len(picking.package_ids) + packages = [Package(self, package.shipping_weight) for package in picking.package_ids] + + + shipment_info = { + 'total_qty': total_qty # required when service type = 'UPS Worldwide Express Freight' + } + + if self.ups_cod: + cod_info = { + 'currency': currency.name, + 'monetary_value': order.amount_total if order else picking.sale_id.amount_total, + 'funds_code': self.ups_cod_funds_code, + } + else: + cod_info = None + + # Hibou Delivery + shipper_company = self.get_shipper_company(order=order, picking=picking) + shipper_warehouse = self.get_shipper_warehouse(order=order, picking=picking) + recipient = self.get_recipient(order=order, picking=picking) + date_planned = fields.Datetime.now() + if self.env.context.get('date_planned'): + date_planned = self.env.context.get('date_planned') + + check_value = srm.check_required_value(shipper_company, shipper_warehouse, recipient, order=order, picking=picking) + if check_value: + return [{'success': False, + 'price': 0.0, + 'error_message': check_value, + 'warning_message': False, + }] + + #ups_service_type = order.ups_service_type or self.ups_default_service_type + ups_service_type = None # See if this gets us all service types + result = srm.get_shipping_price( + shipment_info=shipment_info, packages=packages, shipper=shipper_company, ship_from=shipper_warehouse, + ship_to=recipient, packaging_type=self.ups_default_packaging_id.shipper_package_code, service_type=ups_service_type, + saturday_delivery=self.ups_saturday_delivery, cod_info=cod_info, date_planned=date_planned, multi=True) + # Hibou Delivery + is_third_party = self._get_ups_is_third_party(order=order, picking=picking) + + response = [] + for rate in result: + if rate.get('error_message'): + _logger.error('UPS error: %s' % rate['error_message']) + response.append({ + 'success': False, 'price': 0.0, + 'error_message': _('Error:\n%s') % rate['error_message'], + 'warning_message': False, + }) + else: + if currency.name == rate['currency_code']: + price = float(rate['price']) + else: + quote_currency = ResCurrency.search([('name', '=', rate['currency_code'])], limit=1) + price = quote_currency._convert( + float(rate['price']), currency, company, date_order) + + if is_third_party: + # Don't show delivery amount, if ups bill my account option is true + price = 0.0 + + service_code = rate['service_code'] + carrier = self.ups_find_delivery_carrier_for_service(service_code) + if carrier: + response.append({ + 'carrier': carrier, + 'success': True, + 'price': price, + 'error_message': False, + 'warning_message': False, + 'date_planned': date_planned, + 'date_delivered': None, + 'transit_days': rate.get('transit_days', 0), + 'service_code': service_code, + }) + return response + + def ups_find_delivery_carrier_for_service(self, service_code): + if self.ups_default_service_type == service_code: + return self + # arbitrary decision, lets find the same account number + carrier = self.search([('ups_shipper_number', '=', self.ups_shipper_number), + ('ups_default_service_type', '=', service_code) + ], limit=1) + return carrier diff --git a/delivery_ups_hibou/models/ups_request_patch.py b/delivery_ups_hibou/models/ups_request_patch.py index 3104662e..1b811f16 100644 --- a/delivery_ups_hibou/models/ups_request_patch.py +++ b/delivery_ups_hibou/models/ups_request_patch.py @@ -1,15 +1,20 @@ import suds from odoo.addons.delivery_ups.models.ups_request import UPSRequest +import logging +_logger = logging.getLogger(__name__) SUDS_VERSION = suds.__version__ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from, ship_to, packaging_type, service_type, - saturday_delivery, cod_info, date_planned=False): + saturday_delivery, cod_info, date_planned=False, multi=False): client = self._set_client(self.rate_wsdl, 'Rate', 'RateRequest') request = client.factory.create('ns0:RequestType') - request.RequestOption = 'Rate' + if multi: + request.RequestOption = 'Shop' + else: + request.RequestOption = 'Rate' classification = client.factory.create('ns2:CodeDescriptionType') classification.Code = '00' # Get rates for the shipper account @@ -60,10 +65,11 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from 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.NumOfPieces = int(shipment_info.get('total_qty')) + if not multi: + shipment.Service.Code = service_type or '' + shipment.Service.Description = 'Service Code' + if service_type == "96": + shipment.NumOfPieces = int(shipment_info.get('total_qty')) if saturday_delivery: shipment.ShipmentServiceOptions.SaturdayDeliveryIndicator = saturday_delivery @@ -78,23 +84,45 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from # Check if ProcessRate 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) + error_message = self.get_error_message(response.Response.ResponseStatus.Code, + response.Response.ResponseStatus.Description) + if multi: + return [error_message] + return error_message - result = {} - result['currency_code'] = response.RatedShipment[0].TotalCharges.CurrencyCode + if not multi: + result = {} + result['currency_code'] = response.RatedShipment[0].TotalCharges.CurrencyCode - # Some users are qualified to receive negotiated rates - negotiated_rate = 'NegotiatedRateCharges' in response.RatedShipment[0] and response.RatedShipment[ - 0].NegotiatedRateCharges.TotalCharge.MonetaryValue or None + # Some users are qualified to receive negotiated rates + negotiated_rate = 'NegotiatedRateCharges' in response.RatedShipment[0] and response.RatedShipment[ + 0].NegotiatedRateCharges.TotalCharge.MonetaryValue or None - result['price'] = negotiated_rate or response.RatedShipment[0].TotalCharges.MonetaryValue + result['price'] = negotiated_rate or response.RatedShipment[0].TotalCharges.MonetaryValue - # Hibou Delivery - if hasattr(response.RatedShipment[0], 'GuaranteedDelivery') and hasattr(response.RatedShipment[0].GuaranteedDelivery, 'BusinessDaysInTransit'): - result['transit_days'] = int(response.RatedShipment[0].GuaranteedDelivery.BusinessDaysInTransit) - # End + # Hibou Delivery + if hasattr(response.RatedShipment[0], 'GuaranteedDelivery') and hasattr(response.RatedShipment[0].GuaranteedDelivery, 'BusinessDaysInTransit'): + result['transit_days'] = int(response.RatedShipment[0].GuaranteedDelivery.BusinessDaysInTransit) + # End + else: + result = [] + for rated_shipment in response.RatedShipment: + rate = {} + rate['currency_code'] = rated_shipment.TotalCharges.CurrencyCode + # Some users are qualified to receive negotiated rates + negotiated_rate = 'NegotiatedRateCharges' in rated_shipment and response.RatedShipment[ + 0].NegotiatedRateCharges.TotalCharge.MonetaryValue or None + + rate['price'] = negotiated_rate or rated_shipment.TotalCharges.MonetaryValue + + # Hibou Delivery + if hasattr(rated_shipment, 'GuaranteedDelivery') and hasattr( + rated_shipment.GuaranteedDelivery, 'BusinessDaysInTransit'): + rate['transit_days'] = int(rated_shipment.GuaranteedDelivery.BusinessDaysInTransit) + # End + rate['service_code'] = rated_shipment.Service.Code + result.append(rate) return result except suds.WebFault as e: @@ -102,11 +130,17 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from prefix = '' if SUDS_VERSION >= "0.6": prefix = '/Envelope/Body/Fault' - return self.get_error_message( + error_message = self.get_error_message( e.document.childAtPath(prefix + '/detail/Errors/ErrorDetail/PrimaryErrorCode/Code').getText(), e.document.childAtPath(prefix + '/detail/Errors/ErrorDetail/PrimaryErrorCode/Description').getText()) + if multi: + return [error_message] + return error_message except IOError as e: - return self.get_error_message('0', 'UPS Server Not Found:\n%s' % e) + error_message = self.get_error_message('0', 'UPS Server Not Found:\n%s' % e) + if multi: + return [error_message] + return error_message UPSRequest.get_shipping_price = patched_get_shipping_price From 55de0655c2b8694d865a7c0883318fa206e63c1f Mon Sep 17 00:00:00 2001 From: Cedric Collins Date: Wed, 8 Sep 2021 23:02:32 -0500 Subject: [PATCH 3/7] [MIG] delivery_ups_hibou: migrated from 12.0 --- delivery_ups_hibou/__manifest__.py | 2 +- delivery_ups_hibou/models/delivery_ups.py | 16 ++-- .../models/ups_request_patch.py | 82 ++++++++++--------- 3 files changed, 55 insertions(+), 45 deletions(-) diff --git a/delivery_ups_hibou/__manifest__.py b/delivery_ups_hibou/__manifest__.py index 67931660..c37b2f8b 100644 --- a/delivery_ups_hibou/__manifest__.py +++ b/delivery_ups_hibou/__manifest__.py @@ -1,6 +1,6 @@ { 'name': 'Hibou UPS Shipping', - 'version': '12.0.1.0.0', + 'version': '13.0.1.0.0', 'category': 'Stock', 'author': "Hibou Corp.", 'license': 'AGPL-3', diff --git a/delivery_ups_hibou/models/delivery_ups.py b/delivery_ups_hibou/models/delivery_ups.py index 245f29ba..7d3ad40f 100644 --- a/delivery_ups_hibou/models/delivery_ups.py +++ b/delivery_ups_hibou/models/delivery_ups.py @@ -52,7 +52,7 @@ class ProviderUPS(models.Model): packages = [] total_qty = 0 total_weight = 0 - for line in order.order_line.filtered(lambda line: not line.is_delivery): + for line in order.order_line.filtered(lambda line: not line.is_delivery and not line.display_type): total_qty += line.product_uom_qty total_weight += line.product_id.weight * line.product_qty @@ -180,15 +180,17 @@ class ProviderUPS(models.Model): raise UserError(check_value) package_type = picking.package_ids and picking.package_ids[0].packaging_id.shipper_package_code or self.ups_default_packaging_id.shipper_package_code - result = srm.send_shipping( + srm.send_shipping( shipment_info=shipment_info, packages=packages, shipper=shipper_company, ship_from=shipper_warehouse, - ship_to=recipient, packaging_type=package_type, service_type=ups_service_type, label_file_type=self.ups_label_file_type, ups_carrier_account=ups_carrier_account, - saturday_delivery=picking.carrier_id.ups_saturday_delivery, cod_info=cod_info) + ship_to=recipient, packaging_type=package_type, service_type=ups_service_type, duty_payment=picking.carrier_id.ups_duty_payment, + label_file_type=self.ups_label_file_type, ups_carrier_account=ups_carrier_account, saturday_delivery=picking.carrier_id.ups_saturday_delivery, + cod_info=cod_info) + result = srm.process_shipment() if result.get('error_message'): - raise UserError(result['error_message']) + raise UserError(result['error_message'].__str__()) order = picking.sale_id - company = order.company_id or picking.company_id or self.env.user.company_id + company = order.company_id or picking.company_id or self.env.company currency_order = picking.sale_id.currency_id if not currency_order: currency_order = picking.company_id.currency_id @@ -217,6 +219,8 @@ class ProviderUPS(models.Model): 'exact_price': price, 'tracking_number': carrier_tracking_ref} res = res + [shipping_data] + if self.return_label_on_delivery: + self.ups_get_return_label(picking) return res def ups_rate_shipment_multi(self, order=None, picking=None): diff --git a/delivery_ups_hibou/models/ups_request_patch.py b/delivery_ups_hibou/models/ups_request_patch.py index 1b811f16..4fca9c9e 100644 --- a/delivery_ups_hibou/models/ups_request_patch.py +++ b/delivery_ups_hibou/models/ups_request_patch.py @@ -1,41 +1,41 @@ -import suds +from zeep.exceptions import Fault from odoo.addons.delivery_ups.models.ups_request import UPSRequest import logging _logger = logging.getLogger(__name__) -SUDS_VERSION = suds.__version__ - def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from, ship_to, packaging_type, service_type, saturday_delivery, cod_info, date_planned=False, multi=False): client = self._set_client(self.rate_wsdl, 'Rate', 'RateRequest') - - request = client.factory.create('ns0:RequestType') + service = self._set_service(client, 'Rate') + request = self.factory_ns3.RequestType() if multi: request.RequestOption = 'Shop' else: request.RequestOption = 'Rate' - classification = client.factory.create('ns2:CodeDescriptionType') + classification = self.factory_ns2.CodeDescriptionType() classification.Code = '00' # Get rates for the shipper account classification.Description = 'Get rates for the shipper account' - namespace = 'ns2' - shipment = client.factory.create('{}:ShipmentType'.format(namespace)) + request_type = "rating" + shipment = self.factory_ns2.ShipmentType() # Hibou Delivery if date_planned: if not isinstance(date_planned, str): date_planned = str(date_planned) - shipment.DeliveryTimeInformation = client.factory.create('{}:TimeInTransitRequestType'.format(namespace)) - shipment.DeliveryTimeInformation.Pickup = client.factory.create('{}:PickupType'.format(namespace)) + shipment.DeliveryTimeInformation = self.factory_ns2.TimeInTransitRequestType() + shipment.DeliveryTimeInformation.Pickup = self.factory_ns2.PickupType() shipment.DeliveryTimeInformation.Pickup.Date = date_planned.split(' ')[0] # End - for package in self.set_package_detail(client, packages, packaging_type, namespace, ship_from, ship_to, cod_info): + for package in self.set_package_detail(client, packages, packaging_type, ship_from, ship_to, cod_info, request_type): shipment.Package.append(package) + shipment.Shipper = self.factory_ns2.ShipperType() shipment.Shipper.Name = shipper.name or '' + shipment.Shipper.Address = self.factory_ns2.AddressType() shipment.Shipper.Address.AddressLine = [shipper.street or '', shipper.street2 or ''] shipment.Shipper.Address.City = shipper.city or '' shipment.Shipper.Address.PostalCode = shipper.zip or '' @@ -45,7 +45,9 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from shipment.Shipper.ShipperNumber = self.shipper_number or '' # shipment.Shipper.Phone.Number = shipper.phone or '' + shipment.ShipFrom = self.factory_ns2.ShipFromType() shipment.ShipFrom.Name = ship_from.name or '' + shipment.ShipFrom.Address = self.factory_ns2.AddressType() shipment.ShipFrom.Address.AddressLine = [ship_from.street or '', ship_from.street2 or ''] shipment.ShipFrom.Address.City = ship_from.city or '' shipment.ShipFrom.Address.PostalCode = ship_from.zip or '' @@ -54,7 +56,9 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from shipment.ShipFrom.Address.StateProvinceCode = ship_from.state_id.code or '' # shipment.ShipFrom.Phone.Number = ship_from.phone or '' + shipment.ShipTo = self.factory_ns2.ShipToType() shipment.ShipTo.Name = ship_to.name or '' + shipment.ShipTo.Address = self.factory_ns2.AddressType() shipment.ShipTo.Address.AddressLine = [ship_to.street or '', ship_to.street2 or ''] shipment.ShipTo.Address.City = ship_to.city or '' shipment.ShipTo.Address.PostalCode = ship_to.zip or '' @@ -63,19 +67,22 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from shipment.ShipTo.Address.StateProvinceCode = ship_to.state_id.code or '' # shipment.ShipTo.Phone.Number = ship_to.phone or '' if not ship_to.commercial_partner_id.is_company: - shipment.ShipTo.Address.ResidentialAddressIndicator = suds.null() + shipment.ShipTo.Address.ResidentialAddressIndicator = None if not multi: + shipment.Service = self.factory_ns2.CodeDescriptionType() shipment.Service.Code = service_type or '' shipment.Service.Description = 'Service Code' if service_type == "96": shipment.NumOfPieces = int(shipment_info.get('total_qty')) if saturday_delivery: + shipment.ShipmentServiceOptions = self.factory_ns2.ShipmentServiceOptionsType() shipment.ShipmentServiceOptions.SaturdayDeliveryIndicator = saturday_delivery else: shipment.ShipmentServiceOptions = '' + shipment.ShipmentRatingOptions = self.factory_ns2.ShipmentRatingOptionsType() shipment.ShipmentRatingOptions.NegotiatedRatesIndicator = 1 try: @@ -84,21 +91,23 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from # Check if ProcessRate is not success then return reason for that if response.Response.ResponseStatus.Code != "1": - error_message = self.get_error_message(response.Response.ResponseStatus.Code, - response.Response.ResponseStatus.Description) + error_message = self.get_error_message(response.Response.ResponseStatus.Code, response.Response.ResponseStatus.Description) if multi: return [error_message] return error_message if not multi: - result = {} - result['currency_code'] = response.RatedShipment[0].TotalCharges.CurrencyCode + rate = response.RatedShipment[0] + charge = rate.TotalCharges # Some users are qualified to receive negotiated rates - negotiated_rate = 'NegotiatedRateCharges' in response.RatedShipment[0] and response.RatedShipment[ - 0].NegotiatedRateCharges.TotalCharge.MonetaryValue or None + if 'NegotiatedRateCharges' in rate and rate.NegotiatedRateCharges and rate.NegotiatedRateCharges.TotalCharge.MonetaryValue: + charge = rate.NegotiatedRateCharges.TotalCharge - result['price'] = negotiated_rate or response.RatedShipment[0].TotalCharges.MonetaryValue + result = { + 'currency_code': charge.CurrencyCode, + 'price': charge.MonetaryValue, + } # Hibou Delivery if hasattr(response.RatedShipment[0], 'GuaranteedDelivery') and hasattr(response.RatedShipment[0].GuaranteedDelivery, 'BusinessDaysInTransit'): @@ -106,33 +115,30 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from # End else: result = [] - for rated_shipment in response.RatedShipment: - rate = {} - rate['currency_code'] = rated_shipment.TotalCharges.CurrencyCode + for rate in response.RatedShipment: + charge = rate.TotalCharges # Some users are qualified to receive negotiated rates - negotiated_rate = 'NegotiatedRateCharges' in rated_shipment and response.RatedShipment[ - 0].NegotiatedRateCharges.TotalCharge.MonetaryValue or None + if 'NegotiatedRateCharges' in rate and rate.NegotiatedRateCharges and rate.NegotiatedRateCharges.TotalCharge.MonetaryValue: + charge = rate.NegotiatedRateCharges.TotalCharge - rate['price'] = negotiated_rate or rated_shipment.TotalCharges.MonetaryValue + rated_shipment = { + 'currency_code': charge.CurrencyCode, + 'price': charge.MonetaryValue, + } # Hibou Delivery - if hasattr(rated_shipment, 'GuaranteedDelivery') and hasattr( - rated_shipment.GuaranteedDelivery, 'BusinessDaysInTransit'): - rate['transit_days'] = int(rated_shipment.GuaranteedDelivery.BusinessDaysInTransit) + if hasattr(response.RatedShipment[0], 'GuaranteedDelivery') and hasattr( + response.RatedShipment[0].GuaranteedDelivery, 'BusinessDaysInTransit'): + rated_shipment['transit_days'] = int(response.RatedShipment[0].GuaranteedDelivery.BusinessDaysInTransit) # End - rate['service_code'] = rated_shipment.Service.Code - result.append(rate) + result.append(rated_shipment) 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' - error_message = 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 Fault as e: + code = e.detail.xpath("//err:PrimaryErrorCode/err:Code", namespaces=self.ns)[0].text + description = e.detail.xpath("//err:PrimaryErrorCode/err:Description", namespaces=self.ns)[0].text + error_message = self.get_error_message(code, description) if multi: return [error_message] return error_message From a5fbd418f057377eedd86e46eb46768e680043c7 Mon Sep 17 00:00:00 2001 From: Cedric Collins Date: Mon, 13 Sep 2021 22:31:29 -0500 Subject: [PATCH 4/7] [MIG] delivery_ups_hibou: migrated to 14.0 --- delivery_ups_hibou/__manifest__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/delivery_ups_hibou/__manifest__.py b/delivery_ups_hibou/__manifest__.py index c37b2f8b..c98d21d7 100644 --- a/delivery_ups_hibou/__manifest__.py +++ b/delivery_ups_hibou/__manifest__.py @@ -1,6 +1,6 @@ { 'name': 'Hibou UPS Shipping', - 'version': '13.0.1.0.0', + 'version': '14.0.1.0.0', 'category': 'Stock', 'author': "Hibou Corp.", 'license': 'AGPL-3', From fb9f7f6df694546985a236277174859b2016fd03 Mon Sep 17 00:00:00 2001 From: Cedric Collins Date: Mon, 13 Sep 2021 22:33:11 -0500 Subject: [PATCH 5/7] [FIX] delivery_ups_hibou: fix rate multi --- delivery_ups_hibou/models/ups_request_patch.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/delivery_ups_hibou/models/ups_request_patch.py b/delivery_ups_hibou/models/ups_request_patch.py index 4fca9c9e..abe84a74 100644 --- a/delivery_ups_hibou/models/ups_request_patch.py +++ b/delivery_ups_hibou/models/ups_request_patch.py @@ -128,10 +128,10 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from } # Hibou Delivery - if hasattr(response.RatedShipment[0], 'GuaranteedDelivery') and hasattr( - response.RatedShipment[0].GuaranteedDelivery, 'BusinessDaysInTransit'): - rated_shipment['transit_days'] = int(response.RatedShipment[0].GuaranteedDelivery.BusinessDaysInTransit) + if hasattr(rate, 'GuaranteedDelivery') and hasattr(rate.GuaranteedDelivery, 'BusinessDaysInTransit'): + rated_shipment['transit_days'] = int(rate.GuaranteedDelivery.BusinessDaysInTransit) # End + rated_shipment['service_code'] = rate.Service.Code result.append(rated_shipment) return result From 612cbd8e10561a10159ac143ec0ed37fde248145 Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Mon, 20 Sep 2021 17:49:21 -0700 Subject: [PATCH 6/7] [IMP] delivery_ups_hibou: mechanisms for per package rating and shipping --- delivery_ups_hibou/__init__.py | 2 ++ delivery_ups_hibou/__manifest__.py | 4 ++-- delivery_ups_hibou/models/__init__.py | 2 ++ delivery_ups_hibou/models/delivery_ups.py | 14 +++++++++++++- delivery_ups_hibou/models/ups_request_patch.py | 2 ++ 5 files changed, 21 insertions(+), 3 deletions(-) diff --git a/delivery_ups_hibou/__init__.py b/delivery_ups_hibou/__init__.py index 0650744f..09434554 100644 --- a/delivery_ups_hibou/__init__.py +++ b/delivery_ups_hibou/__init__.py @@ -1 +1,3 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + from . import models diff --git a/delivery_ups_hibou/__manifest__.py b/delivery_ups_hibou/__manifest__.py index c98d21d7..ed655df1 100644 --- a/delivery_ups_hibou/__manifest__.py +++ b/delivery_ups_hibou/__manifest__.py @@ -1,9 +1,9 @@ { 'name': 'Hibou UPS Shipping', - 'version': '14.0.1.0.0', + 'version': '14.0.1.1.0', 'category': 'Stock', 'author': "Hibou Corp.", - 'license': 'AGPL-3', + 'license': 'OPL-1', 'website': 'https://hibou.io/', 'depends': [ 'delivery_ups', diff --git a/delivery_ups_hibou/models/__init__.py b/delivery_ups_hibou/models/__init__.py index 735db17b..102ebacd 100644 --- a/delivery_ups_hibou/models/__init__.py +++ b/delivery_ups_hibou/models/__init__.py @@ -1,2 +1,4 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + from . import delivery_ups from . import ups_request_patch diff --git a/delivery_ups_hibou/models/delivery_ups.py b/delivery_ups_hibou/models/delivery_ups.py index 7d3ad40f..35ec5529 100644 --- a/delivery_ups_hibou/models/delivery_ups.py +++ b/delivery_ups_hibou/models/delivery_ups.py @@ -1,3 +1,5 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + from odoo import api, fields, models, _ from odoo.exceptions import UserError, ValidationError from odoo.addons.delivery_ups.models.ups_request import UPSRequest, Package @@ -223,7 +225,17 @@ class ProviderUPS(models.Model): self.ups_get_return_label(picking) return res - def ups_rate_shipment_multi(self, order=None, picking=None): + def ups_rate_shipment_multi(self, order=None, picking=None, packages=None): + if not packages: + return self._ups_rate_shipment_multi_package(order=order, picking=picking) + else: + rates = [] + for package in packages: + rates += self._ups_rate_shipment_multi_package(order=order, picking=picking, package=package) + return rates + + def _ups_rate_shipment_multi_package(self, order=None, picking=None, package=None): + # TODO package here is ignored, it should not be (UPS is not multi-rating capable until we can get rates for a single package) 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'] diff --git a/delivery_ups_hibou/models/ups_request_patch.py b/delivery_ups_hibou/models/ups_request_patch.py index abe84a74..0215aa0c 100644 --- a/delivery_ups_hibou/models/ups_request_patch.py +++ b/delivery_ups_hibou/models/ups_request_patch.py @@ -1,3 +1,5 @@ +# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. + from zeep.exceptions import Fault from odoo.addons.delivery_ups.models.ups_request import UPSRequest import logging From 5b844bad16ebf0c79b81af0220ef10df596ad148 Mon Sep 17 00:00:00 2001 From: Jared Kipe Date: Thu, 10 Feb 2022 15:29:34 -0800 Subject: [PATCH 7/7] [MIG] delivery_ups_hibou: to 15.0 + transit times and per-package insurance/sig.req. --- delivery_ups_hibou/__manifest__.py | 3 +- delivery_ups_hibou/api/Error1.1.xsd | 59 ++ delivery_ups_hibou/api/IFWS.xsd | 318 ++++++ delivery_ups_hibou/api/LBRecovery.xsd | 166 ++++ delivery_ups_hibou/api/LabelRecoveryWS.wsdl | 54 + delivery_ups_hibou/api/RateWS.wsdl | 58 ++ .../api/RateWebServiceSchema.xsd | 644 ++++++++++++ delivery_ups_hibou/api/Ship.wsdl | 123 +++ .../api/ShipWebServiceSchema.xsd | 933 ++++++++++++++++++ delivery_ups_hibou/api/TNTWS.wsdl | 58 ++ .../api/TimeInTransitWebServiceSchema.xsd | 193 ++++ delivery_ups_hibou/api/UPSSecurity.xsd | 23 + delivery_ups_hibou/api/Void.wsdl | 58 ++ .../api/VoidWebServiceSchema.xsd | 45 + delivery_ups_hibou/api/common.xsd | 65 ++ delivery_ups_hibou/models/__init__.py | 1 + delivery_ups_hibou/models/delivery_ups.py | 134 ++- delivery_ups_hibou/models/stock.py | 9 + .../models/ups_request_patch.py | 355 ++++++- delivery_ups_hibou/views/stock_views.xml | 15 + 20 files changed, 3243 insertions(+), 71 deletions(-) create mode 100644 delivery_ups_hibou/api/Error1.1.xsd create mode 100644 delivery_ups_hibou/api/IFWS.xsd create mode 100644 delivery_ups_hibou/api/LBRecovery.xsd create mode 100644 delivery_ups_hibou/api/LabelRecoveryWS.wsdl create mode 100644 delivery_ups_hibou/api/RateWS.wsdl create mode 100644 delivery_ups_hibou/api/RateWebServiceSchema.xsd create mode 100644 delivery_ups_hibou/api/Ship.wsdl create mode 100644 delivery_ups_hibou/api/ShipWebServiceSchema.xsd create mode 100644 delivery_ups_hibou/api/TNTWS.wsdl create mode 100644 delivery_ups_hibou/api/TimeInTransitWebServiceSchema.xsd create mode 100644 delivery_ups_hibou/api/UPSSecurity.xsd create mode 100644 delivery_ups_hibou/api/Void.wsdl create mode 100644 delivery_ups_hibou/api/VoidWebServiceSchema.xsd create mode 100644 delivery_ups_hibou/api/common.xsd 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 ed655df1..0dc94f26 100644 --- a/delivery_ups_hibou/__manifest__.py +++ b/delivery_ups_hibou/__manifest__.py @@ -1,6 +1,6 @@ { 'name': 'Hibou UPS Shipping', - 'version': '14.0.1.1.0', + 'version': '15.0.1.0.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/api/Error1.1.xsd b/delivery_ups_hibou/api/Error1.1.xsd new file mode 100644 index 00000000..c720608a --- /dev/null +++ b/delivery_ups_hibou/api/Error1.1.xsd @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/IFWS.xsd b/delivery_ups_hibou/api/IFWS.xsd new file mode 100644 index 00000000..c36e5b33 --- /dev/null +++ b/delivery_ups_hibou/api/IFWS.xsd @@ -0,0 +1,318 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/LBRecovery.xsd b/delivery_ups_hibou/api/LBRecovery.xsd new file mode 100644 index 00000000..05588c56 --- /dev/null +++ b/delivery_ups_hibou/api/LBRecovery.xsd @@ -0,0 +1,166 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/LabelRecoveryWS.wsdl b/delivery_ups_hibou/api/LabelRecoveryWS.wsdl new file mode 100644 index 00000000..fe895eba --- /dev/null +++ b/delivery_ups_hibou/api/LabelRecoveryWS.wsdl @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/RateWS.wsdl b/delivery_ups_hibou/api/RateWS.wsdl new file mode 100644 index 00000000..7aeb7555 --- /dev/null +++ b/delivery_ups_hibou/api/RateWS.wsdl @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/RateWebServiceSchema.xsd b/delivery_ups_hibou/api/RateWebServiceSchema.xsd new file mode 100644 index 00000000..951ab3a1 --- /dev/null +++ b/delivery_ups_hibou/api/RateWebServiceSchema.xsd @@ -0,0 +1,644 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/Ship.wsdl b/delivery_ups_hibou/api/Ship.wsdl new file mode 100644 index 00000000..2c0b081d --- /dev/null +++ b/delivery_ups_hibou/api/Ship.wsdl @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/ShipWebServiceSchema.xsd b/delivery_ups_hibou/api/ShipWebServiceSchema.xsd new file mode 100644 index 00000000..cd1c1236 --- /dev/null +++ b/delivery_ups_hibou/api/ShipWebServiceSchema.xsd @@ -0,0 +1,933 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/TNTWS.wsdl b/delivery_ups_hibou/api/TNTWS.wsdl new file mode 100644 index 00000000..5e69403f --- /dev/null +++ b/delivery_ups_hibou/api/TNTWS.wsdl @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/TimeInTransitWebServiceSchema.xsd b/delivery_ups_hibou/api/TimeInTransitWebServiceSchema.xsd new file mode 100644 index 00000000..1c6928e0 --- /dev/null +++ b/delivery_ups_hibou/api/TimeInTransitWebServiceSchema.xsd @@ -0,0 +1,193 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/UPSSecurity.xsd b/delivery_ups_hibou/api/UPSSecurity.xsd new file mode 100644 index 00000000..63378ec4 --- /dev/null +++ b/delivery_ups_hibou/api/UPSSecurity.xsd @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/Void.wsdl b/delivery_ups_hibou/api/Void.wsdl new file mode 100644 index 00000000..caee6a1e --- /dev/null +++ b/delivery_ups_hibou/api/Void.wsdl @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/VoidWebServiceSchema.xsd b/delivery_ups_hibou/api/VoidWebServiceSchema.xsd new file mode 100644 index 00000000..7b69d7c0 --- /dev/null +++ b/delivery_ups_hibou/api/VoidWebServiceSchema.xsd @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/delivery_ups_hibou/api/common.xsd b/delivery_ups_hibou/api/common.xsd new file mode 100644 index 00000000..ddb84840 --- /dev/null +++ b/delivery_ups_hibou/api/common.xsd @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 35ec5529..2718c159 100644 --- a/delivery_ups_hibou/models/delivery_ups.py +++ b/delivery_ups_hibou/models/delivery_ups.py @@ -11,16 +11,35 @@ _logger = logging.getLogger(__name__) class ProviderUPS(models.Model): _inherit = 'delivery.carrier' + def _get_ups_signature_required_type(self, order=None, picking=None, package=None): + # '3' for Adult Sig. + return '2' + + def _get_ups_signature_required(self, order=None, picking=None, package=None): + if self.get_signature_required(order=order, picking=picking, package=package): + return self._get_ups_signature_required_type(order=order, picking=picking, package=package) + 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: if not third_party_account.delivery_type == 'ups': raise ValidationError('Non-UPS Shipping Account indicated during UPS shipment.') return True - if order and self.ups_bill_my_account and order.ups_carrier_account: + if order and order.ups_bill_my_account and order.partner_ups_carrier_account: 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. @@ -32,25 +51,33 @@ class ProviderUPS(models.Model): if not third_party_account.delivery_type == 'ups': raise ValidationError('Non-UPS Shipping Account indicated during UPS shipment.') return third_party_account.name - if order and order.ups_carrier_account: - return order.ups_carrier_account - if picking and picking.sale_id.ups_carrier_account: - return picking.sale_id.ups_carrier_account + if order and order.partner_ups_carrier_account: + return order.partner_ups_carrier_account + if picking and picking.sale_id.partner_ups_carrier_account: + return picking.sale_id.partner_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 return self.ups_shipper_number 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 + max_weight = self.ups_default_package_type_id.max_weight packages = [] total_qty = 0 total_weight = 0 @@ -63,11 +90,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' @@ -97,10 +127,10 @@ class ProviderUPS(models.Model): 'error_message': check_value, 'warning_message': False} - ups_service_type = order.ups_service_type or self.ups_default_service_type + ups_service_type = self.ups_default_service_type result = srm.get_shipping_price( shipment_info=shipment_info, packages=packages, shipper=shipper_company, ship_from=shipper_warehouse, - ship_to=recipient, packaging_type=self.ups_default_packaging_id.shipper_package_code, service_type=ups_service_type, + ship_to=recipient, packaging_type=self.ups_default_package_type_id.shipper_package_code, service_type=ups_service_type, saturday_delivery=self.ups_saturday_delivery, cod_info=cod_info, date_planned=date_planned) if result.get('error_message'): @@ -130,24 +160,38 @@ 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) + 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') + if package_carriers: + # only ship ours + picking_packages = picking_packages.filtered(lambda p: p.carrier_id == self and not p.carrier_tracking_ref) + + if not picking_packages: + continue packages = [] package_names = [] - if picking.package_ids: + if picking_packages: # Create all packages - for package in picking.package_ids: - packages.append(Package(self, package.shipping_weight, quant_pack=package.packaging_id, name=package.name)) + for package in picking_packages: + packages.append(Package(self, package.shipping_weight, quant_pack=package.package_type_id, name=package.name, + insurance_value=superself.get_insurance_value(picking=picking, package=package), insurance_currency_code=insurance_currency_code, signature_required=superself._get_ups_signature_required(picking=picking, package=package))) package_names.append(package.name) + + # what is the point of weight_bulk? # Create one package with the rest (the content that is not in a package) - if picking.weight_bulk: - packages.append(Package(self, picking.weight_bulk)) + # if picking.weight_bulk: + # packages.append(Package(self, picking.weight_bulk)) invoice_line_total = 0 for move in picking.move_lines: @@ -160,10 +204,7 @@ class ProviderUPS(models.Model): 'itl_currency_code': self.env.user.company_id.currency_id.name, 'phone': recipient.mobile or recipient.phone, } - if picking.sale_id and picking.sale_id.carrier_id != picking.carrier_id: - ups_service_type = picking.carrier_id.ups_default_service_type or picking.ups_service_type or superself.ups_default_service_type - else: - ups_service_type = picking.ups_service_type or superself.ups_default_service_type + ups_service_type = picking.carrier_id.ups_default_service_type # Hibou Delivery ups_carrier_account = superself._get_ups_carrier_account(picking) @@ -181,7 +222,7 @@ class ProviderUPS(models.Model): if check_value: raise UserError(check_value) - package_type = picking.package_ids and picking.package_ids[0].packaging_id.shipper_package_code or self.ups_default_packaging_id.shipper_package_code + package_type = picking_packages and picking_packages[0].package_type_id.shipper_package_code or self.ups_default_package_type_id.shipper_package_code srm.send_shipping( shipment_info=shipment_info, packages=packages, shipper=shipper_company, ship_from=shipper_warehouse, ship_to=recipient, packaging_type=package_type, service_type=ups_service_type, duty_payment=picking.carrier_id.ups_duty_payment, @@ -235,14 +276,16 @@ class ProviderUPS(models.Model): return rates def _ups_rate_shipment_multi_package(self, order=None, picking=None, package=None): - # TODO package here is ignored, it should not be (UPS is not multi-rating capable until we can get rates for a single package) 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 + max_weight = self.ups_default_package_type_id.max_weight packages = [] if order: + insurance_value = superself.get_insurance_value(order=order) + signature_required = superself._get_ups_signature_required(order=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 @@ -256,19 +299,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? - total_qty = len(picking.package_ids) - packages = [Package(self, package.shipping_weight) for package in picking.package_ids] - + if package: + total_qty = 1 + packages = [Package(self, package.shipping_weight, insurance_value=superself.get_insurance_value(picking=picking, package=package), insurance_currency_code=insurance_currency_code, signature_required=superself._get_ups_signature_required(picking=picking, package=package))] + elif picking.package_ids: + # all packages.... + total_qty = len(picking.package_ids) + packages = [Package(self, package.shipping_weight, insurance_value=superself.get_insurance_value(picking=picking, package=package), insurance_currency_code=insurance_currency_code, signature_required=superself._get_ups_signature_required(picking=picking, package=package)) 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=superself.get_insurance_value(picking=picking), insurance_currency_code=insurance_currency_code, signature_required=superself._get_ups_signature_required(picking=picking))) shipment_info = { 'total_qty': total_qty # required when service type = 'UPS Worldwide Express Freight' @@ -298,19 +349,25 @@ class ProviderUPS(models.Model): 'error_message': check_value, 'warning_message': False, }] - - #ups_service_type = order.ups_service_type or self.ups_default_service_type - ups_service_type = None # See if this gets us all service types + # We now use Shop if we send multi=True + ups_service_type = self.ups_default_service_type result = srm.get_shipping_price( shipment_info=shipment_info, packages=packages, shipper=shipper_company, ship_from=shipper_warehouse, - ship_to=recipient, packaging_type=self.ups_default_packaging_id.shipper_package_code, service_type=ups_service_type, + ship_to=recipient, packaging_type=self.ups_default_package_type_id.shipper_package_code, service_type=ups_service_type, saturday_delivery=self.ups_saturday_delivery, cod_info=cod_info, date_planned=date_planned, multi=True) # Hibou Delivery is_third_party = self._get_ups_is_third_party(order=order, picking=picking) response = [] for rate in result: - if rate.get('error_message'): + if isinstance(rate, str): + # assume error + response.append({ + 'success': False, 'price': 0.0, + 'error_message': _('Error:\n%s') % rate, + 'warning_message': False, + }) + elif rate.get('error_message'): _logger.error('UPS error: %s' % rate['error_message']) response.append({ 'success': False, 'price': 0.0, @@ -334,6 +391,7 @@ class ProviderUPS(models.Model): if carrier: response.append({ 'carrier': carrier, + 'package': package or self.env['stock.quant.package'].browse(), 'success': True, 'price': price, 'error_message': False, 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 0215aa0c..d5f50259 100644 --- a/delivery_ups_hibou/models/ups_request_patch.py +++ b/delivery_ups_hibou/models/ups_request_patch.py @@ -1,10 +1,48 @@ # Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details. - +import os +from zeep import Client, Plugin from zeep.exceptions import Fault -from odoo.addons.delivery_ups.models.ups_request import UPSRequest +from odoo.addons.delivery_ups.models.ups_request import UPSRequest, FixRequestNamespacePlug, LogPlugin, Package import logging _logger = logging.getLogger(__name__) +TNT_CODE_MAP = { + 'GND': '03', + # TODO fill in the rest if needed.... +} + + +def patched__init__(self, debug_logger, username, password, shipper_number, access_number, prod_environment): + # patch to add the relative url for tnt_wsdl + self.debug_logger = debug_logger + # Product and Testing url + self.endurl = "https://onlinetools.ups.com/webservices/" + if not prod_environment: + self.endurl = "https://wwwcie.ups.com/webservices/" + + # Basic detail require to authenticate + self.username = username + self.password = password + self.shipper_number = shipper_number + self.access_number = access_number + + self.rate_wsdl = '../api/RateWS.wsdl' + self.ship_wsdl = '../api/Ship.wsdl' + self.void_wsdl = '../api/Void.wsdl' + self.tnt_wsdl = '../api/TNTWS.wsdl' + self.ns = {'err': "http://www.ups.com/XMLSchema/XOLTWS/Error/v1.1"} + + +def patched_set_client(self, wsdl, api, root): + # because of WSDL relative path we must patch + wsdl_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), wsdl) + client = Client('file:///%s' % wsdl_path.lstrip('/'), plugins=[FixRequestNamespacePlug(root), LogPlugin(self.debug_logger)]) + self.factory_ns1 = client.type_factory('ns1') + self.factory_ns2 = client.type_factory('ns2') + self.factory_ns3 = client.type_factory('ns3') + self._add_security_header(client, api) + return client + def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from, ship_to, packaging_type, service_type, saturday_delivery, cod_info, date_planned=False, multi=False): @@ -85,7 +123,7 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from shipment.ShipmentServiceOptions = '' shipment.ShipmentRatingOptions = self.factory_ns2.ShipmentRatingOptionsType() - shipment.ShipmentRatingOptions.NegotiatedRatesIndicator = 1 + shipment.ShipmentRatingOptions.NegotiatedRatesIndicator = '1' try: # Get rate using for provided detail @@ -98,43 +136,92 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from return [error_message] return error_message - if not multi: - rate = response.RatedShipment[0] - charge = rate.TotalCharges + if multi: + result = [] + tnt_response = None + tnt_ready = False + for rated_shipment in response.RatedShipment: + res = {} + res['service_code'] = rated_shipment.Service.Code + res['currency_code'] = rated_shipment.TotalCharges.CurrencyCode + negotiated_rate = 'NegotiatedRateCharges' in rated_shipment and rated_shipment.NegotiatedRateCharges.TotalCharge.MonetaryValue or None + + res['price'] = negotiated_rate or rated_shipment.TotalCharges.MonetaryValue + # Hibou Delivery + if hasattr(rated_shipment, 'GuaranteedDelivery') and hasattr(rated_shipment.GuaranteedDelivery, 'BusinessDaysInTransit'): + res['transit_days'] = int(rated_shipment.GuaranteedDelivery.BusinessDaysInTransit) + + if not res.get('transit_days') and date_planned: + if not tnt_response: + try: + tnt_client = self._set_client(self.tnt_wsdl, 'TimeInTransit', 'TimeInTransitRequest') + tnt_service = self._set_service(tnt_client, 'TimeInTransit') + tnt_request = self.factory_ns3.RequestType() + tnt_request.RequestOption = 'TNT' + + tnt_ship_from = self.factory_ns2.RequestShipFromType() + tnt_ship_from.Address = self.factory_ns2.RequestShipFromAddressType() + tnt_ship_from.AttentionName = (ship_from.name or '')[:35] + tnt_ship_from.Name = (ship_from.parent_id.name or ship_from.name or '')[:35] + tnt_ship_from.Address.AddressLine = [l for l in [ship_from.street or '', ship_from.street2 or ''] if l] + tnt_ship_from.Address.City = ship_from.city or '' + tnt_ship_from.Address.PostalCode = ship_from.zip or '' + tnt_ship_from.Address.CountryCode = ship_from.country_id.code or '' + if ship_from.country_id.code in ('US', 'CA', 'IE'): + tnt_ship_from.Address.StateProvinceCode = ship_from.state_id.code or '' + + tnt_ship_to = self.factory_ns2.RequestShipToType() + tnt_ship_to.Address = self.factory_ns2.RequestShipToAddressType() + tnt_ship_to.AttentionName = (ship_to.name or '')[:35] + tnt_ship_to.Name = (ship_to.parent_id.name or ship_to.name or '')[:35] + tnt_ship_to.Address.AddressLine = [l for l in [ship_to.street or '', ship_to.street2 or ''] if l] + tnt_ship_to.Address.City = ship_to.city or '' + tnt_ship_to.Address.PostalCode = ship_to.zip or '' + tnt_ship_to.Address.CountryCode = ship_to.country_id.code or '' + if ship_to.country_id.code in ('US', 'CA', 'IE'): + tnt_ship_to.Address.StateProvinceCode = ship_to.state_id.code or '' + if not ship_to.commercial_partner_id.is_company: + tnt_ship_to.Address.ResidentialAddressIndicator = None + + tnt_pickup = self.factory_ns2.PickupType() + tnt_pickup.Date = date_planned.split(' ')[0].replace('-', '') + tnt_pickup.Time = date_planned.split(' ')[1].replace(':', '') + + tnt_response = tnt_service.ProcessTimeInTransit(Request=tnt_request, + ShipFrom=tnt_ship_from, + ShipTo=tnt_ship_to, + Pickup=tnt_pickup) + tnt_ready = tnt_response.Response.ResponseStatus.Code == "1" + except Exception as e: + _logger.warning('exception during the UPS Time In Transit request. ' + str(e)) + _logger.exception(e) + tnt_ready = False + tnt_response = '-1' + if tnt_ready: + for service in tnt_response.TransitResponse.ServiceSummary: + if TNT_CODE_MAP.get(service.Service.Code) == service_type: + if hasattr(service, 'EstimatedArrival') and hasattr(service.EstimatedArrival, 'BusinessDaysInTransit'): + res['transit_days'] = int(service.EstimatedArrival.BusinessDaysInTransit) + break + # use TNT API to + result.append(res) + else: + result = {} + result['currency_code'] = response.RatedShipment[0].TotalCharges.CurrencyCode # Some users are qualified to receive negotiated rates - if 'NegotiatedRateCharges' in rate and rate.NegotiatedRateCharges and rate.NegotiatedRateCharges.TotalCharge.MonetaryValue: - charge = rate.NegotiatedRateCharges.TotalCharge - - result = { - 'currency_code': charge.CurrencyCode, - 'price': charge.MonetaryValue, - } + negotiated_rate = 'NegotiatedRateCharges' in response.RatedShipment[0] and response.RatedShipment[ + 0].NegotiatedRateCharges.TotalCharge.MonetaryValue or None + result['price'] = negotiated_rate or response.RatedShipment[0].TotalCharges.MonetaryValue # Hibou Delivery if hasattr(response.RatedShipment[0], 'GuaranteedDelivery') and hasattr(response.RatedShipment[0].GuaranteedDelivery, 'BusinessDaysInTransit'): result['transit_days'] = int(response.RatedShipment[0].GuaranteedDelivery.BusinessDaysInTransit) - # End - else: - result = [] - for rate in response.RatedShipment: - charge = rate.TotalCharges - # Some users are qualified to receive negotiated rates - if 'NegotiatedRateCharges' in rate and rate.NegotiatedRateCharges and rate.NegotiatedRateCharges.TotalCharge.MonetaryValue: - charge = rate.NegotiatedRateCharges.TotalCharge + if not result.get('transit_days') and date_planned: + # use TNT API to + _logger.warning(' We would now use the TNT service. But who would show the transit days? 2') - rated_shipment = { - 'currency_code': charge.CurrencyCode, - 'price': charge.MonetaryValue, - } - - # Hibou Delivery - if hasattr(rate, 'GuaranteedDelivery') and hasattr(rate.GuaranteedDelivery, 'BusinessDaysInTransit'): - rated_shipment['transit_days'] = int(rate.GuaranteedDelivery.BusinessDaysInTransit) - # End - rated_shipment['service_code'] = rate.Service.Code - result.append(rated_shipment) return result except Fault as e: @@ -151,4 +238,208 @@ def patched_get_shipping_price(self, shipment_info, packages, shipper, ship_from return error_message +def patched_send_shipping(self, shipment_info, packages, shipper, ship_from, ship_to, packaging_type, service_type, saturday_delivery, duty_payment, cod_info=None, label_file_type='GIF', ups_carrier_account=False): + client = self._set_client(self.ship_wsdl, 'Ship', 'ShipmentRequest') + request = self.factory_ns3.RequestType() + request.RequestOption = 'nonvalidate' + + request_type = "shipping" + label = self.factory_ns2.LabelSpecificationType() + label.LabelImageFormat = self.factory_ns2.LabelImageFormatType() + label.LabelImageFormat.Code = label_file_type + label.LabelImageFormat.Description = label_file_type + if label_file_type != 'GIF': + label.LabelStockSize = self.factory_ns2.LabelStockSizeType() + label.LabelStockSize.Height = '6' + label.LabelStockSize.Width = '4' + + shipment = self.factory_ns2.ShipmentType() + shipment.Description = shipment_info.get('description') + + for package in self.set_package_detail(client, packages, packaging_type, ship_from, ship_to, cod_info, request_type): + shipment.Package.append(package) + + shipment.Shipper = self.factory_ns2.ShipperType() + shipment.Shipper.Address = self.factory_ns2.ShipAddressType() + shipment.Shipper.AttentionName = (shipper.name or '')[:35] + shipment.Shipper.Name = (shipper.parent_id.name or shipper.name or '')[:35] + 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 = self.factory_ns2.ShipPhoneType() + shipment.Shipper.Phone.Number = self._clean_phone_number(shipper.phone) + + shipment.ShipFrom = self.factory_ns2.ShipFromType() + shipment.ShipFrom.Address = self.factory_ns2.ShipAddressType() + shipment.ShipFrom.AttentionName = (ship_from.name or '')[:35] + shipment.ShipFrom.Name = (ship_from.parent_id.name or ship_from.name or '')[:35] + 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 = self.factory_ns2.ShipPhoneType() + shipment.ShipFrom.Phone.Number = self._clean_phone_number(ship_from.phone) + + shipment.ShipTo = self.factory_ns2.ShipToType() + shipment.ShipTo.Address = self.factory_ns2.ShipToAddressType() + shipment.ShipTo.AttentionName = (ship_to.name or '')[:35] + shipment.ShipTo.Name = (ship_to.parent_id.name or ship_to.name or '')[:35] + 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 = self.factory_ns2.ShipPhoneType() + shipment.ShipTo.Phone.Number = self._clean_phone_number(shipment_info['phone']) + if not ship_to.commercial_partner_id.is_company: + shipment.ShipTo.Address.ResidentialAddressIndicator = None + + shipment.Service = self.factory_ns2.ServiceType() + 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 = self.factory_ns2.RateInfoType() + 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 = self.factory_ns2.CurrencyMonetaryType() + 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 = self.factory_ns2.PaymentInfoType() + shipcharge = self.factory_ns2.ShipmentChargeType() + shipcharge.Type = '01' + + # Bill Recevier 'Bill My Account' + if ups_carrier_account: + shipcharge.BillReceiver = self.factory_ns2.BillReceiverType() + shipcharge.BillReceiver.Address = self.factory_ns2.BillReceiverAddressType() + shipcharge.BillReceiver.AccountNumber = ups_carrier_account + shipcharge.BillReceiver.Address.PostalCode = ship_to.zip + else: + shipcharge.BillShipper = self.factory_ns2.BillShipperType() + shipcharge.BillShipper.AccountNumber = self.shipper_number or '' + + payment_info.ShipmentCharge = [shipcharge] + + if duty_payment == 'SENDER': + duty_charge = self.factory_ns2.ShipmentChargeType() + duty_charge.Type = '02' + duty_charge.BillShipper = self.factory_ns2.BillShipperType() + duty_charge.BillShipper.AccountNumber = self.shipper_number or '' + payment_info.ShipmentCharge.append(duty_charge) + + shipment.PaymentInformation = payment_info + + if saturday_delivery: + shipment.ShipmentServiceOptions = self.factory_ns2.ShipmentServiceOptionsType() + shipment.ShipmentServiceOptions.SaturdayDeliveryIndicator = saturday_delivery + else: + shipment.ShipmentServiceOptions = '' + self.shipment = shipment + self.label = label + self.request = request + self.label_file_type = label_file_type + + +def patched_set_package_detail(self, client, packages, packaging_type, ship_from, ship_to, cod_info, request_type): + Packages = [] + if request_type == "rating": + MeasurementType = self.factory_ns2.CodeDescriptionType + elif request_type == "shipping": + MeasurementType = self.factory_ns2.ShipUnitOfMeasurementType + for i, p in enumerate(packages): + package = self.factory_ns2.PackageType() + if hasattr(package, 'Packaging'): + package.Packaging = self.factory_ns2.PackagingType() + package.Packaging.Code = p.packaging_type or packaging_type or '' + elif hasattr(package, 'PackagingType'): + package.PackagingType = self.factory_ns2.CodeDescriptionType() + package.PackagingType.Code = p.packaging_type or packaging_type or '' + + if p.dimension_unit and any(p.dimension.values()): + package.Dimensions = self.factory_ns2.DimensionsType() + package.Dimensions.UnitOfMeasurement = MeasurementType() + 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 = self.factory_ns2.PackageServiceOptionsType() + package.PackageServiceOptions.COD = self.factory_ns2.CODType() + package.PackageServiceOptions.COD.CODFundsCode = str(cod_info['funds_code']) + package.PackageServiceOptions.COD.CODAmount = self.factory_ns2.CODAmountType() if request_type == 'rating' else self.factory_ns2.CurrencyMonetaryType() + package.PackageServiceOptions.COD.CODAmount.MonetaryValue = cod_info['monetary_value'] + package.PackageServiceOptions.COD.CODAmount.CurrencyCode = cod_info['currency'] + + # Hibou Insurance & Signature Requirement + if p.insurance_value: + if not package.PackageServiceOptions: + package.PackageServiceOptions = self.factory_ns2.PackageServiceOptionsType() + if not package.PackageServiceOptions.DeclaredValue: + if request_type == 'shipping': + package.PackageServiceOptions.DeclaredValue = self.factory_ns2.PackageDeclaredValueType() + else: + package.PackageServiceOptions.DeclaredValue = self.factory_ns2.ShipperDeclaredValueType() + package.PackageServiceOptions.DeclaredValue.MonetaryValue = p.insurance_value + package.PackageServiceOptions.DeclaredValue.CurrencyCode = p.insurance_currency_code + if p.signature_required: + if not package.PackageServiceOptions: + package.PackageServiceOptions = self.factory_ns2.PackageServiceOptionsType() + if not package.PackageServiceOptions.DeliveryConfirmation: + package.PackageServiceOptions.DeliveryConfirmation = self.factory_ns2.DeliveryConfirmationType() + package.PackageServiceOptions.DeliveryConfirmation.DCISType = p.signature_required + + package.PackageWeight = self.factory_ns2.PackageWeightType() + package.PackageWeight.UnitOfMeasurement = MeasurementType() + 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 = self.factory_ns2.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._set_client = patched_set_client 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 = carrier._ups_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.packaging_length, 'width': quant_pack.width, 'height': quant_pack.height} + else: + self.dimension = {'length': carrier.ups_default_package_type_id.packaging_length, 'width': carrier.ups_default_package_type_id.width, 'height': carrier.ups_default_package_type_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 + + + + + + + + +