Added (not so clean) invoice PDF generation.

This commit is contained in:
Paul van Tilburg 2011-11-03 11:40:58 +01:00
parent 55b40bf0b6
commit ea78905fec
2 changed files with 124 additions and 5 deletions

View File

@ -27,7 +27,8 @@ unless defined? PUBLIC_DIR
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
:default => "%Y-%m-%d %H:%M",
:month_and_year => "%B %Y",
:month_code => "%Y%m")
:month_code => "%Y%m",
:day_code => "%Y%m%d")
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
:default => "%Y-%m-%d",
:month_and_year => "%B %Y")
@ -195,14 +196,41 @@ module StopTime::Controllers
end
class CustomersNInvoicesX
def get(customer_id, number)
@month = DateTime.new(number[0..3].to_i, number[4..5].to_i, 1)
@number = number[5..-1].to_i
def get(customer_id, invoice_id)
@month = DateTime.new(invoice_id[0..3].to_i, invoice_id[4..5].to_i, 1)
@number = invoice_id[6..-1]
# FIXME: make this (much) nicer!
invoice_id.gsub!(/\.pdf$/, '')
if m = @number.match(/(\d+)\.(\w+)$/)
@number = m[1].to_i
@format = m[2]
else
@number = @number.to_i
@format = "html"
end
@customer = Customer.find(customer_id)
@tasks = @customer.task_summary(@month)
render :invoice
if @format == "html"
render :invoice
elsif @format == "pdf"
pdf_file = PUBLIC_DIR + "#{invoice_id}.pdf"
unless pdf_file.exist?
_generate_invoice_pdf(@customer, @tasks, @month, invoice_id)
end
redirect(StaticX, pdf_file.basename)
end
end
def _generate_invoice_pdf(customer, tasks, month, invoice_id)
template = TEMPLATE_DIR + "invoice.tex.erb"
tex_file = PUBLIC_DIR + "#{invoice_id}.tex"
erb = ERB.new(File.read(template))
File.open(tex_file, "w") { |f| f.write(erb.result(binding)) }
system("rubber --pdf --inplace #{tex_file}")
system("rubber --clean --inplace #{tex_file}")
end
end

91
templates/invoice.tex.erb Normal file
View File

@ -0,0 +1,91 @@
\documentclass[a4paper,12pt,oneside,dutch]{isodoc}
\usepackage{array}
\setupdocument{
% Language and style,
dutch,
fill,
fontpackage=pxfonts,
% Company info.
company=,
who=Foo Bedrijf,
street=Zomaareenstraat 123,
city=Ergens,
zip=1234~ZZ,
country=The Netherlands,
countrycode=NL,
logoaddress={\hspace{-4.4em}
\begin{tabular}{l@{\ }l}
\addresstext: & \who\\
& \street\\
& \zip\ \city\\[.2em]
\cellphonetext: & \lead\cellphone\\
\emailtext: & \email\\[.2em]
\end{tabular}},
% Footer.
areacode=31,
cellphone=6-12345678,
email=info@foobedrijf.nl,
% Addressee info.
%foreign,
to={<%= customer.name %>\\<%= customer.address_street%>\\
<%= customer.address_postal_code %> <%= customer.address_city %>},
% Headlines.
date=<%= DateTime.now.to_formatted_s(:day_code) %>,
ourref=<%= invoice_id %>,
subject=Factuur <%= month.to_formatted_s(:month_and_year) %>,
yourref=,
% Payment data.
term=30,
accountno=12.34.567.890,
accountname=Foo Bedrijf,
}
\newcommand{\addresstext}{adres}
\renewcommand{\chambertext}{KvK-nr}
\renewcommand{\datetext}{Factuurdatum}
\renewcommand{\emailtext}{e-mail}
\renewcommand{\invoicetext}{\Large\textbf{Factuur}}
\renewcommand{\ourreftext}{Factuurnummer}
\renewcommand{\referencetext}{factuurnummer}
\renewcommand{\vatnotext}{btw-nr}
\renewcommand{\websitetext}{website}
\newenvironment{ihtable}%
%{\vskip1em\tabularx{\linewidth}{@{}X@{\ \barsep\quad}|r@{\ \barsep\quad}r@{\ \barsep\quad}r@{}}
{\vskip1em\tabularx{\linewidth}{@{}X|@{\quad}r|@{\quad}r|@{\quad}r@{}}
\descriptiontext&Aantal uur&Uurtarief&\amounttext\ML}%
{\endtabularx}
\newcommand{\ihitem}[4]{#1&#2&\currency~#3&\currency~#4\\}
\newcommand{\ihsubtotal}[1]{\cmidrule[.0em]{4-4}%
\textit{Subtotaal}&&&\currency~#1\\}
\newcommand{\inosubtotal}{&&&\\}
\newcommand{\ihvat}[1]{\textit{Btw-heffing 19\%}&&&\currency~#1\\}
\newcommand{\ihtotal}[1]{\cmidrule[.05em]{4-4}%
\textbf{\Totaltext}&&&\textbf{\currency~#1}}
\begin{document}
\invoice{
\hypersetup{pdftitle=\invoicetext\ naar\ \toname\ gedateerd \date\ (\ourref)}
\begin{ihtable}
<% total = 0.0 %>
<% tasks.each do |task, line| %>
\ihitem{<%= task.name %>}{<% "%.2f" % line[0] %>}%
{<%= "%.2f" % line[1] %>}{<%= "%.2f" % line[2] %>}
<% total += line[2] %>
<% end %>
%\ihsubtotal{...}
\inosubtotal
%\ihvat{...}
\ihtotal{<%= "%.2f" % total %>}
\end{ihtable}
\vspace{2em}
Ik verzoek u vriendelijk het verschuldigde bedrag binnen 30 dagen na
factuurdatum over te maken onder vermelding van het factuurnummer. \\
\accountdata
}
\end{document}