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

(-)a/C4/Circulation.pm (-46 / +8 lines)
Lines 3390-3401 sub GetIssuingCharges { Link Here
3390
    if ( my $item_data = $sth->fetchrow_hashref ) {
3390
    if ( my $item_data = $sth->fetchrow_hashref ) {
3391
        $item_type = $item_data->{itemtype};
3391
        $item_type = $item_data->{itemtype};
3392
        $charge    = $item_data->{rentalcharge};
3392
        $charge    = $item_data->{rentalcharge};
3393
        # FIXME This should follow CircControl
3393
        my $branch = C4::Context::mybranch();
3394
        my $branch = C4::Context::mybranch();
3394
        my $patron = Koha::Patrons->find( $borrowernumber );
3395
        my $patron = Koha::Patrons->find( $borrowernumber );
3395
        my $discount = _get_discount_from_rule($patron->categorycode, $branch, $item_type);
3396
        my $discount = Koha::CirculationRules->get_effective_rule({
3397
            categorycode => $patron->categorycode,
3398
            branchcode   => $branch,
3399
            itemtype     => $item_type,
3400
            rule_name    => 'rentaldiscount'
3401
        });
3396
        if ($discount) {
3402
        if ($discount) {
3397
            # We may have multiple rules so get the most specific
3403
            $charge = ( $charge * ( 100 - $discount->rule_value ) ) / 100;
3398
            $charge = ( $charge * ( 100 - $discount ) ) / 100;
3399
        }
3404
        }
3400
        if ($charge) {
3405
        if ($charge) {
3401
            $charge = sprintf '%.2f', $charge; # ensure no fractions of a penny returned
3406
            $charge = sprintf '%.2f', $charge; # ensure no fractions of a penny returned
Lines 3405-3453 sub GetIssuingCharges { Link Here
3405
    return ( $charge, $item_type );
3410
    return ( $charge, $item_type );
3406
}
3411
}
3407
3412
3408
# Select most appropriate discount rule from those returned
3409
sub _get_discount_from_rule {
3410
    my ($categorycode, $branchcode, $itemtype) = @_;
3411
3412
    # Set search precedences
3413
    my @params = (
3414
        {
3415
            branchcode   => $branchcode,
3416
            itemtype     => $itemtype,
3417
            categorycode => $categorycode,
3418
        },
3419
        {
3420
            branchcode   => undef,
3421
            categorycode => $categorycode,
3422
            itemtype     => $itemtype,
3423
        },
3424
        {
3425
            branchcode   => $branchcode,
3426
            categorycode => $categorycode,
3427
            itemtype     => undef,
3428
        },
3429
        {
3430
            branchcode   => undef,
3431
            categorycode => $categorycode,
3432
            itemtype     => undef,
3433
        },
3434
    );
3435
3436
    foreach my $params (@params) {
3437
        my $rule = Koha::CirculationRules->search(
3438
            {
3439
                rule_name => 'rentaldiscount',
3440
                %$params,
3441
            }
3442
        )->next();
3443
3444
        return $rule->rule_value if $rule;
3445
    }
3446
3447
    # none of the above
3448
    return 0;
3449
}
3450
3451
=head2 AddIssuingCharge
3413
=head2 AddIssuingCharge
3452
3414
3453
  &AddIssuingCharge( $checkout, $charge, $type )
3415
  &AddIssuingCharge( $checkout, $charge, $type )
(-)a/t/db_dependent/Circulation.t (-2 / +46 lines)
Lines 18-24 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
use utf8;
19
use utf8;
20
20
21
use Test::More tests => 51;
21
use Test::More tests => 52;
22
use Test::Exception;
22
use Test::Exception;
23
use Test::MockModule;
23
use Test::MockModule;
24
use Test::Deep qw( cmp_deeply );
24
use Test::Deep qw( cmp_deeply );
Lines 282-287 Koha::CirculationRules->set_rules( Link Here
282
    }
282
    }
283
);
283
);
284
284
285
subtest "GetIssuingCharges tests" => sub {
286
    plan tests => 4;
287
    my $branch_discount = $builder->build_object({ class => 'Koha::Libraries' });
288
    my $branch_no_discount = $builder->build_object({ class => 'Koha::Libraries' });
289
    Koha::CirculationRules->set_rule(
290
        {
291
            categorycode => undef,
292
            branchcode   => $branch_discount->branchcode,
293
            itemtype     => undef,
294
            rule_name    => 'rentaldiscount',
295
            rule_value   => 15
296
        }
297
    );
298
    my $itype_charge = $builder->build_object({
299
        class => 'Koha::ItemTypes',
300
        value => {
301
            rentalcharge => 10
302
        }
303
    });
304
    my $itype_no_charge = $builder->build_object({
305
        class => 'Koha::ItemTypes',
306
        value => {
307
            rentalcharge => 0
308
        }
309
    });
310
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
311
    my $item_1 = $builder->build_sample_item({ itype => $itype_charge->itemtype });
312
    my $item_2 = $builder->build_sample_item({ itype => $itype_no_charge->itemtype });
313
314
    t::lib::Mocks::mock_userenv({ branchcode => $branch_no_discount->branchcode });
315
    # For now the sub always uses the env branch, this should follow CircControl instead
316
    my ($charge, $itemtype) = GetIssuingCharges( $item_1->itemnumber, $patron->borrowernumber);
317
    is( $charge + 0, 10.00, "Charge fetched correctly when no discount exists");
318
    ($charge, $itemtype) = GetIssuingCharges( $item_2->itemnumber, $patron->borrowernumber);
319
    is( $charge + 0, 0.00, "Charge fetched correctly when no discount exists and no charge");
320
321
    t::lib::Mocks::mock_userenv({ branchcode => $branch_discount->branchcode });
322
    # For now the sub always uses the env branch, this should follow CircControl instead
323
    ($charge, $itemtype) = GetIssuingCharges( $item_1->itemnumber, $patron->borrowernumber);
324
    is( $charge + 0, 8.50, "Charge fetched correctly when discount exists");
325
    ($charge, $itemtype) = GetIssuingCharges( $item_2->itemnumber, $patron->borrowernumber);
326
    is( $charge + 0, 0.00, "Charge fetched correctly when discount exists and no charge");
327
328
};
329
285
my ( $reused_itemnumber_1, $reused_itemnumber_2 );
330
my ( $reused_itemnumber_1, $reused_itemnumber_2 );
286
subtest "CanBookBeRenewed tests" => sub {
331
subtest "CanBookBeRenewed tests" => sub {
287
    plan tests => 89;
332
    plan tests => 89;
288
- 

Return to bug 26593