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