Esempio n. 1
0
def create_from_oc(site_name, customer_id, oc_order=None):
    if oc_order:
        customer_id = oc_order.get('customer_id')

    success, oc_customer = oc_api.get(site_name).get_customer(customer_id)

    if not success:
        frappe.throw('Cannot get Customer from Opencart site. Error: %s' % oc_customer.get('error') or 'Unknown')

    customer_group_id = oc_customer.get('customer_group_id', '')
    doc_customer_group = customer_groups.get(site_name, customer_group_id)

    if not doc_customer_group:
        frappe.throw('Could not found Customer Group with customer_group_id "%s"' % customer_group_id)

    default_price_list = doc_customer_group.get('default_price_list')
    territory_name = territories.DEFAULT
    if oc_order:
        if oc_order.get('shipping_iso_code_3') and oc_order.get('shipping_zone_code'):
            territory_name = territories.get_by_iso_code3(oc_order.get('shipping_iso_code_3'), oc_order.get('shipping_zone_code'))
        else:
            territory_name = territories.get_by_iso_code3(oc_order.get('payment_iso_code_3'), oc_order.get('payment_zone_code'))
    # create new Customer
    params = {
        'doctype': 'Customer',
        'customer_type': 'Individual',
        'territory': territory_name,
        'customer_name': make_full_name(oc_order.get('firstname'), oc_order.get('lastname')),
        'customer_group': doc_customer_group.get('name'),
        'naming_series': 'CUST-',
        'default_price_list': default_price_list,
        'oc_guest': 0,
        'oc_is_updating': 1,
        'oc_site': site_name,
        'oc_customer_id': customer_id,
        'oc_store_id': oc_customer.get('store_id') or '',
        'oc_status': 1,
        'oc_sync_from': True,
        'oc_last_sync_from': datetime.now(),
        'oc_sync_to': True,
        'oc_last_sync_to': datetime.now(),
        'oc_firstname': oc_order.get('firstname'),
        'oc_lastname': oc_order.get('lastname'),
        'oc_telephone': oc_order.get('telephone'),
        'oc_fax': oc_order.get('fax'),
        'oc_email': oc_order.get('email')
    }
    doc_customer = frappe.get_doc(params)
    doc_customer.insert(ignore_permissions=True)

    # addresses and contacts
    addresses.create_or_update(site_name, oc_customer, doc_customer)
    contacts.create_or_update(site_name, oc_customer, doc_customer)
    if oc_order:
        # addresses and contacts
        addresses.create_or_update_from_order(site_name, doc_customer, oc_order)
        contacts.create_or_update_from_order(site_name, doc_customer, oc_order)

    return doc_customer
Esempio n. 2
0
def create_guest_from_order(site_name, oc_order):
    doc_store = oc_stores.get(site_name, oc_order.get('store_id'))
    if doc_store:
        customer_group_name = doc_store.get('oc_customer_group')
        if customer_group_name:
            doc_customer_group = frappe.get_doc('Customer Group', customer_group_name)
            default_price_list = doc_customer_group.get('default_price_list')
            if oc_order.get('shipping_iso_code_3') and oc_order.get('shipping_zone_code'):
                territory_name = territories.get_by_iso_code3(oc_order.get('shipping_iso_code_3'), oc_order.get('shipping_zone_code'))
            else:
                territory_name = territories.get_by_iso_code3(oc_order.get('payment_iso_code_3'), oc_order.get('payment_zone_code'))
            # create new Customer
            params = {
                'doctype': 'Customer',
                'customer_type': 'Individual',
                'territory': territory_name,
                'customer_name': make_full_name(oc_order.get('firstname'), oc_order.get('lastname')),
                'customer_group': doc_customer_group.get('name'),
                'naming_series': 'CUST-',
                'default_price_list': default_price_list,
                'oc_guest': 1,
                'oc_is_updating': 1,
                'oc_site': site_name,
                'oc_customer_id': oc_order.get('customer_id'),
                'oc_store_id': oc_order.get('store_id') or '',
                'oc_status': 1,
                'oc_sync_from': True,
                'oc_last_sync_from': datetime.now(),
                'oc_sync_to': True,
                'oc_last_sync_to': datetime.now(),
                'oc_firstname': oc_order.get('firstname'),
                'oc_lastname': oc_order.get('lastname'),
                'oc_telephone': oc_order.get('telephone'),
                'oc_fax': oc_order.get('fax'),
                'oc_email': oc_order.get('email')
            }
            doc_customer = frappe.get_doc(params)
            doc_customer.insert(ignore_permissions=True)

            # addresses and contacts
            addresses.create_or_update_from_order(site_name, doc_customer, oc_order)
            contacts.create_or_update_from_order(site_name, doc_customer, oc_order)

            return doc_customer
        else:
            frappe.throw('Customer Group is not set in Opencart Store "%s"' % doc_store.get('name'))
    else:
        frappe.throw('Opencart Store with store_id "%s" does not exist' % oc_order.get('store_id'))
Esempio n. 3
0
def update_guest_from_order(doc_customer, oc_order):
    site_name = doc_customer.get('oc_site')
    doc_store = oc_stores.get(site_name, oc_order.get('store_id'))
    if doc_store:
        customer_group_name = doc_store.get('oc_customer_group')
        if customer_group_name:
            doc_customer_group = frappe.get_doc('Customer Group', customer_group_name)
            if oc_order.get('shipping_iso_code_3') and oc_order.get('shipping_zone_code'):
                territory_name = territories.get_by_iso_code3(oc_order.get('shipping_iso_code_3'), oc_order.get('shipping_zone_code'))
            else:
                territory_name = territories.get_by_iso_code3(oc_order.get('payment_iso_code_3'), oc_order.get('payment_zone_code'))
            # create new Customer
            doc_customer.update({
                'territory': territory_name,
                'customer_name': make_full_name(oc_order.get('firstname'), oc_order.get('lastname')),
                'customer_group': doc_customer_group.get('name'),
                'oc_is_updating': 1,
                'oc_customer_id': oc_order.get('customer_id'),
                'oc_store_id': oc_order.get('store_id') or '',
                'oc_last_sync_from': datetime.now(),
                'oc_firstname': oc_order.get('firstname'),
                'oc_lastname': oc_order.get('lastname'),
                'oc_telephone': oc_order.get('telephone'),
                'oc_fax': oc_order.get('fax'),
                'oc_email': oc_order.get('email')
            })
            doc_customer.save()

            # addresses and contacts
            addresses.create_or_update_from_order(site_name, doc_customer, oc_order)
            contacts.create_or_update_from_order(site_name, doc_customer, oc_order)

            return doc_customer
        else:
            frappe.throw('Customer Group is not set in Opencart Store "%s"' % doc_store.get('name'))
    else:
        frappe.throw('Opencart Store with store_id "%s" does not exist' % oc_order.get('store_id'))