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 # -------------------------------------------------------------------------
 
 512 my %_called_subs = ();
 
 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();