@@ -505,8 +505,12 @@ def logout_view(request):
505505 return HttpResponseRedirect ('/site/' )
506506
507507@user_passes_test (lambda u : u .is_superuser )
508+ def invoice_page (request ):
509+ pass
510+ @user_passes_test (lambda u : u .is_superuser )
508511def invoice_list (request ):
509- return HttpResponse ("Page Exists!" )
512+ invoices = Invoice .objects .all ()
513+ return render (request , 'projects/invoice_list.html' , {'invoices' : invoices })
510514
511515@user_passes_test (lambda u : u .is_superuser )
512516def invoice (request , project_id ):
@@ -522,6 +526,12 @@ def invoice(request, project_id):
522526 except ValueError :
523527 discount = 0
524528
529+ invoice = project .invoice_set .create (
530+ address = address ,
531+ discount = discount ,
532+ total = 0 ,
533+ )
534+
525535 for key in request .POST :
526536 try :
527537 x = project .milestone_set .get (title = key )
@@ -535,9 +545,16 @@ def invoice(request, project_id):
535545 except ValueError :
536546 qty = 1
537547 total += (x .cost * qty )
538- data .append ({'title' : x .title , 'cost' : x .cost , 'type' :project .pay_type , 'qty' : qty })
539-
540- name = generate (project .client , project .client_mail , data , address ,total , discount )
548+
549+ invoice .details_set .create (title = x .title ,
550+ desc = x .description [0 :20 ],
551+ cost = x .cost ,
552+ type = project .pay_type ,
553+ qty = qty )
554+
555+ invoice .total = total
556+ invoice .save ()
557+ name = generate (invoice )
541558 chunk_size = 8192
542559 response = StreamingHttpResponse (FileWrapper (open (name + ".pdf" ), chunk_size ),
543560 content_type = mimetypes .guess_type (name + ".pdf" ))
@@ -549,11 +566,21 @@ def invoice(request, project_id):
549566 return render (request ,'projects/invoice.html' , {'unpaid' :unpaid , 'project_id' :project .id })
550567
551568
552- def generate (name , email , data , addr , total , discount = 0 ):
569+ def generate (invoice ):
553570 print ("Generating PDF" )
554571 from reportlab .pdfgen import canvas
555572 from reportlab .lib .units import inch
556573 from reportlab .lib .pagesizes import letter
574+
575+ print (invoice .project .id )
576+ project = Project .objects .get (id = invoice .project .id )
577+ name = project .client
578+ email = project .client_mail
579+ addr = invoice .address
580+ total = invoice .total
581+ discount = invoice .discount
582+ data = invoice .details_set .all ()
583+
557584 c = canvas .Canvas (name + ".pdf" , pagesize = letter )
558585 c .drawString (1 * inch , 9 * inch , "Sofza" )
559586 c .drawString (1 * inch , 8.7 * inch , "Raised By" )
@@ -576,22 +603,22 @@ def generate(name, email, data, addr, total, discount=0):
576603 c .line (0 , point * inch , 8 * inch , point * inch )
577604 for x in data :
578605 point -= 0.2
579- c .drawString (1 * inch , point * inch , x [ ' title' ] )
580- c .drawString (5 * inch , point * inch , x [ ' type' ] + str (x [ ' cost' ] ))
581- c .drawString (6 * inch , point * inch , str (x [ ' qty' ] ))
582- c .drawString (7 * inch , point * inch , x [ ' type' ] + str (x [ ' qty' ] * x [ ' cost' ] ))
606+ c .drawString (1 * inch , point * inch , x . title )
607+ c .drawString (5 * inch , point * inch , x . type + str (x . cost ))
608+ c .drawString (6 * inch , point * inch , str (x . qty ))
609+ c .drawString (7 * inch , point * inch , x . type + str (x . qty * x . cost ))
583610 point -= 0.1
584611 c .line (0 , point * inch , 8 * inch , point * inch )
585612 point -= 0.3
586613
587- c .drawString (1 * inch , point * inch , "Total: " + x [ ' type' ] + str (total ))
614+ c .drawString (1 * inch , point * inch , "Total: " + x . type + str (total ))
588615 point -= 0.3
589616
590617 if discount :
591- c .drawString (1 * inch , point * inch , "Discount: " + x [ ' type' ] + str (discount ))
618+ c .drawString (1 * inch , point * inch , "Discount: " + x . type + str (discount ))
592619 point -= 0.3
593620 total -= discount
594- c .drawString (1 * inch , point * inch , "Due amount: " + x [ ' type' ] + str (total ))
621+ c .drawString (1 * inch , point * inch , "Due amount: " + x . type + str (total ))
595622 point -= 0.3
596623 c .save ()
597624 return name
0 commit comments