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