Übergabe des Templates an den ReportGenerator nicht über $form, sondern über ReportGe...
[kivitendo-erp.git] / bin / mozilla / licenses.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) 2002
10 #
11 #  Author: Moritz Bunkus
12 #   Email: m.bunkus@linet-services.de
13 #     Web: http://www.linet-services.de/
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 # Software license module
31 #
32 #======================================================================
33
34 use SL::IC;
35 use SL::IS;
36 use SL::LICENSES;
37
38 require "bin/mozilla/common.pl";
39
40 sub quot {
41   $lxdebug->enter_sub();
42   $_[0] =~ s/\"/\"/g;
43   $lxdebug->leave_sub();
44   return $_[0];
45 }
46
47 sub form_header {
48   $lxdebug->enter_sub();
49
50   $auth->assert('license_edit');
51
52   $form->{jsscript} = 1;
53   $form->header();
54
55   print(
56     qq|<body>
57
58 <form method=post action=$form->{script}>|);
59   $lxdebug->leave_sub();
60 }
61
62 sub form_footer {
63   $lxdebug->enter_sub();
64
65   $auth->assert('license_edit');
66
67   my @items = qw(old_callback previousform);
68   push(@items, @{ $form->{"hidden"} });
69   map({
70       print("<input type=hidden name=$_ value=\"" . quot($form->{$_}) . "\">\n"
71       );
72   } @items);
73
74   print(
75     qq|</form>
76 </body>
77 </html>
78 |);
79   $lxdebug->leave_sub();
80 }
81
82 sub set_std_hidden {
83   $lxdebug->enter_sub();
84   $form->{"hidden"} = ["comment", "validuntil", "quantity", @_];
85   $lxdebug->leave_sub();
86 }
87
88 sub print_part_selection {
89   $lxdebug->enter_sub();
90
91   $auth->assert('license_edit');
92
93   form_header();
94   set_std_hidden("business");
95
96   print(
97     qq|
98
99 <table width=100%>
100   <tr>
101     <th class=listtop colspan=5>|
102       . $locale->text('Select from one of the items below') . qq|</th>
103   </tr>
104   <tr height="5"></tr>
105   <tr class=listheading>
106     <th>&nbsp;</th>
107     <th class=listheading>| . $locale->text('Part Number') . qq|</th>
108     <th class=listheading>| . $locale->text('Description') . qq|</th>
109   </tr>
110         |);
111
112   $j = 1;
113   for ($i = 1; $i <= scalar(@{ $form->{"parts"} }); $i++) {
114     %p = %{ $form->{"parts"}->[$i - 1] };
115     if ($i == 1) {
116       $checked = "checked";
117     } else {
118       $checked = "";
119     }
120
121     print(
122       qq|<tr class=listrow$j>
123       <td><input name=ndx class=radio type=radio value=$i $checked></td>
124       <td><input name=\"new_partnumber_$i\" type=hidden value=\"|
125         . $p{"partnumber"} . qq|\">| . $p{"partnumber"} . qq|</td>
126       <td><input name=\"new_description_$i\" type=hidden value=\"|
127         . $p{"description"} . qq|\">| . $p{"description"} . qq|</td>
128       <input name=\"new_parts_id_$i\" type=hidden value=\"| . $p{"id"} . qq|\">
129     </tr>|);
130
131     $j = ($j + 1) % 2;
132   }
133
134   print(
135     qq|<tr><td colspan=3><hr size=3 noshade></td></tr>
136 </table>
137
138 <input type=hidden name=nextsub value=\"do_add\">
139 <input type=submit name=action value=| . $locale->text('Continue') . qq|>|);
140
141   form_footer();
142   $lxdebug->leave_sub();
143 }
144
145 sub print_customer_selection {
146   $lxdebug->enter_sub();
147
148   $auth->assert('license_edit');
149
150   form_header();
151   set_std_hidden("parts_id", "partnumber", "description");
152
153   print(
154     qq|
155 <table width=100%>
156   <tr>
157     <th class=listtop colspan=5>|
158       . $locale->text('Select from one of the names below') . qq|</th>
159   </tr>
160   <tr height="5"></tr>
161   <tr class=listheading>
162     <th>&nbsp;</th>
163     <th class=listheading>| . $locale->text('Customer Number') . qq|</th>
164     <th class=listheading>| . $locale->text('Company Name') . qq|</th>
165     <th class=listheading>| . $locale->text('Street') . qq|</th>
166     <th class=listheading>| . $locale->text('Zipcode') . qq|</th>
167     <th class=listheading>| . $locale->text('City') . qq|</th>
168   </tr>
169         |);
170
171   print(qq|<tr><td colspan=6><hr size=3 noshade></td></tr>|);
172
173   $j = 1;
174   for ($i = 1; $i <= scalar(@{ $form->{"all_customers"} }); $i++) {
175     %c = %{ $form->{"all_customers"}->[$i - 1] };
176     if ($i == 1) {
177       $checked = "checked";
178     } else {
179       $checked = "";
180     }
181
182     print(
183       qq|<tr class=listrow$j>
184           <td><input name=ndx class=radio type=radio value=$i $checked></td>
185           <td><input name=\"new_customer_id_$i\" type=hidden value=\"|
186         . $c{"id"} . qq|\">$c{"customernumber"}</td>
187           <td><input name=\"new_customer_name_$i\" type=hidden value=\"|
188         . $c{"name"} . qq|\">$c{"name"}</td>
189           <td>$c{"street"}</td>
190           <td>$c{"zipcode"}</td>
191           <td>$c{"city"}</td>
192           </tr>|);
193
194     $j = ($j + 1) % 2;
195   }
196
197   print(
198     qq|
199 </table>
200
201 <input type=hidden name=nextsub value=\"do_add\">
202 <input type=submit name=action value=| . $locale->text('Continue') . qq|>|);
203
204   form_footer();
205   $lxdebug->leave_sub();
206 }
207
208 sub print_license_form {
209   $lxdebug->enter_sub();
210
211   $auth->assert('license_edit');
212
213   print(
214     qq|
215 <table width=100%>
216   <tr>
217     <th class=listtop>| . $locale->text("Add License") . qq|</th>
218   </tr>
219   <tr>
220     <table>
221       <tr>
222         <th align=right>| . $locale->text('Part Number') . qq|</th>
223         <td><input name=partnumber value=\"|
224       . quot($form->{"partnumber"}) . qq|\"></td>
225       </tr>
226       <tr>
227         <th align=right>| . $locale->text('Description') . qq|</th>
228         <td><input name=description value=\"|
229       . quot($form->{"description"}) . qq|\"></td>
230       </tr>
231       <tr>
232         <th align=right>| . $locale->text('Company Name') . qq|</th>|);
233   if ($form->{"all_customer"}) {
234     print(qq|<td><select name=\"customer\">|);
235     foreach (@{ $form->{"all_customer"} }) {
236       if (!defined($form->{"customer_id"})) {
237         $form->{"customer_id"} = $_->{"id"};
238       }
239       $selected = ($_->{"id"} * 1) == $form->{"customer_id"} ? "selected" : "";
240       print(qq|<option $selected> $_->{"name"}--$_->{"id"}</option>|);
241     }
242     print(qq|</select></td>|);
243   } else {
244     print(  qq|<td><input name=customer_name value=\"|
245           . quot($form->{"customer_name"})
246           . qq|\"></td>|);
247   }
248   print(
249     qq|</tr>
250       <tr>
251         <th align=right>| . $locale->text('Comment') . qq|</th>
252         <td><input name=comment value=\"|
253       . quot($form->{"comment"}) . qq|\"></td>
254       </tr>
255       <tr>
256         <th align=right>| . $locale->text('Valid until') . qq|</th>
257         <td><input id=validuntil name=validuntil value=\"|
258       . quot($form->{"validuntil"}) . qq|\">
259          <input type="button" name="validuntil" id="trigger_validuntil" value="?"></td>
260       </tr>
261       <tr>
262         <th align=right>| . $locale->text('Quantity') . qq|</th>
263         <td><input name=quantity value=\"|
264       . quot($form->{"quantity"}) . qq|\"></td>
265       </tr>
266       <tr>
267         <th align=right>| . $locale->text('License key') . qq|</th>
268         <td><input name=licensenumber value=\"|
269       . quot($form->{"licensenumber"}) . qq|\"></td>
270       </tr>
271       <tr>
272         <th align=right>| . $locale->text('Own Product') . qq|</th>
273         <td><input type=checkbox name=own_product value=1 checked></td>
274       </tr>
275     </table>
276
277     <input type=submit name=action value=\"| . $locale->text('Update') . qq|\">
278           |);
279
280   if ($_[0]) {
281     print(
282       qq|&nbsp;
283           <input type=submit name=action value=\"|
284         . $locale->text('Save') . qq|\">\n|);
285   }
286   print(
287     qq|
288   </tr>
289
290 </table>| .
291     $form->write_trigger(\%myconfig, 1, "validuntil", "BL",
292                          "trigger_validuntil"));
293
294   $lxdebug->leave_sub();
295 }
296
297 sub add {
298   $lxdebug->enter_sub();
299
300   $auth->assert('license_edit');
301
302   if (!$lizenzen) {
303     $form->error(
304                  $locale->text(
305                    'The licensing module has been deactivated in lx-erp.conf.')
306     );
307   }
308
309   $form->{"initial"} = 1;
310
311   do_add();
312   $lxdebug->leave_sub();
313 }
314
315 sub do_add {
316   $lxdebug->enter_sub();
317
318   $auth->assert('license_edit');
319
320   $form->{"hidden"} = ["parts_id"];
321   form_header();
322
323   if ($form->{"ndx"}) {
324     $ndx = $form->{"ndx"};
325     foreach (keys(%{$form})) {
326       next unless (/^new_.*_${ndx}$/);
327       s/^new_//;
328       s/_${ndx}$//;
329       $form->{$_} = $form->{"new_${_}_${ndx}"};
330     }
331   }
332
333   if ($form->{"customer"}) {
334     $form->{"customer_id"} = (split(/--/, $form->{"customer"}))[1];
335   }
336
337   if ($form->{"customer_name"}) {
338     LICENSES->get_customers(\%myconfig, $form);
339     if (scalar(@{ $form->{"all_customers"} }) == 1) {
340       %c                       = %{ $form->{"all_customers"}->[0] };
341       $form->{"customer_id"}   = $c{"id"};
342       $form->{"customer_name"} = $c{"name"};
343     } elsif (scalar(@{ $form->{"all_customers"} }) == 0) {
344       $form->{"customer_name"} = "";
345       delete($form->{"customer_id"});
346     } else {
347       print_customer_selection();
348       return;
349     }
350   } elsif (defined($form->{"customer_name"})) {
351     delete($form->{"customer_id"});
352   }
353
354   if ($form->{"partnumber"} || $form->{"description"}) {
355     $form->{"sort"} = "p.partnumber";
356     $form->{searchitems} = "part";
357     IC->all_parts(\%myconfig, $form);
358     if (scalar(@{ $form->{"parts"} }) == 1) {
359       map({ $form->{$_} = $form->{"parts"}->[0]->{$_}; }
360           ("partnumber", "description"));
361       $form->{"parts_id"} = $form->{"parts"}->[0]->{"id"};
362
363     } elsif (scalar(@{ $form->{"parts"} }) == 0) {
364       map({ $form->{$_} = ""; }("partnumber", "description", "parts_id"));
365
366     } else {
367       print_part_selection();
368       return;
369     }
370   } else {
371     delete($form->{"parts_id"});
372   }
373
374   $form->all_vc(\%myconfig, "customer", "");
375
376   print_license_form($form->{"parts_id"} && $form->{"customer_id"});
377
378   form_footer();
379   $lxdebug->leave_sub();
380 }
381
382 sub update {
383   $lxdebug->enter_sub();
384
385   $auth->assert('license_edit');
386
387   do_add();
388
389   $lxdebug->leave_sub();
390 }
391
392 sub continue {
393   $lxdebug->enter_sub();
394   call_sub($form->{"nextsub"});
395   $lxdebug->leave_sub();
396 }
397
398 sub save {
399   $lxdebug->enter_sub();
400
401   $auth->assert('license_edit');
402
403   ($form->{customername}, $form->{customer_id}) = split /--/,
404     $form->{customer};
405
406   $form->isblank("customer", $locale->text('Customer missing!'));
407
408   if (   $form->{quantity} eq ""
409       || $form->{quantity} !~ /^[0-9]*$/
410       || $form->{quantity} < 1) {
411     $form->error($locale->text('Please enter a number of licenses.'));
412   }
413
414   if (!$form->{licensenumber} || $form->{licensenumber} eq "") {
415     $form->error($locale->text('Please enter a license key.'));
416   }
417
418   $rc = LICENSES->save_license(\%myconfig, \%$form);
419
420   # load previous variables
421   if ($form->{previousform}) {
422
423     # save the new form variables before splitting previousform
424     map { $newform{$_} = $form->{$_} } keys %$form;
425
426     $previousform = $form->unescape($form->{previousform});
427
428     # don't trample on previous variables
429     map { delete $form->{$_} } keys %newform;
430
431     # now take it apart and restore original values
432     foreach $item (split /&/, $previousform) {
433       ($key, $value) = split /=/, $item, 2;
434       $value =~ s/%26/&/g;
435       $form->{$key} = $value;
436     }
437
438     $form->{"lizenzen_$form->{row}"} =
439       "<option value=$rc>$newform{licensenumber}</option>";
440     $form->{rowcount}--;
441
442     delete $form->{action};
443
444     # restore original callback
445     $callback = $form->unescape($form->{callback});
446     $form->{callback} = $form->unescape($form->{old_callback});
447     delete $form->{old_callback};
448
449     # put callback together
450     foreach $key (keys %$form) {
451       next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
452
453       # do single escape for Apache 2.0
454       $value = $form->escape($form->{$key}, 1);
455       $callback .= qq|&$key=$value|;
456     }
457     $form->{callback} = $callback;
458
459     # redirect
460     $form->redirect;
461
462   } else {
463     form_header();
464
465     print("Die Lizenz wurde gespeichert.\n");
466     form_footer();
467   }
468
469   $lxdebug->leave_sub();
470 }
471
472 sub search {
473   $lxdebug->enter_sub();
474
475   $auth->assert('license_edit');
476
477   if (!$lizenzen) {
478     $form->error(
479                  $locale->text(
480                    'The licensing module has been deactivated in lx-erp.conf.')
481     );
482   }
483
484   form_header();
485
486   print(
487     qq|
488 <table width=100%>
489   <tr>
490     <th class=listtop>| . $locale->text("Licenses") . qq|</th>
491   </tr>
492   <tr>
493     <table>
494       <tr>
495         <th align=right>| . $locale->text('Part Number') . qq|</th>
496         <td><input name=partnumber></td>
497       </tr>
498       <tr>
499         <th align=right>| . $locale->text('Description') . qq|</th>
500         <td><input name=description></td>
501       </tr>
502       <tr>
503         <th align=right>| . $locale->text('Company Name') . qq|</th>
504         <td><input name=customer_name></td>
505       </tr>
506       <tr>
507         <th align=right>| . $locale->text('Include in Report') . qq|</th>
508         <td><input type=radio name=all value=1 checked>|
509       . $locale->text('All')
510       . qq|&nbsp;<input type=radio name=all value=0>|
511       . $locale->text('Expiring in x month(s)')
512       . qq|&nbsp;<input size=4 name=expiring_in value="1"><br>
513         <input type=checkbox name=show_expired value=1>|
514       . $locale->text('Expired licenses') . qq|</td>
515       </tr>
516     </table>
517   </tr>
518   <tr><td colspan=4><hr size=3 noshade></td></tr>
519 </table>
520
521 <input type=hidden name=nextsub value=\"do_search\">
522 <input type=submit name=action value=\"| . $locale->text('Continue') . qq|\">
523
524         |);
525
526   form_footer();
527   $lxdebug->leave_sub();
528 }
529
530 sub do_search {
531   $lxdebug->enter_sub();
532
533   $auth->assert('license_edit');
534
535   LICENSES->search(\%myconfig, $form);
536
537   $callback = "";
538   map { $callback .= "\&${_}=" . $form->escape($form->{$_}, 1) }
539     qw(db partnumber description customer_name all expiring_in show_expired);
540   $details    = $form->{"script"} . "?action=details" . $callback . "\&id=";
541   $invdetails = "is.pl?action=edit" . $callback . "\&id=";
542   $callback   = $form->{"script"} . "?action=do_search" . $callback;
543
544   $form->{"sortby"} = "validuntil" unless ($form->{"sortby"});
545   $form->{"sortasc"} *= 1;
546   foreach (("partnumber", "description", "name", "validuntil", "invnumber")) {
547     $columns{$_} = $callback . "\&sortby=${_}\&sortasc=";
548     if ($form->{"sortby"} eq $_) {
549       $columns{$_} .= (1 - $form->{"sortasc"});
550     } else {
551       $columns{$_} .= "1";
552     }
553   }
554
555   form_header();
556
557   print(
558     qq|
559 <table width=100%>
560   <tr>
561     <th class=listtop>| . $locale->text("Licenses") . qq|</th>
562   </tr>
563         |);
564   if (scalar(@{ $form->{"licenses"} }) == 0) {
565     print(qq|</table>|
566             . $locale->text(
567                       "No licenses were found that match the search criteria.")
568             . qq|</body></html>|);
569     exit(0);
570   }
571
572   print(
573     qq|
574   <tr>
575     <table>
576       <tr>
577         <th class=listtop><a class=listheading href=\"|
578       . $columns{"partnumber"} . "\">"
579       . $locale->text('Part Number')
580       . qq|</a></th>
581         <th class=listtop><a class=listheading href=\"|
582       . $columns{"description"} . "\">"
583       . $locale->text('Description')
584       . qq|</a></th>
585         <th class=listtop><a class=listheading href=\"|
586       . $columns{"name"} . "\">" . $locale->text('Company Name') . qq|</a></th>
587         <th class=listtop><a class=listheading href=\"|
588       . $columns{"validuntil"} . "\">"
589       . $locale->text('Valid until')
590       . qq|</a></th>
591         <th class=listtop><a class=listheading href=\"|
592       . $columns{"invnumber"} . "\">"
593       . $locale->text('Invoice Number')
594       . qq|</a></th>
595       </tr>
596         |);
597
598   $j = 1;
599   for ($i = 0; $i < scalar(@{ $form->{"licenses"} }); $i++) {
600     $ref = $form->{"licenses"}->[$i];
601     print(
602       qq|
603           <tr class=listrow$j>
604           <td><input type=hidden name=id_$i value=| . $ref->{"id"} . qq|
605           <a href=\"${details}$ref->{"id"}\">$ref->{"partnumber"}</a></td>
606           <td><a href=\"${details}$ref->{"id"}\">$ref->{"description"}</a></td>
607           <td><a href=\"${details}$ref->{"id"}\">$ref->{"name"}</a></td>
608           <td><a href=\"${details}$ref->{"id"}\">$ref->{"validuntil"}</a></td>
609           <td align=right>|
610         . (
611         $ref->{"invnumber"}
612         ? qq|<a href=\"${invdetails}$ref->{"invnumber"}\">$ref->{"invnumber"}</a>|
613         : qq|&nbsp;|
614         )
615         . qq|</td>
616           </tr>|);
617     $j = ($j + 1) % 2;
618   }
619
620   $form->{"num_licenses"} = scalar(@{ $form->{"licenses"} });
621   push(@{ $form->{"hidden"} }, "num_licenses");
622
623   print(
624     qq|
625     </table>
626   </tr>
627   <tr>
628     <td><hr size=3 noshade></td>
629   </tr>
630 </table>
631
632 <p>
633
634 <input type=submit name=action value=\"| . $locale->text("Add") . qq|\">
635         |);
636
637   form_footer();
638   $lxdebug->leave_sub();
639 }
640
641 sub details {
642   $lxdebug->enter_sub();
643
644   $auth->assert('license_edit');
645
646   LICENSES->get_license(\%myconfig, $form);
647   map(
648     { $form->{$_} = $form->{"license"}->{$_}; } keys(%{ $form->{"license"} }));
649
650   form_header();
651
652   print(
653     qq|
654 <table width=100%>
655   <tr>
656     <th class=listtop>| . $locale->text("View License") . qq|</th>
657   </tr>
658   <tr>
659     <table>
660       <tr>
661         <th align=right>| . $locale->text('Part Number') . qq|</th>
662         <td>$form->{"partnumber"}</td>
663       </tr>
664       <tr>
665         <th align=right>| . $locale->text('Description') . qq|</th>
666         <td>$form->{"description"}</td>
667       </tr>
668       <tr>
669         <th align=right>| . $locale->text('Company Name') . qq|</th>
670         <td>$form->{"name"}</td>
671       </tr>
672       <tr>
673         <th align=right>| . $locale->text('Comment') . qq|</th>
674         <td>$form->{"comment"}</td>
675       </tr>
676       <tr>
677         <th align=right>| . $locale->text('Valid until') . qq|</th>
678         <td>$form->{"validuntil"}</td>
679       </tr>
680       <tr>
681         <th align=right>| . $locale->text('Quantity') . qq|</th>
682         <td>$form->{"quantity"}</td>
683       </tr>
684       <tr>
685         <th align=right>| . $locale->text('License key') . qq|</th>
686         <td>$form->{"licensenumber"}</td>
687       </tr>
688     </table>
689   </tr>
690   <tr>
691     <td><hr size=3 noshade></td>
692   </tr>
693 </table>
694
695 <input type=submit name=action value=\"| . $locale->text("Add") . qq|\">
696         |);
697
698   form_footer();
699   $lxdebug->leave_sub();
700 }
701
702 1;