Reputation: 19
I am building an inventory system, I have a page where sell history can be viewed by category and it will display all items sold, quantity sold, total quantity in stock, quantity left, sales amount, profit and so on.... Anytime I search by category (e.g stationaries), the display is shown below.
Item Name stock qty sold qty total stock total amount profit
Books 990 45 75 60 1035 900 990 14567 15666 12666 1777 1555 17777
Sciences 825 45 75 60 1035 900 990 14567 15666 12666 1777 1555 17777
Comics 930 45 75 60 1035 900 990 14567 15666 12666 1777 1555 17777
But I actually want is:
Item Name stock qty sold qty total stock total amount profit
Books 990 45 1035 14567 1777
Sciences 825 75 1035 15666 1555
Comics 930 60 990 12666 17777
The Item names and stock qty are been called from the database and appended to a list. for the quantity of item sold, i called every instance of such item from the database and calcuate their total and then append to a list. So I want to dispaly them for their respective item, same goes for amount and profir. All I just want is how to manipulate the template so as to display it appropriately. My view code is given below:
def search_category (request):
collate=[]
gains=[]
amount=[]
name=[]
store=[]
qty=[]
if 'q' in request.GET and request.GET['q']:
q = request.GET.get('q','')
items = Item.objects.filter(category__name__exact=q)
for item in items:
collate.append(item.subcategory.subcategoryname)
qty.append(int(item.quantity))
len_collate=len(collate)
count=0
#for pick in collate:
keep1=[]
amt_save=[]
gain_save=[]
t=[]
all_amt_save=[]
#sum=0
#t_amount=0
#t_gain=0
while count < len_collate:
soldqty=[]
#histories = RecordSales.objects.filter(ItemName__subcategoryname__exact=pick)#here
histories = RecordSales.objects.filter(ItemName__subcategoryname__exact=collate[count])
lenght=len(histories)
for history in histories:
soldqty.append(history.quantity)
#keep1=[] #array to store sum
len_qty=len(soldqty)
#count=0
#while count < lenght:
sum=0
sums=[]
t_stock=[]
#for num in soldqty:
count_qty=0
#for num in soldqty:
while count_qty < len_qty:
sum=sum + soldqty[count_qty]
#t_stock=qty[count_qty] + sum
count_qty=count_qty + 1
amount.append(history.total)
t_amount=0
amt_count=0
while amt_count < len(amount):
t_amount=t_amount + amount[amt_count]
amt_count=amt_count + 1
gains.append(history.profit)
t_gain=0
gain_count=0
while gain_count < len(gains):
t_gain=t_gain + gains[gain_count]
gain_count=gain_count + 1
name.append(history.ItemName)
#keep1.append(sum)
len_keep1=len(keep1)
amt_save.append(t_amount)
gain_save.append(t_gain)
count=count + 1
keep1.append(sum)
all_amt_count=0
all_amt_total=0
while all_amt_count < len(amt_save):
all_amt_total= all_amt_total + amt_save[all_amt_count]
#all_amt_save.append(all_amt_total)
all_amt_count=all_amt_count + 1
all_g_count=0
all_g_total=0
while all_g_count < len(gain_save):
all_g_total=all_g_total + gain_save[all_g_count]
all_g_count=all_g_count + 1
t_count=0
while t_count < len(keep1):
t_stock=keep1[t_count] + qty[t_count]
t_count=t_count + 1
store.append(t_stock)
total_amount=0
keep_total=[]
for total in amount:
total_amount=total_amount + total
keep_total.append(total_amount)
#total_amount=0
return render_to_response('sell_category.html',
{'items':items,'query': q,'keep1':keep1,'name':name,'items':items,
'keep_total':keep_total,'t':store,'amt_save':amt_save,'gain_save':gain_save,'all_amt':all_amt_total,'t_gain':all_g_total,'len':len_keep1,
})
else:
return render_to_response('sell_category.html', {'error': True})
Thanks.
Upvotes: 0
Views: 98
Reputation: 251186
As you're appending everything to a list then you can use zip()
and some string formatting to get the desired result:
items=['Books','Sciences','Comics']
stock_qua=[990,825,930]
sold_qua=[45,75,60]
total_sto=[1035,1035,990]
total_amt=[14576,15666,12666]
profit=[1777,1555,1777]
print "{0:12s}{1:12s}{2:12s}{3:15s}{4:15s}{5:12s}".format("Item Name", "stock qty","sold qty","total stock","total amount","profit")
for item,stock,sold,tot_st,amt,pro in zip(items,stock_qua,sold_qua,total_sto,total_amt,profit):
print "{0:12s}{1:^10d}{2:^10d}{3:^15d}{4:^15d}{5:^12d}".format(item,stock,sold,tot_st,amt,pro)
output:
Item Name stock qty sold qty total stock total amount profit
Books 990 45 1035 14576 1777
Sciences 825 75 1035 15666 1555
Comics 930 60 990 12666 1777
Upvotes: 1