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