Merge branch 'after-262'
[kivitendo-erp.git] / bin / mozilla / common.pl
index 2b2016f..cef7fca 100644 (file)
@@ -9,10 +9,13 @@
 #
 ######################################################################
 
+use Carp;
 use SL::Common;
+use SL::DB::Helper::Mappings;
 use SL::DBUtils;
 use SL::Form;
 use SL::MoreCommon;
+use SL::Helper::Flash;
 
 use strict;
 
@@ -76,6 +79,11 @@ sub select_part {
     $has_charge = 1;
     map { $_->{has_charge} = 1; } @parts;
   }
+  my $has_bestbefore = 0;
+  if (defined($parts[0]->{bestbefore})) {
+    $has_bestbefore = 1;
+    map { $_->{has_bestbefore} = 1; } @parts;
+  }
   my $has_ean = 0;
   if (defined($parts[0]->{ean})) {
     $has_ean = 1;
@@ -92,7 +100,8 @@ sub select_part {
                                      "nextsub"          => "select_part_internal",
                                      "callback_sub"     => $callback_sub,
                                      "has_charge"       => $has_charge,
-                                     "has_ean"         => $has_ean,
+                                     "has_bestbefore"   => $has_bestbefore,
+                                     "has_ean"          => $has_ean,
                                      "remap_parts_id"   => $remap_parts_id,
                                      "remap_partnumber" => $remap_partnumber });
 
@@ -176,7 +185,7 @@ sub part_selection_internal {
 
   my @header_sort  = qw(partnumber description);
   my %header_title = ( "partnumber"  => $locale->text("Part Number"),
-                       "description" => $locale->text("Part description"),
+                       "description" => $locale->text("Part Description"),
                        );
 
   my @header =
@@ -378,126 +387,11 @@ sub NTI {
 }
 
 sub format_dates {
-  $main::lxdebug->enter_sub();
-
-  my ($dateformat, $longformat, @indices) = @_;
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
-  $dateformat = $myconfig{"dateformat"} unless ($dateformat);
-
-  foreach my $idx (@indices) {
-    if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
-      for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
-        $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] =
-          $locale->reformat_date(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i],
-                                 $dateformat, $longformat);
-      }
-    }
-
-    next unless (defined($form->{$idx}));
-
-    if (!ref($form->{$idx})) {
-      $form->{$idx} = $locale->reformat_date(\%myconfig, $form->{$idx},
-                                             $dateformat, $longformat);
-
-    } elsif (ref($form->{$idx}) eq "ARRAY") {
-      for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
-        $form->{$idx}->[$i] =
-          $locale->reformat_date(\%myconfig, $form->{$idx}->[$i],
-                                 $dateformat, $longformat);
-      }
-    }
-  }
-
-  $main::lxdebug->leave_sub();
+  return $::form->format_dates(@_);
 }
 
 sub reformat_numbers {
-  $main::lxdebug->enter_sub();
-
-  my ($numberformat, $places, @indices) = @_;
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-
-  return $main::lxdebug->leave_sub()
-    if (!$numberformat || ($numberformat eq $myconfig{"numberformat"}));
-
-  foreach my $idx (@indices) {
-    if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
-      for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
-        $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $form->parse_amount(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i]);
-      }
-    }
-
-    next unless (defined($form->{$idx}));
-
-    if (!ref($form->{$idx})) {
-      $form->{$idx} = $form->parse_amount(\%myconfig, $form->{$idx});
-
-    } elsif (ref($form->{$idx}) eq "ARRAY") {
-      for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
-        $form->{$idx}->[$i] =
-          $form->parse_amount(\%myconfig, $form->{$idx}->[$i]);
-      }
-    }
-  }
-
-  my $saved_numberformat = $myconfig{"numberformat"};
-  $myconfig{"numberformat"} = $numberformat;
-
-  foreach my $idx (@indices) {
-    if ($form->{TEMPLATE_ARRAYS} && (ref($form->{TEMPLATE_ARRAYS}->{$idx}) eq "ARRAY")) {
-      for (my $i = 0; $i < scalar(@{$form->{TEMPLATE_ARRAYS}->{$idx}}); $i++) {
-        $form->{TEMPLATE_ARRAYS}->{$idx}->[$i] = $form->format_amount(\%myconfig, $form->{TEMPLATE_ARRAYS}->{$idx}->[$i], $places);
-      }
-    }
-
-    next unless (defined($form->{$idx}));
-
-    if (!ref($form->{$idx})) {
-      $form->{$idx} = $form->format_amount(\%myconfig, $form->{$idx}, $places);
-
-    } elsif (ref($form->{$idx}) eq "ARRAY") {
-      for (my $i = 0; $i < scalar(@{$form->{$idx}}); $i++) {
-        $form->{$idx}->[$i] =
-          $form->format_amount(\%myconfig, $form->{$idx}->[$i], $places);
-      }
-    }
-  }
-
-  $myconfig{"numberformat"} = $saved_numberformat;
-
-  $main::lxdebug->leave_sub();
-}
-
-# -------------------------------------------------------------------------
-
-sub show_history {
-       $main::lxdebug->enter_sub();
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
-       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", {
-       "DATEN" => $form->get_history($dbh,$form->{input_name},"",$form->{order}),
-       "SUCCESS" => ($form->get_history($dbh,$form->{input_name}) ne "0"),
-      uc($sort) => 1,
-      uc($sort)."BY" => $sortby
-       } );
-
-       $dbh->disconnect();
-       $main::lxdebug->leave_sub();
+  return $::form->reformat_numbers(@_);
 }
 
 # -------------------------------------------------------------------------
@@ -520,6 +414,9 @@ sub call_sub {
     $form->error(sprintf($locale->text("Attempt to call an undefined sub named '%s'"), $name));
   }
 
+  $::called_subs{$name}++;
+  confess "RECURSION DETECTION: call_sub($name) called " . $::called_subs{$name} . " time(s)" if $::called_subs{$name} > 10;
+
   {
     no strict "refs";
     &{ $name }(@_);
@@ -531,7 +428,7 @@ sub call_sub {
 # -------------------------------------------------------------------------
 
 sub show_vc_details {
-       $main::lxdebug->enter_sub();
+  $main::lxdebug->enter_sub();
 
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
@@ -550,7 +447,7 @@ sub show_vc_details {
   $form->header();
   print $form->parse_html_template("common/show_vc_details", { "is_customer" => $form->{vc} eq "customer" });
 
-       $main::lxdebug->leave_sub();
+  $main::lxdebug->leave_sub();
 }
 
 # -------------------------------------------------------------------------
@@ -680,19 +577,7 @@ sub cov_selection_internal {
 sub sales_invoice {
   $main::lxdebug->enter_sub();
 
-  $main::auth->assert('invoice_edit');
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
-  $form->{script} = 'is.pl';
-  my $script      = "is";
-  $form->{type} = "invoice";
-  $locale = new Locale "$myconfig{countrycode}", "$script";
-
-  require "bin/mozilla/$form->{script}";
-  &add;
+  print $::form->redirect_header('is.pl?action=add&type=invoice');
 
   $main::lxdebug->leave_sub();
 }
@@ -700,18 +585,7 @@ sub sales_invoice {
 sub ar_transaction {
   $main::lxdebug->enter_sub();
 
-  $main::auth->assert('general_ledger');
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
-  $form->{script} = 'ar.pl';
-  my $script      = "ar";
-  $locale = new Locale "$myconfig{countrycode}", "$script";
-
-  require "bin/mozilla/$form->{script}";
-  &add;
+  print $::form->redirect_header('ar.pl?action=add');
 
   $main::lxdebug->leave_sub();
 }
@@ -719,19 +593,7 @@ sub ar_transaction {
 sub vendor_invoice {
   $main::lxdebug->enter_sub();
 
-  $main::auth->assert('invoice_edit');
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
-  $form->{script} = 'ir.pl';
-  my $script      = "ir";
-  $form->{type} = "invoice";
-  $locale = new Locale "$myconfig{countrycode}", "$script";
-
-  require "bin/mozilla/$form->{script}";
-  &add;
+  print $::form->redirect_header('ir.pl?action=add&type=invoice');
 
   $main::lxdebug->leave_sub();
 }
@@ -739,18 +601,7 @@ sub vendor_invoice {
 sub ap_transaction {
   $main::lxdebug->enter_sub();
 
-  $main::auth->assert('general_ledger');
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
-  $form->{script} = 'ap.pl';
-  my $script      = "ap";
-  $locale = new Locale "$myconfig{countrycode}", "$script";
-
-  require "bin/mozilla/$form->{script}";
-  &add;
+  print $::form->redirect_header('ap.pl?action=add');
 
   $main::lxdebug->leave_sub();
 }
@@ -758,20 +609,13 @@ sub ap_transaction {
 sub gl_transaction {
   $main::lxdebug->enter_sub();
 
-  $main::auth->assert('general_ledger');
-
-  my $form     = $main::form;
-  my %myconfig = %main::myconfig;
-  my $locale   = $main::locale;
-
-  $form->{script} = 'gl.pl';
-  my $script      = "gl";
-  $locale = new Locale "$myconfig{countrycode}", "$script";
-
-  require "bin/mozilla/$form->{script}";
-  &add;
+  print $::form->redirect_header('gl.pl?action=add');
 
   $main::lxdebug->leave_sub();
 }
 
+sub db {
+  goto &SL::DB::Helper::Mappings::db;
+}
+
 1;