Bug 25765: (QA follow-up) Save a DB hit
[srvgit] / Koha / Illrequest.pm
index e81997f..5ab0adc 100644 (file)
@@ -27,6 +27,7 @@ use Try::Tiny;
 use DateTime;
 
 use Koha::Database;
+use Koha::DateUtils qw/ dt_from_string /;
 use Koha::Email;
 use Koha::Exceptions::Ill;
 use Koha::Illcomments;
@@ -39,7 +40,6 @@ use Koha::Biblios;
 use Koha::Items;
 use Koha::ItemTypes;
 use Koha::Libraries;
-use C4::Items qw( AddItem );
 use C4::Circulation qw( CanBookBeIssued AddIssue  );
 
 use base qw(Koha::Object);
@@ -495,19 +495,21 @@ sub _core_status_graph {
             ui_method_name => 'Check out',
             needs_prefs    => [ 'CirculateILL' ],
             needs_perms    => [ 'user_circulate_circulate_remaining_permissions' ],
+            # An array of functions that all must return true
+            needs_all      => [ sub { my $r = shift;  return $r->biblio; } ],
             method         => 'check_out',
             next_actions   => [ ],
             ui_method_icon => 'fa-upload',
         },
-               RET => {
-                       prev_actions   => [ 'CHK' ],
-                       id             => 'RET',
-                       name           => 'Returned to library',
-                       ui_method_name => 'Check in',
-                       method         => 'check_in',
-                       next_actions   => [ 'COMP' ],
-                       ui_method_icon => 'fa-download',
-               }
+        RET => {
+            prev_actions   => [ 'CHK' ],
+            id             => 'RET',
+            name           => 'Returned to library',
+            ui_method_name => 'Check in',
+            method         => 'check_in',
+            next_actions   => [ 'COMP' ],
+            ui_method_icon => 'fa-download',
+        }
     };
 }
 
@@ -687,7 +689,7 @@ Mark a request as completed (status = COMP).
 sub mark_completed {
     my ( $self ) = @_;
     $self->status('COMP')->store;
-    $self->completed(DateTime->now)->store;
+    $self->completed(dt_from_string())->store;
     return {
         error   => 0,
         status  => '',
@@ -1049,6 +1051,25 @@ sub requires_moderation {
     return $require_moderation->{$self->status};
 }
 
+=head3 biblio
+
+    my $biblio = $request->biblio;
+
+For a given request, return the biblio associated with it,
+or undef if none exists
+
+=cut
+
+sub biblio {
+    my ( $self ) = @_;
+
+    return if !$self->biblio_id;
+
+    return Koha::Biblios->find({
+        biblionumber => $self->biblio_id
+    });
+}
+
 =head3 check_out
 
     my $stage_summary = $request->check_out;
@@ -1072,9 +1093,8 @@ sub check_out {
         {},
         { order_by => ['branchcode'] }
     );
-    my $biblio = Koha::Biblios->find({
-        biblionumber => $self->biblio_id
-    });
+    my $biblio = $self->biblio;
+
     # Find all statistical patrons
     my $statistical_patrons = Koha::Patrons->search(
         { 'category_type' => 'x' },
@@ -1146,15 +1166,17 @@ sub check_out {
         my $itemnumber;
         if ($item_count == 0) {
             my $item_hash = {
+                biblionumber  => $self->biblio_id,
                 homebranch    => $params->{branchcode},
                 holdingbranch => $params->{branchcode},
                 location      => $params->{branchcode},
                 itype         => $params->{item_type},
                 barcode       => 'ILL-' . $self->illrequest_id
             };
-            my (undef, undef, $item_no) =
-                AddItem($item_hash, $self->biblio_id);
-            $itemnumber = $item_no;
+            try {
+                my $item = Koha::Item->new($item_hash)->store;
+                $itemnumber = $item->itemnumber;
+            };
         } else {
             $itemnumber = $items[0]->itemnumber;
         }