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