From 0e69a3906ea55acf81b982bbf440bd1cf0c7e7d9 Mon Sep 17 00:00:00 2001 From: finlayt Date: Thu, 6 Jun 2002 02:47:53 +0000 Subject: [PATCH] improved all the code so that priorities are reset properly. --- C4/Reserves2.pm | 168 ++++++++++++++++++++++++++++++------------------ 1 file changed, 106 insertions(+), 62 deletions(-) diff --git a/C4/Reserves2.pm b/C4/Reserves2.pm index 25143de0c7..fb31de210b 100755 --- a/C4/Reserves2.pm +++ b/C4/Reserves2.pm @@ -15,7 +15,7 @@ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 0.01; @ISA = qw(Exporter); -@EXPORT = qw(&FindReserves &CheckReserves &CancelReserve &ReserveWaiting &CreateReserve &updatereserves &getreservetitle &Findgroupreserve); +@EXPORT = qw(&FindReserves &CheckReserves &CheckWaiting &CancelReserve &FillReserve &ReserveWaiting &CreateReserve &updatereserves &getreservetitle &Findgroupreserve); %EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ], # your exported package globals go here, @@ -54,8 +54,8 @@ my $priv_func = sub { sub FindReserves { my ($bib,$bor) = @_; my $dbh = C4Connect; - my $query = "Select *,reserves.branchcode,biblio.title as btitle - from reserves,borrowers,biblio "; + my $query = "SELECT *,reserves.branchcode,biblio.title AS btitle + FROM reserves,borrowers,biblio "; if ($bib ne ''){ $bib = $dbh->quote($bib); if ($bor ne ''){ @@ -100,17 +100,17 @@ sub CheckReserves { my $dbh=C4Connect; my $qitem=$dbh->quote($item); # get the biblionumber... - my $sth=$dbh->prepare("select biblionumber from items where itemnumber=$qitem"); + my $sth=$dbh->prepare("select biblionumber, biblioitemnumber from items where itemnumber=$qitem"); $sth->execute; - my ($biblio) = $sth->fetchrow_array; + my ($biblio, $bibitem) = $sth->fetchrow_array; $sth->finish; $dbh->disconnect; # get the reserves... - my ($count, $reserves) = FindReserves($biblio); + my ($count, @reserves) = Findgroupreserve($bibitem, $biblio); my $priority = 10000000; my $highest; if ($count) { - foreach my $res (@$reserves) { + foreach my $res (@reserves) { if ($res->{'itemnumber'} == $item) { return ("Waiting", $res); } else { @@ -130,6 +130,7 @@ sub CheckReserves { sub CancelReserve { my ($biblio, $item, $borr) = @_; my $dbh=C4Connect; + warn "In CancelReserve"; if (($item and $borr) and (not $biblio)) { # removing a waiting reserve record.... $item = $dbh->quote($item); @@ -146,14 +147,14 @@ sub CancelReserve { } if (($biblio and $borr) and (not $item)) { # removing a reserve record.... - $biblio = $dbh->quote($biblio); + my $q_biblio = $dbh->quote($biblio); $borr = $dbh->quote($borr); # fix up the priorities on the other records.... - my $query = "select priority from reserves - where biblionumber = $biblio - and borrowernumber = $borr - and cancellationdate is NULL - and (found <> 'F' or found is NULL)"; + my $query = "SELECT priority FROM reserves + WHERE biblionumber = $q_biblio + AND borrowernumber = $borr + AND cancellationdate is NULL + AND (found <> 'F' or found is NULL)"; my $sth=$dbh->prepare($query); $sth->execute; my ($priority) = $sth->fetchrow_array; @@ -162,7 +163,7 @@ sub CancelReserve { my $query = "update reserves set cancellationdate = now(), found = Null, priority = 0 - where biblionumber = $biblio + where biblionumber = $q_biblio and borrowernumber = $borr and cancellationdate is NULL and (found <> 'F' or found is NULL)"; @@ -170,25 +171,58 @@ sub CancelReserve { $sth->execute; $sth->finish; # now fix the priority on the others.... - my ($count, $reserves) = FindReserves($biblio); - foreach my $rec (@$reserves) { - if ($rec->{'priority'} > $priority) { - my $newpr = $rec->{'priority'}; - $newpr = $dbh->quote($newpr - 1); - my $query = "update reserves set priority = $newpr - where biblionumber = $rec->{'biblionumber'} - and borrowernumber = $rec->{'borrowernumber'} - and cancellationdate is NULL - and (found <> 'F' or found is NULL)"; - my $sth = $dbh->prepare($query); - $sth->execute; - $sth->finish; - } + fixpriority($priority, $biblio); + } + $dbh->disconnect; +} + + +sub FillReserve { + my ($res) = @_; + my $dbh=C4Connect; +# removing a waiting reserve record.... + my $biblio = $res->{'biblionumber'}; my $qbiblio = $dbh->quote($biblio); + my $borr = $res->{'borrowernumber'}; $borr = $dbh->quote($borr); + my $resdate = $res->{'reservedate'}; $resdate = $dbh->quote($resdate); +# update the database... + my $query = "UPDATE reserves SET found = 'F', + priority = 0 + WHERE biblionumber = $qbiblio + AND reservedate = $resdate + AND borrowernumber = $borr"; + my $sth = $dbh->prepare($query); + $sth->execute; + $sth->finish; + $dbh->disconnect; +# now fix the priority on the others.... + fixpriority($res->{'priority'}, $biblio); +} + +sub fixpriority { + my ($priority, $biblio) = @_; + my $dbh = C4Connect; + my ($count, $reserves) = FindReserves($biblio); + foreach my $rec (@$reserves) { + if ($rec->{'priority'} > $priority) { + my $newpr = $rec->{'priority'}; $newpr = $dbh->quote($newpr - 1); + my $nbib = $rec->{'biblionumber'}; $nbib = $dbh->quote($nbib); + my $nbor = $rec->{'borrowernumber'}; $nbor = $dbh->quote($nbor); + my $nresd = $rec->{'reservedate'}; $nresd = $dbh->quote($nresd); + my $query = "UPDATE reserves SET priority = $newpr + WHERE biblionumber = $nbib + AND borrowernumber = $nbor + AND reservedate = $nresd"; + warn $query; + my $sth = $dbh->prepare($query); + $sth->execute; + $sth->finish; } } $dbh->disconnect; } + + sub ReserveWaiting { my ($item, $borr) = @_; my $dbh = C4Connect; @@ -209,54 +243,64 @@ sub ReserveWaiting { my $data = $sth->fetchrow_hashref; $sth->finish; my $biblio = $data->{'biblionumber'}; - $biblio = $dbh->quote($biblio); + my $q_biblio = $dbh->quote($biblio); # update reserves record.... - $query = "update reserves set priority = 0, found = 'W', itemnumber = $item - where borrowernumber = $borr and biblionumber = $biblio"; + $query = "UPDATE reserves SET priority = 0, found = 'W', itemnumber = $item + WHERE borrowernumber = $borr AND biblionumber = $q_biblio"; $sth = $dbh->prepare($query); $sth->execute; $sth->finish; + $dbh->disconnect; # now fix up the remaining priorities.... - $query = "select priority, borrowernumber from reserves where biblionumber = $biblio - and cancellationdate is NULL - and found is NULL - and priority <> 0 and priority is not NULL"; - $sth = $dbh->prepare($query); - $sth->execute; + fixpriority($data->{'priority'}, $biblio); my $branchcode = $data->{'branchcode'}; - my $priority = $data->{'priority'}; - while (my $data = $sth->fetchrow_hashref) { - if ($data->{'priority'} > $priority) { - my $uquery = "update reserves set priority = priority - 1 - where biblionumber = $biblio - and borrowernumber = $borr - and cancellation is NULL"; - my $usth->$dbh->prepare($query); - $usth->execute; - $usth->finish; - } + return $branchcode; +} + +sub CheckWaiting { + my ($borr)=@_; + my $dbh = C4Connect; + $borr = $dbh->quote($borr); + my @itemswaiting; + my $query = "SELECT * FROM reserves + WHERE borrowernumber = $borr + AND reserves.found = 'W' + AND cancellationdate is NULL"; + my $sth = $dbh->prepare($query); + $sth->execute(); + my $cnt=0; + if (my $data=$sth->fetchrow_hashref) { + @itemswaiting[$cnt] =$data; + $cnt ++; } $sth->finish; - $dbh->disconnect; - return $branchcode; + return ($cnt,\@itemswaiting); } sub Findgroupreserve { my ($bibitem,$biblio)=@_; my $dbh=C4Connect; $bibitem=$dbh->quote($bibitem); - my $query="Select * from reserves - left join reserveconstraints on - reserves.biblionumber=reserveconstraints.biblionumber - where - reserves.biblionumber=$biblio and - ((reserveconstraints.biblioitemnumber=$bibitem - and reserves.borrowernumber=reserveconstraints.borrowernumber - and reserves.reservedate=reserveconstraints.reservedate) - or reserves.constrainttype='a') - and reserves.cancellationdate is NULL - and (reserves.found <> 'F' or reserves.found is NULL)"; -# print $query; + my $query = "SELECT reserves.biblionumber AS biblionumber, + reserves.borrowernumber AS borrowernumber, + reserves.reservedate AS reservedate, + reserves.branchcode AS branchcode, + reserves.cancellationdate AS cancellationdate, + reserves.found AS found, + reserves.reservenotes AS reservenotes, + reserves.priority AS priority, + reserves.timestamp AS timestamp, + reserveconstraints.biblioitemnumber AS biblioitemnumber, + reserves.itemnumber AS itemnumber + FROM reserves LEFT JOIN reserveconstraints + ON reserves.biblionumber = reserveconstraints.biblionumber + WHERE reserves.biblionumber = $biblio + AND ( ( reserveconstraints.biblioitemnumber = $bibitem + AND reserves.borrowernumber = reserveconstraints.borrowernumber + AND reserves.reservedate =reserveconstraints.reservedate ) + OR reserves.constrainttype='a' ) + AND reserves.cancellationdate is NULL + AND (reserves.found <> 'F' or reserves.found is NULL)"; my $sth=$dbh->prepare($query); $sth->execute; my $i=0; -- 2.39.5