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

(-)a/Koha/Patron.pm (-4 / +18 lines)
Lines 31-36 use Koha::ArticleRequests; Link Here
31
use C4::Letters;
31
use C4::Letters;
32
use Koha::AuthUtils;
32
use Koha::AuthUtils;
33
use Koha::Checkouts;
33
use Koha::Checkouts;
34
use Koha::CirculationRules;
34
use Koha::Club::Enrollments;
35
use Koha::Club::Enrollments;
35
use Koha::Database;
36
use Koha::Database;
36
use Koha::DateUtils qw( dt_from_string );
37
use Koha::DateUtils qw( dt_from_string );
Lines 960-975 sub move_to_deleted { Link Here
960
961
961
=head3 can_request_article
962
=head3 can_request_article
962
963
963
    if ( $patron->can_request_article ) { ... }
964
    if ( $patron->can_request_article( $library->id ) ) { ... }
964
965
965
Returns true if the patron can request articles. As limits apply for the patron
966
Returns true if the patron can request articles. As limits apply for the patron
966
on on the same day, those completed the same day are considered as current.
967
on the same day, those completed the same day are considered as current.
968
969
A I<library_id> can be passed as parameter, falling back to userenv if absent.
967
970
968
=cut
971
=cut
969
972
970
sub can_request_article {
973
sub can_request_article {
971
    my ($self) = @_;
974
    my ($self, $library_id) = @_;
972
    my $limit = $self->category->article_request_limit;
975
976
    $library_id //= C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
977
978
    my $rule = Koha::CirculationRules->get_effective_rule(
979
        {
980
            branchcode   => $library_id,
981
            categorycode => $self->categorycode,
982
            rule_name    => 'max_daily_article_requests'
983
        }
984
    );
985
986
    my $limit = ($rule) ? $rule->rule_value : undef;
973
987
974
    return 1 unless defined $limit;
988
    return 1 unless defined $limit;
975
989
(-)a/t/db_dependent/Koha/Patron.t (-116 / +45 lines)
Lines 19-25 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 => 9;
23
use Test::Exception;
23
use Test::Exception;
24
use Test::Warn;
24
use Test::Warn;
25
25
Lines 720-726 subtest 'can_log_into() tests' => sub { Link Here
720
720
721
subtest 'can_request_article() tests' => sub {
721
subtest 'can_request_article() tests' => sub {
722
722
723
    plan tests => 13;
723
    plan tests => 4;
724
724
725
    $schema->storage->txn_begin;
725
    $schema->storage->txn_begin;
726
726
Lines 728-862 subtest 'can_request_article() tests' => sub { Link Here
728
728
729
    my $item = $builder->build_sample_item;
729
    my $item = $builder->build_sample_item;
730
730
731
    my $category = $builder->build_object(
731
    my $library_1 = $builder->build_object( { class => 'Koha::Libraries' } );
732
        {
732
    my $library_2 = $builder->build_object( { class => 'Koha::Libraries' } );
733
            class => 'Koha::Patron::Categories',
733
    my $patron    = $builder->build_object( { class => 'Koha::Patrons' } );
734
            value => {
735
                article_request_limit => 1
736
            }
737
        }
738
    );
739
    my $patron = $builder->build_object(
740
        {
741
            class => 'Koha::Patrons',
742
            value => {
743
                categorycode => $category->categorycode
744
            },
745
        }
746
    );
747
734
748
    is( $patron->can_request_article,
735
    t::lib::Mocks::mock_userenv( { branchcode => $library_2->id } );
749
        1, 'There are no AR, so patron can request more articles' );
750
736
751
    my $article_request_1 = Koha::ArticleRequest->new(
737
    Koha::CirculationRules->set_rule(
752
        {
738
        {
753
            borrowernumber => $patron->id,
739
            categorycode => undef,
754
            biblionumber   => $item->biblionumber,
740
            branchcode   => $library_1->id,
755
            itemnumber     => $item->itemnumber,
741
            rule_name    => 'max_daily_article_requests',
756
            title          => 'an article request',
742
            rule_value   => 4,
757
        }
743
        }
758
    )->request;
759
760
    is( $patron->can_request_article,
761
        0, 'Limit is 1, so patron cannot request more articles' );
762
    is( $patron->article_requests->count,
763
        1, 'There is one current article request' );
764
765
    throws_ok {
766
        Koha::ArticleRequest->new(
767
            {
768
                borrowernumber => $patron->id,
769
                biblionumber   => $item->biblionumber,
770
                itemnumber     => $item->itemnumber,
771
                title          => 'a second article request',
772
            }
773
        )->request;
774
    }
775
    'Koha::Exceptions::ArticleRequest::LimitReached',
776
      'When limit was reached and we ask for a new AR, Limit reached is thrown';
777
778
    is( $patron->can_request_article,
779
        0, 'There is still an AR, so patron cannot request more articles' );
780
    is( $patron->article_requests->count,
781
        1, 'There is still one article request' );
782
783
    $article_request_1->complete();
784
785
    is( $patron->can_request_article, 0,
786
'AR was completed but within one day, so patron cannot request more articles'
787
    );
744
    );
788
745
789
    $article_request_1->updated_on( dt_from_string->add( days => -2 ) )
746
    $builder->build_object(
790
      ->store();
791
792
    is( $patron->can_request_article, 1,
793
'There are no completed AR within one day, so patron can request more articles'
794
    );
795
796
    my $article_request_3 = Koha::ArticleRequest->new(
797
        {
747
        {
798
            borrowernumber => $patron->id,
748
            class => 'Koha::ArticleRequests',
799
            biblionumber   => $item->biblionumber,
749
            value => { status => 'REQUESTED', borrowernumber => $patron->id }
800
            itemnumber     => $item->itemnumber,
801
            title          => 'a third article request',
802
        }
750
        }
803
    )->request;
751
    );
804
752
    $builder->build_object(
805
    is( $patron->can_request_article,
806
        0, 'A new AR was created, so patron cannot request more articles' );
807
    is( $patron->article_requests->count, 2, 'There are 2 article requests' );
808
809
    $article_request_3->cancel();
810
811
    is( $patron->can_request_article,
812
        1, 'New AR was cancelled, so patron can request more articles' );
813
814
    my $article_request_4 = Koha::ArticleRequest->new(
815
        {
753
        {
816
            borrowernumber => $patron->id,
754
            class => 'Koha::ArticleRequests',
817
            biblionumber   => $item->biblionumber,
755
            value => { status => 'PENDING', borrowernumber => $patron->id }
818
            itemnumber     => $item->itemnumber,
819
            title          => 'an fourth article request',
820
        }
756
        }
821
    )->request;
822
823
    $article_request_4->updated_on( dt_from_string->add( days => -30 ) )
824
      ->store();
825
826
    is( $patron->can_request_article, 0,
827
'There is an old AR but not completed or cancelled, so patron cannot request more articles'
828
    );
757
    );
829
    is( $patron->article_requests->count,
758
    $builder->build_object(
830
        3, 'There are 3 current article requests' );
831
832
    $schema->storage->txn_rollback;
833
};
834
835
subtest 'can_request_article() tests' => sub {
836
837
    plan tests => 3;
838
839
    $schema->storage->txn_begin;
840
841
    my $category = $builder->build_object(
842
        {
759
        {
843
            class => 'Koha::Patron::Categories',
760
            class => 'Koha::ArticleRequests',
844
            value => { article_request_limit => 4 }
761
            value => { status => 'PROCESSING', borrowernumber => $patron->id }
845
        }
762
        }
846
    );
763
    );
847
    my $patron = $builder->build_object(
764
    $builder->build_object(
848
        {
765
        {
849
            class => 'Koha::Patrons',
766
            class => 'Koha::ArticleRequests',
850
            value => { categorycode => $category->id }
767
            value => { status => 'CANCELED', borrowernumber => $patron->id }
851
        }
768
        }
852
    );
769
    );
853
770
854
    $builder->build_object( { class => 'Koha::ArticleRequests', value => { status => 'REQUESTED',  borrowernumber => $patron->id } } );
771
    ok(
855
    $builder->build_object( { class => 'Koha::ArticleRequests', value => { status => 'PENDING',    borrowernumber => $patron->id } } );
772
        $patron->can_request_article( $library_1->id ),
856
    $builder->build_object( { class => 'Koha::ArticleRequests', value => { status => 'PROCESSING', borrowernumber => $patron->id } } );
773
        '3 current requests, 4 is the limit: allowed'
857
    $builder->build_object( { class => 'Koha::ArticleRequests', value => { status => 'CANCELED',   borrowernumber => $patron->id } } );
774
    );
858
859
    ok( $patron->can_request_article, 'Patron has 3 current requests, 4 is the limit: allowed' );
860
775
861
    # Completed request, same day
776
    # Completed request, same day
862
    my $completed = $builder->build_object(
777
    my $completed = $builder->build_object(
Lines 869-875 subtest 'can_request_article() tests' => sub { Link Here
869
        }
784
        }
870
    );
785
    );
871
786
872
    ok( !$patron->can_request_article, 'Patron has 3 current requests and a completed one the same day: denied' );
787
    ok( !$patron->can_request_article( $library_1->id ),
788
        '3 current requests and a completed one the same day: denied' );
873
789
874
    $completed->updated_on(
790
    $completed->updated_on(
875
        dt_from_string->add( days => -1 )->set(
791
        dt_from_string->add( days => -1 )->set(
Lines 879-885 subtest 'can_request_article() tests' => sub { Link Here
879
        )
795
        )
880
    )->store;
796
    )->store;
881
797
882
    ok( $patron->can_request_article, 'Patron has 3 current requests and a completed one the day before: allowed' );
798
    ok( $patron->can_request_article( $library_1->id ),
799
        '3 current requests and a completed one the day before: allowed' );
800
801
    Koha::CirculationRules->set_rule(
802
        {
803
            categorycode => undef,
804
            branchcode   => $library_2->id,
805
            rule_name    => 'max_daily_article_requests',
806
            rule_value   => 3,
807
        }
808
    );
809
810
    ok( !$patron->can_request_article,
811
        'Not passing the library_id param makes it fallback to userenv: denied'
812
    );
883
813
884
    $schema->storage->txn_rollback;
814
    $schema->storage->txn_rollback;
885
};
815
};
886
- 

Return to bug 27945