ed2612911e2f6e523d420ae576441e4d194a3904
[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 Data::Dumper;
13
14 use SL::Common;
15
16 sub save_form {
17   $lxdebug->enter_sub();
18
19   my $old_form = "";
20   map({ $old_form .= "$_=" . $form->escape($form->{$_}) . '&'; } keys(%{$form}));
21   chop($old_form);
22
23   $lxdebug->leave_sub();
24
25   return $old_form;
26 }
27
28 sub restore_form {
29   $lxdebug->enter_sub();
30
31   my ($old_form) = @_;
32
33   map({ delete($form->{$_}); } keys(%{$form}));
34
35   foreach my $pair (split('&', $old_form)) {
36     my ($key, $value) = split('=', $form->unescape($pair), 2);
37     $form->{$key} = $value;
38   }
39
40   $lxdebug->leave_sub();
41 }
42
43 sub select_employee {
44   $lxdebug->enter_sub();
45
46   my ($callback_sub, @employees) = @_;
47
48   if (0 == scalar(@employees)) {
49     @employees = SystemBrace->get_all_employees(\%myconfig, $form);
50   }
51
52   my $old_form = save_form();
53
54   $form->header();
55   print($form->parse_html_template("generic/select_employee",
56                                    { "EMPLOYEES" => \@employees,
57                                      "old_form" => $old_form,
58                                      "title" => $locale->text("Select an employee"),
59                                      "nextsub" => "select_employee_internal",
60                                      "callback_sub" => $callback_sub }));
61
62   $lxdebug->leave_sub();
63 }
64
65 sub select_employee_internal {
66   $lxdebug->enter_sub();
67
68   my ($new_id, $new_name, $callback_sub);
69
70   my $new_id = $form->{"new_id_" . $form->{"selection"}};
71   my $new_name = $form->{"new_name_" . $form->{"selection"}};
72   my $callback_sub = $form->{"callback_sub"};
73
74   restore_form($form->{"old_form"});
75
76   &{ $callback_sub }($new_id, $new_name);
77
78   $lxdebug->leave_sub();
79 }
80
81 sub select_part {
82   $lxdebug->enter_sub();
83
84   my ($callback_sub, @parts) = @_;
85
86   my $remap_parts_id = 0;
87   if (defined($parts[0]->{"parts_id"}) && !defined($parts[0]->{"id"})) {
88     $remap_parts_id = 1;
89     map({ $_->{"id"} = $_->{"parts_id"}; } @parts);
90   }
91
92   my $remap_partnumber = 0;
93   if (defined($parts[0]->{"partnumber"}) && !defined($parts[0]->{"number"})) {
94     $remap_partnumber = 1;
95     map({ $_->{"number"} = $_->{"partnumber"}; } @parts);
96   }
97
98   my $has_charge = 0;
99   if (defined($parts[0]->{"chargenumber"})) {
100     $has_charge = 1;
101     map({ $_->{"has_charge"} = 1; } @parts);
102   }
103
104   my $old_form = save_form();
105
106   $form->header();
107   print($form->parse_html_template("generic/select_part",
108                                    { "PARTS" => \@parts,
109                                      "old_form" => $old_form,
110                                      "title" => $locale->text("Select a part"),
111                                      "nextsub" => "select_part_internal",
112                                      "callback_sub" => $callback_sub,
113                                      "has_charge" => $has_charge,
114                                      "remap_parts_id" => $remap_parts_id,
115                                      "remap_partnumber" => $remap_partnumber }));
116
117   $lxdebug->leave_sub();
118 }
119
120 sub select_part_internal {
121   $lxdebug->enter_sub();
122
123   my ($new_item, $callback_sub);
124
125   my $re = "^new_.*_" . $form->{"selection"};
126   map({
127     my $key = $_;
128     $key =~ s/^new_//;
129     $key =~ s/_\d+$//;
130     $new_item->{$key} = $form->{$_};
131   } grep(/$re/, keys(%{$form})));
132
133   if ($form->{"remap_parts_id"}) {
134     $new_item->{"parts_id"} = $new_item->{"id"};
135     delete($new_item->{"id"});
136   }
137   if ($form->{"remap_partnumber"}) {
138     $new_item->{"partnumber"} = $new_item->{"number"};
139     delete($new_item->{"number"});
140   }
141
142   my $callback_sub = $form->{"callback_sub"};
143
144   restore_form($form->{"old_form"});
145
146   &{ $callback_sub }($new_item);
147
148   $lxdebug->leave_sub();
149 }
150
151 sub part_selection_internal {
152   $lxdebug->enter_sub();
153
154   $order_by = "description";
155   $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
156   $order_dir = 1;
157   $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
158
159   $parts = Common->retrieve_parts(\%myconfig, $form, $order_by, $order_dir);
160   map({ $parts->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$parts}));
161   if (0 == scalar(@{$parts})) {
162     $form->show_generic_information($locale->text("No part was found matching the search parameters."));
163   } elsif (1 == scalar(@{$parts})) {
164     $onload = "part_selected('1')";
165   }
166
167   my $callback = "$form->{script}?action=part_selection_internal&";
168   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
169       (qw(login path password partnumber description input_partnumber input_description input_partsid), grep({ /^[fl]_/ } keys %$form)));
170
171   my @header_sort = qw(partnumber description);
172   my %header_title = ( "partnumber" => $locale->text("Part Number"),
173                        "description" => $locale->text("Part description"),
174                        );
175
176   my @header =
177     map(+{ "column_title" => $header_title{$_},
178            "column" => $_,
179            "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
180          },
181         @header_sort);
182
183   $form->{"title"} = $locale->text("Select a part");
184   $form->header();
185   print($form->parse_html_template("generic/part_selection", { "HEADER" => \@header,
186                                                                "PARTS" => $parts,
187                                                                "onload" => $onload }));
188
189   $lxdebug->leave_sub();
190 }
191
192 sub project_selection_internal {
193   $lxdebug->enter_sub();
194
195   $order_by = "description";
196   $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
197   $order_dir = 1;
198   $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
199
200   $projects = Common->retrieve_projects(\%myconfig, $form, $order_by, $order_dir);
201   map({ $projects->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$projects}));
202   if (0 == scalar(@{$projects})) {
203     $form->show_generic_information($locale->text("No project was found matching the search parameters."));
204   } elsif (1 == scalar(@{$projects})) {
205     $onload = "project_selected('1')";
206   }
207
208   my $callback = "$form->{script}?action=project_selection_internal&";
209   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
210       (qw(login path password projectnumber description input_projectnumber input_description input_project_id), grep({ /^[fl]_/ } keys %$form)));
211
212   my @header_sort = qw(projectnumber description);
213   my %header_title = ( "projectnumber" => $locale->text("Project Number"),
214                        "description" => $locale->text("Project description"),
215                        );
216
217   my @header =
218     map(+{ "column_title" => $header_title{$_},
219            "column" => $_,
220            "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
221          },
222         @header_sort);
223
224   $form->{"title"} = $locale->text("Select a project");
225   $form->header();
226   print($form->parse_html_template("generic/project_selection", { "HEADER" => \@header,
227                                                                   "PROJECTS" => $projects,
228                                                                   "onload" => $onload }));
229
230   $lxdebug->leave_sub();
231 }
232
233 sub employee_selection_internal {
234   $lxdebug->enter_sub();
235
236   $order_by = "name";
237   $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
238   $order_dir = 1;
239   $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
240
241   $employees = Common->retrieve_employees(\%myconfig, $form, $order_by, $order_dir);
242   map({ $employees->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$employees}));
243   if (0 == scalar(@{$employees})) {
244     $form->show_generic_information($locale->text("No employee was found matching the search parameters."));
245   } elsif (1 == scalar(@{$employees})) {
246     $onload = "employee_selected('1')";
247   }
248
249   my $callback = "$form->{script}?action=employee_selection_internal&";
250   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
251       (qw(login path password name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
252
253   my @header_sort = qw(name);
254   my %header_title = ( "name" => $locale->text("Name"),
255                        );
256
257   my @header =
258     map(+{ "column_title" => $header_title{$_},
259            "column" => $_,
260            "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
261          },
262         @header_sort);
263
264   $form->{"title"} = $locale->text("Select an employee");
265   $form->header();
266   print($form->parse_html_template("generic/employee_selection", { "HEADER" => \@header,
267                                                                    "EMPLOYEES" => $employees,
268                                                                    "onload" => $onload }));
269
270   $lxdebug->leave_sub();
271 }
272
273 sub delivery_customer_selection {
274   $lxdebug->enter_sub();
275
276   $order_by = "name";
277   $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
278   $order_dir = 1;
279   $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
280
281   $delivery = Common->retrieve_delivery_customer(\%myconfig, $form, $order_by, $order_dir);
282   map({ $delivery->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$delivery}));
283   if (0 == scalar(@{$delivery})) {
284     $form->show_generic_information($locale->text("No Customer was found matching the search parameters."));
285   } elsif (1 == scalar(@{$delivery})) {
286     $onload = "customer_selected('1')";
287   }
288
289   my $callback = "$form->{script}?action=delivery_customer_selection&";
290   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
291       (qw(login path password name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
292
293   my @header_sort = qw(name customernumber address);
294   my %header_title = ( "name" => $locale->text("Name"),
295                        "customernumber" => $locale->text("Customer Number"),
296                        "address" => $locale->text("Address"),
297                      );
298
299   my @header =
300     map(+{ "column_title" => $header_title{$_},
301            "column" => $_,
302            "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
303          },
304         @header_sort);
305
306   $form->{"title"} = $locale->text("Select a Customer");
307   $form->header();
308   print($form->parse_html_template("generic/select_delivery_customer", { "HEADER" => \@header,
309                                                                    "DELIVERY" => $delivery,
310                                                                    "onload" => $onload }));
311
312   $lxdebug->leave_sub();
313 }
314
315 sub vendor_selection {
316   $lxdebug->enter_sub();
317
318   $order_by = "name";
319   $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
320   $order_dir = 1;
321   $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
322
323   $vendor = Common->retrieve_vendor(\%myconfig, $form, $order_by, $order_dir);
324   map({ $vendor->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$vendor}));
325   if (0 == scalar(@{$vendor})) {
326     $form->show_generic_information($locale->text("No Vendor was found matching the search parameters."));
327   } elsif (1 == scalar(@{$vendor})) {
328     $onload = "vendor_selected('1')";
329   }
330
331   my $callback = "$form->{script}?action=vendor_selection&";
332   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
333       (qw(login path password name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
334
335   my @header_sort = qw(name customernumber address);
336   my %header_title = ( "name" => $locale->text("Name"),
337                        "customernumber" => $locale->text("Customer Number"),
338                        "address" => $locale->text("Address"),
339                      );
340
341   my @header =
342     map(+{ "column_title" => $header_title{$_},
343            "column" => $_,
344            "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
345          },
346         @header_sort);
347
348   $form->{"title"} = $locale->text("Select a Customer");
349   $form->header();
350   print($form->parse_html_template("generic/select_vendor", { "HEADER" => \@header,
351                                                                    "VENDOR" => $vendor,
352                                                                    "onload" => $onload }));
353
354   $lxdebug->leave_sub();
355 }
356
357 sub calculate_qty {
358   $lxdebug->enter_sub();
359
360   my @variable_sort = ();
361   my %variable_list = ();
362   my $unit_list = ();
363   $form->{formel} =~ s/\r\n//g;
364
365   my ($variable_string, $formel) = split /###/,$form->{formel};
366
367
368   split /;/, $variable_string;
369   foreach $item (@_) {
370     my($name, $valueunit) = split /=/,$item;
371     my($value, $unit) = split / /, $valueunit;
372
373     push(@variable_sort, $value);
374     $variable_list{$value} = $name;
375     $unit_list{$value} = $unit;
376   }
377
378   my @header_sort = qw(variable value unit);
379   my %header_title = ( "variable" => $locale->text("Variable"),
380                        "value" => $locale->text("Value"),
381                        "unit" => $locale->text("Unit"),
382                      );
383
384   my @variable = map(+{ "description" => $variable_list{$_},
385                         "name" => $_,
386                         "unit" => $unit_list{$_} }, @variable_sort);
387
388   my @header =
389     map(+{ "column_title" => $header_title{$_},
390            "column" => $_,
391          },
392         @header_sort);
393   $form->{formel} = $formel; 
394   $form->{"title"} = $locale->text("Please enter values");
395   $form->header();
396   print($form->parse_html_template("generic/calculate_qty", { "HEADER" => \@header,
397                                                                    "VARIABLES" => \@variable,
398                                                                    "onload" => $onload }));
399
400   $lxdebug->leave_sub();
401 }
402
403 sub calculate_alu {
404   $lxdebug->enter_sub();
405
406
407
408   my ($length, $weight) = split /\r\n/,$form->{formel};
409
410   #print(STDERR "$form->{formel} Formel\n");
411   #print(STDERR "$form->{description} Description\n");
412
413   map({ $form->{$_} = "" } (qw(qty_alu price_alu total_alu qty_eloxal price_eloxal total_eloxal total)));
414
415   if ($form->{description} =~ /.*Alupreisberechnung.*/) {
416     $form->{description} =~ /.*Alupreisberechnung:\n(.*)kg Aluminiumprofil Einzelpreis: (.*) Gesamt: (.*)\n(.*)m Eloxal Einzelpreis: (.*) Gesamt: (.*)/;
417     $form->{qty_alu} = $1;
418     $form->{price_alu} = $2;
419     $form->{total_alu} = $3;
420     $form->{qty_eloxal} = $4;
421     $form->{price_eloxal} = $5;
422     $form->{total_eloxal} = $6;
423     $form->{total} = $form->format_amount(\%myconfig, ($form->parse_amount(\%myconfig, $form->{total_alu}) + $form->parse_amount(\%myconfig, $form->{total_eloxal})));
424   }
425   ($form->{description}, $null) = split /\nAlupreisberechnung/, $form->{description};
426   #map({ print(STDERR "$_ = $form->{$_}\n") } (qw(qty_alu price_alu total_alu qty_eloxal price_eloxal total_eloxal total)));
427
428   my $callback = "$form->{script}?action=vendor_selection&";
429   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
430       (qw(login path password name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
431
432   my @header_sort = qw(name customernumber address);
433   my %header_title = ( "name" => $locale->text("Name"),
434                        "customernumber" => $locale->text("Customer Number"),
435                        "address" => $locale->text("Address"),
436                      );
437   ($null, $form->{weight}) = split / /, $weight;
438   ($null, $form->{length}) = split / /, $length;
439
440   $form->{calc_weight} = $form->parse_amount(\%myconfig, $form->{weight});
441   $form->{calc_length} = $form->parse_amount(\%myconfig, $form->{length});
442
443
444   my @header =
445     map(+{ "column_title" => $header_title{$_},
446            "column" => $_,
447          },
448         @header_sort);
449
450   $form->{"title"} = $locale->text("Enter values for aluminium calculation");
451   $form->header();
452   print($form->parse_html_template("generic/calculate_alu"));
453
454   $lxdebug->leave_sub();
455 }
456
457
458 sub set_longdescription {
459   $lxdebug->enter_sub();
460
461
462   my $callback = "$form->{script}?action=set_longdescription&";
463   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
464       (qw(login path password name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
465
466   $form->{"title"} = $locale->text("Enter longdescription");
467   $form->header();
468   print($form->parse_html_template("generic/set_longdescription"));
469
470   $lxdebug->leave_sub();
471 }
472
473 1;