Exemplo n.º 1
0
def filing(request, region, name, period_name):
    company = request.company
    form = registry.get_form(region, name)
    period = get_period(period_name, None)
    if form is None or period is None:
        raise Http404

    create_json = {
        'filer': '/api/v1/account/{}/'.format(company.account.id),
        'region': region,
        'name': name,
        'period_name': period_name,
        'date': None,
        }

    filing = form.tally(company, period)
    real_filings = sorted(company.filings(form=form, period=period),
                          key=lambda f: f.date)

    # How close to on-time was the initial filing?

    if real_filings:
        real_filings[0].offset = (filing.due_date - real_filings[0].date).days

    return render_to_response('publican/filing.html', {
        'filing': filing,
        'form': form,
        'grid': _generate_grid(form, filing),
        'period': period,
        'real_filings': real_filings,
        'today': company.today,
        'create_json': json.dumps(create_json),
        })
Exemplo n.º 2
0
def pdf(request, region, name, period_name):
    """Generate and return a PDF for the given tax form."""

    company = request.company
    form = registry.get_form(region, name)
    period = get_period(period_name, None)
    if form is None or period is None:
        raise Http404

    filing = form.tally(company, period)

    c = canvas.Canvas("hello.pdf")
    for spec in form.pdf_fields:
        x, y, name = spec[:3]
        value = getattr(filing.pages[0], name, None)
        if value is None:
            value = u''
        if isinstance(value, Decimal):
            dollars, cents = unicode(value).split('.')
            c.drawString(x - 8 - c.stringWidth(dollars), y, dollars)
            c.drawString(x + 4, y, cents)
        elif len(spec) > 3:
            value = unicode(value)
            step = spec[3]
            for i, char in enumerate(value):
                c.drawString(x + i * step, y, char)
        else:
            value = unicode(value)
            c.drawString(x, y, value)

    c.showPage()

    datadir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data')
    pdfpath = os.path.join(datadir, form.filename)

    taxform = PdfFileReader(file(pdfpath, 'rb'))
    rendering = PdfFileReader(StringIO(c.getpdfdata()))
    output = PdfFileWriter()

    watermark = rendering.getPage(0)

    page1 = taxform.getPage(0)
    page1.mergePage(watermark)
    output.addPage(page1)

    pdfdata = StringIO()
    output.write(pdfdata)

    return HttpResponse(pdfdata.getvalue(), content_type='application/pdf')