From 3ab856ff35cbca27d0b1af36e2290f07057642d2 Mon Sep 17 00:00:00 2001 From: Andrew Moore Date: Wed, 20 Aug 2008 11:21:46 -0500 Subject: [PATCH] bug 2503: adding dates to some C4:Circulation methods to allow offline circulation tool to specify dates other than today When uploading an offline circulation file, some actions may need to take place in the past. This patch adds date parameters to some C4::Circulation methods to allow us to set dates on them added issuedate to C4::Circulation::AddIssue adding issuedate to C4::Circulation::AddRenewal adding returdate to C4::Circulation::MarkIssueReturned Signed-off-by: Galen Charlton --- C4/Circulation.pm | 102 +++++++++++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 43 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index de49d4767e..da444a5371 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -822,6 +822,8 @@ Issue a book. Does no check, they are done in CanBookBeIssued. If we reach this =item C<$cancelreserve> +=item C<$issuedate> the date to issue the item in iso format (YYYY-MM-DD). Defaults to today. + AddIssue does the following things : - step 01: check that there is a borrowernumber & a barcode provided - check for RENEWAL (book issued & being issued to the same patron) @@ -840,13 +842,17 @@ AddIssue does the following things : =cut sub AddIssue { - my ( $borrower, $barcode, $date, $cancelreserve ) = @_; + my ( $borrower, $barcode, $datedue, $cancelreserve, $issuedate ) = @_; my $dbh = C4::Context->dbh; my $barcodecheck=CheckValidBarcode($barcode); + + # $issuedate defaults to today. + if ( ! defined $issuedate ) { + $issuedate = strftime( "%Y-%m-%d", localtime ); + } if ($borrower and $barcode and $barcodecheck ne '0'){ # find which item we issue my $item = GetItem('', $barcode) or return undef; # if we don't get an Item, abort. - my $datedue; my $branch; # Get which branchcode we need if (C4::Context->preference('CircControl') eq 'PickupLibrary'){ @@ -874,7 +880,8 @@ sub AddIssue { $borrower->{'borrowernumber'}, $item->{'itemnumber'}, $branch, - $date + $datedue, + $issuedate, ); } @@ -951,27 +958,26 @@ sub AddIssue { (borrowernumber, itemnumber,issuedate, date_due, branchcode) VALUES (?,?,?,?,?)" ); - my $dateduef; - if ($date) { - $dateduef = $date; + my $dateduef; + if ($datedue) { + $dateduef = $datedue; } else { - my $itype=(C4::Context->preference('item-level_itypes')) ? $biblio->{'itype'} : $biblio->{'itemtype'} ; - my $loanlength = GetLoanLength( - $borrower->{'categorycode'}, - $itype, - $branch - ); - $dateduef = CalcDateDue(C4::Dates->new(),$loanlength,$branch); - # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate - if ( C4::Context->preference('ReturnBeforeExpiry') && $dateduef->output('iso') gt $borrower->{dateexpiry} ) { - $dateduef = C4::Dates->new($borrower->{dateexpiry},'iso'); - } - }; - $sth->execute( - $borrower->{'borrowernumber'}, - $item->{'itemnumber'}, - strftime( "%Y-%m-%d", localtime ),$dateduef->output('iso'), C4::Context->userenv->{'branch'} - ); + my $itype = ( C4::Context->preference('item-level_itypes') ) ? $biblio->{'itype'} : $biblio->{'itemtype'}; + my $loanlength = GetLoanLength( $borrower->{'categorycode'}, $itype, $branch ); + $dateduef = CalcDateDue( C4::Dates->new( $issuedate, 'iso' ), $loanlength, $branch ); + + # if ReturnBeforeExpiry ON the datedue can't be after borrower expirydate + if ( C4::Context->preference('ReturnBeforeExpiry') && $dateduef->output('iso') gt $borrower->{dateexpiry} ) { + $dateduef = C4::Dates->new( $borrower->{dateexpiry}, 'iso' ); + } + } + $sth->execute( + $borrower->{'borrowernumber'}, # borrowernumber + $item->{'itemnumber'}, # itemnumber + $issuedate, # issuedate + $dateduef->output('iso'), # date_due + C4::Context->userenv->{'branch'} # branchcode + ); $sth->finish; $item->{'issues'}++; ModItem({ issues => $item->{'issues'}, @@ -1445,7 +1451,7 @@ sub AddReturn { =over 4 -MarkIssueReturned($borrowernumber, $itemnumber, $dropbox_branch); +MarkIssueReturned($borrowernumber, $itemnumber, $dropbox_branch, $returndate); =back @@ -1453,9 +1459,12 @@ Unconditionally marks an issue as being returned by moving the C row to C and setting C to the current date, or the last non-holiday date of the branccode specified in -C . Assumes you've already checked that +C . Assumes you've already checked that it's safe to do this, i.e. last non-holiday > issuedate. +if C<$returndate> is specified (in iso format), it is used as the date +of the return. It is ignored when a dropbox_branch is passed in. + Ideally, this function would be internal to C, not exported, but it is currently needed by one routine in C. @@ -1463,19 +1472,23 @@ routine in C. =cut sub MarkIssueReturned { - my ($borrowernumber, $itemnumber, $dropbox_branch ) = @_; - my $dbh = C4::Context->dbh; - my $query = "UPDATE issues SET returndate="; - my @bind = ($borrowernumber,$itemnumber); - if($dropbox_branch) { - my $calendar = C4::Calendar->new( branchcode => $dropbox_branch ); - my $dropboxdate = $calendar->addDate(C4::Dates->new(), -1 ); - unshift @bind, $dropboxdate->output('iso') ; - $query .= " ? " - } else { - $query .= " now() "; - } - $query .= " WHERE borrowernumber = ? AND itemnumber = ?"; + my ( $borrowernumber, $itemnumber, $dropbox_branch, $returndate ) = @_; + my $dbh = C4::Context->dbh; + my $query = "UPDATE issues SET returndate="; + my @bind; + if ($dropbox_branch) { + my $calendar = C4::Calendar->new( branchcode => $dropbox_branch ); + my $dropboxdate = $calendar->addDate( C4::Dates->new(), -1 ); + $query .= " ? "; + push @bind, $dropboxdate->output('iso'); + } elsif ($returndate) { + $query .= " ? "; + push @bind, $returndate; + } else { + $query .= " now() "; + } + $query .= " WHERE borrowernumber = ? AND itemnumber = ?"; + push @bind, $borrowernumber, $itemnumber; # FIXME transaction my $sth_upd = $dbh->prepare($query); $sth_upd->execute(@bind); @@ -1898,7 +1911,7 @@ sub CanBookBeRenewed { =head2 AddRenewal -&AddRenewal($borrowernumber, $itemnumber, $branch, [$datedue]); +&AddRenewal($borrowernumber, $itemnumber, $branch, [$datedue], [$issuedate]); Renews a loan. @@ -1911,6 +1924,8 @@ C<$branch> is the library branch. Defaults to the homebranch of the ITEM. C<$datedue> can be a C4::Dates object used to set the due date. +C<$issuedate> can be a iso formatted date to use for the issuedate. + If C<$datedue> is the empty string, C<&AddRenewal> will calculate the due date automatically from the book's item type. @@ -1922,10 +1937,11 @@ sub AddRenewal { my $item = GetItem($itemnumber) or return undef; my $biblio = GetBiblioFromItemNumber($itemnumber) or return undef; my $branch = (@_) ? shift : $item->{homebranch}; # opac-renew doesn't send branch - my $datedue; + my $datedue = shift; + my $issuedate = shift; # If the due date wasn't specified, calculate it by adding the # book's loan length to today's date. - unless (@_ and $datedue = shift and $datedue->output('iso')) { + unless ($datedue && $datedue->output('iso')) { my $borrower = C4::Members::GetMemberDetails( $borrowernumber, 0 ) or return undef; my $loanlength = GetLoanLength( @@ -1952,11 +1968,11 @@ sub AddRenewal { # Update the issues record to have the new due date, and a new count # of how many times it has been renewed. my $renews = $issuedata->{'renewals'} + 1; - $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?, lastreneweddate = CURRENT_DATE + $sth = $dbh->prepare("UPDATE issues SET date_due = ?, renewals = ?, lastreneweddate = CURRENT_DATE, issuedate = ? WHERE borrowernumber=? AND itemnumber=?" ); - $sth->execute( $datedue->output('iso'), $renews, $borrowernumber, $itemnumber ); + $sth->execute( $datedue->output('iso'), $renews, $issuedate, $borrowernumber, $itemnumber ); $sth->finish; # Update the renewal count on the item, and tell zebra to reindex -- 2.39.5