Example #1
0
def product_to_dict(user, company, product, android=False):
    # returns all relevant product's data
    ret = {}

    ret['id'] = product.id

    # purchase price
    purchase_price = product.get_purchase_price()
    if not purchase_price:
        ret['purchase_price'] = ''
    else:
        ret['purchase_price'] = format_number(user, company, purchase_price, True)

    # sale price
    price = product.get_price()
    if not price:
        ret['price'] = ''
    else:
        ret['price'] = format_number(user, company, price, True)

    # all discounts in a list
    discounts = []
    all_discounts = product.get_discounts()
    for d in all_discounts:
        # discounts.append(d.id)
        discounts.append(discount_to_dict(user, company, d, android))
    ret['discounts'] = discounts
    if product.image:  # check if product's image exists:
        if android:
            with open(product.image.path, "rb") as image_file:
                encoded_string = base64.b64encode(image_file.read())
            ret['image'] = encoded_string
        else:
            ret['image'] = get_thumbnail(product.image, image_dimensions('product')[2]).url

    # tax: it's a not-null foreign key
    ret['tax_id'] = product.tax.id
    ret['tax'] = format_number(user, company, product.tax.amount)
    # stock: it cannot be 'undefined'
    ret['stock'] = format_number(user, company, product.stock)

    # category?
    if product.category:
        ret['category'] = product.category.name
        ret['category_id'] = product.category.id
    else:
        ret['category'] = None
        ret['category_id'] = None

    ret['code'] = product.code
    ret['shortcut'] = product.shortcut
    ret['name'] = product.name
    ret['description'] = product.description
    ret['private_notes'] = product.private_notes
    ret['unit_type'] = product.unit_type
    ret['unit_type_display'] = product.get_unit_type_display()
    ret['stock'] = format_number(user, company, product.stock)
    ret['color'] = product.color
    ret['favorite'] = product.favorite
    return ret
Example #2
0
def product_to_dict(user, company, product, android=False):
    # returns all relevant product's data
    ret = {}

    ret['id'] = product.id

    # purchase price
    purchase_price = product.get_purchase_price()
    if not purchase_price:
        ret['purchase_price'] = ''
    else:
        ret['purchase_price'] = format_number(user, company, purchase_price, True)

    # sale price
    price = product.get_price()

    if price is None:
        ret['price'] = ''
    else:
        ret['price'] = format_number(user, company, price, True)

    # all discounts in a list
    discounts = []
    all_discounts = product.get_discounts()
    for d in all_discounts:
        # discounts.append(d.id)
        discounts.append(discount_to_dict(user, company, d, android))
    ret['discounts'] = discounts
    if product.image:  # check if product's image exists:
        if android:
            # uf, if images are big, then what, at least we should thumnbnail-it
            with open(product.image.path, "rb") as image_file:
                encoded_string = base64.b64encode(image_file.read())
            ret['image'] = encoded_string
        else:
            ret['image'] = get_thumbnail(product.image, image_dimensions('product')[2]).url

    # tax: it's a not-null foreign key
    ret['tax_id'] = product.tax.id
    ret['tax'] = format_number(user, company, product.tax.amount)

    # category?
    if product.category:
        ret['category'] = product.category.name
        ret['category_id'] = product.category.id
    else:
        ret['category'] = g.NO_CATEGORY_NAME
        ret['category_id'] = -1

    ret['code'] = product.code
    ret['shortcut'] = product.shortcut
    ret['name'] = product.name
    ret['description'] = product.description
    ret['private_notes'] = product.private_notes
    ret['unit_type'] = product.unit_type
    ret['unit_type_display'] = product.get_unit_type_display()
    # stock: it cannot be 'undefined'
    # ret['stock'] = format_number(user, company, product.stock)
    ret['color'] = product.color
    ret['favorite'] = product.favorite

    if hasattr(product, 'stock_products'):
        stock_products = []

        for sp in product.stock_products:
            stock_product = {}
            stock_product['stock_name'] = sp.stock.name
            stock_product['deduction'] = sp.deduction
            stock_product['left_stock'] = sp.stock.left_stock
            stock_product['stock_unit_type'] = sp.stock.unit_type
            stock_products.append(stock_product)

        ret['stock_products'] = stock_products

    return ret