Beispiel #1
0
def genInvoice(response, invoice, request):

    MARGIN_SIZE = 8 * mm
    PAGE_SIZE = A4

    # c = canvas.Canvas("hello.pdf")
    # c.drawString(9*cm, 19*cm, "Hello World!")

    pdf_doc = SimpleDocTemplate(response,
                                pagesize=PAGE_SIZE,
                                leftMargin=MARGIN_SIZE,
                                rightMargin=MARGIN_SIZE,
                                topMargin=4 * MARGIN_SIZE,
                                bottomMargin=3 * MARGIN_SIZE)

    # data = [['', '', '', 'Grand Total', '' , pFooterGrandTotal]]

    pdf_doc.invoice = invoice
    pdf_doc.request = request

    tableHeaderStyle = styles['Normal'].clone('tableHeaderStyle')
    tableHeaderStyle.textColor = colors.white
    tableHeaderStyle.fontSize = 7

    pHeadProd = Paragraph('<strong>Product/<br/>Service</strong>',
                          tableHeaderStyle)
    pHeadDetails = Paragraph('<strong>Details</strong>', tableHeaderStyle)
    pHeadTaxCode = Paragraph('<strong>HSN/<br/>SAC</strong>', tableHeaderStyle)
    pHeadQty = Paragraph('<strong>Qty</strong>', tableHeaderStyle)
    pHeadPrice = Paragraph('<strong>Rate</strong>', tableHeaderStyle)
    # pHeadTotal = Paragraph('<strong>Total</strong>' , tableHeaderStyle)
    pHeadsubTotalTax = Paragraph('<strong>IGST <br/> Tax</strong>',
                                 tableHeaderStyle)
    pHeadsubTotal = Paragraph('<strong>Sub Total</strong>', tableHeaderStyle)

    # # bookingTotal , bookingHrs = getBookingAmount(o)
    #
    # pFooterQty = Paragraph('%s' % ('o.quantity') , styles['Normal'])
    # pFooterTax = Paragraph('%s' %('tax') , styles['Normal'])
    # pFooterTotal = Paragraph('%s' % (1090) , styles['Normal'])
    # pFooterGrandTotal = Paragraph('%s' % ('INR 150') , tableHeaderStyle)

    data = [[
        pHeadProd, pHeadDetails, pHeadTaxCode, pHeadPrice, pHeadQty,
        pHeadsubTotalTax, pHeadsubTotal
    ]]

    totalQuant = 0
    totalTax = 0
    grandTotal = 0
    tableBodyStyle = styles['Normal'].clone('tableBodyStyle')
    tableBodyStyle.fontSize = 7

    for i in json.loads(invoice.products):
        print '***********', i
        pDescSrc = i['data']['name']

        totalQuant += i['quantity']

        print i
        #
        # if 'price' not in i:
        #     print "Continuing"
        #     continue

        i['subTotalTax'] = i['data']['price'] * i['quantity'] * (
            i['data']['productMeta']['taxRate'] / float(100))

        i['subTotal'] = i['data']['price'] * i['quantity'] * (
            1 + i['data']['productMeta']['taxRate'] / float(100))

        totalTax += i['subTotalTax']
        grandTotal += i['subTotal']

        pBodyProd = Paragraph('Product', tableBodyStyle)
        pBodyTitle = Paragraph(pDescSrc, tableBodyStyle)
        pBodyQty = Paragraph(str(i['quantity']), tableBodyStyle)
        pBodyPrice = Paragraph(str(i['data']['price']), tableBodyStyle)
        # if 'taxCode' in i:
        taxCode = '%s(%s %%)' % (i['data']['productMeta']['code'],
                                 i['data']['productMeta']['taxRate'])
        # else:
        # taxCode = ''

        pBodyTaxCode = Paragraph(taxCode, tableBodyStyle)
        pBodysubTotalTax = Paragraph(str(i['subTotalTax']), tableBodyStyle)
        pBodyTotal = Paragraph(str(i['quantity'] * i['data']['price']),
                               tableBodyStyle)
        pBodySubTotal = Paragraph(str(i['subTotal']), tableBodyStyle)

        data.append([
            pBodyProd, pBodyTitle, pBodyTaxCode, pBodyPrice, pBodyQty,
            pBodyTotal, pBodysubTotalTax, pBodySubTotal
        ])

    invoice.subTotal = grandTotal
    # invoice.saveInvoiceForm()

    tableGrandStyle = tableHeaderStyle.clone('tableGrandStyle')
    tableGrandStyle.fontSize = 10

    data += [[
        '', '', '', '', '', '',
        Paragraph(str(totalTax), tableBodyStyle),
        Paragraph(str(grandTotal), tableBodyStyle)
    ],
             [
                 '', '', '', '', '',
                 Paragraph('sub Total (INR)', tableHeaderStyle), '',
                 Paragraph(str(grandTotal), tableGrandStyle)
             ]]
    t = Table(data)
    ts = TableStyle([
        ('ALIGN', (1, 1), (-3, -3), 'RIGHT'),
        ('VALIGN', (0, 1), (-1, -3), 'TOP'),
        ('VALIGN', (0, -2), (-1, -2), 'TOP'),
        ('VALIGN', (0, -1), (-1, -1), 'TOP'),
        ('SPAN', (-3, -1), (-2, -1)),
        ('TEXTCOLOR', (0, 0), (-1, 0), colors.white),
        ('BACKGROUND', (0, 0), (-1, 0), themeColor),
        ('LINEABOVE', (0, 0), (-1, 0), 0.25, themeColor),
        ('LINEABOVE', (0, 1), (-1, 1), 0.25, themeColor),
        ('BACKGROUND', (-2, -2), (-1, -2), colors.HexColor('#eeeeee')),
        ('BACKGROUND', (-3, -1), (-1, -1), themeColor),
        ('LINEABOVE', (-2, -2), (-1, -2), 0.25, colors.gray),
        ('LINEABOVE', (0, -1), (-1, -1), 0.25, colors.gray),
        # ('LINEBELOW',(0,-1),(-1,-1),0.25,colors.gray),
    ])
    t.setStyle(ts)
    t._argW[0] = 1.5 * cm
    t._argW[1] = 6 * cm
    t._argW[2] = 2.4 * cm
    t._argW[3] = 2 * cm
    t._argW[4] = 2 * cm
    t._argW[5] = 2 * cm
    t._argW[6] = 1.6 * cm
    t._argW[7] = 2 * cm

    story = []

    expHead = expanseReportHead(request, invoice)
    story.append(Spacer(2.5, 2 * cm))
    story.append(expHead)
    story.append(Spacer(2.5, 0.75 * cm))

    adrs = invoice.customer

    if invoice.customer.gst is None:
        tin = 'NA'
    else:
        tin = invoice.customer.gst

    summryParaSrc = """
    <font size='11'><strong>Customer details:</strong></font> <br/><br/>
    <font size='9'>
    %s<br/>
    %s<br/>
    %s<br/>
    %s<br/>
    %s , %s<br/>
    %s<br/>
    <strong>GSTIN:</strong>%s<br/>
    </font>
    """ % (invoice.customer.name, invoice.customer.company,
           invoice.customer.street, invoice.customer.city,
           invoice.customer.state, invoice.customer.pincode,
           invoice.customer.country, tin)
    story.append(Paragraph(summryParaSrc, styleN))
    story.append(t)
    story.append(Spacer(2.5, 0.5 * cm))

    pdf_doc.build(story,
                  onFirstPage=addPageNumber,
                  onLaterPages=addPageNumber,
                  canvasmaker=PageNumCanvas)