]> wagnertech.de Git - mfinanz.git/blobdiff - bin/mozilla/vk.pl
Refactoring: if (...) { ... } in post-if umwandeln; weniger Hilfsvariablen; knappere...
[mfinanz.git] / bin / mozilla / vk.pl
index ef99388e2ab7907c301c8476210eb74d9f31a6a4..914d465278b312e40abf025e1bcf9496d1afb3de 100644 (file)
@@ -34,6 +34,7 @@
 use POSIX qw(strftime);
 use List::Util qw(sum first);
 
+use SL::AM;
 use SL::VK;
 use SL::IS;
 use SL::ReportGenerator;
@@ -133,7 +134,7 @@ sub invoice_transactions {
 
   VK->invoice_transactions(\%myconfig, \%$form);
 
-  
+
   if ( $form->{mainsort} eq 'month' or $form->{subsort} eq 'month' ) {
 
     # Data already comes out of SELECT statement in correct month order, but
@@ -149,7 +150,7 @@ sub invoice_transactions {
   $form->{title} = $locale->text('Sales Report');
 
   @columns =
-    qw(description invnumber transdate customernumber customername partnumber partsgroup country business transdate qty unit sellprice sellprice_total discount lastcost lastcost_total marge_total marge_percent employee salesman);
+    qw(description invnumber transdate customernumber customername partnumber partsgroup country business transdate qty parts_unit weight sellprice sellprice_total discount lastcost lastcost_total marge_total marge_percent employee salesman);
 
   my @includeable_custom_variables = grep { $_->{includeable} } @{ $cvar_configs_ic }, @{ $cvar_configs_ct };
   my @searchable_custom_variables  = grep { $_->{searchable} }  @{ $cvar_configs_ic }, @{ $cvar_configs_ct };
@@ -161,8 +162,8 @@ sub invoice_transactions {
   # 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_mainsort l_headers_subsort l_subtotal_mainsort l_subtotal_subsort l_total l_parts l_customername l_customernumber transdatefrom transdateto decimalplaces customer customername customer_id department partnumber partsgroup country business description project_id customernumber salesman employee salesman_id employee_id business_id partsgroup_id mainsort subsort), 
-      "$form->{db}number", 
+  my @hidden_variables  = (qw(l_headers_mainsort l_headers_subsort l_subtotal_mainsort l_subtotal_subsort l_total l_parts l_customername l_customernumber transdatefrom transdateto decimalplaces customer customername customer_id department partnumber partsgroup country business description project_id customernumber salesman employee salesman_id employee_id business_id partsgroup_id mainsort subsort),
+      "$form->{db}number",
       map({ "cvar_$_->{name}" } @searchable_custom_variables),
       map { "l_$_" } @columns
       );
@@ -184,7 +185,8 @@ sub invoice_transactions {
     'invnumber'               => { 'text' => $locale->text('Invoice Number'), },
     'transdate'               => { 'text' => $locale->text('Invoice Date'), },
     'qty'                     => { 'text' => $locale->text('Quantity'), },
-    'unit'                    => { 'text' => $locale->text('Unit'), },
+    'parts_unit'              => { 'text' => $locale->text('Base unit'), },
+    'weight'                  => { 'text' => $locale->text('Weight'), },
     'sellprice'               => { 'text' => $locale->text('Sales price'), },
     'sellprice_total'         => { 'text' => $locale->text('Sales net amount'), },
     'lastcost_total'          => { 'text' => $locale->text('Purchase net amount'), },
@@ -203,75 +205,34 @@ sub invoice_transactions {
 
   map { $column_defs{$_}->{visible} = $form->{"l_$_"} eq 'Y' } @columns;
 
-  my %column_alignment = map { $_ => 'right' } qw(lastcost sellprice sellprice_total lastcost_total unit discount marge_total marge_percent qty);
+  my %column_alignment = map { $_ => 'right' } qw(lastcost sellprice sellprice_total lastcost_total parts_unit discount marge_total marge_percent qty weight);
+
 
-  
   # so now the check-box "Description" is only used as switch for part description in invoice-mode
   # always fill the column "Description" if we are in Zwischensummenmode
-  if (not defined $form->{"l_parts"}) {
-    $form->{"l_description"} = "Y";
-  };
+  $form->{"l_description"} = "Y" if not defined $form->{"l_parts"};;
   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
   my @options;
 
-  if ($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}";
-  }
-# TODO: es wird nur id übergeben
-  if ($form->{department}) {
-    my ($department) = split /--/, $form->{department};
-    push @options, $locale->text('Department') . " : $department";
-  }
-  if ($form->{invnumber}) {
-    push @options, $locale->text('Invoice Number') . " : $form->{invnumber}";
-  }
-  if ($form->{invdate}) {
-    push @options, $locale->text('Invoice Date') . " : $form->{invdate}";
-  }
-  if ($form->{partnumber}) {
-    push @options, $locale->text('Part Number') . " : $form->{partnumber}";
-  }
-  if ($form->{partsgroup_id}) {
-    my $partsgroup = SL::DB::PartsGroup->new(id => $form->{partsgroup_id})->load;
-    push @options, $locale->text('Group') . " : $partsgroup->{partsgroup}";
-  }
-  if ($form->{country}) {
-    push @options, $locale->text('Country') . " : $form->{country}";
-  }
-  if ($form->{employee_id}) {
-    my $employee = SL::DB::Employee->new(id => $form->{employee_id})->load;
-    push @options, $locale->text('Employee') . ' : ' . $employee->name;
-  }
-  if ($form->{salesman_id}) {
-    my $salesman = SL::DB::Employee->new(id => $form->{salesman_id})->load;
-    push @options, $locale->text('Salesman') . ' : ' . $salesman->name;
-  }
-  if ($form->{business_id}) {
-    my $business = SL::DB::Business->new(id => $form->{business_id})->load;
-    push @options, $locale->text('Customer type') . ' : ' . $business->description;
-  }
-  if ($form->{ordnumber}) {
-    push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
-  }
-  if ($form->{notes}) {
-    push @options, $locale->text('Notes') . " : $form->{notes}";
-  }
-  if ($form->{transaction_description}) {
-    push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
-  }
-  if ($form->{transdatefrom}) {
-    push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
-  }
-  if ($form->{transdateto}) {
-    push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
-  }
+  push @options, $locale->text('Description')             . " : $form->{description}"                                                       if $form->{description};
+  push @options, $locale->text('Customer')                . " : $form->{customername}"                                                      if $form->{customer};
+  push @options, $locale->text('Customer Number')         . " : $form->{customernumber}"                                                    if $form->{customernumber};
+  # TODO: es wird nur id übergeben
+  push @options, $locale->text('Department')              . " : " . (split /--/, $form->{department})[0]                                    if $form->{department};
+  push @options, $locale->text('Invoice Number')          . " : $form->{invnumber}"                                                         if $form->{invnumber};
+  push @options, $locale->text('Invoice Date')            . " : $form->{invdate}"                                                           if $form->{invdate};
+  push @options, $locale->text('Part Number')             . " : $form->{partnumber}"                                                        if $form->{partnumber};
+  push @options, $locale->text('Group')                   . " : " . SL::DB::PartsGroup->new(id => $form->{partsgroup_id})->load->partsgroup if $form->{partsgroup_id};
+  push @options, $locale->text('Country')                 . " : $form->{country}"                                                           if $form->{country};
+  push @options, $locale->text('Employee')                . ' : ' . SL::DB::Employee->new(id => $form->{employee_id})->load->name           if $form->{employee_id};
+  push @options, $locale->text('Salesman')                . ' : ' . SL::DB::Employee->new(id => $form->{salesman_id})->load->name           if $form->{salesman_id};
+  push @options, $locale->text('Customer type')           . ' : ' . SL::DB::Business->new(id => $form->{business_id})->load->description    if $form->{business_id};
+  push @options, $locale->text('Order Number')            . " : $form->{ordnumber}"                                                         if $form->{ordnumber};
+  push @options, $locale->text('Notes')                   . " : $form->{notes}"                                                             if $form->{notes};
+  push @options, $locale->text('Transaction description') . " : $form->{transaction_description}"                                           if $form->{transaction_description};
+  push @options, $locale->text('From')                    . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1)                      if $form->{transdatefrom};
+  push @options, $locale->text('Bis')                     . " " . $locale->date(\%myconfig, $form->{transdateto}, 1)                        if $form->{transdateto};
 
   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
@@ -310,47 +271,52 @@ sub invoice_transactions {
   # escape callback for href
   $callback = $form->escape($href);
 
-  my @subtotal_columns = qw(qty sellprice sellprice_total lastcost lastcost_total marge_total marge_percent discount);
+  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
   my @total_columns = qw(sellprice_total lastcost_total marge_total marge_percent );
 
-  my %totals    = map { $_ => 0 } @total_columns;
+  my %totals     = map { $_ => 0 } @total_columns;
   my %subtotals1 = map { $_ => 0 } @subtotal_columns;
   my %subtotals2 = map { $_ => 0 } @subtotal_columns;
 
   my $idx = 0;
 
+  my $basefactor;
+  my $all_units = AM->retrieve_all_units();
+
   foreach my $ar (@{ $form->{AR} }) {
+    $basefactor = $all_units->{$ar->{unit}}->{factor} / $all_units->{$ar->{parts_unit}}->{factor};
+    $basefactor = 1 unless $basefactor;
 
     $ar->{price_factor} = 1 unless $ar->{price_factor};
     # calculate individual sellprice
     # discount was already accounted for in db sellprice
-    $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->{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;
     # 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
     $ar->{marge_total} = $ar->{sellprice_total} ? $ar->{sellprice_total}-$ar->{lastcost_total}  : 0;
     $ar->{discount} *= 100;  # für Ausgabe formatieren, 10% stored as 0.1 in db
 
+    #adapt qty to the chosen unit
+    $ar->{qty} *= $basefactor;
+
+    #weight is the still the weight per part, but here we want the total weight
+    $ar->{weight} *= $ar->{qty};
+
     # Anfangshauptüberschrift
     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
-      if ( $ar->{$form->{'mainsort'}} ) {
-        $headerrow->{description}->{data} = $ar->{$form->{'mainsort'}};
-      } else {
-        $headerrow->{description}->{data} = $locale->text('empty');
+      my $headerrow = {
+        # use $emptyname for mainsort header if mainsort is empty
+        data  => $ar->{$form->{'mainsort'}} || $locale->text('empty'),
+        class => "listmainsortheader",
       };
-
-      $headerrow->{description}->{class} = "listmainsortheader";
-      my $headerrow_set = [ $headerrow ];
-      $report->add_data($headerrow_set);
+      $report->add_data([ { description => $headerrow } ]);
 
       # add empty row after main header
 #      my $emptyheaderrow->{description}->{data} = "";
@@ -360,22 +326,19 @@ sub invoice_transactions {
     };
 
     # subsort überschriften
-    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'} }
+    # special case: subsort headers only makes (aesthetical) sense if we show individual parts
+    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'} })
+        && ($form->{l_headers_subsort} eq "Y")
+        && $form->{l_parts}
     ) {
-      my $headerrow;
-
-      # if subsort name is defined, use that name in header, otherwise use $emptyname
-      if ( $ar->{$form->{'subsort'}} ) {
-        $headerrow->{description}->{data} = $ar->{$form->{'subsort'}};
-      } else {
-        $headerrow->{description}->{data} = $locale->text('empty');
+      my $headerrow = {
+        # if subsort name is defined, use that name in header, otherwise use $emptyname
+        data  => $ar->{$form->{'subsort'}} || $locale->text('empty'),
+        class => "listsubsortheader",
       };
-      $headerrow->{description}->{class} = "listsubsortheader";
-      my $headerrow_set = [ $headerrow ];
-      # special case: subsort headers only makes (aesthetical) sense if we show individual parts
-      $report->add_data($headerrow_set) if $form->{l_headers_subsort} eq "Y" and $form->{l_parts};
+      $report->add_data([ { description => $headerrow } ]);
     };
 
     map { $subtotals1{$_} += $ar->{$_};
@@ -388,7 +351,7 @@ sub invoice_transactions {
       # 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};
+      $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
@@ -396,15 +359,15 @@ sub invoice_transactions {
       # rather than leaving the last value in sellprice/lastcost
 
       $subtotals1{sellprice} = 0;
-      $subtotals1{lastcost} = 0;
+      $subtotals1{lastcost}  = 0;
     };
 
     if ( $subtotals2{qty} != 0 ) {
       $subtotals2{sellprice} = $subtotals2{sellprice_total} / $subtotals2{qty};
-      $subtotals2{lastcost} = $subtotals2{lastcost_total} / $subtotals2{qty};
+      $subtotals2{lastcost}  = $subtotals2{lastcost_total}  / $subtotals2{qty};
     } else {
       $subtotals2{sellprice} = 0;
-      $subtotals2{lastcost} = 0;
+      $subtotals2{lastcost}  = 0;
     };
 
     # Ertrag prozentual in den Summen: (summe VK - summe Ertrag) / summe VK
@@ -415,7 +378,8 @@ 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);
+    map { $ar->{$_} = $form->format_amount(\%myconfig, $ar->{$_}, 2) } qw(marge_total 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);
 
     my $row = { };
@@ -507,8 +471,8 @@ sub create_subtotal_row_invoice {
     $row->{description}->{data} = $locale->text('Total') . ' ' . $name;
   };
 
-  map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(marge_total marge_percent);
-  map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 0) } qw(qty);
+  map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(marge_total marge_percent qty);
+  map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 3) } qw(weight);
   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, $form->{decimalplaces}) } qw(lastcost sellprice sellprice_total lastcost_total);
 
 
@@ -520,4 +484,3 @@ sub create_subtotal_row_invoice {
 }
 
 1;
-