Bug 21579: Make showdiffmarc.pl work for authorities and biblios
[koha.git] / tools / inventory.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2009 Biblibre S.A
4 # John Soros <john.soros@biblibre.com>
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22
23 #need to open cgi and get the fh before anything else opens a new cgi context (see C4::Auth)
24 use CGI qw ( -utf8 );
25 my $input = CGI->new;
26 my $uploadbarcodes = $input->param('uploadbarcodes');
27
28 use C4::Auth;
29 use C4::Context;
30 use C4::Output;
31 use C4::Biblio;
32 use C4::Items;
33 use C4::Koha;
34 use C4::Circulation;
35 use C4::Reports::Guided;    #_get_column_defs
36 use C4::Charset;
37
38 use Koha::Biblios;
39 use Koha::DateUtils;
40 use Koha::AuthorisedValues;
41 use Koha::BiblioFrameworks;
42 use List::MoreUtils qw( none );
43
44 my $minlocation=$input->param('minlocation') || '';
45 my $maxlocation=$input->param('maxlocation');
46 $maxlocation=$minlocation.'Z' unless ( $maxlocation || ! $minlocation );
47 my $location=$input->param('location') || '';
48 my $ignoreissued=$input->param('ignoreissued');
49 my $datelastseen = $input->param('datelastseen'); # last inventory date
50 my $branchcode = $input->param('branchcode') || '';
51 my $branch     = $input->param('branch');
52 my $op         = $input->param('op');
53 my $compareinv2barcd = $input->param('compareinv2barcd');
54 my $dont_checkin = $input->param('dont_checkin');
55
56 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
57     {   template_name   => "tools/inventory.tt",
58         query           => $input,
59         type            => "intranet",
60         authnotrequired => 0,
61         flagsrequired   => { tools => 'inventory' },
62         debug           => 1,
63     }
64 );
65
66 my @authorised_value_list;
67 my $authorisedvalue_categories = '';
68
69 my $frameworks = Koha::BiblioFrameworks->search({}, { order_by => ['frameworktext'] })->unblessed;
70 unshift @$frameworks, { frameworkcode => '' };
71
72 for my $fwk ( @$frameworks ){
73   my $fwkcode = $fwk->{frameworkcode};
74   my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => $fwkcode, kohafield => 'items.location', authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
75   my $authcode = $mss->count ? $mss->next->authorised_value : undef;
76     if ($authcode && $authorisedvalue_categories!~/\b$authcode\W/){
77       $authorisedvalue_categories.="$authcode ";
78       my $data=GetAuthorisedValues($authcode);
79       foreach my $value (@$data){
80         $value->{selected}=1 if ($value->{authorised_value} eq ($location));
81       }
82       push @authorised_value_list,@$data;
83     }
84 }
85
86 my $statuses = [];
87 my @notforloans;
88 for my $statfield (qw/items.notforloan items.itemlost items.withdrawn items.damaged/){
89     my $hash = {};
90     $hash->{fieldname} = $statfield;
91     my $mss = Koha::MarcSubfieldStructures->search({ frameworkcode => '', kohafield => $statfield, authorised_value => [ -and => {'!=' => undef }, {'!=' => ''}] });
92     $hash->{authcode} = $mss->count ? $mss->next->authorised_value : undef;
93     if ($hash->{authcode}){
94         my $arr = GetAuthorisedValues($hash->{authcode});
95         if ( $statfield eq 'items.notforloan') {
96             # Add notforloan == 0 to the list of possible notforloan statuses
97             # The lib value is replaced in the template
98             push @$arr, { authorised_value => 0, id => 'stat0' , lib => '__IGNORE__' } if ! grep { $_->{authorised_value} eq '0' } @$arr;
99             @notforloans = map { $_->{'authorised_value'} } @$arr;
100         }
101         $hash->{values} = $arr;
102         push @$statuses, $hash;
103     }
104 }
105
106 $template->param( statuses => $statuses );
107 my $staton = {}; #authorized values that are ticked
108 for my $authvfield (@$statuses) {
109     $staton->{$authvfield->{fieldname}} = [];
110     for my $authval (@{$authvfield->{values}}){
111         if ( defined $input->param('status-' . $authvfield->{fieldname} . '-' . $authval->{authorised_value}) && $input->param('status-' . $authvfield->{fieldname} . '-' . $authval->{authorised_value}) eq 'on' ){
112             push @{$staton->{$authvfield->{fieldname}}}, $authval->{authorised_value};
113         }
114     }
115 }
116
117 $template->param(
118     authorised_values        => \@authorised_value_list,
119     today                    => dt_from_string,
120     minlocation              => $minlocation,
121     maxlocation              => $maxlocation,
122     location                 => $location,
123     ignoreissued             => $ignoreissued,
124     branchcode               => $branchcode,
125     branch                   => $branch,
126     datelastseen             => $datelastseen,
127     compareinv2barcd         => $compareinv2barcd,
128     uploadedbarcodesflag     => $uploadbarcodes ? 1 : 0,
129 );
130
131 # Walk through uploaded barcodes, report errors, mark as seen, check in
132 my $results = {};
133 my @scanned_items;
134 my @errorloop;
135 my $moddatecount = 0;
136 if ( $uploadbarcodes && length($uploadbarcodes) > 0 ) {
137     my $dbh = C4::Context->dbh;
138     my $date = dt_from_string( scalar $input->param('setdate') );
139     $date = output_pref ( { dt => $date, dateformat => 'iso' } );
140
141     my $strsth  = "select * from issues, items where items.itemnumber=issues.itemnumber and items.barcode =?";
142     my $qonloan = $dbh->prepare($strsth);
143     $strsth="select * from items where items.barcode =? and items.withdrawn = 1";
144     my $qwithdrawn = $dbh->prepare($strsth);
145
146     my @barcodes;
147     my @uploadedbarcodes;
148
149     my $sth = $dbh->column_info(undef,undef,"items","barcode");
150     my $barcode_def = $sth->fetchall_hashref('COLUMN_NAME');
151     my $barcode_size = $barcode_def->{barcode}->{COLUMN_SIZE};
152     my $err_length=0;
153     my $err_data=0;
154     my $lines_read=0;
155     binmode($uploadbarcodes, ":encoding(UTF-8)");
156     while (my $barcode=<$uploadbarcodes>) {
157         push @uploadedbarcodes, grep { /\S/ } split( /[\n\r,;|-]/, $barcode );
158     }
159     for my $barcode (@uploadedbarcodes) {
160         next unless $barcode;
161         ++$lines_read;
162         if (length($barcode)>$barcode_size) {
163             $err_length += 1;
164         }
165         my $check_barcode = $barcode;
166         $check_barcode =~ s/\p{Print}//g;
167         if (length($check_barcode)>0) { # Only printable unicode characters allowed.
168             $err_data += 1;
169         }
170         next if length($barcode)>$barcode_size;
171         next if ( length($check_barcode)>0 );
172         push @barcodes,$barcode;
173     }
174     $template->param( LinesRead => $lines_read );
175     if (! @barcodes) {
176         push @errorloop, {'barcode'=>'No valid barcodes!'};
177         $op=''; # force the initial inventory screen again.
178     }
179     else {
180         $template->param( err_length => $err_length,
181                           err_data   => $err_data );
182     }
183     foreach my $barcode (@barcodes) {
184         if ( $qwithdrawn->execute($barcode) && $qwithdrawn->rows ) {
185             push @errorloop, { 'barcode' => $barcode, 'ERR_WTHDRAWN' => 1 };
186         } else {
187             my $item = GetItem( '', $barcode );
188             if ( defined $item && $item->{'itemnumber'} ) {
189                 # Modify date last seen for scanned items, remove lost status
190                 ModItem( { itemlost => 0, datelastseen => $date }, undef, $item->{'itemnumber'} );
191                 $moddatecount++;
192                 # update item hash accordingly
193                 $item->{itemlost} = 0;
194                 $item->{datelastseen} = $date;
195                 unless ( $dont_checkin ) {
196                     $qonloan->execute($barcode);
197                     if ($qonloan->rows){
198                         my $data = $qonloan->fetchrow_hashref;
199                         my ($doreturn, $messages, $iteminformation, $borrower) =AddReturn($barcode, $data->{homebranch});
200                         if( $doreturn ) {
201                             $item->{onloan} = undef;
202                             $item->{datelastseen} = dt_from_string;
203                         } else {
204                             push @errorloop, { barcode => $barcode, ERR_ONLOAN_NOT_RET => 1 };
205                         }
206                     }
207                 }
208                 push @scanned_items, $item;
209             } else {
210                 push @errorloop, { barcode => $barcode, ERR_BARCODE => 1 };
211             }
212         }
213     }
214     $template->param( date => $date );
215     $template->param( errorloop => \@errorloop ) if (@errorloop);
216 }
217
218 # Build inventorylist: used as result list when you do not pass barcodes
219 # This list is also used when you want to compare with barcodes
220 my ( $inventorylist, $rightplacelist );
221 if ( $op && ( !$uploadbarcodes || $compareinv2barcd )) {
222     ( $inventorylist ) = GetItemsForInventory({
223       minlocation  => $minlocation,
224       maxlocation  => $maxlocation,
225       location     => $location,
226       ignoreissued => $ignoreissued,
227       datelastseen => $datelastseen,
228       branchcode   => $branchcode,
229       branch       => $branch,
230       offset       => 0,
231       statushash   => $staton,
232     });
233 }
234 # Build rightplacelist used to check if a scanned item is in the right place.
235 if( @scanned_items ) {
236     ( $rightplacelist ) = GetItemsForInventory({
237       minlocation  => $minlocation,
238       maxlocation  => $maxlocation,
239       location     => $location,
240       ignoreissued => undef,
241       datelastseen => undef,
242       branchcode   => $branchcode,
243       branch       => $branch,
244       offset       => 0,
245       statushash   => undef,
246     });
247     # Convert the structure to a hash on barcode
248     $rightplacelist = {
249         map { $_->{barcode} ? ( $_->{barcode}, $_ ) : (); } @$rightplacelist
250     };
251 }
252
253 # Report scanned items that are on the wrong place, or have a wrong notforloan
254 # status, or are still checked out.
255 foreach my $item ( @scanned_items ) {
256     $item->{notforloancode} = $item->{notforloan}; # save for later use
257     my $fc = $item->{'frameworkcode'} || '';
258
259     # Populating with authorised values description
260     foreach my $field (qw/ location notforloan itemlost damaged withdrawn /) {
261         my $av = Koha::AuthorisedValues->get_description_by_koha_field(
262             { frameworkcode => $fc, kohafield => "items.$field", authorised_value => $item->{$field} } );
263         if ( $av and defined $item->{$field} and defined $av->{lib} ) {
264             $item->{$field} = $av->{lib};
265         }
266     }
267
268     # If we have scanned items with a non-matching notforloan value
269     if( none { $item->{'notforloancode'} eq $_ } @notforloans ) {
270         $item->{problems}->{changestatus} = 1;
271         additemtoresults( $item, $results );
272     }
273
274     # Report an item that is checked out (unusual!) or wrongly placed
275     if( $item->{onloan} ) {
276         $item->{problems}->{checkedout} = 1;
277         additemtoresults( $item, $results );
278         next; # do not modify item
279     } elsif( !exists $rightplacelist->{ $item->{barcode} } ) {
280         $item->{problems}->{wrongplace} = 1;
281         additemtoresults( $item, $results );
282     }
283 }
284
285 # Compare barcodes with inventory list, report no_barcode and not_scanned.
286 # not_scanned can be interpreted as missing
287 if ( $compareinv2barcd ) {
288     my @scanned_barcodes = map {$_->{barcode}} @scanned_items;
289     for my $item ( @$inventorylist ) {
290         my $barcode = $item->{barcode};
291         if( !$barcode ) {
292             $item->{problems}->{no_barcode} = 1;
293         } elsif ( grep /^$barcode$/, @scanned_barcodes ) {
294             next;
295         } else {
296             $item->{problems}->{not_scanned} = 1;
297         }
298         additemtoresults( $item, $results );
299     }
300 }
301
302 # Construct final results, add biblio information
303 my $loop = $uploadbarcodes
304     ? [ map { $results->{$_} } keys %$results ]
305     : $inventorylist // [];
306 for my $item ( @$loop ) {
307     my $biblio = Koha::Biblios->find( $item->{biblionumber} );
308     $item->{title} = $biblio->title;
309     $item->{author} = $biblio->author;
310 }
311
312 $template->param(
313     moddatecount => $moddatecount,
314     loop         => $loop,
315     op           => $op,
316 );
317
318 # Export to csv
319 if (defined $input->param('CSVexport') && $input->param('CSVexport') eq 'on'){
320     eval {use Text::CSV};
321     my $csv = Text::CSV->new or
322             die Text::CSV->error_diag ();
323     binmode STDOUT, ":encoding(UTF-8)";
324     print $input->header(
325         -type       => 'text/csv',
326         -attachment => 'inventory.csv',
327     );
328
329     my $columns_def_hashref = C4::Reports::Guided::_get_column_defs($input);
330     foreach my $key ( keys %$columns_def_hashref ) {
331         my $initkey = $key;
332         $key =~ s/[^\.]*\.//;
333         $columns_def_hashref->{$initkey}=NormalizeString($columns_def_hashref->{$initkey} // '');
334         $columns_def_hashref->{$key} = $columns_def_hashref->{$initkey};
335     }
336
337     my @translated_keys;
338     for my $key (qw / biblioitems.title    biblio.author
339                       items.barcode        items.itemnumber
340                       items.homebranch     items.location
341                       items.itemcallnumber items.notforloan
342                       items.itemlost       items.damaged
343                       items.withdrawn      items.stocknumber
344                       / ) {
345        push @translated_keys, $columns_def_hashref->{$key};
346     }
347     push @translated_keys, 'problem' if $uploadbarcodes;
348
349     $csv->combine(@translated_keys);
350     print $csv->string, "\n";
351
352     my @keys = qw/ title author barcode itemnumber homebranch location itemcallnumber notforloan itemlost damaged withdrawn stocknumber /;
353     for my $item ( @$loop ) {
354         my @line;
355         for my $key (@keys) {
356             push @line, $item->{$key};
357         }
358         my $errstr = '';
359         foreach my $key ( keys %{$item->{problems}} ) {
360             if( $key eq 'wrongplace' ) {
361                 $errstr .= "wrong place,";
362             } elsif( $key eq 'changestatus' ) {
363                 $errstr .= "unknown notforloan status,";
364             } elsif( $key eq 'not_scanned' ) {
365                 $errstr .= "missing,";
366             } elsif( $key eq 'no_barcode' ) {
367                 $errstr .= "no barcode,";
368             } elsif( $key eq 'checkedout' ) {
369                 $errstr .= "checked out,";
370             }
371         }
372         $errstr =~ s/,$//;
373         push @line, $errstr;
374         $csv->combine(@line);
375         print $csv->string, "\n";
376     }
377     # Adding not found barcodes
378     foreach my $error (@errorloop) {
379         my @line;
380         if ($error->{'ERR_BARCODE'}) {
381             push @line, map { $_ eq 'barcode' ? $error->{'barcode'} : ''} @keys;
382             push @line, "barcode not found";
383             $csv->combine(@line);
384             print $csv->string, "\n";
385         }
386     }
387     exit;
388 }
389
390 output_html_with_http_headers $input, $cookie, $template->output;
391
392 sub additemtoresults {
393     my ( $item, $results ) = @_;
394     my $itemno = $item->{itemnumber};
395     # since the script appends to $item, we can just overwrite the hash entry
396     $results->{$itemno} = $item;
397 }