PrintOptions_Helper: Angabe eines prefix ermöglichen.
[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
8 # generate the printing options displayed at the bottom of oe and is forms.
9 # this function will attempt to guess what type of form is displayed, and will generate according options
10 #
11 # about the coding:
12 # this version builds the arrays of options pretty directly. if you have trouble understanding how,
13 # the opthash function builds hashrefs which are then pieced together for the template arrays.
14 # unneeded options are "undef"ed out, and then grepped out.
15 #
16 # the inline options is untested, but intended to be used later in metatemplating
17 sub get_print_options {
18   my ($class, %params) = @_;
19
20   my $form     = $params{form}     || $::form;
21   my $myconfig = $params{myconfig} || \%::myconfig;
22   my $locale   = $params{locale}   || $::locale;
23   my $options  = $params{options};
24
25   my $prefix = $options->{dialog_name_prefix} || '';
26
27   # names 3 parameters and returns a hashref, for use in templates
28   sub opthash { +{ value => shift, selected => shift, oname => shift } }
29   my (@FORMNAME, @LANGUAGE_ID, @FORMAT, @SENDMODE, @MEDIA, @PRINTER_ID, @SELECTS) = ();
30
31   # note: "||"-selection is only correct for values where "0" is _not_ a correct entry
32   $form->{sendmode}   = "attachment";
33   $form->{format}     = $form->{format} || $myconfig->{template_format} || "pdf";
34   $form->{copies}     = $form->{copies} || $myconfig->{copies}          || 3;
35   $form->{media}      = $form->{media}  || $myconfig->{default_media}   || "screen";
36   $form->{printer_id} = defined $form->{printer_id}           ? $form->{printer_id} :
37                         defined $myconfig->{default_printer_id} ? $myconfig->{default_printer_id} : "";
38
39   $form->{PD}{ $form->{formname} } = "selected";
40   $form->{DF}{ $form->{format} }   = "selected";
41   $form->{OP}{ $form->{media} }    = "selected";
42   $form->{SM}{ $form->{sendmode} } = "selected";
43
44   push @FORMNAME, grep $_,
45     ($form->{type} eq 'purchase_order') ? (
46       opthash("purchase_order",      $form->{PD}{purchase_order},      $locale->text('Purchase Order')),
47       opthash("bin_list",            $form->{PD}{bin_list},            $locale->text('Bin List'))
48     ) : undef,
49     ($form->{type} eq 'credit_note') ?
50       opthash("credit_note",         $form->{PD}{credit_note},         $locale->text('Credit Note')) : undef,
51     ($form->{type} eq 'sales_order') ? (
52       opthash("sales_order",         $form->{PD}{sales_order},         $locale->text('Confirmation')),
53       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
54     ) : undef,
55     ($form->{type} =~ /sales_quotation$/) ?
56       opthash('sales_quotation',     $form->{PD}{sales_quotation},     $locale->text('Quotation')) : undef,
57     ($form->{type} =~ /request_quotation$/) ?
58       opthash('request_quotation',   $form->{PD}{request_quotation},   $locale->text('Request for Quotation')) : undef,
59     ($form->{type} eq 'invoice') ? (
60       opthash("invoice",             $form->{PD}{invoice},             $locale->text('Invoice')),
61       opthash("proforma",            $form->{PD}{proforma},            $locale->text('Proforma Invoice')),
62     ) : undef,
63     ($form->{type} eq 'invoice' && $form->{storno}) ? (
64       opthash("storno_invoice",      $form->{PD}{storno_invoice},      $locale->text('Storno Invoice')),
65     ) : undef,
66     ($form->{type} =~ /_delivery_order$/) ? (
67       opthash($form->{type},         $form->{PD}{$form->{type}},       $locale->text('Delivery Order')),
68       opthash('pick_list',           $form->{PD}{pick_list},           $locale->text('Pick List')),
69     ) : undef;
70
71   push @SENDMODE,
72     opthash("attachment",            $form->{SM}{attachment},          $locale->text('Attachment')),
73     opthash("inline",                $form->{SM}{inline},              $locale->text('In-line'))
74       if ($form->{media} eq 'email');
75
76   my $printable_templates = any { $::lx_office_conf{print_templates}->{$_} } qw(latex opendocument);
77   push @MEDIA, grep $_,
78       opthash("screen",              $form->{OP}{screen},              $locale->text('Screen')),
79     ($printable_templates && $form->{printers} && scalar @{ $form->{printers} }) ?
80       opthash("printer",             $form->{OP}{printer},             $locale->text('Printer')) : undef,
81     ($printable_templates && !$options->{no_queue}) ?
82       opthash("queue",               $form->{OP}{queue},               $locale->text('Queue')) : undef
83         if ($form->{media} ne 'email');
84
85   push @FORMAT, grep $_,
86     ($::lx_office_conf{print_templates}->{opendocument} &&     $::lx_office_conf{applications}->{openofficeorg_writer}  &&     $::lx_office_conf{applications}->{xvfb}
87                                                         && (-x $::lx_office_conf{applications}->{openofficeorg_writer}) && (-x $::lx_office_conf{applications}->{xvfb})
88      && !$options->{no_opendocument_pdf}) ?
89       opthash("opendocument_pdf",    $form->{DF}{"opendocument_pdf"},  $locale->text("PDF (OpenDocument/OASIS)")) : undef,
90     ($::lx_office_conf{print_templates}->{latex}) ?
91       opthash("pdf",                 $form->{DF}{pdf},                 $locale->text('PDF')) : undef,
92     ($::lx_office_conf{print_templates}->{latex} && !$options->{no_postscript}) ?
93       opthash("postscript",          $form->{DF}{postscript},          $locale->text('Postscript')) : undef,
94     (!$options->{no_html}) ?
95       opthash("html", $form->{DF}{html}, "HTML") : undef,
96     ($::lx_office_conf{print_templates}->{opendocument} && !$options->{no_opendocument}) ?
97       opthash("opendocument",        $form->{DF}{opendocument},        $locale->text("OpenDocument/OASIS")) : undef,
98     ($::lx_office_conf{print_templates}->{excel} && !$options->{no_excel}) ?
99       opthash("excel",               $form->{DF}{excel},               $locale->text("Excel")) : undef;
100
101   push @LANGUAGE_ID,
102     map { opthash($_->{id}, ($_->{id} eq $form->{language_id} ? 'selected' : ''), $_->{description}) } +{}, @{ $form->{languages} }
103       if (ref $form->{languages} eq 'ARRAY');
104
105   push @PRINTER_ID,
106     map { opthash($_->{id}, ($_->{id} eq $form->{printer_id} ? 'selected' : ''), $_->{printer_description}) } +{}, @{ $form->{printers} }
107       if ((ref $form->{printers} eq 'ARRAY') && scalar @{ $form->{printers } });
108
109   @SELECTS = map {
110     sname => $_->[1],
111     DATA  => $_->[0],
112     show  => !$options->{"hide_" . $_->[1]} && scalar @{ $_->[0] }
113   },
114   [ \@FORMNAME,    'formname',    ],
115   [ \@LANGUAGE_ID, 'language_id', ],
116   [ \@FORMAT,      'format',      ],
117   [ \@SENDMODE,    'sendmode',    ],
118   [ \@MEDIA,       'media',       ],
119   [ \@PRINTER_ID,  'printer_id',  ];
120
121   my %dont_display_groupitems = (
122     'dunning' => 1,
123     'letter'  => 1,
124     );
125
126   my %template_vars = (
127     name_prefix          => $prefix || '',
128     display_copies       => scalar @{ $form->{printers} || [] } && $::lx_office_conf{print_templates}->{latex} && $form->{media} ne 'email',
129     display_remove_draft => (!$form->{id} && $form->{draft_id}),
130     display_groupitems   => !$dont_display_groupitems{$form->{type}},
131     groupitems_checked   => $form->{groupitems} ? "checked" : '',
132     remove_draft_checked => $form->{remove_draft} ? "checked" : ''
133   );
134
135   return $form->parse_html_template("generic/print_options", { SELECTS  => \@SELECTS, %template_vars } );
136 }
137
138 1;