SuSa angepasst, so dass auch bei EUR korrekte Werte berechnet werden
[kivitendo-erp.git] / bin / mozilla / ic.pl
index 05aaced..fad711c 100644 (file)
@@ -33,6 +33,7 @@
 
 use POSIX qw(strftime);
 use List::Util qw(max);
+use List::MoreUtils qw(any);
 
 use SL::AM;
 use SL::IC;
@@ -131,12 +132,42 @@ sub confirm_price_update {
 
   $auth->assert('part_service_assembly_edit');
 
+  my @errors      = ();
+  my $value_found = false;
+
+  foreach my $idx (qw(sellprice listprice), (1..$form->{price_rows})) {
+    my $name      = $idx =~ m/\d/ ? $form->{"pricegroup_${idx}"}      : $idx eq 'sellprice' ? $locale->text('Sell Price') : $locale->text('List Price');
+    my $type      = $idx =~ m/\d/ ? $form->{"pricegroup_type_${idx}"} : $form->{"${idx}_type"};
+    my $value_idx = $idx =~ m/\d/ ? "price_${idx}" : $idx;
+    my $value     = $form->parse_amount(\%myconfig, $form->{$value_idx});
+
+    if ((0 > $value) && ($type eq 'percent')) {
+      push @errors, $locale->text('You cannot adjust the price for pricegroup "#1" by a negative percentage.', $name);
+
+    } elsif (!$value && ($form->{$value_idx} ne '')) {
+      push @errors, $locale->text('No valid number entered for pricegroup "#1".', $name);
+
+    } elsif (0 < $value) {
+      $value_found = 1;
+    }
+  }
+
+  push @errors, $locale->text('No prices will be updated because no prices have been entered.') if (!$value_found);
+
+  my $num_matches = IC->get_num_matches_for_priceupdate();
+
+  $form->header();
+
+  if (@errors) {
+    $form->show_generic_error(join('<br>', @errors), 'back_button' => 1);
+  }
+
   $form->{nextsub} = "update_prices";
-  $form->header;
 
   map { delete $form->{$_} } qw(action header);
 
-  print $form->parse_html_template('ic/confirm_price_update', { HIDDENS => [ map { name => $_, value => $form->{$_} }, keys %$form ] });
+  print $form->parse_html_template('ic/confirm_price_update', { HIDDENS     => [ map { name => $_, value => $form->{$_} }, keys %$form ],
+                                                                num_matches => $num_matches });
 
   $lxdebug->leave_sub();
 }
@@ -146,8 +177,10 @@ sub update_prices {
 
   $auth->assert('part_service_assembly_edit');
 
-  if (IC->update_prices(\%myconfig, \%$form)) {
-    $form->redirect($form->{update_count} . $locale->text('prices updated!'));
+  my $num_updated = IC->update_prices(\%myconfig, \%$form);
+
+  if (-1 != $num_updated) {
+    $form->redirect($locale->text('#1 prices were updated.', $num_updated));
   } else {
     $form->error($locale->text('Could not update prices!'));
   }
@@ -1541,10 +1574,15 @@ sub assembly_row {
     # save form variables in a previousform variable
     foreach my $key (sort keys %$form) {
       next unless ref $form->{$key} eq '' && $form->{$key};
+      next if $key =~ /^select/; # get rid of these stupid select things
 
       # escape ampersands
       $form->{$key} =~ s/&/%26/g;
-      $previousform .= qq|$key=$form->{$key}&|;
+      if (any { $key eq $_ } qw(sellprice listprice lastcost)) {
+        $previousform .= sprintf qq|%s=%s&|, $key, $form->format_amount(\%myconfig, $form->{$key});
+      } else {
+        $previousform .= qq|$key=$form->{$key}&|;
+      }
     }
     chop $previousform;
     $previousform = $form->escape($form->escape($previousform, 1));
@@ -1566,11 +1604,11 @@ sub assembly_row {
     . $locale->text('Part Number')
     . qq|</th>|;
   $column_header{description} =
-    qq|<th nowrap width=50%>| . $locale->text('Part Description') . qq|</th>|;
+    qq|<th nowrap width=50% align="left">| . $locale->text('Part Description') . qq|</th>|;
   $column_header{lastcost} =
-    qq|<th nowrap width=50%>| . $locale->text('Purchase Price') . qq|</th>|;
+    qq|<th nowrap width=50%>| . $locale->text('Purchase Prices') . qq|</th>|;
   $column_header{total} =
-    qq|<th align=right nowrap>| . $locale->text('Extended') . qq|</th>|;
+    qq|<th align=right nowrap>| . $locale->text('Sale Prices') . qq|</th>|;
   $column_header{bom}        = qq|<th>| . $locale->text('BOM') . qq|</th>|;
   $column_header{partsgroup} = qq|<th>| . $locale->text('Group') . qq|</th>|;
 
@@ -1594,9 +1632,9 @@ sub assembly_row {
     $form->{"partnumber_$i"} =~ s/\"/&quot;/g;
 
     $linetotal =
-      $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / ($form->{"price_factor_$i"} || 1), 2);
+      $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / ($form->{"price_factor_$i"} || 1), 4);
     $line_purchase_price  =
-      $form->round_amount($form->{"lastcost_$i"} * $form->{"qty_$i"} / ($form->{"price_factor_$i"} || 1), 2); #lastcost == purchase_price | ungenaue datenbankfeld-ΓΌbersetzung
+      $form->round_amount($form->{"lastcost_$i"} * $form->{"qty_$i"} / ($form->{"price_factor_$i"} || 1), 4); #lastcost == purchase_price | ungenaue datenbankfeld-uebersetzung
     $form->{assemblytotal} += $linetotal;
     $form->{assembly_purchase_price_total}  += $line_purchase_price;
 
@@ -1679,6 +1717,7 @@ sub assembly_row {
         </tr>
   <input type=hidden name="id_$i" value=$form->{"id_$i"}>
   <input type=hidden name="sellprice_$i" value=$form->{"sellprice_$i"}>
+  <input type=hidden name="lastcost_$i" value=$form->{"lastcost_$i"}>
   <input type=hidden name="weight_$i" value=$form->{"weight_$i"}>
   <input type=hidden name="price_factor_id_$i" value=$form->{"price_factor_id_$i"}>
   <input type=hidden name="price_factor_$i" value=$form->{"price_factor_$i"}>
@@ -1686,6 +1725,16 @@ sub assembly_row {
   }
 
   print qq|
+       <tr>
+         <td colspan="6"></td>
+         <td>| . $locale->text('Totals') . qq|</td>
+          <td align="right">| . $form->format_amount(\%myconfig, $form->{assembly_purchase_price_total}, 2) .
+       qq|</td>        
+        <td align="right">| . $form->format_amount(\%myconfig, $form->{assemblytotal}, 2) .
+       qq|  </td>
+         </tr>
+          <input type="hidden" name="assembly_rows" value="| . $form->{assembly_rows} . 
+      qq|">
       </table>
     </td>
   </tr>
@@ -1697,6 +1746,9 @@ sub assembly_row {
 sub update {
   $lxdebug->enter_sub();
 
+  # parse pricegroups. and no, don't rely on check_form for this...
+  map { $form->{"price_$_"} = $form->parse_amount(\%myconfig, $form->{"price_$_"}) } 1 .. $form->{price_rows};
+
   if ($form->{item} eq "assembly") {
     my $i = $form->{assembly_rows};
 
@@ -1829,7 +1881,8 @@ sub save {
       # change/add values for assembly item
       map { $form->{"${_}_$i"} = $newform{$_} } qw(partnumber description bin unit weight listprice sellprice inventory_accno income_accno expense_accno price_factor_id);
 
-      $form->{sellprice} += $form->{"sellprice_$i"} * $form->{"qty_$i"};
+      # das ist __voll__ bekloppt, dass so auszurechnen jb 22.5.09
+      #$form->{sellprice} += $form->{"sellprice_$i"} * $form->{"qty_$i"};
       $form->{weight}    += $form->{"weight_$i"} * $form->{"qty_$i"};
 
     } else {