From af17dbf654a6c3157835ce388b7b3ca6bbb7a581 Mon Sep 17 00:00:00 2001
From: Andrew Isherwood <andrew.isherwood@ptfs-europe.com>
Date: Thu, 13 Feb 2020 14:16:48 +0000
Subject: [PATCH] Bug 24608: Allow due date modification

This patch adds the ability to modify due dates from a patron's
checkouts list.
---
 Koha/Illbackends/BLDSS                             |  1 +
 Koha/Illbackends/FreeForm                          |  1 +
 .../intranet-tmpl/prog/en/includes/strings.inc     |  1 +
 .../prog/en/modules/circ/circulation.tt            |  4 ++
 koha-tmpl/intranet-tmpl/prog/js/checkouts.js       | 58 +++++++++++++++++++++-
 svc/checkouts                                      |  2 +
 6 files changed, 66 insertions(+), 1 deletion(-)
 create mode 120000 Koha/Illbackends/BLDSS
 create mode 120000 Koha/Illbackends/FreeForm

diff --git a/Koha/Illbackends/BLDSS b/Koha/Illbackends/BLDSS
new file mode 120000
index 0000000000..95e9e7ea7f
--- /dev/null
+++ b/Koha/Illbackends/BLDSS
@@ -0,0 +1 @@
+/home/koha-koha/illbackends/BLDSS
\ No newline at end of file
diff --git a/Koha/Illbackends/FreeForm b/Koha/Illbackends/FreeForm
new file mode 120000
index 0000000000..05911ec0ff
--- /dev/null
+++ b/Koha/Illbackends/FreeForm
@@ -0,0 +1 @@
+/home/koha-koha/illbackends/FreeForm
\ No newline at end of file
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/strings.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/strings.inc
index 5ef174db7c..3d4dcbd333 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/strings.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/strings.inc
@@ -42,6 +42,7 @@
     var CANCEL = _("Cancel");
     var RESUME = _("Resume");
     var SUSPEND = _("Suspend");
+    var SAVE = _("Save");
     var SUSPEND_UNTIL = _("Suspend until:");
     var SUSPEND_HOLD_ON = _("Suspend hold on");
     var CLEAR_DATE_TO_SUSPEND_INDEFINITELY = _("Clear date to suspend indefinitely");
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
index 1eeb19e3af..51ae68e75a 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
@@ -1050,6 +1050,10 @@
         var MSG_CONFIRM_DELETE_MESSAGE = _("Are you sure you want to delete this message? This cannot be undone.");
         var SuspendHoldsIntranet = [% ( Koha.Preference('SuspendHoldsIntranet') ) ? 1 : 0 | html %];
     </script>
+    <script>
+        /* Set values neededd in checkouts.js */
+        var canEditDueDates = [% Koha.Preference('EditDueDates') | html %];
+    </script>
     [% Asset.js("js/pages/circulation.js") | $raw %]
     [% Asset.js("js/checkouts.js") | $raw %]
     [% Asset.js("js/holds.js") | $raw %]
diff --git a/koha-tmpl/intranet-tmpl/prog/js/checkouts.js b/koha-tmpl/intranet-tmpl/prog/js/checkouts.js
index c1ca172f33..d9295ed9e7 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/checkouts.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/checkouts.js
@@ -268,7 +268,11 @@ $(document).ready(function() {
                             due = "<span class='overdue'>" + due + "</span>";
                         }
 
-                        due = "<span id='date_due_" + oObj.itemnumber + "' class='date_due'>" + due + "</span>";
+                        due = "<div data-issue_id='"+ oObj.issue_id + "' style='white-space:nowrap'><span id='date_due_" + oObj.itemnumber + "' class='date_due'>" + due + "</span>";
+
+                        if (canEditDueDates) {
+                            due += " <a href='#' class='edit_due_date_btn btn btn-default btn-xs'><i class='fa fa-pencil'></i> Edit</a></div>";
+                        }
 
                         if ( oObj.lost && oObj.claims_returned ) {
                             due += "<span class='lost claims_returned'>" + oObj.lost.escapeHtml() + "</span>";
@@ -1091,4 +1095,56 @@ $(document).ready(function() {
 
     });
 
+    // Allow due date editing if appropriate
+    if (canEditDueDates) {
+        $(document).on('click', '.edit_due_date_btn', function(e) {
+            e.preventDefault();
+            var date_display = $(this).siblings('.date_due');
+            $('#issues-table .edit_due_date_btn').attr('disabled', '1');
+            var current = date_display.text();
+            date_display.hide();
+            $(this).closest('div').append('<input style="max-width:10em" type="text" id="due_date_select" value="' + current + '"></input>' +
+                '<a style="margin-left:1.5em" href="#" class="save_due_date_btn btn btn-default btn-xs"><i class="fa fa-save"></i> ' + SAVE + '</a>');
+            $(this).hide();
+            $("#due_date_select").datetimepicker({
+                onClose: function(dateText, inst) {
+                    validate_date(dateText, inst);
+                },
+                minDate: Date_from_syspref(date_display.text()),
+                hour: 23,
+                minute: 59
+            }).on("change", function(e) {
+                if ( ! is_valid_date( $(this).val() ) ) {$(this).val("");}
+            });
+        });
+        $(document).on('click', '.save_due_date_btn', function(e) {
+            e.preventDefault();
+            var saveBtn = $(this);
+            var p = saveBtn.closest('div');
+            var checkoutId = p.data('issue_id');
+            var due_date = saveBtn.siblings('#due_date_select')[0].value;
+            p.append("<img style='margin-left:1.5em' id='due_date_spinner' src='" + interface + "/" + theme + "/img/spinner-small.gif' />");
+            saveBtn.hide();
+            if (due_date && due_date.length > 0) {
+                var dt = DateTime_from_syspref(due_date);
+                $.ajax({
+                    url: `/api/v1/checkouts/${checkoutId}`,
+                    type: 'PUT',
+                    data: JSON.stringify({ date_due: dt.toISOString() }),
+                    success: function( data ) {
+                        var dateFormatted = ISO_to_syspref(data.due_date);
+                        saveBtn.siblings('.date_due').text(dateFormatted).show();
+                        $('#issues-table .edit_due_date_btn').removeAttr('disabled');
+                        p.find('#due_date_select').remove()
+                        p.find('.ui-datepicker-trigger').remove()
+                        p.find('#due_date_spinner').remove();
+                        p.find('.edit_due_date_btn').show();
+                        saveBtn.remove();
+                    },
+                    contentType: "json"
+                });
+            }
+        });
+    }
+
  });
diff --git a/svc/checkouts b/svc/checkouts
index f8fd541f36..335c742776 100755
--- a/svc/checkouts
+++ b/svc/checkouts
@@ -64,6 +64,7 @@ print $input->header( -type => 'text/plain', -charset => 'UTF-8' );
 my @parameters;
 my $sql = '
     SELECT
+        issue_id,
         issues.issuedate,
         issues.date_due,
         issues.date_due < now() as date_due_overdue,
@@ -189,6 +190,7 @@ while ( my $c = $sth->fetchrow_hashref() ) {
     my @subtitles = split(/ \| /, $c->{'subtitle'} // '' );
     my $checkout = {
         DT_RowId             => $c->{itemnumber} . '-' . $c->{borrowernumber},
+        issue_id             => $c->{issue_id},
         title                => $c->{title},
         subtitle             => \@subtitles,
         medium               => $c->{medium} // '',
-- 
2.11.0