6f9e9bedb5761c69454a616743a3c62f7edc11b1
[kivitendo-erp.git] / bin / mozilla / common.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 # Stuff that can be used from other modules
9 #
10 ######################################################################
11
12 use SL::Form;
13 use SL::Common;
14 use SL::MoreCommon;
15
16 sub build_std_url {
17   $lxdebug->enter_sub(2);
18
19   my $script = $form->{script};
20
21   my @parts;
22
23   foreach my $key ((qw(login password), @_)) {
24     next unless ($key);
25
26     if ($key =~ /(.*?)=(.*)/) {
27       if ($1 eq 'script') {
28         $script = $2;
29       } else {
30         push @parts, $key;
31       }
32
33     } else {
34       push @parts, "${key}=" . E($form->{$key});
35     }
36   }
37
38   my $url = "${script}?" . join('&', @parts);
39
40   $lxdebug->leave_sub(2);
41
42   return $url;
43 }
44
45 sub select_employee {
46   $lxdebug->enter_sub();
47
48   my ($callback_sub, @employees) = @_;
49
50   if (0 == scalar(@employees)) {
51     @employees = SystemBrace->get_all_employees(\%myconfig, $form);
52   }
53
54   my $old_form = save_form();
55
56   $form->header();
57   print($form->parse_html_template("generic/select_employee",
58                                    { "EMPLOYEES" => \@employees,
59                                      "old_form" => $old_form,
60                                      "title" => $locale->text("Select an employee"),
61                                      "nextsub" => "select_employee_internal",
62                                      "callback_sub" => $callback_sub }));
63
64   $lxdebug->leave_sub();
65 }
66
67 sub select_employee_internal {
68   $lxdebug->enter_sub();
69
70   my ($new_id, $new_name, $callback_sub);
71
72   my $new_id = $form->{"new_id_" . $form->{"selection"}};
73   my $new_name = $form->{"new_name_" . $form->{"selection"}};
74   my $callback_sub = $form->{"callback_sub"};
75
76   restore_form($form->{"old_form"});
77
78   call_sub($callback_sub, $new_id, $new_name);
79
80   $lxdebug->leave_sub();
81 }
82
83 sub select_part {
84   $lxdebug->enter_sub();
85
86   my ($callback_sub, @parts) = @_;
87
88   my $remap_parts_id = 0;
89   if (defined($parts[0]->{"parts_id"}) && !defined($parts[0]->{"id"})) {
90     $remap_parts_id = 1;
91     map({ $_->{"id"} = $_->{"parts_id"}; } @parts);
92   }
93
94   my $remap_partnumber = 0;
95   if (defined($parts[0]->{"partnumber"}) && !defined($parts[0]->{"number"})) {
96     $remap_partnumber = 1;
97     map({ $_->{"number"} = $_->{"partnumber"}; } @parts);
98   }
99
100   my $has_charge = 0;
101   if (defined($parts[0]->{"chargenumber"})) {
102     $has_charge = 1;
103     map({ $_->{"has_charge"} = 1; } @parts);
104   }
105
106   my $old_form = save_form();
107
108   $form->header();
109   print($form->parse_html_template("generic/select_part",
110                                    { "PARTS" => \@parts,
111                                      "old_form" => $old_form,
112                                      "title" => $locale->text("Select a part"),
113                                      "nextsub" => "select_part_internal",
114                                      "callback_sub" => $callback_sub,
115                                      "has_charge" => $has_charge,
116                                      "remap_parts_id" => $remap_parts_id,
117                                      "remap_partnumber" => $remap_partnumber }));
118
119   $lxdebug->leave_sub();
120 }
121
122 sub select_part_internal {
123   $lxdebug->enter_sub();
124
125   my ($new_item, $callback_sub);
126
127   my $re = "^new_.*_" . $form->{"selection"};
128   map({
129     my $key = $_;
130     $key =~ s/^new_//;
131     $key =~ s/_\d+$//;
132     $new_item->{$key} = $form->{$_};
133   } grep(/$re/, keys(%{$form})));
134
135   if ($form->{"remap_parts_id"}) {
136     $new_item->{"parts_id"} = $new_item->{"id"};
137     delete($new_item->{"id"});
138   }
139   if ($form->{"remap_partnumber"}) {
140     $new_item->{"partnumber"} = $new_item->{"number"};
141     delete($new_item->{"number"});
142   }
143
144   my $callback_sub = $form->{"callback_sub"};
145
146   restore_form($form->{"old_form"});
147
148   call_sub($callback_sub, $new_item);
149
150   $lxdebug->leave_sub();
151 }
152
153 sub part_selection_internal {
154   $lxdebug->enter_sub();
155
156   $order_by = "description";
157   $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
158   $order_dir = 1;
159   $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
160
161   $parts = Common->retrieve_parts(\%myconfig, $form, $order_by, $order_dir);
162   map({ $parts->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$parts}));
163   if (0 == scalar(@{$parts})) {
164     $form->show_generic_information($locale->text("No part was found matching the search parameters."));
165   } elsif (1 == scalar(@{$parts})) {
166     $onload = "part_selected('1')";
167   }
168
169   my $callback = "$form->{script}?action=part_selection_internal&";
170   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
171       (qw(login password partnumber description input_partnumber input_description input_partsid), grep({ /^[fl]_/ } keys %$form)));
172
173   my @header_sort = qw(partnumber description);
174   my %header_title = ( "partnumber" => $locale->text("Part Number"),
175                        "description" => $locale->text("Part description"),
176                        );
177
178   my @header =
179     map(+{ "column_title" => $header_title{$_},
180            "column" => $_,
181            "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
182          },
183         @header_sort);
184
185   $form->{"title"} = $locale->text("Select a part");
186   $form->header();
187   print($form->parse_html_template("generic/part_selection", { "HEADER" => \@header,
188                                                                "PARTS" => $parts,
189                                                                "onload" => $onload }));
190
191   $lxdebug->leave_sub();
192 }
193
194 sub project_selection_internal {
195   $lxdebug->enter_sub();
196
197   $order_by = "description";
198   $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
199   $order_dir = 1;
200   $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
201
202   $projects = Common->retrieve_projects(\%myconfig, $form, $order_by, $order_dir);
203   map({ $projects->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$projects}));
204   if (0 == scalar(@{$projects})) {
205     $form->show_generic_information($locale->text("No project was found matching the search parameters."));
206   } elsif (1 == scalar(@{$projects})) {
207     $onload = "project_selected('1')";
208   }
209
210   my $callback = "$form->{script}?action=project_selection_internal&";
211   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
212       (qw(login password projectnumber description input_projectnumber input_description input_project_id), grep({ /^[fl]_/ } keys %$form)));
213
214   my @header_sort = qw(projectnumber description);
215   my %header_title = ( "projectnumber" => $locale->text("Project Number"),
216                        "description" => $locale->text("Project description"),
217                        );
218
219   my @header =
220     map(+{ "column_title" => $header_title{$_},
221            "column" => $_,
222            "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
223          },
224         @header_sort);
225
226   $form->{"title"} = $locale->text("Select a project");
227   $form->header();
228   print($form->parse_html_template("generic/project_selection", { "HEADER" => \@header,
229                                                                   "PROJECTS" => $projects,
230                                                                   "onload" => $onload }));
231
232   $lxdebug->leave_sub();
233 }
234
235 sub employee_selection_internal {
236   $lxdebug->enter_sub();
237
238   $order_by = "name";
239   $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
240   $order_dir = 1;
241   $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
242
243   $employees = Common->retrieve_employees(\%myconfig, $form, $order_by, $order_dir);
244   map({ $employees->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$employees}));
245   if (0 == scalar(@{$employees})) {
246     $form->show_generic_information($locale->text("No employee was found matching the search parameters."));
247   } elsif (1 == scalar(@{$employees})) {
248     $onload = "employee_selected('1')";
249   }
250
251   my $callback = "$form->{script}?action=employee_selection_internal&";
252   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
253       (qw(login password name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
254
255   my @header_sort = qw(name);
256   my %header_title = ( "name" => $locale->text("Name"),
257                        );
258
259   my @header =
260     map(+{ "column_title" => $header_title{$_},
261            "column" => $_,
262            "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
263          },
264         @header_sort);
265
266   $form->{"title"} = $locale->text("Select an employee");
267   $form->header();
268   print($form->parse_html_template("generic/employee_selection", { "HEADER" => \@header,
269                                                                    "EMPLOYEES" => $employees,
270                                                                    "onload" => $onload }));
271
272   $lxdebug->leave_sub();
273 }
274
275 sub delivery_customer_selection {
276   $lxdebug->enter_sub();
277
278   $order_by = "name";
279   $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
280   $order_dir = 1;
281   $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
282
283   $delivery = Common->retrieve_delivery_customer(\%myconfig, $form, $order_by, $order_dir);
284   map({ $delivery->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$delivery}));
285   if (0 == scalar(@{$delivery})) {
286     $form->show_generic_information($locale->text("No Customer was found matching the search parameters."));
287   } elsif (1 == scalar(@{$delivery})) {
288     $onload = "customer_selected('1')";
289   }
290
291   my $callback = "$form->{script}?action=delivery_customer_selection&";
292   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
293       (qw(login password name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
294
295   my @header_sort = qw(name customernumber address);
296   my %header_title = ( "name" => $locale->text("Name"),
297                        "customernumber" => $locale->text("Customer Number"),
298                        "address" => $locale->text("Address"),
299                      );
300
301   my @header =
302     map(+{ "column_title" => $header_title{$_},
303            "column" => $_,
304            "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
305          },
306         @header_sort);
307
308   $form->{"title"} = $locale->text("Select a Customer");
309   $form->header();
310   print $form->parse_html_template("generic/select_delivery_customer", { "HEADER"   => \@header,
311                                                                          "DELIVERY" => $delivery,
312                                                                          "onload"   => $onload });
313
314   $lxdebug->leave_sub();
315 }
316
317 sub vendor_selection {
318   $lxdebug->enter_sub();
319
320   $order_by = "name";
321   $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
322   $order_dir = 1;
323   $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
324
325   $vendor = Common->retrieve_vendor(\%myconfig, $form, $order_by, $order_dir);
326   map({ $vendor->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$vendor}));
327   if (0 == scalar(@{$vendor})) {
328     $form->show_generic_information($locale->text("No Vendor was found matching the search parameters."));
329   } elsif (1 == scalar(@{$vendor})) {
330     $onload = "vendor_selected('1')";
331   }
332
333   my $callback = "$form->{script}?action=vendor_selection&";
334   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
335       (qw(login password name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
336
337   my @header_sort = qw(name customernumber address);
338   my %header_title = ( "name" => $locale->text("Name"),
339                        "customernumber" => $locale->text("Customer Number"),
340                        "address" => $locale->text("Address"),
341                      );
342
343   my @header =
344     map(+{ "column_title" => $header_title{$_},
345            "column" => $_,
346            "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
347          },
348         @header_sort);
349
350   $form->{"title"} = $locale->text("Select a Customer");
351   $form->header();
352   print $form->parse_html_template("generic/select_vendor", { "HEADER" => \@header,
353                                                               "VENDOR" => $vendor,
354                                                               "onload" => $onload });
355
356   $lxdebug->leave_sub();
357 }
358
359 sub calculate_qty {
360   $lxdebug->enter_sub();
361
362   my @variable_sort = ();
363   my %variable_list = ();
364   my $unit_list = ();
365   $form->{formel} =~ s/\r\n//g;
366
367   my ($variable_string, $formel) = split /###/,$form->{formel};
368
369
370   split /;/, $variable_string;
371   foreach $item (@_) {
372     my($name, $valueunit) = split /=/,$item;
373     my($value, $unit) = split / /, $valueunit;
374
375     push(@variable_sort, $value);
376     $variable_list{$value} = $name;
377     $unit_list{$value} = $unit;
378   }
379
380   my @header_sort = qw(variable value unit);
381   my %header_title = ( "variable" => $locale->text("Variable"),
382                        "value" => $locale->text("Value"),
383                        "unit" => $locale->text("Unit"),
384                      );
385
386   my @variable = map(+{ "description" => $variable_list{$_},
387                         "name" => $_,
388                         "unit" => $unit_list{$_} }, @variable_sort);
389
390   my @header =
391     map(+{ "column_title" => $header_title{$_},
392            "column" => $_,
393          },
394         @header_sort);
395   $form->{formel} = $formel; 
396   $form->{"title"} = $locale->text("Please enter values");
397   $form->header();
398   print($form->parse_html_template("generic/calculate_qty", { "HEADER"    => \@header,
399                                                               "VARIABLES" => \@variable,
400                                                               "onload"    => $onload }));
401
402   $lxdebug->leave_sub();
403 }
404
405 sub set_longdescription {
406   $lxdebug->enter_sub();
407
408   $form->{title} = $locale->text("Enter longdescription");
409   $form->header();
410   print $form->parse_html_template("generic/set_longdescription");
411
412   $lxdebug->leave_sub();
413 }
414
415 sub H {
416   return $form->quote_html($_[0]);
417 }
418
419 sub Q {
420   return $form->quote($_[0]);
421 }
422
423 sub E {
424   return $form->escape($_[0]);
425 }
426
427 sub NTI {
428   my ($element) = @_;
429
430   $element =~ s/tabindex\s*=\s*"\d+"//;
431   return $element;
432 }
433
434 sub format_dates {
435   $lxdebug->enter_sub();
436
437   my ($dateformat, $longformat, @indices) = @_;
438
439   $dateformat = $myconfig{"dateformat"} unless ($dateformat);
440
441   foreach my $idx (@indices) {
442     next unless (defined($form->{$idx}));
443
444     if (!ref($form->{$idx})) {
445       $form->{$idx} = $locale->reformat_date(\%myconfig, $form->{$idx},
446                                              $dateformat, $longformat);
447
448     } elsif (ref($form->{$idx}) eq "ARRAY") {
449       for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
450         $form->{$idx}->[$i] =
451           $locale->reformat_date(\%myconfig, $form->{$idx}->[$i],
452                                  $dateformat, $longformat);
453       }
454     }
455   }
456
457   $lxdebug->leave_sub();
458 }
459
460 sub reformat_numbers {
461   $lxdebug->enter_sub();
462
463   my ($numberformat, $places, @indices) = @_;
464
465   return $lxdebug->leave_sub()
466     if (!$numberformat || ($numberformat eq $myconfig{"numberformat"}));
467
468   foreach my $idx (@indices) {
469     next unless (defined($form->{$idx}));
470
471     if (!ref($form->{$idx})) {
472       $form->{$idx} = $form->parse_amount(\%myconfig, $form->{$idx});
473
474     } elsif (ref($form->{$idx}) eq "ARRAY") {
475       for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
476         $form->{$idx}->[$i] =
477           $form->parse_amount(\%myconfig, $form->{$idx}->[$i]);
478       }
479     }
480   }
481
482   my $saved_numberformat = $myconfig{"numberformat"};
483   $myconfig{"numberformat"} = $numberformat;
484
485   foreach my $idx (@indices) {
486     next unless (defined($form->{$idx}));
487
488     if (!ref($form->{$idx})) {
489       $form->{$idx} = $form->format_amount(\%myconfig, $form->{$idx}, $places);
490
491     } elsif (ref($form->{$idx}) eq "ARRAY") {
492       for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
493         $form->{$idx}->[$i] =
494           $form->format_amount(\%myconfig, $form->{$idx}->[$i], $places);
495       }
496     }
497   }
498
499   $myconfig{"numberformat"} = $saved_numberformat;
500
501   $lxdebug->leave_sub();
502 }
503
504 sub show_history {
505         $lxdebug->enter_sub();
506         my $dbh = $form->dbconnect(\%myconfig);
507         my ($sort, $sortby) = split(/\-\-/, $form->{order});
508   $sort =~ s/.*\.(.*)/$1/;
509   
510         $form->{title} = $locale->text("History");
511     $form->header();
512     print $form->parse_html_template( "common/show_history", {
513         "DATEN" => $form->get_history($dbh,$form->{input_name},"",$form->{order}),
514         "SUCCESS" => ($form->get_history($dbh,$form->{input_name}) ne "0"),
515       uc($sort) => 1,
516       uc($sort)."BY" => $sortby
517         } );
518         
519         $dbh->disconnect();
520         $lxdebug->leave_sub();  
521 }
522
523 sub call_sub {
524   $lxdebug->enter_sub();
525
526   my $name = shift;
527
528   if (!$name) {
529     $form->error($locale->text("Trying to call a sub without a name"));
530   }
531
532   $name =~ s/[^a-zA-Z0-9_]//g;
533
534   if (!defined(&{ $name })) {
535     $form->error(sprintf($locale->text("Attempt to call an undefined sub named '%s'"), $name));
536   }
537
538   &{ $name }(@_);
539
540   $lxdebug->leave_sub();
541 }
542
543 sub show_vc_details {
544         $lxdebug->enter_sub();
545
546   $form->{vc} = $form->{vc} eq "customer" ? "customer" : "vendor";
547   $form->isblank("vc_id",
548                  $form->{vc} eq "customer" ?
549                  $locale->text("No customer has been selected yet.") :
550                  $locale->text("No vendor has been selected yet."));
551
552   Common->get_vc_details(\%myconfig, $form, $form->{vc}, $form->{vc_id});
553
554   $form->{title} = $form->{vc} eq "customer" ?
555     $locale->text("Customer details") : $locale->text("Vendor details");
556   $form->header();
557   print $form->parse_html_template("common/show_vc_details", { "is_customer" => $form->{vc} eq "customer" });
558
559         $lxdebug->leave_sub();
560 }
561
562 sub mark_as_paid_common {
563   $lxdebug->enter_sub();
564   use SL::DBUtils;
565   my ($myconfig, $db_name) = @_;
566
567   if($form->{mark_as_paid}) {
568     my $dbh ||= $form->get_standard_dbh($myconfig);
569     my $query = qq|UPDATE $db_name SET paid = amount WHERE id = ?|;
570     do_query($form, $dbh, $query, $form->{id});
571     $dbh->commit();
572     $form->redirect($locale->text("Marked as paid"));
573 }
574   else {
575     my $referer  =  $ENV{HTTP_REFERER};
576     my $login    =  $form->escape($form->{login});
577     my $password =  $form->escape($form->{password});
578     my $id       =  $form->escape($form->{id});
579     $referer     =~ s/^(.*)action\=.*\&(.*)$/$1action\=mark_as_paid\&mark_as_paid\=1\&login\=$login\&password\=$password\&id\=$id\&$2/;
580     $form->header();
581     print qq|<body>|;
582     print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
583     print qq|<input type="button" value="|.$locale->text('yes').qq|" onclick="document.location.href='|.$referer.qq|'">&nbsp;|;
584     print qq|<input type="button" value="|.$locale->text('no').qq|" onclick="javascript:history.back();">|;
585     print qq|</body></html>|;
586 }
587   
588   $lxdebug->leave_sub();
589 }
590
591 1;