Merge branch 'master' of vc.linet-services.de:public/lx-office-erp
authorSven Schöling <s.schoeling@linet-services.de>
Wed, 12 Oct 2011 15:01:16 +0000 (17:01 +0200)
committerSven Schöling <s.schoeling@linet-services.de>
Wed, 12 Oct 2011 15:01:16 +0000 (17:01 +0200)
SL/CVar.pm
SL/DO.pm
SL/IS.pm
SL/OE.pm
SL/Template/Simple.pm
t/helper/template/simple.t [new file with mode: 0644]

index 2408222..bd58cbd 100644 (file)
@@ -716,6 +716,16 @@ sub parse {
   return $value;
 }
 
+sub format_to_template {
+  my ($self, $value, $config) = @_;
+  # stupid template expects everything formated. except objects
+  # do not use outside of print routines for legacy templates
+
+  return $::form->parse_amount(\%::myconfig, $value) if $config->{type} eq 'number';
+  return $value->to_lxoffice if $config->{type} eq 'date' && blessed $value && $value->can('to_lxoffice');
+  return $value;
+}
+
 1;
 
 __END__
index f6c56b6..17d896f 100644 (file)
--- a/SL/DO.pm
+++ b/SL/DO.pm
@@ -888,7 +888,9 @@ sub order_details {
       }
     }
 
-    map { push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} }, $form->{"ic_cvar_$_->{name}_$i"} } @{ $ic_cvar_configs };
+    push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
+      CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
+        for @{ $ic_cvar_configs };
   }
 
   $h_pg->finish();
index d90a0d0..c9a4428 100644 (file)
--- a/SL/IS.pm
+++ b/SL/IS.pm
@@ -353,7 +353,9 @@ sub invoice_details {
         $sth->finish;
       }
 
-      map { push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} }, $form->{"ic_cvar_$_->{name}_$i"} } @{ $ic_cvar_configs };
+      push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
+        CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
+          for @{ $ic_cvar_configs };
     }
   }
 
@@ -877,7 +879,7 @@ sub post_invoice {
   # record payments and offsetting AR
   if (!$form->{storno}) {
     for my $i (1 .. $form->{paidaccounts}) {
-      
+
       if ($form->{"acc_trans_id_$i"}
           && $payments_only
           && ($::lx_office_conf{features}->{payments_changeable} == 0)) {
index 5bf2a3d..bfa43f2 100644 (file)
--- a/SL/OE.pm
+++ b/SL/OE.pm
@@ -1296,7 +1296,9 @@ sub order_details {
         $sth->finish;
       }
 
-      map { push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} }, $form->{"ic_cvar_$_->{name}_$i"} } @{ $ic_cvar_configs };
+      push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
+        CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
+          for @{ $ic_cvar_configs };
     }
   }
 
index 8ad6220..9960908 100644 (file)
@@ -85,27 +85,15 @@ sub uses_temp_file {
 }
 
 sub _get_loop_variable {
-  my $self      = shift;
-  my $var       = shift;
-  my $get_array = shift;
-  my @indices   = @_;
-
+  my ($self, $var, $get_array, @indices) = @_;
   my $form      = $self->{form};
-  my $value;
+  my ($value, @methods);
 
   if ($var =~ m/\./) {
-    $value = $form;
-    for my $part (split(m/\./, $var)) {
-      if (ref($value) =~ m/^(?:Form|HASH)$/) {
-        $value = $value->{$part};
-      } elsif (blessed($value) && $value->can($part)) {
-        $value = $value->$part;
-      } else {
-        $value = '';
-        last;
-      }
-    }
-  } elsif (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
+    ($var, @methods) = split m/\./, $var;
+  }
+
+  if (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
     $value = $form->{TEMPLATE_ARRAYS}->{$var};
   } else {
     $value = $form->{$var};
@@ -116,6 +104,17 @@ sub _get_loop_variable {
     $value = $value->[$indices[$i]];
   }
 
+  for my $part (@methods) {
+    if (ref($value) =~ m/^(?:Form|HASH)$/) {
+      $value = $value->{$part};
+    } elsif (blessed($value) && $value->can($part)) {
+      $value = $value->$part;
+    } else {
+      $value = '';
+      last;
+    }
+  }
+
   return $value;
 }
 
diff --git a/t/helper/template/simple.t b/t/helper/template/simple.t
new file mode 100644 (file)
index 0000000..056fb8b
--- /dev/null
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+
+use strict;
+use lib 't';
+
+use DateTime;
+use Test::More;
+
+use_ok qw(SL::Template::Simple);
+
+my $t = SL::Template::Simple->new(form => {});
+
+sub test ($$$$$) {
+  my ($form, $template_array, $query, $result, $text) = @_;
+
+  $t->{form} = bless $form, 'Form';
+  $t->{form}->{TEMPLATE_ARRAYS} = $template_array  if $template_array;
+  is_deeply $t->_get_loop_variable(@$query), $result, $text;
+}
+
+test { a => 1 }, {}, [ 'a', 0 ], 1, 'simple access';
+test { }, { a => [ 1 ] }, [ 'a', 0, 0 ], 1, 'template access';
+test { }, { a => [ 1..4 ] }, [ 'a', 0, 3 ], 4, 'template access > 1';
+test { }, { a => [ [ 1 ] ] }, [ 'a', 0, 0, 0 ], 1, 'template access more than one layer';
+test { }, { a => [ 1 ] }, [ 'a', 0, 3 ], undef, 'short circuit if array is missing';
+test { a => 2 }, { a => [ 1 ] }, [ 'a', 0 ], 2, 'no template access ignores templates';
+test { a => 2 }, { a => [ 1 ] }, [ 'a', 1 ], [ 1 ], 'array access returns array';
+
+test { a => 2, TEMPLATE_ARRAY => [ a => [1] ] }, undef, [ 'a', 0, 0 ], 2 , 'wrong template_array gets ignored';
+test { a => 2, TEMPLATE_ARRAY => 1 }, undef, [ 'a', 0, 0 ], 2 , 'wrong template_array gets ignored 2';
+
+test { a => { b => 2 }, 'a.b' => 5 }, {}, [ 'a.b', 0 ], 2, 'dot access';
+test { a => { b => { c => 5 } } }, {}, [ 'a.b.c', 0 ], 5, 'deep dot access';
+test { a => { b => 2 } }, {}, [ 'a.b', 1 ], 2, 'dot access ignores array';
+test { a => { b => 2 } }, { 'a.b' => 3 }, [ 'a.b', 0, 0 ], 2, 'dot access ignores template';
+
+{ package LXOTestDummy; sub b { 5 } }
+my $o = bless [], 'LXOTestDummy';
+
+test { 'a.b' => 2, a => $o }, {}, [ 'a.b', 0 ], 5, 'dot object access';
+test { 'a.b.b' => 2, a => { b => $o } }, {}, [ 'a.b.b', 0 ], 5, 'deep dot object access';
+test { 'a.b.b' => 2, a => { b => $o } }, {}, [ 'a.c', 0 ], undef, 'dot hash does not shortcut';
+test { 'a.b.b' => 2, a => { b => $o } }, {}, [ 'a.b.c', 0 ], '', 'dot object shortcuts to empty string';
+
+test {}, { a => [ { b => 2 } ], 'a.b' => 5 },  [ 'a.b', 0, 0 ], 2, 'array dot access';
+test {}, { a => [ { b => { c => 5 } } ] },  [ 'a.b.c', 0, 0 ], 5, 'array deep dot access';
+test {}, { a => [ { b => 2 } ] }, [ 'a.b', 1, 0 ], 2, 'array dot access ignores array';
+test { 'a.b' => 3 }, { a => [ { b => 2 } ] }, , [ 'a.b', 0, 0 ], 2, 'array dot access ignores template';
+
+test {}, { a => [ $o ] },  [ 'a.b', 0, 0 ], 5, 'array dot object access';
+test {}, { a => [ { b => $o } ] }, [ 'a.b.b', 0, 0 ], 5, 'array deep dot object access';
+test {}, { a => [ { b => $o } ] },  [ 'a.c', 0, 0 ], undef, 'array dot hash does not shortcut';
+test {}, { a => [ { b => $o } ] },  [ 'a.b.c', 0, 0 ], '', 'array dot object shortcuts to empty string';
+
+done_testing();