def thank1_detail(request):
    global user_name
    global user_id1 
    global tot_bal1
    global mf_bal1
    global po_bal1
    global fo_bal1
    global nw_bal1
    global mfid
    global quantity
    global price
    global total
    global ic
    i=0
    counter=0
    user = account.objects.get(pk=user_id1)
    mfid = request.POST["abc1"]
    p = IPO.objects.get(pk=mfid)
    ipo_list = IPO.objects.all()
    fo_list = FO.objects.all()
    for ipo in ipo_list:
     i=i+1
     mfid = request.POST["abc"+str(i)]
     quantity = request.POST["abcd"+str(i)]
     quantity1= request.POST["pqrs"+str(i)]
     #if he has put some quantity in buy
     if quantity!="":
      p = IPO.objects.get(pk=mfid)
      price=int(p.ipo_cost)
      total=price*int(quantity)
      #Quantity asked is less than available
      if int(quantity)<=int(p.ipo_Qty):
       if total<=po_bal1:
        user.po_bal=int(user.po_bal)-total
        user.tot_bal=int(user.tot_bal)-total
        user.save()
        counter=1
        p.ipo_Qty=int(p.ipo_Qty)-int(quantity)
        p.save()
        po_bal1=user.po_bal
        tot_bal1=user.tot_bal
        nw_bal1 =(tot_bal1)-(mf_bal1+po_bal1+fo_bal1)
        mc = Transaction.objects.count()
        c=Transaction(user.user_id,"ipo"+str(mc),mfid,total,quantity)
        c.save()
        c=Transaction.objects.all()
    #  else:
     #     return HttpResponseRedirect("/cooperative_Bank/fm/IPO")
     if quantity1!="":
         co=0;
         c = Transaction.objects.filter(transaction_user=user_id1)
         p = IPO.objects.get(pk=mfid)
         price=int(p.ipo_cost)
         for j in c:
            if j.transaction_trans==mfid:
                co=co+j.transaction_Quantity
         if int(quantity1)<co:
            total=price*int(quantity1)
            user.po_bal=int(user.po_bal)+total
            user.tot_bal=int(user.tot_bal)+total
            user.save()
            counter=1
            p.ipo_Qty=int(p.ipo_Qty)+int(quantity1)
            p.save()
            po_bal1=user.po_bal
            tot_bal1=user.tot_bal
            nw_bal1 =(tot_bal1)-(mf_bal1+po_bal1+fo_bal1)
            #If transaction is possible then they are saved in the transaction table database.
            mc = Transaction.objects.count()
            c=Transaction(user.user_id,"ipo"+str(mc),mfid,total,-int(quantity1))
            c.save()
            c=Transaction.objects.all()
      #   else:
       #   return HttpResponseRedirect("/cooperative_Bank/fm/IPO")           
    return render_to_response('Thank1.html',{'fo_list':fo_list,"user_name":user_name ,"tot_bal1":tot_bal1 , "fo_bal1":fo_bal1 ,"nw_bal1":nw_bal1,"user_id1":user_id1,"total":total,"counter":counter,"mf_bal1":mf_bal1})
def thank2_detail(request):
    global user_name
    global user_id1 
    global tot_bal1
    global mf_bal1
    global po_bal1
    global fo_bal1
    global nw_bal1
    global mfid
    global quantity
    global price
    global total
    i=0
    counter=0
    user = account.objects.get(pk=user_id1)
    fo_list = FO.objects.all()
    for fo in fo_list:
     i=i+1
     mfid = request.POST["abc"+str(i)]
     quantity = request.POST["abcd"+str(i)]
     quantity1= request.POST["pqrs"+str(i)]
     if quantity!="":
      p = FO.objects.get(pk=mfid)
      price=int(p.fo_cost)
      total=price*int(quantity)
      #if quantity asked of FO is less than that available then go on.
      if int(quantity)<=int(p.fo_Qty):
       if total<=mf_bal1:
        user.fo_bal=int(user.fo_bal)-total
        user.tot_bal=int(user.tot_bal)-total
        user.save() 
        counter=1
        fo_bal1=user.fo_bal   
        tot_bal1=user.tot_bal
        nw_bal1 =(tot_bal1)-(mf_bal1+po_bal1+fo_bal1)
        p.fo_Qty=int(p.fo_Qty)-int(quantity)
        p.save()
        mc = Transaction.objects.count()
        c=Transaction(user.user_id,"FO"+str(mc),mfid,total,quantity)
        c.save()
        c=Transaction.objects.all()
      #else Return to the FO list page.
   #   else:
    #      return HttpResponseRedirect("/cooperative_Bank/fm/FO")  
     if quantity1!="":
         co=0;
         c = Transaction.objects.filter(transaction_user=user_id1)
         p = FO.objects.get(pk=mfid)
         price=int(p.fo_cost)
         for j in c:
            if j.transaction_trans==mfid:
                co=co+j.transaction_Quantity
         if int(quantity1)<co:
            total=price*int(quantity1)
            user.fo_bal=int(user.fo_bal)+total
            user.tot_bal=int(user.tot_bal)+total
            user.save() 
            counter=1
            fo_bal1=user.fo_bal   
            tot_bal1=user.tot_bal
            nw_bal1 =(tot_bal1)-(mf_bal1+po_bal1+fo_bal1)
            p.fo_Qty=int(p.fo_Qty)+int(quantity1)
            p.save()
            mc = Transaction.objects.count()
            c=Transaction(user.user_id,"FO"+str(mc),mfid,total,-int(quantity))
            c.save()
            c=Transaction.objects.all()        
     #    else:
      #    return HttpResponseRedirect("/cooperative_Bank/fm/FO") 
    return render_to_response('Thank2.html',{'fo_list':fo_list,"user_name":user_name ,"tot_bal1":tot_bal1 , "fo_bal1":fo_bal1 ,"nw_bal1":nw_bal1,"user_id1":user_id1,"total":total,"counter":counter,"mf_bal1":mf_bal1})
def thank_detail(request):
    global user_name
    global user_id1 
    global tot_bal1
    global mf_bal1
    global po_bal1
    global fo_bal1
    global nw_bal1
    global mfid
    global quantity
    global price
    global total
    global mc
    global c
    i=0
    counter=0
    user = account.objects.get(pk=user_id1)
    mfid = request.POST["abc1"]
    p = MutualFund.objects.get(pk=mfid)
    mf_list = MutualFund.objects.all()
    fo_list = FO.objects.all()
  ##We can simultaneously buy /sell more than one product depending on the balance.
    for mf in mf_list:
     i=i+1
     mfid = request.POST["abc"+str(i)]
     quantity = request.POST["abcd"+str(i)]
     quantity1= request.POST["pqrs"+str(i)]
    #if he has entered some quantity in buy then check if it is feasible
     if quantity!="":
      p = MutualFund.objects.get(pk=mfid)
      price=int(p.mf_cost)
      total=price*int(quantity)
      #If quantity asked by him is less than available and his balance in Mutual Fund is greater than the cost of acquisition then he can buy
      if int(quantity)<=int(p.mf_Qty):
       if total<=mf_bal1:
        c=Transaction.objects.all()
        mc = Transaction.objects.count()
        user.mf_bal=int(user.mf_bal)-total
        user.tot_bal=int(user.tot_bal)-total
        user.save()
        p.mf_Qty=int(p.mf_Qty)-int(quantity)
        p.save()
        counter=1
        mf_bal1=user.mf_bal 
        tot_bal1=user.tot_bal
        nw_bal1 =(tot_bal1)-(mf_bal1+po_bal1+fo_bal1)
     #Transaction table is updated .
        c=Transaction(user.user_id,"mf"+str(mc),mfid,total,quantity)
        c.save()
        c=Transaction.objects.all()
      #else:
       #   return HttpResponseRedirect("/cooperative_Bank/fm/MF")  
      #Sell option was added in Phase 2 of our project in which customer was provided with the option of selling the MF he is currently holding
     if quantity1!="":
         co=0;
         c = Transaction.objects.filter(transaction_user=user_id1)
         p = MutualFund.objects.get(pk=mfid)
         price=int(p.mf_cost)
         for j in c:
            if j.transaction_trans==mfid:
                co=co+j.transaction_Quantity
         if int(quantity1)<co:
            total=price*int(quantity1)
            user.mf_bal=int(user.mf_bal)+total
            user.tot_bal=int(user.tot_bal)+total
            user.save()
            p.mf_Qty=int(p.mf_Qty)+int(quantity1)
            p.save()
            counter=1
            mf_bal1=user.mf_bal 
            tot_bal1=user.tot_bal
            nw_bal1 =(tot_bal1)-(mf_bal1+po_bal1+fo_bal1)
            # print_vars()  
            c=Transaction(user.user_id,"mf"+str(mc),mfid,total,-int(quantity1))
            c.save()
            c=Transaction.objects.all()
         #else:
        #  return HttpResponseRedirect("/cooperative_Bank/fm/MF")
    return render_to_response('Thank.html',{'fo_list':fo_list,"user_name":user_name ,"tot_bal1":tot_bal1 , "fo_bal1":fo_bal1 ,"nw_bal1":nw_bal1,"user_id1":user_id1,"total":total,"counter":counter,"mf_bal1":mf_bal1,"quantity":quantity,"c":c})