87afc32932ba1f8e5da54d0dd8e37b519b78e35f
[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   $main::lxdebug->enter_sub();
214
215   my $form     = $main::form;
216   my %myconfig = %main::myconfig;
217   my $locale   = $main::locale;
218
219   assert_bp_access();
220
221   $form->{ $form->{vc} } = $form->unescape($form->{ $form->{vc} });
222   ($form->{ $form->{vc} }, $form->{"$form->{vc}_id"}) =
223     split(/--/, $form->{ $form->{vc} });
224
225   BP->get_spoolfiles(\%myconfig, \%$form);
226
227   my $title = $form->escape($form->{title});
228   my $href  = "bp.pl?action=list_spool&vc=$form->{vc}&type=$form->{type}&title=$title";
229
230   $title = $form->escape($form->{title}, 1);
231   my $callback =
232     "bp.pl?action=list_spool&vc=$form->{vc}&type=$form->{type}&title=$title";
233   my $option;
234
235   if ($form->{ $form->{vc} }) {
236     $callback .= "&$form->{vc}=" . $form->escape($form->{ $form->{vc} }, 1);
237     $href .= "&$form->{vc}=" . $form->escape($form->{ $form->{vc} });
238     $option =
239       ($form->{vc} eq 'customer')
240       ? $locale->text('Customer')
241       : $locale->text('Vendor');
242     $option .= " : $form->{$form->{vc}}";
243   }
244   if ($form->{account}) {
245     $callback .= "&account=" . $form->escape($form->{account}, 1);
246     $href .= "&account=" . $form->escape($form->{account});
247     $option .= "\n<br>" if ($option);
248     $option .= $locale->text('Account') . " : $form->{account}";
249   }
250   if ($form->{invnumber}) {
251     $callback .= "&invnumber=" . $form->escape($form->{invnumber}, 1);
252     $href .= "&invnumber=" . $form->escape($form->{invnumber});
253     $option .= "\n<br>" if ($option);
254     $option .= $locale->text('Invoice Number') . " : $form->{invnumber}";
255   }
256   if ($form->{ordnumber}) {
257     $callback .= "&ordnumber=" . $form->escape($form->{ordnumber}, 1);
258     $href .= "&ordnumber=" . $form->escape($form->{ordnumber});
259     $option .= "\n<br>" if ($option);
260     $option .= $locale->text('Order Number') . " : $form->{ordnumber}";
261   }
262   if ($form->{quonumber}) {
263     $callback .= "&quonumber=" . $form->escape($form->{quonumber}, 1);
264     $href .= "&quonumber=" . $form->escape($form->{quonumber});
265     $option .= "\n<br>" if ($option);
266     $option .= $locale->text('Quotation Number') . " : $form->{quonumber}";
267   }
268
269   if ($form->{transdatefrom}) {
270     $callback .= "&transdatefrom=$form->{transdatefrom}";
271     $href     .= "&transdatefrom=$form->{transdatefrom}";
272     $option   .= "\n<br>" if ($option);
273     $option   .=
274         $locale->text('From') . "&nbsp;"
275       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
276   }
277   if ($form->{transdateto}) {
278     $callback .= "&transdateto=$form->{transdateto}";
279     $href     .= "&transdateto=$form->{transdateto}";
280     $option   .= "\n<br>" if ($option);
281     $option   .=
282         $locale->text('To') . "&nbsp;"
283       . $locale->date(\%myconfig, $form->{transdateto}, 1);
284   }
285
286   my $name = ucfirst $form->{vc};
287
288   my @columns = qw(transdate);
289   if ($form->{type} =~ /_order$/) {
290     push @columns, "ordnumber";
291   }
292   if ($form->{type} =~ /_quotation$/) {
293     push @columns, "quonumber";
294   }
295
296   push @columns, qw(name spoolfile);
297   my @column_index = $form->sort_columns(@columns);
298   unshift @column_index, "checked";
299
300   my %column_header;
301   my %column_data;
302
303   $column_header{checked}   = "<th class=listheading>&nbsp;</th>";
304   $column_header{transdate} =
305       "<th><a class=listheading href=$href&sort=transdate>"
306     . $locale->text('Date')
307     . "</a></th>";
308   $column_header{invnumber} =
309       "<th><a class=listheading href=$href&sort=invnumber>"
310     . $locale->text('Invoice')
311     . "</a></th>";
312   $column_header{ordnumber} =
313       "<th><a class=listheading href=$href&sort=ordnumber>"
314     . $locale->text('Order')
315     . "</a></th>";
316   $column_header{quonumber} =
317       "<th><a class=listheading href=$href&sort=quonumber>"
318     . $locale->text('Quotation')
319     . "</a></th>";
320   $column_header{name} =
321       "<th><a class=listheading href=$href&sort=name>"
322     . $locale->text($name)
323     . "</a></th>";
324   $column_header{spoolfile} =
325     "<th class=listheading>" . $locale->text('Spoolfile') . "</th>";
326
327   $form->header;
328
329   print qq|
330 <body>
331
332 <form method=post action=bp.pl>
333
334 <table width=100%>
335   <tr>
336     <th class=listtop>$form->{title}</th>
337   </tr>
338   <tr height="5"></tr>
339   <tr>
340     <td>$option</td>
341   </tr>
342   <tr>
343     <td>
344       <table width=100%>
345         <tr class=listheading>
346 |;
347
348   map { print "\n$column_header{$_}" } @column_index;
349
350   print qq|
351         </tr>
352 |;
353
354   # add sort and escape callback, this one we use for the add sub
355   $form->{callback} = $callback .= "&sort=$form->{sort}";
356
357   # escape callback for href
358   $callback = $form->escape($callback);
359
360   my $i = 0;
361   my $j = 0;
362   my $spoolfile;
363   my $spool = $::lx_office_conf{paths}->{spool};
364
365   foreach my $ref (@{ $form->{SPOOL} }) {
366
367     $i++;
368
369     $form->{"checked_$i"} = "checked" if $form->{"checked_$i"};
370
371     if ($ref->{invoice}) {
372       $ref->{module} = ($ref->{module} eq 'ar') ? "is" : "ir";
373     }
374     my $module = "$ref->{module}.pl";
375
376     $column_data{transdate} = "<td>$ref->{transdate}&nbsp;</td>";
377
378     if ($spoolfile eq $ref->{spoolfile}) {
379       $column_data{checked} = qq|<td></td>|;
380     } else {
381       $column_data{checked} =
382         qq|<td><input name=checked_$i type=checkbox style=checkbox $form->{"checked_$i"} $form->{"checked_$i"}></td>|;
383     }
384
385     $column_data{invnumber} =
386       "<td><a href=$module?action=edit&id=$ref->{id}&type=$form->{type}&callback=$callback>$ref->{invnumber}</a></td>";
387     $column_data{ordnumber} =
388       "<td><a href=$module?action=edit&id=$ref->{id}&type=$form->{type}&callback=$callback>$ref->{ordnumber}</a></td>";
389     $column_data{quonumber} =
390       "<td><a href=$module?action=edit&id=$ref->{id}&type=$form->{type}&callback=$callback>$ref->{quonumber}</a></td>";
391     $column_data{name}      = "<td>$ref->{name}</td>";
392     $column_data{spoolfile} =
393       qq|<td><a href=$spool/$ref->{spoolfile}>$ref->{spoolfile}</a></td>
394 <input type=hidden name="spoolfile_$i" value=$ref->{spoolfile}>
395 |;
396
397     $spoolfile = $ref->{spoolfile};
398
399     $j++;
400     $j %= 2;
401     print "
402         <tr class=listrow$j>
403 ";
404
405     map { print "\n$column_data{$_}" } @column_index;
406
407     print qq|
408         </tr>
409 |;
410
411   }
412
413   print qq|
414 <input type=hidden name=rowcount value=$i>
415
416       </table>
417     </td>
418   </tr>
419   <tr>
420     <td><hr size=3 noshade></td>
421   </tr>
422 </table>
423
424 <br>
425
426 <input name=callback type=hidden value="$form->{callback}">
427
428 <input type=hidden name=title value="$form->{title}">
429 <input type=hidden name=vc value="$form->{vc}">
430 <input type=hidden name=type value="$form->{type}">
431 <input type=hidden name=sort value="$form->{sort}">
432
433 <input type=hidden name=account value="$form->{account}">
434 |;
435
436 #  if ($myconfig{printer}) {
437     print qq|
438 <input type=hidden name=transdateto value=$form->{transdateto}>
439 <input type=hidden name=transdatefrom value=$form->{transdatefrom}>
440 <input type=hidden name=invnumber value=$form->{invnumber}>
441 <input type=hidden name=ordnumber value=$form->{ordnumber}>
442 <input type=hidden name=quonumber value=$form->{quonumber}>
443 <input type=hidden name=customer value=$form->{customer}>
444 <input type=hidden name=vendor value=$form->{vendor}>
445 <input class=submit type=submit name=action value="|
446       . $locale->text('Select all') . qq|">
447 <input class=submit type=submit name=action value="|
448       . $locale->text('Remove') . qq|">
449 <input class=submit type=submit name=action value="|
450       . $locale->text('Print') . qq|">
451 |;
452
453 $form->get_lists(printers=>"ALL_PRINTERS");
454 print qq|<select name="printer">|;
455 print map(qq|<option value="$_->{id}">| . H($_->{printer_description}) . qq|</option>|, @{ $form->{ALL_PRINTERS} });
456 print qq|</select>|;
457
458 #  }
459
460   print qq|
461 </form>
462
463 </body>
464 </html>
465 |;
466
467   $main::lxdebug->leave_sub();
468 }
469
470 sub select_all {
471   $main::lxdebug->enter_sub();
472
473   my $form     = $main::form;
474
475   assert_bp_access();
476
477   map { $form->{"checked_$_"} = 1 } (1 .. $form->{rowcount});
478   &list_spool;
479
480   $main::lxdebug->leave_sub();
481 }
482
483 sub continue { call_sub($main::form->{"nextsub"}); }
484