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

(-)a/Koha/Patron.pm (+84 lines)
Lines 1004-1009 sub can_request_article { Link Here
1004
    return $count < $limit ? 1 : 0;
1004
    return $count < $limit ? 1 : 0;
1005
}
1005
}
1006
1006
1007
=head3 article_request_fee
1008
1009
    my $fee = $patron->article_request_fee(
1010
        {
1011
          [ library_id => $library->id, ]
1012
        }
1013
    );
1014
1015
Returns the fee to be charged to the patron when it places an article request.
1016
1017
A I<library_id> can be passed as parameter, falling back to userenv if absent.
1018
1019
=cut
1020
1021
sub article_request_fee {
1022
    my ($self, $params) = @_;
1023
1024
    my $library_id = $params->{library_id};
1025
1026
    $library_id //= C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
1027
1028
    my $rule = Koha::CirculationRules->get_effective_rule(
1029
        {
1030
            branchcode   => $library_id,
1031
            categorycode => $self->categorycode,
1032
            rule_name    => 'article_request_fee'
1033
        }
1034
    );
1035
1036
    my $fee = ($rule) ? $rule->rule_value + 0 : 0;
1037
1038
    return $fee;
1039
}
1040
1041
=head3 add_article_request_fee_if_needed
1042
1043
    my $fee = $patron->add_article_request_fee_if_needed(
1044
        {
1045
          [ item_id    => $item->id,
1046
            library_id => $library->id, ]
1047
        }
1048
    );
1049
1050
If an article request fee needs to be charged, it adds a debit to the patron's
1051
account.
1052
1053
Returns the fee line.
1054
1055
A I<library_id> can be passed as parameter, falling back to userenv if absent.
1056
1057
=cut
1058
1059
sub add_article_request_fee_if_needed {
1060
    my ($self, $params) = @_;
1061
1062
    my $library_id = $params->{library_id};
1063
    my $item_id    = $params->{item_id};
1064
1065
    $library_id //= C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
1066
1067
    my $amount = $self->article_request_fee(
1068
        {
1069
            library_id => $library_id,
1070
        }
1071
    );
1072
1073
    my $debit_line;
1074
1075
    if ( $amount > 0 ) {
1076
        $debit_line = $self->account->add_debit(
1077
            {
1078
                amount     => $amount,
1079
                user_id    => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
1080
                interface  => C4::Context->interface,
1081
                library_id => $library_id,
1082
                type       => 'ARTICLE_REQUEST',
1083
                item_id    => $item_id,
1084
            }
1085
        );
1086
    }
1087
1088
    return $debit_line;
1089
}
1090
1007
=head3 article_requests
1091
=head3 article_requests
1008
1092
1009
    my $article_requests = $patron->article_requests;
1093
    my $article_requests = $patron->article_requests;
(-)a/t/db_dependent/Koha/Patron.t (-2 / +112 lines)
Lines 19-28 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 11;
22
use Test::More tests => 13;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::Warn;
24
use Test::Warn;
25
25
26
use Koha::CirculationRules;
26
use Koha::Database;
27
use Koha::Database;
27
use Koha::DateUtils qw(dt_from_string);
28
use Koha::DateUtils qw(dt_from_string);
28
use Koha::ArticleRequests;
29
use Koha::ArticleRequests;
Lines 820-825 subtest 'article_requests() tests' => sub { Link Here
820
821
821
    $schema->storage->txn_begin;
822
    $schema->storage->txn_begin;
822
823
824
    my $library = $builder->build_object({ class => 'Koha::Libraries' });
825
    t::lib::Mocks::mock_userenv( { branchcode => $library->id } );
826
823
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
827
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
824
828
825
    my $article_requests = $patron->article_requests;
829
    my $article_requests = $patron->article_requests;
Lines 907-912 subtest 'safe_to_delete() tests' => sub { Link Here
907
    ok( $patron->safe_to_delete, 'Can delete, all conditions met' );
911
    ok( $patron->safe_to_delete, 'Can delete, all conditions met' );
908
    my $messages = $patron->safe_to_delete->messages;
912
    my $messages = $patron->safe_to_delete->messages;
909
    is_deeply( $messages, [], 'Patron can be deleted, no messages' );
913
    is_deeply( $messages, [], 'Patron can be deleted, no messages' );
914
};
915
916
subtest 'article_request_fee() tests' => sub {
917
918
    plan tests => 3;
919
920
    $schema->storage->txn_begin;
921
922
    # Cleanup, to avoid interference
923
    Koha::CirculationRules->search( { rule_name => 'article_request_fee' } )->delete;
924
925
    t::lib::Mocks::mock_preference( 'ArticleRequests', 1 );
926
927
    my $item = $builder->build_sample_item;
928
929
    my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
930
    my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
931
    my $patron    = $builder->build_object( { class => 'Koha::Patrons' } );
932
933
    # Rule that should never be picked, because the patron's category is always picked
934
    Koha::CirculationRules->set_rule(
935
        {   categorycode => undef,
936
            branchcode   => undef,
937
            rule_name    => 'article_request_fee',
938
            rule_value   => 1,
939
        }
940
    );
941
942
    is( $patron->article_request_fee( { library_id => $library_2->id } ), 1, 'library_id used correctly' );
943
944
    Koha::CirculationRules->set_rule(
945
        {   categorycode => $patron->categorycode,
946
            branchcode   => undef,
947
            rule_name    => 'article_request_fee',
948
            rule_value   => 2,
949
        }
950
    );
951
952
    Koha::CirculationRules->set_rule(
953
        {   categorycode => $patron->categorycode,
954
            branchcode   => $library_1->id,
955
            rule_name    => 'article_request_fee',
956
            rule_value   => 3,
957
        }
958
    );
959
960
    is( $patron->article_request_fee( { library_id => $library_2->id } ), 2, 'library_id used correctly' );
961
962
    t::lib::Mocks::mock_userenv( { branchcode => $library_1->id } );
963
964
    is( $patron->article_request_fee(), 3, 'env used correctly' );
965
966
    $schema->storage->txn_rollback;
967
};
968
969
subtest 'add_article_request_fee_if_needed() tests' => sub {
970
971
    plan tests => 12;
972
973
    $schema->storage->txn_begin;
974
975
    my $amount = 0;
976
977
    my $patron_mock = Test::MockModule->new('Koha::Patron');
978
    $patron_mock->mock( 'article_request_fee', sub { return $amount; } );
979
980
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
981
982
    is( $patron->article_request_fee, $amount, 'article_request_fee mocked' );
983
984
    my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
985
    my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
986
    my $staff     = $builder->build_object( { class => 'Koha::Patrons' } );
987
    my $item      = $builder->build_sample_item;
988
989
    t::lib::Mocks::mock_userenv(
990
        { branchcode => $library_1->id, patron => $staff } );
991
992
    my $debit = $patron->add_article_request_fee_if_needed();
993
    is( $debit, undef, 'No fee, no debit line' );
994
995
    # positive value
996
    $amount = 1;
997
998
    $debit = $patron->add_article_request_fee_if_needed({ item_id => $item->id });
999
    is( ref($debit), 'Koha::Account::Line', 'Debit object type correct' );
1000
    is( $debit->amount, $amount,
1001
        'amount set to $patron->article_request_fee value' );
1002
    is( $debit->manager_id, $staff->id,
1003
        'manager_id set to userenv session user' );
1004
    is( $debit->branchcode, $library_1->id,
1005
        'branchcode set to userenv session library' );
1006
    is( $debit->debit_type_code, 'ARTICLE_REQUEST',
1007
        'debit_type_code set correctly' );
1008
    is( $debit->itemnumber, $item->id,
1009
        'itemnumber set correctly' );
1010
1011
    $amount = 100;
1012
1013
    $debit = $patron->add_article_request_fee_if_needed({ library_id => $library_2->id });
1014
    is( ref($debit), 'Koha::Account::Line', 'Debit object type correct' );
1015
    is( $debit->amount, $amount,
1016
        'amount set to $patron->article_request_fee value' );
1017
    is( $debit->branchcode, $library_2->id,
1018
        'branchcode set to userenv session library' );
1019
    is( $debit->itemnumber, undef,
1020
        'itemnumber set correctly to undef' );
910
1021
911
    $schema->storage->txn_rollback;
1022
    $schema->storage->txn_rollback;
912
};
1023
};
913
- 

Return to bug 27946