Further update to new ledger-like internals
This commit is contained in:
parent
5a7a7184dd
commit
1cbc906f1e
22 changed files with 153 additions and 149 deletions
|
@ -19,14 +19,16 @@ sub _call_old_hooks {
|
|||
|
||||
my $data = $entry->{attributes};
|
||||
|
||||
for ($entry, $entry->contras) {
|
||||
my $item = {
|
||||
%$data,
|
||||
amount => $_->{amount},
|
||||
description => $_->{description},
|
||||
};
|
||||
for (1 .. $entry->quantity) {
|
||||
for ($entry, $entry->contras) {
|
||||
my $item = {
|
||||
%$data,
|
||||
amount => $_->{amount},
|
||||
description => $_->{description},
|
||||
};
|
||||
|
||||
RevBank::Plugins::call_hooks($hook, $self, $_->{user}, $item);
|
||||
RevBank::Plugins::call_hooks($hook, $self, $_->{user}, $item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -118,14 +120,17 @@ sub checkout {
|
|||
my %deltas;
|
||||
for my $entry (@$entries) {
|
||||
$entry->user($user);
|
||||
$deltas{$_->{user}} += $_->{amount} for $entry, $entry->contras;
|
||||
|
||||
$deltas{$_->{user}} += $_->{amount} * $entry->quantity
|
||||
for $entry, $entry->contras;
|
||||
}
|
||||
|
||||
my $transaction_id = time() - 1300000000;
|
||||
RevBank::Plugins::call_hooks("checkout", $self, $user, $transaction_id);
|
||||
|
||||
for my $account (keys %deltas) {
|
||||
RevBank::Users::update($account, $deltas{$account}, $transaction_id);
|
||||
RevBank::Users::update($account, $deltas{$account}, $transaction_id)
|
||||
if $deltas{$account} != 0;
|
||||
}
|
||||
|
||||
RevBank::Plugins::call_hooks("checkout_done", $self, $user, $transaction_id);
|
||||
|
@ -142,24 +147,45 @@ sub select_items {
|
|||
my @matches;
|
||||
for my $entry (@{ $self->{entries} }) {
|
||||
my %attributes = %{ $entry->{attributes} };
|
||||
for my $item ($entry, $entry->contras) {
|
||||
push @matches, { %attributes, %$item }
|
||||
if @_ == 1 # No key or match given: match everything
|
||||
or @_ == 2 and $entry->has_attribute($key) # Just a key
|
||||
for (1 .. $entry->quantity) {
|
||||
for my $item ($entry, $entry->contras) {
|
||||
push @matches, { %attributes, %$item }
|
||||
if @_ == 1 # No key or match given: match everything
|
||||
or @_ == 2 and $entry->has_attribute($key) # Just a key
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return @matches;
|
||||
}
|
||||
|
||||
sub entries {
|
||||
my ($self, $attribute) = @_;
|
||||
|
||||
my @entries = @{ $self->{entries} };
|
||||
return grep $_->has_attribute($attribute), @entries if defined $attribute;
|
||||
return @entries;
|
||||
}
|
||||
|
||||
sub is_multi_user {
|
||||
Carp::carp("\$cart->is_multi_user is no longer supported, ignoring");
|
||||
}
|
||||
|
||||
sub changed {
|
||||
my ($self) = @_;
|
||||
return delete $self->{changed};
|
||||
|
||||
my $changed = 0;
|
||||
for my $entry ($self->entries('changed')) {
|
||||
$entry->attribute('changed', undef);
|
||||
$changed = 1;
|
||||
}
|
||||
$changed = 1 if delete $self->{changed};
|
||||
return $changed;
|
||||
}
|
||||
|
||||
sub sum {
|
||||
my ($self) = @_;
|
||||
return List::Util::sum(map $_->{amount} * $_->quantity, @{ $self->{entries} });
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
|
|
|
@ -34,6 +34,8 @@ sub add_contra {
|
|||
amount => $amount, # should usually have opposite sign (+/-)
|
||||
description => $description,
|
||||
};
|
||||
|
||||
$self->attribute('changed', 1);
|
||||
}
|
||||
|
||||
sub has_attribute {
|
||||
|
@ -57,12 +59,13 @@ sub quantity {
|
|||
if (defined $new) {
|
||||
$new >= 0 or croak "Quantity must be positive";
|
||||
$$ref = $new;
|
||||
$self->attribute('changed', 1);
|
||||
}
|
||||
|
||||
return $$ref;
|
||||
}
|
||||
|
||||
sub multiple {
|
||||
sub multiplied {
|
||||
my ($self) = @_;
|
||||
|
||||
return $self->{quantity} != 1;
|
||||
|
@ -81,7 +84,7 @@ sub as_printable {
|
|||
$self->sanity_check;
|
||||
|
||||
my @s;
|
||||
push @s, $self->{quantity} . "x {" if $self->multiple;
|
||||
push @s, $self->{quantity} . "x {" if $self->multiplied;
|
||||
|
||||
# Normally, the implied sign is "+", and an "-" is only added for negative
|
||||
# numbers. Here, the implied sign is "-", and a "+" is only added for
|
||||
|
@ -102,7 +105,7 @@ sub as_printable {
|
|||
|
||||
}
|
||||
|
||||
push @s, "}" if $self->multiple;
|
||||
push @s, "}" if $self->multiplied;
|
||||
|
||||
return @s;
|
||||
}
|
||||
|
@ -122,10 +125,10 @@ sub as_loggable {
|
|||
my $description =
|
||||
$quantity == 1
|
||||
? $_->{description}
|
||||
: sprintf("[%fx%.2f]", $quantity, $_->{amount});
|
||||
: sprintf("[%sx %.2f] %s", $quantity, abs($_->{amount}), $_->{description});
|
||||
|
||||
push @s, sprintf(
|
||||
"%-12s %4s EUR %5.2f %s",
|
||||
"%-12s %4s EUR %5.2f # %s",
|
||||
$_->{user},
|
||||
($total > 0 ? 'GAIN' : $total < 0 ? 'LOSE' : ''),
|
||||
abs($total),
|
||||
|
@ -143,6 +146,7 @@ sub user {
|
|||
croak "User can only be set once" if defined $self->{user};
|
||||
|
||||
$self->{user} = $new;
|
||||
$self->attribute('changed', 1);
|
||||
$_->{description} =~ s/\$you/$new/g for $self, @{ $self->{contras} };
|
||||
}
|
||||
|
||||
|
|
|
@ -24,8 +24,12 @@ sub hook_plugin_fail {
|
|||
sub hook_cart_changed {
|
||||
my ($class, $cart) = @_;
|
||||
$cart->size or return;
|
||||
say "Pending:";
|
||||
$cart->display;
|
||||
say "Enter username to pay/finish or 'abort' to abort.\n";
|
||||
|
||||
my $sum = $cart->sum;
|
||||
my $what = $sum > 0 ? "add %.2f" : "pay %.2f";
|
||||
say sprintf "Enter username to $what; type 'abort' to abort.\n", abs $sum;
|
||||
}
|
||||
|
||||
sub hook_abort {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue