Ejemplo 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
Ejemplo n.º 2
0
def update_or_create_item_discount(site_name, doc_item, oc_discount, save=False, is_updating=False):
    disc_template = '{customer_group_id}-{quantity}-{priority}-{date_start}-{date_end}'

    oc_discount_copy = dict(oc_discount)
    oc_discount_copy.update({
        'date_start': parse_date(str(oc_discount.get('date_start'))) if oc_discount.get('date_start') else '',
        'date_end': parse_date(str(oc_discount.get('date_end'))) if oc_discount.get('date_end') else ''
    })
    oc_discount_hash = disc_template.format(**oc_discount_copy)
    # frappe.msgprint('items::update_or_create_item_discount')
    for doc_oc_discount in doc_item.get('oc_discounts'):
        if site_name != doc_oc_discount.get('oc_site'):
            continue
        doc_customer_group = frappe.get_doc('Customer Group', doc_oc_discount.get('customer_group'))
        if site_name != doc_customer_group.get('oc_site'):
            continue
        customer_group_id = doc_customer_group.get('oc_customer_group_id')
        if not customer_group_id:
            frappe.throw('customer_group_id is not set in Customer Group "%s"' % doc_oc_discount.get('customer_group'))
        doc_discount_hash = disc_template.format(**{
            'customer_group_id': customer_group_id,
            # 'price': doc_oc_discount.get('price'),
            'priority': int(doc_oc_discount.get('priority', 0)),
            'quantity': int(doc_oc_discount.get('quantity', 0)),
            'date_start': parse_date(str(doc_oc_discount.get('date_start'))) if doc_oc_discount.get('date_start') else '',
            'date_end': parse_date(str(doc_oc_discount.get('date_end'))) if doc_oc_discount.get('date_end') else '',
        })
        if oc_discount_hash == doc_discount_hash:
            doc_oc_discount.update({'price': oc_discount.get('price')})
            update_discount_prices(doc_oc_discount, oc_discount)
            doc_oc_discount.save()
            break
    else:
        doc_customer_group = customer_groups.get(site_name, oc_discount.get('customer_group_id'))
        if not doc_customer_group:
            frappe.throw('Cannot not found Customer Group with customer_group_id "%s" for Item "%s"' % (customer_group_id, doc_item.get('name')))
        doc_oc_discount = frappe.get_doc({
            'doctype': 'Opencart Discount',
            'oc_site': site_name,
            'item_name': doc_item.get('name'),
            'customer_group': doc_customer_group.get('name'),
            'quantity': oc_discount.get('quantity'),
            'priority': oc_discount.get('priority'),
            'price': oc_discount.get('price'),
            'date_start': oc_discount.get('date_start'),
            'date_end': oc_discount.get('date_end'),
        })
        update_discount_prices(doc_oc_discount, oc_discount)
        doc_item.append('oc_discounts', doc_oc_discount)
        if is_updating:
            doc_item.update({'oc_is_updating': 1})
        if save:
            doc_item.save()
Ejemplo n.º 3
0
def pull_products_from_oc(site_name, silent=False):
    results = {}
    results_list = []
    check_count = 0
    update_count = 0
    add_count = 0
    skip_count = 0
    success = True

    site_doc = frappe.get_doc('Opencart Site', site_name)
    opencart_api = oc_api.get(site_name)
    items_default_warehouse = site_doc.get('items_default_warehouse')
    if not items_default_warehouse:
        sync_info([], 'Please specify a Default Warehouse and proceed.', stop=True, silent=silent)

    for oc_category in opencart_api.get_all_categories():
        doc_item_group = item_groups.get_item_group(site_name, oc_category.id)
        for oc_product in opencart_api.get_products_by_category(oc_category.id):
            check_count += 1
            item_code = oc_product.get('model', '').upper()
            doc_item = get_item_by_item_code(item_code)

            # skip product if it is disabled on Opencart site
            if not int(oc_product.get('status') or 0):
                skip_count += 1
                extras = (1, 'skipped', 'Skipped: item with Item No. "%s" is disabled on Opencart site' % item_code)
                results_list.append((oc_product.get('name'), '', oc_product.get('product_id'), '', '') + extras)
                continue

            if doc_item_group:
                if doc_item:
                    try:
                        update_item(site_name, doc_item, oc_product, item_group=doc_item_group.get('name'))
                    except Exception as ex:
                        skip_count += 1
                        extras = (1, 'skipped', 'Skipped: due to exception: %s' % str(ex))
                        results_list.append((oc_product.get('name'), '', oc_product.get('product_id'), '', '') + extras)
                        continue

                    update_count += 1
                    extras = (1, 'updated', 'Updated')
                    results_list.append((doc_item.get('name'),
                                         doc_item_group.get('name'),
                                         oc_product.get('product_id'),
                                         doc_item.get_formatted('oc_last_sync_from'),
                                         doc_item.get('modified')) + extras)
                else:
                    skip_count += 1
                    extras = (1, 'skipped', 'Skipped: cannot found item with Item No. "%s"' % item_code)
                    results_list.append((oc_product.get('name'), '', oc_product.get('product_id'), '', '') + extras)
                    continue

                    params = {
                        'doctype': 'Item',
                        'item_code': oc_product.get('model'),
                        'item_group': doc_item_group.get('name'),
                        'is_group': 'No',
                        'default_warehouse': items_default_warehouse,
                        'item_name': oc_product.get('name'),
                        'description': oc_product.get('description'),
                        'show_in_website': 1,
                        'image': oc_product.get('image'),
                        'min_order_qty': oc_product.get('minimum'),
                        'oc_is_updating': 1,
                        'oc_site': site_name,
                        'oc_product_id': oc_product.get('product_id'),
                        'oc_manufacturer_id': oc_product.get('manufacturer_id'),
                        'oc_tax_class_id': oc_product.get('tax_class_id'),
                        'oc_stock_status': oc_product.get('stock_status'),
                        'oc_model': oc_product.get('model'),
                        'oc_sku': oc_product.get('sku'),
                        'oc_quantity': oc_product.get('quantity'),
                        'oc_status': int(oc_product.get('status') or 0),
                        'oc_meta_title': oc_product.get('meta_title'),
                        'oc_meta_keyword': oc_product.get('meta_keyword'),
                        'oc_meta_description': oc_product.get('meta_description'),
                        'price': oc_product.get('price'),
                        'oc_sync_from': True,
                        'oc_last_sync_from': datetime.now(),
                        'oc_sync_to': True,
                        'oc_last_sync_to': datetime.now()
                    }
                    doc_item = frappe.get_doc(params)
                    doc_item.insert(ignore_permissions=True)

                    # discounts
                    for oc_discount in oc_product.get('discounts'):
                        customer_group = customer_groups.get(site_name, oc_discount.get('customer_group_id'))
                        if not customer_group:
                            continue
                        doc_item.append('oc_discounts', {
                            'item_name': doc_item.get('name'),
                            'customer_group': customer_group.get('name'),
                            'quantity': oc_discount.get('quantity'),
                            'priority': oc_discount.get('priority'),
                            'price': oc_discount.get('price'),
                            'date_start': oc_discount.get('date_start'),
                            'date_end': oc_discount.get('date_end'),
                        })

                    # cpesials
                    for oc_special in oc_product.get('special'):
                        customer_group = customer_groups.get(site_name, oc_special.get('customer_group_id'))
                        if not customer_group:
                            continue
                        doc_item.append('oc_specials', {
                            'item_name': doc_item.get('name'),
                            'customer_group': customer_group.get('name'),
                            'priority': oc_special.get('priority'),
                            'price': oc_special.get('price'),
                            'date_start': oc_special.get('date_start'),
                            'date_end': oc_special.get('date_end'),
                        })
                    doc_item.update({'oc_is_updating': 1})
                    doc_item.save()
                    add_count += 1
                    extras = (1, 'added', 'Added')
                    results_list.append((doc_item.get('name'),
                                        doc_item_group.get('name'),
                                        oc_product.get('product_id'),
                                        doc_item.get_formatted('oc_last_sync_from'),
                                        doc_item.get('modified')) + extras)
            else:
                skip_count += 1
                extras = (1, 'skipped', 'Skipped: missed parent category')
                results_list.append((oc_product.get('name'), '', oc_product.get('product_id'), '', '') + extras)

    results = {
        'check_count': check_count,
        'add_count': add_count,
        'update_count': update_count,
        'skip_count': skip_count,
        'results': results_list,
        'success': success,
    }
    return results
Ejemplo n.º 4
0
def pull_product_from_oc(site_name, item_name, doc_item=None, silent=False):
    results = {}
    success = True
    status = None

    if doc_item is None:
        doc_item = frappe.get_doc('Item', item_name)

    site_doc = frappe.get_doc('Opencart Site', site_name)
    items_default_warehouse = site_doc.get('items_default_warehouse')
    doc_oc_product = get_opencart_product(site_name, doc_item.get('name'))
    success, oc_product = oc_api.get(site_name).get_product(doc_oc_product.get('oc_product_id'))

    if not success:
        results = {
            'success': success,
            'status': 'Cannot get product from Opencart site'
        }
        return results

    if doc_item:
        update_item(site_name, doc_item, oc_product)
        extras = (1, 'updated', 'Updated')
        status = (doc_item.get('name'),
                  '',  # doc_item_group.get('name'),
                  doc_item.get('oc_product_id'),
                  doc_item.get_formatted('oc_last_sync_from'),
                  doc_item.get('modified')) + extras
    else:
        params = {
            'doctype': 'Item',
            'item_code': oc_product.get('model'),
            # 'item_group': doc_item_group.get('name'),
            'is_group': 'No',
            'default_warehouse': items_default_warehouse,
            'item_name': oc_product.get('name'),
            'description': oc_product.get('description'),
            'show_in_website': 1,
            'image': oc_product.get('image'),
            'min_order_qty': oc_product.get('minimum'),
            'oc_is_updating': 1,
            'oc_site': site_name,
            'oc_product_id': oc_product.get('product_id'),
            'oc_manufacturer_id': oc_product.get('manufacturer_id'),
            'oc_tax_class_id': oc_product.get('tax_class_id'),
            'oc_stock_status': oc_product.get('stock_status'),
            'oc_model': oc_product.get('model'),
            'oc_sku': oc_product.get('sku'),
            'oc_quantity': oc_product.get('quantity'),
            'oc_status': int(oc_product.get('status') or 0),
            'oc_meta_title': oc_product.get('meta_title'),
            'oc_meta_keyword': oc_product.get('meta_keyword'),
            'oc_meta_description': oc_product.get('meta_description'),
            'price': oc_product.get('price'),
            'oc_sync_from': True,
            'oc_last_sync_from': datetime.now(),
            'oc_sync_to': True,
            'oc_last_sync_to': datetime.now()
        }
        doc_item = frappe.get_doc(params)
        doc_item.insert(ignore_permissions=True)

        # discounts
        for oc_discount in oc_product.get('discounts'):
            customer_group = customer_groups.get(site_name, oc_discount.get('customer_group_id'))
            if not customer_group:
                continue
            doc_item.append('oc_discounts', {
                'item_name': doc_item.get('name'),
                'customer_group': customer_group.get('name'),
                'quantity': oc_discount.get('quantity'),
                'priority': oc_discount.get('priority'),
                'price': oc_discount.get('price'),
                'date_start': oc_discount.get('date_start'),
                'date_end': oc_discount.get('date_end'),
            })

        # cpesials
        for oc_special in oc_product.get('special'):
            customer_group = customer_groups.get(site_name, oc_special.get('customer_group_id'))
            if not customer_group:
                continue
            doc_item.append('oc_specials', {
                'item_name': doc_item.get('name'),
                'customer_group': customer_group.get('name'),
                'priority': oc_special.get('priority'),
                'price': oc_special.get('price'),
                'date_start': oc_special.get('date_start'),
                'date_end': oc_special.get('date_end'),
            })
        doc_item.update({'oc_is_updating': 1})
        doc_item.save()
        extras = (1, 'added', 'Added')
        status = (doc_item.get('name'),
                  '',  # doc_item_group.get('name'),
                  doc_item.get('oc_product_id'),
                  doc_item.get_formatted('oc_last_sync_from'),
                  doc_item.get('modified')) + extras
    results = {
        'success': success,
        'status': status
    }
    return results
Ejemplo n.º 5
0
def pull_customers_from_oc(site_name, silent=False):
    '''Sync customers from Opencart site'''
    results = {}
    results_list = []
    check_count = 0
    update_count = 0
    add_count = 0
    skip_count = 0
    success = False

    site_doc = frappe.get_doc('Opencart Site', site_name)
    # root_customer_group = site_doc.get('root_customer_group')
    default_customer_territory = site_doc.get('default_customer_territory') or 'All Territories'
    doc_customer_groups_cache = {}
    for success, oc_customer in oc_api.get(site_name).get_customers():
    # success, oc_customer = oc_api.get(site_name).get_customer('1332')
    # while True:
    #     if check_count >= 1:
    #         break
        check_count += 1

        oc_customer_name = make_full_name(oc_customer.get('firstname'), oc_customer.get('lastname'))
        customer_id = oc_customer.get('customer_id')

        # validation
        # missed_mandatory_fields = [field for field in OC_MANDATORY_FIELDS if not oc_customer.get(field)]
        # if missed_mandatory_fields:
        #     skip_count += 1
        #     extras = (1, 'skipped', 'Skipped: mandatory fileds missed: %s' % ', '.join(missed_mandatory_fields))
        #     results_list.append((oc_customer_name, customer_id, '', '', '') + extras)
        #     continue

        doc_customer_group = doc_customer_groups_cache.get(oc_customer.get('customer_group_id'))
        if not doc_customer_group:
            doc_customer_group = customer_groups.get(site_name, oc_customer.get('customer_group_id'))
            doc_customer_groups_cache[oc_customer.get('customer_group_id')] = doc_customer_group

        if not doc_customer_group:
            skip_count += 1
            extras = (1, 'skipped', 'Skipped: parent group missed')
            results_list.append((oc_customer_name, customer_id, '', '', '') + extras)
            continue

        doc_customer = get_customer(site_name, customer_id)
        if doc_customer:
            # update existed Customer
            # check here for a need to update Customer
            params = {
                'customer_name': oc_customer_name,
                'customer_group': doc_customer_group.get('name'),
                'oc_last_sync_from': datetime.now(),
                'oc_is_updating': 1,
                'oc_store_id': oc_customer.get('store_id') or '',
                'oc_status': 1,
                'oc_firstname': oc_customer.get('firstname'),
                'oc_lastname': oc_customer.get('lastname'),
                'oc_telephone': oc_customer.get('telephone'),
                'oc_fax': oc_customer.get('fax'),
                'oc_email': oc_customer.get('email'),
            }
            doc_customer.update(params)
            doc_customer.save()

            # addresses and contacts
            addresses.create_or_update(site_name, oc_customer, doc_customer)
            contacts.create_or_update(site_name, oc_customer, doc_customer)

            update_count += 1
            extras = (1, 'updated', 'Updated')
            results_list.append((doc_customer.get('name'),
                                 doc_customer.get('oc_customer_id'),
                                 doc_customer_group.get('name'),
                                 doc_customer.get_formatted('oc_last_sync_from'),
                                 doc_customer.get('modified')) + extras)

        else:
            default_price_list = doc_customer_group.get('default_price_list')
            # do not allow to aad new customer if default_price_list is missed in customer's group
            # if not default_price_list:
            #     skip_count += 1
            #     extras = (1, 'skipped', 'Skipped: missed default price list in customer group')
            #     results_list.append((oc_customer_name, customer_id, '', '', '') + extras)
            #     continue

            # create new Customer
            params = {
                'doctype': 'Customer',
                'customer_type': 'Individual',
                'territory': default_customer_territory,
                'customer_name': oc_customer_name,
                'customer_group': doc_customer_group.get('name'),
                'naming_series': 'CUST-',
                'default_price_list': default_price_list,
                '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_customer.get('firstname'),
                'oc_lastname': oc_customer.get('lastname'),
                'oc_telephone': oc_customer.get('telephone'),
                'oc_fax': oc_customer.get('fax'),
                'oc_email': oc_customer.get('email')
            }
            doc_customer = frappe.get_doc(params)
            if not doc_customer.get('customer_name').strip():
                continue
            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)

            add_count += 1
            extras = (1, 'added', 'Added')
            results_list.append((doc_customer.get('customer_name'),
                                 doc_customer.get('oc_customer_id'),
                                 doc_customer_group.get('name'),
                                 doc_customer.get_formatted('oc_last_sync_from'),
                                 doc_customer.get('modified')) + extras)
    results = {
        'check_count': check_count,
        'add_count': add_count,
        'update_count': update_count,
        'skip_count': skip_count,
        'results': results_list,
        'success': success,
    }
    return results