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