From 891f1454a92b079cae53c1dc52f7e916b2bee583 Mon Sep 17 00:00:00 2001
From: Aleisha Amohia <aleishaamohia@hotmail.com>
Date: Sun, 19 Nov 2017 22:04:32 +0000
Subject: [PATCH] Bug 19532: Recalls awaiting pickup report

this patch adds reports for all recalls awaiting pickup, and recalls
that have been waiting more than X days. a syspref,
RecallsMaxPickUpDelay, is added to give a general max shelf time for
recalls

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
---
 circ/recalls_waiting.pl                            |  71 +++++++++
 ...g_19532_-_add_RecallsMaxPickUpDelay_syspref.sql |   1 +
 installer/data/mysql/sysprefs.sql                  |   1 +
 .../en/modules/admin/preferences/circulation.pref  |   5 +
 .../prog/en/modules/circ/recalls_waiting.tt        | 163 +++++++++++++++++++++
 5 files changed, 241 insertions(+)
 create mode 100755 circ/recalls_waiting.pl
 create mode 100644 installer/data/mysql/atomicupdate/bug_19532_-_add_RecallsMaxPickUpDelay_syspref.sql
 create mode 100644 koha-tmpl/intranet-tmpl/prog/en/modules/circ/recalls_waiting.tt

diff --git a/circ/recalls_waiting.pl b/circ/recalls_waiting.pl
new file mode 100755
index 0000000..1eafdd8
--- /dev/null
+++ b/circ/recalls_waiting.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/perl
+
+# Copyright 2017 Aleisha Amohia <aleisha@catalyst.net.nz>
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+use CGI qw ( -utf8 );
+use C4::Auth;
+use C4::Output;
+use Koha::Recalls;
+use Koha::BiblioFrameworks;
+use Koha::DateUtils;
+use Koha::Patrons;
+
+my $query = new CGI;
+my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
+    {
+        template_name   => "circ/recalls_waiting.tt",
+        query           => $query,
+        type            => "intranet",
+        authnotrequired => 0,
+        flagsrequired   => { circulate => "circulate_remaining_permissions" },
+        debug           => 1,
+    }
+);
+
+my $op = $query->param('op') || 'list';
+
+if ($op eq 'expire_recall') {
+    my $recall_id = $query->param('recall_id');
+    Koha::Recalls->find($recall_id)->update({ expirationdate => dt_from_string(), status => 'E' });
+    $op = 'list';
+}
+
+if ($op eq 'list') {
+    my @recalls = Koha::Recalls->search({ status => 'W' });
+    my $borrower = Koha::Patrons->find($loggedinuser);
+    my @over;
+    my $maxdelay = C4::Context->preference('RecallsMaxPickUpDelay') || 7;
+    my $today = dt_from_string();
+    foreach my $r (@recalls){
+        my $lastwaitingday = dt_from_string($r->waitingdate)->add( days => $maxdelay );
+        if ( $today > $lastwaitingday ){
+            push @over, $r;
+        }
+    }
+    $template->param(
+        recalls => \@recalls,
+        over => \@over,
+    );
+}
+
+# Checking if there is a Fast Cataloging Framework
+$template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
+
+# writing the template
+output_html_with_http_headers $query, $cookie, $template->output;
diff --git a/installer/data/mysql/atomicupdate/bug_19532_-_add_RecallsMaxPickUpDelay_syspref.sql b/installer/data/mysql/atomicupdate/bug_19532_-_add_RecallsMaxPickUpDelay_syspref.sql
new file mode 100644
index 0000000..0cd21dd
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_19532_-_add_RecallsMaxPickUpDelay_syspref.sql
@@ -0,0 +1 @@
+INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES ('RecallsMaxPickUpDelay','7','','Define the maximum time a recall can be waiting for pickup','Integer');
diff --git a/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql
index 0f86627..5ab4c0e 100644
--- a/installer/data/mysql/sysprefs.sql
+++ b/installer/data/mysql/sysprefs.sql
@@ -458,6 +458,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
 ('QueryWeightFields','1',NULL,'If ON, enables field weighting','YesNo'),
 ('QuoteOfTheDay','0',NULL,'Enable or disable display of Quote of the Day on the OPAC home page','YesNo'),
 ('RandomizeHoldsQueueWeight','0',NULL,'if ON, the holds queue in circulation will be randomized, either based on all location codes, or by the location codes specified in StaticHoldsQueueWeight','YesNo'),
+('RecallsMaxPickUpDelay','7','','Define the maximum time a recall can be waiting for pickup','Integer'),
 ('RecordLocalUseOnReturn','0',NULL,'If ON, statistically record returns of unissued items as local use, instead of return','YesNo'),
 ('RefundLostOnReturnControl','CheckinLibrary','CheckinLibrary|ItemHomeBranch|ItemHoldingBranch','If a lost item is returned, choose which branch to pick rules for refunding.','Choice'),
 ('RenewalLog','0','','If ON, log information about renewals','YesNo'),
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref
index 2407b6c..8794461 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref
@@ -160,6 +160,11 @@ Circulation:
 
     Checkout Policy:
         -
+            - Mark a recall as problematic if it has been waiting to be picked up for
+            - pref: RecallsMaxPickUpDelay
+            - class: integer
+            - days.
+        -
             - pref: AllowTooManyOverride
               choices:
                   yes: Allow
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/recalls_waiting.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/recalls_waiting.tt
new file mode 100644
index 0000000..effb431
--- /dev/null
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/recalls_waiting.tt
@@ -0,0 +1,163 @@
+[% USE Koha %]
+[% USE KohaDates %]
+[% INCLUDE 'doc-head-open.inc' %]
+<title>Koha &rsaquo; Circulation &rsaquo; Recalls awaiting pickup</title>
+[% INCLUDE 'doc-head-close.inc' %]
+<style type="text/css"> p { margin-top: 0; }</style>
+<link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/datatables.css" />
+<script type="text/javascript" src="[% interface %]/[% theme %]/js/recalls.js"></script>
+[% INCLUDE 'datatables.inc' %]
+<script type="text/javascript">
+    $(document).ready(function() {
+        $('#results').tabs();
+
+        $("#recallswaiting-table, #recallsover-table").dataTable($.extend(true, {}, dataTablesDefaults, {
+            "autoWidth": false,
+            "aoColumnDefs": [
+                { "aTargets": [ 'nosort' ], "bSortable": false, "bSearchable": false },
+            ],
+            "sPaginationType": "four_button"
+        }));
+    });
+</script>
+</head>
+<body id="circ_recalls_awaiting_pickup" class="circ">
+[% INCLUDE 'header.inc' %]
+[% INCLUDE 'cat-search.inc' %]
+
+<div id="breadcrumbs">
+    <a href="/cgi-bin/koha/mainpage.pl">Home</a>
+    &rsaquo; <a href="/cgi-bin/koha/circ/circulation-home.pl">Circulation</a>
+    &rsaquo; <a href="/cgi-bin/koha/circ/recalls_waiting.pl">Recalls awaiting pickup</a>
+</div>
+
+[% IF Koha.Preference('CircSidebar') %]<div id="doc3" class="yui-t2">[% ELSE %]<div id="doc2" class="yui-t7">[% END %]
+
+<div id="bd">
+    <div id="yui-main">
+        [% IF Koha.Preference('CircSidebar') %]<div class="yui-b">[% END %]
+            <div class="yui-g">
+
+                <h1>Recalls awaiting pickup</h1>
+
+                [% IF recalls.size > 0 %]
+
+                <div id="results" class="toptabs">
+
+                    <ul>
+                        <li><a href="#recallswaiting">Recalls waiting: [% recalls.count %]</a></li>
+                        <li><a href="#recallsover">Recalls waiting over [% Koha.Preference('RecallsMaxPickUpDelay') %] days: [% over.count %]</a></li>
+                    </ul>
+
+                    <div id="recallswaiting">
+                        [% IF ( recalls.size > 0 ) %]
+                            <table id="recallswaiting-table">
+                                <thead><tr>
+                                    <th class="title-string">Available since</th>
+                                    <th class="anti-the">Title</th>
+                                    <th>Requested by</th>
+                                    <th>Pickup location</th>
+                                    <th class="nosort">Expire</th>
+                                </tr></thead>
+                                <tbody>
+                                    [% FOREACH recall IN recalls %]<tr>
+                                        <td>[% recall.waitingdate | $KohaDates %]</td>
+                                        <td>
+                                            <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% recall.biblionumber %]">
+                                                [% recall.biblio.title %]
+                                                [% FOREACH s IN recall.biblio.subtitles %]
+                                                    [% s %]
+                                                [% END %]
+                                                [% recall.item.enumchron %]
+                                            </a>
+                                            [% recall.biblio.author %]
+                                            <br><i>Barcode: [% recall.item.barcode %]</i>
+                                        </td>
+                                        <td>
+                                            <a href="/cgi-bin/koha/members/moremember.pl?borrowernumber=[% recall.borrowernumber %]">[% recall.borrower.firstname %] [% recall.borrower.surname %]</a>
+                                            [% IF ( recall.borrower.phone ) %]<br />[% recall.borrower.phone %][% END %]
+                                            [% IF ( recall.borrower.email ) %]<br /><a href="mailto:[% recall.borrower.email %]?subject=Recall waiting: [% recall.biblio.title %]">[% recall.borrower.email %]</a>[% END %]
+                                        </td>
+                                        <td>[% recall.branch.branchname %]</td>
+                                        <td>
+                                            <form action="/cgi-bin/koha/circ/recalls_waiting.pl" method="post">
+                                                <input type="hidden" name="recall_id" value="[% recall.recall_id %]">
+                                                <input type="hidden" name="op" value="expire_recall">
+                                                <fieldset class="action">
+                                                    <button type="submit" class="btn btn-default btn-xs" id="expire_recall"><i class="fa fa-times"></i> Expire</button>
+                                                </fieldset>
+                                            </form>
+                                        </td>
+                                    </tr>[% END %]
+                                </tbody>
+                            </table>
+                        [% ELSE %]
+                            <div class="dialog message">No recalls waiting.</div>
+                        [% END %]
+                    </div>
+
+                    <div id="recallsover">
+                        [% IF ( over.size > 0 ) %]
+                            [% IF ( Koha.Preference('RecallsMaxPickUpDelay') ) %]<p>Recalls listed here have been awaiting pickup for more than [% Koha.Preference('RecallsMaxPickUpDelay') %] days.</p>[% END %]
+                            <table id="recallsover-table">
+                                <thead><tr>
+                                    <th class="title-string">Available since</th>
+                                    <th class="anti-the">Title</th>
+                                    <th>Requested by</th>
+                                    <th>Pickup location</th>
+                                    <th class="nosort">Expire</th>
+                                </tr></thead>
+                                <tbody>
+                                    [% FOREACH recall IN over %]<tr>
+                                        <td>[% recall.waitingdate | $KohaDates %]</td>
+                                        <td>
+                                            <a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% recall.biblionumber %]">
+                                                [% recall.biblio.title %]
+                                                [% FOREACH s IN recall.biblio.subtitles %]
+                                                    [% s %]
+                                                [% END %]
+                                                [% recall.item.enumchron %]
+                                            </a>
+                                            [% recall.biblio.author %]
+                                            <br><i>Barcode: [% recall.item.barcode %]</i>
+                                        </td>
+                                        <td>
+                                            <a href="/cgi-bin/koha/members/moremember.pl?borrowernumber=[% recall.borrowernumber %]">[% recall.borrower.firstname %] [% recall.borrower.surname %]</a>
+                                            [% IF ( recall.borrower.phone ) %]<br />[% recall.borrower.phone %][% END %]
+                                            [% IF ( recall.borrower.email ) %]<br /><a href="mailto:[% recall.borrower.email %]?subject=Recall waiting: [% recall.biblio.title %]">[% recall.borrower.email %]</a>[% END %]
+                                        </td>
+                                        <td>[% recall.branch.branchname %]</td>
+                                        <td>
+                                            <form action="/cgi-bin/koha/circ/recalls_waiting.pl" method="post">
+                                                <input type="hidden" name="recall_id" value="[% recall.recall_id %]">
+                                                <input type="hidden" name="op" value="expire_recall">
+                                                <fieldset class="action">
+                                                    <button type="submit" class="btn btn-default btn-xs" id="expire_recall"><i class="fa fa-times"></i> Expire</button>
+                                                </fieldset>
+                                            </form>
+                                        </td>
+                                    </tr>[% END %]
+                                </tbody>
+                            </table>
+                        [% ELSE %]
+                            <div class="dialog message">No recalls waiting.</div>
+                        [% END %]
+                    </div>
+
+                </div>
+
+                [% END %]
+
+            </div>
+        </div>
+
+    </div>
+
+    [% IF Koha.Preference('CircSidebar') %]
+        <div class="yui-b noprint">
+            [% INCLUDE 'circ-nav.inc' %]
+        </div>
+    [% END %]
+</div>
+
+[% INCLUDE 'intranet-bottom.inc' %]
-- 
2.1.4