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

(-)a/Koha/Patron.pm (+84 lines)
Lines 1003-1008 sub can_request_article { Link Here
1003
    return $count < $limit ? 1 : 0;
1003
    return $count < $limit ? 1 : 0;
1004
}
1004
}
1005
1005
1006
=head3 article_request_fee
1007
1008
    my $fee = $patron->article_request_fee(
1009
        {
1010
          [ library_id => $library->id, ]
1011
        }
1012
    );
1013
1014
Returns the fee to be charged to the patron when it places an article request.
1015
1016
A I<library_id> can be passed as parameter, falling back to userenv if absent.
1017
1018
=cut
1019
1020
sub article_request_fee {
1021
    my ($self, $params) = @_;
1022
1023
    my $library_id = $params->{library_id};
1024
1025
    $library_id //= C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
1026
1027
    my $rule = Koha::CirculationRules->get_effective_rule(
1028
        {
1029
            branchcode   => $library_id,
1030
            categorycode => $self->categorycode,
1031
            rule_name    => 'article_request_fee'
1032
        }
1033
    );
1034
1035
    my $fee = ($rule) ? $rule->rule_value + 0 : 0;
1036
1037
    return $fee;
1038
}
1039
1040
=head3 add_article_request_fee_if_needed
1041
1042
    my $fee = $patron->add_article_request_fee_if_needed(
1043
        {
1044
          [ item_id    => $item->id,
1045
            library_id => $library->id, ]
1046
        }
1047
    );
1048
1049
If an article request fee needs to be charged, it adds a debit to the patron's
1050
account.
1051
1052
Returns the fee line.
1053
1054
A I<library_id> can be passed as parameter, falling back to userenv if absent.
1055
1056
=cut
1057
1058
sub add_article_request_fee_if_needed {
1059
    my ($self, $params) = @_;
1060
1061
    my $library_id = $params->{library_id};
1062
    my $item_id    = $params->{item_id};
1063
1064
    $library_id //= C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
1065
1066
    my $amount = $self->article_request_fee(
1067
        {
1068
            library_id => $library_id,
1069
        }
1070
    );
1071
1072
    my $debit_line;
1073
1074
    if ( $amount > 0 ) {
1075
        $debit_line = $self->account->add_debit(
1076
            {
1077
                amount     => $amount,
1078
                user_id    => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef,
1079
                interface  => C4::Context->interface,
1080
                library_id => $library_id,
1081
                type       => 'ARTICLE_REQUEST',
1082
                item_id    => $item_id,
1083
            }
1084
        );
1085
    }
1086
1087
    return $debit_line;
1088
}
1089
1006
=head3 article_requests
1090
=head3 article_requests
1007
1091
1008
    my $article_requests = $patron->article_requests;
1092
    my $article_requests = $patron->article_requests;
(-)a/t/db_dependent/Koha/Patron.t (-2 / +114 lines)
Lines 19-28 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 10;
22
use Test::More tests => 12;
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 846-848 subtest 'article_requests() tests' => sub { Link Here
846
850
847
    $schema->storage->txn_rollback;
851
    $schema->storage->txn_rollback;
848
};
852
};
849
- 
853
854
subtest 'article_request_fee() tests' => sub {
855
856
    plan tests => 3;
857
858
    $schema->storage->txn_begin;
859
860
    # Cleanup, to avoid interference
861
    Koha::CirculationRules->search( { rule_name => 'article_request_fee' } )->delete;
862
863
    t::lib::Mocks::mock_preference( 'ArticleRequests', 1 );
864
865
    my $item = $builder->build_sample_item;
866
867
    my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
868
    my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
869
    my $patron    = $builder->build_object( { class => 'Koha::Patrons' } );
870
871
    # Rule that should never be picked, because the patron's category is always picked
872
    Koha::CirculationRules->set_rule(
873
        {   categorycode => undef,
874
            branchcode   => undef,
875
            rule_name    => 'article_request_fee',
876
            rule_value   => 1,
877
        }
878
    );
879
880
    is( $patron->article_request_fee( { library_id => $library_2->id } ), 1, 'library_id used correctly' );
881
882
    Koha::CirculationRules->set_rule(
883
        {   categorycode => $patron->categorycode,
884
            branchcode   => undef,
885
            rule_name    => 'article_request_fee',
886
            rule_value   => 2,
887
        }
888
    );
889
890
    Koha::CirculationRules->set_rule(
891
        {   categorycode => $patron->categorycode,
892
            branchcode   => $library_1->id,
893
            rule_name    => 'article_request_fee',
894
            rule_value   => 3,
895
        }
896
    );
897
898
    is( $patron->article_request_fee( { library_id => $library_2->id } ), 2, 'library_id used correctly' );
899
900
    t::lib::Mocks::mock_userenv( { branchcode => $library_1->id } );
901
902
    is( $patron->article_request_fee(), 3, 'env used correctly' );
903
904
    $schema->storage->txn_rollback;
905
};
906
907
subtest 'add_article_request_fee_if_needed() tests' => sub {
908
909
    plan tests => 12;
910
911
    $schema->storage->txn_begin;
912
913
    my $amount = 0;
914
915
    my $patron_mock = Test::MockModule->new('Koha::Patron');
916
    $patron_mock->mock( 'article_request_fee', sub { return $amount; } );
917
918
    my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
919
920
    is( $patron->article_request_fee, $amount, 'article_request_fee mocked' );
921
922
    my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
923
    my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
924
    my $staff     = $builder->build_object( { class => 'Koha::Patrons' } );
925
    my $item      = $builder->build_sample_item;
926
927
    t::lib::Mocks::mock_userenv(
928
        { branchcode => $library_1->id, patron => $staff } );
929
930
    my $debit = $patron->add_article_request_fee_if_needed();
931
    is( $debit, undef, 'No fee, no debit line' );
932
933
    # positive value
934
    $amount = 1;
935
936
    $debit = $patron->add_article_request_fee_if_needed({ item_id => $item->id });
937
    is( ref($debit), 'Koha::Account::Line', 'Debit object type correct' );
938
    is( $debit->amount, $amount,
939
        'amount set to $patron->article_request_fee value' );
940
    is( $debit->manager_id, $staff->id,
941
        'manager_id set to userenv session user' );
942
    is( $debit->branchcode, $library_1->id,
943
        'branchcode set to userenv session library' );
944
    is( $debit->debit_type_code, 'ARTICLE_REQUEST',
945
        'debit_type_code set correctly' );
946
    is( $debit->itemnumber, $item->id,
947
        'itemnumber set correctly' );
948
949
    $amount = 100;
950
951
    $debit = $patron->add_article_request_fee_if_needed({ library_id => $library_2->id });
952
    is( ref($debit), 'Koha::Account::Line', 'Debit object type correct' );
953
    is( $debit->amount, $amount,
954
        'amount set to $patron->article_request_fee value' );
955
    is( $debit->branchcode, $library_2->id,
956
        'branchcode set to userenv session library' );
957
    is( $debit->itemnumber, undef,
958
        'itemnumber set correctly to undef' );
959
960
    $schema->storage->txn_rollback;
961
};

Return to bug 27946