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 ######################################################################
21 $main::lxdebug->enter_sub(2);
23 my $form = $main::form;
25 my $script = $form->{script};
29 foreach my $key (@_) {
32 if ($key =~ /(.*?)=(.*)/) {
40 foreach my $var ($form->flatten_variables($key)) {
41 push @parts, E($var->{key}) . '=' . E($var->{value});
46 my $url = "${script}?" . join('&', @parts);
48 $main::lxdebug->leave_sub(2);
53 # -------------------------------------------------------------------------
56 $main::lxdebug->enter_sub();
58 my ($callback_sub, @parts) = @_;
60 my $form = $main::form;
61 my $locale = $main::locale;
63 my $remap_parts_id = 0;
64 if (defined($parts[0]->{parts_id}) && !defined($parts[0]->{id})) {
66 map { $_->{id} = $_->{parts_id}; } @parts;
69 my $remap_partnumber = 0;
70 if (defined($parts[0]->{partnumber}) && !defined($parts[0]->{number})) {
71 $remap_partnumber = 1;
72 map { $_->{number} = $_->{partnumber}; } @parts;
76 if (defined($parts[0]->{chargenumber})) {
78 map { $_->{has_charge} = 1; } @parts;
80 my $has_bestbefore = 0;
81 if (defined($parts[0]->{bestbefore})) {
83 map { $_->{has_bestbefore} = 1; } @parts;
86 if (defined($parts[0]->{ean})) {
88 map { $_->{has_ean} = 1; } @parts;
91 my $old_form = save_form();
94 print $form->parse_html_template("generic/select_part",
96 "old_form" => $old_form,
97 "title" => $locale->text("Select a part"),
98 "nextsub" => "select_part_internal",
99 "callback_sub" => $callback_sub,
100 "has_charge" => $has_charge,
101 "has_bestbefore" => $has_bestbefore,
102 "has_ean" => $has_ean,
103 "remap_parts_id" => $remap_parts_id,
104 "remap_partnumber" => $remap_partnumber });
106 $main::lxdebug->leave_sub();
109 sub select_part_internal {
110 $main::lxdebug->enter_sub();
112 my $form = $main::form;
114 my ($new_item, $callback_sub);
116 my $re = "^new_.*_$form->{selection}\$";
118 foreach (grep /$re/, keys %{ $form }) {
120 $new_key =~ s/^new_//;
121 $new_key =~ s/_\d+$//;
122 $new_item->{$new_key} = $form->{$_};
125 if ($form->{remap_parts_id}) {
126 $new_item->{parts_id} = $new_item->{id};
127 delete $new_item->{id};
130 if ($form->{remap_partnumber}) {
131 $new_item->{partnumber} = $new_item->{number};
132 delete $new_item->{number};
135 $callback_sub = $form->{callback_sub};
137 restore_form($form->{old_form});
139 call_sub($callback_sub, $new_item);
141 $main::lxdebug->leave_sub();
144 sub part_selection_internal {
145 $main::lxdebug->enter_sub();
147 my $form = $main::form;
148 my %myconfig = %main::myconfig;
149 my $locale = $main::locale;
151 my $order_by = "description";
152 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
154 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
158 foreach my $opt (split m/:/, $form->{options}) {
160 my ($key, $value) = split m/=/, $opt, 2;
161 $options{$key} = $value;
168 map { $form->{$_} = $options{$_} if ($options{$_}) } qw(no_services no_assemblies assemblies click_button);
170 my $parts = Common->retrieve_parts(\%myconfig, $form, $order_by, $order_dir);
173 if (0 == scalar(@{$parts})) {
174 $form->show_generic_information($locale->text("No part was found matching the search parameters."));
175 } elsif (1 == scalar(@{$parts})) {
176 $onload = "part_selected('1')";
179 map { $parts->[$_]->{selected} = $_ ? 0 : 1; } (0..$#{$parts});
181 my $callback = build_std_url('action=part_selection_internal', qw(partnumber description input_partnumber input_description input_partsid),
182 grep({ /^[fl]_/ } keys %{ $form }));
184 my @header_sort = qw(partnumber description);
185 my %header_title = ( "partnumber" => $locale->text("Part Number"),
186 "description" => $locale->text("Part Description"),
190 map(+{ "column_title" => $header_title{$_},
192 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
196 $form->{formname} ||= 'Form';
198 $form->{title} = $locale->text("Select a part");
200 print $form->parse_html_template("generic/part_selection", { "HEADER" => \@header,
202 "onload" => $onload });
204 $main::lxdebug->leave_sub();
207 # -------------------------------------------------------------------------
209 sub delivery_customer_selection {
210 $main::lxdebug->enter_sub();
212 my $form = $main::form;
213 my %myconfig = %main::myconfig;
214 my $locale = $main::locale;
216 my $order_by = "name";
217 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
219 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
221 my $delivery = Common->retrieve_delivery_customer(\%myconfig, $form, $order_by, $order_dir);
222 map({ $delivery->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$delivery}));
225 if (0 == scalar(@{$delivery})) {
226 $form->show_generic_information($locale->text("No Customer was found matching the search parameters."));
227 } elsif (1 == scalar(@{$delivery})) {
228 $onload = "customer_selected('1')";
231 my $callback = "$form->{script}?action=delivery_customer_selection&";
232 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
233 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
235 my @header_sort = qw(name customernumber address);
236 my %header_title = ( "name" => $locale->text("Name"),
237 "customernumber" => $locale->text("Customer Number"),
238 "address" => $locale->text("Address"),
242 map(+{ "column_title" => $header_title{$_},
244 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
248 $form->{"title"} = $locale->text("Select a Customer");
250 print $form->parse_html_template("generic/select_delivery_customer", { "HEADER" => \@header,
251 "DELIVERY" => $delivery,
252 "onload" => $onload });
254 $main::lxdebug->leave_sub();
257 # -------------------------------------------------------------------------
259 sub vendor_selection {
260 $main::lxdebug->enter_sub();
262 my $form = $main::form;
263 my %myconfig = %main::myconfig;
264 my $locale = $main::locale;
266 my $order_by = "name";
267 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
269 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
271 my $vendor = Common->retrieve_vendor(\%myconfig, $form, $order_by, $order_dir);
272 map({ $vendor->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$vendor}));
275 if (0 == scalar(@{$vendor})) {
276 $form->show_generic_information($locale->text("No Vendor was found matching the search parameters."));
277 } elsif (1 == scalar(@{$vendor})) {
278 $onload = "vendor_selected('1')";
281 my $callback = "$form->{script}?action=vendor_selection&";
282 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
283 (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
285 my @header_sort = qw(name customernumber address);
286 my %header_title = ( "name" => $locale->text("Name"),
287 "customernumber" => $locale->text("Customer Number"),
288 "address" => $locale->text("Address"),
292 map(+{ "column_title" => $header_title{$_},
294 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
298 $form->{"title"} = $locale->text("Select a Customer");
300 print $form->parse_html_template("generic/select_vendor", { "HEADER" => \@header,
302 "onload" => $onload });
304 $main::lxdebug->leave_sub();
307 # -------------------------------------------------------------------------
310 $main::lxdebug->enter_sub();
312 my $form = $main::form;
313 my $locale = $main::locale;
315 $form->{formel} =~ s/\r\n//g;
317 my ($variable_string, $formel) = split /###/,$form->{formel};
319 my $onload; # note! this sub is mostly called over a javascript invocation, and it's unlikey that onload is set.
321 foreach my $item (split m/;/, $variable_string) {
322 next unless $item =~ m/^ \s* (\w+) \s* = \s* (\w+) \s* (\w+) \s* $/x;
330 my @header_sort = qw(variable value unit);
332 variable => $locale->text("Variable"),
333 value => $locale->text("Value"),
334 unit => $locale->text("Unit"),
337 column_title => $header_title{$_},
341 $form->{formel} = $formel;
342 $form->{title} = $locale->text("Please enter values");
344 print $form->parse_html_template("generic/calculate_qty", { "HEADER" => \@header,
345 "VARIABLES" => \@variable,
346 "onload" => $onload });
348 $main::lxdebug->leave_sub();
351 # -------------------------------------------------------------------------
353 sub set_longdescription {
354 $main::lxdebug->enter_sub();
356 my $form = $main::form;
357 my $locale = $main::locale;
359 $form->{title} = $locale->text("Enter longdescription");
361 print $form->parse_html_template("generic/set_longdescription");
363 $main::lxdebug->leave_sub();
366 # -------------------------------------------------------------------------
369 return $main::locale->quote_special_chars('HTML', $_[0]);
373 return $main::locale->quote_special_chars('URL@HTML', $_[0]);
377 return $main::form->escape($_[0]);
383 $element =~ s/tabindex\s*=\s*"\d+"//;
388 $main::lxdebug->enter_sub();
390 my ($dateformat, $longformat, @indices) = @_;
392 my $form = $main::form;
393 my %myconfig = %main::myconfig;
394 my $locale = $main::locale;
396 $dateformat = $myconfig{"dateformat"} unless ($dateformat);
398 foreach my $idx (@indices) {
399 if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
400 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
401 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] =
402 $locale->reformat_date(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i],
403 $dateformat, $longformat);
407 next unless (defined($form->{$idx}));
409 if (!ref($form->{$idx})) {
410 $form->{$idx} = $locale->reformat_date(\%myconfig, $form->{$idx},
411 $dateformat, $longformat);
413 } elsif (ref($form->{$idx}) eq "ARRAY") {
414 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
415 $form->{$idx}->[$i] =
416 $locale->reformat_date(\%myconfig, $form->{$idx}->[$i],
417 $dateformat, $longformat);
422 $main::lxdebug->leave_sub();
425 sub reformat_numbers {
426 $main::lxdebug->enter_sub();
428 my ($numberformat, $places, @indices) = @_;
430 my $form = $main::form;
431 my %myconfig = %main::myconfig;
433 return $main::lxdebug->leave_sub()
434 if (!$numberformat || ($numberformat eq $myconfig{"numberformat"}));
436 foreach my $idx (@indices) {
437 if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
438 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
439 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $form->parse_amount(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i]);
443 next unless (defined($form->{$idx}));
445 if (!ref($form->{$idx})) {
446 $form->{$idx} = $form->parse_amount(\%myconfig, $form->{$idx});
448 } elsif (ref($form->{$idx}) eq "ARRAY") {
449 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
450 $form->{$idx}->[$i] =
451 $form->parse_amount(\%myconfig, $form->{$idx}->[$i]);
456 my $saved_numberformat = $myconfig{"numberformat"};
457 $myconfig{"numberformat"} = $numberformat;
459 foreach my $idx (@indices) {
460 if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
461 for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
462 $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $form->format_amount(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i], $places);
466 next unless (defined($form->{$idx}));
468 if (!ref($form->{$idx})) {
469 $form->{$idx} = $form->format_amount(\%myconfig, $form->{$idx}, $places);
471 } elsif (ref($form->{$idx}) eq "ARRAY") {
472 for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
473 $form->{$idx}->[$i] =
474 $form->format_amount(\%myconfig, $form->{$idx}->[$i], $places);
479 $myconfig{"numberformat"} = $saved_numberformat;
481 $main::lxdebug->leave_sub();
484 # -------------------------------------------------------------------------
487 $main::lxdebug->enter_sub();
489 my $form = $main::form;
490 my %myconfig = %main::myconfig;
491 my $locale = $main::locale;
493 my $dbh = $form->dbconnect(\%myconfig);
494 my ($sort, $sortby) = split(/\-\-/, $form->{order});
495 $sort =~ s/.*\.(.*)/$1/;
497 $form->{title} = $locale->text("History");
499 print $form->parse_html_template( "common/show_history", {
500 "DATEN" => $form->get_history($dbh,$form->{input_name},"",$form->{order}),
501 "SUCCESS" => ($form->get_history($dbh,$form->{input_name}) ne "0"),
503 uc($sort)."BY" => $sortby
507 $main::lxdebug->leave_sub();
510 # -------------------------------------------------------------------------
513 $main::lxdebug->enter_sub();
517 my $form = $main::form;
518 my $locale = $main::locale;
521 $form->error($locale->text("Trying to call a sub without a name"));
524 $name =~ s/[^a-zA-Z0-9_]//g;
526 if (!defined(&{ $name })) {
527 $form->error(sprintf($locale->text("Attempt to call an undefined sub named '%s'"), $name));
530 $::called_subs{$name}++;
531 confess "RECURSION DETECTION: call_sub($name) called " . $::called_subs{$name} . " time(s)" if $::called_subs{$name} > 10;
538 $main::lxdebug->leave_sub();
541 # -------------------------------------------------------------------------
543 sub show_vc_details {
544 $main::lxdebug->enter_sub();
546 my $form = $main::form;
547 my %myconfig = %main::myconfig;
548 my $locale = $main::locale;
550 $form->{vc} = $form->{vc} eq "customer" ? "customer" : "vendor";
551 $form->isblank("vc_id",
552 $form->{vc} eq "customer" ?
553 $locale->text("No customer has been selected yet.") :
554 $locale->text("No vendor has been selected yet."));
556 Common->get_vc_details(\%myconfig, $form, $form->{vc}, $form->{vc_id});
558 $form->{title} = $form->{vc} eq "customer" ?
559 $locale->text("Customer details") : $locale->text("Vendor details");
561 print $form->parse_html_template("common/show_vc_details", { "is_customer" => $form->{vc} eq "customer" });
563 $main::lxdebug->leave_sub();
566 # -------------------------------------------------------------------------
568 sub retrieve_partunits {
569 $main::lxdebug->enter_sub();
571 my $form = $main::form;
573 my @part_ids = grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount});
576 my %partunits = IO->retrieve_partunits('part_ids' => \@part_ids);
578 foreach my $i (1..$form->{rowcount}) {
579 next unless ($form->{"id_${i}"});
580 $form->{"partunit_${i}"} = $partunits{$form->{"id_${i}"}};
584 $main::lxdebug->leave_sub();
587 # -------------------------------------------------------------------------
589 sub mark_as_paid_common {
590 $main::lxdebug->enter_sub();
592 my ($myconfig, $db_name) = @_;
594 my $form = $main::form;
595 my $locale = $main::locale;
597 if($form->{mark_as_paid}) {
598 my $dbh ||= $form->get_standard_dbh($myconfig);
599 my $query = qq|UPDATE $db_name SET paid = amount, datepaid = current_date WHERE id = ?|;
600 do_query($form, $dbh, $query, $form->{id});
602 $form->redirect($locale->text("Marked as paid"));
605 my $referer = $ENV{HTTP_REFERER};
608 if ($referer =~ /action/) {
609 $referer =~ /^(.*)\?action\=[^\&]*(\&.*)$/;
616 $referer = $script . "?action=mark_as_paid&mark_as_paid=1&id=$form->{id}" . $callback;
619 print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
620 print qq|<input type="button" value="|.$locale->text('yes').qq|" onclick="document.location.href='|.$referer.qq|'"> |;
621 print qq|<input type="button" value="|.$locale->text('no').qq|" onclick="javascript:history.back();">|;
622 print qq|</body></html>|;
625 $main::lxdebug->leave_sub();
628 sub cov_selection_internal {
629 $main::lxdebug->enter_sub();
631 my $form = $main::form;
632 my %myconfig = %main::myconfig;
633 my $locale = $main::locale;
635 my $order_by = "name";
636 $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
638 $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
640 my $type = $form->{"is_vendor"} ? $locale->text("vendor") : $locale->text("customer");
642 my $covs = Common->retrieve_customers_or_vendors(\%myconfig, $form, $order_by, $order_dir, $form->{"is_vendor"}, $form->{"allow_both"});
643 map({ $covs->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$covs}));
646 if (0 == scalar(@{$covs})) {
647 $form->show_generic_information(sprintf($locale->text("No %s was found matching the search parameters."), $type));
648 } elsif (1 == scalar(@{$covs})) {
649 $onload = "cov_selected('1')";
652 my $callback = "$form->{script}?action=cov_selection_internal&";
653 map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
654 (qw(name input_name input_id is_vendor allow_both), grep({ /^[fl]_/ } keys %$form)));
656 my @header_sort = qw(name address contact);
657 my %header_title = ( "name" => $locale->text("Name"),
658 "address" => $locale->text("Address"),
659 "contact" => $locale->text("Contact"),
663 map(+{ "column_title" => $header_title{$_},
665 "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
669 foreach my $cov (@{ $covs }) {
670 $cov->{address} = "$cov->{street}, $cov->{zipcode} $cov->{city}";
671 $cov->{address} =~ s{^,}{}x;
672 $cov->{address} =~ s{\ +}{\ }gx;
674 $cov->{contact} = join " ", map { $cov->{$_} } qw(cp_gender cp_title cp_givenname cp_name);
675 $cov->{contact} =~ s{\ +}{\ }gx;
678 $form->{"title"} = $form->{is_vendor} ? $locale->text("Select a vendor") : $locale->text("Select a customer");
680 print($form->parse_html_template("generic/cov_selection", { "HEADER" => \@header,
682 "onload" => $onload }));
684 $main::lxdebug->leave_sub();
688 # Functions to call add routines beneath different reports
691 $main::lxdebug->enter_sub();
693 print $::form->redirect_header('is.pl?action=add&type=invoice');
695 $main::lxdebug->leave_sub();
699 $main::lxdebug->enter_sub();
701 print $::form->redirect_header('ar.pl?action=add');
703 $main::lxdebug->leave_sub();
707 $main::lxdebug->enter_sub();
709 print $::form->redirect_header('ir.pl?action=add&type=invoice');
711 $main::lxdebug->leave_sub();
715 $main::lxdebug->enter_sub();
717 print $::form->redirect_header('ap.pl?action=add');
719 $main::lxdebug->leave_sub();
723 $main::lxdebug->enter_sub();
725 print $::form->redirect_header('gl.pl?action=add');
727 $main::lxdebug->leave_sub();
732 require SL::DB::Helpers::Mappings;
734 goto &SL::DB::Helpers::Mappings::db;