Skip to content

Commit dfcb903

Browse files
committed
Invoice model wired
1 parent dc7380b commit dfcb903

5 files changed

Lines changed: 370 additions & 14 deletions

File tree

ada.pdf

1.8 KB
Binary file not shown.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
import datetime
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
('projects', '0019_auto_20160115_1052'),
12+
]
13+
14+
operations = [
15+
migrations.AddField(
16+
model_name='details',
17+
name='type',
18+
field=models.CharField(default='Developer', max_length=2),
19+
preserve_default=False,
20+
),
21+
migrations.AddField(
22+
model_name='invoice',
23+
name='address',
24+
field=models.CharField(default='Karachi', max_length=50),
25+
preserve_default=False,
26+
),
27+
migrations.AlterField(
28+
model_name='details',
29+
name='desc',
30+
field=models.CharField(max_length=25),
31+
),
32+
migrations.AlterField(
33+
model_name='milestone',
34+
name='description',
35+
field=models.CharField(max_length=1000),
36+
),
37+
migrations.AlterField(
38+
model_name='project',
39+
name='last_updated',
40+
field=models.DateTimeField(default=datetime.datetime.now),
41+
),
42+
]

projects/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def __unicode__(self):
9898
class Milestone(models.Model):
9999
url_id = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
100100
title = models.CharField(max_length = 50)
101-
description = models.CharField(max_length = 500)
101+
description = models.CharField(max_length = 1000)
102102
cost = models.PositiveIntegerField(default=0)
103103
start_date = models.DateField()
104104
deadline = models.DateField()
@@ -141,6 +141,7 @@ class Invoice(models.Model):
141141
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
142142
project = models.ForeignKey(Project, null = False)
143143
date = models.DateField(default = datetime.now)
144+
address = models.CharField(max_length =50)
144145
discount = models.PositiveIntegerField()
145146
total = models.PositiveIntegerField(null = False)
146147

@@ -150,7 +151,8 @@ def __unicode__(self):
150151
class Details(models.Model):
151152
invoice = models.ForeignKey(Invoice, null = False)
152153
title = models.CharField(max_length = 50)
153-
desc = models.CharField(max_length = 20)
154+
desc = models.CharField(max_length = 25)
155+
type = models.CharField(max_length=2)
154156
cost = models.PositiveIntegerField()
155157
qty = models.PositiveIntegerField()
156158

projects/views.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
508511
def 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)
512516
def 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

Comments
 (0)