bp::list_spool auf template umgestellt.
[kivitendo-erp.git] / bin / mozilla / bp.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 2003
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # Batch printing
31 #
32 #======================================================================
33
34 use SL::BP;
35 use Data::Dumper;
36 use List::Util qw(first);
37
38 1;
39
40 require "bin/mozilla/common.pl";
41
42 use strict;
43
44 # end of main
45
46 sub assert_bp_access {
47   my $form     = $main::form;
48
49   my %access_map = (
50     'invoice'           => 'invoice_edit',
51     'sales_order'       => 'sales_order_edit',
52     'sales_quotation'   => 'sales_quotation_edit',
53     'purchase_order'    => 'purchase_order_edit',
54     'request_quotation' => 'request_quotation_edit',
55     'check'             => 'cash',
56     'receipt'           => 'cash',
57   );
58
59   if ($form->{type} && $access_map{$form->{type}}) {
60     $main::auth->assert($access_map{$form->{type}});
61
62   } else {
63     $main::auth->assert('DOES_NOT_EXIST');
64   }
65 }
66
67 sub search {
68   $::lxdebug->enter_sub;
69
70   assert_bp_access();
71
72   # setup customer/vendor selection
73   BP->get_vc(\%::myconfig, $::form);
74
75   my %label = (
76        invoice           => { title => $::locale->text('Sales Invoices'),  invnumber => 1, ordnumber => 1 },
77        sales_order       => { title => $::locale->text('Sales Orders'),    ordnumber => 1, },
78        purchase_order    => { title => $::locale->text('Purchase Orders'), ordnumber => 1, },
79        sales_quotation   => { title => $::locale->text('Quotations'),      quonumber => 1, },
80        request_quotation => { title => $::locale->text('RFQs'),            quonumber => 1, },
81        check             => { title => $::locale->text('Checks'),          chknumber => 1, },
82        receipt           => { title => $::locale->text('Receipts'),        rctnumber => 1, },
83   );
84
85   my $bp_accounts = $::form->{type} =~ /check|receipt/
86                  && BP->payment_accounts(\%::myconfig, $::form);
87
88   $::form->header;
89   print $::form->parse_html_template('bp/search', {
90     label         => \%label,
91     show_accounts => $bp_accounts,
92     account_sub   => sub { ("$_[0]{accno}--$_[0]{description}")x2 },
93     vc_keys       => sub { "$_[0]{name}--$_[0]{id}" },
94   });
95
96   $::lxdebug->leave_sub;
97 }
98
99 sub remove {
100   $main::lxdebug->enter_sub();
101
102   my $form     = $main::form;
103   my $locale   = $main::locale;
104
105   assert_bp_access();
106
107   my $selected = 0;
108
109   for my $i (1 .. $form->{rowcount}) {
110     if ($form->{"checked_$i"}) {
111       $selected = 1;
112       last;
113     }
114   }
115
116   $form->error('Nothing selected!') unless $selected;
117
118   $form->{title} = $locale->text('Confirm!');
119
120   $form->header;
121
122   print qq|
123 <body>
124
125 <form method=post action=bp.pl>
126 |;
127
128   map { delete $form->{$_} } qw(action header);
129
130   foreach my $key (keys %$form) {
131     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
132     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
133   }
134
135   print qq|
136 <h2 class=confirm>$form->{title}</h2>
137
138 <h4>|
139     . $locale->text(
140           'Are you sure you want to remove the marked entries from the queue?')
141     . qq|</h4>
142
143 <input name=action class=submit type=submit value="|
144     . $locale->text('Yes') . qq|">
145 </form>
146
147 </body>
148 </html>
149 |;
150
151   $main::lxdebug->leave_sub();
152 }
153
154 sub yes {
155   $main::lxdebug->enter_sub();
156
157   my $form     = $main::form;
158   my %myconfig = %main::myconfig;
159   my $locale   = $main::locale;
160
161   assert_bp_access();
162
163   $form->info($locale->text('Removing marked entries from queue ...'));
164   $form->{callback} .= "&header=1" if $form->{callback};
165
166   $form->redirect($locale->text('Removed spoolfiles!'))
167     if (BP->delete_spool(\%myconfig, \%$form));
168   $form->error($locale->text('Cannot remove files!'));
169
170   $main::lxdebug->leave_sub();
171 }
172
173 sub print {
174   $main::lxdebug->enter_sub();
175
176   my $form     = $main::form;
177   my %myconfig = %main::myconfig;
178   my $locale   = $main::locale;
179
180   assert_bp_access();
181
182   $form->get_lists(printers => 'ALL_PRINTERS');
183   # use the command stored in the databse or fall back to $myconfig{printer}
184   my $selected_printer =  first { $_ } map ({ $_ ->{printer_command} }
185                                          grep { $_->{id} eq $form->{printer} }
186                                            @{ $form->{ALL_PRINTERS} }),
187                                        $myconfig{printer};
188
189   if ($form->{callback}) {
190     map { $form->{callback} .= "&checked_$_=1" if $form->{"checked_$_"} }
191       (1 .. $form->{rowcount});
192     $form->{callback} .= "&header=1";
193   }
194
195   for my $i (1 .. $form->{rowcount}) {
196     if ($form->{"checked_$i"}) {
197       $form->info($locale->text('Printing ... '));
198
199       if (BP->print_spool(\%myconfig, \%$form, "| $selected_printer")) {
200         print $locale->text('done');
201         $form->redirect($locale->text('Marked entries printed!'));
202       }
203       ::end_of_request();
204     }
205   }
206
207   $form->error('Nothing selected!');
208
209   $main::lxdebug->leave_sub();
210 }
211
212 sub list_spool {
213   $::lxdebug->enter_sub;
214   assert_bp_access();
215
216   # parse old vc picker
217   $::form->{ $::form->{vc} } = $::form->unescape($::form->{ $::form->{vc} });
218   ($::form->{ $::form->{vc} }, $::form->{"$::form->{vc}_id"}) = split(/--/, $::form->{ $::form->{vc} });
219
220   BP->get_spoolfiles(\%::myconfig, $::form);
221
222   my @href_options = ('vc', 'type', 'title', $::form->{vc});
223
224   my %option_texts = (
225     customer      => sub { $::locale->text('Customer')         . " : $::form->{customer}" },
226     vendor        => sub { $::locale->text('Customer')         . " : $::form->{vendor}" },
227     account       => sub { $::locale->text('Account')          . " : $::form->{account}" },
228     invnumber     => sub { $::locale->text('Invoice Number')   . " : $::form->{invnumber}" },
229     ordnumber     => sub { $::locale->text('Order Number')     . " : $::form->{ordnumber}" },
230     quonumber     => sub { $::locale->text('Quotation Number') . " : $::form->{quonumber}" },
231     transdatefrom => sub { $::locale->text('From') . "&nbsp;" . $::locale->date(\%::myconfig, $::form->{transdatefrom}, 1) },
232     transdateto   => sub { $::locale->text('To')   . "&nbsp;" . $::locale->date(\%::myconfig, $::form->{transdateto}, 1) },
233   );
234
235   my @options;
236   for my $key ($::form->{vc}, qw(account invnumber ordnumber quonumber transdatefrom transdateto)) {
237     next unless $::form->{$key};
238     push @href_options, $key;
239     push @options, $option_texts{$key} ? $option_texts{$key}->() : '';
240   }
241
242   my $last_spoolfile;
243   for my $ref (@{ $::form->{SPOOL} }) {
244     $ref->{module}   = ($ref->{module} eq 'ar') ? "is" : "ir" if $ref->{invoice};
245     $ref->{new_file} = $last_spoolfile ne $ref->{spoolfile};
246   } continue {
247     $last_spoolfile = $ref->{spoolfile};
248   }
249
250   $::form->get_lists(printers => "ALL_PRINTERS");
251
252   $::form->header;
253   print $::form->parse_html_template('bp/list_spool', {
254      spool        => $::lx_office_conf{paths}->{spool},
255      href         => build_std_url('bp.pl', @href_options),
256      is_invoice   => scalar ($::form->{type} =~ /^invoice$/),
257      is_order     => scalar ($::form->{type} =~ /_order$/),
258      is_quotation => scalar ($::form->{type} =~ /_quotation$/),
259      options      => \@options,
260   });
261
262   $::lxdebug->leave_sub;
263 }
264
265 sub select_all {
266   $main::lxdebug->enter_sub();
267
268   my $form     = $main::form;
269
270   assert_bp_access();
271
272   map { $form->{"checked_$_"} = 1 } (1 .. $form->{rowcount});
273   &list_spool;
274
275   $main::lxdebug->leave_sub();
276 }
277
278 sub continue { call_sub($main::form->{"nextsub"}); }
279