From e47016c1cc29d1106d1afc2d90cdba1255412f10 Mon Sep 17 00:00:00 2001
From: Galen Charlton <gmc@equinoxinitiative.org>
Date: Fri, 15 Sep 2017 14:38:18 -0400
Subject: [PATCH] LP#1178802: force ceiling dates to be end of day

This patch ensures that when a hard due date (or hard due date value)
is created or edited, the ceiling date is set to the end of that day.

This fixes a problem where a ceiling date would not be applied for
loans checked out earlier on the ceiling date itself.

This patch intentionally does not touch existing hard due date
values.  It also does not use the database or middle layer to coerce
the ceiling dates to end of day to leave upon the possiblity that
an Angular version of the hard due date editor will allow operators
to set both date and time. (Note that Dojo does not readily have a
combined date/time picker.)

To test
-------
[1] Apply the patch, then in the hard due date editor, verify that
    when creating a hard due date _value_, the date gets saved as
    ending at 23:59:59 in the local time zone.
[2] Similarly, verify that when editing an existing hard due date value,
    the date gets saved as ending at 23:59:59.
[3] Repeat steps 1 and 2, but this time for hard due dates.

Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Signed-off-by: Michele Morgan <mmorgan@noblenet.org>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
---
 .../conify/global/config/hard_due_date.tt2          | 21 +++++++++++++++++++++
 .../conify/global/config/hard_due_date_values.tt2   | 20 ++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/Open-ILS/src/templates/conify/global/config/hard_due_date.tt2 b/Open-ILS/src/templates/conify/global/config/hard_due_date.tt2
index 1c9940d5a2..d50a483ed3 100644
--- a/Open-ILS/src/templates/conify/global/config/hard_due_date.tt2
+++ b/Open-ILS/src/templates/conify/global/config/hard_due_date.tt2
@@ -4,6 +4,9 @@
     dojo.require("openils.CGI");
     dojo.require("openils.Util");
     dojo.require("openils.widget.AutoGrid");
+    dojo.require("dojo.date.locale");
+    dojo.require("dojo.date.stamp");
+
 
     function get_chdd_name(rowIndex, item) {
         if (!item) return null;
@@ -37,6 +40,24 @@
                 openils.Util.show("one_only");
             }
             chddGrid.loadAll({"order_by": {"chdd": "name"}}, filter);
+
+            function munge_ceiling(chdd) {
+                var cd = dojo.date.stamp.fromISOString(chdd.ceiling_date());
+                // set to end of day in client time zone; if we were using
+                // moment, we could do moment().endOf('day') instead
+                cd.setHours(23, 59, 59);
+                chdd.ceiling_date(dojo.date.stamp.toISOString(cd));
+            }
+            chddGrid.onEditPane = function(pane) {
+                pane.onSubmit = function(chdd, ops) {
+                    munge_ceiling(chdd);
+                    (new openils.PermaCrud())[pane.mode](chdd, ops);
+               }
+            }
+            chddGrid.createPaneOnSubmit = function(chdd, ops, pane) {
+                munge_ceiling(chdd);
+                (new openils.PermaCrud())[pane.mode](chdd, ops);
+            }
         }
     );
 </script>
diff --git a/Open-ILS/src/templates/conify/global/config/hard_due_date_values.tt2 b/Open-ILS/src/templates/conify/global/config/hard_due_date_values.tt2
index 91fbe2e7c2..8854bcb437 100644
--- a/Open-ILS/src/templates/conify/global/config/hard_due_date_values.tt2
+++ b/Open-ILS/src/templates/conify/global/config/hard_due_date_values.tt2
@@ -6,6 +6,8 @@
     dojo.require("openils.Util");
     dojo.require("openils.widget.AutoGrid");
     dojo.require("dijit.form.TextBox");
+    dojo.require("dojo.date.locale");
+    dojo.require("dojo.date.stamp");
 
     openils.Util.addOnLoad(
         function() {
@@ -33,6 +35,24 @@
                 "create": chdd_id
             };
 
+            function munge_ceiling(chddv) {
+                var cd = dojo.date.stamp.fromISOString(chddv.ceiling_date());
+                // set to end of day in client time zone; if we were using
+                // moment, we could do moment().endOf('day') instead
+                cd.setHours(23, 59, 59);
+                chddv.ceiling_date(dojo.date.stamp.toISOString(cd));
+            }
+            chddvGrid.onEditPane = function(pane) {
+                pane.onSubmit = function(chddv, ops) {
+                    munge_ceiling(chddv);
+                    (new openils.PermaCrud())[pane.mode](chddv, ops);
+               }
+            }
+            chddvGrid.createPaneOnSubmit = function(chddv, ops, pane) {
+                munge_ceiling(chddv);
+                (new openils.PermaCrud())[pane.mode](chddv, ops);
+            }
+
             chddvGrid.loadAll({"order_by": {"chddv": "active_date"}}, filter);
         }
     );
-- 
2.11.0