Merge branch 'clients'
[kivitendo-erp.git] / bin / mozilla / vk.pl
index 914d465..a60d430 100644 (file)
@@ -103,7 +103,12 @@ sub invoice_transactions {
   my ($callback, $href, @columns);
 
   # can't currently be configured from report, empty line between main sortings
-  my $addemptylines = '1';
+  my $addemptylines = 1;
+  
+  # don't add empty lines between mainsort subtotals when only subtotal_mainsort is selected
+  if ($form->{l_subtotal_mainsort} eq "Y" and not defined $form->{l_headers_mainsort} and not defined $form->{l_headers_subsort} and not defined $form->{l_subtotal_subsort} ) {
+    $addemptylines = 0 
+  };
 
   if ( $form->{customer} =~ /--/ ) {
     # Felddaten kommen aus Dropdownbox
@@ -119,12 +124,12 @@ sub invoice_transactions {
 
     &check_name('customer', no_select => 1);
 
-    # $form->{customer_id} wurde schon von check_name gesetzt
+    # $form->{customer_id} was already set by check_name
     $form->{customername} = $form->{customer};
   };
-  # ist $form->{customer} leer passiert hier nichts weiter
+  # if $form->{customer} is empty nothing further happens here
 
-  # decimalplaces überprüfen oder auf Default 2 setzen
+  # test for decimalplaces or set to default of 2
   $form->{decimalplaces} = 2 unless $form->{decimalplaces} > 0 && $form->{decimalplaces} < 6;
 
   my $cvar_configs_ct = CVar->get_configs('module' => 'CT');
@@ -265,6 +270,15 @@ sub invoice_transactions {
       'data'           => $form->{AR}
   );
 
+  my $num_visible_columns = scalar $report->get_visible_columns;
+  my %empty_row           = (
+    description           => {
+      data                => '',
+      class               => 'listrowempty',
+      colspan             => $num_visible_columns,
+    },
+  );
+
   # add sort and escape callback, this one we use for the add sub
   $form->{callback} = $href .= "&sort=$form->{mainsort}";
 
@@ -272,9 +286,9 @@ sub invoice_transactions {
   $callback = $form->escape($href);
 
   my @subtotal_columns = qw(qty weight sellprice sellprice_total lastcost lastcost_total marge_total marge_percent discount);
-  # Gesamtsumme:
-  # Summe von sellprice_total, lastcost_total und marge_total
-  # Durchschnitt von marge_percent
+  # Total sum:
+  # sum of sellprice_total, lastcost_total and marge_total
+  # average of marge_percent
   my @total_columns = qw(sellprice_total lastcost_total marge_total marge_percent );
 
   my %totals     = map { $_ => 0 } @total_columns;
@@ -295,8 +309,8 @@ sub invoice_transactions {
     # discount was already accounted for in db sellprice
     $ar->{sellprice}       = $ar->{sellprice}  / $ar->{price_factor} / $basefactor;
     $ar->{lastcost}        = $ar->{lastcost}   / $ar->{price_factor} / $basefactor;
-    $ar->{sellprice_total} = $ar->{qty} * ( $ar->{fxsellprice} * ( 1 - $ar->{discount} ) ) / $ar->{price_factor};
-    $ar->{lastcost_total}  = $ar->{qty} * $ar->{lastcost} * $basefactor;
+    $ar->{sellprice_total} = $form->round_amount( $ar->{qty} * ( $ar->{fxsellprice} * ( 1 - $ar->{discount} ) ) / $ar->{price_factor}, $form->{"decimalplaces"});
+    $ar->{lastcost_total}  = $form->round_amount( $ar->{qty} * $ar->{lastcost} * $basefactor, $form->{"decimalplaces"});
     # 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} * 100) : 0;
     # marge_total neu berechnen
@@ -313,8 +327,9 @@ sub invoice_transactions {
     if ( $form->{l_headers_mainsort} eq "Y" && ( $idx == 0 or $ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx - 1]->{ $form->{'mainsort'} } )) {
       my $headerrow = {
         # use $emptyname for mainsort header if mainsort is empty
-        data  => $ar->{$form->{'mainsort'}} || $locale->text('empty'),
-        class => "listmainsortheader",
+        data    => $ar->{$form->{'mainsort'}} || $locale->text('empty'),
+        class   => "listmainsortheader",
+        colspan => $num_visible_columns,
       };
       $report->add_data([ { description => $headerrow } ]);
 
@@ -335,8 +350,9 @@ sub invoice_transactions {
     ) {
       my $headerrow = {
         # if subsort name is defined, use that name in header, otherwise use $emptyname
-        data  => $ar->{$form->{'subsort'}} || $locale->text('empty'),
-        class => "listsubsortheader",
+        data    => $ar->{$form->{'subsort'}} || $locale->text('empty'),
+        class   => "listsubsortheader",
+        colspan => $num_visible_columns,
       };
       $report->add_data([ { description => $headerrow } ]);
     };
@@ -378,57 +394,51 @@ sub invoice_transactions {
     # wird laufend bei jeder Position neu berechnet
     $totals{marge_percent}    = $totals{sellprice_total}    ? ( ($totals{sellprice_total} - $totals{lastcost_total}) / $totals{sellprice_total}   ) * 100 : 0;
 
-    map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(marge_total marge_percent qty);
+    map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(marge_percent qty);
     map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 3) } qw(weight);
-    map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, $form->{"decimalplaces"} )} qw(lastcost sellprice sellprice_total lastcost_total);
+    map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, $form->{"decimalplaces"} )} qw(lastcost sellprice sellprice_total lastcost_total marge_total);
 
-    my $row = { };
-
-    foreach my $column (@columns) {
-      $row->{$column} = {
-        'data'  => $ar->{$column},
-        'align' => $column_alignment{$column},
-      };
-    }
+    # Einzelzeilen nur zeigen wenn l_parts gesetzt ist, nützlich, wenn man nur
+    # Subtotals und Totals sehen möchte
+    if ($form->{l_parts}) {
+      my %row = (
+        map { ($_ => { data => $ar->{$_}, align => $column_alignment{$_} }) } @columns
+      );
 
-   $row->{description}->{class} = 'listsortdescription';
+      $row{invnumber}->{link} = build_std_url("script=is.pl", 'action=edit') . "&id=" . E($ar->{id}) . "&callback=${callback}";
 
-    $row->{invnumber}->{link} = build_std_url("script=is.pl", 'action=edit')
-      . "&id=" . E($ar->{id}) . "&callback=${callback}";
+      # use partdescription according to invoice in article mode
+      $row{description}->{data} = $ar->{invoice_description};
 
-    # Einzelzeilen nur zeigen wenn l_parts gesetzt ist, nützlich, wenn man nur
-    # Subtotals und Totals sehen möchte
-    my $row_set = $form->{l_parts} ? [ $row ] : [ ];
+      $report->add_data(\%row);
+    }
 
     # hier wird bei l_subtotal nicht differenziert zwischen mainsort und subsort
     # macht man l_subtotal_mainsort aus wird l_subtotal_subsort auch nicht ausgeführt
-    if (($form->{l_subtotal_mainsort} eq 'Y')
+    if (   ($form->{l_subtotal_mainsort} eq 'Y')
+        && ($form->{l_subtotal_subsort}  eq 'Y')
         && (($idx == (scalar @{ $form->{AR} } - 1))   # last element always has a subtotal
           || ($ar->{ $form->{'subsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'subsort'}   })
           || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
           )) {   # if value that is sorted by changes, print subtotal
 
-      if ($form->{l_subtotal_subsort} eq 'Y') {
-        push @{ $row_set }, create_subtotal_row_invoice(\%subtotals2, \@columns, \%column_alignment, \@subtotal_columns, 'listsubsortsubtotal', $ar->{ $form->{'subsort'} }) ;
-        push @{ $row_set }, insert_empty_row() if $form->{l_parts} and $addemptylines;
-      };
+      $report->add_data(create_subtotal_row_invoice(\%subtotals2, \@columns, \%column_alignment, \@subtotal_columns, $form->{l_parts} ? 'listsubtotal' : undef, $ar->{ $form->{'subsort'} }));
+      $report->add_data({ %empty_row }) if $form->{l_parts} and $addemptylines;
     }
 
     # if last mainsort is reached or mainsort has changed, add mainsort subtotal and empty row
-    if (($form->{l_subtotal_mainsort} eq 'Y')
+    if (   ($form->{l_subtotal_mainsort} eq 'Y')
+        && ($form->{l_subtotal_mainsort} eq 'Y')
+        && ($form->{mainsort}            ne $form->{subsort})
         && (($idx == (scalar @{ $form->{AR} } - 1))   # last element always has a subtotal
             || ($ar->{ $form->{'mainsort'} } ne $form->{AR}->[$idx + 1]->{ $form->{'mainsort'} })
             )) {   # if value that is sorted by changes, print subtotal
-      if ($form->{l_subtotal_mainsort} eq 'Y' and $form->{mainsort} ne $form->{subsort} ) {
         # subtotal is overriden if mainsort and subsort are equal, don't print
         # subtotal line even if it is selected
-        push @{ $row_set }, create_subtotal_row_invoice(\%subtotals1, \@columns, \%column_alignment, \@subtotal_columns, 'listmainsortsubtotal', $ar->{$form->{mainsort}});
-        push @{ $row_set }, insert_empty_row() if $addemptylines; # insert empty row after mainsort
-      };
+      $report->add_data(create_subtotal_row_invoice(\%subtotals1, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal', $ar->{$form->{mainsort}}));
+      $report->add_data({ %empty_row }) if $addemptylines; # insert empty row after mainsort
     }
 
-    $report->add_data($row_set);
-
     $idx++;
   }
   if ( $form->{l_total} eq "Y" ) {
@@ -440,16 +450,6 @@ sub invoice_transactions {
   $main::lxdebug->leave_sub();
 }
 
-
-sub insert_empty_row {
-    my $dummyrow;
-    $dummyrow->{description}->{data} = "";
-    my $dummyrowset = [ $dummyrow ];
-    return $dummyrow;
-};
-
-
-
 sub create_subtotal_row_invoice {
   $main::lxdebug->enter_sub();
 
@@ -459,7 +459,7 @@ sub create_subtotal_row_invoice {
   my %myconfig = %main::myconfig;
   my $locale   = $main::locale;
 
-  my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
+  my $row = { map { $_ => { data => '', class => $class, align => $column_alignment->{$_}, } } @{ $columns } };
 
   # set name as "empty" if no value is given, except if we are dealing with the
   # absolute total, then just write "Total sum"