Globale Variablen für Systemeinstellungen und Features nach %::lx_office_conf verschoben
[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   $form->{title} = $locale->text('Add License');
327
328   if (!$::lx_office_conf{system}->{lizenzen}) {
329     $form->error(
330                  $locale->text(
331                    'The licensing module has been deactivated in lx-erp.conf.')
332     );
333   }
334
335   $form->{"initial"} = 1;
336
337   do_add();
338   $main::lxdebug->leave_sub();
339 }
340
341 sub do_add {
342   $main::lxdebug->enter_sub();
343
344   $main::auth->assert('license_edit');
345
346   my $form     = $main::form;
347   my %myconfig = %main::myconfig;
348
349   $form->{"hidden"} = ["parts_id"];
350   form_header();
351
352   if ($form->{"ndx"}) {
353     my $ndx = $form->{"ndx"};
354     foreach (keys(%{$form})) {
355       next unless (/^new_.*_${ndx}$/);
356       s/^new_//;
357       s/_${ndx}$//;
358       $form->{$_} = $form->{"new_${_}_${ndx}"};
359     }
360   }
361
362   if ($form->{"customer"}) {
363     $form->{"customer_id"} = (split(/--/, $form->{"customer"}))[1];
364   }
365
366   if ($form->{"customer_name"}) {
367     LICENSES->get_customers(\%myconfig, $form);
368     if (scalar(@{ $form->{"all_customers"} }) == 1) {
369       my %c                       = %{ $form->{"all_customers"}->[0] };
370       $form->{"customer_id"}   = $c{"id"};
371       $form->{"customer_name"} = $c{"name"};
372     } elsif (scalar(@{ $form->{"all_customers"} }) == 0) {
373       $form->{"customer_name"} = "";
374       delete($form->{"customer_id"});
375     } else {
376       print_customer_selection();
377       return;
378     }
379   } elsif (defined($form->{"customer_name"})) {
380     delete($form->{"customer_id"});
381   }
382
383   if ($form->{"partnumber"} || $form->{"description"}) {
384     $form->{"sort"} = "p.partnumber";
385     $form->{searchitems} = "part";
386     IC->all_parts(\%myconfig, $form);
387     if (scalar(@{ $form->{"parts"} }) == 1) {
388       map({ $form->{$_} = $form->{"parts"}->[0]->{$_}; }
389           ("partnumber", "description"));
390       $form->{"parts_id"} = $form->{"parts"}->[0]->{"id"};
391
392     } elsif (scalar(@{ $form->{"parts"} }) == 0) {
393       map({ $form->{$_} = ""; }("partnumber", "description", "parts_id"));
394
395     } else {
396       print_part_selection();
397       return;
398     }
399   } else {
400     delete($form->{"parts_id"});
401   }
402
403   $form->all_vc(\%myconfig, "customer", "");
404
405   print_license_form($form->{"parts_id"} && $form->{"customer_id"});
406
407   form_footer();
408   $main::lxdebug->leave_sub();
409 }
410
411 sub update {
412   $main::lxdebug->enter_sub();
413
414   $main::auth->assert('license_edit');
415
416   my $form     = $main::form;
417
418   do_add();
419
420   $main::lxdebug->leave_sub();
421 }
422
423 sub continue {
424   $main::lxdebug->enter_sub();
425
426   my $form     = $main::form;
427
428   call_sub($form->{"nextsub"});
429   $main::lxdebug->leave_sub();
430 }
431
432 sub save {
433   $main::lxdebug->enter_sub();
434
435   $main::auth->assert('license_edit');
436
437   my $form     = $main::form;
438   my %myconfig = %main::myconfig;
439   my $locale   = $main::locale;
440
441   ($form->{customername}, $form->{customer_id}) = split /--/,
442     $form->{customer};
443
444   $form->isblank("customer", $locale->text('Customer missing!'));
445
446   if (   $form->{quantity} eq ""
447       || $form->{quantity} !~ /^[0-9]*$/
448       || $form->{quantity} < 1) {
449     $form->error($locale->text('Please enter a number of licenses.'));
450   }
451
452   if (!$form->{licensenumber} || $form->{licensenumber} eq "") {
453     $form->error($locale->text('Please enter a license key.'));
454   }
455
456   my $rc = LICENSES->save_license(\%myconfig, \%$form);
457
458   # load previous variables
459   if ($form->{previousform}) {
460
461     # save the new form variables before splitting previousform
462     my %newform;
463     map { $newform{$_} = $form->{$_} } keys %$form;
464
465     my $previousform = $form->unescape($form->{previousform});
466
467     # don't trample on previous variables
468     map { delete $form->{$_} } keys %newform;
469
470     # now take it apart and restore original values
471     foreach my $item (split /&/, $previousform) {
472       my ($key, $value) = split /=/, $item, 2;
473       $value =~ s/%26/&/g;
474       $form->{$key} = $value;
475     }
476
477     $form->{"lizenzen_$form->{row}"} =
478       "<option value=$rc>$newform{licensenumber}</option>";
479     $form->{rowcount}--;
480
481     delete $form->{action};
482
483     # restore original callback
484     my $callback = $form->unescape($form->{callback});
485     $form->{callback} = $form->unescape($form->{old_callback});
486     delete $form->{old_callback};
487
488     # put callback together
489     foreach my $key (keys %$form) {
490       next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
491
492       # do single escape for Apache 2.0
493       my $value = $form->escape($form->{$key}, 1);
494       $callback .= qq|&$key=$value|;
495     }
496     $form->{callback} = $callback;
497
498     # redirect
499     $form->redirect;
500
501   } else {
502     form_header();
503
504     print("Die Lizenz wurde gespeichert.\n");
505     form_footer();
506   }
507
508   $main::lxdebug->leave_sub();
509 }
510
511 sub search {
512   $main::lxdebug->enter_sub();
513
514   $main::auth->assert('license_edit');
515
516   my $form     = $main::form;
517   my $locale   = $main::locale;
518
519   $form->{title} = $locale->text('Licenses');
520
521   if (!$::lx_office_conf{system}->{lizenzen}) {
522     $form->error(
523                  $locale->text(
524                    'The licensing module has been deactivated in lx-erp.conf.')
525     );
526   }
527
528   form_header();
529
530   print(
531     qq|
532 <table width=100%>
533   <tr>
534     <th class=listtop>| . $locale->text("Licenses") . qq|</th>
535   </tr>
536   <tr>
537     <table>
538       <tr>
539         <th align=right>| . $locale->text('Part Number') . qq|</th>
540         <td><input name=partnumber></td>
541       </tr>
542       <tr>
543         <th align=right>| . $locale->text('Description') . qq|</th>
544         <td><input name=description></td>
545       </tr>
546       <tr>
547         <th align=right>| . $locale->text('Company Name') . qq|</th>
548         <td><input name=customer_name></td>
549       </tr>
550       <tr>
551         <th align=right>| . $locale->text('Include in Report') . qq|</th>
552         <td><input type=radio name=all value=1 checked>|
553       . $locale->text('All')
554       . qq|&nbsp;<input type=radio name=all value=0>|
555       . $locale->text('Expiring in x month(s)')
556       . qq|&nbsp;<input size=4 name=expiring_in value="1"><br>
557         <input type=checkbox name=show_expired value=1>|
558       . $locale->text('Expired licenses') . qq|</td>
559       </tr>
560     </table>
561   </tr>
562   <tr><td colspan=4><hr size=3 noshade></td></tr>
563 </table>
564
565 <input type=hidden name=nextsub value=\"do_search\">
566 <input type=submit name=action value=\"| . $locale->text('Continue') . qq|\">
567
568         |);
569
570   form_footer();
571   $main::lxdebug->leave_sub();
572 }
573
574 sub do_search {
575   $main::lxdebug->enter_sub();
576
577   $main::auth->assert('license_edit');
578
579   my $form     = $main::form;
580   my %myconfig = %main::myconfig;
581   my $locale   = $main::locale;
582
583   LICENSES->search(\%myconfig, $form);
584
585   my $callback = "";
586   map { $callback .= "\&${_}=" . $form->escape($form->{$_}, 1) }
587     qw(db partnumber description customer_name all expiring_in show_expired);
588   my $details    = $form->{"script"} . "?action=details" . $callback . "\&id=";
589   my $invdetails = "is.pl?action=edit" . $callback . "\&id=";
590   $callback   = $form->{"script"} . "?action=do_search" . $callback;
591
592   $form->{"sortby"} = "validuntil" unless ($form->{"sortby"});
593   $form->{"sortasc"} *= 1;
594   my %columns;
595   foreach (("partnumber", "description", "name", "validuntil", "invnumber")) {
596     $columns{$_} = $callback . "\&sortby=${_}\&sortasc=";
597     if ($form->{"sortby"} eq $_) {
598       $columns{$_} .= (1 - $form->{"sortasc"});
599     } else {
600       $columns{$_} .= "1";
601     }
602   }
603
604   form_header();
605
606   print(
607     qq|
608 <table width=100%>
609   <tr>
610     <th class=listtop>| . $locale->text("Licenses") . qq|</th>
611   </tr>
612         |);
613   if (scalar(@{ $form->{"licenses"} }) == 0) {
614     print(qq|</table>|
615             . $locale->text(
616                       "No licenses were found that match the search criteria.")
617             . qq|</body></html>|);
618     ::end_of_request();
619   }
620
621   print(
622     qq|
623   <tr>
624     <table>
625       <tr>
626         <th class=listtop><a class=listheading href=\"|
627       . $columns{"partnumber"} . "\">"
628       . $locale->text('Part Number')
629       . qq|</a></th>
630         <th class=listtop><a class=listheading href=\"|
631       . $columns{"description"} . "\">"
632       . $locale->text('Description')
633       . qq|</a></th>
634         <th class=listtop><a class=listheading href=\"|
635       . $columns{"name"} . "\">" . $locale->text('Company Name') . qq|</a></th>
636         <th class=listtop><a class=listheading href=\"|
637       . $columns{"validuntil"} . "\">"
638       . $locale->text('Valid until')
639       . qq|</a></th>
640         <th class=listtop><a class=listheading href=\"|
641       . $columns{"invnumber"} . "\">"
642       . $locale->text('Invoice Number')
643       . qq|</a></th>
644       </tr>
645         |);
646
647   my $j = 1;
648   for (my $i = 0; $i < scalar(@{ $form->{"licenses"} }); $i++) {
649     my $ref = $form->{"licenses"}->[$i];
650     print(
651       qq|
652           <tr class=listrow$j>
653           <td><input type=hidden name=id_$i value=| . $ref->{"id"} . qq|
654           <a href=\"${details}$ref->{"id"}\">$ref->{"partnumber"}</a></td>
655           <td><a href=\"${details}$ref->{"id"}\">$ref->{"description"}</a></td>
656           <td><a href=\"${details}$ref->{"id"}\">$ref->{"name"}</a></td>
657           <td><a href=\"${details}$ref->{"id"}\">$ref->{"validuntil"}</a></td>
658           <td align=right>|
659         . (
660         $ref->{"invnumber"}
661         ? qq|<a href=\"${invdetails}$ref->{"invnumber"}\">$ref->{"invnumber"}</a>|
662         : qq|&nbsp;|
663         )
664         . qq|</td>
665           </tr>|);
666     $j = ($j + 1) % 2;
667   }
668
669   $form->{"num_licenses"} = scalar(@{ $form->{"licenses"} });
670   push(@{ $form->{"hidden"} }, "num_licenses");
671
672   print(
673     qq|
674     </table>
675   </tr>
676   <tr>
677     <td><hr size=3 noshade></td>
678   </tr>
679 </table>
680
681 <p>
682
683 <input type=submit name=action value=\"| . $locale->text("Add") . qq|\">
684         |);
685
686   form_footer();
687   $main::lxdebug->leave_sub();
688 }
689
690 sub details {
691   $main::lxdebug->enter_sub();
692
693   $main::auth->assert('license_edit');
694
695   my $form     = $main::form;
696   my %myconfig = %main::myconfig;
697   my $locale   = $main::locale;
698
699   LICENSES->get_license(\%myconfig, $form);
700   map(
701     { $form->{$_} = $form->{"license"}->{$_}; } keys(%{ $form->{"license"} }));
702
703   form_header();
704
705   print(
706     qq|
707 <table width=100%>
708   <tr>
709     <th class=listtop>| . $locale->text("View License") . qq|</th>
710   </tr>
711   <tr>
712     <table>
713       <tr>
714         <th align=right>| . $locale->text('Part Number') . qq|</th>
715         <td>$form->{"partnumber"}</td>
716       </tr>
717       <tr>
718         <th align=right>| . $locale->text('Description') . qq|</th>
719         <td>$form->{"description"}</td>
720       </tr>
721       <tr>
722         <th align=right>| . $locale->text('Company Name') . qq|</th>
723         <td>$form->{"name"}</td>
724       </tr>
725       <tr>
726         <th align=right>| . $locale->text('Comment') . qq|</th>
727         <td>$form->{"comment"}</td>
728       </tr>
729       <tr>
730         <th align=right>| . $locale->text('Valid until') . qq|</th>
731         <td>$form->{"validuntil"}</td>
732       </tr>
733       <tr>
734         <th align=right>| . $locale->text('Quantity') . qq|</th>
735         <td>$form->{"quantity"}</td>
736       </tr>
737       <tr>
738         <th align=right>| . $locale->text('License key') . qq|</th>
739         <td>$form->{"licensenumber"}</td>
740       </tr>
741     </table>
742   </tr>
743   <tr>
744     <td><hr size=3 noshade></td>
745   </tr>
746 </table>
747
748 <input type=submit name=action value=\"| . $locale->text("Add") . qq|\">
749         |);
750
751   form_footer();
752   $main::lxdebug->leave_sub();
753 }
754
755 1;