Anzahlungs-Rg.: eigener Typ for Schlussrechnung (final_invoice (F))
[kivitendo-erp.git] / SL / Helper / PrintOptions.pm
1 package SL::Helper::PrintOptions;
2
3 use strict;
4
5 use List::MoreUtils qw(any);
6
7 sub opthash { +{ value => shift, selected => shift, oname => shift } }
8
9 # generate the printing options displayed at the bottom of oe and is forms.
10 # this function will attempt to guess what type of form is displayed, and will generate according options
11 #
12 # about the coding:
13 # this version builds the arrays of options pretty directly. if you have trouble understanding how,
14 # the opthash function builds hashrefs which are then pieced together for the template arrays.
15 # unneeded options are "undef"ed out, and then grepped out.
16 #
17 # the inline options is untested, but intended to be used later in metatemplating
18 sub get_print_options {
19   my ($class, %params) = @_;
20
21   no warnings 'once';
22
23   my $form     = $params{form}     || $::form;
24   my $myconfig = $params{myconfig} || \%::myconfig;
25   my $locale   = $params{locale}   || $::locale;
26   my $options  = $params{options};
27
28   use warnings 'once';
29
30   my $prefix = $options->{dialog_name_prefix} || '';
31
32   # names 3 parameters and returns a hashref, for use in templates
33   my (@FORMNAME, @LANGUAGE_ID, @FORMAT, @SENDMODE, @MEDIA, @PRINTER_ID, @SELECTS) = ();
34
35   # note: "||"-selection is only correct for values where "0" is _not_ a correct entry
36   $form->{sendmode}   = "attachment";
37   $form->{format}     = $form->{format} || $myconfig->{template_format} || "pdf";
38   $form->{copies}     = $form->{copies} || $myconfig->{copies}          || 3;
39   $form->{media}      = $form->{media}  || $myconfig->{default_media}   || "screen";
40   $form->{printer_id} = defined $form->{printer_id}           ? $form->{printer_id} :
41                         defined $myconfig->{default_printer_id} ? $myconfig->{default_printer_id} : "";
42
43   $form->{PD}{ $form->{formname} } = "selected";
44   $form->{DF}{ $form->{format} }   = "selected";
45   $form->{OP}{ $form->{media} }    = "selected";
46   $form->{SM}{ $form->{sendmode} } = "selected";
47
48   push @FORMNAME, grep $_,
49     ($form->{type} eq 'purchase_order') ? (
50       opthash("purchase_order",      $form->{PD}{purchase_order},      $locale->text('Purchase Order')),
51       opthash("bin_list",            $form->{PD}{bin_list},            $locale->text('Bin List'))
52     ) : undef,
53     ($form->{type} eq 'credit_note') ?
54       opthash("credit_note",         $form->{PD}{credit_note},         $locale->text('Credit Note')) : undef,
55     ($form->{type} eq 'sales_order') ? (
56       opthash("sales_order",         $form->{PD}{sales_order},         $locale->text('Confirmation')),
57       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
58       opthash("ic_supply",           $form->{PD}{ic_supply},            $locale->text('Intra-Community supply')),
59     ) : undef,
60     ($form->{type} =~ /sales_quotation$/) ?
61       opthash('sales_quotation',     $form->{PD}{sales_quotation},     $locale->text('Quotation')) : undef,
62     ($form->{type} =~ /request_quotation$/) ?
63       opthash('request_quotation',   $form->{PD}{request_quotation},   $locale->text('Request for Quotation')) : undef,
64     ($form->{type} eq 'invoice') ? (
65       opthash("invoice",             $form->{PD}{invoice},             $locale->text('Invoice')),
66       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
67       opthash("invoice_copy",        $form->{PD}{invoice_copy},        $locale->text('Invoice Copy')),
68     ) : undef,
69     ($form->{type} eq 'invoice' && $form->{storno}) ? (
70       opthash("storno_invoice",      $form->{PD}{storno_invoice},      $locale->text('Storno Invoice')),
71     ) : undef,
72     ($form->{type} eq 'invoice_for_advance_payment') ? (
73       opthash("invoice_for_advance_payment", $form->{PD}{invoice_for_advance_payment},      $locale->text('Invoice for Advance Payment')),
74     ) : undef,
75     ($form->{type} eq 'final_invoice') ? (
76       opthash("final_invoice", $form->{PD}{final_invoice},             $locale->text('Final Invoice')),
77     ) : undef,
78     ($form->{type} =~ /_delivery_order$/) ? (
79       opthash($form->{type},         $form->{PD}{$form->{type}},       $locale->text('Delivery Order')),
80       opthash('pick_list',           $form->{PD}{pick_list},           $locale->text('Pick List')),
81     ) : undef,
82     ($form->{type} =~ /^letter$/) ? (
83       opthash('letter',              $form->{PD}{letter},              $locale->text('Letter')),
84     ) : undef;
85
86   push @SENDMODE,
87     opthash("attachment",            $form->{SM}{attachment},          $locale->text('Attachment')),
88     opthash("inline",                $form->{SM}{inline},              $locale->text('In-line'))
89       if ($form->{media} eq 'email');
90
91   my $printable_templates = any { $::lx_office_conf{print_templates}->{$_} } qw(latex opendocument);
92   push @MEDIA, grep $_,
93       opthash("screen",              $form->{OP}{screen},              $locale->text('Screen')),
94     ($printable_templates && $form->{printers} && scalar @{ $form->{printers} }) ?
95       opthash("printer",             $form->{OP}{printer},             $locale->text('Printer')) : undef,
96     ($printable_templates && !$options->{no_queue}) ?
97       opthash("queue",               $form->{OP}{queue},               $locale->text('Queue')) : undef
98         if ($form->{media} ne 'email');
99
100   push @FORMAT, grep $_,
101     ($::lx_office_conf{print_templates}->{opendocument} &&     $::lx_office_conf{applications}->{openofficeorg_writer}  &&     $::lx_office_conf{applications}->{xvfb}
102                                                         && (-x $::lx_office_conf{applications}->{openofficeorg_writer}) && (-x $::lx_office_conf{applications}->{xvfb})
103      && !$options->{no_opendocument_pdf}) ?
104       opthash("opendocument_pdf",    $form->{DF}{"opendocument_pdf"},  $locale->text("PDF (OpenDocument/OASIS)")) : undef,
105     ($::lx_office_conf{print_templates}->{latex}) ?
106       opthash("pdf",                 $form->{DF}{pdf},                 $locale->text('PDF')) : undef,
107     ($::lx_office_conf{print_templates}->{latex} && !$options->{no_postscript}) ?
108       opthash("postscript",          $form->{DF}{postscript},          $locale->text('Postscript')) : undef,
109     (!$options->{no_html}) ?
110       opthash("html", $form->{DF}{html}, "HTML") : undef,
111     ($::lx_office_conf{print_templates}->{opendocument} && !$options->{no_opendocument}) ?
112       opthash("opendocument",        $form->{DF}{opendocument},        $locale->text("OpenDocument/OASIS")) : undef,
113     ($::lx_office_conf{print_templates}->{excel} && !$options->{no_excel}) ?
114       opthash("excel",               $form->{DF}{excel},               $locale->text("Excel")) : undef;
115
116   push @LANGUAGE_ID,
117     map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
118       if (ref $form->{languages} eq 'ARRAY');
119
120   push @PRINTER_ID,
121     map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
122       if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
123
124   @SELECTS = map {
125     sname  => $_->[1],
126     DATA   => $_->[0],
127     show   => !$options->{"hide_" . $_->[1]} && scalar @{ $_->[0]},
128     hname  => $locale->text($_->[2])
129   },
130   [ \@FORMNAME,    'formname',    'Formname' ],
131   [ \@LANGUAGE_ID, 'language_id', 'Language' ],
132   [ \@FORMAT,      'format',      'Format'   ],
133   [ \@SENDMODE,    'sendmode',    'Sendmode' ],
134   [ \@MEDIA,       'media',       'Media'    ],
135   [ \@PRINTER_ID,  'printer_id',  'Printer'  ];
136
137   my %dont_display_groupitems = (
138     'dunning' => 1,
139     );
140
141   my %template_vars = (
142     name_prefix          => $prefix || '',
143     show_headers         => $options->{show_headers},
144     display_copies       => scalar @{ $form->{printers} || [] } && $::lx_office_conf{print_templates}->{latex} && $form->{media} ne 'email',
145     display_remove_draft => (!$form->{id} && $form->{draft_id}),
146     display_groupitems   => !$dont_display_groupitems{$form->{type}},
147     display_bothsided    => $options->{show_bothsided},
148     groupitems_checked   => $form->{groupitems} ? "checked" : '',
149     bothsided_checked    => $form->{bothsided}  ? "checked" : '',
150     remove_draft_checked => $form->{remove_draft} ? "checked" : ''
151   );
152
153   return $form->parse_html_template("generic/print_options", { SELECTS  => \@SELECTS, %template_vars } );
154 }
155
156 1;
157
158 __END__
159
160 =pod
161
162 =encoding utf8
163
164 =head1 NAME
165
166 SL::Helper::PrintOptions - A helper for generating the print options for
167 templates
168
169 =head1 SYNOPSIS
170
171   # render your template with print_options
172   $self->render('letter/edit',
173     %params,
174     letter        => $letter,
175     print_options => SL::Helper::PrintOptions->get_print_options (
176       options => { no_postscript   => 1,
177                    no_opendocument => 1,
178                    no_html         => 1,
179                    no_queue        => 1 }),
180
181   );
182
183 Then, in the template, you can render the options with
184     C<[% print_options %]>. Look at the template
185     C<generic/print_options> to see, which variables you get back.
186
187 =head1 FUNCTIONS
188
189 =over 4
190
191 =item C<get_print_options %params>
192
193 Parses the template C<generic/print_options>. It does some guessings
194     and settings according to the params, (namely C<form>).
195
196
197 The recognized parameters are:
198
199 =over 2
200
201 =item * C<form>: defaults to $::form if not given. There are several
202     keys in C<form> which control the output of the options,
203     e.g. C<format>, C<media>, C<copies>, C<printers>, C<printer_id>,
204     C<type>, C<formname>, ...
205
206 =item * C<myconfig>: defaults to %::myconfig
207
208 =item * C<locale>: defaults to $::locale
209
210 =item * C<options>: Options can be:
211
212 * C<dialog_name_prefix>: a string prefixed to the template
213     variables. E.g. if prefix is C<mypref_> the value for copies
214     returned from the user is in $::form->{mypref_copies}
215
216 * C<show_header>: render headings for the input elements
217
218 * C<no_queue>: if set, do not show option for printing to queue
219
220 * C<no_opendocument>: if set, do not show option for printing
221     opendocument format
222
223 * C<no_postscript>: if set, do not show option for printing
224     postscript format
225
226 * C<no_html>: if set, do not show option for printing
227     html format
228
229 * C<no_opendocument_pdf>
230
231 * C<no_excel>
232
233 * and some more
234
235 =back
236
237 =back
238
239 =head1 AUTHOR
240
241 ?
242
243 Bernd Bleßmann E<lt>bernd@kivitendo-premium.deE<gt> (I just moved
244     it from io.pl to here and did some minor changes)
245
246 =head1 BUGS
247
248 incomplete documentation
249
250 =cut