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