X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/ed3be96543e7f224f5a406a73b61f4dc7e8a31e2..3b70d2a0bcd5b89df940495f1431f66eabe0a21e:/SL/DB/Helper/PriceUpdater.pm
diff --git a/SL/DB/Helper/PriceUpdater.pm b/SL/DB/Helper/PriceUpdater.pm
new file mode 100644
index 000000000..a927f0e19
--- /dev/null
+++ b/SL/DB/Helper/PriceUpdater.pm
@@ -0,0 +1,111 @@
+package SL::DB::Helper::PriceUpdater;
+
+use strict;
+
+use parent qw(Exporter);
+our @EXPORT = qw(update_prices);
+
+use Carp;
+
+sub update_prices {
+ my $self = shift;
+ my %params = @_;
+
+ croak('Missing parameters amount/percent') unless $params{amount} || $params{percent};
+
+ my @prices = ref $params{prices} eq 'ARRAY' ? @{ $params{prices} } : ( $params{prices} || 'sellprice' );
+
+ foreach my $field (@prices) {
+ my $rounding_error = 0;
+
+ foreach my $item (@{ $self->items }) {
+ my $new_price;
+ if ($params{amount}) {
+ $new_price = $item->$field + $params{amount} + $rounding_error;
+ } else {
+ $new_price = $item->$field * $params{percent} / 100 + $rounding_error;
+ }
+
+ $item->$field($::form->round_amount($new_price, 2));
+ $rounding_error += $new_price - $item->$field;
+
+ _dbg("new_price $new_price new_price_no_err " . ($new_price - $rounding_error) . " rounded " . $item->$field .
+ " error_old " . ($rounding_error - $new_price + $item->$field) . " error_new $rounding_error");
+ }
+ }
+
+ return $self->calculate_prices_and_taxes if $params{calculate};
+ return $self;
+}
+
+sub _dbg {
+ # $::lxdebug->message(0, __PACKAGE__ . ': ' . join(' ', @_));
+}
+
+1;
+
+__END__
+
+=encoding utf8
+
+=head1 NAME
+
+SL::DB::Helper::PriceUpdater - Mixin for updating all prices by a fixed amount or by a percentage
+
+=head1 FUNCTIONS
+
+=over 4
+
+=item C
+
+Updates the prices of all items as returned by the function C
+provided by the mixing class.
+
+Supported arguments via C<%params> are:
+
+=over 2
+
+=item C
+
+Absolute amount to add or subtract. Either C or C
+must be given. Resulting prices are rounded to two significant places.
+
+=item C
+
+Percentage to set the prices to (with 100 meaning "no
+change"). Resulting prices are rounded to two significant
+places. Rounding errors are carried over to the next item.
+
+Either C or C must be given.
+
+=item C
+
+A string or an array of strings naming the prices to update. If
+missing only the C field will be updated.
+
+=item C
+
+If trueish the all prices, taxes and amounts are re-calculated by
+calling
+L.
+Returns that function's result.
+
+=back
+
+Returns C<$self> unless C<$params{calculate}> is trueish.
+
+=back
+
+=head1 EXPORTS
+
+This mixin exports the function L.
+
+=head1 BUGS
+
+Nothing here yet.
+
+=head1 AUTHOR
+
+Moritz Bunkus Em.bunkus@linet-services.deE
+
+=cut