+subtest 'automatic_checkin' => sub {
+
+ plan tests => 9;
+
+ $schema->storage->txn_begin;
+
+ my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+
+ my $due_ac_item =
+ $builder->build_sample_item(
+ { homebranch => $patron->branchcode, itemlost => 0 } );
+ my $ac_item =
+ $builder->build_sample_item(
+ { homebranch => $patron->branchcode, itemlost => 0 } );
+ my $odue_ac_item =
+ $builder->build_sample_item(
+ { homebranch => $patron->branchcode, itemlost => 0 } );
+ my $normal_item =
+ $builder->build_sample_item(
+ { homebranch => $patron->branchcode, itemlost => 0 } );
+
+ $due_ac_item->itemtype->automatic_checkin(1)->store;
+ $odue_ac_item->itemtype->automatic_checkin(1)->store;
+ $ac_item->itemtype->automatic_checkin(1)->store;
+ $normal_item->itemtype->automatic_checkin(0)->store;
+
+ my $today = dt_from_string;
+ my $tomorrow = dt_from_string->add( days => 1 );
+ my $yesterday = dt_from_string->subtract( days => 1 );
+
+ # Checkout do for automatic checkin
+ my $checkout_due_aci = Koha::Checkout->new(
+ {
+ borrowernumber => $patron->borrowernumber,
+ itemnumber => $due_ac_item->itemnumber,
+ branchcode => $patron->branchcode,
+ date_due => $today,
+ }
+ )->store;
+
+ # Checkout not due for automatic checkin
+ my $checkout_odue_aci = Koha::Checkout->new(
+ {
+ borrowernumber => $patron->borrowernumber,
+ itemnumber => $odue_ac_item->itemnumber,
+ branchcode => $patron->branchcode,
+ date_due => $yesterday
+ }
+ )->store;
+
+ # Checkout not due for automatic checkin
+ my $checkout_aci = Koha::Checkout->new(
+ {
+ borrowernumber => $patron->borrowernumber,
+ itemnumber => $ac_item->itemnumber,
+ branchcode => $patron->branchcode,
+ date_due => $tomorrow
+ }
+ )->store;
+
+ # due checkout for nomal itemtype
+ my $checkout_ni = Koha::Checkout->new(
+ {
+ borrowernumber => $patron->borrowernumber,
+ itemnumber => $normal_item->itemnumber,
+ branchcode => $patron->branchcode,
+ date_due => $today,
+ }
+ )->store;
+
+ my $searched = Koha::Checkouts->find( $checkout_ni->issue_id );
+ is( $searched->issue_id, $checkout_ni->issue_id,
+ 'checkout for normal_item exists' );
+
+ $searched = Koha::Checkouts->find( $checkout_aci->issue_id );
+ is( $searched->issue_id, $checkout_aci->issue_id,
+ 'checkout for ac_item exists' );
+
+ $searched = Koha::Checkouts->find( $checkout_due_aci->issue_id );
+ is(
+ $searched->issue_id,
+ $checkout_due_aci->issue_id,
+ 'checkout for due_ac_item exists'
+ );
+
+ $searched = Koha::Checkouts->find( $checkout_odue_aci->issue_id );
+ is(
+ $searched->issue_id,
+ $checkout_odue_aci->issue_id,
+ 'checkout for odue_ac_item exists'
+ );
+
+ Koha::Checkouts->automatic_checkin;
+
+ $searched = Koha::Checkouts->find( $checkout_ni->issue_id );
+ is( $searched->issue_id, $checkout_ni->issue_id,
+ 'checkout for normal_item still exists' );
+
+ $searched = Koha::Checkouts->find( $checkout_aci->issue_id );
+ is( $searched->issue_id, $checkout_aci->issue_id,
+ 'checkout for ac_item still exists' );
+
+ $searched = Koha::Checkouts->find( $checkout_due_aci->issue_id );
+ is( $searched, undef, 'checkout for due_ac_item doesn\'t exist anymore' );
+
+ $searched = Koha::Checkouts->find( $checkout_odue_aci->issue_id );
+ is( $searched, undef, 'checkout for odue_ac_item doesn\'t exist anymore' );
+
+ $searched = Koha::Old::Checkouts->find( $checkout_odue_aci->issue_id );
+ is( dt_from_string($searched->returndate), $yesterday, 'old checkout for odue_ac_item has the right return date' );
+
+ $schema->storage->txn_rollback;
+}