Bug 16154: Fix some other occurrences
[koha.git] / tools / inventory.pl
index 6a5ddc4..990855f 100755 (executable)
@@ -5,24 +5,24 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
 
 #need to open cgi and get the fh before anything else opens a new cgi context (see C4::Auth)
-use CGI;
+use CGI qw ( -utf8 );
 my $input = CGI->new;
 my $uploadbarcodes = $input->param('uploadbarcodes');
 
@@ -52,6 +52,7 @@ 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 ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     {   template_name   => "tools/inventory.tt",
@@ -150,7 +151,7 @@ my @scanned_items;
 my @errorloop;
 if ( $uploadbarcodes && length($uploadbarcodes) > 0 ) {
     my $dbh = C4::Context->dbh;
-    my $date = dt_from_string( $input->param('setdate') );
+    my $date = dt_from_string( scalar $input->param('setdate') );
     $date = output_pref ( { dt => $date, dateformat => 'iso' } );
 
     my $strsth  = "select * from issues, items where items.itemnumber=issues.itemnumber and items.barcode =?";
@@ -161,6 +162,7 @@ if ( $uploadbarcodes && length($uploadbarcodes) > 0 ) {
     my $count = 0;
 
     my @barcodes;
+    my @uploadedbarcodes;
 
     my $sth = $dbh->column_info(undef,undef,"items","barcode");
     my $barcode_def = $sth->fetchall_hashref('COLUMN_NAME');
@@ -169,8 +171,13 @@ if ( $uploadbarcodes && length($uploadbarcodes) > 0 ) {
     my $err_data=0;
     my $lines_read=0;
     binmode($uploadbarcodes, ":encoding(UTF-8)");
-    while (my $barcode=<$uploadbarcodes>){
-        $barcode =~ s/\r?\n$//;
+    while (my $barcode=<$uploadbarcodes>) {
+        $barcode =~ s/\r/\n/g;
+        $barcode =~ s/\n\n/\n/g;
+        my @data = split(/\n/,$barcode);
+        push @uploadedbarcodes, @data;
+    }
+    for my $barcode (@uploadedbarcodes) {
         next unless $barcode;
         ++$lines_read;
         if (length($barcode)>$barcode_size) {
@@ -203,14 +210,16 @@ if ( $uploadbarcodes && length($uploadbarcodes) > 0 ) {
                 ModItem( { datelastseen => $date }, undef, $item->{'itemnumber'} );
                 push @scanned_items, $item;
                 $count++;
-                $qonloan->execute($barcode);
-                if ($qonloan->rows){
-                    my $data = $qonloan->fetchrow_hashref;
-                    my ($doreturn, $messages, $iteminformation, $borrower) =AddReturn($barcode, $data->{homebranch});
-                    if ($doreturn){
-                        push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_RET'=>1}
-                    } else {
-                        push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_NOT_RET'=>1}
+                unless ( $dont_checkin ) {
+                    $qonloan->execute($barcode);
+                    if ($qonloan->rows){
+                        my $data = $qonloan->fetchrow_hashref;
+                        my ($doreturn, $messages, $iteminformation, $borrower) =AddReturn($barcode, $data->{homebranch});
+                        if ($doreturn){
+                            push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_RET'=>1}
+                        } else {
+                            push @errorloop, {'barcode'=>$barcode,'ERR_ONLOAN_NOT_RET'=>1}
+                        }
                     }
                 }
             } else {
@@ -226,20 +235,54 @@ if ( $uploadbarcodes && length($uploadbarcodes) > 0 ) {
 
 # now build the result list: inventoried items if requested, and mis-placed items -always-
 my $inventorylist;
+my $wrongplacelist;
 my @items_with_problems;
 if ( $markseen or $op ) {
     # retrieve all items in this range.
     my $totalrecords;
-    ($inventorylist, $totalrecords) = GetItemsForInventory($minlocation, $maxlocation, $location, $itemtype, $ignoreissued, '', $branchcode, $branch, 0, undef , $staton);
+
+    # We use datelastseen only when comparing the results to the barcode file.
+    my $paramdatelastseen = ($compareinv2barcd) ? $datelastseen : '';
+    ($inventorylist, $totalrecords) = GetItemsForInventory( {
+      minlocation  => $minlocation,
+      maxlocation  => $maxlocation,
+      location     => $location,
+      itemtype     => $itemtype,
+      ignoreissued => $ignoreissued,
+      datelastseen => $paramdatelastseen,
+      branchcode   => $branchcode,
+      branch       => $branch,
+      offset       => 0,
+      size         => undef,
+      statushash   => $staton,
+      interface    => 'staff',
+    } );
+
+    # For the items that may be marked as "wrong place", we only check the location (callnumbers, location and branch)
+    ($wrongplacelist, $totalrecords) = GetItemsForInventory( {
+      minlocation  => $minlocation,
+      maxlocation  => $maxlocation,
+      location     => $location,
+      itemtype     => undef,
+      ignoreissued => undef,
+      datelastseen => undef,
+      branchcode   => $branchcode,
+      branch       => $branch,
+      offset       => 0,
+      size         => undef,
+      statushash   => undef,
+      interface    => 'staff',
+    } );
+
 }
 
 # If "compare barcodes list to results" has been checked, we want to alert for missing items
 if ( $compareinv2barcd ) {
-    # set "missing" flags for all items with a datelastseen (dls) before the choosen datelastseen (cdls)
+    # set "missing" flags for all items with a datelastseen (dls) before the chosen datelastseen (cdls)
     my $dls = output_pref( { dt => dt_from_string( $datelastseen ),
                              dateformat => 'iso' } );
     foreach my $item ( @$inventorylist ) {
-        my $cdls = output_pref( { dt => dt_from_string( $_->{datelastseen} ),
+        my $cdls = output_pref( { dt => dt_from_string( $item->{datelastseen} ),
                                   dateformat => 'iso' } );
         if ( $cdls lt $dls ) {
             $item->{problem} = 'missingitem';
@@ -252,7 +295,7 @@ if ( $compareinv2barcd ) {
 
 
 # insert "wrongplace" to all scanned items that are not supposed to be in this range
-# note this list is always displayed, whatever the librarian has choosen for comparison
+# note this list is always displayed, whatever the librarian has chosen for comparison
 my $moddatecount = 0;
 foreach my $item ( @scanned_items ) {
 
@@ -280,13 +323,13 @@ foreach my $item ( @scanned_items ) {
         $item->{problem} = 'changestatus';
         push @items_with_problems, { %$item };
     }
-    if (none { $item->{barcode} eq $_->{barcode} && !$_->{'onloan'} } @$inventorylist) {
+    if (none { $item->{barcode} eq $_->{barcode} && !$_->{'onloan'} } @$wrongplacelist) {
         $item->{problem} = 'wrongplace';
         push @items_with_problems, { %$item };
     }
 
     # Modify date last seen for scanned items
-    ModDateLastSeen($_->{'itemnumber'});
+    ModDateLastSeen($item->{'itemnumber'});
     $moddatecount++;
 }
 
@@ -345,7 +388,7 @@ if (defined $input->param('CSVexport') && $input->param('CSVexport') eq 'on'){
                       items.homebranch     items.location
                       items.itemcallnumber items.notforloan
                       items.itemlost       items.damaged
-                      items.stocknumber
+                      items.withdrawn      items.stocknumber
                       / ) {
        push @translated_keys, $columns_def_hashref->{$key};
     }
@@ -354,7 +397,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 stocknumber /;
+    my @keys = qw / title author barcode itemnumber homebranch location itemcallnumber notforloan lost damaged withdrawn stocknumber /;
     for my $item ( @results ) {
         my @line;
         for my $key (@keys) {