예제 #1
0
def delete_address(customer_id, address_id):
    address = SR().find_one(
        Address({
            'id': address_id,
            'customer_id': customer_id
        }))
    SR().delete(address)
예제 #2
0
def get_one_address(customer_id, address_id):
    addr = SR().find_one(
        Address({
            'id': address_id,
            'customer_id': customer_id
        }))
    return addr
예제 #3
0
def restore_prices(customer_code="TESTDEBTOR"):
    connection = authUtil.build_connection()
    status, prices = connection.retrieve_organisation_data(37, customer_code)
    if not status:
        return {
            'status': 'Failed',
            'message': 'Retrieve data from squizz failed.'
        }

    sr = SR()
    try:
        # Truncate prices
        sr.truncate(Price, False)

        # Traverse products and convert to key, id pairs
        prod_key_id = {}
        for product in sr.list_all(Product, ['keyProductId', 'id']):
            prod_key_id[product.keyProductID] = product.id

        # Rewrite prices
        for price in prices:
            if price.keyProductID not in prod_key_id:
                continue
            price.productId = prod_key_id[price.keyProductID]
            sr.insert(price, False)

    except Exception as e:
        sr.connection.rollback()
        sr.cursor.close()
        raise e
    else:
        sr.connection.commit()
        sr.cursor.close()

    return {'status': 'Success', 'message': 'Price data Updated.'}
예제 #4
0
    def create_order(self,
                     org: Organization,
                     customer: Customer,
                     delivery: Address,
                     billing: Address,
                     order_details_list: [OrderDetail],
                     status,
                     instructions=""):
        """Create an order for the provided customer"""
        new_order = Order()
        new_order.organizationId = org.id
        new_order.createdDate = datetime.datetime.now().strftime(
            "%Y-%m-%d  %H:%M:%S"),
        new_order.instructions = instructions
        new_order.deliveryOrgName = delivery.organization
        new_order.deliveryContact = delivery.contact
        new_order.deliveryEmail = delivery.email
        new_order.deliveryAddress1 = delivery.address_line1
        new_order.deliveryAddress2 = delivery.address_line2
        new_order.deliveryAddress3 = delivery.address_line3
        new_order.deliveryRegionName = delivery.region
        new_order.deliveryCountryName = delivery.country
        new_order.deliveryPostcode = delivery.postcode
        new_order.billingContact = billing.contact
        new_order.billingOrgName = billing.organization
        new_order.billingEmail = billing.email
        new_order.billingAddress1 = billing.address_line1
        new_order.billingAddress2 = billing.address_line2
        new_order.billingAddress3 = billing.address_line3
        new_order.billingRegionName = billing.region
        new_order.billingCountryName = billing.country
        new_order.billingPostcode = billing.postcode
        new_order.billStatus = status
        new_order.customer_id = customer.id

        # Create Order and Related Order lines
        sr = SR()
        try:
            sr.insert(new_order, False)
            for line in order_details_list:
                line.orderId = new_order.id
                sr.insert(line, False)

        except Exception as e:
            sr.connection.rollback()
            sr.cursor.close()
            raise e
        else:
            sr.connection.commit()
            sr.cursor.close()
            new_order.lines = order_details_list
            return new_order
예제 #5
0
def create_customer_with_address(customer, address):
    sr = SR()
    try:
        created_customer = sr.insert(customer, commit=False)
        address.customer_id = created_customer.id
        sr.insert(address, commit=False)
    except Exception as e:
        sr.connection.rollback()
        sr.cursor.close()
        raise e
    else:
        sr.connection.commit()
        sr.cursor.close()
예제 #6
0
def restore_category():
    connection = authUtil.build_connection()
    status, categories = connection.retrieve_organisation_data(8)

    if not status:
        return {
            'status': 'Failed',
            'message': 'Retrieve data from squizz failed.'
        }

    sr = SR()
    try:
        # Rewrite categories
        sr.truncate(CateProd, False)
        sr.truncate(Category, False)
        sr.batch_insert(categories, commit=False)

        # Traverse products and convert to key, id pairs
        prod_key_id = {}
        for product in sr.list_all(Product):
            prod_key_id[product.keyProductID] = product.id

        # Rewrite category product relationships
        for category in categories:
            if category.keyCategoryParentID is None:
                continue
            if category.keyProductIDs is None:
                continue
            print(category.keyProductIDs)
            for productKey in category.keyProductIDs:
                if productKey not in prod_key_id:
                    continue
                cate_prod_rel = CateProd({
                    'categoryId': category.id,
                    'productId': prod_key_id[productKey]
                })
                sr.insert(cate_prod_rel, commit=False)

    except Exception as e:
        sr.connection.rollback()
        sr.cursor.close()
        raise e
    else:
        sr.connection.commit()
        sr.cursor.close()

    return {'status': 'Success', 'message': 'Category data Updated'}
예제 #7
0
def list_all_categories():
    # Parent categories
    p_cate_list = []
    # Children categories
    c_cate_dict = {}

    # Retrieve all categories
    for category in SR().list_all(Category):
        if category.keyCategoryParentID is None:
            p_cate_list.append(category)
        else:
            if category.keyCategoryParentID not in c_cate_dict:
                c_cate_dict[category.keyCategoryParentID] = [category]
            else:
                c_cate_dict[category.keyCategoryParentID].append(category)

    return p_cate_list, c_cate_dict
예제 #8
0
def update_address(address):
    SR().update(address)
예제 #9
0
def create_customer_address(customer_id, address):
    address.customer_id = customer_id
    SR().insert(address)
예제 #10
0
def list_customer_addresses(customer_id):
    SR().get_one_by_id(Customer(pk=customer_id))
    cust_addr = Address()
    cust_addr.customer_id = customer_id
    return SR().find_all(cust_addr)
예제 #11
0
def delete_customer(customer_id):
    SR().delete(Customer(pk=customer_id))
예제 #12
0
def update_customer(customer):
    SR().update(customer)
예제 #13
0
def create_customer(customer, address=None):
    if address is None:
        SR().insert(customer)
    else:
        create_customer_with_address(customer, address)
예제 #14
0
def get_one_customer(customer_id):
    customer = SR().get_one_by_id(Customer(pk=customer_id))
    return customer
예제 #15
0
def list_all_customers():
    customers = SR().list_all(Customer)
    return customers
예제 #16
0
def list_used_customer_codes():
    customers = SR().list_all(Customer)
    used_codes = set([c.customer_code for c in customers])
    return used_codes