diff options
author | tetragon <tetragon@4979c152-3d1c-0410-bac9-87ea11338e46> | 2007-09-10 21:13:14 +0000 |
---|---|---|
committer | tetragon <tetragon@4979c152-3d1c-0410-bac9-87ea11338e46> | 2007-09-10 21:13:14 +0000 |
commit | 59b539628096d57bc8803682bb9990939d5e9178 (patch) | |
tree | 8165aeba946e1a329d5579d5c0b7ab34ad65907a | |
parent | a5577e8f380ef6301e1a976780c7636da32e0a9f (diff) |
Initial templating of the GL report.
Adding CSV output
git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/trunk@1555 4979c152-3d1c-0410-bac9-87ea11338e46
-rwxr-xr-x | LedgerSMB/Template.pm | 11 | ||||
-rwxr-xr-x | LedgerSMB/Template/CSV.pm | 106 | ||||
-rw-r--r-- | UI/gl-report.csv | 3 | ||||
-rw-r--r-- | UI/gl-report.html | 92 | ||||
-rw-r--r-- | bin/gl.pl | 185 |
5 files changed, 294 insertions, 103 deletions
diff --git a/LedgerSMB/Template.pm b/LedgerSMB/Template.pm index 2432b5f1..de0fa797 100755 --- a/LedgerSMB/Template.pm +++ b/LedgerSMB/Template.pm @@ -11,7 +11,7 @@ This module renders templates. =over -=item new(user => \%myconfig, template => $string, format => 'HTML', [language => $string,] [include_path => $path], [no_auto_output => $bool], [method => $string] ); +=item new(user => \%myconfig, template => $string, format => 'HTML', [language => $string,] [include_path => $path], [no_auto_output => $bool], [method => $string], [no_escape => $bool]] ); This command instantiates a new template: template is the file name of the template to be processed. @@ -19,6 +19,7 @@ format is the type of format to be used. Currently only HTML is supported language (optional) specifies the language for template selection. include_path allows one to override the template directory and use this with user interface templates. no_auto_output disables the automatic output of rendered templates. +no_escape disables escaping on the template variables. method is the output method to use, defaults to HTTP media is a synonym for method @@ -62,6 +63,7 @@ sub new { $self->{format} = $args{format}; $self->{format} = 'PS' if lc $self->{format} eq 'postscript'; $self->{language} = $args{language}; + $self->{no_escape} = $args{no_escape}; if ($args{outputfile}) { $self->{outputfile} = "${LedgerSMB::Sysconfig::tempdir}/$args{outputfile}"; @@ -112,7 +114,12 @@ sub render { throw Error::Simple $@; } - my $cleanvars = $format->can('preprocess')->($vars); + my $cleanvars; + if ($self->{no_escape}) { + $cleanvars = $vars; + } else { + $cleanvars = $format->can('preprocess')->($vars); + } if (UNIVERSAL::isa($self->{locale}, 'LedgerSMB::Locale')){ $cleanvars->{text} = sub { return $self->{locale}->text(@_)}; diff --git a/LedgerSMB/Template/CSV.pm b/LedgerSMB/Template/CSV.pm new file mode 100755 index 00000000..42324a89 --- /dev/null +++ b/LedgerSMB/Template/CSV.pm @@ -0,0 +1,106 @@ + +=head1 NAME + +LedgerSMB::Template::CSV Template support module for LedgerSMB + +=head1 METHODS + +=over + +=item get_template ($name) + +Returns the appropriate template filename for this format. + +=item preprocess ($vars) + +Returns $vars. + +=item process ($parent, $cleanvars) + +Processes the template for text. + +=item postprocess ($parent) + +Returns the output filename. + +=back + +=head1 Copyright (C) 2007, The LedgerSMB core team. + +This work contains copyrighted information from a number of sources all used +with permission. + +It is released under the GNU General Public License Version 2 or, at your +option, any later version. See COPYRIGHT file for details. For a full list +including contact information of contributors, maintainers, and copyright +holders, see the CONTRIBUTORS file. +=cut + +package LedgerSMB::Template::CSV; + +use Error qw(:try); +use Template; +use LedgerSMB::Template::TTI18N; + +sub get_template { + my $name = shift; + return "${name}.csv"; +} + +sub preprocess { + my $rawvars = shift; + my $vars; + my $type = ref $rawvars; + + #XXX fix escaping function + return $rawvars if $type =~ /^LedgerSMB::Locale/; + if ( $type eq 'ARRAY' ) { + for (@{$rawvars}) { + push @{$vars}, preprocess( $_ ); + } + } elsif ( $type eq 'HASH' ) { + for ( keys %{$rawvars} ) { + $vars->{preprocess($_)} = preprocess( $rawvars->{$_} ); + } + } else { + $vars = $rawvars; + $vars =~ s/\ / /; + $vars =~ s/(\t\n\r )+/ /g; + $vars =~ s/(^ +| +$)//g; + $vars =~ s/~/\\~/g; + $vars =~ s/<.*?>//g; + $vars = qq|"$vars"| if $vars !~ /^\w+$/; + $vars = '' if $vars =~ /^""$/; + } + return $vars; +} + +sub process { + my $parent = shift; + my $cleanvars = shift; + my $template; + + $template = Template->new({ + INCLUDE_PATH => $parent->{include_path}, + START_TAG => quotemeta('<?lsmb'), + END_TAG => quotemeta('?>'), + DELIMITER => ';', + }) || throw Error::Simple Template->error(); + + if (not $template->process( + get_template($parent->{template}), + {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs, + 'escape' => \&preprocess}, + "$parent->{outputfile}.csv", binmode => ':utf8')) { + throw Error::Simple $template->error(); + } + $parent->{mimetype} = 'text/plain'; +} + +sub postprocess { + my $parent = shift; + $parent->{rendered} = "$parent->{outputfile}.csv"; + return "$parent->{outputfile}.csv"; +} + +1; diff --git a/UI/gl-report.csv b/UI/gl-report.csv new file mode 100644 index 00000000..83dc27ce --- /dev/null +++ b/UI/gl-report.csv @@ -0,0 +1,3 @@ +<?lsmb FOREACH column IN columns ?><?lsmb heading.$column ?><?lsmb IF NOT loop.last ?>~<?lsmb END ?><?lsmb END ?> +<?lsmb FOREACH row IN rows ?><?lsmb FOREACH column IN columns ?><?lsmb row.$column ?><?lsmb IF NOT loop.last ?>~<?lsmb END ?><?lsmb END ?> +<?lsmb END ?><?lsmb FOREACH column IN columns ?><?lsmb totals.$column ?><?lsmb IF NOT loop.last ?>~<?lsmb END ?><?lsmb END ?> diff --git a/UI/gl-report.html b/UI/gl-report.html new file mode 100644 index 00000000..69e2d866 --- /dev/null +++ b/UI/gl-report.html @@ -0,0 +1,92 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> +<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> +<head> + <title><?lsmb form.titlebar ?></title> + <meta http-equiv="Pragma" content="no-cache" /> + <meta http-equiv="Expires" content="-1" /> + <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" /> + <link rel="stylesheet" href="css/<?lsmb form.stylesheet ?>" type="text/css" title="LedgerSMB stylesheet" /> + <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> + <meta name="robots" content="noindex,nofollow" /> + +</head> + + + +<body> + +<table width=100%> + <tr> + <th class=listtop><?lsmb form.title ?></th> + </tr> + <tr height="5"></tr> + <tr> + <td><?lsmb options ?></td> + </tr> + <tr> + <td> + <table width=100%> + <tr class="listheading"> +<?lsmb FOREACH column IN columns ?> +<?lsmb heading.$column ?> +<?lsmb END ?> + </tr> + +<?lsmb FOREACH row IN rows ?> +<?lsmb IF row.is_subtotal ?> + <tr class="listsubtotal"> + <?lsmb FOREACH column IN columns ?> + <th align="right" class="listsubtotal"><?lsmb row.$column ?></th> + <?lsmb END ?> + </tr> +<?lsmb ELSE ?> + <tr class="listrow<?lsmb row.i ?>"> + <?lsmb FOREACH column IN columns ?> + <?lsmb IF column == 'balance' ?> + <td align="right"> + <?lsmb ELSIF column == 'debit' ?> + <td align="right"> + <?lsmb ELSIF column == 'credit' ?> + <td align="right"> + <?lsmb ELSE ?> + <td> + <?lsmb END ?> + <?lsmb row.$column ?></td> + <?lsmb END ?> + </tr> +<?lsmb END ?> +<?lsmb END ?> + <tr class=listtotal> +<?lsmb FOREACH column IN columns ?> +<th align=right class=listtotal><?lsmb totals.$column ?></th> +<?lsmb END ?> + </tr> + </table> + </td> + </tr> + <tr> + <td><hr size=3 noshade></td> + </tr> +</table> + +<br /> + +<form method=post action=gl.pl> +<?lsmb FOREACH pair IN form.callback.split('&') ?> +<?lsmb hidden = pair.split('=') ?> +<?lsmb IF NOT loop.first ?> +<input type="hidden" name="<?lsmb hidden.0 ?>" value="<?lsmb hidden.1 ?>" /> +<?lsmb END ?> +<?lsmb END ?> +<input type="hidden" name="callback" value="<?lsmb form.callback ?>" /> +<?lsmb FOREACH button IN buttons ?> +<?lsmb button ?> +<?lsmb END ?> +<button type="submit" class="submit" name="action" value="csv_gl_report"> +<?lsmb text('CSV Report') ?> +</button> +</form> + +</body> +</html> @@ -47,6 +47,7 @@ use LedgerSMB::GL; use LedgerSMB::PE; +use LedgerSMB::Template; require "bin/arap.pl"; @@ -640,31 +641,6 @@ sub generate_report { $column_header{balance} = "<th>" . $locale->text('Balance') . "</th>"; $column_header{cleared} = qq|<th>| . $locale->text('R') . qq|</th>|; - $form->header; - - print qq| -<body> - -<table width=100%> - <tr> - <th class=listtop>$form->{title}</th> - </tr> - <tr height="5"></tr> - <tr> - <td>$option</td> - </tr> - <tr> - <td> - <table width=100%> - <tr class=listheading> -|; - - for (@column_index) { print "$column_header{$_}\n" } - - print " - </tr> -"; - # add sort to callback $form->{callback} = "$callback&sort=$form->{sort}"; $callback = $form->escape( $form->{callback} ); @@ -677,24 +653,17 @@ sub generate_report { $cml = -1 if $form->{contra}; } + my @rows; if ( ( $form->{accno} || $form->{gifi_accno} ) && $form->{balance} ) { + my %column_data; - for (@column_index) { $column_data{$_} = "<td> </td>" } - $column_data{balance} = "<td align=right>" - . $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, - 2, 0 ) - . "</td>"; + for (@column_index) { $column_data{$_} = " " } + $column_data{balance} = + $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, + 2, 0 ); - $i++; - $i %= 2; - print qq| - <tr class=listrow$i> -|; - for (@column_index) { print "$column_data{$_}\n" } - - print qq| - </tr> -|; + $column_data{i} = 1; + push @rows, \%column_data; } # reverse href @@ -702,13 +671,14 @@ sub generate_report { $form->sort_order(); $href =~ s/direction=$form->{direction}/direction=$direction/; - $i = 0; + my $i = 0; foreach $ref ( @{ $form->{GL} } ) { + my %column_data; # if item ne sort print subtotal if ( $form->{l_subtotal} eq 'Y' ) { if ( $sameitem ne $ref->{ $form->{sort} } ) { - &gl_subtotal; + push @rows, &gl_subtotal_tt(); } } @@ -725,62 +695,45 @@ sub generate_report { $ref->{credit} = $form->format_amount( \%myconfig, $ref->{credit}, 2, " " ); - for (qw(id transdate)) { $column_data{$_} = "<td>$ref->{$_}</td>" } + for (qw(id transdate)) { $column_data{$_} = "$ref->{$_}" } $column_data{reference} = -"<td><a href=$ref->{module}.pl?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback>$ref->{reference}</td>"; +"<a href=$ref->{module}.pl?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback>$ref->{reference}"; $ref->{notes} =~ s/\r?\n/<br>/g; for (qw(description source memo notes department)) { - $column_data{$_} = "<td>$ref->{$_} </td>"; + $column_data{$_} = "$ref->{$_} "; } - $column_data{debit} = "<td align=right>$ref->{debit}</td>"; - $column_data{credit} = "<td align=right>$ref->{credit}</td>"; + $column_data{debit} = "$ref->{debit}"; + $column_data{credit} = "$ref->{credit}"; $column_data{accno} = -"<td><a href=$href&accno=$ref->{accno}&callback=$callback>$ref->{accno}</a> $ref->{accname}</td>"; +"<a href=$href&accno=$ref->{accno}&callback=$callback>$ref->{accno}</a> $ref->{accname}"; $column_data{gifi_accno} = -"<td><a href=$href&gifi_accno=$ref->{gifi_accno}&callback=$callback>$ref->{gifi_accno}</a> </td>"; - $column_data{balance} = "<td align=right>" - . $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, - 2, 0 ) - . "</td>"; +"<a href=$href&gifi_accno=$ref->{gifi_accno}&callback=$callback>$ref->{gifi_accno}</a> "; + $column_data{balance} = $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, + 2, 0 ); $column_data{cleared} = - ( $ref->{cleared} ) ? "<td>*</td>" : "<td> </td>"; + ( $ref->{cleared} ) ? "*" : " "; if ( $ref->{id} != $sameid ) { $i++; $i %= 2; } - print " - <tr class=listrow$i>"; - for (@column_index) { print "$column_data{$_}\n" } - print "</tr>"; + $column_data{'i'} = $i; + push @rows, \%column_data; $sameid = $ref->{id}; } - &gl_subtotal if ( $form->{l_subtotal} eq 'Y' ); - - for (@column_index) { $column_data{$_} = "<td> </td>" } + push @rows, &gl_subtotal_tt() if ( $form->{l_subtotal} eq 'Y' ); - $column_data{debit} = - "<th align=right class=listtotal>" - . $form->format_amount( \%myconfig, $totaldebit, 2, " " ) . "</th>"; - $column_data{credit} = - "<th align=right class=listtotal>" - . $form->format_amount( \%myconfig, $totalcredit, 2, " " ) . "</th>"; - $column_data{balance} = - "<th align=right class=listtotal>" - . $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, 2, 0 ) - . "</th>"; + for (@column_index) { $column_data{$_} = " " } - print qq| - <tr class=listtotal> -|; - - for (@column_index) { print "$column_data{$_}\n" } + $column_data{debit} = $form->format_amount( \%myconfig, $totaldebit, 2, " " ); + $column_data{credit} = $form->format_amount( \%myconfig, $totalcredit, 2, " " ); + $column_data{balance} = $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, 2, 0 ); $i = 1; if ( $myconfig{acs} !~ /General Ledger--General Ledger/ ) { @@ -819,40 +772,70 @@ qq|<button class="submit" type="submit" name="action" value="vendor_invoice_">| delete $button{$item}; } - print qq| - </tr> - </table> - </td> - </tr> - <tr> - <td><hr size=3 noshade></td> - </tr> -</table> + my @buttons; + foreach $item ( sort { $a->{order} <=> $b->{order} } %button ) { + push @buttons, $item->{code}; + } -<br> +##SC: Taking this out for now... +## if ( $form->{lynx} ) { +## require "bin/menu.pl"; +## &menubar; +## } + + my $template; + if ($form->{action} eq 'csv_gl_report') { + $template = LedgerSMB::Template->new( + user => \%myconfig, + locale => $locale, + path => 'UI', + template => 'gl-report', + format => 'CSV' + ); + } else { + $template = LedgerSMB::Template->new( + user => \%myconfig, + locale => $locale, + path => 'UI', + template => 'gl-report', + format => 'HTML', + no_escape => 1 + ); + } + $template->render({ + form => $form, + buttons => \@buttons, + options => $option, + columns => \@column_index, + heading => \%column_header, + rows => \@rows, + totals => \%column_data + }); -<form method=post action=$form->{script}> -|; +} - $form->hide_form(qw(callback path login sessionid)); +sub csv_gl_report { &generate_report } - foreach $item ( sort { $a->{order} <=> $b->{order} } %button ) { - print $item->{code}; - } +sub gl_subtotal_tt { - if ( $form->{lynx} ) { - require "bin/menu.pl"; - &menubar; - } + my %column_data; + $subtotaldebit = + $form->format_amount( \%myconfig, $subtotaldebit, 2, " " ); + $subtotalcredit = + $form->format_amount( \%myconfig, $subtotalcredit, 2, " " ); - print qq| + for (@column_index) { $column_data{$_} = " " } + $column_data{'is_subtotal'} = 1; -</form> + $column_data{debit} = $subtotaldebit; + $column_data{credit} = $subtotalcredit; -</body> -</html> -|; + $subtotaldebit = 0; + $subtotalcredit = 0; + + $sameitem = $ref->{ $form->{sort} }; + return \%column_data; } sub gl_subtotal { |