Esempio n. 1
0
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)
Esempio n. 2
0
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)
Esempio n. 3
0
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 []
Esempio n. 4
0
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)