Nach der Projektauswahl die richtige Funktion aufrufen.
[kivitendo-erp.git] / bin / mozilla / common.pl
index 6ed951c..efeb8f0 100644 (file)
@@ -47,6 +47,144 @@ sub build_std_url {
 
 # -------------------------------------------------------------------------
 
+sub select_part {
+  $lxdebug->enter_sub();
+
+  my ($callback_sub, @parts) = @_;
+
+  my $remap_parts_id = 0;
+  if (defined($parts[0]->{parts_id}) && !defined($parts[0]->{id})) {
+    $remap_parts_id = 1;
+    map { $_->{id} = $_->{parts_id}; } @parts;
+  }
+
+  my $remap_partnumber = 0;
+  if (defined($parts[0]->{partnumber}) && !defined($parts[0]->{number})) {
+    $remap_partnumber = 1;
+    map { $_->{number} = $_->{partnumber}; } @parts;
+  }
+
+  my $has_charge = 0;
+  if (defined($parts[0]->{chargenumber})) {
+    $has_charge = 1;
+    map { $_->{has_charge} = 1; } @parts;
+  }
+  my $has_ean = 0;
+  if (defined($parts[0]->{ean})) {
+    $has_ean = 1;
+    map { $_->{has_ean} = 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,
+                                     "has_ean"         => $has_ean,
+                                     "remap_parts_id"   => $remap_parts_id,
+                                     "remap_partnumber" => $remap_partnumber });
+
+  $lxdebug->leave_sub();
+}
+
+sub select_part_internal {
+  $lxdebug->enter_sub();
+
+  my ($new_item, $callback_sub);
+
+  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_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};
+
+  restore_form($form->{old_form});
+
+  call_sub($callback_sub, $new_item);
+
+  $lxdebug->leave_sub();
+}
+
+sub part_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"}));
+
+  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 assemblies click_button);
+
+  $parts = Common->retrieve_parts(\%myconfig, $form, $order_by, $order_dir);
+
+  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')";
+  }
+
+  map { $parts->[$_]->{selected} = $_ ? 0 : 1; } (0..$#{$parts});
+
+  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),
+         },
+        @header_sort);
+
+  $form->{formname} ||= 'Form';
+
+  $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 delivery_customer_selection {
   $lxdebug->enter_sub();
 
@@ -138,45 +276,36 @@ sub vendor_selection {
 sub calculate_qty {
   $lxdebug->enter_sub();
 
-  my @variable_sort = ();
-  my %variable_list = ();
-  my $unit_list = ();
   $form->{formel} =~ s/\r\n//g;
 
   my ($variable_string, $formel) = split /###/,$form->{formel};
 
-
-  split m/;/, $variable_string;
-  foreach $item (@_) {
-    my($name, $valueunit) = split /=/,$item;
-    my($value, $unit) = split / /, $valueunit;
-
-    push(@variable_sort, $value);
-    $variable_list{$value} = $name;
-    $unit_list{$value} = $unit;
+  foreach $item (split m/;/, $variable_string) {
+    next unless $item =~ m/^ \s* (\w+) \s* = \s* (\w+) \s* (\w+) \s* $/x;
+    push @variable, {
+      description => $1,
+      name        => $2,
+      unit        => $3,
+    };
   }
 
   my @header_sort = qw(variable value unit);
-  my %header_title = ( "variable" => $locale->text("Variable"),
-                       "value" => $locale->text("Value"),
-                       "unit" => $locale->text("Unit"),
-                     );
+  my %header_title = (
+    variable => $locale->text("Variable"),
+    value    => $locale->text("Value"),
+    unit     => $locale->text("Unit"),
+  );
+  my @header = map +{
+    column_title => $header_title{$_},
+    column       => $_,
+  }, @header_sort;
 
-  my @variable = map(+{ "description" => $variable_list{$_},
-                        "name" => $_,
-                        "unit" => $unit_list{$_} }, @variable_sort);
-
-  my @header =
-    map(+{ "column_title" => $header_title{$_},
-           "column" => $_,
-         },
-        @header_sort);
   $form->{formel} = $formel;
-  $form->{"title"} = $locale->text("Please enter values");
+  $form->{title}  = $locale->text("Please enter values");
   $form->header();
-  print($form->parse_html_template("generic/calculate_qty", { "HEADER"    => \@header,
+  print $form->parse_html_template("generic/calculate_qty", { "HEADER"    => \@header,
                                                               "VARIABLES" => \@variable,
-                                                              "onload"    => $onload }));
+                                                              "onload"    => $onload });
 
   $lxdebug->leave_sub();
 }
@@ -196,11 +325,11 @@ sub set_longdescription {
 # -------------------------------------------------------------------------
 
 sub H {
-  return $form->quote_html($_[0]);
+  return $locale->quote_special_chars('HTML', $_[0]);
 }
 
 sub Q {
-  return $form->quote($_[0]);
+  return $locale->quote_special_chars('URL@HTML', $_[0]);
 }
 
 sub E {
@@ -383,7 +512,15 @@ sub mark_as_paid_common {
 
   } else {
     my $referer = $ENV{HTTP_REFERER};
-    $referer =~ s/^(.*)action\=.*\&(.*)$/$1action\=mark_as_paid\&mark_as_paid\=1\&id\=$form->{id}\&$2/;
+    if ($referer =~ /action/) {
+      $referer =~ /^(.*)\?action\=[^\&]*(\&.*)$/;
+      $script = $1;
+      $callback = $2;
+    } else {
+      $script = $referer;
+      $callback = "";
+    }
+    $referer = $script . "?action=mark_as_paid&mark_as_paid=1&id=$form->{id}" . $callback;
     $form->header();
     print qq|<body>|;
     print qq|<p><b>|.$locale->text('Mark as paid?').qq|</b></p>|;
@@ -448,4 +585,89 @@ sub cov_selection_internal {
   $lxdebug->leave_sub();
 }
 
+
+# Functions to call add routines beneath different reports
+
+sub sales_invoice {
+  $lxdebug->enter_sub();
+
+  $auth->assert('invoice_edit');
+
+
+  $form->{script} = 'is.pl';
+  $script         = "is";
+  $form->{type} = "invoice";
+  $locale = new Locale "$myconfig{countrycode}", "$script";
+
+  require "bin/mozilla/$form->{script}";
+  &add;
+
+  $lxdebug->leave_sub();
+}
+
+sub ar_transaction {
+  $lxdebug->enter_sub();
+
+  $auth->assert('general_ledger');
+
+
+  $form->{script} = 'ar.pl';
+  $script         = "ar";
+  $locale = new Locale "$myconfig{countrycode}", "$script";
+
+  require "bin/mozilla/$form->{script}";
+  &add;
+
+  $lxdebug->leave_sub();
+}
+
+sub vendor_invoice {
+  $lxdebug->enter_sub();
+
+  $auth->assert('invoice_edit');
+
+
+  $form->{script} = 'ir.pl';
+  $script         = "ir";
+  $form->{type} = "invoice";
+  $locale = new Locale "$myconfig{countrycode}", "$script";
+
+  require "bin/mozilla/$form->{script}";
+  &add;
+
+  $lxdebug->leave_sub();
+}
+
+sub ap_transaction {
+  $lxdebug->enter_sub();
+
+  $auth->assert('general_ledger');
+
+
+  $form->{script} = 'ap.pl';
+  $script         = "ap";
+  $locale = new Locale "$myconfig{countrycode}", "$script";
+
+  require "bin/mozilla/$form->{script}";
+  &add;
+
+  $lxdebug->leave_sub();
+}
+
+sub gl_transaction {
+  $lxdebug->enter_sub();
+
+  $auth->assert('general_ledger');
+
+
+  $form->{script} = 'gl.pl';
+  $script         = "gl";
+  $locale = new Locale "$myconfig{countrycode}", "$script";
+
+  require "bin/mozilla/$form->{script}";
+  &add;
+
+  $lxdebug->leave_sub();
+}
+
 1;