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
def payment_to_dict(user, company, payment): return { "type": payment.type, "amount_paid": format_number(user, company, payment.amount_paid), "currency": payment.currency, "total": format_number(user, company, payment.total), "total_btc": format_number(user, company, payment.total_btc), "transaction_datetime": format_date(user, company, payment.transaction_datetime), "btc_transaction_reference": payment.btc_transaction_reference, "paypal_transaction_reference": payment.paypal_transaction_reference, "payment_info": payment.payment_info, "status": payment.get_status_display(), }
def tax_to_dict(user, company, tax): return { 'id': tax.id, 'name': tax.name, 'amount': format_number(user, company, tax.amount), 'default': tax.default, }
def bill_earnings(start_date=None, end_date=None): billset = Bill.objects.filter(company=c, payment__status=PAID) if start_date: billset = billset.filter(timestamp__gte=start_date) if end_date: billset = billset.filter(timestamp__lt=end_date) if billset.count() == 0: return { 'income': format_number(request.user, c, Decimal(0)), 'profit': format_number(request.user, c, Decimal(0)), } income = billset.aggregate(Sum('payment__total'))['payment__total__sum'] return { 'income': format_number(request.user, c, income), 'profit': format_number(request.user, c, income - billset.aggregate(Sum('base'))['base__sum'] - billset.aggregate(Sum('discount'))['discount__sum']) }
def get_default_tax(user, company): """ returns the default tax id and formatted value in dictionary: {id:<id>, amount:<amount>} if there's no default tax, return the first one if there's no taxes at all, return zeros if there are many default taxes, return the one with the lowest id """ try: # the 'normal' scenario: there's one default tax tax = Tax.objects.get(company=company, default=True) except Tax.MultipleObjectsReturned: # more than one default tax, use the first by id tax = Tax.objects.filter(company=company, default=True).order_by('id')[0] except Tax.DoesNotExist: # no default tax specified, use the first by id try: tax = Tax.objects.filter(company=company).order_by('id')[0] except: # even that doesn't work return {'id': 0,'amount': 0} return {'id': tax.id, 'amount': format_number(user, company, tax.amount)}
def discount_to_dict(user, company, d, android=False): ret = { 'id': d.id, 'description': d.description, 'code': d.code, 'type': d.type, 'amount': format_number(user, company, d.amount, True), 'enabled': d.enabled, 'active': d.is_active, } if android and d.start_date: ret['start_date'] = [d.start_date.year, d.start_date.month, d.start_date.day] else: ret['start_date'] = format_date(user, company, d.start_date) if android and d.end_date: ret['end_date'] = [d.end_date.year, d.end_date.month, d.end_date.day] else: ret['end_date'] = format_date(user, company, d.end_date) return ret
def bill_item_to_dict(user, company, item): i = {} i["item_id"] = item.id # values from product i["product_id"] = item.product_id i["code"] = item.code i["shortcut"] = item.shortcut i["name"] = item.name i["description"] = item.description i["private_notes"] = item.private_notes i["unit_type"] = item.unit_type # i['stock'] = format_number(user, company, item.stock) # values from bill Item i["bill_id"] = item.bill.id i["bill_notes"] = item.bill_notes i["base"] = format_number(user, company, item.base) i["quantity"] = drop_trailing_zeros(format_number(user, company, item.quantity, high_precision=True)) i["tax_rate"] = format_number(user, company, item.tax_rate) i["batch"] = format_number(user, company, item.batch) i["discount"] = format_number(user, company, item.discount) i["net"] = format_number(user, company, item.net) i["tax"] = format_number(user, company, item.tax) i["total"] = format_number(user, company, item.total) # if group_tax_rates() was called on a list with this item in it, # there should be tax_rate_id property on the item. if it's not, f**k it, # the calling function won't use it anyway try: i["tax_rate_id"] = item.tax_rate_id except (AttributeError, FieldDoesNotExist): pass return i
def bill_to_dict(user, company, bill): # fields in bill: # company # type # recipient_contact > FK contact # note # sub_total | # discount | decimal fields, with everything calculated # tax | # timestamp # status > choices in g.BILL_STATUS b = { "id": bill.id, "issuer": company_to_dict(user, bill.issuer), "register": register_to_dict(user, company, bill.register), "user_id": bill.user_id, "user_name": bill.user_name, "serial": bill.serial, "serial_prefix": bill.serial_prefix, "serial_number": bill.serial_number, "notes": bill.notes, "discount_amount": format_number(user, company, bill.discount_amount), "discount_type": bill.discount_type, "currency": bill.payment.currency, # prices "base": format_number(user, company, bill.base), "discount": format_number(user, company, bill.discount), "tax": format_number(user, company, bill.tax), "total": format_number(user, company, bill.total), "timestamp": format_date(user, company, bill.timestamp) + " " + format_time(user, company, bill.timestamp), } if bill.contact: b["contact"] = contact_to_dict(user, company, bill.contact) # get items items = list(BillItem.objects.filter(bill=bill)) # gather tax rates for all items grouped_taxes = group_tax_rates(items) tax_rates = grouped_taxes["rates"] tax_sums = grouped_taxes["sums"] for rate in tax_rates: rate["amount"] = format_number(user, company, rate["amount"]) rate["tax_sum"] = format_number(user, company, rate["tax_sum"]) rate["net_sum"] = format_number(user, company, rate["net_sum"]) rate["gross_sum"] = format_number(user, company, rate["gross_sum"]) tax_sums["tax_sum"] = format_number(user, company, tax_sums["tax_sum"]) tax_sums["net_sum"] = format_number(user, company, tax_sums["net_sum"]) tax_sums["gross_sum"] = format_number(user, company, tax_sums["gross_sum"]) b["tax_rates"] = tax_rates b["tax_sums"] = tax_sums # format all items i = [] for item in items: i.append(bill_item_to_dict(user, company, item)) b["items"] = i b["payment"] = payment_to_dict(user, company, bill.payment) return b
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