$form->{path} entfernt und hardcodiert auf bin/mozilla gesetzt.
[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 = qw(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   map { $callback .= "\&${_}=" . $form->escape($form->{$_}, 1) }
505     qw(db login password partnumber description customer_name all expiring_in show_expired);
506   $details    = $form->{"script"} . "?action=details" . $callback . "\&id=";
507   $invdetails = "is.pl?action=edit" . $callback . "\&id=";
508   $callback   = $form->{"script"} . "?action=do_search" . $callback;
509
510   $form->{"sortby"} = "validuntil" unless ($form->{"sortby"});
511   $form->{"sortasc"} *= 1;
512   foreach (("partnumber", "description", "name", "validuntil", "invnumber")) {
513     $columns{$_} = $callback . "\&sortby=${_}\&sortasc=";
514     if ($form->{"sortby"} eq $_) {
515       $columns{$_} .= (1 - $form->{"sortasc"});
516     } else {
517       $columns{$_} .= "1";
518     }
519   }
520
521   form_header();
522
523   print(
524     qq|
525 <table width=100%>
526   <tr>
527     <th class=listtop>| . $locale->text("Licenses") . qq|</th>
528   </tr>
529         |);
530   if (scalar(@{ $form->{"licenses"} }) == 0) {
531     print(qq|</table>|
532             . $locale->text(
533                       "No licenses were found that match the search criteria.")
534             . qq|</body></html>|);
535     exit(0);
536   }
537
538   print(
539     qq|
540   <tr>
541     <table>
542       <tr>
543         <th class=listtop><a class=listheading href=\"|
544       . $columns{"partnumber"} . "\">"
545       . $locale->text('Part Number')
546       . qq|</a></th>
547         <th class=listtop><a class=listheading href=\"|
548       . $columns{"description"} . "\">"
549       . $locale->text('Description')
550       . qq|</a></th>
551         <th class=listtop><a class=listheading href=\"|
552       . $columns{"name"} . "\">" . $locale->text('Company Name') . qq|</a></th>
553         <th class=listtop><a class=listheading href=\"|
554       . $columns{"validuntil"} . "\">"
555       . $locale->text('Valid until')
556       . qq|</a></th>
557         <th class=listtop><a class=listheading href=\"|
558       . $columns{"invnumber"} . "\">"
559       . $locale->text('Invoice Number')
560       . qq|</a></th>
561       </tr>
562         |);
563
564   $j = 1;
565   for ($i = 0; $i < scalar(@{ $form->{"licenses"} }); $i++) {
566     $ref = $form->{"licenses"}->[$i];
567     print(
568       qq|
569           <tr class=listrow$j>
570           <td><input type=hidden name=id_$i value=| . $ref->{"id"} . qq|
571           <a href=\"${details}$ref->{"id"}\">$ref->{"partnumber"}</a></td>
572           <td><a href=\"${details}$ref->{"id"}\">$ref->{"description"}</a></td>
573           <td><a href=\"${details}$ref->{"id"}\">$ref->{"name"}</a></td>
574           <td><a href=\"${details}$ref->{"id"}\">$ref->{"validuntil"}</a></td>
575           <td align=right>|
576         . (
577         $ref->{"invnumber"}
578         ? qq|<a href=\"${invdetails}$ref->{"invnumber"}\">$ref->{"invnumber"}</a>|
579         : qq|&nbsp;|
580         )
581         . qq|</td>
582           </tr>|);
583     $j = ($j + 1) % 2;
584   }
585
586   $form->{"num_licenses"} = scalar(@{ $form->{"licenses"} });
587   push(@{ $form->{"hidden"} }, "num_licenses");
588
589   print(
590     qq|
591     </table>
592   </tr>
593   <tr>
594     <td><hr size=3 noshade></td>
595   </tr>
596 </table>
597
598 <p>
599
600 <input type=submit name=action value=\"| . $locale->text("Add") . qq|\">
601         |);
602
603   form_footer();
604   $lxdebug->leave_sub();
605 }
606
607 sub details {
608   $lxdebug->enter_sub();
609   LICENSES->get_license(\%myconfig, $form);
610   map(
611     { $form->{$_} = $form->{"license"}->{$_}; } keys(%{ $form->{"license"} }));
612
613   form_header();
614
615   print(
616     qq|
617 <table width=100%>
618   <tr>
619     <th class=listtop>| . $locale->text("View License") . qq|</th>
620   </tr>
621   <tr>
622     <table>
623       <tr>
624         <th align=right>| . $locale->text('Part Number') . qq|</th>
625         <td>$form->{"partnumber"}</td>
626       </tr>
627       <tr>
628         <th align=right>| . $locale->text('Description') . qq|</th>
629         <td>$form->{"description"}</td>
630       </tr>
631       <tr>
632         <th align=right>| . $locale->text('Company Name') . qq|</th>
633         <td>$form->{"name"}</td>
634       </tr>
635       <tr>
636         <th align=right>| . $locale->text('Comment') . qq|</th>
637         <td>$form->{"comment"}</td>
638       </tr>
639       <tr>
640         <th align=right>| . $locale->text('Valid until') . qq|</th>
641         <td>$form->{"validuntil"}</td>
642       </tr>
643       <tr>
644         <th align=right>| . $locale->text('Quantity') . qq|</th>
645         <td>$form->{"quantity"}</td>
646       </tr>
647       <tr>
648         <th align=right>| . $locale->text('License key') . qq|</th>
649         <td>$form->{"licensenumber"}</td>
650       </tr>
651     </table>
652   </tr>
653   <tr>
654     <td><hr size=3 noshade></td>
655   </tr>
656 </table>
657
658 <input type=submit name=action value=\"| . $locale->text("Add") . qq|\">
659         |);
660
661   form_footer();
662   $lxdebug->leave_sub();
663 }
664
665 1;