2 # make_invoice makes latex invoices, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 495 2006-08-10 08:02:01Z don $
17 make_invoice - makes invoices using latex
21 make_invoice [options]
24 --log,-l the log file to use to make the invoice
25 --template,-t the template to use to make the invoice
26 --min-time-interval, -m minimum time to bill, default 0
27 --time-granularity, -g time granularity, default 0
28 --hourly-fee, -f hourly fee, default 50.00
29 --svn,-s whether to use subversion or not
30 --debug, -d debugging level (Default 0)
31 --help, -h display this help
32 --man, -m display manual
40 The log file to use to generate the invoice
42 =item B<--template, -t>
44 The tex template to use to generate the invoice
48 Whether to use subversion or not; defaults to yes if .svn exists in
49 the current directory.
53 Invoice directory to place invoice in (automatically calculated if not
56 =item B<--min-time-interval, -m>
58 Minimum time interval to bill, defaults to 0.
60 =item B<--time-granularity, -g>
62 Time granularity, defaults to 0.
64 =item B<--hourly-fee,-f>
66 Hourly fee, defaults to 50.00
70 Debug verbosity. (Default 0)
74 Display brief useage information.
91 use POSIX qw(ceil strftime);
95 use Params::Validate qw(validate_with :types);
97 my %options = (log => undef,
101 time_interval => 0.00,
102 time_granularity => 0.00,
103 hourly_fee => '50.00',
111 GetOptions(\%options,
112 'log|l=s','template|t=s','invoice|i=s','svn|s!',
113 'time_granularity|time-granularity|g=s',
114 'time_interval|min-time-interval|T=s',
115 'hourly_fee|hourly-fee|f=s',
118 'debug|d+','help|h|?','man|m');
120 pod2usage() if $options{help};
121 pod2usage({verbose=>2}) if $options{man};
123 $DEBUG = $options{debug};
126 if (not defined $options{log}) {
127 push @USAGE_ERRORS,"You must pass a log file with --log";
129 if (not defined $options{template}) {
130 push @USAGE_ERRORS,"You must pass a template file with --template";
133 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
135 if (not defined $options{svn}) {
136 $options{svn} = -e '.svn';
140 my $log_fh = IO::File->new($options{log},'r') or
141 die "Unable to open $options{log} for reading: $!";
142 my $template_fh = IO::File->new($options{template},'r') or
143 die "Unable to open $options{template} for reading: $!";
147 my $tex_log = <<'EOF';
148 \setlength\LTleft{0pt plus 1fill minus 1fill}%
150 \begin{longtable}{|p{9cm}|r|r|r|r|}%
153 \mbox{Description} & Item Cost & Quantity & Cost & Total \\
156 my $totaldelta = undef;
160 my $first_date = undef;
161 my $last_date = undef;
169 next if /^Total: \d+\.\d{2}$/;
172 $tex_log .= format_events(date => $date,
181 s/\s*\[[\.\d]+\]\s*\[[\.\d]+\]\s*$//;
183 my ($d1,$d2) = map {s/^\s*\*\s*//;
184 UnixDate(ParseDate($_),'%s')
186 if (not defined $first_date) {
193 my $hours = $delta / (60*60);
194 if ($hours < $options{time_interval}) {
195 $hours = $options{time_interval}
197 if ($options{time_granularity} > 0) {
198 $hours = ceil($hours / $options{time_granularity})*$options{time_granularity};
201 $totaldelta += $delta;
202 $calc_log .= qq($string [).sprintf('%.2f',$hours).qq(] [).sprintf('%.2f',$totaldelta/(60*60)).qq(]\n);
204 elsif (/^\s+-\s*(.+)/) {
208 $calc_log .= $_.qq(\n);
211 $calc_log .= $_.qq(\n);
214 $calc_log .= "\nTotal: ".sprintf('%.2f',$totaldelta/(60*60)).qq(\n);
216 $tex_log .= format_events(date => $date,
229 \multicolumn{4}{|r|}{\textbf{Total}} & \$%
232 $tex_log .= sprintf('%.2f',$total)."%\n";
243 $template = <$template_fh>;
246 my $invoice_start = strftime('%c',localtime($first_date));
247 my $invoice_stop = strftime('%c',localtime($last_date));
249 my $tt = Text::Template->new(TYPE=>'string',
251 DELIMITERS => ['{--','--}'],
253 my $tex_invoice = $tt->fill_in(HASH=>{start => $invoice_start,
254 stop => $invoice_stop,
256 total => sprintf('%0.2f',$total),
259 if (not defined $tex_invoice) {
260 die $Text::Template::ERROR;
263 if ($options{log_only}) {
268 if ($options{tex_only}) {
274 my $invoice_date = strftime('%Y_%m_%d',localtime($last_date));
275 my $invoice_dir = "invoice_$invoice_date";
277 if (not -d $invoice_dir) {
279 system('svn','mkdir',$invoice_dir) == 0 or
280 die "Unable to create invoice directory $invoice_dir";
283 system('mkdir','-p',$invoice_dir) == 0 or
284 die "Unable to create invoice directory $invoice_dir";
289 if (-e 'common_makefile' and not -e '$invoice_dir/Makefile') {
291 system('ln','-sf','../common_makefile','Makefile') == 0 or
292 die "Unable to link common_makefile to Makefile";
294 system('svn','add','Makefile') == 0 or
295 die "Unable to add Makefile";
300 # now we write stuff out
302 my $calc_log_fh = IO::File->new("log_${invoice_date}",'w') or
303 die "Unable to open log_${invoice_date} for writing: $!";
304 print {$calc_log_fh} $calc_log;
307 my $tex_invoice_fh = IO::File->new("invoice_${invoice_date}.tex",'w') or
308 die "Unable to open log_${invoice_date} for writing: $!";
309 print {$tex_invoice_fh} $tex_invoice;
310 close($tex_invoice_fh);
314 "log_${invoice_date}",
315 "invoice_${invoice_date}.tex",
316 ) == 0 or die "Unable to add log and invoice to svn";
317 system('svn','propset','svn:ignore',
318 "*.aux\n*.log\n*.dvi\n*.ps\n*.pdf\nauto\n",
320 ) == 0 or die "Unable to set svn:ignore";
325 my %param = validate_with(params => \@_,
326 spec => {time => {type => SCALAR,
328 date => {type => SCALAR,
330 date2 => {type => SCALAR,
332 total => {type => SCALARREF,
334 events => {type => ARRAYREF,
338 ${$param{total}} += $param{time} * $options{hourly_fee};
340 # $param{date} =~ s/\s+\d+\:\d+\:\d+\s+[A-Z]{0,3}\s*//;
341 my $output = '\hline'."\n".' \mbox{'.strftime('%A, %B %e, %H:%M',localtime($param{date})).
342 ' to '.strftime('%H:%M %Z',localtime($param{date2}))."}\n\n".
343 ' \begin{itemize*}'."\n";
344 $output .= join('',map { s/_/\\_/g; " \\item $_\n";} @{$param{events}});
345 $output .= ' \end{itemize*} & \$'.sprintf('%.2f',$options{hourly_fee}).' & '.sprintf('%.2f',$param{time}).
346 ' & \$'.sprintf('%.2f',$param{time}*$options{hourly_fee}).' & \$'.
347 sprintf('%.2f',${$param{total}}) .
352 ## sub format_events{
353 ## my ($date,$date2,$time,@events) = @_;
354 ## my $output = ' \Fee{'.strftime('%A, %B %e, %H:%M',localtime(UnixDate($date,'%s'))).
355 ## ' to '.strftime('%H:%M %Z',localtime(UnixDate($date2,'%s')))."\n".
356 ## ' \begin{itemize*}'."\n";
357 ## $output .= join('',map {" \\item $_\n"} @events);
358 ## $output .= ' \end{itemize*}}{50.00}{'.$time.'}'."\n";