+=item CreateReserve
+
+CreateReserve($env,$branch,$borrowernumber,$biblionumber,$constraint,$bibitems,$priority,$notes,$title,$checkitem,$found)
+
+FIXME - A somewhat different version of this function appears in
+C4::Reserves. Pick one and stick with it.
+
+=cut
+
+sub CreateReserve {
+ my (
+ $env, $branch, $borrowernumber, $biblionumber,
+ $constraint, $bibitems, $priority, $notes,
+ $title, $checkitem, $found
+ ) = @_;
+ my $fee;
+ if ( $library_name =~ /Horowhenua/ ) {
+ $fee =
+ CalcHLTReserveFee( $env, $borrowernumber, $biblionumber, $constraint,
+ $bibitems );
+ }
+ else {
+ $fee =
+ CalcReserveFee( $env, $borrowernumber, $biblionumber, $constraint,
+ $bibitems );
+ }
+ my $dbh = C4::Context->dbh;
+ my $const = lc substr( $constraint, 0, 1 );
+ my @datearr = localtime(time);
+ my $resdate =
+ ( 1900 + $datearr[5] ) . "-" . ( $datearr[4] + 1 ) . "-" . $datearr[3];
+ my $waitingdate;
+
+ # If the reserv had the waiting status, we had the value of the resdate
+ if ( $found eq 'W' ) {
+ $waitingdate = $resdate;
+ }
+
+ #eval {
+ # updates take place here
+ if ( $fee > 0 ) {
+ my $nextacctno = &getnextacctno( $env, $borrowernumber, $dbh );
+ my $query = qq/
+ INSERT INTO accountlines
+ (borrowernumber,accountno,date,amount,description,accounttype,amountoutstanding)
+ VALUES
+ (?,?,now(),?,?,'Res',?)
+ /;
+ my $usth = $dbh->prepare($query);
+ $usth->execute( $borrowernumber, $nextacctno, $fee,
+ "Reserve Charge - $title", $fee );
+ $usth->finish;
+ }
+
+ #if ($const eq 'a'){
+ my $query = qq/
+ INSERT INTO reserves
+ (borrowernumber,biblionumber,reservedate,branchcode,constrainttype,
+ priority,reservenotes,itemnumber,found,waitingdate)
+ VALUES
+ (?,?,?,?,?,
+ ?,?,?,?,?)
+ /;
+ my $sth = $dbh->prepare($query);
+ $sth->execute(
+ $borrowernumber, $biblionumber, $resdate, $branch,
+ $const, $priority, $notes, $checkitem,
+ $found, $waitingdate
+ );
+ $sth->finish;
+
+ #}
+ if ( ( $const eq "o" ) || ( $const eq "e" ) ) {
+ my $numitems = @$bibitems;
+ my $i = 0;
+ while ( $i < $numitems ) {
+ my $biblioitem = @$bibitems[$i];
+ my $query = qq/
+ INSERT INTO reserveconstraints
+ (borrowernumber,biblionumber,reservedate,biblioitemnumber)
+ VALUES
+ (?,?,?,?)
+ /;
+ my $sth = $dbh->prepare("");
+ $sth->execute( $borrowernumber, $biblionumber, $resdate,
+ $biblioitem );
+ $sth->finish;
+ $i++;
+ }
+ }
+ return;
+}
+
+# FIXME - A functionally identical version of this function appears in
+# C4::Reserves. Pick one and stick with it.
+# XXX - Internal use only
+# FIXME - opac-reserves.pl need to use it, temporarily put into @EXPORT
+
+sub CalcReserveFee {
+ my ( $env, $borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
+
+ #check for issues;
+ my $dbh = C4::Context->dbh;
+ my $const = lc substr( $constraint, 0, 1 );
+ my $query = qq/
+ SELECT * FROM borrowers,categories
+ WHERE borrowernumber = ?
+ AND borrowers.categorycode = categories.categorycode
+ /;
+ my $sth = $dbh->prepare($query);
+ $sth->execute($borrowernumber);
+ my $data = $sth->fetchrow_hashref;
+ $sth->finish();
+ my $fee = $data->{'reservefee'};
+ my $cntitems = @- > $bibitems;
+
+ if ( $fee > 0 ) {
+
+ # check for items on issue
+ # first find biblioitem records
+ my @biblioitems;
+ my $sth1 = $dbh->prepare(
+ "SELECT * FROM biblio,biblioitems
+ WHERE (biblio.biblionumber = ?)
+ AND (biblio.biblionumber = biblioitems.biblionumber)"
+ );
+ $sth1->execute($biblionumber);
+ while ( my $data1 = $sth1->fetchrow_hashref ) {
+ if ( $const eq "a" ) {
+ push @biblioitems, $data1;
+ }
+ else {
+ my $found = 0;
+ my $x = 0;
+ while ( $x < $cntitems ) {
+ if ( @$bibitems->{'biblioitemnumber'} ==
+ $data->{'biblioitemnumber'} )
+ {
+ $found = 1;
+ }
+ $x++;
+ }
+ if ( $const eq 'o' ) {
+ if ( $found == 1 ) {
+ push @biblioitems, $data1;
+ }
+ }
+ else {
+ if ( $found == 0 ) {
+ push @biblioitems, $data1;
+ }
+ }
+ }
+ }
+ $sth1->finish;
+ my $cntitemsfound = @biblioitems;
+ my $issues = 0;
+ my $x = 0;
+ my $allissued = 1;
+ while ( $x < $cntitemsfound ) {
+ my $bitdata = $biblioitems[$x];
+ my $sth2 = $dbh->prepare(
+ "SELECT * FROM items
+ WHERE biblioitemnumber = ?"
+ );
+ $sth2->execute( $bitdata->{'biblioitemnumber'} );
+ while ( my $itdata = $sth2->fetchrow_hashref ) {
+ my $sth3 = $dbh->prepare(
+ "SELECT * FROM issues
+ WHERE itemnumber = ?
+ AND returndate IS NULL"
+ );
+ $sth3->execute( $itdata->{'itemnumber'} );
+ if ( my $isdata = $sth3->fetchrow_hashref ) {
+ }
+ else {
+ $allissued = 0;
+ }
+ }
+ $x++;
+ }
+ if ( $allissued == 0 ) {
+ my $rsth =
+ $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ?");
+ $rsth->execute($biblionumber);
+ if ( my $rdata = $rsth->fetchrow_hashref ) {
+ }
+ else {
+ $fee = 0;
+ }
+ }
+ }
+
+ # print "fee $fee";
+ return $fee;
+}
+
+# The following are junior and young adult item types that should not incur a
+# reserve charge.
+#
+# Juniors: BJC, BJCN, BJF, BJK, BJM, BJN, BJP, BJSF, BJSN, DJ, DJP, FJ, JVID,
+# VJ, VJP, PJ, TJ, TJP, VJ, VJP.
+#
+# Young adults: BYF, BYN, BYP, DY, DYP, PY, PYP, TY, TYP, VY, VYP.
+#
+# All other item types should incur a reserve charge.
+sub CalcHLTReserveFee {
+ my ( $env, $borrowernumber, $biblionumber, $constraint, $bibitems ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare(
+ "SELECT * FROM borrowers,categories
+ WHERE (borrowernumber = ?)
+ AND (borrowers.categorycode = categories.categorycode)"
+ );
+ $sth->execute($borrowernumber);
+ my $data = $sth->fetchrow_hashref;
+ $sth->finish();
+ my $fee = $data->{'reservefee'};
+
+ my $matchno;
+ my @nocharge =
+ qw/BJC BJCN BJF BJK BJM BJN BJP BJSF BJSN DJ DJP FJ NJ CJ VJ VJP PJ TJ TJP BYF BYN BYP DY DYP PY PYP TY TYP VY VYP/;
+ $sth = $dbh->prepare(
+ "SELECT * FROM biblio,biblioitems
+ WHERE (biblio.biblionumber = ?)
+ AND (biblio.biblionumber = biblioitems.biblionumber)"
+ );
+ $sth->execute($biblionumber);
+ $data = $sth->fetchrow_hashref;
+ my $itemtype = $data->{'itemtype'};
+ for ( my $i = 0 ; $i < @nocharge ; $i++ ) {
+ if ( $itemtype eq $nocharge[$i] ) {
+ $matchno++;
+ last;
+ }
+ }
+
+ if ( $matchno > 0 ) {
+ $fee = 0;
+ }
+ return $fee;
+}
+
+=item GetNextAccountNumber
+
+GetNextAccountNumber()
+
+=cut
+
+sub GetNextAccountNumber {
+ my ( $env, $borrowernumber, $dbh ) = @_;
+ my $nextaccntno = 1;
+ my $sth = $dbh->prepare(
+ "select * from accountlines
+ where (borrowernumber = ?)
+ order by accountno desc"
+ );
+ $sth->execute($borrowernumber);
+ if ( my $accdata = $sth->fetchrow_hashref ) {
+ $nextaccntno = $accdata->{'accountno'} + 1;
+ }
+ $sth->finish;
+ return ($nextaccntno);
+}
+
+#-------------------------------------------------------------------------------------
+
+=item UpdateReserve
+
+&UpdateReserve($rank,$biblio,$borrower,$branch)
+
+=cut
+
+sub UpdateReserve {
+ #subroutine to update a reserve
+ my ( $rank, $biblio, $borrower, $branch , $itemnumber) = @_;
+ return if $rank eq "W";
+ return if $rank eq "n";
+ my $dbh = C4::Context->dbh;
+ if ( $rank eq "del" ) {
+ my $query = qq/
+ UPDATE reserves
+ SET cancellationdate=now()
+ WHERE biblionumber = ?
+ AND borrowernumber = ?
+ AND cancellationdate is NULL
+ AND (found <> 'F' or found is NULL)
+ /;
+ my $sth = $dbh->prepare($query);
+ $sth->execute( $biblio, $borrower );
+ $sth->finish;
+
+ }
+ else {
+ my $query = qq/
+ UPDATE reserves SET priority = ? ,branchcode = ?, itemnumber = ?, found = NULL
+ WHERE biblionumber = ?
+ AND borrowernumber = ?
+ AND cancellationdate is NULL
+ AND (found <> 'F' or found is NULL)
+ /;
+ my $sth = $dbh->prepare($query);
+ $sth->execute( $rank, $branch,$itemnumber, $biblio, $borrower);
+ $sth->finish;
+ FixPriority( $biblio, $borrower, $rank);
+ }
+}
+
+=item GetReserveTitle
+
+$data = GetReserveTitle($biblio,$bor,$date,$timestamp);
+
+=cut
+
+sub GetReserveTitle {
+ my ( $biblio, $bor, $date, $timestamp ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $query = qq/
+ SELECT *
+ FROM reserveconstraints,biblioitems
+ WHERE reserveconstraints.biblioitemnumber=biblioitems.biblioitemnumber
+ AND reserveconstraints.biblionumber=?
+ AND reserveconstraints.borrowernumber = ?
+ AND reserveconstraints.reservedate=?
+ AND reserveconstraints.timestamp=?
+ /;
+ my $sth = $dbh->prepare($query);
+ $sth->execute( $biblio, $bor, $date, $timestamp );
+ my $data = $sth->fetchrow_hashref;
+ $sth->finish;
+ return $data;
+}
+
+=item FindReservesInQueue
+
+ $results = &FindReservesInQueue($biblionumber);
+
+Simple variant of FindReserves, exept the result is now displaying only the queue list of reservations with the same biblionumber (At this time only displayed in request.pl)
+
+C<&FindReservesInQueue> returns a two-element array:
+
+C<$results> is a reference to an array of references of hashes. Each hash
+has for keys a list of column from reserves table (see details in function).
+
+=cut
+
+#'
+
+sub FindReservesInQueue {
+ my ($biblionumber) = @_;
+ my $dbh = C4::Context->dbh;
+
+ # Find the desired items in the reserves
+ my $query = qq/
+ SELECT branchcode,
+ timestamp AS rtimestamp,
+ priority,
+ biblionumber,
+ borrowernumber,
+ reservedate,
+ constrainttype,
+ found,
+ itemnumber
+ FROM reserves
+ WHERE cancellationdate IS NULL
+ AND biblionumber = ?
+ AND (found <> \'F\' OR found IS NULL)
+ AND priority <> \'0\'
+ ORDER BY priority
+ /;
+ my $sth = $dbh->prepare($query);
+ $sth->execute($biblionumber);
+ my @results;
+ my $i = 0;
+ while ( my $data = $sth->fetchrow_hashref ) {
+
+ # FIXME - What is this if-statement doing? How do constraints work?
+ if ( $data->{constrainttype} eq 'o' ) {
+ $query = '
+ SELECT biblioitemnumber
+ FROM reserveconstraints
+ WHERE biblionumber = ?
+ AND borrowernumber = ?
+ AND reservedate = ?
+ ';
+ my $csth = $dbh->prepare($query);
+ $csth->execute( $data->{biblionumber}, $data->{borrowernumber},
+ $data->{reservedate}, );
+
+ my @bibitemno;
+ while ( my $bibitemnos = $csth->fetchrow_array ) {
+ push( @bibitemno, $bibitemnos );
+ }
+ my $count = @bibitemno;
+
+ # if we have two or more different specific itemtypes
+ # reserved by same person on same day
+ my $bdata;
+ if ( $count > 1 ) {
+ $bdata = GetBiblioItemData( $bibitemno[$i] );
+ $i++;
+ }
+ else {
+ # Look up the book we just found.
+ $bdata = GetBiblioItemData( $bibitemno[0] );
+ }
+ $csth->finish;
+
+ # Add the results of this latest search to the current
+ # results.
+ # FIXME - An 'each' would probably be more efficient.
+ foreach my $key ( keys %$bdata ) {
+ $data->{$key} = $bdata->{$key};
+ }
+ }
+ push @results, $data;
+ }
+ $sth->finish;
+
+ return ( $#results + 1, \@results );