Einlagern: Nach der Artikelauswahl per Popup automatisch "Erneuern" drücken.
[kivitendo-erp.git] / bin / mozilla / common.pl
index 6f9e9be..f3fc1e8 100644 (file)
@@ -9,8 +9,9 @@
 #
 ######################################################################
 
-use SL::Form;
 use SL::Common;
+use SL::DBUtils;
+use SL::Form;
 use SL::MoreCommon;
 
 sub build_std_url {
@@ -20,7 +21,7 @@ sub build_std_url {
 
   my @parts;
 
-  foreach my $key ((qw(login password), @_)) {
+  foreach my $key (@_) {
     next unless ($key);
 
     if ($key =~ /(.*?)=(.*)/) {
@@ -31,7 +32,9 @@ sub build_std_url {
       }
 
     } else {
-      push @parts, "${key}=" . E($form->{$key});
+      foreach my $var ($form->flatten_variables($key)) {
+        push @parts, E($var->{key}) . '=' . E($var->{value});
+      }
     }
   }
 
@@ -42,43 +45,7 @@ sub build_std_url {
   return $url;
 }
 
-sub select_employee {
-  $lxdebug->enter_sub();
-
-  my ($callback_sub, @employees) = @_;
-
-  if (0 == scalar(@employees)) {
-    @employees = SystemBrace->get_all_employees(\%myconfig, $form);
-  }
-
-  my $old_form = save_form();
-
-  $form->header();
-  print($form->parse_html_template("generic/select_employee",
-                                   { "EMPLOYEES" => \@employees,
-                                     "old_form" => $old_form,
-                                     "title" => $locale->text("Select an employee"),
-                                     "nextsub" => "select_employee_internal",
-                                     "callback_sub" => $callback_sub }));
-
-  $lxdebug->leave_sub();
-}
-
-sub select_employee_internal {
-  $lxdebug->enter_sub();
-
-  my ($new_id, $new_name, $callback_sub);
-
-  my $new_id = $form->{"new_id_" . $form->{"selection"}};
-  my $new_name = $form->{"new_name_" . $form->{"selection"}};
-  my $callback_sub = $form->{"callback_sub"};
-
-  restore_form($form->{"old_form"});
-
-  call_sub($callback_sub, $new_id, $new_name);
-
-  $lxdebug->leave_sub();
-}
+# -------------------------------------------------------------------------
 
 sub select_part {
   $lxdebug->enter_sub();
@@ -86,35 +53,35 @@ sub select_part {
   my ($callback_sub, @parts) = @_;
 
   my $remap_parts_id = 0;
-  if (defined($parts[0]->{"parts_id"}) && !defined($parts[0]->{"id"})) {
+  if (defined($parts[0]->{parts_id}) && !defined($parts[0]->{id})) {
     $remap_parts_id = 1;
-    map({ $_->{"id"} = $_->{"parts_id"}; } @parts);
+    map { $_->{id} = $_->{parts_id}; } @parts;
   }
 
   my $remap_partnumber = 0;
-  if (defined($parts[0]->{"partnumber"}) && !defined($parts[0]->{"number"})) {
+  if (defined($parts[0]->{partnumber}) && !defined($parts[0]->{number})) {
     $remap_partnumber = 1;
-    map({ $_->{"number"} = $_->{"partnumber"}; } @parts);
+    map { $_->{number} = $_->{partnumber}; } @parts;
   }
 
   my $has_charge = 0;
-  if (defined($parts[0]->{"chargenumber"})) {
+  if (defined($parts[0]->{chargenumber})) {
     $has_charge = 1;
-    map({ $_->{"has_charge"} = 1; } @parts);
+    map { $_->{has_charge} = 1; } @parts;
   }
 
   my $old_form = save_form();
 
   $form->header();
-  print($form->parse_html_template("generic/select_part",
-                                   { "PARTS" => \@parts,
-                                     "old_form" => $old_form,
-                                     "title" => $locale->text("Select a part"),
-                                     "nextsub" => "select_part_internal",
-                                     "callback_sub" => $callback_sub,
-                                     "has_charge" => $has_charge,
-                                     "remap_parts_id" => $remap_parts_id,
-                                     "remap_partnumber" => $remap_partnumber }));
+  print $form->parse_html_template("generic/select_part",
+                                   { "PARTS"            => \@parts,
+                                     "old_form"         => $old_form,
+                                     "title"            => $locale->text("Select a part"),
+                                     "nextsub"          => "select_part_internal",
+                                     "callback_sub"     => $callback_sub,
+                                     "has_charge"       => $has_charge,
+                                     "remap_parts_id"   => $remap_parts_id,
+                                     "remap_partnumber" => $remap_partnumber });
 
   $lxdebug->leave_sub();
 }
@@ -124,26 +91,28 @@ sub select_part_internal {
 
   my ($new_item, $callback_sub);
 
-  my $re = "^new_.*_" . $form->{"selection"};
-  map({
-    my $key = $_;
-    $key =~ s/^new_//;
-    $key =~ s/_\d+$//;
-    $new_item->{$key} = $form->{$_};
-  } grep(/$re/, keys(%{$form})));
-
-  if ($form->{"remap_parts_id"}) {
-    $new_item->{"parts_id"} = $new_item->{"id"};
-    delete($new_item->{"id"});
+  my $re = "^new_.*_" . $form->{selection};
+
+  foreach (grep /$re/, keys %{ $form }) {
+    my $new_key           =  $_;
+    $new_key              =~ s/^new_//;
+    $new_key              =~ s/_\d+$//;
+    $new_item->{$new_key} =  $form->{$_};
   }
-  if ($form->{"remap_partnumber"}) {
-    $new_item->{"partnumber"} = $new_item->{"number"};
-    delete($new_item->{"number"});
+
+  if ($form->{remap_parts_id}) {
+    $new_item->{parts_id} = $new_item->{id};
+    delete $new_item->{id};
+  }
+
+  if ($form->{remap_partnumber}) {
+    $new_item->{partnumber} = $new_item->{number};
+    delete $new_item->{number};
   }
 
-  my $callback_sub = $form->{"callback_sub"};
+  my $callback_sub = $form->{callback_sub};
 
-  restore_form($form->{"old_form"});
+  restore_form($form->{old_form});
 
   call_sub($callback_sub, $new_item);
 
@@ -153,124 +122,62 @@ sub select_part_internal {
 sub part_selection_internal {
   $lxdebug->enter_sub();
 
-  $order_by = "description";
-  $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
+  $order_by  = "description";
+  $order_by  = $form->{"order_by"} if (defined($form->{"order_by"}));
   $order_dir = 1;
   $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
 
+  my %options;
+
+  foreach my $opt (split m/:/, $form->{options}) {
+    if ($opt =~ /=/) {
+      my ($key, $value) = split m/=/, $opt, 2;
+      $options{$key} = $value;
+
+    } else {
+      $options{$opt} = 1;
+    }
+  }
+
+  map { $form->{$_} = $options{$_} if ($options{$_}) } qw(no_services no_assemblies click_button);
+
   $parts = Common->retrieve_parts(\%myconfig, $form, $order_by, $order_dir);
-  map({ $parts->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$parts}));
+
   if (0 == scalar(@{$parts})) {
     $form->show_generic_information($locale->text("No part was found matching the search parameters."));
   } elsif (1 == scalar(@{$parts})) {
     $onload = "part_selected('1')";
   }
 
-  my $callback = "$form->{script}?action=part_selection_internal&";
-  map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
-      (qw(login password partnumber description input_partnumber input_description input_partsid), grep({ /^[fl]_/ } keys %$form)));
+  map { $parts->[$_]->{selected} = $_ ? 0 : 1; } (0..$#{$parts});
 
-  my @header_sort = qw(partnumber description);
-  my %header_title = ( "partnumber" => $locale->text("Part Number"),
+  my $callback = build_std_url('action=part_selection_internal', qw(partnumber description input_partnumber input_description input_partsid),
+                               grep({ /^[fl]_/ } keys %{ $form }));
+
+  my @header_sort  = qw(partnumber description);
+  my %header_title = ( "partnumber"  => $locale->text("Part Number"),
                        "description" => $locale->text("Part description"),
                        );
 
   my @header =
     map(+{ "column_title" => $header_title{$_},
-           "column" => $_,
-           "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
+           "column"       => $_,
+           "callback"     => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
          },
         @header_sort);
 
-  $form->{"title"} = $locale->text("Select a part");
-  $form->header();
-  print($form->parse_html_template("generic/part_selection", { "HEADER" => \@header,
-                                                               "PARTS" => $parts,
-                                                               "onload" => $onload }));
-
-  $lxdebug->leave_sub();
-}
-
-sub project_selection_internal {
-  $lxdebug->enter_sub();
-
-  $order_by = "description";
-  $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
-  $order_dir = 1;
-  $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
-
-  $projects = Common->retrieve_projects(\%myconfig, $form, $order_by, $order_dir);
-  map({ $projects->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$projects}));
-  if (0 == scalar(@{$projects})) {
-    $form->show_generic_information($locale->text("No project was found matching the search parameters."));
-  } elsif (1 == scalar(@{$projects})) {
-    $onload = "project_selected('1')";
-  }
-
-  my $callback = "$form->{script}?action=project_selection_internal&";
-  map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
-      (qw(login password projectnumber description input_projectnumber input_description input_project_id), grep({ /^[fl]_/ } keys %$form)));
-
-  my @header_sort = qw(projectnumber description);
-  my %header_title = ( "projectnumber" => $locale->text("Project Number"),
-                       "description" => $locale->text("Project description"),
-                       );
+  $form->{formname} ||= 'Form';
 
-  my @header =
-    map(+{ "column_title" => $header_title{$_},
-           "column" => $_,
-           "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
-         },
-        @header_sort);
-
-  $form->{"title"} = $locale->text("Select a project");
+  $form->{title} = $locale->text("Select a part");
   $form->header();
-  print($form->parse_html_template("generic/project_selection", { "HEADER" => \@header,
-                                                                  "PROJECTS" => $projects,
-                                                                  "onload" => $onload }));
+  print $form->parse_html_template("generic/part_selection", { "HEADER" => \@header,
+                                                               "PARTS"  => $parts,
+                                                               "onload" => $onload });
 
   $lxdebug->leave_sub();
 }
 
-sub employee_selection_internal {
-  $lxdebug->enter_sub();
-
-  $order_by = "name";
-  $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
-  $order_dir = 1;
-  $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
-
-  $employees = Common->retrieve_employees(\%myconfig, $form, $order_by, $order_dir);
-  map({ $employees->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$employees}));
-  if (0 == scalar(@{$employees})) {
-    $form->show_generic_information($locale->text("No employee was found matching the search parameters."));
-  } elsif (1 == scalar(@{$employees})) {
-    $onload = "employee_selected('1')";
-  }
-
-  my $callback = "$form->{script}?action=employee_selection_internal&";
-  map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
-      (qw(login password name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
-
-  my @header_sort = qw(name);
-  my %header_title = ( "name" => $locale->text("Name"),
-                       );
-
-  my @header =
-    map(+{ "column_title" => $header_title{$_},
-           "column" => $_,
-           "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
-         },
-        @header_sort);
-
-  $form->{"title"} = $locale->text("Select an employee");
-  $form->header();
-  print($form->parse_html_template("generic/employee_selection", { "HEADER" => \@header,
-                                                                   "EMPLOYEES" => $employees,
-                                                                   "onload" => $onload }));
-
-  $lxdebug->leave_sub();
-}
+# -------------------------------------------------------------------------
 
 sub delivery_customer_selection {
   $lxdebug->enter_sub();
@@ -290,7 +197,7 @@ sub delivery_customer_selection {
 
   my $callback = "$form->{script}?action=delivery_customer_selection&";
   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
-      (qw(login password name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
+      (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
 
   my @header_sort = qw(name customernumber address);
   my %header_title = ( "name" => $locale->text("Name"),
@@ -314,6 +221,8 @@ sub delivery_customer_selection {
   $lxdebug->leave_sub();
 }
 
+# -------------------------------------------------------------------------
+
 sub vendor_selection {
   $lxdebug->enter_sub();
 
@@ -332,7 +241,7 @@ sub vendor_selection {
 
   my $callback = "$form->{script}?action=vendor_selection&";
   map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
-      (qw(login password name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
+      (qw(name input_name input_id), grep({ /^[fl]_/ } keys %$form)));
 
   my @header_sort = qw(name customernumber address);
   my %header_title = ( "name" => $locale->text("Name"),
@@ -356,6 +265,8 @@ sub vendor_selection {
   $lxdebug->leave_sub();
 }
 
+# -------------------------------------------------------------------------
+
 sub calculate_qty {
   $lxdebug->enter_sub();
 
@@ -367,7 +278,7 @@ sub calculate_qty {
   my ($variable_string, $formel) = split /###/,$form->{formel};
 
 
-  split /;/, $variable_string;
+  split m/;/, $variable_string;
   foreach $item (@_) {
     my($name, $valueunit) = split /=/,$item;
     my($value, $unit) = split / /, $valueunit;
@@ -392,7 +303,7 @@ sub calculate_qty {
            "column" => $_,
          },
         @header_sort);
-  $form->{formel} = $formel; 
+  $form->{formel} = $formel;
   $form->{"title"} = $locale->text("Please enter values");
   $form->header();
   print($form->parse_html_template("generic/calculate_qty", { "HEADER"    => \@header,
@@ -402,6 +313,8 @@ sub calculate_qty {
   $lxdebug->leave_sub();
 }
 
+# -------------------------------------------------------------------------
+
 sub set_longdescription {
   $lxdebug->enter_sub();
 
@@ -412,6 +325,8 @@ sub set_longdescription {
   $lxdebug->leave_sub();
 }
 
+# -------------------------------------------------------------------------
+
 sub H {
   return $form->quote_html($_[0]);
 }
@@ -501,12 +416,14 @@ sub reformat_numbers {
   $lxdebug->leave_sub();
 }
 
+# -------------------------------------------------------------------------
+
 sub show_history {
        $lxdebug->enter_sub();
        my $dbh = $form->dbconnect(\%myconfig);
        my ($sort, $sortby) = split(/\-\-/, $form->{order});
   $sort =~ s/.*\.(.*)/$1/;
-  
+
        $form->{title} = $locale->text("History");
     $form->header();
     print $form->parse_html_template( "common/show_history", {
@@ -515,11 +432,13 @@ sub show_history {
       uc($sort) => 1,
       uc($sort)."BY" => $sortby
        } );
-       
+
        $dbh->disconnect();
-       $lxdebug->leave_sub();  
+       $lxdebug->leave_sub();
 }
 
+# -------------------------------------------------------------------------
+
 sub call_sub {
   $lxdebug->enter_sub();
 
@@ -540,6 +459,8 @@ sub call_sub {
   $lxdebug->leave_sub();
 }
 
+# -------------------------------------------------------------------------
+
 sub show_vc_details {
        $lxdebug->enter_sub();
 
@@ -559,9 +480,30 @@ sub show_vc_details {
        $lxdebug->leave_sub();
 }
 
+# -------------------------------------------------------------------------
+
+sub retrieve_partunits {
+  $lxdebug->enter_sub();
+
+  my @part_ids = grep { $_ } map { $form->{"id_${_}"} } (1..$form->{rowcount});
+
+  if (@part_ids) {
+    my %partunits = IO->retrieve_partunits('part_ids' => \@part_ids);
+
+    foreach my $i (1..$form->{rowcount}) {
+      next unless ($form->{"id_${i}"});
+      $form->{"partunit_${i}"} = $partunits{$form->{"id_${i}"}};
+    }
+  }
+
+  $lxdebug->leave_sub();
+}
+
+# -------------------------------------------------------------------------
+
 sub mark_as_paid_common {
   $lxdebug->enter_sub();
-  use SL::DBUtils;
+
   my ($myconfig, $db_name) = @_;
 
   if($form->{mark_as_paid}) {
@@ -570,21 +512,71 @@ sub mark_as_paid_common {
     do_query($form, $dbh, $query, $form->{id});
     $dbh->commit();
     $form->redirect($locale->text("Marked as paid"));
-}
-  else {
-    my $referer  =  $ENV{HTTP_REFERER};
-    my $login    =  $form->escape($form->{login});
-    my $password =  $form->escape($form->{password});
-    my $id       =  $form->escape($form->{id});
-    $referer     =~ s/^(.*)action\=.*\&(.*)$/$1action\=mark_as_paid\&mark_as_paid\=1\&login\=$login\&password\=$password\&id\=$id\&$2/;
+
+  } else {
+    my $referer = $ENV{HTTP_REFERER};
+    $referer =~ s/^(.*)action\=.*\&(.*)$/$1action\=mark_as_paid\&mark_as_paid\=1\&id\=$form->{id}\&$2/;
     $form->header();
     print qq|<body>|;
     print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
     print qq|<input type="button" value="|.$locale->text('yes').qq|" onclick="document.location.href='|.$referer.qq|'">&nbsp;|;
     print qq|<input type="button" value="|.$locale->text('no').qq|" onclick="javascript:history.back();">|;
     print qq|</body></html>|;
+  }
+
+  $lxdebug->leave_sub();
 }
-  
+
+sub cov_selection_internal {
+  $lxdebug->enter_sub();
+
+  $order_by = "name";
+  $order_by = $form->{"order_by"} if (defined($form->{"order_by"}));
+  $order_dir = 1;
+  $order_dir = $form->{"order_dir"} if (defined($form->{"order_dir"}));
+
+  my $type = $form->{"is_vendor"} ? $locale->text("vendor") : $locale->text("customer");
+
+  $covs = Common->retrieve_customers_or_vendors(\%myconfig, $form, $order_by, $order_dir, $form->{"is_vendor"}, $form->{"allow_both"});
+  map({ $covs->[$_]->{"selected"} = $_ ? 0 : 1; } (0..$#{$covs}));
+  if (0 == scalar(@{$covs})) {
+    $form->show_generic_information(sprintf($locale->text("No %s was found matching the search parameters."), $type));
+  } elsif (1 == scalar(@{$covs})) {
+    $onload = "cov_selected('1')";
+  }
+
+  my $callback = "$form->{script}?action=cov_selection_internal&";
+  map({ $callback .= "$_=" . $form->escape($form->{$_}) . "&" }
+      (qw(name input_name input_id is_vendor allow_both), grep({ /^[fl]_/ } keys %$form)));
+
+  my @header_sort = qw(name address contact);
+  my %header_title = ( "name" => $locale->text("Name"),
+                       "address" => $locale->text("Address"),
+                       "contact" => $locale->text("Contact"),
+                       );
+
+  my @header =
+    map(+{ "column_title" => $header_title{$_},
+           "column" => $_,
+           "callback" => $callback . "order_by=${_}&order_dir=" . ($order_by eq $_ ? 1 - $order_dir : $order_dir),
+         },
+        @header_sort);
+
+  foreach my $cov (@{ $covs }) {
+    $cov->{address} = "$cov->{street}, $cov->{zipcode} $cov->{city}";
+    $cov->{address} =~ s{^,}{}x;
+    $cov->{address} =~ s{\ +}{\ }gx;
+
+    $cov->{contact} = join " ", map { $cov->{$_} } qw(cp_greeting cp_title cp_givenname cp_name);
+    $cov->{contact} =~ s{\ +}{\ }gx;
+  }
+
+  $form->{"title"} = $form->{is_vendor} ? $locale->text("Select a vendor") : $locale->text("Select a customer");
+  $form->header();
+  print($form->parse_html_template("generic/cov_selection", { "HEADER" => \@header,
+                                                              "COVS" => $covs,
+                                                              "onload" => $onload }));
+
   $lxdebug->leave_sub();
 }