def bill(request): """ It generates a bill for the user which lists all the items, their quantity , subtotal and then adds it to the surcharges and generates the Grand total. Argument:Http Request Return:Render Bill """ id = request.GET['order_id'] purchase_order = PurchaseOrder.objects.filter(id=id) purchased_item = PurchasedItem.objects.filter(\ purchase_order=purchase_order).values('item__category__name',\ 'item__category','item__category__parent__name','item__category__unit',\ 'item__category__unit__unit').\ order_by('item__category').distinct() purchased_item_obj = PurchasedItem.objects.filter(\ purchase_order=purchase_order).values('item__name', 'item__category',\ 'qty', 'price_per_unit').order_by('item__category') cost = PurchasedItem.objects.filter(purchase_order=purchase_order).\ values('price', 'item__category', 'item__name',\ 'item__category__parent__name').order_by('item__category') bill_obj = Bill.objects.values('delivery_charges').get(purchase_order=id) bill_values = [] field_check = 1 for category in purchased_item: flag1 = 1 list = [] list.append(category['item__category__name']) item_names = [] for item in purchased_item_obj: if category['item__category'] == item['item__category']: if flag1 == 1: item_names.append(':') item_names.append(item['item__name']) flag1 = 0 else: item_names.append(',') item_names.append(item['item__name']) flag1 = 1 item_qty = [] for qty in purchased_item_obj: if category['item__category'] == qty['item__category']: if flag1 == 1: item_qty.append(qty['qty']) flag1 = 0 else: item_qty.append(',') item_qty.append(qty['qty']) if category['item__category__unit']: item_qty.append(category['item__category__unit__unit']) else: item_qty.append('no_unit') flag1 = 1 price_unit = [] for price_per in purchased_item_obj: if category['item__category'] == price_per['item__category']: if flag1 == 1: price_unit.append(price_per['price_per_unit']) flag1 = 0 else: price_unit.append(',') price_unit.append(price_per['price_per_unit']) total = 0 for itemcost in cost: if category['item__category'] == itemcost['item__category']: total = total + itemcost['price'] list.append(item_names) list.append(item_qty) list.append(price_unit) list.append(total) bill_values.append(list) taxes_applied = TaxesApplied.objects.\ filter(purchase_order=purchase_order).values('surcharge', 'tax', 'surcharge_name', 'surcharge_value') taxes_applied_obj = TaxesApplied.objects.\ filter(purchase_order=purchase_order).aggregate(Count('id')) bill = Bill.objects.values('total_cost', 'totalplusdelivery',\ 'grand_total', 'delivery_charges').get(purchase_order=id) total_cost = bill['total_cost'] totalplusdelivery = bill['totalplusdelivery'] grand_total = bill['grand_total'] delivery_charges = bill['delivery_charges'] purchase_order_obj = PurchaseOrder.objects.values('buyer',\ 'buyer__first_name', 'buyer__last_name', 'reference','reference_date',\ 'delivery_address', 'organisation', 'date_time', 'total_discount',\ 'buyer__customer__title').get(id = id) total_discount = purchase_order_obj['total_discount'] taxes_applied_obj = TaxesApplied.objects.\ filter(purchase_order=purchase_order).aggregate(Count('id')) suspense_order = SuspenseOrder.objects.values('distance_estimated').\ filter(purchase_order=id) total_distance = 0 if suspense_order: for distance in suspense_order: total_distance = total_distance + distance['distance_estimated'] if total_distance == 0: if total_discount == 0: tax_count = taxes_applied_obj['id__count'] + 2 else: tax_count = taxes_applied_obj['id__count'] + 3 else: if total_discount == 0: tax_count = taxes_applied_obj['id__count'] + 4 else: tax_count = taxes_applied_obj['id__count'] + 5 else: if total_discount == 0: tax_count = taxes_applied_obj['id__count'] + 2 else: tax_count = taxes_applied_obj['id__count'] + 3 if taxes_applied_obj['id__count'] == 0: tax_count = tax_count + 1 buyer = purchase_order_obj['buyer'] address = Customer.objects.values('address__street_address',\ 'address__district', 'address__pin', 'address__province').get(user=buyer) organisation_id = purchase_order_obj['organisation'] date = purchase_order_obj['date_time'] customer_obj = Customer.objects.values('company').get(user=buyer) admin_organisations = AdminOrganisations.objects.values('pan_no',\ 'stc_no').get(id=organisation_id) voucherid = VoucherId.objects.values('purchase_order_of_session').\ filter(purchase_order=id)[0] total_in_words = num2eng(grand_total) ref_letter = _YOUR_LETTER_No header = HeaderFooter.objects.values('header').get(is_active=True) footer = HeaderFooter.objects.values('footer').get(is_active=True) return render(request, 'prints/bill.html', {\ 'stc_no' : admin_organisations, 'pan_no' : admin_organisations,\ 'id':voucherid['purchase_order_of_session'], 'ref':purchase_order_obj,\ 'date':date, 'purchase_order':purchase_order, 'address':address,\ 'total_cost':total_cost, 'grand_cost':grand_total,\ 'taxes_applied':taxes_applied,\ 'buyer':purchase_order_obj, 'buyer_name':customer_obj,\ 'site':purchase_order_obj, 'delivery_charges':delivery_charges,\ 'total_discount':total_discount, 'tax_count':tax_count,\ 'bill_values':bill_values, 'header':header, 'footer': footer,\ 'totalplusdelivery':totalplusdelivery,\ 'total_in_words':total_in_words, 'ref_letter':ref_letter})
def quoted_bill(request): """ It generates a proforma bill for the user which lists all the items, their quantity , subtotal and then adds it to the surcharges and generates the Grand total. Argument:Http Request Return:Render Proforma Bill """ quoted_order_id = request.GET['quoted_order_id'] quoted_order = QuotedOrder.objects.filter(id=quoted_order_id) quoted_item = QuotedItem.objects.filter(quoted_order=quoted_order_id).\ values('item__category__name', 'item__category', 'item__category__unit', 'item__category__unit__unit').\ order_by('item__category').distinct() quoted_item_obj = QuotedItem.objects.filter(quoted_order=quoted_order_id).\ values('item__name',\ 'item__category', 'qty', 'price_per_unit').order_by('item__category') cost = QuotedItem.objects.filter(quoted_order=quoted_order_id).\ values('price', 'item__category', 'item__name').order_by('item__category') quoted_order_sessionid = QuotedOrderofSession.objects.filter\ (quoted_order_id=quoted_order_id).values('quoted_order_session')[0] bill_values = [] for category in quoted_item: flag1 = 1 list = [] list.append(category['item__category__name']) item_names = [] for item in quoted_item_obj: if category['item__category'] == item['item__category']: if flag1 == 1: item_names.append(':') item_names.append(item['item__name']) flag1 = 0 else: item_names.append(',') item_names.append(item['item__name']) flag1 = 1 item_qty = [] for qty in quoted_item_obj: if category['item__category'] == qty['item__category']: if flag1 == 1: item_qty.append(qty['qty']) flag1 = 0 else: item_qty.append(',') item_qty.append(qty['qty']) if category['item__category__unit']: item_qty.append(category['item__category__unit__unit']) else: item_qty.append('no_unit') flag1 = 1 price_unit = [] for price_per in quoted_item_obj: if category['item__category'] == price_per['item__category']: if flag1 == 1: price_unit.append(price_per['price_per_unit']) flag1 = 0 else: price_unit.append(',') price_unit.append(price_per['price_per_unit']) total = 0 for itemcost in cost: if category['item__category'] == itemcost['item__category']: total = total + itemcost['price'] list.append(item_names) list.append(item_qty) list.append(price_unit) list.append(total) bill_values.append(list) taxes_applied = QuotedTaxesApplied.objects.\ filter(quoted_order=quoted_order).values('surcharge', 'tax', 'surcharge_name', 'surcharge_value') taxes_applied_obj = QuotedTaxesApplied.objects.\ filter(quoted_order=quoted_order).aggregate(Count('id')) bill = QuotedBill.objects.values('total_cost', 'grand_total',\ 'delivery_charges', 'totalplusdelivery').get(quoted_order=quoted_order_id) total_cost = bill['total_cost'] grand_total = bill['grand_total'] delivery_charges = bill['delivery_charges'] totalplusdelivery = bill['totalplusdelivery'] quoted_order_obj = QuotedOrder.objects.values('buyer','buyer__first_name',\ 'buyer__last_name','reference','delivery_address','organisation',\ 'date_time','total_discount','buyer__customer__title',\ 'reference_date').get(id = quoted_order_id) total_discount = quoted_order_obj['total_discount'] taxes_applied_obj = QuotedTaxesApplied.objects.\ filter(quoted_order=quoted_order).aggregate(Count('id')) try: suspense_order = QuotedSuspenseOrder.objects.\ values('distance_estimated').get(quoted_order=quoted_order_id) if suspense_order['distance_estimated'] == 0: if total_discount == 0: tax_count = taxes_applied_obj['id__count'] + 2 else: tax_count = taxes_applied_obj['id__count'] + 3 else: if total_discount == 0: tax_count = taxes_applied_obj['id__count'] + 4 else: tax_count = taxes_applied_obj['id__count'] + 5 except: if total_discount == 0: tax_count = taxes_applied_obj['id__count'] + 2 else: tax_count = taxes_applied_obj['id__count'] + 3 if taxes_applied_obj['id__count'] == 0: tax_count = tax_count + 1 buyer = quoted_order_obj['buyer'] address = Customer.objects.values('address__street_address',\ 'address__district', 'address__pin', 'address__province').get(user=buyer) organisation_id = quoted_order_obj['organisation'] date = quoted_order_obj['date_time'] customer_obj = Customer.objects.values('company').get(user=buyer) admin_organisations = AdminOrganisations.objects.values('pan_no',\ 'stc_no').get(id = organisation_id) header = HeaderFooter.objects.values('header').get(is_active=True) footer = HeaderFooter.objects.values('footer').get(is_active=True) permanent_note = NoteLine.objects.values('note').filter(is_permanent=True) quoted_note = QuotedOrderNote.objects.values('note').\ filter(quoted_order=quoted_order_id) account_holder = _ACCOUNT_HOLDER name_of_bank = _NAME_OF_BANK branch = _BRANCH online_account = _ONLINE_ACCOUNT ifsc_code = _IFSC_CODE ref_letter = _YOUR_LETTER_No total_in_words = num2eng(grand_total) return render(request, 'bills/quote_bill.html', { 'stc_no':admin_organisations,'pan_no':admin_organisations,\ 'ref':quoted_order_obj, 'date':date,\ 'quoted_order':quoted_order, 'address':address,\ 'total_cost': total_cost, 'grand_cost':grand_total,\ 'taxes_applied': taxes_applied,\ 'buyer':quoted_order_obj, 'buyer_name':customer_obj,\ 'site': quoted_order_obj, 'delivery_charges':delivery_charges,\ 'total_discount':total_discount, 'tax_count':tax_count,\ 'bill_values':bill_values, 'total_in_words':total_in_words,\ 'quoted_order_id':quoted_order_sessionid['quoted_order_session'],\ 'header':header,'footer':footer, 'permanent_note':permanent_note,\ 'quoted_note':quoted_note, 'account_holder':account_holder,\ 'name_of_bank':name_of_bank, 'branch':branch,\ 'online_account':online_account, 'ifsc_code':ifsc_code,\ 'totalplusdelivery':totalplusdelivery, 'ref_letter':ref_letter})
def suspense_bill(request): """ It generates a Suspense Bill for the user which lists all the items, their quantity , subtotal and then adds it to the surcharges and generates the Grand total. Argument:Http Request Return:Render Suspense Bill """ id = request.GET['order_id'] voucherid = VoucherId.objects.filter(purchase_order=id).\ values('voucher_no', 'purchased_item__item__category', 'session').distinct() suspenseorder = SuspenseOrder.objects.values('voucher', 'session_id',\ 'distance_estimated').filter(purchase_order=id) rate = Surcharge.objects.values('value').\ filter(tax_name = 'Transportation')[0] for distance_temp in suspenseorder: distance = distance_temp['distance_estimated'] * rate['value'] distance_temp['distance_estimated'] = distance purchased_item = PurchasedItem.objects.filter(\ purchase_order=id).values('item__category__name',\ 'item__category','item__category__parent__name').\ order_by('item__category').distinct() purchased_item_obj = PurchasedItem.objects.filter(\ purchase_order=id).values('item__name', 'item__category',\ 'qty', 'price_per_unit').order_by('item__category') cost = PurchasedItem.objects.filter(purchase_order=id).\ values('price', 'item__category', 'item__name',\ 'item__category__parent__name').order_by('item__category') bill = Bill.objects.values('totalplusdelivery', 'grand_total').get(purchase_order=id) bill_values = [] field_check = 1 for category in purchased_item: flag1 = 1 list = [] list.append(category['item__category__name']) item_names = [] for item in purchased_item_obj: if category['item__category'] == item['item__category']: if flag1 == 1: item_names.append(':') item_names.append(item['item__name']) flag1 = 0 else: item_names.append(',') item_names.append(item['item__name']) flag1 = 1 total = 0 for itemcost in cost: if category['item__category'] == itemcost['item__category']: total = total + itemcost['price'] for voucher_obj in voucherid: if category['item__category'] == voucher_obj[ 'purchased_item__item__category']: try: suspense_obj = SuspenseOrder.objects.values(\ 'distance_estimated').get(\ voucher=voucher_obj['voucher_no'],\ session_id=voucher_obj['session']) total = total + suspense_obj['distance_estimated'] * rate[ 'value'] except: pass list.append(item_names) list.append(int(total)) bill_values.append(list) taxes_applied = TaxesApplied.objects.\ filter(purchase_order=id).values('surcharge', 'tax') taxes_applied_obj = TaxesApplied.objects.\ filter(purchase_order=id).aggregate(Count('id')) surcharge = Surcharge.objects.values('id', 'tax_name', 'value') totalplusdelivery = bill['totalplusdelivery'] grand_total = bill['grand_total'] purchase_order_obj = PurchaseOrder.objects.values('buyer',\ 'buyer__first_name', 'buyer__last_name', 'reference','reference_date',\ 'delivery_address', 'organisation', 'date_time', 'total_discount',\ 'buyer__customer__title').get(id = id) taxes_applied_obj = TaxesApplied.objects.\ filter(purchase_order=id).aggregate(Count('id')) buyer = purchase_order_obj['buyer'] address = Customer.objects.values('address__street_address',\ 'address__district', 'address__pin', 'address__province').get(user=buyer) organisation_id = purchase_order_obj['organisation'] date = purchase_order_obj['date_time'] customer_obj = Customer.objects.values('company').get(user=buyer) admin_organisations = AdminOrganisations.objects.values('pan_no',\ 'stc_no').get(id=organisation_id) voucherid = VoucherId.objects.values('purchase_order_of_session').\ filter(purchase_order=id)[0] total_in_words = num2eng(grand_total) ref_letter = _YOUR_LETTER_No header = HeaderFooter.objects.values('header').get(is_active=True) footer = HeaderFooter.objects.values('footer').get(is_active=True) return render(request, 'prints/suspense_bill.html', {\ 'stc_no' : admin_organisations, 'pan_no' : admin_organisations,\ 'id':voucherid['purchase_order_of_session'], 'ref':purchase_order_obj,\ 'date':date, 'address':address,\ 'grand_cost':grand_total,\ 'taxes_applied':taxes_applied, 'surcharge':surcharge,\ 'buyer':purchase_order_obj, 'buyer_name':customer_obj,\ 'site':purchase_order_obj,\ 'bill_values':bill_values, 'header':header, 'footer': footer,\ 'totalplusdelivery':totalplusdelivery,\ 'total_in_words':total_in_words, 'ref_letter':ref_letter})
def receipt(request): """ It generates a Receipt. Argument:Http Request Return:Render Receipt """ id = request.GET['order_id'] voucherid_temp = VoucherId.objects.values('receipt_no_of_session').filter( purchase_order=id)[0] if not voucherid_temp['receipt_no_of_session']: today_date = datetime.date.today() financialsession = FinancialSession.objects.\ values('id','session_start_date','session_end_date') for value in financialsession: start_date = value['session_start_date'] end_date = value['session_end_date'] if start_date <= today_date <= end_date: session_id = value['id'] max_receipt_no = VoucherId.objects.filter( session=session_id).aggregate(Max('receipt_no_of_session')) if max_receipt_no['receipt_no_of_session__max']: voucherid_obj = VoucherId.objects.values( 'receipt_no_of_session', 'session', 'purchase_order__date_time').filter( receipt_no_of_session=max_receipt_no[ 'receipt_no_of_session__max'], session_id=session_id)[0] voucherid_obj2 = VoucherId.objects.values( 'receipt_no_of_session', 'session', 'purchase_order__date_time').filter(purchase_order=id)[0] if voucherid_obj['session'] == voucherid_obj2['session']: VoucherId.objects.filter(purchase_order=id).update( receipt_no_of_session=max_receipt_no[ 'receipt_no_of_session__max'] + 1, receipt_date=today_date) else: VoucherId.objects.filter(purchase_order=id).update( receipt_no_of_session=1, receipt_date=today_date) else: VoucherId.objects.filter(purchase_order=id).update( receipt_no_of_session=1, receipt_date=today_date) voucherid = VoucherId.objects.values('purchase_order_of_session', 'receipt_no_of_session').\ filter(purchase_order=id)[0] bill = Bill.objects.values('amount_received').get(purchase_order=id) purchase_order = PurchaseOrder.objects.values('buyer', 'date_time',\ 'delivery_address', 'mode_of_payment__method','mode_of_payment',\ 'cheque_dd_number', 'cheque_dd_date','buyer__customer__company').get(id=id) date = purchase_order['date_time'] total_in_words = num2eng(bill['amount_received']) customer_obj = PurchaseOrder.objects.values('buyer',\ 'buyer__first_name', 'buyer__last_name','buyer__customer__title').\ get(id = id) address = Customer.objects.values('address__street_address',\ 'address__district', 'address__pin', 'address__province').\ get(user = purchase_order['buyer']) purchased_item = PurchasedItem.objects.values('item__category__name').\ filter(purchase_order=id).distinct() header = HeaderFooter.objects.values('header').get(is_active=True) return render(request, 'prints/receipt.html', {\ 'receiptno': voucherid['receipt_no_of_session'], 'order_no':voucherid['purchase_order_of_session'], 'date': date, 'cost':bill, 'amount':total_in_words, 'address':address,\ 'method': purchase_order, 'buyer':customer_obj,\ 'material':purchased_item, 'header':header})
def voucher_print(request): """ This function displays a particular voucher Argument:Http Request Return:Render Voucher """ number = request.GET['voucher_no'] session = request.GET['session'] purchase_order_id = request.GET['purchase_order'] flag = 0 suspense_order = SuspenseOrder.objects.filter( voucher=number, purchase_order=purchase_order_id) if suspense_order: flag = 1 calculatedistribution = CalculateDistribution.objects.\ values('college_income_calculated', 'admin_charges_calculated',\ 'consultancy_asset', 'development_fund', 'total').\ get(voucher_no = number, session = session) total_in_words = num2eng(calculatedistribution['total']) voucherid = VoucherId.objects.values('purchase_order', 'ratio',\ 'college_income', 'admin_charges', 'distribution__name',\ 'purchased_item__item__category__name',\ 'purchased_item__item__category__parent__parent', 'purchased_item__item__category__parent').\ filter(voucher_no = number, session = session)[0] purchase_order = voucherid['purchase_order'] distribution = voucherid['distribution__name'] ratio = voucherid['ratio'] college_income = voucherid['college_income'] admin_charges = voucherid['admin_charges'] category_name = voucherid['purchased_item__item__category__name'] lab_id = voucherid['purchased_item__item__category__parent__parent'] lab_id_level_down = voucherid['purchased_item__item__category__parent'] emp = Staff.objects.values('name','position__position').filter(lab=lab_id).\ filter(always_included=1).order_by('position__rank','-seniority_credits') if emp: pass else: emp = Staff.objects.values('name', 'position__position').filter( lab=lab_id_level_down).filter(always_included=1).order_by( 'position__rank', '-seniority_credits') purchase_order_obj = PurchaseOrder.objects.\ values('date_time', 'buyer','buyer__first_name','buyer__last_name',\ 'tds','buyer__customer__title','buyer__customer__company').\ get(id = purchase_order) address = Customer.objects.values('address__street_address',\ 'address__district', 'address__pin', 'address__province').\ get(user = purchase_order_obj['buyer']) date = purchase_order_obj['date_time'] bill = Bill.objects.values('delivery_charges','total_cost',\ 'grand_total','amount_received','totalplusdelivery').\ get(purchase_order = purchase_order_id) amount_received_inwords = num2eng(bill['amount_received']) taxes_applied = TaxesApplied.objects.values('surcharge__tax_name',\ 'surcharge__value','tax').filter(purchase_order = purchase_order_id) voucheridobj = VoucherId.objects.values('purchase_order_of_session', 'purchase_order__mode_of_payment__method', 'purchase_order__cheque_dd_number','purchase_order__cheque_dd_date', 'purchase_order__mode_of_payment').\ filter(purchase_order=purchase_order_id)[0] header = HeaderFooter.objects.values('header').get(is_active=True) footer = HeaderFooter.objects.values('footer').get(is_active=True) if flag == 0: return render(request, 'voucher/voucher_report.html', {\ 'calculate_distribution' : calculatedistribution,\ 'admin_charges': admin_charges, 'college_income': college_income, \ 'ratio':ratio,'d_name': distribution,\ 'purchase_order': voucheridobj['purchase_order_of_session'],\ 'voucher':number, 'date': date,'address': address,\ 'buyer': purchase_order_obj, 'material': category_name,\ 'total_in_words': total_in_words, 'employee' : emp,\ 'header': header}) else: return render(request, 'voucher/voucher_report_suspence.html',{ 'address':address, 'cost':bill, 'inwords':amount_received_inwords,\ 'date':date, 'suspense_voucher':number,\ 'job':voucheridobj['purchase_order_of_session'],\ 'tds':purchase_order_obj, 'tax':taxes_applied, 'header': header,\ 'material':category_name, 'buyer': purchase_order_obj, 'method':voucheridobj['purchase_order__mode_of_payment__method'], 'method_number':voucheridobj['purchase_order__cheque_dd_number'], 'method_date':voucheridobj['purchase_order__cheque_dd_date'], 'method_id':voucheridobj['purchase_order__mode_of_payment']})