Alle Dateien durch Perltidy laufen lassen. Die verwendeten Optionen sind am Ende...
[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(
295                    'The licensing module has been deactivated in lx-erp.conf.')
296     );
297   }
298
299   $form->{"initial"} = 1;
300
301   do_add();
302   $lxdebug->leave_sub();
303 }
304
305 sub do_add {
306   $lxdebug->enter_sub();
307   $form->{"hidden"} = ["parts_id"];
308   form_header();
309
310   if ($form->{"ndx"}) {
311     $ndx = $form->{"ndx"};
312     foreach (keys(%{$form})) {
313       next unless (/^new_.*_${ndx}$/);
314       s/^new_//;
315       s/_${ndx}$//;
316       $form->{$_} = $form->{"new_${_}_${ndx}"};
317     }
318   }
319
320   if ($form->{"customer"}) {
321     $form->{"customer_id"} = (split(/--/, $form->{"customer"}))[1];
322   }
323
324   if ($form->{"customer_name"}) {
325     LICENSES->get_customers(\%myconfig, $form);
326     if (scalar(@{ $form->{"all_customers"} }) == 1) {
327       %c                       = %{ $form->{"all_customers"}->[0] };
328       $form->{"customer_id"}   = $c{"id"};
329       $form->{"customer_name"} = $c{"name"};
330     } elsif (scalar(@{ $form->{"all_customers"} }) == 0) {
331       $form->{"customer_name"} = "";
332       delete($form->{"customer_id"});
333     } else {
334       print_customer_selection();
335       return;
336     }
337   } elsif (defined($form->{"customer_name"})) {
338     delete($form->{"customer_id"});
339   }
340
341   if ($form->{"partnumber"} || $form->{"description"}) {
342     $form->{"sort"} = "p.partnumber";
343     $form->{searchitems} = "part";
344     IC->all_parts(\%myconfig, $form);
345     if (scalar(@{ $form->{"parts"} }) == 1) {
346       map({ $form->{$_} = $form->{"parts"}->[0]->{$_}; }
347           ("partnumber", "description"));
348       $form->{"parts_id"} = $form->{"parts"}->[0]->{"id"};
349
350     } elsif (scalar(@{ $form->{"parts"} }) == 0) {
351       map({ $form->{$_} = ""; }("partnumber", "description", "parts_id"));
352
353     } else {
354       print_part_selection();
355       return;
356     }
357   } else {
358     delete($form->{"parts_id"});
359   }
360
361   $form->all_vc(\%myconfig, "customer", "");
362
363   print_license_form($form->{"parts_id"} && $form->{"customer_id"});
364
365   form_footer();
366   $lxdebug->leave_sub();
367 }
368
369 sub update {
370   $lxdebug->enter_sub();
371   do_add();
372   $lxdebug->leave_sub();
373 }
374
375 sub continue {
376   $lxdebug->enter_sub();
377   &{ $form->{nextsub} };
378   $lxdebug->leave_sub();
379 }
380
381 sub save {
382   $lxdebug->enter_sub();
383
384   ($form->{customername}, $form->{customer_id}) = split /--/,
385     $form->{customer};
386
387   $form->isblank("customer", $locale->text('Customer missing!'));
388
389   if (   $form->{quantity} eq ""
390       || $form->{quantity} !~ /^[0-9]*$/
391       || $form->{quantity} < 1) {
392     $form->error($locale->text('Please enter a number of licenses.'));
393   }
394
395   if (!$form->{licensenumber} || $form->{licensenumber} eq "") {
396     $form->error($locale->text('Please enter a license key.'));
397   }
398
399   $rc = LICENSES->save_license(\%myconfig, \%$form);
400
401   # load previous variables
402   if ($form->{previousform}) {
403
404     # save the new form variables before splitting previousform
405     map { $newform{$_} = $form->{$_} } keys %$form;
406
407     $previousform = $form->unescape($form->{previousform});
408
409     # don't trample on previous variables
410     map { delete $form->{$_} } keys %newform;
411
412     # now take it apart and restore original values
413     foreach $item (split /&/, $previousform) {
414       ($key, $value) = split /=/, $item, 2;
415       $value =~ s/%26/&/g;
416       $form->{$key} = $value;
417     }
418
419     $form->{"lizenzen_$form->{row}"} =
420       "<option value=$rc>$newform{licensenumber}</option>";
421     $form->{rowcount}--;
422
423     delete $form->{action};
424
425     # restore original callback
426     $callback = $form->unescape($form->{callback});
427     $form->{callback} = $form->unescape($form->{old_callback});
428     delete $form->{old_callback};
429
430     # put callback together
431     foreach $key (keys %$form) {
432
433       # do single escape for Apache 2.0
434       $value = $form->escape($form->{$key}, 1);
435       $callback .= qq|&$key=$value|;
436     }
437     $form->{callback} = $callback;
438
439     # redirect
440     $form->redirect;
441
442   } else {
443     form_header();
444
445     print("Die Lizenz wurde gespeichert.\n");
446     form_footer();
447   }
448
449   $lxdebug->leave_sub();
450 }
451
452 sub search {
453   $lxdebug->enter_sub();
454   if (!$lizenzen) {
455     $form->error(
456                  $locale->text(
457                    'The licensing module has been deactivated in lx-erp.conf.')
458     );
459   }
460
461   form_header();
462
463   print(
464     qq|
465 <table width=100%>
466   <tr>
467     <th class=listtop>| . $locale->text("Licenses") . qq|</th>
468   </tr>
469   <tr>
470     <table>
471       <tr>
472         <th align=right>| . $locale->text('Part Number') . qq|</th>
473         <td><input name=partnumber></td>
474       </tr>
475       <tr>
476         <th align=right>| . $locale->text('Description') . qq|</th>
477         <td><input name=description></td>
478       </tr>
479       <tr>
480         <th align=right>| . $locale->text('Company Name') . qq|</th>
481         <td><input name=customer_name></td>
482       </tr>
483       <tr>
484         <th align=right>| . $locale->text('Include in Report') . qq|</th>
485         <td><input type=radio name=all value=1 checked>|
486       . $locale->text('All')
487       . qq|&nbsp;<input type=radio name=all value=0>|
488       . $locale->text('Expiring in x month(s)')
489       . qq|&nbsp;<input size=4 name=expiring_in value="1"><br>
490         <input type=checkbox name=show_expired value=1>|
491       . $locale->text('Expired licenses')
492       . qq|</td>
493       </tr>
494     </table>
495   </tr>
496   <tr><td colspan=4><hr size=3 noshade></td></tr>                               
497 </table>
498
499 <input type=hidden name=nextsub value=\"do_search\">
500 <input type=submit name=action value=\"| . $locale->text('Continue') . qq|\">
501
502         |);
503
504   form_footer();
505   $lxdebug->leave_sub();
506 }
507
508 sub do_search {
509   $lxdebug->enter_sub();
510   LICENSES->search(\%myconfig, $form);
511
512   $callback = "";
513   foreach (
514            ("db",          "path",        "login",         "password",
515             "partnumber",  "description", "customer_name", "all",
516             "expiring_in", "show_expired")
517     ) {
518     $callback .= "\&${_}=" . $form->escape($form->{$_}, 1);
519   }
520   $details    = $form->{"script"} . "?action=details" . $callback . "\&id=";
521   $invdetails = "is.pl?action=edit" . $callback . "\&id=";
522   $callback   = $form->{"script"} . "?action=do_search" . $callback;
523
524   $form->{"sortby"} = "validuntil" unless ($form->{"sortby"});
525   $form->{"sortasc"} *= 1;
526   foreach (("partnumber", "description", "name", "validuntil", "invnumber")) {
527     $columns{$_} = $callback . "\&sortby=${_}\&sortasc=";
528     if ($form->{"sortby"} eq $_) {
529       $columns{$_} .= (1 - $form->{"sortasc"});
530     } else {
531       $columns{$_} .= "1";
532     }
533   }
534
535   form_header();
536
537   print(
538     qq|
539 <table width=100%>
540   <tr>
541     <th class=listtop>| . $locale->text("Licenses") . qq|</th>
542   </tr>
543         |);
544   if (scalar(@{ $form->{"licenses"} }) == 0) {
545     print(qq|</table>|
546             . $locale->text(
547                       "No licenses were found that match the search criteria.")
548             . qq|</body></html>|);
549     exit(0);
550   }
551
552   print(
553     qq|
554   <tr>
555     <table>
556       <tr>
557         <th class=listtop><a class=listheading href=\"|
558       . $columns{"partnumber"} . "\">"
559       . $locale->text('Part Number')
560       . qq|</a></th>
561         <th class=listtop><a class=listheading href=\"|
562       . $columns{"description"} . "\">"
563       . $locale->text('Description')
564       . qq|</a></th>
565         <th class=listtop><a class=listheading href=\"|
566       . $columns{"name"} . "\">"
567       . $locale->text('Company Name')
568       . qq|</a></th>
569         <th class=listtop><a class=listheading href=\"|
570       . $columns{"validuntil"} . "\">"
571       . $locale->text('Valid until')
572       . qq|</a></th>
573         <th class=listtop><a class=listheading href=\"|
574       . $columns{"invnumber"} . "\">"
575       . $locale->text('Invoice Number')
576       . qq|</a></th>
577       </tr>
578         |);
579
580   $j = 1;
581   for ($i = 0; $i < scalar(@{ $form->{"licenses"} }); $i++) {
582     $ref = $form->{"licenses"}->[$i];
583     print(
584       qq|
585           <tr class=listrow$j>
586           <td><input type=hidden name=id_$i value=| . $ref->{"id"} . qq|
587           <a href=\"${details}$ref->{"id"}\">$ref->{"partnumber"}</a></td>
588           <td><a href=\"${details}$ref->{"id"}\">$ref->{"description"}</a></td>
589           <td><a href=\"${details}$ref->{"id"}\">$ref->{"name"}</a></td>
590           <td><a href=\"${details}$ref->{"id"}\">$ref->{"validuntil"}</a></td>
591           <td align=right>|
592         . (
593         $ref->{"invnumber"}
594         ? qq|<a href=\"${invdetails}$ref->{"invnumber"}\">$ref->{"invnumber"}</a>|
595         : qq|&nbsp;|
596         )
597         . qq|</td>
598           </tr>|);
599     $j = ($j + 1) % 2;
600   }
601
602   $form->{"num_licenses"} = scalar(@{ $form->{"licenses"} });
603   push(@{ $form->{"hidden"} }, "num_licenses");
604
605   print(
606     qq|
607     </table>
608   </tr>
609   <tr>
610     <td><hr size=3 noshade></td>
611   </tr>
612 </table>
613
614 <p>
615
616 <input type=submit name=action value=\"| . $locale->text("Add") . qq|\">
617         |);
618
619   form_footer();
620   $lxdebug->leave_sub();
621 }
622
623 sub details {
624   $lxdebug->enter_sub();
625   LICENSES->get_license(\%myconfig, $form);
626   map(
627     { $form->{$_} = $form->{"license"}->{$_}; } keys(%{ $form->{"license"} }));
628
629   form_header();
630
631   print(
632     qq|
633 <table width=100%>
634   <tr>
635     <th class=listtop>| . $locale->text("View License") . qq|</th>
636   </tr>
637   <tr>
638     <table>
639       <tr>
640         <th align=right>| . $locale->text('Part Number') . qq|</th>
641         <td>$form->{"partnumber"}</td>
642       </tr>
643       <tr>
644         <th align=right>| . $locale->text('Description') . qq|</th>
645         <td>$form->{"description"}</td>
646       </tr>
647       <tr>
648         <th align=right>| . $locale->text('Company Name') . qq|</th>
649         <td>$form->{"name"}</td>
650       </tr>
651       <tr>
652         <th align=right>| . $locale->text('Comment') . qq|</th>
653         <td>$form->{"comment"}</td>
654       </tr>
655       <tr>
656         <th align=right>| . $locale->text('Valid until') . qq|</th>
657         <td>$form->{"validuntil"}</td>
658       </tr>
659       <tr>
660         <th align=right>| . $locale->text('Quantity') . qq|</th>
661         <td>$form->{"quantity"}</td>
662       </tr>
663       <tr>
664         <th align=right>| . $locale->text('License key') . qq|</th>
665         <td>$form->{"licensenumber"}</td>
666       </tr>
667     </table>
668   </tr>
669   <tr>
670     <td><hr size=3 noshade></td>
671   </tr>
672 </table>
673
674 <input type=submit name=action value=\"| . $locale->text("Add") . qq|\">
675         |);
676
677   form_footer();
678   $lxdebug->leave_sub();
679 }
680
681 1;