1 package SL::DB::Helper::PriceUpdater;
5 use parent qw(Exporter);
6 our @EXPORT = qw(update_prices);
14 croak('Missing parameters amount/percent') unless $params{amount} || $params{percent};
16 my @prices = ref $params{prices} eq 'ARRAY' ? @{ $params{prices} } : ( $params{prices} || 'sellprice' );
18 foreach my $field (@prices) {
19 my $rounding_error = 0;
21 foreach my $item (@{ $self->items }) {
23 if ($params{amount}) {
24 $new_price = $item->$field + $params{amount} + $rounding_error;
26 $new_price = $item->$field * $params{percent} / 100 + $rounding_error;
29 $item->$field($::form->round_amount($new_price, 2));
30 $rounding_error += $new_price - $item->$field;
32 _dbg("new_price $new_price new_price_no_err " . ($new_price - $rounding_error) . " rounded " . $item->$field .
33 " error_old " . ($rounding_error - $new_price + $item->$field) . " error_new $rounding_error");
37 return $self->calculate_prices_and_taxes if $params{calculate};
42 # $::lxdebug->message(0, __PACKAGE__ . ': ' . join(' ', @_));
53 SL::DB::Helper::PriceUpdater - Mixin for updating all prices by a fixed amount or by a percentage
59 =item C<update_prices %params>
61 Updates the prices of all items as returned by the function C<items>
62 provided by the mixing class.
64 Supported arguments via C<%params> are:
70 Absolute amount to add or subtract. Either C<amount> or C<percent>
71 must be given. Resulting prices are rounded to two significant places.
75 Percentage to set the prices to (with 100 meaning "no
76 change"). Resulting prices are rounded to two significant
77 places. Rounding errors are carried over to the next item.
79 Either C<amount> or C<percent> must be given.
83 A string or an array of strings naming the prices to update. If
84 missing only the C<sellprice> field will be updated.
88 If trueish the all prices, taxes and amounts are re-calculated by
90 L<SL::DB::Helper::PriceTaxCalculator::calculate_prices_and_taxes>.
91 Returns that function's result.
95 Returns C<$self> unless C<$params{calculate}> is trueish.
101 This mixin exports the function L</update_prices>.
109 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>