From 59b539628096d57bc8803682bb9990939d5e9178 Mon Sep 17 00:00:00 2001 From: tetragon Date: Mon, 10 Sep 2007 21:13:14 +0000 Subject: 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 --- LedgerSMB/Template.pm | 11 ++- LedgerSMB/Template/CSV.pm | 106 ++++++++++++++++++++++++++ UI/gl-report.csv | 3 + UI/gl-report.html | 92 +++++++++++++++++++++++ bin/gl.pl | 185 +++++++++++++++++++++------------------------- 5 files changed, 294 insertions(+), 103 deletions(-) create mode 100755 LedgerSMB/Template/CSV.pm create mode 100644 UI/gl-report.csv create mode 100644 UI/gl-report.html 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(' 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 @@ +~ +~ +~ 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 @@ + + + + <?lsmb form.titlebar ?> + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+

+ +
+ +
+ + + + + + + + + + + +
+ + + diff --git a/bin/gl.pl b/bin/gl.pl index 89a4cf88..16871f25 100644 --- a/bin/gl.pl +++ b/bin/gl.pl @@ -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} = "" . $locale->text('Balance') . ""; $column_header{cleared} = qq|| . $locale->text('R') . qq||; - $form->header; - - print qq| - - - - - - - - - - - - - - - - -
$form->{title}
$option
- - -|; - - for (@column_index) { print "$column_header{$_}\n" } - - print " - -"; - # 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{$_} = "" } - $column_data{balance} = ""; + for (@column_index) { $column_data{$_} = " " } + $column_data{balance} = + $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, + 2, 0 ); - $i++; - $i %= 2; - print qq| - -|; - for (@column_index) { print "$column_data{$_}\n" } - - print qq| - -|; + $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{$_} = "" } + for (qw(id transdate)) { $column_data{$_} = "$ref->{$_}" } $column_data{reference} = -""; +"{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/
/g; for (qw(description source memo notes department)) { - $column_data{$_} = "
"; + $column_data{$_} = "$ref->{$_} "; } - $column_data{debit} = ""; - $column_data{credit} = ""; + $column_data{debit} = "$ref->{debit}"; + $column_data{credit} = "$ref->{credit}"; $column_data{accno} = -""; +"{accno}&callback=$callback>$ref->{accno} $ref->{accname}"; $column_data{gifi_accno} = -""; - $column_data{balance} = ""; +"{gifi_accno}&callback=$callback>$ref->{gifi_accno} "; + $column_data{balance} = $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, + 2, 0 ); $column_data{cleared} = - ( $ref->{cleared} ) ? "" : ""; + ( $ref->{cleared} ) ? "*" : " "; if ( $ref->{id} != $sameid ) { $i++; $i %= 2; } - print " - "; - for (@column_index) { print "$column_data{$_}\n" } - print ""; + $column_data{'i'} = $i; + push @rows, \%column_data; $sameid = $ref->{id}; } - &gl_subtotal if ( $form->{l_subtotal} eq 'Y' ); - - for (@column_index) { $column_data{$_} = "" } + push @rows, &gl_subtotal_tt() if ( $form->{l_subtotal} eq 'Y' ); - $column_data{debit} = - ""; - $column_data{credit} = - ""; - $column_data{balance} = - ""; + for (@column_index) { $column_data{$_} = " " } - print qq| - -|; - - 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| -
 " - . $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, - 2, 0 ) - . "
$ref->{$_}{module}.pl?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback>$ref->{reference}$ref->{$_} $ref->{debit}$ref->{credit}{accno}&callback=$callback>$ref->{accno} $ref->{accname}{gifi_accno}&callback=$callback>$ref->{gifi_accno} " - . $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, - 2, 0 ) - . "* 
 " - . $form->format_amount( \%myconfig, $totaldebit, 2, " " ) . "" - . $form->format_amount( \%myconfig, $totalcredit, 2, " " ) . "" - . $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, 2, 0 ) - . "
-

+ my @buttons; + foreach $item ( sort { $a->{order} <=> $b->{order} } %button ) { + push @buttons, $item->{code}; + } -
+##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 + }); -
{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; -
+ $column_data{debit} = $subtotaldebit; + $column_data{credit} = $subtotalcredit; - - -|; + $subtotaldebit = 0; + $subtotalcredit = 0; + + $sameitem = $ref->{ $form->{sort} }; + return \%column_data; } sub gl_subtotal { -- cgit v1.2.3