def get_sendcloud_available_services(delivery_address_name, shipment_parcel): try: api_key, api_password = frappe.db.get_value( 'Shipment Service Provider', 'SendCloud', ['api_key', 'api_password']) url = 'https://panel.sendcloud.sc/api/v2/shipping_methods' responses = requests.get(url, auth=(api_key, api_password)) responses_dict = json.loads(responses.text) delivery_address = get_address(delivery_address_name) available_services = [] for service in responses_dict['shipping_methods']: for country in service['countries']: if country['iso_2'] == delivery_address.country_code: available_service = frappe._dict() available_service.service_provider = 'SendCloud' available_service.carrier = service['carrier'] available_service.service_name = service['name'] available_service.total_price = total_parcel_price( country['price'], json.loads(shipment_parcel)) available_service.service_id = service['id'] available_services.append(available_service) return available_services except Exception as exc: frappe.msgprint(_('Error occurred on SendCloud: {0}').format(str(exc)), indicator='orange', alert=True)
def create_letmeship_shipment(pickup_from_type, delivery_to_type, pickup_address_name, delivery_address_name, shipment_parcel, description_of_content, pickup_date, value_of_goods, service_info, shipment_notific_email, tracking_notific_email, pickup_contact_name=None, delivery_contact_name=None, pickup_type=None): pickup_address = get_address(pickup_address_name) delivery_address = get_address(delivery_address_name) if pickup_from_type != 'Company': pickup_contact = get_contact(pickup_contact_name) else: pickup_contact = get_company_contact() if delivery_to_type != 'Company': delivery_contact = get_contact(delivery_contact_name) else: delivery_contact = get_company_contact() # LetMeShip have limit of 30 characters for Company field if len(pickup_address.address_title) > 30: pickup_address.address_title = pickup_address.address_title[:30] if len(delivery_address.address_title) > 30: delivery_address.address_title = \ delivery_address.address_title[:30] pickupOrder = False if pickup_type and pickup_type == "Pickup": pickupOrder = True parcel_list = get_parcel_list(json.loads(shipment_parcel), description_of_content) service_provider = frappe.db.get_value('Shipment Service Provider', 'Let Me Ship', ['api_key', 'api_password'], as_dict=1) if not service_provider: return [] url = 'https://api.letmeship.com/v1/shipments' headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Access-Control-Allow-Origin': 'string' } payload = { 'pickupInfo': { 'address': { 'countryCode': pickup_address.country_code, 'zip': pickup_address.pincode, 'city': pickup_address.city, 'street': pickup_address.address_line1, 'addressInfo1': pickup_address.address_line2, 'houseNo': '', }, 'company': pickup_address.address_title, 'person': { 'title': pickup_contact.title, 'firstname': pickup_contact.first_name, 'lastname': pickup_contact.last_name }, 'phone': { 'phoneNumber': pickup_contact.phone, 'phoneNumberPrefix': pickup_contact.phone_prefix }, 'email': pickup_contact.email, }, 'deliveryInfo': { 'address': { 'countryCode': delivery_address.country_code, 'zip': delivery_address.pincode, 'city': delivery_address.city, 'street': delivery_address.address_line1, 'addressInfo1': delivery_address.address_line2, 'houseNo': '', }, 'company': delivery_address.address_title, 'person': { 'title': delivery_contact.title, 'firstname': delivery_contact.first_name, 'lastname': delivery_contact.last_name }, 'phone': { 'phoneNumber': delivery_contact.phone, 'phoneNumberPrefix': delivery_contact.phone_prefix }, 'email': delivery_contact.email, }, 'service': { 'baseServiceDetails': { 'id': service_info['id'], 'name': service_info['service_name'], 'carrier': service_info['carrier'], 'priceInfo': service_info['price_info'], }, 'supportedExWorkType': [], 'messages': [''], 'description': '', 'serviceInfo': '', }, 'shipmentDetails': { 'contentDescription': description_of_content, 'shipmentType': 'PARCEL', 'shipmentSettings': { 'saturdayDelivery': False, 'ddp': False, 'insurance': False, 'pickupOrder': pickupOrder, 'pickupTailLift': False, 'deliveryTailLift': False, 'holidayDelivery': False, }, 'goodsValue': value_of_goods, 'parcelList': parcel_list, 'pickupInterval': { 'date': pickup_date }, 'contentDescription': description_of_content, }, 'shipmentNotification': { 'trackingNotification': { 'deliveryNotification': True, 'problemNotification': True, 'emails': [tracking_notific_email], 'notificationText': '', }, 'recipientNotification': { 'notificationText': '', 'emails': [shipment_notific_email] } }, 'labelEmail': True, } try: response_data = requests.post(url=url, auth=(service_provider.api_key, service_provider.api_password), headers=headers, data=json.dumps(payload)) response_data = json.loads(response_data.text) if 'shipmentId' in response_data: base_price = response_data['service']['priceInfo']['basePrice'] net_price = response_data['service']['priceInfo']['netPrice'] total_vat = response_data['service']['priceInfo']['totalVat'] shipment_amount = response_data['service']['priceInfo'][ 'totalPrice'] awb_number = '' tracking_response = \ requests.get('https://api.letmeship.com/v1/shipments/{id}'.format(id=response_data['shipmentId' ]), auth=(service_provider.api_key, service_provider.api_password), headers=headers) tracking_response_data = json.loads(tracking_response.text) if 'trackingData' in tracking_response_data: for parcel in tracking_response_data['trackingData'][ 'parcelList']: if 'awbNumber' in parcel: awb_number = parcel['awbNumber'] return { 'service_provider': 'LetMeShip', 'shipment_id': response_data['shipmentId'], 'carrier': service_info['carrier'], 'carrier_service': service_info['service_name'], 'base_price': base_price, 'net_price': net_price, 'total_vat': total_vat, 'shipment_amount': shipment_amount, 'awb_number': awb_number, } elif 'message' in response_data: frappe.throw( _('Error occurred while creating Shipment: {0}').format( response_data['message'])) except Exception as exc: frappe.msgprint( _('Error occurred while creating Shipment: {0}').format(str(exc)), indicator='orange', alert=True)
def get_letmeship_available_services(pickup_from_type, delivery_to_type, pickup_address_name, delivery_address_name, shipment_parcel, description_of_content, pickup_date, value_of_goods, pickup_contact_name=None, delivery_contact_name=None, pickup_type=None): pickup_address = get_address(pickup_address_name) delivery_address = get_address(delivery_address_name) if pickup_from_type != 'Company': pickup_contact = get_contact(pickup_contact_name) else: pickup_contact = get_company_contact() if delivery_to_type != 'Company': delivery_contact = get_contact(delivery_contact_name) else: delivery_contact = get_company_contact() # LetMeShip have limit of 30 characters for Company field if len(pickup_address.address_title) > 30: pickup_address.address_title = pickup_address.address_title[:30] if len(delivery_address.address_title) > 30: delivery_address.address_title = \ delivery_address.address_title[:30] pickupOrder = False if pickup_type and pickup_type == "Pickup": pickupOrder = True parcel_list = get_parcel_list(json.loads(shipment_parcel), description_of_content) service_provider = frappe.db.get_value('Shipment Service Provider', 'Let Me Ship', ['api_key', 'api_password'], as_dict=1) if not service_provider: return [] url = 'https://api.letmeship.com/v1/available' headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Access-Control-Allow-Origin': 'string' } payload = { 'pickupInfo': { 'address': { 'countryCode': pickup_address.country_code, 'zip': pickup_address.pincode, 'city': pickup_address.city, 'street': pickup_address.address_line1, 'addressInfo1': pickup_address.address_line2, 'houseNo': '', }, 'company': pickup_address.address_title, 'person': { 'title': pickup_contact.title, 'firstname': pickup_contact.first_name, 'lastname': pickup_contact.last_name }, 'phone': { 'phoneNumber': pickup_contact.phone, 'phoneNumberPrefix': pickup_contact.phone_prefix }, 'email': pickup_contact.email, }, 'deliveryInfo': { 'address': { 'countryCode': delivery_address.country_code, 'zip': delivery_address.pincode, 'city': delivery_address.city, 'street': delivery_address.address_line1, 'addressInfo1': delivery_address.address_line2, 'houseNo': '', }, 'company': delivery_address.address_title, 'person': { 'title': delivery_contact.title, 'firstname': delivery_contact.first_name, 'lastname': delivery_contact.last_name }, 'phone': { 'phoneNumber': delivery_contact.phone, 'phoneNumberPrefix': delivery_contact.phone_prefix }, 'email': delivery_contact.email, }, 'shipmentDetails': { 'contentDescription': description_of_content, 'shipmentType': 'PARCEL', 'shipmentSettings': { 'saturdayDelivery': False, 'ddp': False, 'insurance': False, 'pickupOrder': pickupOrder, 'pickupTailLift': False, 'deliveryTailLift': False, 'holidayDelivery': False, }, 'goodsValue': value_of_goods, 'parcelList': parcel_list, 'pickupInterval': { 'date': pickup_date }, } } try: available_services = [] response_data = requests.post(url=url, auth=(service_provider.api_key, service_provider.api_password), headers=headers, data=json.dumps(payload)) response_data = json.loads(response_data.text) if 'serviceList' in response_data: for response in response_data['serviceList']: available_service = frappe._dict() basic_info = response['baseServiceDetails'] price_info = basic_info['priceInfo'] available_service.service_provider = 'LetMeShip' available_service.id = basic_info['id'] available_service.carrier = basic_info['carrier'] available_service.service_name = \ match_parcel_service_type_alias(basic_info['name'], basic_info['carrier']) available_service.is_preferred = \ frappe.db.get_value('Parcel Service Type', available_service.service_name, 'show_in_preferred_services_list') available_service.real_weight = price_info['realWeight'] available_service.total_price = price_info['netPrice'] available_service.price_info = price_info available_services.append(available_service) return available_services else: frappe.throw( _('Error occurred while fetching LetMeShip prices: {0}'). format(response_data['message'])) except Exception as exc: frappe.msgprint( _('Error occurred while fetching LetMeShip Prices: {0}').format( str(exc)), indicator='orange', alert=True) return []
def get_packlink_available_services( pickup_address_name, delivery_address_name, shipment_parcel, pickup_date, ): pickup_address = get_address(pickup_address_name) from_zip = pickup_address.pincode from_country_code = pickup_address.country_code delivery_address = get_address(delivery_address_name) to_zip = delivery_address.pincode to_country_code = delivery_address.country_code shipment_parcel_params = '' for (index, parcel) in \ enumerate(packlink_get_parcel_list(json.loads(shipment_parcel))): shipment_parcel_params += \ 'packages[{index}][height]={height}&packages[{index}][length]={length}&packages[{index}][weight]={weight}&packages[{index}][width]={width}&'.format(index=index, height=parcel[ 'height'], length=parcel['length'], weight=parcel['weight'], width=parcel['width']) url = \ 'https://api.packlink.com/v1/services?from[country]={}&from[zip]={}&to[country]={}&to[zip]={}&{}sortBy=totalPrice&source=PRO'.format(from_country_code, from_zip, to_country_code, to_zip, shipment_parcel_params) api_key = frappe.db.get_value('Shipment Service Provider', 'Packlink', 'api_key') if not api_key: return [] try: responses = requests.get(url, headers={'Authorization': api_key}) responses_dict = json.loads(responses.text) # If an error occured on the api. Show the error message if 'messages' in responses_dict: frappe.msgprint(_('Packlink: {0}'.format(str(responses_dict['messages' ][0]['message']))), indicator='orange', alert=True) available_services = [] for response in responses_dict: if parse_pickup_date(pickup_date) \ in response['available_dates'].keys(): available_service = frappe._dict() available_service.service_provider = 'Packlink' available_service.carrier = response['carrier_name'] available_service.service_name = \ match_parcel_service_type_alias(response['name'], response['carrier_name']) available_service.is_preferred = \ frappe.db.get_value('Parcel Service Type', available_service.service_name, 'show_in_preferred_services_list') available_service.base_price = response['price']['base_price'] available_service.net_price = response['price']['base_price'] available_service.total_vat = response['price']['tax_price'] available_service.actual_price = response['price']['total_price'] available_service.total_price = response['price']['base_price'] available_service.service_id = response['id'] available_service.available_dates = \ response['available_dates'] available_services.append(available_service) return available_services except Exception as exc: frappe.msgprint(_('Error occurred on Packlink: {0}' ).format(str(exc)), indicator='orange', alert=True) return []
def create_packlink_shipment( pickup_from_type, delivery_to_type, pickup_address_name, delivery_address_name, shipment_parcel, description_of_content, pickup_date, value_of_goods, pickup_contact_name, delivery_contact_name, service_info, ): api_key = frappe.db.get_value('Shipment Service Provider', 'Packlink', 'api_key') pickup_address = get_address(pickup_address_name) from_country_code = pickup_address.country_code if pickup_from_type != 'Company': pickup_contact = get_contact(pickup_contact_name) else: pickup_contact = get_company_contact() delivery_address = get_address(delivery_address_name) to_country_code = delivery_address.country_code if delivery_to_type != 'Company': delivery_contact = get_contact(delivery_contact_name) else: delivery_contact = get_company_contact() data = { 'additional_data': { 'postal_zone_id_from': '', 'postal_zone_name_from': pickup_address.country, 'postal_zone_id_to': '', 'postal_zone_name_to': delivery_address.country, }, 'collection_date': parse_pickup_date(pickup_date), 'collection_time': '', 'content': description_of_content, 'contentvalue': value_of_goods, 'content_second_hand': False, 'from': { 'city': pickup_address.city, 'company': pickup_address.address_title, 'country': from_country_code, 'email': pickup_contact.email, 'name': pickup_contact.first_name, 'phone': pickup_contact.phone, 'state': pickup_address.country, 'street1': pickup_address.address_line1, 'street2': pickup_address.address_line2, 'surname': pickup_contact.last_name, 'zip_code': pickup_address.pincode, }, 'insurance': {'amount': 0, 'insurance_selected': False}, 'price': {}, 'packages': packlink_get_parcel_list(json.loads(shipment_parcel)), 'service_id': service_info['service_id'], 'to': { 'city': delivery_address.city, 'company': delivery_address.address_title, 'country': to_country_code, 'email': delivery_contact.email, 'name': delivery_contact.first_name, 'phone': delivery_contact.phone, 'state': delivery_address.country, 'street1': delivery_address.address_line1, 'street2': delivery_address.address_line2, 'surname': delivery_contact.last_name, 'zip_code': delivery_address.pincode, }, } url = 'https://api.packlink.com/v1/shipments' headers = {'Authorization': api_key, 'Content-Type': 'application/json'} try: response_data = requests.post(url, json=data, headers=headers) response_data = json.loads(response_data.text) if 'reference' in response_data: return { 'service_provider': 'Packlink', 'shipment_id': response_data['reference'], 'carrier': service_info['carrier'], 'carrier_service': service_info['service_name'], 'base_price': service_info['base_price'], 'net_price': service_info['net_price'], 'total_vat': service_info['total_vat'], 'shipment_amount': service_info['actual_price'], 'awb_number': '', } except Exception as exc: frappe.msgprint(_('Error occurred while creating Shipment: {0}' ).format(str(exc)), indicator='orange', alert=True)
def create_sendcloud_shipment(shipment, delivery_to_type, delivery_address_name, delivery_contact_name, service_info, shipment_parcel, description_of_content, value_of_goods): delivery_address = get_address(delivery_address_name) to_country_code = delivery_address.country_code if delivery_to_type != 'Company': delivery_contact = get_contact(delivery_contact_name) else: delivery_contact = get_company_contact() parcels = [] for i, parcel in enumerate(json.loads(shipment_parcel), start=1): parcel_data = { 'name': "{} {}".format(delivery_contact.first_name, delivery_contact.last_name), 'company_name': delivery_address.address_title, 'address': delivery_address.address_line1, 'address_2': delivery_address.address_line2, 'city': delivery_address.city, 'postal_code': delivery_address.pincode, 'telephone': delivery_contact.phone, 'request_label': True, 'email': delivery_contact.email, 'data': [], 'country': to_country_code, 'shipment': { 'id': service_info['service_id'] }, 'order_number': "{}-{}".format(shipment, i), 'external_reference': "{}-{}".format(shipment, i), 'weight': parcel.get('weight'), 'parcel_items': get_parcel_items(parcel, description_of_content, value_of_goods) } parcels.append(parcel_data) data = {'parcels': parcels} try: url = 'https://panel.sendcloud.sc/api/v2/parcels?errors=verbose' api_key, api_password = frappe.db.get_value( 'Shipment Service Provider', 'SendCloud', ['api_key', 'api_password']) response_data = requests.post(url, json=data, auth=(api_key, api_password)) response_data = json.loads(response_data.text) print(response_data) if hasattr(response_data, 'failed_parcels'): frappe.msgprint( _('Error occurred while creating Shipment: {0}').format( response_data['failed_parcels'][0]['errors']), indicator='orange', alert=True) else: shipment_id = ', '.join( [str(x['id']) for x in response_data['parcels']]) awb_number = ', '.join( [str(x['tracking_number']) for x in response_data['parcels']]) return { 'service_provider': 'SendCloud', 'shipment_id': shipment_id, 'carrier': service_info['carrier'], 'carrier_service': service_info['service_name'], 'shipment_amount': service_info['total_price'], 'awb_number': awb_number } except Exception as exc: frappe.msgprint( _('Error occurred while creating Shipment: {0}').format(str(exc)), indicator='orange', alert=True)