From a99a65f7863cae877a7bfb2663087eaedfb674ee Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Wed, 21 Jul 2021 13:56:22 +0000
Subject: [PATCH] Bug 28730: Add option to format AH field (due date) in SIP
 checkout response

This patch adds a new option to sip accounts:
format_due_date

If set to 1 the AH field in checkouts will follow the dateFormat system preference and format as due dates (ignoring time portion if 23:59:59 or 11:59:59)

To test:
 1 - checkout an item via SIP:
perl misc/sip_cli_emulator.pl -a localhost -p 6001 -su term1 -sp term1 -l CPL -t CR --item 3999900000001 --patron enda -m checkout
 2 - Note the AH field is like "YYYYMMDD    HHMMSS"
 3 - Apply patch
 4 - repeat SIP checkout - nothing has changed
 5 - edit SIPConfig.xml and add format_due_date="1" to 'term1' account:
    <login id="term1"  password="term1" delimiter="|" error-detect="enabled" institution="CPL" encoding="ascii"     checked_in_ok="1" format_due_date="1"/>
 6 - restart all
 7 - repeat the SIP checkout
 8 - AH field now matches dateFormat system preference
 9 - Change your dateFormat preference
10 - repeat the SIP checkout
11 - AH matches new format

Note: If you cannot renew and don't get an AH just check the item back in:
perl misc/sip_cli_emulator.pl -a localhost -p 6001 -su term1 -sp term1 -l CPL -t CR --item 3999900000001 --patron enda -m checkin
---
 C4/SIP/Sip/MsgType.pm        |  9 ++++++++-
 t/db_dependent/SIP/Message.t | 11 ++++++++++-
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/C4/SIP/Sip/MsgType.pm b/C4/SIP/Sip/MsgType.pm
index 37b34c0304..571f4ac3ea 100644
--- a/C4/SIP/Sip/MsgType.pm
+++ b/C4/SIP/Sip/MsgType.pm
@@ -21,6 +21,7 @@ use C4::Auth qw(&check_api_auth);
 use Koha::Patrons;
 use Koha::Patron::Attributes;
 use Koha::Items;
+use Koha::DateUtils qw( output_pref );
 
 use UNIVERSAL::can;
 
@@ -562,7 +563,13 @@ sub handle_checkout {
         $resp .= add_field( FID_ITEM_ID,   $item_id, $server );
         $resp .= add_field( FID_TITLE_ID,  $item->title_id, $server );
         if ( $item->due_date ) {
-            $resp .= add_field( FID_DUE_DATE, timestamp( $item->due_date ), $server );
+            my $due_date;
+            if( $account->{format_due_date} ){
+                $due_date = output_pref({ str => $item->due_date, as_due_date => 1 });
+            } else {
+                $due_date = timestamp( $item->due_date );
+            }
+            $resp .= add_field( FID_DUE_DATE, $due_date, $server );
         } else {
             $resp .= add_field( FID_DUE_DATE, q{}, $server );
         }
diff --git a/t/db_dependent/SIP/Message.t b/t/db_dependent/SIP/Message.t
index 790248b748..eef8f28204 100755
--- a/t/db_dependent/SIP/Message.t
+++ b/t/db_dependent/SIP/Message.t
@@ -71,7 +71,7 @@ subtest 'Testing Patron Info Request V2' => sub {
 subtest 'Checkout V2' => sub {
     my $schema = Koha::Database->new->schema;
     $schema->storage->txn_begin;
-    plan tests => 3;
+    plan tests => 5;
     $C4::SIP::Sip::protocol_version = 2;
     test_checkout_v2();
     $schema->storage->txn_rollback;
@@ -614,6 +614,15 @@ sub test_checkout_v2 {
     $msg->handle_checkout( $server );
     $respcode = substr( $response, 0, 2 );
     is( Koha::Checkouts->search({ itemnumber => $item_object->id })->count, 1, "Item was checked out (prevcheckout_block_checkout disabled)");
+
+    $msg->handle_checkout( $server );
+    ok( $response =~ m/AH\d{8}    \d{6}/, "Found AH field as timestamp in response");
+    $server->{account}->{format_due_date} = 1;
+    t::lib::Mocks::mock_preference( 'dateFormat',  'sql' );
+    undef $response;
+    $msg->handle_checkout( $server );
+    ok( $response =~ m/AH\d{4}-\d{2}-\d{2}/, "Found AH field as SQL date in response");
+
 }
 
 sub test_checkin_v2 {
-- 
2.20.1