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