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"}));
132 foreach my $opt (split m/:/, $form->{options}) {
134 my ($key, $value) = split m/=/, $opt, 2;
135 $options{$key} = $value;
142 map { $form->{$_} = $options{$_} if ($options{$_}) } qw(no_services no_assemblies assemblies click_button);
144 $parts = Common->retrieve_parts(\%myconfig, $form, $order_by, $order_dir);
146 if (0 == scalar(@{$parts})) {
147 $form->show_generic_information($locale->text("No part was found matching the search parameters."));
148 } elsif (1 == scalar(@{$parts})) {
149 $onload = "part_selected('1')";
152 map { $parts->[$_]->{selected} = $_ ? 0 : 1; } (0..$#{$parts});
154 my $callback = build_std_url('action=part_selection_internal', qw(partnumber description input_partnumber input_description input_partsid),
155 grep({ /^[fl]_/ } keys %{ $form }));
157 my @header_sort = qw(partnumber description);
158 my %header_title = ( "partnumber" => $locale->text("Part Number"),
159 "description" => $locale->text("Part description"),
163 map(+{ "column_title" => $header_title{$_},
165 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
169 $form->{formname} ||= 'Form';
171 $form->{title} = $locale->text("Select a part");
173 print $form->parse_html_template("generic/part_selection", { "HEADER" => \@header,
175 "onload" => $onload });
177 $lxdebug->leave_sub();
180 # -------------------------------------------------------------------------
182 sub delivery_customer_selection {
183 $lxdebug->enter_sub();
186 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
188 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
190 $delivery = Common->retrieve_delivery_customer(\%myconfig, $form, $order_by, $order_dir);
191 map({ $delivery->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$delivery}));
192 if (0 == scalar(@{$delivery})) {
193 $form->show_generic_information($locale->text("No Customer was found matching the search parameters."));
194 } elsif (1 == scalar(@{$delivery})) {
195 $onload = "customer_selected('1')";
198 my $callback = "$form->{script}?action=delivery_customer_selection&";
199 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
200 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
202 my @header_sort = qw(name customernumber address);
203 my %header_title = ( "name" => $locale->text("Name"),
204 "customernumber" => $locale->text("Customer Number"),
205 "address" => $locale->text("Address"),
209 map(+{ "column_title" => $header_title{$_},
211 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
215 $form->{"title"} = $locale->text("Select a Customer");
217 print $form->parse_html_template("generic/select_delivery_customer", { "HEADER" => \@header,
218 "DELIVERY" => $delivery,
219 "onload" => $onload });
221 $lxdebug->leave_sub();
224 # -------------------------------------------------------------------------
226 sub vendor_selection {
227 $lxdebug->enter_sub();
230 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
232 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
234 $vendor = Common->retrieve_vendor(\%myconfig, $form, $order_by, $order_dir);
235 map({ $vendor->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$vendor}));
236 if (0 == scalar(@{$vendor})) {
237 $form->show_generic_information($locale->text("No Vendor was found matching the search parameters."));
238 } elsif (1 == scalar(@{$vendor})) {
239 $onload = "vendor_selected('1')";
242 my $callback = "$form->{script}?action=vendor_selection&";
243 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
244 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
246 my @header_sort = qw(name customernumber address);
247 my %header_title = ( "name" => $locale->text("Name"),
248 "customernumber" => $locale->text("Customer Number"),
249 "address" => $locale->text("Address"),
253 map(+{ "column_title" => $header_title{$_},
255 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
259 $form->{"title"} = $locale->text("Select a Customer");
261 print $form->parse_html_template("generic/select_vendor", { "HEADER" => \@header,
263 "onload" => $onload });
265 $lxdebug->leave_sub();
268 # -------------------------------------------------------------------------
271 $lxdebug->enter_sub();
273 my @variable_sort = ();
274 my %variable_list = ();
276 $form->{formel} =~ s/\r\n//g;
278 my ($variable_string, $formel) = split /###/,$form->{formel};
281 split m/;/, $variable_string;
283 my($name, $valueunit) = split /=/,$item;
284 my($value, $unit) = split / /, $valueunit;
286 push(@variable_sort, $value);
287 $variable_list{$value} = $name;
288 $unit_list{$value} = $unit;
291 my @header_sort = qw(variable value unit);
292 my %header_title = ( "variable" => $locale->text("Variable"),
293 "value" => $locale->text("Value"),
294 "unit" => $locale->text("Unit"),
297 my @variable = map(+{ "description" => $variable_list{$_},
299 "unit" => $unit_list{$_} }, @variable_sort);
302 map(+{ "column_title" => $header_title{$_},
306 $form->{formel} = $formel;
307 $form->{"title"} = $locale->text("Please enter values");
309 print($form->parse_html_template("generic/calculate_qty", { "HEADER" => \@header,
310 "VARIABLES" => \@variable,
311 "onload" => $onload }));
313 $lxdebug->leave_sub();
316 # -------------------------------------------------------------------------
318 sub set_longdescription {
319 $lxdebug->enter_sub();
321 $form->{title} = $locale->text("Enter longdescription");
323 print $form->parse_html_template("generic/set_longdescription");
325 $lxdebug->leave_sub();
328 # -------------------------------------------------------------------------
331 return $locale->quote_special_chars('HTML', $_[0]);
335 return $locale->quote_special_chars('URL@HTML', $_[0]);
339 return $form->escape($_[0]);
345 $element =~ s/tabindex\s*=\s*"\d+"//;
350 $lxdebug->enter_sub();
352 my ($dateformat, $longformat, @indices) = @_;
354 $dateformat = $myconfig{"dateformat"} unless ($dateformat);
356 foreach my $idx (@indices) {
357 next unless (defined($form->{$idx}));
359 if (!ref($form->{$idx})) {
360 $form->{$idx} = $locale->reformat_date(\%myconfig, $form->{$idx},
361 $dateformat, $longformat);
363 } elsif (ref($form->{$idx}) eq "ARRAY") {
364 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
365 $form->{$idx}->[$i] =
366 $locale->reformat_date(\%myconfig, $form->{$idx}->[$i],
367 $dateformat, $longformat);
372 $lxdebug->leave_sub();
375 sub reformat_numbers {
376 $lxdebug->enter_sub();
378 my ($numberformat, $places, @indices) = @_;
380 return $lxdebug->leave_sub()
381 if (!$numberformat || ($numberformat eq $myconfig{"numberformat"}));
383 foreach my $idx (@indices) {
384 next unless (defined($form->{$idx}));
386 if (!ref($form->{$idx})) {
387 $form->{$idx} = $form->parse_amount(\%myconfig, $form->{$idx});
389 } elsif (ref($form->{$idx}) eq "ARRAY") {
390 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
391 $form->{$idx}->[$i] =
392 $form->parse_amount(\%myconfig, $form->{$idx}->[$i]);
397 my $saved_numberformat = $myconfig{"numberformat"};
398 $myconfig{"numberformat"} = $numberformat;
400 foreach my $idx (@indices) {
401 next unless (defined($form->{$idx}));
403 if (!ref($form->{$idx})) {
404 $form->{$idx} = $form->format_amount(\%myconfig, $form->{$idx}, $places);
406 } elsif (ref($form->{$idx}) eq "ARRAY") {
407 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
408 $form->{$idx}->[$i] =
409 $form->format_amount(\%myconfig, $form->{$idx}->[$i], $places);
414 $myconfig{"numberformat"} = $saved_numberformat;
416 $lxdebug->leave_sub();
419 # -------------------------------------------------------------------------
422 $lxdebug->enter_sub();
423 my $dbh = $form->dbconnect(\%myconfig);
424 my ($sort, $sortby) = split(/\-\-/, $form->{order});
425 $sort =~ s/.*\.(.*)/$1/;
427 $form->{title} = $locale->text("History");
429 print $form->parse_html_template( "common/show_history", {
430 "DATEN" => $form->get_history($dbh,$form->{input_name},"",$form->{order}),
431 "SUCCESS" => ($form->get_history($dbh,$form->{input_name}) ne "0"),
433 uc($sort)."BY" => $sortby
437 $lxdebug->leave_sub();
440 # -------------------------------------------------------------------------
443 $lxdebug->enter_sub();
448 $form->error($locale->text("Trying to call a sub without a name"));
451 $name =~ s/[^a-zA-Z0-9_]//g;
453 if (!defined(&{ $name })) {
454 $form->error(sprintf($locale->text("Attempt to call an undefined sub named '%s'"), $name));
459 $lxdebug->leave_sub();
462 # -------------------------------------------------------------------------
464 sub show_vc_details {
465 $lxdebug->enter_sub();
467 $form->{vc} = $form->{vc} eq "customer" ? "customer" : "vendor";
468 $form->isblank("vc_id",
469 $form->{vc} eq "customer" ?
470 $locale->text("No customer has been selected yet.") :
471 $locale->text("No vendor has been selected yet."));
473 Common->get_vc_details(\%myconfig, $form, $form->{vc}, $form->{vc_id});
475 $form->{title} = $form->{vc} eq "customer" ?
476 $locale->text("Customer details") : $locale->text("Vendor details");
478 print $form->parse_html_template("common/show_vc_details", { "is_customer" => $form->{vc} eq "customer" });
480 $lxdebug->leave_sub();
483 # -------------------------------------------------------------------------
485 sub retrieve_partunits {
486 $lxdebug->enter_sub();
488 my @part_ids = grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount});
491 my %partunits = IO->retrieve_partunits('part_ids' => \@part_ids);
493 foreach my $i (1..$form->{rowcount}) {
494 next unless ($form->{"id_${i}"});
495 $form->{"partunit_${i}"} = $partunits{$form->{"id_${i}"}};
499 $lxdebug->leave_sub();
502 # -------------------------------------------------------------------------
504 sub mark_as_paid_common {
505 $lxdebug->enter_sub();
507 my ($myconfig, $db_name) = @_;
509 if($form->{mark_as_paid}) {
510 my $dbh ||= $form->get_standard_dbh($myconfig);
511 my $query = qq|UPDATE $db_name SET paid = amount WHERE id = ?|;
512 do_query($form, $dbh, $query, $form->{id});
514 $form->redirect($locale->text("Marked as paid"));
517 my $referer = $ENV{HTTP_REFERER};
518 $referer =~ s/^(.*)action\=.*\&(.*)$/$1action\=mark_as_paid\&mark_as_paid\=1\&id\=$form->{id}\&$2/;
521 print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
522 print qq|<input type="button" value="|.$locale->text('yes').qq|" onclick="document.location.href='|.$referer.qq|'"> |;
523 print qq|<input type="button" value="|.$locale->text('no').qq|" onclick="javascript:history.back();">|;
524 print qq|</body></html>|;
527 $lxdebug->leave_sub();
530 sub cov_selection_internal {
531 $lxdebug->enter_sub();
534 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
536 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
538 my $type = $form->{"is_vendor"} ? $locale->text("vendor") : $locale->text("customer");
540 $covs = Common->retrieve_customers_or_vendors(\%myconfig, $form, $order_by, $order_dir, $form->{"is_vendor"}, $form->{"allow_both"});
541 map({ $covs->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$covs}));
542 if (0 == scalar(@{$covs})) {
543 $form->show_generic_information(sprintf($locale->text("No %s was found matching the search parameters."), $type));
544 } elsif (1 == scalar(@{$covs})) {
545 $onload = "cov_selected('1')";
548 my $callback = "$form->{script}?action=cov_selection_internal&";
549 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
550 (qw(name input_name input_id is_vendor allow_both), grep({ /^[fl]_/ } keys %$form)));
552 my @header_sort = qw(name address contact);
553 my %header_title = ( "name" => $locale->text("Name"),
554 "address" => $locale->text("Address"),
555 "contact" => $locale->text("Contact"),
559 map(+{ "column_title" => $header_title{$_},
561 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
565 foreach my $cov (@{ $covs }) {
566 $cov->{address} = "$cov->{street}, $cov->{zipcode} $cov->{city}";
567 $cov->{address} =~ s{^,}{}x;
568 $cov->{address} =~ s{\ +}{\ }gx;
570 $cov->{contact} = join " ", map { $cov->{$_} } qw(cp_greeting cp_title cp_givenname cp_name);
571 $cov->{contact} =~ s{\ +}{\ }gx;
574 $form->{"title"} = $form->{is_vendor} ? $locale->text("Select a vendor") : $locale->text("Select a customer");
576 print($form->parse_html_template("generic/cov_selection", { "HEADER" => \@header,
578 "onload" => $onload }));
580 $lxdebug->leave_sub();