From c9a36ad3f714e5902a6cea2d4e7840b1f202c104 Mon Sep 17 00:00:00 2001
From: Agustin Moyano <agustinmoyano@theke.io>
Date: Fri, 4 Dec 2020 18:47:31 -0300
Subject: [PATCH] Bug 23207: Add automatic checkin feature

This patch adds the posibility to set an itemtype with automatic
checkin. It means that when the checkout is due, it will
automatically check in.

To test:
1. apply patches
2. updatedatabase
3. go to koha administration -> item types and edin an item type (from
   now on called itemtype1)
CHECK => there is a checkbox almost at the end called automatic checkin
4. check that checkbox and save
5. checkout 2 items from itemtype1 and one item from another itemtype
   (from now on called itemtype2)
6. go to mysql database console (koha-mysql) and manually set date_due = current_timestamp
   in issues table for the item of itemtype2 and only one of the items of
itemtype1
7. run cronjob at misc/cronjobs/automatic_checkin.pl
8. go to mysql database console again and select * from issues
SUCCESS => All issues are present except for the issue of itemtype1
which had it's date_due set to current_timestamp. That one was
automatically checked in.
9. prove t/db_dependent/Koha/Checkouts.t
---
 Koha/Checkouts.pm                                  | 28 ++++++++++++++++++++++
 admin/itemtypes.pl                                 |  3 +++
 debian/koha-common.cron.hourly                     |  1 +
 .../prog/en/modules/admin/itemtypes.tt             |  9 +++++++
 misc/cronjobs/automatic_checkin.pl                 | 27 +++++++++++++++++++++
 5 files changed, 68 insertions(+)
 create mode 100755 misc/cronjobs/automatic_checkin.pl

diff --git a/Koha/Checkouts.pm b/Koha/Checkouts.pm
index 5037e3fdf4..a7b50603d0 100644
--- a/Koha/Checkouts.pm
+++ b/Koha/Checkouts.pm
@@ -22,6 +22,7 @@ use Modern::Perl;
 use Carp;
 
 use C4::Context;
+use C4::Circulation;
 use Koha::Checkout;
 use Koha::Database;
 use Koha::DateUtils;
@@ -62,6 +63,33 @@ sub calculate_dropbox_date {
     return $dropbox_date;
 }
 
+=head3 automatic_checkin
+
+my $automatic_checkins = Koha::Checkouts->automatic_checkin()
+
+Checks in every due issue which itemtype has automatic_checkin enabled
+
+=cut
+
+sub automatic_checkin {
+    my ($self, $params) = @_;
+
+    my $current_date = dt_from_string;
+
+    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
+    my $due_checkouts = $self->search(
+        { date_due => { '<=' => $dtf->format_datetime($current_date) } },
+        { prefetch => 'item'}
+    );
+
+    while(my $checkout = $due_checkouts->next) {
+        if($checkout->item->itemtype->automatic_checkin) {
+            C4::Circulation::AddReturn($checkout->item->barcode, $checkout->branchcode);
+        }
+    }
+
+}
+
 =head3 type
 
 =cut
diff --git a/admin/itemtypes.pl b/admin/itemtypes.pl
index 07866f72fb..769e64416a 100755
--- a/admin/itemtypes.pl
+++ b/admin/itemtypes.pl
@@ -112,6 +112,7 @@ if ( $op eq 'add_form' ) {
     my $searchcategory = $input->param('searchcategory');
     my $rentalcharge_daily_calendar  = $input->param('rentalcharge_daily_calendar') // 0;
     my $rentalcharge_hourly_calendar = $input->param('rentalcharge_hourly_calendar') // 0;
+    my $automatic_checkin = $input->param('automatic_checkin') // 0;
 
     if ( $itemtype and $is_a_modif ) {    # it's a modification
         $itemtype->description($description);
@@ -131,6 +132,7 @@ if ( $op eq 'add_form' ) {
         $itemtype->searchcategory($searchcategory);
         $itemtype->rentalcharge_daily_calendar($rentalcharge_daily_calendar);
         $itemtype->rentalcharge_hourly_calendar($rentalcharge_hourly_calendar);
+        $itemtype->automatic_checkin($automatic_checkin);
 
         eval {
           $itemtype->store;
@@ -163,6 +165,7 @@ if ( $op eq 'add_form' ) {
                 searchcategory      => $searchcategory,
                 rentalcharge_daily_calendar  => $rentalcharge_daily_calendar,
                 rentalcharge_hourly_calendar => $rentalcharge_hourly_calendar,
+                automatic_checkin   => $automatic_checkin,
             }
         );
         eval {
diff --git a/debian/koha-common.cron.hourly b/debian/koha-common.cron.hourly
index 4db64bbaa2..0b8c2c8bdb 100644
--- a/debian/koha-common.cron.hourly
+++ b/debian/koha-common.cron.hourly
@@ -17,3 +17,4 @@
 
 
 koha-foreach --chdir --enabled /usr/share/koha/bin/cronjobs/holds/build_holds_queue.pl
+koha-foreach --chdir --enabled /usr/share/koha/bin/cronjobs/automatic_checkin.pl
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt
index 8ef4d32abe..65d59e308f 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/itemtypes.tt
@@ -340,6 +340,15 @@ Item types administration
                     <span class="hint">Select 'All libraries' if all libraries use this item type. Otherwise, select the specific libraries that use this item type.</span>
                 </li>
                 <li>
+                    <label for="automatic_checkin">Automatic checkin: </label>
+                        [% IF itemtype.automatic_checkin %]
+                            <input type="checkbox" id="automatic_checkin" name="automatic_checkin" checked="checked" value="1" />
+                        [% ELSE %]
+                            <input type="checkbox" id="automatic_checkin" name="automatic_checkin" value="1" />
+                        [% END %]
+                        <span class="hint">If checked, items will be checked in automatically after a period of time. Useful for materials where you really don't care for fisical return, like museum passes, etc.</span>
+                </li>
+                <li>
                     <label for="summary">Summary: </label>
                    <textarea id="summary" name="summary" cols="55" rows="5">[% itemtype.summary | html %]</textarea>
                     <p>Enter a summary that will overwrite the default one in search results lists. Example, for a website itemtype : </p>
diff --git a/misc/cronjobs/automatic_checkin.pl b/misc/cronjobs/automatic_checkin.pl
new file mode 100755
index 0000000000..4eff2d5083
--- /dev/null
+++ b/misc/cronjobs/automatic_checkin.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Copyright (C) 2020  Theke Solutions
+#
+# 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 Koha::Checkouts;
+use Koha::Script -cron;
+use C4::Log;
+
+cronlogaction();
+
+Koha::Checkouts->automatic_checkin;
\ No newline at end of file
-- 
2.11.0