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 ######################################################################
14 use SL::DB::Helper::Mappings;
18 use SL::Helper::Flash;
23 $main::lxdebug->enter_sub(2);
25 my $form = $main::form;
27 my $script = $form->{script};
31 foreach my $key (@_) {
34 if ($key =~ /(.*?)=(.*)/) {
42 foreach my $var ($form->flatten_variables($key)) {
43 push @parts, E($var->{key}) . '=' . E($var->{value});
48 my $url = "${script}?" . join('&', @parts);
50 $main::lxdebug->leave_sub(2);
55 # -------------------------------------------------------------------------
58 $main::lxdebug->enter_sub();
60 my ($callback_sub, @parts) = @_;
62 my $form = $main::form;
63 my $locale = $main::locale;
65 my $remap_parts_id = 0;
66 if (defined($parts[0]->{parts_id}) && !defined($parts[0]->{id})) {
68 map { $_->{id} = $_->{parts_id}; } @parts;
71 my $remap_partnumber = 0;
72 if (defined($parts[0]->{partnumber}) && !defined($parts[0]->{number})) {
73 $remap_partnumber = 1;
74 map { $_->{number} = $_->{partnumber}; } @parts;
78 if (defined($parts[0]->{chargenumber})) {
80 map { $_->{has_charge} = 1; } @parts;
82 my $has_bestbefore = 0;
83 if (defined($parts[0]->{bestbefore})) {
85 map { $_->{has_bestbefore} = 1; } @parts;
88 if (defined($parts[0]->{ean})) {
90 map { $_->{has_ean} = 1; } @parts;
93 my $old_form = save_form();
96 print $form->parse_html_template("generic/select_part",
98 "old_form" => $old_form,
99 "title" => $locale->text("Select a part"),
100 "nextsub" => "select_part_internal",
101 "callback_sub" => $callback_sub,
102 "has_charge" => $has_charge,
103 "has_bestbefore" => $has_bestbefore,
104 "has_ean" => $has_ean,
105 "remap_parts_id" => $remap_parts_id,
106 "remap_partnumber" => $remap_partnumber });
108 $main::lxdebug->leave_sub();
111 sub select_part_internal {
112 $main::lxdebug->enter_sub();
114 my $form = $main::form;
116 my ($new_item, $callback_sub);
118 my $re = "^new_.*_$form->{selection}\$";
120 foreach (grep /$re/, keys %{ $form }) {
122 $new_key =~ s/^new_//;
123 $new_key =~ s/_\d+$//;
124 $new_item->{$new_key} = $form->{$_};
127 if ($form->{remap_parts_id}) {
128 $new_item->{parts_id} = $new_item->{id};
129 delete $new_item->{id};
132 if ($form->{remap_partnumber}) {
133 $new_item->{partnumber} = $new_item->{number};
134 delete $new_item->{number};
137 $callback_sub = $form->{callback_sub};
139 restore_form($form->{old_form});
141 call_sub($callback_sub, $new_item);
143 $main::lxdebug->leave_sub();
146 sub part_selection_internal {
147 $main::lxdebug->enter_sub();
149 my $form = $main::form;
150 my %myconfig = %main::myconfig;
151 my $locale = $main::locale;
153 my $order_by = "description";
154 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
156 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
160 foreach my $opt (split m/:/, $form->{options}) {
162 my ($key, $value) = split m/=/, $opt, 2;
163 $options{$key} = $value;
170 map { $form->{$_} = $options{$_} if ($options{$_}) } qw(no_services no_assemblies assemblies click_button);
172 my $parts = Common->retrieve_parts(\%myconfig, $form, $order_by, $order_dir);
175 if (0 == scalar(@{$parts})) {
176 $form->show_generic_information($locale->text("No part was found matching the search parameters."));
177 } elsif (1 == scalar(@{$parts})) {
178 $onload = "part_selected('1')";
181 map { $parts->[$_]->{selected} = $_ ? 0 : 1; } (0..$#{$parts});
183 my $callback = build_std_url('action=part_selection_internal', qw(partnumber description input_partnumber input_description input_partsid),
184 grep({ /^[fl]_/ } keys %{ $form }));
186 my @header_sort = qw(partnumber description);
187 my %header_title = ( "partnumber" => $locale->text("Part Number"),
188 "description" => $locale->text("Part Description"),
192 map(+{ "column_title" => $header_title{$_},
194 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
198 $form->{formname} ||= 'Form';
200 $form->{title} = $locale->text("Select a part");
202 print $form->parse_html_template("generic/part_selection", { "HEADER" => \@header,
204 "onload" => $onload });
206 $main::lxdebug->leave_sub();
209 # -------------------------------------------------------------------------
211 sub delivery_customer_selection {
212 $main::lxdebug->enter_sub();
214 my $form = $main::form;
215 my %myconfig = %main::myconfig;
216 my $locale = $main::locale;
218 my $order_by = "name";
219 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
221 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
223 my $delivery = Common->retrieve_delivery_customer(\%myconfig, $form, $order_by, $order_dir);
224 map({ $delivery->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$delivery}));
227 if (0 == scalar(@{$delivery})) {
228 $form->show_generic_information($locale->text("No Customer was found matching the search parameters."));
229 } elsif (1 == scalar(@{$delivery})) {
230 $onload = "customer_selected('1')";
233 my $callback = "$form->{script}?action=delivery_customer_selection&";
234 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
235 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
237 my @header_sort = qw(name customernumber address);
238 my %header_title = ( "name" => $locale->text("Name"),
239 "customernumber" => $locale->text("Customer Number"),
240 "address" => $locale->text("Address"),
244 map(+{ "column_title" => $header_title{$_},
246 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
250 $form->{"title"} = $locale->text("Select a Customer");
252 print $form->parse_html_template("generic/select_delivery_customer", { "HEADER" => \@header,
253 "DELIVERY" => $delivery,
254 "onload" => $onload });
256 $main::lxdebug->leave_sub();
259 # -------------------------------------------------------------------------
261 sub vendor_selection {
262 $main::lxdebug->enter_sub();
264 my $form = $main::form;
265 my %myconfig = %main::myconfig;
266 my $locale = $main::locale;
268 my $order_by = "name";
269 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
271 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
273 my $vendor = Common->retrieve_vendor(\%myconfig, $form, $order_by, $order_dir);
274 map({ $vendor->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$vendor}));
277 if (0 == scalar(@{$vendor})) {
278 $form->show_generic_information($locale->text("No Vendor was found matching the search parameters."));
279 } elsif (1 == scalar(@{$vendor})) {
280 $onload = "vendor_selected('1')";
283 my $callback = "$form->{script}?action=vendor_selection&";
284 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
285 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
287 my @header_sort = qw(name customernumber address);
288 my %header_title = ( "name" => $locale->text("Name"),
289 "customernumber" => $locale->text("Customer Number"),
290 "address" => $locale->text("Address"),
294 map(+{ "column_title" => $header_title{$_},
296 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
300 $form->{"title"} = $locale->text("Select a Customer");
302 print $form->parse_html_template("generic/select_vendor", { "HEADER" => \@header,
304 "onload" => $onload });
306 $main::lxdebug->leave_sub();
309 # -------------------------------------------------------------------------
312 $main::lxdebug->enter_sub();
314 my $form = $main::form;
315 my $locale = $main::locale;
317 $form->{formel} =~ s/\r\n//g;
319 my ($variable_string, $formel) = split /###/,$form->{formel};
321 my $onload; # note! this sub is mostly called over a javascript invocation, and it's unlikey that onload is set.
323 foreach my $item (split m/;/, $variable_string) {
324 next unless $item =~ m/^ \s* (\w+) \s* = \s* (\w+) \s* (\w+) \s* $/x;
332 my @header_sort = qw(variable value unit);
334 variable => $locale->text("Variable"),
335 value => $locale->text("Value"),
336 unit => $locale->text("Unit"),
339 column_title => $header_title{$_},
343 $form->{formel} = $formel;
344 $form->{title} = $locale->text("Please enter values");
346 print $form->parse_html_template("generic/calculate_qty", { "HEADER" => \@header,
347 "VARIABLES" => \@variable,
348 "onload" => $onload });
350 $main::lxdebug->leave_sub();
353 # -------------------------------------------------------------------------
355 sub set_longdescription {
356 $main::lxdebug->enter_sub();
358 my $form = $main::form;
359 my $locale = $main::locale;
361 $form->{title} = $locale->text("Enter longdescription");
363 print $form->parse_html_template("generic/set_longdescription");
365 $main::lxdebug->leave_sub();
368 # -------------------------------------------------------------------------
371 return $main::locale->quote_special_chars('HTML', $_[0]);
375 return $main::locale->quote_special_chars('URL@HTML', $_[0]);
379 return $main::form->escape($_[0]);
385 $element =~ s/tabindex\s*=\s*"\d+"//;
390 $main::lxdebug->enter_sub();
392 my ($dateformat, $longformat, @indices) = @_;
394 my $form = $main::form;
395 my %myconfig = %main::myconfig;
396 my $locale = $main::locale;
398 $dateformat = $myconfig{"dateformat"} unless ($dateformat);
400 foreach my $idx (@indices) {
401 if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
402 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
403 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] =
404 $locale->reformat_date(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i],
405 $dateformat, $longformat);
409 next unless (defined($form->{$idx}));
411 if (!ref($form->{$idx})) {
412 $form->{$idx} = $locale->reformat_date(\%myconfig, $form->{$idx},
413 $dateformat, $longformat);
415 } elsif (ref($form->{$idx}) eq "ARRAY") {
416 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
417 $form->{$idx}->[$i] =
418 $locale->reformat_date(\%myconfig, $form->{$idx}->[$i],
419 $dateformat, $longformat);
424 $main::lxdebug->leave_sub();
427 sub reformat_numbers {
428 $main::lxdebug->enter_sub();
430 my ($numberformat, $places, @indices) = @_;
432 my $form = $main::form;
433 my %myconfig = %main::myconfig;
435 return $main::lxdebug->leave_sub()
436 if (!$numberformat || ($numberformat eq $myconfig{"numberformat"}));
438 foreach my $idx (@indices) {
439 if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
440 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
441 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $form->parse_amount(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i]);
445 next unless (defined($form->{$idx}));
447 if (!ref($form->{$idx})) {
448 $form->{$idx} = $form->parse_amount(\%myconfig, $form->{$idx});
450 } elsif (ref($form->{$idx}) eq "ARRAY") {
451 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
452 $form->{$idx}->[$i] =
453 $form->parse_amount(\%myconfig, $form->{$idx}->[$i]);
458 my $saved_numberformat = $myconfig{"numberformat"};
459 $myconfig{"numberformat"} = $numberformat;
461 foreach my $idx (@indices) {
462 if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
463 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
464 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $form->format_amount(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i], $places);
468 next unless (defined($form->{$idx}));
470 if (!ref($form->{$idx})) {
471 $form->{$idx} = $form->format_amount(\%myconfig, $form->{$idx}, $places);
473 } elsif (ref($form->{$idx}) eq "ARRAY") {
474 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
475 $form->{$idx}->[$i] =
476 $form->format_amount(\%myconfig, $form->{$idx}->[$i], $places);
481 $myconfig{"numberformat"} = $saved_numberformat;
483 $main::lxdebug->leave_sub();
486 # -------------------------------------------------------------------------
489 $main::lxdebug->enter_sub();
491 my $form = $main::form;
492 my %myconfig = %main::myconfig;
493 my $locale = $main::locale;
495 my $dbh = $form->dbconnect(\%myconfig);
496 my ($sort, $sortby) = split(/\-\-/, $form->{order});
497 $sort =~ s/.*\.(.*)/$1/;
499 $form->{title} = $locale->text("History");
501 print $form->parse_html_template( "common/show_history", {
502 "DATEN" => $form->get_history($dbh,$form->{input_name},"",$form->{order}),
503 "SUCCESS" => ($form->get_history($dbh,$form->{input_name}) ne "0"),
505 uc($sort)."BY" => $sortby
509 $main::lxdebug->leave_sub();
512 # -------------------------------------------------------------------------
515 $main::lxdebug->enter_sub();
519 my $form = $main::form;
520 my $locale = $main::locale;
523 $form->error($locale->text("Trying to call a sub without a name"));
526 $name =~ s/[^a-zA-Z0-9_]//g;
528 if (!defined(&{ $name })) {
529 $form->error(sprintf($locale->text("Attempt to call an undefined sub named '%s'"), $name));
532 $::called_subs{$name}++;
533 confess "RECURSION DETECTION: call_sub($name) called " . $::called_subs{$name} . " time(s)" if $::called_subs{$name} > 10;
540 $main::lxdebug->leave_sub();
543 # -------------------------------------------------------------------------
545 sub show_vc_details {
546 $main::lxdebug->enter_sub();
548 my $form = $main::form;
549 my %myconfig = %main::myconfig;
550 my $locale = $main::locale;
552 $form->{vc} = $form->{vc} eq "customer" ? "customer" : "vendor";
553 $form->isblank("vc_id",
554 $form->{vc} eq "customer" ?
555 $locale->text("No customer has been selected yet.") :
556 $locale->text("No vendor has been selected yet."));
558 Common->get_vc_details(\%myconfig, $form, $form->{vc}, $form->{vc_id});
560 $form->{title} = $form->{vc} eq "customer" ?
561 $locale->text("Customer details") : $locale->text("Vendor details");
563 print $form->parse_html_template("common/show_vc_details", { "is_customer" => $form->{vc} eq "customer" });
565 $main::lxdebug->leave_sub();
568 # -------------------------------------------------------------------------
570 sub retrieve_partunits {
571 $main::lxdebug->enter_sub();
573 my $form = $main::form;
575 my @part_ids = grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount});
578 my %partunits = IO->retrieve_partunits('part_ids' => \@part_ids);
580 foreach my $i (1..$form->{rowcount}) {
581 next unless ($form->{"id_${i}"});
582 $form->{"partunit_${i}"} = $partunits{$form->{"id_${i}"}};
586 $main::lxdebug->leave_sub();
589 # -------------------------------------------------------------------------
591 sub mark_as_paid_common {
592 $main::lxdebug->enter_sub();
594 my ($myconfig, $db_name) = @_;
596 my $form = $main::form;
597 my $locale = $main::locale;
599 if($form->{mark_as_paid}) {
600 my $dbh ||= $form->get_standard_dbh($myconfig);
601 my $query = qq|UPDATE $db_name SET paid = amount, datepaid = current_date WHERE id = ?|;
602 do_query($form, $dbh, $query, $form->{id});
604 $form->redirect($locale->text("Marked as paid"));
607 my $referer = $ENV{HTTP_REFERER};
610 if ($referer =~ /action/) {
611 $referer =~ /^(.*)\?action\=[^\&]*(\&.*)$/;
618 $referer = $script . "?action=mark_as_paid&mark_as_paid=1&id=$form->{id}" . $callback;
621 print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
622 print qq|<input type="button" value="|.$locale->text('yes').qq|" onclick="document.location.href='|.$referer.qq|'"> |;
623 print qq|<input type="button" value="|.$locale->text('no').qq|" onclick="javascript:history.back();">|;
624 print qq|</body></html>|;
627 $main::lxdebug->leave_sub();
630 sub cov_selection_internal {
631 $main::lxdebug->enter_sub();
633 my $form = $main::form;
634 my %myconfig = %main::myconfig;
635 my $locale = $main::locale;
637 my $order_by = "name";
638 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
640 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
642 my $type = $form->{"is_vendor"} ? $locale->text("vendor") : $locale->text("customer");
644 my $covs = Common->retrieve_customers_or_vendors(\%myconfig, $form, $order_by, $order_dir, $form->{"is_vendor"}, $form->{"allow_both"});
645 map({ $covs->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$covs}));
648 if (0 == scalar(@{$covs})) {
649 $form->show_generic_information(sprintf($locale->text("No %s was found matching the search parameters."), $type));
650 } elsif (1 == scalar(@{$covs})) {
651 $onload = "cov_selected('1')";
654 my $callback = "$form->{script}?action=cov_selection_internal&";
655 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
656 (qw(name input_name input_id is_vendor allow_both), grep({ /^[fl]_/ } keys %$form)));
658 my @header_sort = qw(name address contact);
659 my %header_title = ( "name" => $locale->text("Name"),
660 "address" => $locale->text("Address"),
661 "contact" => $locale->text("Contact"),
665 map(+{ "column_title" => $header_title{$_},
667 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
671 foreach my $cov (@{ $covs }) {
672 $cov->{address} = "$cov->{street}, $cov->{zipcode} $cov->{city}";
673 $cov->{address} =~ s{^,}{}x;
674 $cov->{address} =~ s{\ +}{\ }gx;
676 $cov->{contact} = join " ", map { $cov->{$_} } qw(cp_gender cp_title cp_givenname cp_name);
677 $cov->{contact} =~ s{\ +}{\ }gx;
680 $form->{"title"} = $form->{is_vendor} ? $locale->text("Select a vendor") : $locale->text("Select a customer");
682 print($form->parse_html_template("generic/cov_selection", { "HEADER" => \@header,
684 "onload" => $onload }));
686 $main::lxdebug->leave_sub();
690 # Functions to call add routines beneath different reports
693 $main::lxdebug->enter_sub();
695 print $::form->redirect_header('is.pl?action=add&type=invoice');
697 $main::lxdebug->leave_sub();
701 $main::lxdebug->enter_sub();
703 print $::form->redirect_header('ar.pl?action=add');
705 $main::lxdebug->leave_sub();
709 $main::lxdebug->enter_sub();
711 print $::form->redirect_header('ir.pl?action=add&type=invoice');
713 $main::lxdebug->leave_sub();
717 $main::lxdebug->enter_sub();
719 print $::form->redirect_header('ap.pl?action=add');
721 $main::lxdebug->leave_sub();
725 $main::lxdebug->enter_sub();
727 print $::form->redirect_header('gl.pl?action=add');
729 $main::lxdebug->leave_sub();
733 goto &SL::DB::Helper::Mappings::db;