From eddf8d3609d6edcdaf9a9002e7493b29e1be379f Mon Sep 17 00:00:00 2001 From: Paul Poulain Date: Wed, 11 May 2011 16:54:56 +0200 Subject: [PATCH] Bug 6328 fine in days does not work Some code coming from BibLibre has been lost in the process of inclusion in 3.4. The result is that fine in days does not work at all (you can setup rules, but it does nothing) Step to reproduce: - Koha > Admin > circ rules > set 1 day fine every day of overdue for default rule - Issue a book return date last week - check-in the book => no debarment is set The following patch will fix all of those problems by : * updating borrowers.debarred to a date field (instead of tinyint). It contains the limit of the debarment * changing API of DebarMember and UpdateBorrowerDebarred to pass a date * display debarrdate where applicable. Note that a debarrdate of 31/12/9999 is considered as unlimited and not displayed * added a debarrcomment, usefull to explain why a patron is debarred (this is independant from debarrdate changes and can be used when placing an unlimited debarment too) [2011-05-12] F. Demians. It works as described. And I can confirm this functionality is impatiently awaited by French libraries since one year. Thanks BibLibre for the good work and for contributing this code. Bug 6328 Followup--update DB structure Thanks Katrin. Bug 6328: make comment a textbox / fix debar by notice trigger Debarring by notice triggers was broken, because the new function expects a date as second parameter. The comment field in patron account details was a very long text field. Patch changes it to be a textbox instead. Bug 6328: Lift debarment leaves patron account 'Lift debarment' redirects to an empty circulation page. BZ6328 follow-up 3 Fixes comment 23 from Fernando L. Canizo : when the patron was debarred and debar removed he still could not check-out. The changes in the IsMemberBlocked (that were on biblibre/master) were lost somewhere The sub was still checking for old_issues instead of calling CheckBorrowerDebarred to get a debardate if applicable Note : this bug was appearing only is you had issuing rules defined for itemtype/categorycode/branch. Seemed to work if you had only default rules. That's probably why it hadn't been spotted before --- C4/Circulation.pm | 60 ++++++++++++++++++++ C4/Members.pm | 48 ++++------------ C4/Overdues.pm | 23 ++++---- circ/circulation.pl | 11 ++++ circ/returns.pl | 7 ++- installer/data/mysql/kohastructure.sql | 6 +- installer/data/mysql/updatedatabase.pl | 12 ++++ .../prog/en/modules/circ/circulation.tt | 11 ++++ .../intranet-tmpl/prog/en/modules/circ/returns.tt | 3 + .../prog/en/modules/members/memberentrygen.tt | 35 +++++++++++- .../prog/en/modules/members/moremember.tt | 7 +- koha-tmpl/opac-tmpl/prog/en/modules/opac-user.tt | 2 +- members/memberentry.pl | 19 +++++- members/moremember.pl | 12 ++++- members/setstatus.pl | 4 +- misc/cronjobs/overdue_notices.pl | 2 +- opac/opac-user.pl | 1 + 17 files changed, 200 insertions(+), 63 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index 47068f9..0dc6c38 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -43,6 +43,7 @@ use Date::Calc qw( Date_to_Days Day_of_Week Add_Delta_Days + check_date ); use POSIX qw(strftime); use C4::Branch; # GetBranches @@ -1626,6 +1627,10 @@ sub AddReturn { if ($borrowernumber) { my $fix = _FixOverduesOnReturn($borrowernumber, $item->{itemnumber}, $exemptfine, $dropbox); defined($fix) or warn "_FixOverduesOnReturn($borrowernumber, $item->{itemnumber}...) failed!"; # zero is OK, check defined + + # fix fine days + my $debardate = _FixFineDaysOnReturn( $borrower, $item, $issue->{date_due} ); + $messages->{'Debarred'} = $debardate if ($debardate); } # find reserves..... @@ -1749,6 +1754,61 @@ sub MarkIssueReturned { $sth_del->execute($borrowernumber, $itemnumber); } +=head2 _FixFineDaysOnReturn + + &_FixFineDaysOnReturn($borrower, $item, $datedue); + +C<$borrower> borrower hashref + +C<$item> item hashref + +C<$datedue> date due + +Internal function, called only by AddReturn that calculate and update the user fine days, and debars him + +=cut + +sub _FixFineDaysOnReturn { + my ( $borrower, $item, $datedue ) = @_; + + if ($datedue) { + $datedue = C4::Dates->new( $datedue, "iso" ); + } else { + return; + } + + my $branchcode = _GetCircControlBranch( $item, $borrower ); + my $calendar = C4::Calendar->new( branchcode => $branchcode ); + my $today = C4::Dates->new(); + + my $deltadays = $calendar->daysBetween( $datedue, C4::Dates->new() ); + + my $circcontrol = C4::Context::preference('CircControl'); + my $issuingrule = GetIssuingRule( $borrower->{categorycode}, $item->{itype}, $branchcode ); + my $finedays = $issuingrule->{finedays}; + + # exit if no finedays defined + return unless $finedays; + my $grace = $issuingrule->{firstremind}; + + if ( $deltadays - $grace > 0 ) { + my @newdate = Add_Delta_Days( Today(), $deltadays * $finedays ); + my $isonewdate = join( '-', @newdate ); + my ( $deby, $debm, $debd ) = split( /-/, $borrower->{debarred} ); + if ( check_date( $deby, $debm, $debd ) ) { + my @olddate = split( /-/, $borrower->{debarred} ); + + if ( Delta_Days( @olddate, @newdate ) > 0 ) { + C4::Members::DebarMember( $borrower->{borrowernumber}, $isonewdate ); + return $isonewdate; + } + } else { + C4::Members::DebarMember( $borrower->{borrowernumber}, $isonewdate ); + return $isonewdate; + } + } +} + =head2 _FixOverduesOnReturn &_FixOverduesOnReturn($brn,$itm, $exemptfine, $dropboxmode); diff --git a/C4/Members.pm b/C4/Members.pm index dcc168b..8dcd8e0 100644 --- a/C4/Members.pm +++ b/C4/Members.pm @@ -659,39 +659,12 @@ sub IsMemberBlocked { my $borrowernumber = shift; my $dbh = C4::Context->dbh; - # does patron have current fine days? - my $strsth=qq{ - SELECT - ADDDATE(returndate, finedays * DATEDIFF(returndate,date_due) ) AS blockingdate, - DATEDIFF(ADDDATE(returndate, finedays * DATEDIFF(returndate,date_due)),NOW()) AS blockedcount - FROM old_issues - }; - if(C4::Context->preference("item-level_itypes")){ - $strsth.= - qq{ LEFT JOIN items ON (items.itemnumber=old_issues.itemnumber) - LEFT JOIN issuingrules ON (issuingrules.itemtype=items.itype)} - }else{ - $strsth .= - qq{ LEFT JOIN items ON (items.itemnumber=old_issues.itemnumber) - LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber) - LEFT JOIN issuingrules ON (issuingrules.itemtype=biblioitems.itemtype) }; - } - $strsth.= - qq{ WHERE finedays IS NOT NULL - AND date_due < returndate - AND borrowernumber = ? - ORDER BY blockingdate DESC, blockedcount DESC - LIMIT 1}; - my $sth=$dbh->prepare($strsth); - $sth->execute($borrowernumber); - my $row = $sth->fetchrow_hashref; - my $blockeddate = $row->{'blockeddate'}; - my $blockedcount = $row->{'blockedcount'}; + my $blockeddate = CheckBorrowerDebarred($borrowernumber); - return (1, $blockedcount) if $blockedcount > 0; + return ( 1, $blockeddate ) if $blockeddate; # if he have late issues - $sth = $dbh->prepare( + my $sth = $dbh->prepare( "SELECT COUNT(*) as latedocs FROM issues WHERE borrowernumber = ? @@ -700,9 +673,9 @@ sub IsMemberBlocked { $sth->execute($borrowernumber); my $latedocs = $sth->fetchrow_hashref->{'latedocs'}; - return (-1, $latedocs) if $latedocs > 0; + return ( -1, $latedocs ) if $latedocs > 0; - return (0, 0); + return ( 0, 0 ); } =head2 GetMemberIssuesAndFines @@ -2112,7 +2085,7 @@ sub GetBorrowersNamesAndLatestIssue { =head2 DebarMember - my $success = DebarMember( $borrowernumber ); +my $success = DebarMember( $borrowernumber, $todate ); marks a Member as debarred, and therefore unable to checkout any more items. @@ -2124,13 +2097,16 @@ true on success, false on failure sub DebarMember { my $borrowernumber = shift; + my $todate = shift; return unless defined $borrowernumber; return unless $borrowernumber =~ /^\d+$/; - return ModMember( borrowernumber => $borrowernumber, - debarred => 1 ); - + return ModMember( + borrowernumber => $borrowernumber, + debarred => $todate + ); + } =head2 ModPrivacy diff --git a/C4/Overdues.pm b/C4/Overdues.pm index 28b135c..cdc2d50 100644 --- a/C4/Overdues.pm +++ b/C4/Overdues.pm @@ -1048,16 +1048,17 @@ sub CheckBorrowerDebarred { SELECT debarred FROM borrowers WHERE borrowernumber=? + AND debarred > NOW() |; my $sth = $dbh->prepare($query); $sth->execute($borrowernumber); - my ($debarredstatus) = $sth->fetchrow; - return ( $debarredstatus eq '1' ? 1 : 0 ); + my $debarredstatus = $sth->fetchrow; + return $debarredstatus; } =head2 UpdateBorrowerDebarred - ($borrowerstatut) = &UpdateBorrowerDebarred($borrowernumber); +($borrowerstatut) = &UpdateBorrowerDebarred($borrowernumber, $todate); update status of borrowers in borrowers table (field debarred) @@ -1066,16 +1067,16 @@ C<$borrowernumber> borrower number =cut sub UpdateBorrowerDebarred{ - my($borrowernumber) = @_; - my $dbh = C4::Context->dbh; - my $query=qq|UPDATE borrowers - SET debarred='1' + my ( $borrowernumber, $todate ) = @_; + my $dbh = C4::Context->dbh; + my $query = qq|UPDATE borrowers + SET debarred=? WHERE borrowernumber=? |; - my $sth=$dbh->prepare($query); - $sth->execute($borrowernumber); - $sth->finish; - return 1; + my $sth = $dbh->prepare($query); + $sth->execute( $todate, $borrowernumber ); + $sth->finish; + return 1; } =head2 CheckExistantNotifyid diff --git a/circ/circulation.pl b/circ/circulation.pl index da4878f..7e6b42c 100755 --- a/circ/circulation.pl +++ b/circ/circulation.pl @@ -30,6 +30,7 @@ use C4::Dates qw/format_date/; use C4::Branch; # GetBranches use C4::Koha; # GetPrinter use C4::Circulation; +use C4::Overdues qw/CheckBorrowerDebarred/; use C4::Members; use C4::Biblio; use C4::Reserves; @@ -259,6 +260,16 @@ if ($borrowernumber) { issuecount => $issue, finetotal => $fines ); + + my $debar = CheckBorrowerDebarred($borrowernumber); + if ($debar) { + $template->param( 'userdebarred' => 1 ); + $template->param( 'debarredcomment' => $borrower->{debarredcomment} ); + if ( $debar ne "9999-12-31" ) { + $template->param( 'userdebarreddate' => C4::Dates::format_date($debar) ); + } + } + } # diff --git a/circ/returns.pl b/circ/returns.pl index 6826d7c..e7c8d10 100755 --- a/circ/returns.pl +++ b/circ/returns.pl @@ -456,7 +456,12 @@ foreach my $code ( keys %$messages ) { } elsif ( $code eq 'Wrongbranch' ) { } - + elsif ( $code eq 'Debarred' ) { + $err{debarred} = format_date( $messages->{'Debarred'} ); + $err{debarcardnumber} = $borrower->{cardnumber}; + $err{debarborrowernumber} = $borrower->{borrowernumber}; + $err{debarname} = "$borrower->{firstname} $borrower->{surname}"; + } else { die "Unknown error code $code"; # note we need all the (empty) elsif's above, or we die. # This forces the issue of staying in sync w/ Circulation.pm diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 13fa53d..ab72771 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -232,7 +232,8 @@ CREATE TABLE `borrowers` ( `dateexpiry` date default NULL, `gonenoaddress` tinyint(1) default NULL, `lost` tinyint(1) default NULL, - `debarred` tinyint(1) default NULL, + `debarred` date default NULL, + `debarredcomment` VARCHAR(255) DEFAULT NULL, `contactname` mediumtext, `contactfirstname` text, `contacttitle` text, @@ -686,7 +687,8 @@ CREATE TABLE `deletedborrowers` ( `dateexpiry` date default NULL, `gonenoaddress` tinyint(1) default NULL, `lost` tinyint(1) default NULL, - `debarred` tinyint(1) default NULL, + `debarred` date default NULL, + `debarredcomment` VARCHAR(255) DEFAULT NULL, `contactname` mediumtext, `contactfirstname` text, `contacttitle` text, diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index bdfc9ac..a78e718 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -4439,6 +4439,18 @@ if (C4::Context->preference("Version") < TransformToNum($DBversion)) { SetVersion($DBversion); } +$DBversion = "3.05.00.XXX"; +if ( C4::Context->preference("Version") < TransformToNum($DBversion) ) { + my $borrowers = $dbh->selectcol_arrayref( "SELECT borrowernumber from borrowers where debarred <>0;", { Columns => [1] } ); + $dbh->do("ALTER TABLE borrowers MODIFY debarred DATE DEFAULT NULL;"); + $dbh->do( "UPDATE borrowers set debarred='9999-12-31' where borrowernumber IN (" . join( ",", @$borrowers ) . ");" ) if ($borrowers and scalar(@$borrowers)>0); + $dbh->do("ALTER TABLE borrowers ADD COLUMN debarredcomment VARCHAR(255) DEFAULT NULL AFTER debarred;"); + $dbh->do("ALTER TABLE deletedborrowers MODIFY debarred DATE DEFAULT NULL;"); + $dbh->do("ALTER TABLE deletedborrowers ADD COLUMN debarredcomment VARCHAR(255) DEFAULT NULL AFTER debarred;"); + print "Upgrade done (Change borrowers.debarred into Date )\n"; + + SetVersion($DBversion); +} =head1 FUNCTIONS diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt index dd77fcc..eb8ab26 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt @@ -562,6 +562,17 @@ No patron matched [% message %]
  • Lost: Patron's card is lost
  • [% END %] + [% IF ( userdebarred ) %] +
  • + Restricted: Patron's account is restricted [% IF (userdebarreddate ) %] until [% userdebarreddate %] [% END %] [% IF (debarredcomment ) %]([% debarredcomment %])[% END %] +
    + + + + +
    +
  • [% END %] + [% IF ( dbarred ) %]
  • Restricted: Patron's account is restricted Lift restriction
  • [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt index 4224ab1..58e12f4 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/returns.tt @@ -304,6 +304,9 @@ function Dopop(link) { [% IF ( errmsgloo.withdrawn ) %]

    Item is withdrawn.

    [% END %] + [% IF ( errmsgloo.debarred ) %] +

    [% errmsgloo.debarname %]([% errmsgloo.debarcardnumber %]) is now debarred until [% errmsgloo.debarred %]

    + [% END %] [% END %] [% IF ( soundon ) %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt index 8a34bb1..729324d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/memberentrygen.tt @@ -956,10 +956,43 @@ [% END %] - + [% END %] +
  • + + [% IF ( debarred ) %] + + + + + [% ELSE %] + + + + + [% END %] + +
    + + + Show Calendar + +
    + + +
  • + + [% END %] [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt index cfa75ca..8d9d1e0 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt @@ -158,12 +158,11 @@ function validate1(date) { [% IF ( flagged ) %]