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