View | Details | Raw Unified | Return to bug 31051
Collapse All | Expand All

(-)a/Koha/Patron.pm (-18 / +7 lines)
Lines 2437-2463 Use the replacement price of patron's old and current issues to calculate how mu Link Here
2437
=cut
2437
=cut
2438
2438
2439
sub get_savings {
2439
sub get_savings {
2440
    my ( $self ) = @_;
2440
    my ($self) = @_;
2441
2441
2442
    my $savings = 0;
2442
    my @itemnumbers = grep { defined $_ } ( $self->old_checkouts->get_column('itemnumber'), $self->checkouts->get_column('itemnumber') );
2443
2443
2444
    # get old checkouts
2444
    return Koha::Items->search(
2445
    my @old_checkouts = $self->old_checkouts->as_list;
2445
        { itemnumber => { -in => \@itemnumbers } },
2446
    foreach my $old_checkout ( @old_checkouts ) {
2446
        {   select => [ { sum => 'me.replacementprice' } ],
2447
        if ( $old_checkout->item ) {
2447
            as     => ['total_savings']
2448
            $savings += $old_checkout->item->replacementprice;
2449
        }
2448
        }
2450
    }
2449
    )->next->get_column('total_savings') // 0;
2451
2452
    # get current checkouts
2453
    my @checkouts = $self->checkouts->as_list;
2454
    foreach my $checkout ( @checkouts ) {
2455
        if ( $checkout->item ) {
2456
            $savings += $checkout->item->replacementprice;
2457
        }
2458
    }
2459
2460
    return $savings;
2461
}
2450
}
2462
2451
2463
=head2 Internal methods
2452
=head2 Internal methods
(-)a/t/db_dependent/Koha/Patron.t (-2 / +9 lines)
Lines 1352-1358 subtest 'notify_library_of_registration()' => sub { Link Here
1352
};
1352
};
1353
1353
1354
subtest 'get_savings tests' => sub {
1354
subtest 'get_savings tests' => sub {
1355
    plan tests => 2;
1355
1356
    plan tests => 4;
1356
1357
1357
    $schema->storage->txn_begin;
1358
    $schema->storage->txn_begin;
1358
1359
Lines 1365-1370 subtest 'get_savings tests' => sub { Link Here
1365
    my $item1 = $builder->build_object({ class => 'Koha::Items' }, { value => { biblionumber => $biblio1->biblionumber, replacementprice => '5.00', holdingbranch => $library->branchcode, homebranch => $library->branchcode } });
1366
    my $item1 = $builder->build_object({ class => 'Koha::Items' }, { value => { biblionumber => $biblio1->biblionumber, replacementprice => '5.00', holdingbranch => $library->branchcode, homebranch => $library->branchcode } });
1366
    my $item2 = $builder->build_object({ class => 'Koha::Items' }, { value => { biblionumber => $biblio1->biblionumber, replacementprice => '5.00', holdingbranch => $library->branchcode, homebranch => $library->branchcode } });
1367
    my $item2 = $builder->build_object({ class => 'Koha::Items' }, { value => { biblionumber => $biblio1->biblionumber, replacementprice => '5.00', holdingbranch => $library->branchcode, homebranch => $library->branchcode } });
1367
1368
1369
    is( $patron->get_savings, 0, 'No checkouts, no savings' );
1370
1371
    # Add an old checkout with deleted itemnumber
1372
    $builder->build_object({ class => 'Koha::Old::Checkouts', value => { itemnumber => undef, borrowernumber => $patron->id } });
1373
1374
    is( $patron->get_savings, 0, 'No checkouts with itemnumber, no savings' );
1375
1368
    AddIssue( $patron->unblessed, $item1->barcode );
1376
    AddIssue( $patron->unblessed, $item1->barcode );
1369
    AddIssue( $patron->unblessed, $item2->barcode );
1377
    AddIssue( $patron->unblessed, $item2->barcode );
1370
1378
1371
- 

Return to bug 31051