Bug 20581: (follow-up) Fix status_alias comparison
[koha.git] / tools / inventory.pl
index d6f9e4a..eff4fe2 100755 (executable)
@@ -39,19 +39,23 @@ use Koha::Biblios;
 use Koha::DateUtils;
 use Koha::AuthorisedValues;
 use Koha::BiblioFrameworks;
+use Koha::ClassSources;
 use List::MoreUtils qw( none );
 
 my $minlocation=$input->param('minlocation') || '';
 my $maxlocation=$input->param('maxlocation');
+my $class_source=$input->param('class_source');
 $maxlocation=$minlocation.'Z' unless ( $maxlocation || ! $minlocation );
 my $location=$input->param('location') || '';
 my $ignoreissued=$input->param('ignoreissued');
+my $ignore_waiting_holds = $input->param('ignore_waiting_holds');
 my $datelastseen = $input->param('datelastseen'); # last inventory date
 my $branchcode = $input->param('branchcode') || '';
 my $branch     = $input->param('branch');
 my $op         = $input->param('op');
 my $compareinv2barcd = $input->param('compareinv2barcd');
 my $dont_checkin = $input->param('dont_checkin');
+my $out_of_order = $input->param('out_of_order');
 
 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     {   template_name   => "tools/inventory.tt",
@@ -71,7 +75,7 @@ unshift @$frameworks, { frameworkcode => '' };
 
 for my $fwk ( @$frameworks ){
   my $fwkcode = $fwk->{frameworkcode};
-  my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fwkcode, kohafield => 'items.location', authorised_value => { not => undef } });
+  my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fwkcode, kohafield => 'items.location', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
   my $authcode = $mss->count ? $mss->next->authorised_value : undef;
     if ($authcode && $authorisedvalue_categories!~/\b$authcode\W/){
       $authorisedvalue_categories.="$authcode ";
@@ -88,14 +92,14 @@ my @notforloans;
 for my $statfield (qw/items.notforloan items.itemlost items.withdrawn items.damaged/){
     my $hash = {};
     $hash->{fieldname} = $statfield;
-    my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => $statfield, authorised_value => { not => undef } });
+    my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => $statfield, authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
     $hash->{authcode} = $mss->count ? $mss->next->authorised_value : undef;
     if ($hash->{authcode}){
         my $arr = GetAuthorisedValues($hash->{authcode});
         if ( $statfield eq 'items.notforloan') {
             # Add notforloan == 0 to the list of possible notforloan statuses
             # The lib value is replaced in the template
-            push @$arr, { authorised_value => 0, id => 'stat0' , lib => 'ignore' } if ! grep { $_->{authorised_value} eq '0' } @$arr;
+            push @$arr, { authorised_value => 0, id => 'stat0' , lib => '__IGNORE__' } if ! grep { $_->{authorised_value} eq '0' } @$arr;
             @notforloans = map { $_->{'authorised_value'} } @$arr;
         }
         $hash->{values} = $arr;
@@ -114,6 +118,10 @@ for my $authvfield (@$statuses) {
     }
 }
 
+my @class_sources = Koha::ClassSources->search({ used => 1 });
+my $pref_class = C4::Context->preference("DefaultClassificationSource");
+
+
 $template->param(
     authorised_values        => \@authorised_value_list,
     today                    => dt_from_string,
@@ -126,6 +134,9 @@ $template->param(
     datelastseen             => $datelastseen,
     compareinv2barcd         => $compareinv2barcd,
     uploadedbarcodesflag     => $uploadbarcodes ? 1 : 0,
+    ignore_waiting_holds     => $ignore_waiting_holds,
+    class_sources            => \@class_sources,
+    pref_class               => $pref_class
 );
 
 # Walk through uploaded barcodes, report errors, mark as seen, check in
@@ -154,10 +165,7 @@ if ( $uploadbarcodes && length($uploadbarcodes) > 0 ) {
     my $lines_read=0;
     binmode($uploadbarcodes, ":encoding(UTF-8)");
     while (my $barcode=<$uploadbarcodes>) {
-        $barcode =~ s/\r/\n/g;
-        $barcode =~ s/\n\n/\n/g;
-        my @data = split(/\n/,$barcode);
-        push @uploadedbarcodes, @data;
+        push @uploadedbarcodes, grep { /\S/ } split( /[\n\r,;|-]/, $barcode );
     }
     for my $barcode (@uploadedbarcodes) {
         next unless $barcode;
@@ -225,6 +233,7 @@ if ( $op && ( !$uploadbarcodes || $compareinv2barcd )) {
     ( $inventorylist ) = GetItemsForInventory({
       minlocation  => $minlocation,
       maxlocation  => $maxlocation,
+      class_source => $class_source,
       location     => $location,
       ignoreissued => $ignoreissued,
       datelastseen => $datelastseen,
@@ -232,6 +241,7 @@ if ( $op && ( !$uploadbarcodes || $compareinv2barcd )) {
       branch       => $branch,
       offset       => 0,
       statushash   => $staton,
+      ignore_waiting_holds => $ignore_waiting_holds,
     });
 }
 # Build rightplacelist used to check if a scanned item is in the right place.
@@ -239,6 +249,7 @@ if( @scanned_items ) {
     ( $rightplacelist ) = GetItemsForInventory({
       minlocation  => $minlocation,
       maxlocation  => $maxlocation,
+      class_source => $class_source,
       location     => $location,
       ignoreissued => undef,
       datelastseen => undef,
@@ -246,6 +257,7 @@ if( @scanned_items ) {
       branch       => $branch,
       offset       => 0,
       statushash   => undef,
+      ignore_waiting_holds => $ignore_waiting_holds,
     });
     # Convert the structure to a hash on barcode
     $rightplacelist = {
@@ -255,22 +267,19 @@ if( @scanned_items ) {
 
 # Report scanned items that are on the wrong place, or have a wrong notforloan
 # status, or are still checked out.
-foreach my $item ( @scanned_items ) {
-    $item->{notforloancode} = $item->{notforloan}; # save for later use
+for ( my $i = 0; $i < @scanned_items; $i++ ) {
 
-    # Populating with authorised values
-    foreach my $field ( keys %$item ) {
-        # If the koha field is mapped to a marc field
-        my $fc = $item->{'frameworkcode'} || '';
-        my ($f, $sf) = GetMarcFromKohaField("items.$field", $fc);
-        if ($f and $sf) {
-            # We replace the code with it's description
-            my $av = Koha::AuthorisedValues->search_by_marc_field({ frameworkcode => $fc, tagfield => $f, tagsubfield => $sf, });
-            $av = $av->count ? $av->unblessed : [];
-            my $authvals = { map { ( $_->{authorised_value} => $_->{lib} ) } @$av };
-            if ($authvals and defined $item->{$field} and defined $authvals->{$item->{$field}}) {
-              $item->{$field} = $authvals->{$item->{$field}};
-            }
+    my $item = $scanned_items[$i];
+
+    $item->{notforloancode} = $item->{notforloan}; # save for later use
+    my $fc = $item->{'frameworkcode'} || '';
+
+    # Populating with authorised values description
+    foreach my $field (qw/ location notforloan itemlost damaged withdrawn /) {
+        my $av = Koha::AuthorisedValues->get_description_by_koha_field(
+            { frameworkcode => $fc, kohafield => "items.$field", authorised_value => $item->{$field} } );
+        if ( $av and defined $item->{$field} and defined $av->{lib} ) {
+            $item->{$field} = $av->{lib};
         }
     }
 
@@ -280,6 +289,24 @@ foreach my $item ( @scanned_items ) {
         additemtoresults( $item, $results );
     }
 
+    # Check for items shelved out of order
+    if ($out_of_order) {
+        unless ( $i == 0 ) {
+            my $previous_item = $scanned_items[ $i - 1 ];
+            if ( $previous_item && $item->{cn_sort} lt $previous_item->{cn_sort} ) {
+                $item->{problems}->{out_of_order} = 1;
+                additemtoresults( $item, $results );
+            }
+        }
+        unless ( $i == scalar(@scanned_items) ) {
+            my $next_item = $scanned_items[ $i + 1 ];
+            if ( $next_item && $item->{cn_sort} gt $next_item->{cn_sort} ) {
+                $item->{problems}->{out_of_order} = 1;
+                additemtoresults( $item, $results );
+            }
+        }
+    }
+
     # Report an item that is checked out (unusual!) or wrongly placed
     if( $item->{onloan} ) {
         $item->{problems}->{checkedout} = 1;
@@ -358,7 +385,7 @@ if (defined $input->param('CSVexport') && $input->param('CSVexport') eq 'on'){
     $csv->combine(@translated_keys);
     print $csv->string, "\n";
 
-    my @keys = qw / title author barcode itemnumber homebranch location itemcallnumber notforloan lost damaged withdrawn stocknumber /;
+    my @keys = qw/ title author barcode itemnumber homebranch location itemcallnumber notforloan itemlost damaged withdrawn stocknumber /;
     for my $item ( @$loop ) {
         my @line;
         for my $key (@keys) {