Bug 28581: Use 'from_email_address' where appropriate
[koha-ffzg.git] / Koha / Checkouts.pm
index ccfeb36..0f2da95 100644 (file)
@@ -22,8 +22,10 @@ use Modern::Perl;
 use Carp;
 
 use C4::Context;
+use C4::Circulation;
 use Koha::Checkout;
 use Koha::Database;
+use Koha::DateUtils;
 
 use base qw(Koha::Objects);
 
@@ -47,13 +49,47 @@ sub calculate_dropbox_date {
     my $userenv    = C4::Context->userenv;
     my $branchcode = $userenv->{branch} // q{};
 
-    my $calendar = Koha::Calendar->new( branchcode => $branchcode );
-    my $today        = DateTime->now( time_zone => C4::Context->tz() );
-    my $dropbox_date = $calendar->addDate( $today, -1 );
+    my $daysmode = Koha::CirculationRules->get_effective_daysmode(
+        {
+            categorycode => undef,
+            itemtype     => undef,
+            branchcode   => $branchcode,
+        }
+    );
+    my $calendar     = Koha::Calendar->new( branchcode => $branchcode, days_mode => $daysmode );
+    my $today        = dt_from_string;
+    my $dropbox_date = $calendar->addDuration( $today, -1 );
 
     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, undef, dt_from_string($checkout->date_due) );
+        }
+    }
+}
+
 =head3 type
 
 =cut