b36fcebeeebb3896e7ceb283f3e90119f256ec6d
[kivitendo-erp.git] / SL / Template / Plugin / LxERP.pm
1 package SL::Template::Plugin::LxERP;
2
3 use base qw( Template::Plugin );
4 use Template::Plugin;
5
6 use List::Util qw(min);
7
8 use SL::AM;
9
10 use strict;
11
12 sub new {
13   my $class   = shift;
14   my $context = shift;
15
16   bless { }, $class;
17 }
18
19 sub format_amount {
20   my ($self, $var, $places, $skip_zero, $dash) = @_;
21
22   return $main::form->format_amount(\%main::myconfig, $var * 1, $places, $dash) unless $skip_zero && $var == 0;
23   return '';
24 }
25
26 sub round_amount {
27   my ($self, $var, $places, $skip_zero) = @_;
28
29   return $main::form->round_amount($var * 1, $places) unless $skip_zero && $var == 0;
30   return '';
31 }
32
33 sub format_amount_units {
34   my ($self, $amount, $amount_unit, $part_unit) = @_;
35
36   return $main::form->format_amount_units('amount'      => $amount,
37                                           'part_unit'   => $part_unit,
38                                           'amount_unit' => $amount_unit,
39                                           'conv_units'  => 'convertible_not_smaller');
40 }
41
42 sub format_percent {
43   my ($self, $var, $places, $skip_zero) = @_;
44
45   $places ||= 2;
46
47   return $self->format_amount($var * 100, $places, $skip_zero);
48 }
49
50 sub escape_br {
51   my ($self, $var) = @_;
52
53   $var =~ s/\r//g;
54   $var =~ s/\n/<br>/g;
55
56   return $var;
57 }
58
59 sub format_string {
60   my $self   = shift;
61   my $string = shift;
62
63   return $main::form->format_string($string, @_);
64 }
65
66 sub numtextrows {
67   my $self = shift;
68
69   return $main::form->numtextrows(@_);
70 }
71
72 sub _turn90_word {
73   my $self = shift;
74   my $word = shift || "";
75
76   return join '<br>', map { $main::locale->quote_special_chars('HTML', $_) } split(m//, $word);
77 }
78
79 sub turn90 {
80   my $self            = shift;
81   my $word            = shift;
82   my $args            = shift;
83
84   $args             ||= { };
85   $word             ||= "";
86
87   $args->{split_by} ||= 'chars';
88   $args->{class}      = " class=\"$args->{class}\"" if ($args->{class});
89
90   if ($args->{split_by} eq 'words') {
91     my @words = split m/\s+/, $word;
92
93     if (1 >= scalar @words) {
94       return $self->_turn90_word($words[0]);
95     }
96
97     return qq|<table><tr>| . join('', map { '<td valign="bottom"' . $args->{class} . '>' . $self->_turn90_word($_) . '</td>' } @words) . qq|</tr></table>|;
98
99   } else {
100     return $self->_turn90_word($word);
101   }
102 }
103
104 sub abs {
105   my $self = shift;
106   my $var  = shift;
107
108   return $var < 0 ? $var * -1 : $var;
109 }
110
111 sub t8 {
112   my ($self, $text, @args) = @_;
113   $self->{locale} ||= Locale->new($::myconfig{countrycode}, 'all');
114   return $self->{locale}->text($text, @args) || $text;
115 }
116
117 1;