}
=cut
-
-sub user_transaction_history {
- my( $self, $conn, $auth, $userid, $type ) = @_;
- my $e = new_editor(authtoken=>$auth);
- return $e->event unless $e->checkauth;
- return $e->event unless $e->allowed('VIEW_USER_TRANSACTIONS');
-
- my $api = $self->api_name;
- my @xact_finish = (xact_finish => undef ) if $api =~ /still_open/;
-
- my @xacts = @{ $e->search_money_billable_transaction(
- [ { usr => $userid, @xact_finish },
- { flesh => 1,
- flesh_fields => { mbt => [ qw/billings payments grocery circulation/ ] },
- order_by => { mbt => 'xact_start DESC' },
- }
- ]
- ) };
+sub _make_mbts {
+ my @xacts = shift;
my @mbts;
for my $x (@xacts) {
push @mbts, $s;
}
+ return @mbts;
+}
+
+sub user_transaction_history {
+ my( $self, $conn, $auth, $userid, $type ) = @_;
+ my $e = new_editor(authtoken=>$auth);
+ return $e->event unless $e->checkauth;
+ return $e->event unless $e->allowed('VIEW_USER_TRANSACTIONS');
+
+ my $api = $self->api_name;
+ my @xact_finish = (xact_finish => undef ) if $api =~ /still_open/;
+ my @xacts = @{ $e->search_money_billable_transaction(
+ [ { usr => $userid, @xact_finish },
+ { flesh => 1,
+ flesh_fields => { mbt => [ qw/billings payments grocery circulation/ ] },
+ order_by => { mbt => 'xact_start DESC' },
+ }
+ ]
+ ) };
+ my @mbts = _make_mbts( @xacts );
if(defined($type)) {
@mbts = grep { $_->xact_type eq $type } @mbts;
}
+sub _make_mbts {
+ my @xacts = shift;
+
+ my @mbts;
+ for my $x (@xacts) {
+ my $s = new Fieldmapper::money::billable_transaction_summary;
+ $s->id( $x->id );
+ $s->usr( $userid );
+ $s->xact_start( $x->xact_start );
+ $s->xact_finish( $x->xact_finish );
+
+ my $to = 0.0;
+ my $lb = undef;
+ for my $b (@{ $x->billings }) {
+ next if ($b->voided eq 'f' or !$b->voided);
+ $to += $b->amount;
+ $lb ||= $b->billing_ts;
+ if ($b->billing_ts ge $lb) {
+ $lb = $b->billing_ts;
+ $s->last_billing_note($b->note);
+ $s->last_billing_ts($b->billing_ts);
+ $s->last_billing_type($b->billing_type);
+ }
+ }
+ $s->total_owed( $to );
+
+ my $tp = 0.0;
+ my $lp = undef;
+ for my $p (@{ $x->payments }) {
+ next if ($p->voided eq 'f' or !$p->voided);
+ $tp += $p->amount;
+ $lp ||= $p->payment_ts;
+ if ($b->payment_ts ge $lp) {
+ $lp = $b->payment_ts;
+ $s->last_payment_note($b->note);
+ $s->last_payment_ts($b->payment_ts);
+ $s->last_payment_type($b->payment_type);
+ }
+ }
+ $s->total_paid( $tp );
+
+ $s->balance_owed( $s->total_owed - $s->total_paid );
+
+ $s->xact_type( 'grocery' ) if ($x->grocery);
+ $s->xact_type( 'circulation' ) if ($x->circulation);
+
+ push @mbts, $s;
+ }
+
+ return @mbts;
+}
+
__PACKAGE__->register_method (
method => 'fetch_mbts',
sub fetch_mbts {
my($s, $c, $authtoken, $id) = @_;
- my $sum = $U->cstorereq(
- 'open-ils.cstore.direct.money.billable_transaction_summary.retrieve', $id );
+ my @xacts = @{ $e->search_money_billable_transaction(
+ [ { id => $id },
+ { flesh => 1, flesh_fields => { mbt => [ qw/billings payments grocery circulation/ ] } }
+ ]
+ ) };
+
+ my ($sum) = _make_mbts(@xacts);
+
return OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_SUMMARY_NOT_FOUND', id => $id) unless $sum;
my ($reqr, $evt) = $U->checkses($authtoken);