From a8123e41baf1a36e609dca48060c154f8cbe53c7 Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatersolutions.com>
Date: Wed, 25 Jan 2017 16:57:10 +0000
Subject: [PATCH] Bug 17996 - longoverdue.pl should not set 'datereturned' when
 --mark-returned option is used

When longoverdue.pl returns items, it sets old_issues.returndate to the
current date. This means that when staff look at the borrowers'
circulation history, they see a returndate displayed, which is
confusing.

We don't lose information by not setting old_issues.returndate, because
items.itemlost_on will contain the date that the item was lost.

Test Plan:
1) Apply this patch
2) Mark an item lost using longoverdue.pl with --mark-returned
3) Note the item's old_issues line does not have a returndate
---
 C4/Circulation.pm            | 18 +++++++++++-------
 misc/cronjobs/longoverdue.pl |  4 ++--
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index 8105b0e9f9..168478f6fb 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -2164,15 +2164,17 @@ sub AddReturn {
 
 =head2 MarkIssueReturned
 
-  MarkIssueReturned($borrowernumber, $itemnumber, $returndate, $privacy);
+  MarkIssueReturned($borrowernumber, $itemnumber, $returndate, $privacy, $set_returndate);
 
 Unconditionally marks an issue as being returned by
-moving the C<issues> row to C<old_issues> and
-setting C<returndate> to the current date.
+moving the C<issues> row to C<old_issues>
 
 if C<$returndate> is specified (in iso format), it is used as the date
 of the return.
 
+if C<$returndate> is not specified and C<$set_returndate> is not 0 then
+the return date will be set to current_date
+
 C<$privacy> contains the privacy parameter. If the patron has set privacy to 2,
 the old_issue is immediately anonymised
 
@@ -2183,7 +2185,9 @@ and offline_circ/process_koc.pl.
 =cut
 
 sub MarkIssueReturned {
-    my ( $borrowernumber, $itemnumber, $returndate, $privacy ) = @_;
+    my ( $borrowernumber, $itemnumber, $returndate, $privacy, $set_returndate ) = @_;
+
+    $set_returndate //= 1;
 
     # Retrieve the issue
     my $issue = Koha::Checkouts->find( { itemnumber => $itemnumber } ) or return;
@@ -2212,7 +2216,7 @@ sub MarkIssueReturned {
         if ( $returndate ) {
             $issue->returndate( $returndate )->store->discard_changes; # update and refetch
         }
-        else {
+        elsif( $set_returndate) {
             $issue->returndate( \'NOW()' )->store->discard_changes; # update and refetch
         }
 
@@ -3767,7 +3771,7 @@ sub ReturnLostItem{
 
 
 sub LostItem{
-    my ($itemnumber, $mark_lost_from, $force_mark_returned) = @_;
+    my ( $itemnumber, $mark_lost_from, $force_mark_returned, $set_returndate ) = @_;
 
     unless ( $mark_lost_from ) {
         # Temporary check to avoid regressions
@@ -3813,7 +3817,7 @@ sub LostItem{
             #warn " $issues->{'borrowernumber'}  /  $itemnumber ";
         }
 
-        MarkIssueReturned($borrowernumber,$itemnumber,undef,$patron->privacy) if $mark_returned;
+        MarkIssueReturned($borrowernumber,$itemnumber,undef,$patron->privacy,$set_returndate) if $mark_returned;
     }
 
     #When item is marked lost automatically cancel its outstanding transfers and set items holdingbranch to the transfer source branch (frombranch)
diff --git a/misc/cronjobs/longoverdue.pl b/misc/cronjobs/longoverdue.pl
index c75ffd29ca..37b9dd42e1 100755
--- a/misc/cronjobs/longoverdue.pl
+++ b/misc/cronjobs/longoverdue.pl
@@ -385,10 +385,10 @@ foreach my $startrange (sort keys %$lost) {
             if($confirm) {
                 ModItem({ itemlost => $lostvalue }, $row->{'biblionumber'}, $row->{'itemnumber'});
                 if ( $charge && $charge eq $lostvalue ) {
-                    LostItem( $row->{'itemnumber'}, 'cronjob', $mark_returned );
+                    LostItem( $row->{'itemnumber'}, 'cronjob', $mark_returned, 0 );
                 } elsif ( $mark_returned ) {
                     my $patron = Koha::Patrons->find( $row->{borrowernumber} );
-                    MarkIssueReturned($row->{borrowernumber},$row->{itemnumber},undef,$patron->privacy)
+                    MarkIssueReturned($row->{borrowernumber},$row->{itemnumber},undef,$patron->privacy,0)
                 }
             }
             $count++;
-- 
2.11.0