Bug 20462: Ensure batch item deletion will not delete the same item twice
[koha.git] / tools / inventory.pl
index 1ee40a1..3da000a 100755 (executable)
@@ -34,6 +34,8 @@ use C4::Koha;
 use C4::Circulation;
 use C4::Reports::Guided;    #_get_column_defs
 use C4::Charset;
+
+use Koha::Biblios;
 use Koha::DateUtils;
 use Koha::AuthorisedValues;
 use Koha::BiblioFrameworks;
@@ -69,7 +71,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 ";
@@ -86,7 +88,7 @@ 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});
@@ -152,10 +154,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;
@@ -255,20 +254,14 @@ if( @scanned_items ) {
 # status, or are still checked out.
 foreach my $item ( @scanned_items ) {
     $item->{notforloancode} = $item->{notforloan}; # save for later use
+    my $fc = $item->{'frameworkcode'} || '';
 
-    # 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}};
-            }
+    # 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};
         }
     }
 
@@ -311,9 +304,9 @@ my $loop = $uploadbarcodes
     ? [ map { $results->{$_} } keys %$results ]
     : $inventorylist // [];
 for my $item ( @$loop ) {
-    my $biblio = C4::Biblio::GetBiblioData($item->{biblionumber});
-    $item->{title} = $biblio->{title};
-    $item->{author} = $biblio->{author};
+    my $biblio = Koha::Biblios->find( $item->{biblionumber} );
+    $item->{title} = $biblio->title;
+    $item->{author} = $biblio->author;
 }
 
 $template->param(
@@ -356,7 +349,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) {