Bug 24879: Adjust tests
[koha.git] / cataloguing / additem.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2004-2010 BibLibre
5 # Parts Copyright Catalyst IT 2011
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23
24 use CGI qw ( -utf8 );
25 use C4::Auth qw( get_template_and_user haspermission );
26 use C4::Output qw( output_and_exit_if_error output_and_exit output_html_with_http_headers );
27 use C4::Biblio qw(
28     GetFrameworkCode
29     GetMarcBiblio
30     GetMarcFromKohaField
31     GetMarcStructure
32     IsMarcStructureInternal
33     ModBiblio
34 );
35 use C4::Context;
36 use C4::Circulation qw( barcodedecode LostItem );
37 use C4::Barcodes;
38 use C4::Barcodes::ValueBuilder;
39 use Koha::DateUtils qw( dt_from_string );
40 use Koha::Items;
41 use Koha::ItemTypes;
42 use Koha::Libraries;
43 use Koha::Patrons;
44 use Koha::SearchEngine::Indexer;
45 use C4::Search qw( enabled_staff_search_views );
46 use Storable qw( freeze thaw );
47 use URI::Escape qw( uri_escape_utf8 );
48 use C4::Members;
49 use Koha::UI::Form::Builder::Item;
50 use Koha::Result::Boolean;
51
52 use MARC::File::XML;
53 use URI::Escape qw( uri_escape_utf8 );
54 use Encode qw( encode_utf8 );
55 use MIME::Base64 qw( decode_base64url encode_base64url );
56 use List::Util qw( first );
57 use List::MoreUtils qw( any uniq );
58
59 our $dbh = C4::Context->dbh;
60
61 sub get_item_from_cookie {
62     my ( $input ) = @_;
63
64     my $item_from_cookie;
65     my $lastitemcookie = $input->cookie('LastCreatedItem');
66     if ($lastitemcookie) {
67         $lastitemcookie = decode_base64url($lastitemcookie);
68         eval {
69             if ( thaw($lastitemcookie) ) {
70                 $item_from_cookie = thaw($lastitemcookie);
71             }
72         };
73         if ($@) {
74             $lastitemcookie ||= 'undef';
75             warn "Storable::thaw failed to thaw LastCreatedItem-cookie. Cookie value '".encode_base64url($lastitemcookie)."'. Caught error follows: '$@'";
76         }
77     }
78     return $item_from_cookie;
79 }
80
81 my $input        = CGI->new;
82
83 my $biblionumber;
84 my $itemnumber;
85 if( $input->param('itemnumber') && !$input->param('biblionumber') ){
86     $itemnumber = $input->param('itemnumber');
87     my $item = Koha::Items->find( $itemnumber );
88     $biblionumber = $item->biblionumber;
89 } else {
90     $biblionumber = $input->param('biblionumber');
91     $itemnumber = $input->param('itemnumber');
92 }
93
94 my $biblio = Koha::Biblios->find($biblionumber);
95
96 my $op           = $input->param('op') || q{};
97 my $hostitemnumber = $input->param('hostitemnumber');
98 my $marcflavour  = C4::Context->preference("marcflavour");
99 my $searchid     = $input->param('searchid');
100 # fast cataloguing datas
101 my $fa_circborrowernumber = $input->param('circborrowernumber');
102 my $fa_barcode            = $input->param('barcode');
103 my $fa_branch             = $input->param('branch');
104 my $fa_stickyduedate      = $input->param('stickyduedate');
105 my $fa_duedatespec        = $input->param('duedatespec');
106
107 our $frameworkcode = &GetFrameworkCode($biblionumber);
108
109 # Defining which userflag is needing according to the framework currently used
110 my $userflags;
111 if (defined $input->param('frameworkcode')) {
112     $userflags = ($input->param('frameworkcode') eq 'FA') ? "fast_cataloging" : "edit_items";
113 }
114
115 if (not defined $userflags) {
116     $userflags = ($frameworkcode eq 'FA') ? "fast_cataloging" : "edit_items";
117 }
118
119 my ($template, $loggedinuser, $cookie)
120     = get_template_and_user({template_name => "cataloguing/additem.tt",
121                  query => $input,
122                  type => "intranet",
123                  flagsrequired => {editcatalogue => $userflags},
124                  });
125
126
127 # Does the user have a restricted item editing permission?
128 my $uid = Koha::Patrons->find( $loggedinuser )->userid;
129 my $restrictededition = $uid ? haspermission($uid,  {'editcatalogue' => 'edit_items_restricted'}) : undef;
130 # In case user is a superlibrarian, editing is not restricted
131 $restrictededition = 0 if ($restrictededition != 0 &&  C4::Context->IsSuperLibrarian());
132 # In case user has fast cataloging permission (and we're in fast cataloging), editing is not restricted
133 $restrictededition = 0 if ($restrictededition != 0 && $frameworkcode eq 'FA' && haspermission($uid, {'editcatalogue' => 'fast_cataloging'}));
134
135 our $tagslib = &GetMarcStructure(1,$frameworkcode);
136 my $record = GetMarcBiblio({ biblionumber => $biblionumber });
137
138 output_and_exit_if_error( $input, $cookie, $template,
139     { module => 'cataloguing', record => $record } );
140
141 my $current_item;
142 my $nextop="additem";
143 my @errors; # store errors found while checking data BEFORE saving item.
144
145 # Getting last created item cookie
146 my $prefillitem = C4::Context->preference('PrefillItem');
147
148 #-------------------------------------------------------------------------------
149 if ($op eq "additem") {
150
151     my $add_submit                 = $input->param('add_submit');
152     my $add_duplicate_submit       = $input->param('add_duplicate_submit');
153     my $add_multiple_copies_submit = $input->param('add_multiple_copies_submit');
154     my $number_of_copies           = $input->param('number_of_copies');
155
156     my @columns = Koha::Items->columns;
157     my $item = Koha::Item->new;
158     $item->biblionumber($biblio->biblionumber);
159     for my $c ( @columns ) {
160         if ( $c eq 'more_subfields_xml' ) {
161             my @more_subfields_xml = $input->multi_param("items.more_subfields_xml");
162             my @unlinked_item_subfields;
163             for my $subfield ( @more_subfields_xml ) {
164                 my $v = $input->param('items.more_subfields_xml_' . $subfield);
165                 push @unlinked_item_subfields, $subfield, $v;
166             }
167             if ( @unlinked_item_subfields ) {
168                 my $marc = MARC::Record->new();
169                 # use of tag 999 is arbitrary, and doesn't need to match the item tag
170                 # used in the framework
171                 $marc->append_fields(MARC::Field->new('999', ' ', ' ', @unlinked_item_subfields));
172                 $marc->encoding("UTF-8");
173                 $item->more_subfields_xml($marc->as_xml("USMARC"));
174                 next;
175             }
176             $item->more_subfields_xml(undef);
177         } else {
178             my @v = grep { $_ ne "" }
179                 uniq $input->multi_param( "items." . $c );
180
181             next unless @v;
182
183             if ( $c eq 'permanent_location' ) { # See 27837
184                 $item->make_column_dirty('permanent_location');
185             }
186
187             $item->$c(join ' | ', @v);
188         }
189     }
190
191     # if autoBarcode is set to 'incremental', calculate barcode...
192     if ( ! defined $item->barcode && C4::Context->preference('autoBarcode') eq 'incremental' ) {
193         my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
194         $item->barcode($barcode);
195     }
196
197     $item->barcode(barcodedecode($item->barcode));
198
199     # If we have to add or add & duplicate, we add the item
200     if ( $add_submit || $add_duplicate_submit || $prefillitem) {
201
202         # check for item barcode # being unique
203         if ( defined $item->barcode
204             && Koha::Items->search( { barcode => $item->barcode } )->count )
205         {
206             # if barcode exists, don't create, but report The problem.
207             push @errors, "barcode_not_unique";
208
209             $current_item = $item->unblessed; # Restore edit form for the same item
210         }
211         else {
212             $item->store->discard_changes;
213
214             # This is a bit tricky : if there is a cookie for the last created item and
215             # we just added an item, the cookie value is not correct yet (it will be updated
216             # next page). To prevent the form from being filled with outdated values, we
217             # force the use of "add and duplicate" feature, so the form will be filled with
218             # correct values.
219
220             # Pushing the last created item cookie back
221             if ( $prefillitem ) {
222                 my $last_created_item_cookie = $input->cookie(
223                     -name => 'LastCreatedItem',
224                     # We encode_base64url the whole freezed structure so we're sure we won't have any encoding problems
225                     -value   => encode_base64url( freeze( { %{$item->unblessed}, itemnumber => undef } ) ),
226                     -HttpOnly => 1,
227                     -expires => '',
228                     -sameSite => 'Lax'
229                 );
230
231                 $cookie = [ $cookie, $last_created_item_cookie ];
232             }
233
234         }
235         $nextop = "additem";
236
237     }
238
239     # If we have to add & duplicate
240     if ($prefillitem || $add_duplicate_submit) {
241
242         $current_item = $item->unblessed;
243
244         if (C4::Context->preference('autoBarcode') eq 'incremental') {
245             my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
246             $current_item->{barcode} = $barcode;
247         }
248         else {
249             # we have to clear the barcode field in the duplicate item record to make way for the new one generated by the javascript plugin
250             $current_item->{barcode} = undef; # FIXME or delete?
251         }
252
253         # Don't use the "prefill" feature if we want to generate the form with all the info from this item
254         # It will remove subfields that are not in SubfieldsToUseWhenPrefill.
255         $prefillitem = 0 if $add_duplicate_submit;
256     }
257
258     # If we have to add multiple copies
259     if ($add_multiple_copies_submit) {
260
261         $current_item = $item->unblessed;
262
263         my $copynumber = $current_item->{copynumber};
264         my $oldbarcode = $current_item->{barcode};
265
266         # If there is a barcode and we can't find their new values, we can't add multiple copies
267         my $testbarcode;
268         my $barcodeobj = C4::Barcodes->new;
269         $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
270         if ( $oldbarcode && !$testbarcode ) {
271
272             push @errors, "no_next_barcode";
273
274         }
275         else {
276             # We add each item
277
278             # For the first iteration
279             my $barcodevalue = $oldbarcode;
280             my $exist_itemnumber;
281
282             for ( my $i = 0 ; $i < $number_of_copies ; ) {
283
284                 # If there is a barcode
285                 if ($barcodevalue) {
286
287 # Getting a new barcode (if it is not the first iteration or the barcode we tried already exists)
288                     $barcodevalue = $barcodeobj->next_value($oldbarcode)
289                       if ( $i > 0 || $exist_itemnumber );
290
291                     # Putting it into the record
292                     if ($barcodevalue) {
293                         if ( C4::Context->preference("autoBarcode") eq
294                             'hbyymmincr' && $i > 0 )
295                         { # The first copy already contains the homebranch prefix
296                              # This is terribly hacky but the easiest way to fix the way hbyymmincr is working
297                              # Contrary to what one might think, the barcode plugin does not prefix the returned string with the homebranch
298                              # For a single item, it is handled with some JS code (see cataloguing/value_builder/barcode.pl)
299                              # But when adding multiple copies we need to prefix it here,
300                              # so we retrieve the homebranch from the item and prefix the barcode with it.
301                             my $homebranch = $current_item->{homebranch};
302                             $barcodevalue = $homebranch . $barcodevalue;
303                         }
304                         $current_item->{barcode} = $barcodevalue;
305                     }
306
307                     # Checking if the barcode already exists
308                     $exist_itemnumber = Koha::Items->search({ barcode => $barcodevalue })->count;
309                 }
310
311                 # Updating record with the new copynumber
312                 if ($copynumber) {
313                     $current_item->{copynumber} = $copynumber;
314                 }
315
316                 # Adding the item
317                 if ( !$exist_itemnumber ) {
318                     delete $current_item->{itemnumber};
319                     $current_item = Koha::Item->new($current_item)->store(
320                         { skip_record_index => 1 } );
321                     $current_item->discard_changes; # Cannot chain discard_changes
322                     $current_item = $current_item->unblessed;
323
324 # We count the item only if it was really added
325 # That way, all items are added, even if there was some already existing barcodes
326 # FIXME : Please note that there is a risk of infinite loop here if we never find a suitable barcode
327                     $i++;
328
329                     # Only increment copynumber if item was really added
330                     $copynumber++ if ( $copynumber && $copynumber =~ m/^\d+$/ );
331                 }
332
333                 # Preparing the next iteration
334                 $oldbarcode = $barcodevalue;
335             }
336
337             my $indexer = Koha::SearchEngine::Indexer->new(
338                 { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
339             $indexer->index_records( $biblionumber, "specialUpdate",
340                 "biblioserver" );
341
342             undef($current_item);
343         }
344     }
345     if ($frameworkcode eq 'FA' && $fa_circborrowernumber){
346         print $input->redirect(
347            '/cgi-bin/koha/circ/circulation.pl?'
348            .'borrowernumber='.$fa_circborrowernumber
349            .'&barcode='.uri_escape_utf8($fa_barcode)
350            .'&duedatespec='.$fa_duedatespec
351            .'&stickyduedate='.$fa_stickyduedate
352         );
353         exit;
354     }
355
356
357 #-------------------------------------------------------------------------------
358 } elsif ($op eq "edititem") {
359 #-------------------------------------------------------------------------------
360 # retrieve item if exist => then, it's a modif
361     $current_item = Koha::Items->find($itemnumber)->unblessed;
362     # FIXME Handle non existent item
363     $nextop = "saveitem";
364 #-------------------------------------------------------------------------------
365 } elsif ($op eq "dupeitem") {
366 #-------------------------------------------------------------------------------
367 # retrieve item if exist => then, it's a modif
368     $current_item = Koha::Items->find($itemnumber)->unblessed;
369     # FIXME Handle non existent item
370     if (C4::Context->preference('autoBarcode') eq 'incremental') {
371         my ( $barcode ) = C4::Barcodes::ValueBuilder::incremental::get_barcode;
372         $current_item->{barcode} = $barcode;
373     }
374     else {
375         $current_item->{barcode} = undef; # Don't save it!
376     }
377
378     $nextop = "additem";
379 #-------------------------------------------------------------------------------
380 } elsif ($op eq "delitem") {
381 #-------------------------------------------------------------------------------
382     # check that there is no issue on this item before deletion.
383     my $item = Koha::Items->find($itemnumber);
384     my $deleted;
385     if( $item ) {
386         $deleted = $item->safe_delete;
387     } else {
388         $deleted = Koha::Result::Boolean->new(0)->add_message({ message => 'item_not_found' });
389     }
390     if ( $deleted ) {
391         print $input->redirect("additem.pl?biblionumber=$biblionumber&frameworkcode=$frameworkcode&searchid=$searchid");
392         exit;
393     }
394     else {
395         push @errors, @{ $deleted->messages }[0]->message;
396         $nextop = "additem";
397     }
398 #-------------------------------------------------------------------------------
399 } elsif ($op eq "delallitems") {
400 #-------------------------------------------------------------------------------
401     my $items = Koha::Items->search({ biblionumber => $biblionumber });
402     while ( my $item = $items->next ) {
403         my $deleted = $item->safe_delete({ skip_record_index => 1 });
404         push @errors, @{$deleted->messages}[0]->message unless $deleted;
405     }
406     my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
407     $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" );
408     if ( @errors ) {
409         $nextop="additem";
410     } else {
411         my $defaultview = C4::Context->preference('IntranetBiblioDefaultView');
412         my $views = { C4::Search::enabled_staff_search_views };
413         if ($defaultview eq 'isbd' && $views->{can_view_ISBD}) {
414             print $input->redirect("/cgi-bin/koha/catalogue/ISBDdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
415         } elsif  ($defaultview eq 'marc' && $views->{can_view_MARC}) {
416             print $input->redirect("/cgi-bin/koha/catalogue/MARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
417         } elsif  ($defaultview eq 'labeled_marc' && $views->{can_view_labeledMARC}) {
418             print $input->redirect("/cgi-bin/koha/catalogue/labeledMARCdetail.pl?biblionumber=$biblionumber&searchid=$searchid");
419         } else {
420             print $input->redirect("/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber&searchid=$searchid");
421         }
422         exit;
423     }
424 #-------------------------------------------------------------------------------
425 } elsif ($op eq "saveitem") {
426 #-------------------------------------------------------------------------------
427
428     my $itemnumber = $input->param('itemnumber');
429     my $item = Koha::Items->find($itemnumber);
430     # FIXME Handle non existent item
431     my $olditemlost = $item->itemlost;
432     my @columns = Koha::Items->columns;
433     my $new_values = $item->unblessed;
434     for my $c ( @columns ) {
435         if ( $c eq 'more_subfields_xml' ) {
436             my @more_subfields_xml = $input->multi_param("items.more_subfields_xml");
437             my @unlinked_item_subfields;
438             for my $subfield ( uniq @more_subfields_xml ) {
439                 my @v = $input->multi_param('items.more_subfields_xml_' . encode_utf8($subfield));
440                 push @unlinked_item_subfields, $subfield, $_ for @v;
441             }
442             if ( @unlinked_item_subfields ) {
443                 my $marc = MARC::Record->new();
444                 # use of tag 999 is arbitrary, and doesn't need to match the item tag
445                 # used in the framework
446                 $marc->append_fields(MARC::Field->new('999', ' ', ' ', @unlinked_item_subfields));
447                 $marc->encoding("UTF-8");
448                 $new_values->{more_subfields_xml} = $marc->as_xml("USMARC");
449                 next;
450             }
451             $item->more_subfields_xml(undef);
452         } else {
453             my @v = map { ( defined $_ && $_ eq '' ) ? undef : $_ } $input->multi_param( "items." . $c );
454             next unless @v;
455
456             if ( $c eq 'permanent_location' ) { # See 27837
457                 $item->make_column_dirty('permanent_location');
458             }
459
460             if ( scalar(@v) == 1 && not defined $v[0] ) {
461                 delete $new_values->{$c};
462             } else {
463                 $new_values->{$c} = join ' | ', @v;
464             }
465         }
466     }
467     $item = $item->set_or_blank($new_values);
468
469     # check that the barcode don't exist already
470     if (
471         defined $item->barcode
472         && Koha::Items->search(
473             {
474                 barcode    => $item->barcode,
475                 itemnumber => { '!=' => $item->itemnumber }
476             }
477         )->count
478       )
479     {
480         # FIXME We shouldn't need that, ->store would explode as there is a unique constraint on items.barcode
481         push @errors,"barcode_not_unique";
482         $current_item = $item->unblessed; # Restore edit form for the same item
483     } else {
484         my $newitemlost = $item->itemlost;
485         if ( $newitemlost && $newitemlost ge '1' && !$olditemlost ) {
486             LostItem( $item->itemnumber, 'additem' );
487         }
488         $item->store;
489     }
490
491     $nextop="additem";
492 } elsif ($op eq "delinkitem"){
493
494     my $analyticfield = '773';
495     if ($marcflavour  eq 'MARC21'){
496         $analyticfield = '773';
497     } elsif ($marcflavour eq 'UNIMARC') {
498         $analyticfield = '461';
499     }
500     foreach my $field ($record->field($analyticfield)){
501         if ($field->subfield('9') eq $hostitemnumber){
502             $record->delete_field($field);
503             last;
504         }
505     }
506         my $modbibresult = ModBiblio($record, $biblionumber,'');
507 }
508
509 # update OAI-PMH sets
510 if ($op) {
511     if (C4::Context->preference("OAI-PMH:AutoUpdateSets")) {
512         C4::OAI::Sets::UpdateOAISetsBiblio($biblionumber, $record);
513     }
514 }
515
516 #
517 #-------------------------------------------------------------------------------
518 # build screen with existing items. and "new" one
519 #-------------------------------------------------------------------------------
520
521 # now, build existiing item list
522
523 my @items;
524 for my $item ( $biblio->items->as_list, $biblio->host_items->as_list ) {
525     push @items, $item->columns_to_str;
526 }
527
528 my @witness_attributes = uniq map {
529     my $item = $_;
530     map { defined $item->{$_} && $item->{$_} ne "" ? $_ : () } keys %$item
531 } @items;
532
533 our ( $itemtagfield, $itemtagsubfield ) = GetMarcFromKohaField("items.itemnumber");
534
535 my $subfieldcode_attribute_mappings;
536 for my $subfield_code ( keys %{ $tagslib->{$itemtagfield} } ) {
537
538     my $subfield = $tagslib->{$itemtagfield}->{$subfield_code};
539
540     next if IsMarcStructureInternal( $subfield );
541     next unless $subfield->{tab} eq 10; # Is this really needed?
542
543     my $attribute;
544     if ( $subfield->{kohafield} ) {
545         ( $attribute = $subfield->{kohafield} ) =~ s|^items\.||;
546     } else {
547         $attribute = $subfield_code; # It's in more_subfields_xml
548     }
549     next unless grep { $attribute eq $_ } @witness_attributes;
550     $subfieldcode_attribute_mappings->{$subfield_code} = $attribute;
551 }
552
553 my @header_value_loop = map {
554     {
555         header_value  => $tagslib->{$itemtagfield}->{$_}->{lib},
556         attribute     => $subfieldcode_attribute_mappings->{$_},
557         subfield_code => $_,
558     }
559 } sort keys %$subfieldcode_attribute_mappings;
560
561 # Using last created item if it exists
562 if (   $prefillitem
563     && $op ne "additem"
564     && $op ne "edititem"
565     && $op ne "dupeitem" )
566 {
567     my $item_from_cookie = get_item_from_cookie($input);
568     $current_item = $item_from_cookie if $item_from_cookie;
569 }
570
571 if ( $current_item->{more_subfields_xml} ) {
572     # FIXME Use Maybe MARC::Record::new_from_xml if encoding issues on subfield (??)
573     $current_item->{marc_more_subfields_xml} = MARC::Record->new_from_xml($current_item->{more_subfields_xml}, 'UTF-8');
574 }
575
576 my $branchcode = $input->param('branch') || C4::Context->userenv->{branch};
577
578 # If we are not adding a new item
579 # OR
580 # If the subfield must be prefilled with last catalogued item
581 my @subfields_to_prefill;
582 if ( $nextop eq 'additem' && $op ne 'dupeitem' && $prefillitem ) {
583     @subfields_to_prefill = split(' ', C4::Context->preference('SubfieldsToUseWhenPrefill'));
584     # Setting to 1 element if SubfieldsToUseWhenPrefill is empty to prevent all the subfields to be prefilled
585     @subfields_to_prefill = ("") unless @subfields_to_prefill;
586 }
587
588 # Getting list of subfields to keep when restricted editing is enabled
589 my @subfields_to_allow = $restrictededition ? split ' ', C4::Context->preference('SubfieldsToAllowForRestrictedEditing') : ();
590
591 my $subfields =
592   Koha::UI::Form::Builder::Item->new(
593     { biblionumber => $biblionumber, item => $current_item } )->edit_form(
594     {
595         branchcode           => $branchcode,
596         restricted_editition => $restrictededition,
597         (
598             @subfields_to_allow
599             ? ( subfields_to_allow => \@subfields_to_allow )
600             : ()
601         ),
602         (
603             @subfields_to_prefill
604             ? ( subfields_to_prefill => \@subfields_to_prefill )
605             : ()
606         ),
607         prefill_with_default_values => 1,
608         branch_limit => C4::Context->userenv->{"branch"},
609         (
610             $op eq 'dupeitem'
611             ? ( ignore_invisible_subfields => 1 )
612             : ()
613         ),
614     }
615 );
616
617 if (   $frameworkcode eq 'FA' ) {
618     my ( $barcode_field ) = grep {$_->{kohafield} eq 'items.barcode'} @$subfields;
619     $barcode_field->{marc_value}->{value} ||= $input->param('barcode');
620 }
621
622 if( my $default_location = C4::Context->preference('NewItemsDefaultLocation') ) {
623     my ( $location_field ) = grep {$_->{kohafield} eq 'items.location'} @$subfields;
624     $location_field->{marc_value}->{value} ||= $default_location;
625 }
626
627 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
628 $template->param(
629     biblio       => $biblio,
630     items        => \@items,
631     item_header_loop => \@header_value_loop,
632     subfields        => $subfields,
633     itemnumber       => $itemnumber,
634     barcode          => $current_item->{barcode},
635     op      => $nextop,
636     popup => scalar $input->param('popup') ? 1: 0,
637     C4::Search::enabled_staff_search_views,
638 );
639 $template->{'VARS'}->{'searchid'} = $searchid;
640
641 if ($frameworkcode eq 'FA'){
642     # fast cataloguing datas
643     $template->param(
644         'circborrowernumber' => $fa_circborrowernumber,
645         'barcode'            => $fa_barcode,
646         'branch'             => $fa_branch,
647         'stickyduedate'      => $fa_stickyduedate,
648         'duedatespec'        => $fa_duedatespec,
649     );
650 }
651
652 foreach my $error (@errors) {
653     $template->param($error => 1);
654 }
655 output_html_with_http_headers $input, $cookie, $template->output;