#!perl use RevBank::Products qw(read_products); HELP1 "" => "Add a product to pending transaction"; sub command :Tab(&tab) ($self, $cart, $command, @) { $command =~ /\S/ or return NEXT; $command =~ /^\+/ and return NEXT; my $products = read_products; my $product = $products->{ $command } or return NEXT; my $price = $product->{price}; my $contra_desc = "\$you bought $product->{description}"; my @addons = @{ $product->{addons} // [] }; my $display = undef; $display = "Product" if @addons and $price->cents > 0; $display = "Reimbursement" if @addons and $price->cents < 0; my $entry = $cart->add( -$price, $product->{description}, { product_id => $product->{id}, plugin => $self->id, product => $product, deduplicate => join("/", $self->id, $product->{id}), } ); $entry->add_contra( $product->{contra}, +$price, $contra_desc, $display ); for my $addon (@addons) { my $addon_price = $addon->{price}; if ($addon->{percent}) { my $sum = List::Util::sum map { $_->{amount} } grep { $_->{user} eq $addon->{contra} } $entry->contras; $addon_price = $addon_price / 100 * $sum; } $entry->amount( $entry->amount - $addon_price ); $entry->add_contra( $addon->{contra}, $addon_price, "$addon->{description} ($contra_desc)", $addon->{description} ); } return ACCEPT; } sub tab { return grep !/^\+/, grep /\D/, keys %{ read_products() }; }