gelöschte Benutzer in ar/search ausblenden
[kivitendo-erp.git] / bin / mozilla / vk.pl
index dbbc2bd..e871053 100644 (file)
@@ -35,6 +35,7 @@ use POSIX qw(strftime);
 use List::Util qw(sum first);
 
 use SL::VK;
+use SL::IS;
 use SL::ReportGenerator;
 use Data::Dumper;
 
@@ -53,7 +54,6 @@ sub search_invoice {
   my $form     = $main::form;
   my %myconfig = %main::myconfig;
   my $locale   = $main::locale;
-  my $cgi      = $main::cgi;
 
   my ($customer, $department);
 
@@ -86,9 +86,24 @@ sub invoice_transactions {
 
   my ($callback, $href, @columns);
 
-  $form->{customer} = $form->unescape($form->{customer});
-  
-  ($form->{customername}, $form->{customer_id}) = split(/--/, $form->{customer});
+  if ( $form->{customer} =~ /--/ ) {
+    # Felddaten kommen aus Dropdownbox
+    ($form->{customername}, $form->{customer_id}) = split(/--/, $form->{customer});
+  } elsif ($form->{customer}) {
+    # es wurde ein Wert im Freitextfeld übergeben, auf Eindeutigkeit überprüfen
+
+    # check_name wird mit no_select => 1 ausgeführt, ist die Abfrage nicht eindeutig kommt ein Fehler
+    # und die Abfrage muß erneut ausgeführt werden
+
+    # Ohne no_select kommt bei Auswahl des Kunden ein Aufruf von update der ins
+    # Nichts führt, daher diese Zwischenlösung
+
+    &check_name('customer', no_select => 1);
+
+    # $form->{customer_id} wurde schon von check_name gesetzt
+    $form->{customername} = $form->{customer};
+  };
+  # ist $form->{customer} leer passiert hier nichts weiter
 
   # decimalplaces überprüfen oder auf Default 2 setzen
   $form->{decimalplaces} = 2 unless $form->{decimalplaces} > 0 && $form->{decimalplaces} < 6;
@@ -109,12 +124,12 @@ sub invoice_transactions {
   $form->{title} = $locale->text('Sales Report');
 
   @columns =
-    qw(description invnumber partnumber parts_id transdate qty unit sellprice sellprice_total discount lastcost lastcost_total marge_total marge_percent);
+    qw(description invnumber transdate customernumber partnumber transdate qty unit sellprice sellprice_total discount lastcost lastcost_total marge_total marge_percent);
 
   # hidden variables für pdf/csv export übergeben
   # einmal mit l_ um zu bestimmen welche Spalten ausgegeben werden sollen
   # einmal optionen für die Überschrift (z.B. transdatefrom, partnumber, ...)
-  my @hidden_variables  = (qw(l_headers l_subtotal l_total transdatefrom transdateto decimalplaces customer customername customer_id department partnumber description project_id), "$form->{db}number", map { "l_$_" } @columns);
+  my @hidden_variables  = (qw(l_headers l_subtotal l_total l_customernumber transdatefrom transdateto decimalplaces customer customername customer_id department partnumber description project_id customernumber), "$form->{db}number", map { "l_$_" } @columns);
   my @hidden_nondefault = grep({ $form->{$_} } @hidden_variables);
   # Variablen werden dann als Hidden Variable mitgegeben, z.B.
   # <input type="hidden" name="report_generator_hidden_transdateto" value="21.05.2010">
@@ -136,8 +151,9 @@ sub invoice_transactions {
     'lastcost'                => { 'text' => $locale->text('Purchase price'), },
     'marge_total'             => { 'text' => $locale->text('Sales margin'), },
     'marge_percent'           => { 'text' => $locale->text('Sales margin %'), },
+    'customernumber'          => { 'text' => $locale->text('Customer Number'), },
   );
-  
+
   my %column_alignment = map { $_ => 'right' } qw(lastcost sellprice sellprice_total lastcost_total unit discount marge_total marge_percent qty);
 
   $form->{"l_type"} = "Y";
@@ -146,11 +162,14 @@ sub invoice_transactions {
 
   my @options;
   if ($form->{description}) {
-    push @options, $locale->text('description') . " : $form->{description}";
+    push @options, $locale->text('Description') . " : $form->{description}";
   }
   if ($form->{customer}) {
     push @options, $locale->text('Customer') . " : $form->{customername}";
   }
+  if ($form->{customernumber}) {
+    push @options, $locale->text('Customer Number') . " : $form->{customernumber}";
+  }
   if ($form->{department}) {
     my ($department) = split /--/, $form->{department};
     push @options, $locale->text('Department') . " : $department";
@@ -185,9 +204,10 @@ sub invoice_transactions {
   $report->set_options('top_info_text'        => join("\n", @options),
                        'output_format'        => 'HTML',
                        'title'                => $form->{title},
-                       'attachment_basename'  => $locale->text('sales_report') . strftime('_%Y%m%d', localtime time),
+                       'attachment_basename'  => $locale->text('Sales Report') . strftime('_%Y%m%d', localtime time),
     );
   $report->set_options_from_form();
+  $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
 
   $report->set_columns(%column_defs);
   $report->set_column_order(@columns);
@@ -222,7 +242,7 @@ sub invoice_transactions {
     $ar->{sellprice} = $ar->{sellprice} / $ar->{price_factor};
     $ar->{lastcost} = $ar->{lastcost} / $ar->{price_factor};
     $ar->{sellprice_total} = $ar->{qty} * $ar->{sellprice};
-    $ar->{lastcost_total}  = $ar->{qty} * $ar->{lastcost}; 
+    $ar->{lastcost_total}  = $ar->{qty} * $ar->{lastcost};
     # marge_percent wird neu berechnet, da Wert in invoice leer ist (Bug)
     $ar->{marge_percent} = $ar->{sellprice_total} ? (($ar->{sellprice_total}-$ar->{lastcost_total}) / $ar->{sellprice_total}) : 0;
     # marge_total neu berechnen
@@ -234,23 +254,23 @@ sub invoice_transactions {
       my $name;
       my $headerrow;
       if ( $form->{mainsort} eq 'parts_id' ) {
-       $headerrow->{description}->{data} = "$ar->{description}";
+        $headerrow->{description}->{data} = "$ar->{description}";
       } else {
-       $headerrow->{description}->{data} = "$ar->{name}";
+        $headerrow->{description}->{data} = "$ar->{name}";
       };
       $headerrow->{description}->{class} = "listmainsortheader";
       my $headerrow_set = [ $headerrow ];
       $report->add_data($headerrow_set);
 
       # add empty row after main header
-#      my $emptyheaderrow->{description}->{data} = ""; 
+#      my $emptyheaderrow->{description}->{data} = "";
 #      $emptyheaderrow->{description}->{class} = "listmainsortheader";
 #      my $emptyheaderrow_set = [ $emptyheaderrow ];
-#      $report->add_data($emptyheaderrow_set) if $form->{l_headers} eq "Y"; 
+#      $report->add_data($emptyheaderrow_set) if $form->{l_headers} eq "Y";
     };
 
     # subsort überschriften
-    if ( $idx == 0 
+    if ( $idx == 0
       or $ar->{ $form->{'subsort'} }  ne $form->{AR}->[$idx - 1]->{ $form->{'subsort'} }
       or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} }
     ) {
@@ -271,13 +291,31 @@ sub invoice_transactions {
     map { $subtotals1{$_} += $ar->{$_};
           $subtotals2{$_} += $ar->{$_};
         } @subtotal_columns;
-         
-    map { $totals{$_}    += $ar->{$_} } @total_columns;  
 
-    $subtotals2{sellprice} = $subtotals2{sellprice_total} / $subtotals2{qty} if $subtotals2{qty} != 0;
-    $subtotals1{sellprice} = $subtotals1{sellprice_total} / $subtotals1{qty} if $subtotals1{qty} != 0;
-    $subtotals2{lastcost} = $subtotals2{lastcost_total} / $subtotals2{qty} if $subtotals2{qty} != 0;
-    $subtotals1{lastcost} = $subtotals1{lastcost_total} / $subtotals1{qty} if $subtotals1{qty} != 0;
+    map { $totals{$_}    += $ar->{$_} } @total_columns;
+
+    if ( $subtotals1{qty} != 0 ) {
+      # calculate averages for subtotals1 and subtotals2
+      # credited positions reduce both total and qty and thus don't influence average prices
+      $subtotals1{sellprice} = $subtotals1{sellprice_total} / $subtotals1{qty};
+      $subtotals1{lastcost} = $subtotals1{lastcost_total} / $subtotals1{qty};
+    } else {
+      # qty is zero, so we have a special case where each position in subtotal
+      # group has a corresponding credit note so that the total qty is zero in
+      # this case we also want the total amounts to be zero, so overwrite them,
+      # rather than leaving the last value in sellprice/lastcost
+
+      $subtotals1{sellprice} = 0;
+      $subtotals1{lastcost} = 0;
+    };
+
+    if ( $subtotals2{qty} != 0 ) {
+      $subtotals2{sellprice} = $subtotals2{sellprice_total} / $subtotals2{qty};
+      $subtotals2{lastcost} = $subtotals2{lastcost_total} / $subtotals2{qty};
+    } else {
+      $subtotals2{sellprice} = 0;
+      $subtotals2{lastcost} = 0;
+    };
 
     # Ertrag prozentual in den Summen: (summe VK - summe Ertrag) / summe VK
     $subtotals1{marge_percent} = $subtotals1{sellprice_total} ? (($subtotals1{sellprice_total} - $subtotals1{lastcost_total}) / $subtotals1{sellprice_total}) : 0;
@@ -298,7 +336,7 @@ sub invoice_transactions {
         'align' => $column_alignment{$column},
       };
     }
-  
+
    $row->{description}->{class} = 'listsortdescription';
 
     $row->{invnumber}->{link} = build_std_url("script=is.pl", 'action=edit')
@@ -317,14 +355,14 @@ sub invoice_transactions {
       } else {
         $name = 'name';
       };
-      
-      if ($form->{l_subtotal} eq 'Y' ) {
+
+      if ($form->{l_subtotal} eq 'Y') {
         push @{ $row_set }, create_subtotal_row_invoice(\%subtotals2, \@columns, \%column_alignment, \@subtotal_columns, 'listsubsortsubtotal', $ar->{$name}) ;
         push @{ $row_set }, insert_empty_row();
       };
     }
 
-    # if mainsort has changed, add mainsort subtotal and empty row 
+    # if mainsort has changed, add mainsort subtotal and empty row
     if (($form->{l_subtotal} eq 'Y')
         && (($idx == (scalar @{ $form->{AR} } - 1))   # last element always has a subtotal
             || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
@@ -340,7 +378,7 @@ sub invoice_transactions {
         push @{ $row_set }, insert_empty_row();
       };
     }
-  
+
     $report->add_data($row_set);
 
     $idx++;
@@ -373,7 +411,7 @@ sub create_subtotal_row_invoice {
   my %myconfig = %main::myconfig;
 
   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
-  
+
   $row->{description}->{data} = "Summe " . $name;
 
   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(marge_total marge_percent);