From 317869be499f862bae0e21e4b618b638369ed323 Mon Sep 17 00:00:00 2001 From: Matthias Meusburger Date: Thu, 23 Aug 2012 16:02:52 +0200 Subject: [PATCH] Bug 7684: multiple fixes for inventory * when a file was uploaded and the comparison with catalogue range requested, the comparison was wrong: the logic was wrong * items that were not supposed to be scanned (ie: supposed to be on another shelf) didn't had the author and title, it was hard to retrieve them on the shelved * some useful fields were missing, like homebranch, location, status * the CSV export contained all the item information. It should contain the same informations as the screen Behaviour now: * scan a list of barcode & select a range of location * if a barcode has been scanned and should not be (mis placed item), the information is displayed * if you choose "compare barcodes list to result option", the resulting list contains all items that have been scanned and those that were supposed to be. Any item not in both list appears with a specific message on the last column Signed-off-by: Leila --- C4/Items.pm | 39 ++- C4/Templates.pm | 2 +- koha-tmpl/intranet-tmpl/prog/en/columns.def | 1 + .../prog/en/modules/tools/inventory.tt | 242 +++++++++-------- tools/ajax-inventory.pl | 43 +++ tools/inventory.pl | 284 ++++++++++++++------ 6 files changed, 407 insertions(+), 204 deletions(-) create mode 100755 tools/ajax-inventory.pl diff --git a/C4/Items.pm b/C4/Items.pm index 4a98712..78b34db 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -974,9 +974,7 @@ sub GetLostItems { =head2 GetItemsForInventory - $itemlist = GetItemsForInventory($minlocation, $maxlocation, - $location, $itemtype $datelastseen, $branch, - $offset, $size, $statushash); +($itemlist, $iTotalRecords) = GetItemsForInventory($minlocation, $maxlocation, $location, $itemtype, $ignoreissued, $datelastseen, $branchcode, $offset, $size, $statushash); Retrieve a list of title/authors/barcode/callnumber, for biblio inventory. @@ -989,6 +987,8 @@ the datelastseen can be used to specify that you want to see items not seen sinc offset & size can be used to retrieve only a part of the whole listing (defaut behaviour) $statushash requires a hashref that has the authorized values fieldname (intems.notforloan, etc...) as keys, and an arrayref of statuscodes we are searching for as values. +$iTotalRecords is the number of rows that would have been returned without the $offset, $size limit clause + =cut sub GetItemsForInventory { @@ -997,7 +997,7 @@ sub GetItemsForInventory { my ( @bind_params, @where_strings ); my $query = <<'END_SQL'; -SELECT items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, datelastseen +SELECT SQL_CALC_FOUND_ROWS items.itemnumber, barcode, itemcallnumber, title, author, biblio.biblionumber, biblio.frameworkcode, datelastseen, homebranch, location, notforloan, damaged, itemlost, stocknumber FROM items LEFT JOIN biblio ON items.biblionumber = biblio.biblionumber LEFT JOIN biblioitems on items.biblionumber = biblioitems.biblionumber @@ -1040,7 +1040,7 @@ END_SQL } push @bind_params, $branchcode; } - + if ( $itemtype ) { push @where_strings, 'biblioitems.itemtype = ?'; push @bind_params, $itemtype; @@ -1056,20 +1056,33 @@ END_SQL $query .= join ' AND ', @where_strings; } $query .= ' ORDER BY items.cn_sort, itemcallnumber, title'; + $query .= " LIMIT $offset, $size" if ($offset and $size); my $sth = $dbh->prepare($query); $sth->execute( @bind_params ); my @results; - $size--; - while ( my $row = $sth->fetchrow_hashref ) { - $offset-- if ($offset); - $row->{datelastseen}=format_date($row->{datelastseen}); - if ( ( !$offset ) && $size ) { - push @results, $row; - $size--; + my $tmpresults = $sth->fetchall_arrayref({}); + $sth = $dbh->prepare("SELECT FOUND_ROWS()"); + $sth->execute(); + my ($iTotalRecords) = $sth->fetchrow_array(); + + foreach my $row (@$tmpresults) { + $row->{datelastseen} = format_date( $row->{datelastseen} ); + + # Auth values + foreach (keys %$row) { + # If the koha field is mapped to a marc field + my ($f, $sf) = GetMarcFromKohaField("items.$_", $row->{'frameworkcode'}); + if ($f and $sf) { + # We replace the code with it's description + my $authvals = C4::Koha::GetKohaAuthorisedValuesFromField($f, $sf, $row->{'frameworkcode'}); + $row->{$_} = $authvals->{$row->{$_}} if defined $authvals->{$row->{$_}}; + } } + push @results, $row; } - return \@results; + + return (\@results, $iTotalRecords); } =head2 GetItemsCount diff --git a/C4/Templates.pm b/C4/Templates.pm index 21604fc..44457c9 100644 --- a/C4/Templates.pm +++ b/C4/Templates.pm @@ -328,7 +328,7 @@ sub getlanguage { my $lang; # cookie - if ( $query->cookie('KohaOpacLanguage') ) { + if ($query and $query->cookie('KohaOpacLanguage') ) { $lang = $query->cookie('KohaOpacLanguage'); $lang =~ s/[^a-zA-Z_-]*//; # sanitize cookie } diff --git a/koha-tmpl/intranet-tmpl/prog/en/columns.def b/koha-tmpl/intranet-tmpl/prog/en/columns.def index c48fa48..dc863e4 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/columns.def +++ b/koha-tmpl/intranet-tmpl/prog/en/columns.def @@ -138,3 +138,4 @@ biblioitems.place Place of publication biblioitems.lccn LCCN biblioitems.marcxml MARC blob biblioitems.url URL +biblioitems.title Title diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/inventory.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/inventory.tt index 424928f..1052714 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/inventory.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/inventory.tt @@ -1,30 +1,72 @@ [% INCLUDE 'doc-head-open.inc' %] Koha › Tools › Inventory [% INCLUDE 'doc-head-close.inc' %] + + + [% INCLUDE 'calendar.inc' %] @@ -32,34 +74,33 @@ $(document).ready(function(){ [% INCLUDE 'header.inc' %] [% INCLUDE 'cat-search.inc' %] - +
- +
-
-
+
+

Inventory/Stocktaking

- [% IF ( Number ) %]
[% Number %] items modified : datelastseen set to [% date %]
[% END %] - [% IF ( errorfile ) %]
[% errorfile %] can't be opened
[% END %] - [% FOREACH errorloo IN errorloop %] + [% IF (moddatecount) %]
[% moddatecount %] items modified : datelastseen set to [% date %]
[% END %] + [% IF (errorfile) %]
[% errorfile %] can't be opened
[% END %] + [% FOREACH error IN errorloop %]
- [% errorloo.barcode %] - [% IF ( errorloo.ERR_BARCODE ) %]: barcode not found[% END %] - [% IF ( errorloo.ERR_WTHDRAWN ) %]: item withdrawn[% END %] - [% IF ( errorloo.ERR_ONLOAN_RET ) %]: item was on loan. It was returned before marked as seen[% END %] - [% IF ( errorloo.ERR_ONLOAN_NOT_RET ) %]: item was on loan. couldn't be returned.[% END %] + [% error.barcode %] + [% IF (error.ERR_BARCODE) %]: barcode not found[% END %] + [% IF (error.ERR_WTHDRAWN) %]: item withdrawn[% END %] + [% IF (error.ERR_ONLOAN_RET) %]: item was on loan. It was returned before marked as seen[% END %] + [% IF (error.ERR_ONLOAN_NOT_RET) %]: item was on loan. couldn't be returned.[% END %]
[% END %] - [% UNLESS ( loop ) %] -
+ [% UNLESS op %] +
Use a barcode file -
    +
    1. -
    2. -
      [% INCLUDE 'date-format.inc' %]
      +
@@ -81,43 +122,43 @@ $(document).ready(function(){ [% END %] - [% IF ( authorised_values ) %] + [% IF (authorised_values) %]
  • [% END %]
  • - + (items.itemcallnumber)
  • - [% IF ( statuses ) %] - - + [% IF (statuses) %] + +
    Item statuses
    - [% FOREACH statuse IN statuses %] - [% IF ( statuse.values ) %] + [% FOREACH status IN statuses %] + [% IF (status.values) %]
    - [% statuse.fieldname %] -
      - [% FOREACH value IN statuse.values %] - [% IF ( value.lib ) %]
    • + [% status.fieldname %] +
        + [% FOREACH value IN status.values %] + [% IF (value.lib) %]
      • - +
      • [% END %] [% END %]
      @@ -126,25 +167,19 @@ $(document).ready(function(){ [% END %]
    -
    +
      [% END %]
    1. - -
      [% INCLUDE 'date-format.inc' %]
      +
    2. - [% IF ( ignoreissued ) %] + [% IF (ignoreissued) %]
    3. [% ELSE %] [% END %] -
    4. - items
    5. -
    6. - -
    7. @@ -158,10 +193,10 @@ $(document).ready(function(){
    8. -
    -
    - [% END %] - [% IF ( loop ) %] +
    +
    + [% END %] + [% IF (op) %]
    @@ -171,74 +206,65 @@ $(document).ready(function(){ - - + +
    + + + + + - [% FOREACH loo IN loop %] + + + [% FOREACH result IN loop %] + + + + [% END %] +
    Seen BarcodeLocation TitleStatusLostDamaged Unseen since Problems
    - + - [% loo.barcode %] + [% result.barcode | html %] -

    [% loo.itemcallnumber %] - [% loo.title |html %]

    -

    [% loo.author %]

    + [% result.homebranch | html %] [% result.location | html %] [[% result.itemcallnumber | html %]]
    -

    [% loo.datelastseen %]

    +

    [% result.title | html %]

    [% result.author | html %]

    - [% IF ( loo.notfoundbarcode ) %] -

    Not found among barcodes in barcodes file.

    - [% ELSIF ( loo.notfoundkoha ) %] -

    Not found in Koha.

    - [% ELSE %] -

    None

    - [% END %] + [% result.notforloan | html %] +
    + [% result.itemlost | html %] + + [% result.damaged | html %] + + [% result.datelastseen | html %] + + [% IF (result.wrongplace) %]

    Item should not have been scanned

    [% ELSIF (result.missingitem) %]

    Item missing

    [% ELSIF (result.changestatus) %]

    Change item status

    [% END %]
    - - -
    - [% IF ( offset ) %] -
    - - - - - - - - - - -
    - [% END %] - [% IF ( nextoffset ) %] -
    - - - - - - - - - - +
    +

    + + + +
    +
    - [% END %] + [% END %]
    diff --git a/tools/ajax-inventory.pl b/tools/ajax-inventory.pl new file mode 100755 index 0000000..24739b9 --- /dev/null +++ b/tools/ajax-inventory.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl +# + +use Modern::Perl; + +use CGI; +use JSON; + +use C4::Auth; +use C4::Circulation qw/CanBookBeRenewed/; +use C4::Context; +use C4::Koha qw/getitemtypeimagelocation/; +use C4::Reserves qw/CheckReserves/; +use C4::Utils::DataTables; +use C4::Output; +use C4::Biblio; +use C4::Items; +use C4::Dates qw/format_date format_date_in_iso/; +use C4::Koha; +use C4::Branch; # GetBranches +use C4::Reports::Guided; #_get_column_defs +use C4::Charset; +use List::MoreUtils qw /none/; + + +my $input = new CGI; + +# Authentication +my ($status, $cookie, $sessionId) = C4::Auth::check_api_auth($input, { tools => 'inventory' }); +exit unless ($status eq "ok"); + + +my $seen = $input->param('seen'); +my @seent = split(/\|/, $seen); + +# mark seen if applicable (ie: coming form mark seen checkboxes) +foreach ( @seent ) { + /SEEN-(.+)/ and &ModDateLastSeen($1); +} + + +print $input->header('application/json'); + diff --git a/tools/inventory.pl b/tools/inventory.pl index 824906b..78c17c3 100755 --- a/tools/inventory.pl +++ b/tools/inventory.pl @@ -35,6 +35,10 @@ use C4::Dates qw/format_date format_date_in_iso/; use C4::Koha; use C4::Branch; # GetBranches use C4::Circulation; +use C4::Reports::Guided; #_get_column_defs +use C4::Charset; +use List::MoreUtils qw/none/; + my $minlocation=$input->param('minlocation') || ''; my $maxlocation=$input->param('maxlocation'); @@ -50,26 +54,27 @@ my $pagesize = $input->param('pagesize'); $pagesize=50 unless $pagesize; my $branchcode = $input->param('branchcode') || ''; my $branch = $input->param('branch'); -my $op = $input->param('op'); -my $res; #contains the results loop -# warn "uploadbarcodes : ".$uploadbarcodes; -# use Data::Dumper; warn Dumper($input); - -my ($template, $borrowernumber, $cookie) - = get_template_and_user({template_name => "tools/inventory.tmpl", - query => $input, - type => "intranet", - authnotrequired => 0, - flagsrequired => {tools => 'inventory'}, - debug => 1, - }); +my $op = $input->param('op'); +my $compareinv2barcd = $input->param('compareinv2barcd'); +my $res; #contains the results loop + +my ( $template, $borrowernumber, $cookie ) = get_template_and_user( + { template_name => "tools/inventory.tmpl", + query => $input, + type => "intranet", + authnotrequired => 0, + flagsrequired => { tools => 'inventory' }, + debug => 1, + } +); + my $branches = GetBranches(); my @branch_loop; for my $branch_hash (keys %$branches) { - push @branch_loop, {value => "$branch_hash", - branchname => $branches->{$branch_hash}->{'branchname'}, - selected => ($branch_hash eq $branchcode?1:0)}; + push @branch_loop, {value => "$branch_hash", + branchname => $branches->{$branch_hash}->{'branchname'}, + selected => ($branch_hash eq $branchcode?1:0)}; } @branch_loop = sort {$a->{branchname} cmp $b->{branchname}} @branch_loop; @@ -87,7 +92,7 @@ for my $fwk (keys %$frameworks){ my $data=GetAuthorisedValues($authcode); foreach my $value (@$data){ $value->{selected}=1 if ($value->{authorised_value} eq ($location)); - } + } push @authorised_value_list,@$data; } } @@ -103,58 +108,79 @@ for my $statfield (qw/items.notforloan items.itemlost items.withdrawn items.dama push @$statuses, $hash; } } + + $template->param( statuses => $statuses ); -my $staton = {}; #authorized values that are ticked +my $staton = {}; #authorized values that are ticked for my $authvfield (@$statuses) { $staton->{$authvfield->{fieldname}} = []; for my $authval (@{$authvfield->{values}}){ - if ( defined $input->param('status-' . $authvfield->{fieldname} . '-' . $authval->{id}) && $input->param('status-' . $authvfield->{fieldname} . '-' . $authval->{id}) eq 'on' ){ - push @{$staton->{$authvfield->{fieldname}}}, $authval->{id}; + if ( defined $input->param('status-' . $authvfield->{fieldname} . '-' . $authval->{authorised_value}) && $input->param('status-' . $authvfield->{fieldname} . '-' . $authval->{authorised_value}) eq 'on' ){ + push @{$staton->{$authvfield->{fieldname}}}, $authval->{authorised_value}; } } } +my $notforloanlist; my $statussth = ''; for my $authvfield (@$statuses) { if ( scalar @{$staton->{$authvfield->{fieldname}}} > 0 ){ my $joinedvals = join ',', @{$staton->{$authvfield->{fieldname}}}; $statussth .= "$authvfield->{fieldname} in ($joinedvals) and "; + $notforloanlist = $joinedvals if ($authvfield->{fieldname} eq "items.notforloan"); } } $statussth =~ s, and $,,g; - -$template->param(branchloop => \@branch_loop, - authorised_values=>\@authorised_value_list, - today => C4::Dates->today(), - minlocation => $minlocation, - maxlocation => $maxlocation, - location=>$location, - ignoreissued=>$ignoreissued, - branchcode=>$branchcode, - branch => $branch, - offset => $offset, - pagesize => $pagesize, - datelastseen => $datelastseen, - ); +$template->param( + branchloop => \@branch_loop, + authorised_values => \@authorised_value_list, + today => C4::Dates->today(), + minlocation => $minlocation, + maxlocation => $maxlocation, + location => $location, + ignoreissued => $ignoreissued, + branchcode => $branchcode, + branch => $branch, + offset => $offset, + pagesize => $pagesize, + datelastseen => $datelastseen, + compareinv2barcd => $compareinv2barcd, + notforloanlist => $notforloanlist +); + +my @notforloans; +if (defined $notforloanlist) { + @notforloans = split(/,/, $notforloanlist); +} + + + my @brcditems; -if ($uploadbarcodes && length($uploadbarcodes)>0){ - my $dbh=C4::Context->dbh; - my $date = format_date_in_iso($input->param('setdate')) || C4::Dates->today('iso'); -# warn "$date"; - my $strsth="select * from issues, items where items.itemnumber=issues.itemnumber and items.barcode =?"; +my $barcodelist; +my @errorloop; +if ( $uploadbarcodes && length($uploadbarcodes) > 0 ) { + my $dbh = C4::Context->dbh; + my $date = format_date_in_iso( $input->param('setdate') ) || C4::Dates->today('iso'); + + my $strsth = "select * from issues, items where items.itemnumber=issues.itemnumber and items.barcode =?"; my $qonloan = $dbh->prepare($strsth); $strsth="select * from items where items.barcode =? and items.withdrawn = 1"; my $qwithdrawn = $dbh->prepare($strsth); - my @errorloop; - my $count=0; - while (my $barcode=<$uploadbarcodes>){ + + my $count = 0; + # This allows us to handle files where lines are delimited by \r, \n, or \r\n. + my $file_contents = read_file( $uploadbarcodes ); + my @barcodes = split( /\r\n|\n|\r/, $file_contents ); + + for my $barcode ( @barcodes ){ $barcode =~ s/\r?\n$//; - if ($qwithdrawn->execute($barcode) &&$qwithdrawn->rows){ - push @errorloop, {'barcode'=>$barcode,'ERR_WTHDRAWN'=>1}; - }else{ - my $item = GetItem('', $barcode); - if (defined $item && $item->{'itemnumber'}){ - ModItem({ datelastseen => $date }, undef, $item->{'itemnumber'}); + $barcodelist .= ($barcodelist) ? '|' . $barcode : $barcode; + if ( $qwithdrawn->execute($barcode) && $qwithdrawn->rows ) { + push @errorloop, { 'barcode' => $barcode, 'ERR_WTHDRAWN' => 1 }; + } else { + my $item = GetItem( '', $barcode ); + if ( defined $item && $item->{'itemnumber'} ) { + ModItem( { datelastseen => $date }, undef, $item->{'itemnumber'} ); push @brcditems, $item; $count++; $qonloan->execute($barcode); @@ -171,48 +197,101 @@ if ($uploadbarcodes && length($uploadbarcodes)>0){ push @errorloop, {'barcode'=>$barcode,'ERR_BARCODE'=>1}; } } + } $qonloan->finish; $qwithdrawn->finish; - $template->param(date=>format_date($date),Number=>$count); -# $template->param(errorfile=>$errorfile) if ($errorfile); - $template->param(errorloop=>\@errorloop) if (@errorloop); + $template->param( date => format_date($date), Number => $count ); + $template->param( errorloop => \@errorloop ) if (@errorloop); + } -#if we want to compare the results to a list of barcodes, or we have no barcode file -if ( ! ($uploadbarcodes && length($uploadbarcodes)>0 ) || ( $input->param('compareinv2barcd') eq 'on' && length($uploadbarcodes)>0) ) { - if ($markseen) { - foreach ($input->param) { - /SEEN-(.+)/ and &ModDateLastSeen($1); - } +$template->param(barcodelist => $barcodelist); + +# now build the result list: inventoried items if requested, and mis-placed items -always- +my $inventorylist; +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); + + # Real copy + my @res_copy; + foreach (@$inventorylist) { + push @res_copy, $_; } - if ($markseen or $op) { - $res = GetItemsForInventory( $minlocation, $maxlocation, $location, $itemtype, $ignoreissued, $datelastseen, $branchcode, $branch, $offset, $pagesize, $staton ); - $template->param(loop =>$res, - nextoffset => ($offset+$pagesize), - prevoffset => ($offset?$offset-$pagesize:0), - ); - } - if ( defined $input->param('compareinv2barcd') && ( ( $input->param('compareinv2barcd') eq 'on' ) && ( scalar @brcditems != scalar @$res ) ) && length($uploadbarcodes) > 0 ){ - if ( scalar @brcditems > scalar @$res ){ - for my $brcditem (@brcditems) { - if (! grep( $_->{barcode} =~ /$brcditem->{barcode}/ , @$res) ){ - $brcditem->{notfoundkoha} = 1; - push @$res, $brcditem; - } - } - } else { - my @notfound; - for my $item (@$res) { - if ( ! grep( $_->{barcode} =~ /$item->{barcode}/ , @brcditems) ){ - $item->{notfoundbarcode} = 1; - push @notfound, $item; - } + $res = \@res_copy; +} + +# set "missing" flags for all items with a datelastseen before the choosen datelastseen +foreach (@$res) { $_->{missingitem}=1 if C4::Dates->new($_->{datelastseen})->output('iso') lt C4::Dates->new($datelastseen)->output('iso'); } + +# removing missing items from loop if "Compare barcodes list to results" has not been checked +@$res = grep {!$_->{missingitem} == 1 } @$res if (!$input->param('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 +foreach my $temp (@brcditems) { + + # Saving notforloan code before it's replaced by it's authorised value for later comparison + $temp->{'notforloancode'} = $temp->{'notforloan'}; + + # Populating with authorised values + foreach (keys %$temp) { + # If the koha field is mapped to a marc field + my $fc = $temp->{'frameworkcode'} || ''; + my ($f, $sf) = GetMarcFromKohaField("items.$_", $fc); + if ($f and $sf) { + # We replace the code with it's description + my $authvals = C4::Koha::GetKohaAuthorisedValuesFromField($f, $sf, $fc); + if ($authvals and defined $temp->{$_} and defined $authvals->{$temp->{$_}}) { + $temp->{$_} = $authvals->{$temp->{$_}}; } - $res = [@$res, @notfound]; } } + + next if $temp->{onloan}; # skip checked out items + + # If we have scanned items with a non-matching notforloan value + if (none { $temp->{'notforloancode'} eq $_ } @notforloans) { + $temp->{'changestatus'} = 1; + my $biblio = C4::Biblio::GetBiblioData($temp->{biblionumber}); + $temp->{title} = $biblio->{title}; + $temp->{author} = $biblio->{author}; + $temp->{datelastseen} = format_date($temp->{datelastseen}); + push @$res, $temp; + + } + if (none { $temp->{barcode} eq $_->{barcode} && !$_->{'onloan'} } @$inventorylist) { + my $temp2 = { %$temp }; + $temp2->{wrongplace}=1; + my $biblio = C4::Biblio::GetBiblioData($temp->{biblionumber}); + $temp2->{title} = $biblio->{title}; + $temp2->{author} = $biblio->{author}; + $temp2->{datelastseen} = format_date($temp->{datelastseen}); + push @$res, $temp2; + } +} + +# Finally, modifying datelastseen for remaining items +my $moddatecount = 0; +foreach (@$res) { + unless ($_->{'missingitem'}) { + ModDateLastSeen($_->{'itemnumber'}); + $moddatecount++; + } } +# Removing items that don't have any problems from loop +@$res = grep { $_->{missingitem} || $_->{wrongplace} || $_->{changestatus} } @$res; + +$template->param( + moddatecount => $moddatecount, + loop => $res, + nextoffset => ( $offset + $pagesize ), + prevoffset => ( $offset ? $offset - $pagesize : 0 ), + op => $op +); + if (defined $input->param('CSVexport') && $input->param('CSVexport') eq 'on'){ eval {use Text::CSV}; my $csv = Text::CSV->new or @@ -221,14 +300,55 @@ if (defined $input->param('CSVexport') && $input->param('CSVexport') eq 'on'){ -type => 'text/csv', -attachment => 'inventory.csv', ); - for my $re (@$res){ + + my $columns_def_hashref = C4::Reports::Guided::_get_column_defs(); + foreach my $key ( keys %$columns_def_hashref ) { + my $initkey = $key; + $key =~ s/[^\.]*\.//; + $columns_def_hashref->{$initkey}=NormalizeString($columns_def_hashref->{$initkey}); + $columns_def_hashref->{$key} = $columns_def_hashref->{$initkey}; + } + + my @translated_keys; + for my $key (qw / biblioitems.title biblio.author + items.barcode items.itemnumber + items.homebranch items.location + items.itemcallnumber items.notforloan + items.itemlost items.damaged + items.stocknumber + / ) { + push @translated_keys, $columns_def_hashref->{$key}; + } + + $csv->combine(@translated_keys); + print $csv->string, "\n"; + + my @keys = qw / title author barcode itemnumber homebranch location itemcallnumber notforloan lost damaged stocknumber /; + for my $re (@$res) { my @line; - for my $key (keys %$re) { + for my $key (@keys) { push @line, $re->{$key}; } + if ($re->{wrongplace}) { + push @line, "wrong place"; + } elsif ($re->{missingitem}) { + push @line, "missing item"; + } elsif ($re->{changestatus}) { + push @line, "change item status"; + } + $csv->combine(@line); + print $csv->string, "\n"; + } + # Adding not found barcodes + foreach my $error (@errorloop) { + my @line; + if ($error->{'ERR_BARCODE'}) { + push @line, map { $_ eq 'barcode' ? $error->{'barcode'} : ''} @keys; + push @line, "barcode not found"; $csv->combine(@line); print $csv->string, "\n"; } + } exit; } -- 1.7.10.4