+# FIXME: We need three pieces of data to operate:
+# ~ lower bound (number of days),
+# ~ upper bound (number of days),
+# ~ new lost value.
+# Right now we get only two, causing the endrange hack. This is a design-level failure.
+# FIXME: do checks on --lost ranges to make sure they are exclusive.
+# FIXME: do checks on --lost ranges to make sure the authorized values exist.
+# FIXME: do checks on --lost ranges to make sure don't go past endrange.
+# FIXME: convert to using pod2usage
+# FIXME: allow --help or -h
+#
+if ( ! defined($lost) ) {
+ print $usage;
+ die "ERROR: No --lost (-l) option defined";
+}
+unless ($confirm) {
+ $verbose = 1; # If you're not running it for real, then the whole point is the print output.
+ print "### TEST MODE -- NO ACTIONS TAKEN ###\n";
+}
+
+# In my opinion, this line is safe SQL to have outside the API. --atz
+our $bounds_sth = C4::Context->dbh->prepare("SELECT DATE_SUB(CURDATE(), INTERVAL ? DAY)");
+
+sub bounds ($) {
+ $bounds_sth->execute(shift);
+ return $bounds_sth->fetchrow;
+}
+
+# FIXME - This sql should be inside the API.
+sub longoverdue_sth {
+ my $query = "
+ SELECT items.itemnumber, borrowernumber, date_due
+ FROM issues, items
+ WHERE items.itemnumber = issues.itemnumber
+ AND DATE_SUB(CURDATE(), INTERVAL ? DAY) > date_due
+ AND DATE_SUB(CURDATE(), INTERVAL ? DAY) <= date_due
+ AND itemlost <> ?
+ ORDER BY date_due
+ ";
+ return C4::Context->dbh->prepare($query);
+}
+
+#FIXME - Should add a 'system' user and get suitable userenv for it for logging, etc.
+
+my $count;
+# my @ranges = map {
+my @report;
+my $total = 0;
+my $i = 0;
+
+# FIXME - The item is only marked returned if you supply --charge .
+# We need a better way to handle this.
+#
+my $sth_items = longoverdue_sth();
+
+foreach my $startrange (sort keys %$lost) {
+ if( my $lostvalue = $lost->{$startrange} ) {
+ my ($date1) = bounds($startrange);
+ my ($date2) = bounds( $endrange);
+ # print "\nRange ", ++$i, "\nDue $startrange - $endrange days ago ($date2 to $date1), lost => $lostvalue\n" if($verbose);
+ $verbose and
+ printf "\nRange %s\nDue %3s - %3s days ago (%s to %s), lost => %s\n", ++$i,
+ $startrange, $endrange, $date2, $date1, $lostvalue;
+ $sth_items->execute($startrange, $endrange, $lostvalue);
+ $count=0;
+ while (my $row=$sth_items->fetchrow_hashref) {
+ printf ("Due %s: item %5s from borrower %5s to lost: %s\n", $row->{date_due}, $row->{itemnumber}, $row->{borrowernumber}, $lostvalue) if($verbose);
+ if($confirm) {
+ ModItem({ itemlost => $lostvalue }, $row->{'biblionumber'}, $row->{'itemnumber'});
+ chargelostitem($row->{'itemnumber'}) if( $charge && $charge eq $lostvalue);
+ }
+ $count++;
+ }
+ push @report, {
+ startrange => $startrange,
+ endrange => $endrange,
+ range => "$startrange - $endrange",
+ date1 => $date1,
+ date2 => $date2,
+ lostvalue => $lostvalue,
+ count => $count,
+ };
+ $total += $count;
+ }
+ $endrange = $startrange;
+}