[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb: [1555] trunk



Revision: 1555
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=1555&view=rev
Author:   tetragon
Date:     2007-09-10 14:13:14 -0700 (Mon, 10 Sep 2007)

Log Message:
-----------
Initial templating of the GL report.
Adding CSV output

Modified Paths:
--------------
    trunk/LedgerSMB/Template.pm
    trunk/bin/gl.pl

Added Paths:
-----------
    trunk/LedgerSMB/Template/CSV.pm
    trunk/UI/gl-report.csv
    trunk/UI/gl-report.html

Added: trunk/LedgerSMB/Template/CSV.pm
===================================================================
--- trunk/LedgerSMB/Template/CSV.pm	                        (rev 0)
+++ trunk/LedgerSMB/Template/CSV.pm	2007-09-10 21:13:14 UTC (rev 1555)
@@ -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;


Property changes on: trunk/LedgerSMB/Template/CSV.pm
___________________________________________________________________
Name: svn:executable
   + *

Modified: trunk/LedgerSMB/Template.pm
===================================================================
--- trunk/LedgerSMB/Template.pm	2007-09-10 21:01:06 UTC (rev 1554)
+++ trunk/LedgerSMB/Template.pm	2007-09-10 21:13:14 UTC (rev 1555)
@@ -11,7 +11,7 @@
 
 =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 @@
 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 @@
 	$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 @@
 		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(@_)};

Added: trunk/UI/gl-report.csv
===================================================================
--- trunk/UI/gl-report.csv	                        (rev 0)
+++ trunk/UI/gl-report.csv	2007-09-10 21:13:14 UTC (rev 1555)
@@ -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 ?>

Added: trunk/UI/gl-report.html
===================================================================
--- trunk/UI/gl-report.html	                        (rev 0)
+++ trunk/UI/gl-report.html	2007-09-10 21:13:14 UTC (rev 1555)
@@ -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>

Modified: trunk/bin/gl.pl
===================================================================
--- trunk/bin/gl.pl	2007-09-10 21:01:06 UTC (rev 1554)
+++ trunk/bin/gl.pl	2007-09-10 21:13:14 UTC (rev 1555)
@@ -47,6 +47,7 @@
 
 use LedgerSMB::GL;
 use LedgerSMB::PE;
+use LedgerSMB::Template;
 
 require "bin/arap.pl";
 
@@ -640,31 +641,6 @@
     $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 @@
         $cml = -1 if $form->{contra};
     }
 
+    my @rows;
     if ( ( $form->{accno} || $form->{gifi_accno} ) && $form->{balance} ) {
+        my %column_data;
 
-        for (@column_index) { $column_data{$_} = "<td>&nbsp;</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 @@
     $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,63 +695,46 @@
         $ref->{credit} =
           $form->format_amount( \%myconfig, $ref->{credit}, 2, "&nbsp;" );
 
-        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->{$_}&nbsp;</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>&nbsp;</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>&nbsp;</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' );
+    push @rows, &gl_subtotal_tt() if ( $form->{l_subtotal} eq 'Y' );
 
-    for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
+    for (@column_index) { $column_data{$_} = " " }
 
-    $column_data{debit} =
-      "<th align=right class=listtotal>"
-      . $form->format_amount( \%myconfig, $totaldebit, 2, "&nbsp;" ) . "</th>";
-    $column_data{credit} =
-      "<th align=right class=listtotal>"
-      . $form->format_amount( \%myconfig, $totalcredit, 2, "&nbsp;" ) . "</th>";
-    $column_data{balance} =
-        "<th align=right class=listtotal>"
-      . $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, 2, 0 )
-      . "</th>";
+    $column_data{debit} = $form->format_amount( \%myconfig, $totaldebit, 2, "&nbsp;" );
+    $column_data{credit} = $form->format_amount( \%myconfig, $totalcredit, 2, "&nbsp;" );
+    $column_data{balance} = $form->format_amount( \%myconfig, $form->{balance} * $ml * $cml, 2, 0 );
 
-    print qq|
-	<tr class=listtotal>
-|;
-
-    for (@column_index) { print "$column_data{$_}\n" }
-
     $i = 1;
     if ( $myconfig{acs} !~ /General Ledger--General Ledger/ ) {
         $button{'General Ledger--Add Transaction'}{code} =
@@ -819,40 +772,70 @@
         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;
+##    }
 
-<form method=post action=$form->{script}>
-|;
+    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 => ..hidden..,
+        options => $option,
+        columns => ..hidden..,
+        heading => \%column_header,
+        rows => ..hidden..,
+        totals => \%column_data
+    });
 
-    $form->hide_form(qw(callback path login sessionid));
+}
 
-    foreach $item ( sort { $a->{order} <=> $b->{order} } %button ) {
-        print $item->{code};
-    }
+sub csv_gl_report { &generate_report }
 
-    if ( $form->{lynx} ) {
-        require "bin/menu.pl";
-        &menubar;
-    }
+sub gl_subtotal_tt {
 
-    print qq|
+    my %column_data;
+    $subtotaldebit =
+      $form->format_amount( \%myconfig, $subtotaldebit, 2, "&nbsp;" );
+    $subtotalcredit =
+      $form->format_amount( \%myconfig, $subtotalcredit, 2, "&nbsp;" );
 
-</form>
+    for (@column_index) { $column_data{$_} = " " }
+    $column_data{'is_subtotal'} = 1;
 
-</body>
-</html>
-|;
+    $column_data{debit} = $subtotaldebit;
+    $column_data{credit} = $subtotalcredit;
 
+    $subtotaldebit  = 0;
+    $subtotalcredit = 0;
+
+    $sameitem = $ref->{ $form->{sort} };
+
+    return \%column_data;
 }
 
 sub gl_subtotal {


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.