1445eba05c40dcc049435ddc362c1e9e2b33ea27
[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   } elsif ($form->{type} eq 'packing_list') {
63     $main::lxdebug->message(0, "1");
64     if (!$main::auth->assert('sales_order_edit', 1)) {
65     $main::lxdebug->message(0, "2");
66       $main::auth->assert('invoice_edit') ;
67     }
68     $main::lxdebug->message(0, "3");
69
70   } else {
71     $main::auth->assert('DOES_NOT_EXIST');
72   }
73 }
74
75 sub search {
76   $main::lxdebug->enter_sub();
77
78   my $form     = $main::form;
79   my %myconfig = %main::myconfig;
80   my $locale   = $main::locale;
81
82   my ($name, $account, $onload);
83
84   assert_bp_access();
85
86   # $locale->text('Sales Invoices')
87   # $locale->text('Packing Lists')
88   # $locale->text('Sales Orders')
89   # $locale->text('Purchase Orders')
90   # $locale->text('Quotations')
91   # $locale->text('RFQs')
92   # $locale->text('Checks')
93   # $locale->text('Receipts')
94
95   # setup customer/vendor selection
96   BP->get_vc(\%myconfig, \%$form);
97
98   if (@{ $form->{"all_$form->{vc}"} || [] }) {
99     map { $name .= "<option>$_->{name}--$_->{id}\n" }
100       @{ $form->{"all_$form->{vc}"} };
101     $name = qq|<select name=$form->{vc}><option>\n$name</select>|;
102   } else {
103     $name = qq|<input name=$form->{vc} size=35>|;
104   }
105
106   # $locale->text('Customer')
107   # $locale->text('Vendor')
108
109   my %label = (
110        invoice =>
111          { title => 'Sales Invoices', name => 'Customer', l_invnumber => 'Y' },
112        packing_list =>
113          { title => 'Packing Lists', name => 'Customer', l_invnumber => 'Y' },
114        sales_order =>
115          { title => 'Sales Orders', name => 'Customer', l_ordnumber => 'Y' },
116        purchase_order =>
117          { title => 'Purchase Orders', name => 'Vendor', l_ordnumber => 'Y' },
118        sales_quotation =>
119          { title => 'Quotations', name => 'Customer', l_quonumber => 'Y' },
120        request_quotation =>
121          { title => 'RFQs', name => 'Vendor', l_quonumber => 'Y' },
122        check   => { title => 'Checks',   name => 'Vendor' },
123        receipt => { title => 'Receipts', name => 'Customer' });
124
125   $label{invoice}{invnumber} = qq|
126         <tr>
127           <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
128           <td colspan=3><input name=invnumber size=20></td>
129         </tr>
130 |;
131   $label{invoice}{ordnumber} = qq|
132         <tr>
133           <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
134           <td colspan=3><input name=ordnumber size=20></td>
135         </tr>
136 |;
137   $label{sales_quotation}{quonumber} = qq|
138         <tr>
139           <th align=right nowrap>| . $locale->text('Quotation Number') . qq|</th>
140           <td colspan=3><input name=quonumber size=20></td>
141         </tr>
142 |;
143
144   $label{check}{chknumber} = qq|
145           <tr>
146           <th align=right nowrap>| . $locale->text('Reference') . qq|</th>
147           <td colspan=3><input name=chknumber size=20></td>
148         </tr>
149 |;
150
151   $label{packing_list}{invnumber}      = $label{invoice}{invnumber};
152   $label{packing_list}{ordnumber}      = $label{invoice}{ordnumber};
153   $label{sales_order}{ordnumber}       = $label{invoice}{ordnumber};
154   $label{purchase_order}{ordnumber}    = $label{invoice}{ordnumber};
155   $label{request_quotation}{quonumber} = $label{sales_quotation}{quonumber};
156   $label{receipt}{rctnumber}           = $label{check}{chknumber};
157
158   # do one call to text
159   $form->{title} =
160       $locale->text('Print') . " "
161     . $locale->text($label{ $form->{type} }{title});
162
163   if ($form->{type} =~ /(check|receipt)/) {
164     if (BP->payment_accounts(\%myconfig, \%$form)) {
165       $account = qq|
166         <tr>
167           <th align=right>| . $locale->text('Account') . qq|</th>
168 |;
169
170       if ($form->{accounts}) {
171         $account .= qq|
172           <td colspan=3><select name=account>
173 |;
174         foreach my $ref (@{ $form->{accounts} }) {
175           $account .= qq|
176           <option>$ref->{accno}--$ref->{description}
177 |;
178         }
179
180         $account .= qq|
181           </select>
182 |;
183       } else {
184         $account .= qq|
185           <td colspan=3><input name=account></td>
186 |;
187
188       }
189
190       $account .= qq|
191          </tr>
192 |;
193
194     }
195   }
196
197   # use JavaScript Calendar or not
198   $form->{jsscript} = 1;
199   my $jsscript = "";
200   my ($button1, $button2);
201   if ($form->{jsscript}) {
202
203     # with JavaScript Calendar
204     $button1 = qq|
205        <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
206        <input type=button name=transdatefrom id="trigger1" value=|
207       . $locale->text('button') . qq|></td>
208       |;
209     $button2 = qq|
210        <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
211        <input type=button name=transdateto name=transdateto id="trigger2" value=|
212       . $locale->text('button') . qq|></td>
213      |;
214
215     #write Trigger
216     $jsscript =
217       Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger1",
218                           "transdateto", "BL", "trigger2");
219   } else {
220
221     # without JavaScript Calendar
222     $button1 = qq|
223                               <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
224     $button2 = qq|
225                               <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\"></td>|;
226   }
227   $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
228   $form->header;
229   $onload = qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
230   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
231   print qq|
232 <body onLoad="$onload">
233
234 <form method=post action=bp.pl>
235
236 <input type=hidden name=vc value=$form->{vc}>
237 <input type=hidden name=type value=$form->{type}>
238 <input type=hidden name=title value="$form->{title}">
239
240 <table width=100%>
241   <tr><th class=listtop>$form->{title}</th></tr>
242   <tr height="5"></tr>
243   <tr>
244     <td>
245       <table>
246         <tr>
247           <th align=right>Kunde</th>
248           <td colspan=3>$name</td>
249         </tr>
250         $account
251         $label{$form->{type}}{invnumber}
252         $label{$form->{type}}{ordnumber}
253         $label{$form->{type}}{quonumber}
254         $label{$form->{type}}{chknumber}
255         $label{$form->{type}}{rctnumber}
256         <tr>
257           <th align=right nowrap>| . $locale->text('From') . qq|</th>
258           $button1
259           <th align=right>| . $locale->text('Bis') . qq|</th>
260           $button2
261         </tr>
262         <input type=hidden name=sort value=transdate>
263       </table>
264     </td>
265   </tr>
266   <tr>
267     <td><hr size=3 noshade></td>
268   </tr>
269 </table>
270
271 <input type=hidden name=nextsub value=list_spool>
272
273 <br>
274 <input class=submit type=submit name=action value="|
275     . $locale->text('Continue') . qq|">
276
277 </form>
278
279 </body>
280
281 $jsscript
282
283 </html>
284 |;
285
286   $main::lxdebug->leave_sub();
287 }
288
289 sub remove {
290   $main::lxdebug->enter_sub();
291
292   my $form     = $main::form;
293   my $locale   = $main::locale;
294
295   assert_bp_access();
296
297   my $selected = 0;
298
299   for my $i (1 .. $form->{rowcount}) {
300     if ($form->{"checked_$i"}) {
301       $selected = 1;
302       last;
303     }
304   }
305
306   $form->error('Nothing selected!') unless $selected;
307
308   $form->{title} = $locale->text('Confirm!');
309
310   $form->header;
311
312   print qq|
313 <body>
314
315 <form method=post action=bp.pl>
316 |;
317
318   map { delete $form->{$_} } qw(action header);
319
320   foreach my $key (keys %$form) {
321     next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
322     print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
323   }
324
325   print qq|
326 <h2 class=confirm>$form->{title}</h2>
327
328 <h4>|
329     . $locale->text(
330           'Are you sure you want to remove the marked entries from the queue?')
331     . qq|</h4>
332
333 <input name=action class=submit type=submit value="|
334     . $locale->text('Yes') . qq|">
335 </form>
336
337 </body>
338 </html>
339 |;
340
341   $main::lxdebug->leave_sub();
342 }
343
344 sub yes {
345   $main::lxdebug->enter_sub();
346
347   my $form     = $main::form;
348   my %myconfig = %main::myconfig;
349   my $locale   = $main::locale;
350
351   assert_bp_access();
352
353   $form->info($locale->text('Removing marked entries from queue ...'));
354   $form->{callback} .= "&header=1" if $form->{callback};
355
356   $form->redirect($locale->text('Removed spoolfiles!'))
357     if (BP->delete_spool(\%myconfig, \%$form, $main::spool));
358   $form->error($locale->text('Cannot remove files!'));
359
360   $main::lxdebug->leave_sub();
361 }
362
363 sub print {
364   $main::lxdebug->enter_sub();
365
366   my $form     = $main::form;
367   my %myconfig = %main::myconfig;
368   my $locale   = $main::locale;
369
370   assert_bp_access();
371
372   $form->get_lists(printers => 'ALL_PRINTERS');
373   # use the command stored in the databse or fall back to $myconfig{printer}
374   my $selected_printer =  first { $_ } map ({ $_ ->{printer_command} }
375                                          grep { $_->{id} eq $form->{printer} }
376                                            @{ $form->{ALL_PRINTERS} }),
377                                        $myconfig{printer};
378
379   if ($form->{callback}) {
380     map { $form->{callback} .= "&checked_$_=1" if $form->{"checked_$_"} }
381       (1 .. $form->{rowcount});
382     $form->{callback} .= "&header=1";
383   }
384
385   for my $i (1 .. $form->{rowcount}) {
386     if ($form->{"checked_$i"}) {
387       $form->info($locale->text('Printing ... '));
388
389       if (BP->print_spool(\%myconfig, \%$form, $main::spool, "| $selected_printer")) {
390         print $locale->text('done');
391         $form->redirect($locale->text('Marked entries printed!'));
392       }
393       exit;
394     }
395   }
396
397   $form->error('Nothing selected!');
398
399   $main::lxdebug->leave_sub();
400 }
401
402 sub list_spool {
403   $main::lxdebug->enter_sub();
404
405   my $form     = $main::form;
406   my %myconfig = %main::myconfig;
407   my $locale   = $main::locale;
408
409   assert_bp_access();
410
411   $form->{ $form->{vc} } = $form->unescape($form->{ $form->{vc} });
412   ($form->{ $form->{vc} }, $form->{"$form->{vc}_id"}) =
413     split(/--/, $form->{ $form->{vc} });
414
415   BP->get_spoolfiles(\%myconfig, \%$form);
416
417   my $title = $form->escape($form->{title});
418   my $href  = "bp.pl?action=list_spool&vc=$form->{vc}&type=$form->{type}&title=$title";
419
420   $title = $form->escape($form->{title}, 1);
421   my $callback =
422     "bp.pl?action=list_spool&vc=$form->{vc}&type=$form->{type}&title=$title";
423   my $option;
424
425   if ($form->{ $form->{vc} }) {
426     $callback .= "&$form->{vc}=" . $form->escape($form->{ $form->{vc} }, 1);
427     $href .= "&$form->{vc}=" . $form->escape($form->{ $form->{vc} });
428     $option =
429       ($form->{vc} eq 'customer')
430       ? $locale->text('Customer')
431       : $locale->text('Vendor');
432     $option .= " : $form->{$form->{vc}}";
433   }
434   if ($form->{account}) {
435     $callback .= "&account=" . $form->escape($form->{account}, 1);
436     $href .= "&account=" . $form->escape($form->{account});
437     $option .= "\n<br>" if ($option);
438     $option .= $locale->text('Account') . " : $form->{account}";
439   }
440   if ($form->{invnumber}) {
441     $callback .= "&invnumber=" . $form->escape($form->{invnumber}, 1);
442     $href .= "&invnumber=" . $form->escape($form->{invnumber});
443     $option .= "\n<br>" if ($option);
444     $option .= $locale->text('Invoice Number') . " : $form->{invnumber}";
445   }
446   if ($form->{ordnumber}) {
447     $callback .= "&ordnumber=" . $form->escape($form->{ordnumber}, 1);
448     $href .= "&ordnumber=" . $form->escape($form->{ordnumber});
449     $option .= "\n<br>" if ($option);
450     $option .= $locale->text('Order Number') . " : $form->{ordnumber}";
451   }
452   if ($form->{quonumber}) {
453     $callback .= "&quonumber=" . $form->escape($form->{quonumber}, 1);
454     $href .= "&quonumber=" . $form->escape($form->{quonumber});
455     $option .= "\n<br>" if ($option);
456     $option .= $locale->text('Quotation Number') . " : $form->{quonumber}";
457   }
458
459   if ($form->{transdatefrom}) {
460     $callback .= "&transdatefrom=$form->{transdatefrom}";
461     $href     .= "&transdatefrom=$form->{transdatefrom}";
462     $option   .= "\n<br>" if ($option);
463     $option   .=
464         $locale->text('From') . "&nbsp;"
465       . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
466   }
467   if ($form->{transdateto}) {
468     $callback .= "&transdateto=$form->{transdateto}";
469     $href     .= "&transdateto=$form->{transdateto}";
470     $option   .= "\n<br>" if ($option);
471     $option   .=
472         $locale->text('To') . "&nbsp;"
473       . $locale->date(\%myconfig, $form->{transdateto}, 1);
474   }
475
476   my $name = ucfirst $form->{vc};
477
478   my @columns = qw(transdate);
479   if ($form->{type} =~ /(invoice|packing_list|check|receipt)/) {
480     push @columns, "invnumber";
481   }
482   if ($form->{type} =~ /_order$/) {
483     push @columns, "ordnumber";
484   }
485   if ($form->{type} =~ /_quotation$/) {
486     push @columns, "quonumber";
487   }
488
489   push @columns, qw(name spoolfile);
490   my @column_index = $form->sort_columns(@columns);
491   unshift @column_index, "checked";
492
493   my %column_header;
494   my %column_data;
495
496   $column_header{checked}   = "<th class=listheading>&nbsp;</th>";
497   $column_header{transdate} =
498       "<th><a class=listheading href=$href&sort=transdate>"
499     . $locale->text('Date')
500     . "</a></th>";
501   $column_header{invnumber} =
502       "<th><a class=listheading href=$href&sort=invnumber>"
503     . $locale->text('Invoice')
504     . "</a></th>";
505   $column_header{ordnumber} =
506       "<th><a class=listheading href=$href&sort=ordnumber>"
507     . $locale->text('Order')
508     . "</a></th>";
509   $column_header{quonumber} =
510       "<th><a class=listheading href=$href&sort=quonumber>"
511     . $locale->text('Quotation')
512     . "</a></th>";
513   $column_header{name} =
514       "<th><a class=listheading href=$href&sort=name>"
515     . $locale->text($name)
516     . "</a></th>";
517   $column_header{spoolfile} =
518     "<th class=listheading>" . $locale->text('Spoolfile') . "</th>";
519
520   $form->header;
521
522   print qq|
523 <body>
524
525 <form method=post action=bp.pl>
526
527 <table width=100%>
528   <tr>
529     <th class=listtop>$form->{title}</th>
530   </tr>
531   <tr height="5"></tr>
532   <tr>
533     <td>$option</td>
534   </tr>
535   <tr>
536     <td>
537       <table width=100%>
538         <tr class=listheading>
539 |;
540
541   map { print "\n$column_header{$_}" } @column_index;
542
543   print qq|
544         </tr>
545 |;
546
547   # add sort and escape callback, this one we use for the add sub
548   $form->{callback} = $callback .= "&sort=$form->{sort}";
549
550   # escape callback for href
551   $callback = $form->escape($callback);
552
553   my $i = 0;
554   my $j = 0;
555   my $spoolfile;
556
557   foreach my $ref (@{ $form->{SPOOL} }) {
558
559     $i++;
560
561     $form->{"checked_$i"} = "checked" if $form->{"checked_$i"};
562
563     if ($ref->{invoice}) {
564       $ref->{module} = ($ref->{module} eq 'ar') ? "is" : "ir";
565     }
566     my $module = "$ref->{module}.pl";
567
568     $column_data{transdate} = "<td>$ref->{transdate}&nbsp;</td>";
569
570     if ($spoolfile eq $ref->{spoolfile}) {
571       $column_data{checked} = qq|<td></td>|;
572     } else {
573       $column_data{checked} =
574         qq|<td><input name=checked_$i type=checkbox style=checkbox $form->{"checked_$i"} $form->{"checked_$i"}></td>|;
575     }
576
577     $column_data{invnumber} =
578       "<td><a href=$module?action=edit&id=$ref->{id}&type=$form->{type}&callback=$callback>$ref->{invnumber}</a></td>";
579     $column_data{ordnumber} =
580       "<td><a href=$module?action=edit&id=$ref->{id}&type=$form->{type}&callback=$callback>$ref->{ordnumber}</a></td>";
581     $column_data{quonumber} =
582       "<td><a href=$module?action=edit&id=$ref->{id}&type=$form->{type}&callback=$callback>$ref->{quonumber}</a></td>";
583     $column_data{name}      = "<td>$ref->{name}</td>";
584     $column_data{spoolfile} =
585       qq|<td><a href=$main::spool/$ref->{spoolfile}>$ref->{spoolfile}</a></td>
586 <input type=hidden name="spoolfile_$i" value=$ref->{spoolfile}>
587 |;
588
589     $spoolfile = $ref->{spoolfile};
590
591     $j++;
592     $j %= 2;
593     print "
594         <tr class=listrow$j>
595 ";
596
597     map { print "\n$column_data{$_}" } @column_index;
598
599     print qq|
600         </tr>
601 |;
602
603   }
604
605   print qq|
606 <input type=hidden name=rowcount value=$i>
607
608       </table>
609     </td>
610   </tr>
611   <tr>
612     <td><hr size=3 noshade></td>
613   </tr>
614 </table>
615
616 <br>
617
618 <input name=callback type=hidden value="$form->{callback}">
619
620 <input type=hidden name=title value="$form->{title}">
621 <input type=hidden name=vc value="$form->{vc}">
622 <input type=hidden name=type value="$form->{type}">
623 <input type=hidden name=sort value="$form->{sort}">
624
625 <input type=hidden name=account value="$form->{account}">
626 |;
627
628 #  if ($myconfig{printer}) {
629     print qq|
630 <input type=hidden name=transdateto value=$form->{transdateto}>
631 <input type=hidden name=transdatefrom value=$form->{transdatefrom}>
632 <input type=hidden name=invnumber value=$form->{invnumber}>
633 <input type=hidden name=ordnumber value=$form->{ordnumber}>
634 <input type=hidden name=quonumber value=$form->{quonumber}>
635 <input type=hidden name=customer value=$form->{customer}>
636 <input type=hidden name=vendor value=$form->{vendor}>
637 <input class=submit type=submit name=action value="|
638       . $locale->text('Select all') . qq|">
639 <input class=submit type=submit name=action value="|
640       . $locale->text('Remove') . qq|">
641 <input class=submit type=submit name=action value="|
642       . $locale->text('Print') . qq|">
643 |;
644
645 $form->get_lists(printers=>"ALL_PRINTERS");
646 print qq|<select name="printer">|;
647 print map(qq|<option value="$_->{id}">| . H($_->{printer_description}) . qq|</option>|, @{ $form->{ALL_PRINTERS} });
648 print qq|</select>|;
649
650 #  }
651
652   print qq|
653 </form>
654
655 </body>
656 </html>
657 |;
658
659   $main::lxdebug->leave_sub();
660 }
661
662 sub select_all {
663   $main::lxdebug->enter_sub();
664
665   my $form     = $main::form;
666
667   assert_bp_access();
668
669   map { $form->{"checked_$_"} = 1 } (1 .. $form->{rowcount});
670   &list_spool;
671
672   $main::lxdebug->leave_sub();
673 }
674
675 sub continue { call_sub($main::form->{"nextsub"}); }
676