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