@@ -, +, @@ --- Koha/ArticleRequest.pm | 12 ++++- Koha/Exceptions/ArticleRequest.pm | 47 ++++++++++++++++ Koha/Patron.pm | 26 +++++++++ admin/categories.pl | 9 ++++ circ/request-article.pl | 48 +++++++++++------ .../prog/en/modules/admin/categories.tt | 14 +++++ .../prog/en/modules/circ/request-article.tt | 5 ++ .../en/modules/opac-request-article.tt | 7 ++- opac/opac-request-article.pl | 54 ++++++++++++------- 9 files changed, 182 insertions(+), 40 deletions(-) create mode 100644 Koha/Exceptions/ArticleRequest.pm --- a/Koha/ArticleRequest.pm +++ a/Koha/ArticleRequest.pm @@ -27,7 +27,9 @@ use Koha::Items; use Koha::Libraries; use Koha::DateUtils qw( dt_from_string ); use C4::Context; +use Koha::ArticleRequests; use Koha::Account::Lines; +use Koha::Exceptions::ArticleRequest; use base qw(Koha::Object); @@ -48,11 +50,17 @@ Koha::ArticleRequest - Koha Article Request Object class sub open { my ($self) = @_; + Koha::Exceptions::ArticleRequest::LimitReached->throw( + error => 'Patron cannot request more articles for today' + ) unless $self->borrower->can_request_article; + $self->status(Koha::ArticleRequest::Status::Pending); - if(C4::Context->preference('ArticleRequests') && $self->borrower->category->article_request_fee) { + + my $fee = $self->borrower->category->article_request_fee; + if($fee && $fee > 0) { my $debit_line = $self->borrower->account->add_debit( { - amount => $self->borrower->category->article_request_fee, + amount => $fee, user_id => C4::Context->userenv ? C4::Context->userenv->{'number'} : undef, interface => C4::Context->interface, library_id => C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef, --- a/Koha/Exceptions/ArticleRequest.pm +++ a/Koha/Exceptions/ArticleRequest.pm @@ -0,0 +1,47 @@ +package Koha::Exceptions::ArticleRequest; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Exception::Class ( + 'Koha::Exceptions::ArticleRequest' => { + description => 'Something went wrong!', + }, + 'Koha::Exceptions::ArticleRequest::LimitReached' => { + isa => 'Koha::Exceptions::ArticleRequest', + description => 'Article request limit was reached' + }, +); + +=head1 NAME + +Koha::Exceptions::ArticleRequest - Base class for ArticleRequest exceptions + +=head1 Exceptions + +=head2 Koha::Exceptions::ArticleRequest + +Generic ArticleRequest exception + +=head2 Koha::Exceptions::ArticleRequest::IsNotCredit + +Exception to be used when an action on an account line requires it to be a +credit and it isn't. + +=cut + +1; --- a/Koha/Patron.pm +++ a/Koha/Patron.pm @@ -958,6 +958,32 @@ sub move_to_deleted { return Koha::Database->new->schema->resultset('Deletedborrower')->create($patron_infos); } +=head3 can_request_article + +my $can_request = $borrower->can_request_article + +Returns true if patron can request articles + +=cut + +sub can_request_article { + my ($self) = @_; + my $limit = $self->category->article_request_limit; + + return 1 if !$limit; + + my $date = dt_from_string; + my $count = Koha::ArticleRequests->search({ + borrowernumber => $self->borrowernumber, + status => {'!=' => 'CANCELED'}, + created_on => { + '>=' => $date->date.' 00:00:00', + '<=' => $date->date.' 23:59:59' + } + })->count; + return $count < $limit ? 1 : 0; +} + =head3 article_requests my @requests = $borrower->article_requests(); --- a/admin/categories.pl +++ a/admin/categories.pl @@ -68,6 +68,7 @@ if ( $op eq 'add_validate' ) { my $min_password_length = $input->param('min_password_length'); my $require_strong_password = $input->param('require_strong_password'); my $article_request_fee = $input->param('article_request_fee'); + my $article_request_limit = $input->param('article_request_limit'); my @branches = grep { $_ ne q{} } $input->multi_param('branches'); $reset_password = undef if $reset_password eq -1; @@ -80,6 +81,12 @@ if ( $op eq 'add_validate' ) { if ($article_request_fee < 0) { push @messages, {type => 'error', code => 'article_request_negative_fee' }; $op = 'add_form'; + } elsif ($article_request_limit ne '' && $article_request_limit !~ /\d+/) { + push @messages, {type => 'error', code => 'article_request_numeric_limit' }; + $op = 'add_form'; + } elsif ($article_request_limit ne '' && $article_request_limit < 0) { + push @messages, {type => 'error', code => 'article_request_negative_limit' }; + $op = 'add_form'; } else { if ($enrolmentperioddate) { $enrolmentperioddate = output_pref( @@ -113,6 +120,7 @@ if ( $op eq 'add_validate' ) { $category->min_password_length($min_password_length); $category->require_strong_password($require_strong_password); $category->article_request_fee($article_request_fee); + $category->article_request_limit($article_request_limit); eval { $category->store; $category->replace_library_limits( \@branches ); @@ -145,6 +153,7 @@ if ( $op eq 'add_validate' ) { min_password_length => $min_password_length, require_strong_password => $require_strong_password, article_request_fee => $article_request_fee, + article_request_limit => $article_request_limit }); eval { $category->store; --- a/circ/request-article.pl +++ a/circ/request-article.pl @@ -27,6 +27,7 @@ use C4::Serials qw( CountSubscriptionFromBiblionumber ); use Koha::Biblios; use Koha::Patrons; use Koha::ArticleRequests; +use Try::Tiny; my $cgi = CGI->new; @@ -68,23 +69,29 @@ if ( $action eq 'create' ) { my $patron_notes = $cgi->param('patron_notes') || undef; my $format = $cgi->param('format') || undef; - my $ar = Koha::ArticleRequest->new( - { - borrowernumber => $borrowernumber, - biblionumber => $biblionumber, - branchcode => $branchcode, - itemnumber => $itemnumber, - title => $title, - author => $author, - volume => $volume, - issue => $issue, - date => $date, - pages => $pages, - chapters => $chapters, - patron_notes => $patron_notes, - format => $format, - } - )->store(); + try { + my $ar = Koha::ArticleRequest->new( + { + borrowernumber => $borrowernumber, + biblionumber => $biblionumber, + branchcode => $branchcode, + itemnumber => $itemnumber, + title => $title, + author => $author, + volume => $volume, + issue => $issue, + date => $date, + pages => $pages, + chapters => $chapters, + patron_notes => $patron_notes, + format => $format, + } + )->store(); + } catch { + $template->param( + error_message => $_->{message} + ); + }; } @@ -109,6 +116,13 @@ if ( !$patron && $patron_cardnumber ) { } } +if( $patron && !$patron->can_request_article) { + $patron = undef; + $template->param( + error_message => 'Patron cannot request more articles for today' + ); +} + $template->param( biblio => $biblio, patron => $patron, --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categories.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/categories.tt @@ -110,6 +110,10 @@ This patron category already exists. [% CASE 'article_request_negative_fee' %] Article request fee cannot be negative. + [% CASE 'article_request_negative_limit' %] + Maximum article request limit cannot be negative. + [% CASE 'article_request_numeric_limit' %] + Maximum article request limit must be a positive number. [% CASE %] [% m.code | html %] [% END %] @@ -209,6 +213,10 @@ +
  • + + per day +
  • [% END %]
  • @@ -535,6 +543,7 @@ Hold fee [% IF Koha.Preference('ArticleRequests') %] Article request fee + Maximum request limit [% END %] [% IF ( EnhancedMessagingPreferences ) %] Messaging @@ -600,6 +609,11 @@ [% ELSE %] - [% END %] + [% IF (category.article_request_limit > 0) %] + [% category.article_request_limit | html %] + [% ELSE %] + - + [% END %] [% END %] [% IF Koha.Preference('EnhancedMessagingPreferences') %] --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/request-article.tt +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/request-article.tt @@ -47,6 +47,11 @@

    Request article from [% INCLUDE 'biblio-title.inc' link = 1 %]

    + [% IF error_message %] +
    +

    [% error_message | html %]

    +
    + [% END %] [% IF no_patrons_found %]

    Patron not found

    --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt +++ a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-request-article.tt @@ -34,7 +34,7 @@
    - [% IF biblio.can_article_request( patron ) %] + [% IF !error_message && biblio.can_article_request( patron ) %]

    Place article request for [% biblio.title | html %]

    [% IF ( disclaimer && !action) %]
    @@ -223,6 +223,11 @@ [% END %] + [% ELSIF error_message %] +

    [% biblio.title | html %]

    +
    + [% error_message | html %] +
    [% ELSE %]

    [% biblio.title | html %]

    --- a/opac/opac-request-article.pl +++ a/opac/opac-request-article.pl @@ -26,6 +26,7 @@ use C4::Output qw( output_html_with_http_headers ); use Koha::Biblios; use Koha::Patrons; +use Try::Tiny; my $cgi = CGI->new; @@ -59,26 +60,33 @@ if ( $action eq 'create' ) { my $patron_notes = $cgi->param('patron_notes') || undef; my $format = $cgi->param('format') || undef; - my $ar = Koha::ArticleRequest->new( - { - borrowernumber => $borrowernumber, - biblionumber => $biblionumber, - branchcode => $branchcode, - itemnumber => $itemnumber, - title => $title, - author => $author, - volume => $volume, - issue => $issue, - date => $date, - pages => $pages, - chapters => $chapters, - patron_notes => $patron_notes, - format => $format, - } - )->store(); - - print $cgi->redirect("/cgi-bin/koha/opac-user.pl#opac-user-article-requests"); - exit; + try { + my $ar = Koha::ArticleRequest->new( + { + borrowernumber => $borrowernumber, + biblionumber => $biblionumber, + branchcode => $branchcode, + itemnumber => $itemnumber, + title => $title, + author => $author, + volume => $volume, + issue => $issue, + date => $date, + pages => $pages, + chapters => $chapters, + patron_notes => $patron_notes, + format => $format, + } + )->store(); + + print $cgi->redirect("/cgi-bin/koha/opac-user.pl#opac-user-article-requests"); + exit; + } catch { + exit unless $_->[0] && $_->[0] eq 'EXIT'; + $template->param( + error_message => $_->{message} + ); + }; # Should we redirect? } elsif ( !$action && C4::Context->preference('ArticleRequestsOpacHostRedirection') ) { @@ -96,6 +104,12 @@ elsif ( !$action && C4::Context->preference('ArticleRequestsOpacHostRedirection' my $patron = Koha::Patrons->find($borrowernumber); +if(!$patron->can_request_article) { + $template->param( + error_message => 'You cannot request more articles for today' + ); +} + $template->param( biblio => $biblio, patron => $patron, --