1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 ######################################################################
8 # Stuff that can be used from other modules
10 ######################################################################
18 $lxdebug->enter_sub(2);
20 my $script = $form->{script};
24 foreach my $key (@_) {
27 if ($key =~ /(.*?)=(.*)/) {
35 foreach my $var ($form->flatten_variables($key)) {
36 push @parts, E($var->{key}) . '=' . E($var->{value});
41 my $url = "${script}?" . join('&', @parts);
43 $lxdebug->leave_sub(2);
48 # -------------------------------------------------------------------------
51 $lxdebug->enter_sub();
53 my ($callback_sub, @parts) = @_;
55 my $remap_parts_id = 0;
56 if (defined($parts[0]->{parts_id}) && !defined($parts[0]->{id})) {
58 map { $_->{id} = $_->{parts_id}; } @parts;
61 my $remap_partnumber = 0;
62 if (defined($parts[0]->{partnumber}) && !defined($parts[0]->{number})) {
63 $remap_partnumber = 1;
64 map { $_->{number} = $_->{partnumber}; } @parts;
68 if (defined($parts[0]->{chargenumber})) {
70 map { $_->{has_charge} = 1; } @parts;
73 my $old_form = save_form();
76 print $form->parse_html_template("generic/select_part",
78 "old_form" => $old_form,
79 "title" => $locale->text("Select a part"),
80 "nextsub" => "select_part_internal",
81 "callback_sub" => $callback_sub,
82 "has_charge" => $has_charge,
83 "remap_parts_id" => $remap_parts_id,
84 "remap_partnumber" => $remap_partnumber });
86 $lxdebug->leave_sub();
89 sub select_part_internal {
90 $lxdebug->enter_sub();
92 my ($new_item, $callback_sub);
94 my $re = "^new_.*_" . $form->{selection};
96 foreach (grep /$re/, keys %{ $form }) {
98 $new_key =~ s/^new_//;
99 $new_key =~ s/_\d+$//;
100 $new_item->{$new_key} = $form->{$_};
103 if ($form->{remap_parts_id}) {
104 $new_item->{parts_id} = $new_item->{id};
105 delete $new_item->{id};
108 if ($form->{remap_partnumber}) {
109 $new_item->{partnumber} = $new_item->{number};
110 delete $new_item->{number};
113 my $callback_sub = $form->{callback_sub};
115 restore_form($form->{old_form});
117 call_sub($callback_sub, $new_item);
119 $lxdebug->leave_sub();
122 sub part_selection_internal {
123 $lxdebug->enter_sub();
125 $order_by = "description";
126 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
128 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
130 %options = map { $_ => 1 } split m/:/, $form->{options};
132 map { $form->{$_} = 1 if ($options{$_}) } qw(no_services no_assemblies stockable);
134 $parts = Common->retrieve_parts(\%myconfig, $form, $order_by, $order_dir);
136 if (0 == scalar(@{$parts})) {
137 $form->show_generic_information($locale->text("No part was found matching the search parameters."));
138 } elsif (1 == scalar(@{$parts})) {
139 $onload = "part_selected('1')";
142 map { $parts->[$_]->{selected} = $_ ? 0 : 1; } (0..$#{$parts});
144 my $callback = build_std_url('action=part_selection_internal', qw(partnumber description input_partnumber input_description input_partsid),
145 grep({ /^[fl]_/ } keys %{ $form }));
147 my @header_sort = qw(partnumber description);
148 my %header_title = ( "partnumber" => $locale->text("Part Number"),
149 "description" => $locale->text("Part description"),
153 map(+{ "column_title" => $header_title{$_},
155 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
159 $form->{title} = $locale->text("Select a part");
161 print $form->parse_html_template("generic/part_selection", { "HEADER" => \@header,
163 "onload" => $onload });
165 $lxdebug->leave_sub();
168 # -------------------------------------------------------------------------
170 sub delivery_customer_selection {
171 $lxdebug->enter_sub();
174 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
176 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
178 $delivery = Common->retrieve_delivery_customer(\%myconfig, $form, $order_by, $order_dir);
179 map({ $delivery->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$delivery}));
180 if (0 == scalar(@{$delivery})) {
181 $form->show_generic_information($locale->text("No Customer was found matching the search parameters."));
182 } elsif (1 == scalar(@{$delivery})) {
183 $onload = "customer_selected('1')";
186 my $callback = "$form->{script}?action=delivery_customer_selection&";
187 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
188 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
190 my @header_sort = qw(name customernumber address);
191 my %header_title = ( "name" => $locale->text("Name"),
192 "customernumber" => $locale->text("Customer Number"),
193 "address" => $locale->text("Address"),
197 map(+{ "column_title" => $header_title{$_},
199 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
203 $form->{"title"} = $locale->text("Select a Customer");
205 print $form->parse_html_template("generic/select_delivery_customer", { "HEADER" => \@header,
206 "DELIVERY" => $delivery,
207 "onload" => $onload });
209 $lxdebug->leave_sub();
212 # -------------------------------------------------------------------------
214 sub vendor_selection {
215 $lxdebug->enter_sub();
218 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
220 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
222 $vendor = Common->retrieve_vendor(\%myconfig, $form, $order_by, $order_dir);
223 map({ $vendor->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$vendor}));
224 if (0 == scalar(@{$vendor})) {
225 $form->show_generic_information($locale->text("No Vendor was found matching the search parameters."));
226 } elsif (1 == scalar(@{$vendor})) {
227 $onload = "vendor_selected('1')";
230 my $callback = "$form->{script}?action=vendor_selection&";
231 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
232 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
234 my @header_sort = qw(name customernumber address);
235 my %header_title = ( "name" => $locale->text("Name"),
236 "customernumber" => $locale->text("Customer Number"),
237 "address" => $locale->text("Address"),
241 map(+{ "column_title" => $header_title{$_},
243 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
247 $form->{"title"} = $locale->text("Select a Customer");
249 print $form->parse_html_template("generic/select_vendor", { "HEADER" => \@header,
251 "onload" => $onload });
253 $lxdebug->leave_sub();
256 # -------------------------------------------------------------------------
259 $lxdebug->enter_sub();
261 my @variable_sort = ();
262 my %variable_list = ();
264 $form->{formel} =~ s/\r\n//g;
266 my ($variable_string, $formel) = split /###/,$form->{formel};
269 split m/;/, $variable_string;
271 my($name, $valueunit) = split /=/,$item;
272 my($value, $unit) = split / /, $valueunit;
274 push(@variable_sort, $value);
275 $variable_list{$value} = $name;
276 $unit_list{$value} = $unit;
279 my @header_sort = qw(variable value unit);
280 my %header_title = ( "variable" => $locale->text("Variable"),
281 "value" => $locale->text("Value"),
282 "unit" => $locale->text("Unit"),
285 my @variable = map(+{ "description" => $variable_list{$_},
287 "unit" => $unit_list{$_} }, @variable_sort);
290 map(+{ "column_title" => $header_title{$_},
294 $form->{formel} = $formel;
295 $form->{"title"} = $locale->text("Please enter values");
297 print($form->parse_html_template("generic/calculate_qty", { "HEADER" => \@header,
298 "VARIABLES" => \@variable,
299 "onload" => $onload }));
301 $lxdebug->leave_sub();
304 # -------------------------------------------------------------------------
306 sub set_longdescription {
307 $lxdebug->enter_sub();
309 $form->{title} = $locale->text("Enter longdescription");
311 print $form->parse_html_template("generic/set_longdescription");
313 $lxdebug->leave_sub();
316 # -------------------------------------------------------------------------
319 return $form->quote_html($_[0]);
323 return $form->quote($_[0]);
327 return $form->escape($_[0]);
333 $element =~ s/tabindex\s*=\s*"\d+"//;
338 $lxdebug->enter_sub();
340 my ($dateformat, $longformat, @indices) = @_;
342 $dateformat = $myconfig{"dateformat"} unless ($dateformat);
344 foreach my $idx (@indices) {
345 next unless (defined($form->{$idx}));
347 if (!ref($form->{$idx})) {
348 $form->{$idx} = $locale->reformat_date(\%myconfig, $form->{$idx},
349 $dateformat, $longformat);
351 } elsif (ref($form->{$idx}) eq "ARRAY") {
352 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
353 $form->{$idx}->[$i] =
354 $locale->reformat_date(\%myconfig, $form->{$idx}->[$i],
355 $dateformat, $longformat);
360 $lxdebug->leave_sub();
363 sub reformat_numbers {
364 $lxdebug->enter_sub();
366 my ($numberformat, $places, @indices) = @_;
368 return $lxdebug->leave_sub()
369 if (!$numberformat || ($numberformat eq $myconfig{"numberformat"}));
371 foreach my $idx (@indices) {
372 next unless (defined($form->{$idx}));
374 if (!ref($form->{$idx})) {
375 $form->{$idx} = $form->parse_amount(\%myconfig, $form->{$idx});
377 } elsif (ref($form->{$idx}) eq "ARRAY") {
378 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
379 $form->{$idx}->[$i] =
380 $form->parse_amount(\%myconfig, $form->{$idx}->[$i]);
385 my $saved_numberformat = $myconfig{"numberformat"};
386 $myconfig{"numberformat"} = $numberformat;
388 foreach my $idx (@indices) {
389 next unless (defined($form->{$idx}));
391 if (!ref($form->{$idx})) {
392 $form->{$idx} = $form->format_amount(\%myconfig, $form->{$idx}, $places);
394 } elsif (ref($form->{$idx}) eq "ARRAY") {
395 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
396 $form->{$idx}->[$i] =
397 $form->format_amount(\%myconfig, $form->{$idx}->[$i], $places);
402 $myconfig{"numberformat"} = $saved_numberformat;
404 $lxdebug->leave_sub();
407 # -------------------------------------------------------------------------
410 $lxdebug->enter_sub();
411 my $dbh = $form->dbconnect(\%myconfig);
412 my ($sort, $sortby) = split(/\-\-/, $form->{order});
413 $sort =~ s/.*\.(.*)/$1/;
415 $form->{title} = $locale->text("History");
417 print $form->parse_html_template( "common/show_history", {
418 "DATEN" => $form->get_history($dbh,$form->{input_name},"",$form->{order}),
419 "SUCCESS" => ($form->get_history($dbh,$form->{input_name}) ne "0"),
421 uc($sort)."BY" => $sortby
425 $lxdebug->leave_sub();
428 # -------------------------------------------------------------------------
431 $lxdebug->enter_sub();
436 $form->error($locale->text("Trying to call a sub without a name"));
439 $name =~ s/[^a-zA-Z0-9_]//g;
441 if (!defined(&{ $name })) {
442 $form->error(sprintf($locale->text("Attempt to call an undefined sub named '%s'"), $name));
447 $lxdebug->leave_sub();
450 # -------------------------------------------------------------------------
452 sub show_vc_details {
453 $lxdebug->enter_sub();
455 $form->{vc} = $form->{vc} eq "customer" ? "customer" : "vendor";
456 $form->isblank("vc_id",
457 $form->{vc} eq "customer" ?
458 $locale->text("No customer has been selected yet.") :
459 $locale->text("No vendor has been selected yet."));
461 Common->get_vc_details(\%myconfig, $form, $form->{vc}, $form->{vc_id});
463 $form->{title} = $form->{vc} eq "customer" ?
464 $locale->text("Customer details") : $locale->text("Vendor details");
466 print $form->parse_html_template("common/show_vc_details", { "is_customer" => $form->{vc} eq "customer" });
468 $lxdebug->leave_sub();
471 # -------------------------------------------------------------------------
473 sub retrieve_partunits {
474 $lxdebug->enter_sub();
476 my @part_ids = grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount});
479 my %partunits = IO->retrieve_partunits('part_ids' => \@part_ids);
481 foreach my $i (1..$form->{rowcount}) {
482 next unless ($form->{"id_${i}"});
483 $form->{"partunit_${i}"} = $partunits{$form->{"id_${i}"}};
487 $lxdebug->leave_sub();
490 # -------------------------------------------------------------------------
492 sub mark_as_paid_common {
493 $lxdebug->enter_sub();
495 my ($myconfig, $db_name) = @_;
497 if($form->{mark_as_paid}) {
498 my $dbh ||= $form->get_standard_dbh($myconfig);
499 my $query = qq|UPDATE $db_name SET paid = amount WHERE id = ?|;
500 do_query($form, $dbh, $query, $form->{id});
502 $form->redirect($locale->text("Marked as paid"));
505 my $referer = $ENV{HTTP_REFERER};
506 $referer =~ s/^(.*)action\=.*\&(.*)$/$1action\=mark_as_paid\&mark_as_paid\=1\&id\=$form->{id}\&$2/;
509 print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
510 print qq|<input type="button" value="|.$locale->text('yes').qq|" onclick="document.location.href='|.$referer.qq|'"> |;
511 print qq|<input type="button" value="|.$locale->text('no').qq|" onclick="javascript:history.back();">|;
512 print qq|</body></html>|;
515 $lxdebug->leave_sub();
518 sub cov_selection_internal {
519 $lxdebug->enter_sub();
522 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
524 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
526 my $type = $form->{"is_vendor"} ? $locale->text("vendor") : $locale->text("customer");
528 $covs = Common->retrieve_customers_or_vendors(\%myconfig, $form, $order_by, $order_dir, $form->{"is_vendor"}, $form->{"allow_both"});
529 map({ $covs->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$covs}));
530 if (0 == scalar(@{$covs})) {
531 $form->show_generic_information(sprintf($locale->text("No %s was found matching the search parameters."), $type));
532 } elsif (1 == scalar(@{$covs})) {
533 $onload = "cov_selected('1')";
536 my $callback = "$form->{script}?action=cov_selection_internal&";
537 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
538 (qw(name input_name input_id is_vendor allow_both), grep({ /^[fl]_/ } keys %$form)));
540 my @header_sort = qw(name address contact);
541 my %header_title = ( "name" => $locale->text("Name"),
542 "address" => $locale->text("Address"),
543 "contact" => $locale->text("Contact"),
547 map(+{ "column_title" => $header_title{$_},
549 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
553 foreach my $cov (@{ $covs }) {
554 $cov->{address} = "$cov->{street}, $cov->{zipcode} $cov->{city}";
555 $cov->{address} =~ s{^,}{}x;
556 $cov->{address} =~ s{\ +}{\ }gx;
558 $cov->{contact} = join " ", map { $cov->{$_} } qw(cp_greeting cp_title cp_givenname cp_name);
559 $cov->{contact} =~ s{\ +}{\ }gx;
562 $form->{"title"} = $form->{is_vendor} ? $locale->text("Select a vendor") : $locale->text("Select a customer");
564 print($form->parse_html_template("generic/cov_selection", { "HEADER" => \@header,
566 "onload" => $onload }));
568 $lxdebug->leave_sub();