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