3 #A script that lets the user populate a basket from an iso2709 file
4 #the script first displays a list of import batches, then when a batch is selected displays all the biblios in it.
5 #The user can then pick which biblios they want to order
7 # Copyright 2008 - 2011 BibLibre SARL
9 # This file is part of Koha.
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
31 use C4::Auth qw( get_template_and_user );
32 use C4::Output qw( output_html_with_http_headers );
33 use C4::ImportBatch qw( GetImportRecordsRange GetImportRecordMarc GetImportRecordMatches SetImportRecordStatus SetMatchedBiblionumber SetImportBatchStatus GetImportBatch GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction );
35 use C4::Search qw( FindDuplicate );
36 use C4::Acquisition qw( populate_order_with_prices );
44 use C4::Items qw( PrepareItemrecordDisplay AddItemFromMarc );
45 use C4::Budgets qw( GetBudget GetBudgets GetBudgetHierarchy CanUserUseBudget GetBudgetByCode );
46 use C4::Acquisition qw( populate_order_with_prices );
47 use C4::Suggestions; # GetSuggestion
50 use Koha::Number::Price;
52 use Koha::Acquisition::Baskets;
53 use Koha::Acquisition::Currencies;
54 use Koha::Acquisition::Orders;
55 use Koha::Acquisition::Booksellers;
59 my ($template, $loggedinuser, $cookie, $userflags) = get_template_and_user({
60 template_name => "acqui/addorderiso2709.tt",
63 flagsrequired => { acquisition => 'order_manage' },
66 my $cgiparams = $input->Vars;
67 my $op = $cgiparams->{'op'} || '';
68 my $booksellerid = $input->param('booksellerid');
69 my $allmatch = $input->param('allmatch');
70 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
72 $template->param(scriptname => "/cgi-bin/koha/acqui/addorderiso2709.pl",
73 booksellerid => $booksellerid,
74 booksellername => $bookseller->name,
77 if ($cgiparams->{'import_batch_id'} && $op eq ""){
78 $op = "batch_details";
82 if (! $cgiparams->{'basketno'}){
83 die "Basketnumber required to order from iso2709 file import";
85 my $basket = Koha::Acquisition::Baskets->find( $cgiparams->{basketno} );
88 # 1st step = choose the file to import into acquisition
91 $template->param("basketno" => $cgiparams->{'basketno'});
93 import_batches_list($template);
95 # 2nd step = display the content of the chosen file
97 } elsif ($op eq "batch_details"){
98 #display lines inside the selected batch
99 # get currencies (for change rates calcs if needed)
100 my @currencies = Koha::Acquisition::Currencies->search;
102 $template->param("batch_details" => 1,
103 "basketno" => $cgiparams->{'basketno'},
104 currencies => \@currencies,
105 bookseller => $bookseller,
106 "allmatch" => $allmatch,
108 import_biblios_list($template, $cgiparams->{'import_batch_id'});
109 if ( $basket->effective_create_items eq 'ordering' && !$basket->is_standing ) {
110 # prepare empty item form
111 my $cell = PrepareItemrecordDisplay( '', '', undef, 'ACQ' );
113 # warn "==> ".Data::Dumper::Dumper($cell);
115 $cell = PrepareItemrecordDisplay( '', '', undef, '' );
116 $template->param( 'NoACQframework' => 1 );
119 push @itemloop, $cell;
121 $template->param( items => \@itemloop );
124 # 3rd step = import the records
126 } elsif ( $op eq 'import_records' ) {
127 #import selected lines
128 $template->param('basketno' => $cgiparams->{'basketno'});
129 # Budget_id is mandatory for adding an order, we just add a default, the user needs to modify this aftewards
130 my $budgets = GetBudgets();
131 if (scalar @$budgets == 0){
132 die "No budgets defined, can't continue";
134 my $budget_id = @$budgets[0]->{'budget_id'};
135 #get all records from a batch, and check their import status to see if they are checked.
136 #(default values: quantity 1, uncertainprice yes, first budget)
138 # retrieve the file you want to import
139 my $import_batch_id = $cgiparams->{'import_batch_id'};
140 my $biblios = GetImportRecordsRange($import_batch_id);
143 my @import_record_id_selected = $input->multi_param("import_record_id");
144 my @quantities = $input->multi_param('quantity');
145 my @prices = $input->multi_param('price');
146 my @orderreplacementprices = $input->multi_param('replacementprice');
147 my @budgets_id = $input->multi_param('budget_id');
148 my @discount = $input->multi_param('discount');
149 my @sort1 = $input->multi_param('sort1');
150 my @sort2 = $input->multi_param('sort2');
151 my $matcher_id = $input->param('matcher_id');
152 my $active_currency = Koha::Acquisition::Currencies->get_active;
153 my $biblio_count = 0;
154 for my $biblio (@$biblios){
157 # Check if this import_record_id was selected
158 next if not grep { $_ eq $$biblio{import_record_id} } @import_record_id_selected;
159 my ( $marcblob, $encoding ) = GetImportRecordMarc( $biblio->{'import_record_id'} );
160 my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
161 my $match = GetImportRecordMatches( $biblio->{'import_record_id'}, 1 );
162 my $biblionumber=$#$match > -1?$match->[0]->{'biblionumber'}:0;
163 my $c_quantity = shift( @quantities ) || GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour') ) || 1;
164 my $c_budget_id = shift( @budgets_id ) || $input->param('all_budget_id') || $budget_id;
165 my $c_discount = shift ( @discount);
166 $c_discount = $c_discount / 100 if $c_discount > 1;
167 my $c_sort1 = shift( @sort1 ) || $input->param('all_sort1') || '';
168 my $c_sort2 = shift( @sort2 ) || $input->param('all_sort2') || '';
169 my $c_replacement_price = shift( @orderreplacementprices );
170 my $c_price = shift( @prices ) || GetMarcPrice($marcrecord, C4::Context->preference('marcflavour'));
172 # Insert the biblio, or find it through matcher
173 unless ( $biblionumber ) {
175 if ( $matcher_id eq '_TITLE_AUTHOR_' ) {
176 $duplifound = 1 if FindDuplicate($marcrecord);
179 my $matcher = C4::Matcher->fetch($matcher_id);
180 my @matches = $matcher->get_matches( $marcrecord, my $max_matches = 1 );
181 $duplifound = 1 if @matches;
184 $duplinbatch = $import_batch_id and next if $duplifound;
191 my ( $isbnfield, $isbnsubfield ) = GetMarcFromKohaField( 'biblioitems.isbn' );
192 if ( $marcrecord->field($isbnfield) ) {
193 foreach my $field ( $marcrecord->field($isbnfield) ) {
194 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
195 my $newisbn = $field->subfield($isbnsubfield);
197 $field->update( $isbnsubfield => $newisbn );
201 ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' );
202 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
204 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
207 SetMatchedBiblionumber( $biblio->{import_record_id}, $biblionumber );
209 # Add items from MarcItemFieldsToOrder
210 my @homebranches = $input->multi_param('homebranch_' . $biblio_count);
211 my $count = scalar @homebranches;
212 my @holdingbranches = $input->multi_param('holdingbranch_' . $biblio_count);
213 my @itypes = $input->multi_param('itype_' . $biblio_count);
214 my @nonpublic_notes = $input->multi_param('nonpublic_note_' . $biblio_count);
215 my @public_notes = $input->multi_param('public_note_' . $biblio_count);
216 my @locs = $input->multi_param('loc_' . $biblio_count);
217 my @ccodes = $input->multi_param('ccode_' . $biblio_count);
218 my @notforloans = $input->multi_param('notforloan_' . $biblio_count);
219 my @uris = $input->multi_param('uri_' . $biblio_count);
220 my @copynos = $input->multi_param('copyno_' . $biblio_count);
221 my @budget_codes = $input->multi_param('budget_code_' . $biblio_count);
222 my @itemprices = $input->multi_param('itemprice_' . $biblio_count);
223 my @replacementprices = $input->multi_param('replacementprice_' . $biblio_count);
224 my @itemcallnumbers = $input->multi_param('itemcallnumber_' . $biblio_count);
225 my $itemcreation = 0;
228 for (my $i = 0; $i < $count; $i++) {
230 my $item = Koha::Item->new(
232 biblionumber => $biblionumber,
233 homebranch => $homebranches[$i],
234 holdingbranch => $holdingbranches[$i],
235 itemnotes_nonpublic => $nonpublic_notes[$i],
236 itemnotes => $public_notes[$i],
237 location => $locs[$i],
238 ccode => $ccodes[$i],
239 itype => $itypes[$i],
240 notforloan => $notforloans[$i],
242 copynumber => $copynos[$i],
243 price => $itemprices[$i],
244 replacementprice => $replacementprices[$i],
245 itemcallnumber => $itemcallnumbers[$i],
248 push( @itemnumbers, $item->itemnumber );
250 if ($itemcreation == 1) {
251 # Group orderlines from MarcItemFieldsToOrder
253 for (my $i = 0; $i < $count; $i++) {
254 $budget_hash->{$budget_codes[$i]}->{quantity} += 1;
255 $budget_hash->{$budget_codes[$i]}->{price} = $itemprices[$i];
256 $budget_hash->{$budget_codes[$i]}->{replacementprice} = $replacementprices[$i];
257 $budget_hash->{$budget_codes[$i]}->{itemnumbers} //= [];
258 push @{ $budget_hash->{$budget_codes[$i]}->{itemnumbers} }, $itemnumbers[$i];
261 # Create orderlines from MarcItemFieldsToOrder
262 while(my ($budget_id, $infos) = each %$budget_hash) {
265 biblionumber => $biblionumber,
266 basketno => $cgiparams->{'basketno'},
267 quantity => $infos->{quantity},
268 budget_id => $budget_id,
269 currency => $cgiparams->{'all_currency'},
272 my $price = $infos->{price};
274 # in France, the cents separator is the , but sometimes, ppl use a .
275 # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
276 $price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
277 $price = Koha::Number::Price->new($price)->unformat;
278 $orderinfo{tax_rate} = $bookseller->tax_rate;
279 my $c = $c_discount ? $c_discount : $bookseller->discount / 100;
280 $orderinfo{discount} = $c;
282 $orderinfo{ecost} = $price * ( 1 - $c / 100 );
283 $orderinfo{rrp} = $price;
285 $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
286 $orderinfo{unitprice} = $orderinfo{ecost};
287 $orderinfo{total} = $orderinfo{ecost} * $infos->{quantity};
289 $orderinfo{listprice} = 0;
291 $orderinfo{replacementprice} = $infos->{replacementprice} || 0;
293 # remove uncertainprice flag if we have found a price in the MARC record
294 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
297 C4::Acquisition::populate_order_with_prices(
299 order => \%orderinfo,
300 booksellerid => $booksellerid,
307 my $order = Koha::Acquisition::Order->new( \%orderinfo )->store;
308 $order->add_item( $_ ) for @{ $budget_hash->{$budget_id}->{itemnumbers} };
313 my $patron = Koha::Patrons->find( $loggedinuser );
314 # get quantity in the MARC record (1 if none)
315 my $quantity = GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour')) || 1;
317 biblionumber => $biblionumber,
318 basketno => $cgiparams->{'basketno'},
319 quantity => $c_quantity,
320 branchcode => $patron->branchcode,
321 budget_id => $c_budget_id,
325 order_internalnote => $cgiparams->{'all_order_internalnote'},
326 order_vendornote => $cgiparams->{'all_order_vendornote'},
327 currency => $cgiparams->{'all_currency'},
328 replacementprice => $c_replacement_price,
330 # get the price if there is one.
332 # in France, the cents separator is the , but sometimes, ppl use a .
333 # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
334 $c_price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
335 $c_price = Koha::Number::Price->new($c_price)->unformat;
336 $orderinfo{tax_rate} = $bookseller->tax_rate;
337 my $c = $c_discount ? $c_discount : $bookseller->discount / 100;
338 $orderinfo{discount} = $c;
340 $orderinfo{ecost} = $c_price * ( 1 - $c / 100 );
341 $orderinfo{rrp} = $c_price;
343 $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
344 $orderinfo{unitprice} = $orderinfo{ecost};
345 $orderinfo{total} = $orderinfo{ecost} * $c_quantity;
347 $orderinfo{listprice} = 0;
350 # remove uncertainprice flag if we have found a price in the MARC record
351 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
354 C4::Acquisition::populate_order_with_prices(
356 order => \%orderinfo,
357 booksellerid => $booksellerid,
364 my $order = Koha::Acquisition::Order->new( \%orderinfo )->store;
366 # 4th, add items if applicable
367 # parse the item sent by the form, and create an item just for the import_record_id we are dealing with
368 # this is not optimised, but it's working !
369 if ( $basket->effective_create_items eq 'ordering' && !$basket->is_standing ) {
370 my @tags = $input->multi_param('tag');
371 my @subfields = $input->multi_param('subfield');
372 my @field_values = $input->multi_param('field_value');
373 my @serials = $input->multi_param('serial');
374 my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values );
375 my $record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
376 for (my $qtyloop=1;$qtyloop <= $c_quantity;$qtyloop++) {
377 my ( $biblionumber, $bibitemnum, $itemnumber ) = AddItemFromMarc( $record, $biblionumber );
378 $order->add_item( $itemnumber );
381 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
387 # If all bibliographic records from the batch have been imported we modifying the status of the batch accordingly
388 SetImportBatchStatus( $import_batch_id, 'imported' )
389 if @{ GetImportRecordsRange( $import_batch_id, undef, undef, 'imported' )}
390 == @{ GetImportRecordsRange( $import_batch_id )};
394 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=".$cgiparams->{'basketno'}."&duplinbatch=$duplinbatch");
396 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=".$cgiparams->{'basketno'}."&booksellerid=$booksellerid&allmatch=1");
401 my $budgets = GetBudgets();
402 my $budget_id = @$budgets[0]->{'budget_id'};
403 # build bookfund list
404 my $patron = Koha::Patrons->find( $loggedinuser )->unblessed;
405 my $budget = GetBudget($budget_id);
408 my $budget_loop = [];
409 my $budgets_hierarchy = GetBudgetHierarchy;
410 foreach my $r ( @{$budgets_hierarchy} ) {
411 next unless (CanUserUseBudget($patron, $r, $userflags));
412 push @{$budget_loop},
413 { b_id => $r->{budget_id},
414 b_txt => $r->{budget_name},
415 b_code => $r->{budget_code},
416 b_sort1_authcat => $r->{'sort1_authcat'},
417 b_sort2_authcat => $r->{'sort2_authcat'},
418 b_active => $r->{budget_period_active},
419 b_sel => ( $r->{budget_id} == $budget_id ) ? 1 : 0,
424 sort { uc( $a->{b_txt}) cmp uc( $b->{b_txt}) } @{$budget_loop};
426 $template->param( budget_loop => $budget_loop,);
428 output_html_with_http_headers $input, $cookie, $template->output;
431 sub import_batches_list {
433 my $batches = GetImportBatchRangeDesc();
436 foreach my $batch (@$batches) {
437 if ( $batch->{'import_status'} =~ /^staged$|^reverted$/ && $batch->{'record_type'} eq 'biblio') {
438 # check if there is at least 1 line still staged
439 my $stagedList=GetImportRecordsRange($batch->{'import_batch_id'}, undef, 1, $batch->{import_status}, { order_by_direction => 'ASC' });
440 if (scalar @$stagedList) {
442 import_batch_id => $batch->{'import_batch_id'},
443 num_records => $batch->{'num_records'},
444 num_items => $batch->{'num_items'},
445 staged_date => $batch->{'upload_timestamp'},
446 import_status => $batch->{'import_status'},
447 file_name => $batch->{'file_name'},
448 comments => $batch->{'comments'},
451 # if there are no more line to includes, set the status to imported
452 # FIXME This should be removed in the future.
453 SetImportBatchStatus( $batch->{'import_batch_id'}, 'imported' );
457 $template->param(batch_list => \@list);
458 my $num_batches = GetNumberOfNonZ3950ImportBatches();
459 $template->param(num_results => $num_batches);
462 sub import_biblios_list {
463 my ($template, $import_batch_id) = @_;
464 my $batch = GetImportBatch($import_batch_id,'staged');
465 return () unless $batch and $batch->{import_status} =~ /^staged$|^reverted$/;
466 my $biblios = GetImportRecordsRange($import_batch_id,'','',$batch->{import_status});
470 my $ccodes = { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' } ) };
471 my $locations = { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) };
472 my $notforloans = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.notforloan' } ) };
475 foreach (sort keys %$locations) {
476 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
479 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
480 push @ccodes, { code => $_, description => $ccodes->{$_} };
483 foreach (sort {$notforloans->{$a} cmp $notforloans->{$b}} keys %$notforloans) {
484 push @notforloans, { code => $_, description => $notforloans->{$_} };
487 my $biblio_count = 0;
488 foreach my $biblio (@$biblios) {
491 my $citation = $biblio->{'title'};
492 $citation .= " $biblio->{'author'}" if $biblio->{'author'};
493 $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
494 $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
495 $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
496 $citation .= $biblio->{'issn'} if $biblio->{'issn'};
497 $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
498 my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
500 import_record_id => $biblio->{'import_record_id'},
501 citation => $citation,
503 status => $biblio->{'status'},
504 record_sequence => $biblio->{'record_sequence'},
505 overlay_status => $biblio->{'overlay_status'},
506 match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
507 match_citation => $#$match > -1 ? $match->[0]->{'title'} || '' . ' ' . $match->[0]->{'author'} || '': '',
508 match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
510 my ( $marcblob, $encoding ) = GetImportRecordMarc( $biblio->{'import_record_id'} );
511 my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
513 my $infos = get_infos_syspref('MarcFieldsToOrder', $marcrecord, ['price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2','replacementprice']);
514 my $price = $infos->{price};
515 my $replacementprice = $infos->{replacementprice};
516 my $quantity = $infos->{quantity};
517 my $budget_code = $infos->{budget_code};
518 my $discount = $infos->{discount};
519 my $sort1 = $infos->{sort1};
520 my $sort2 = $infos->{sort2};
523 my $biblio_budget = GetBudgetByCode($budget_code);
525 $budget_id = $biblio_budget->{budget_id};
531 my $all_items_quantity = 0;
532 my $alliteminfos = get_infos_syspref_on_item('MarcItemFieldsToOrder', $marcrecord, ['homebranch', 'holdingbranch', 'itype', 'nonpublic_note', 'public_note', 'loc', 'ccode', 'notforloan', 'uri', 'copyno', 'price', 'replacementprice', 'itemcallnumber', 'quantity', 'budget_code']);
533 if ($alliteminfos != -1) {
534 foreach my $iteminfos (@$alliteminfos) {
535 my $item_homebranch = $iteminfos->{homebranch};
536 my $item_holdingbranch = $iteminfos->{holdingbranch};
537 my $item_itype = $iteminfos->{itype};
538 my $item_nonpublic_note = $iteminfos->{nonpublic_note};
539 my $item_public_note = $iteminfos->{public_note};
540 my $item_loc = $iteminfos->{loc};
541 my $item_ccode = $iteminfos->{ccode};
542 my $item_notforloan = $iteminfos->{notforloan};
543 my $item_uri = $iteminfos->{uri};
544 my $item_copyno = $iteminfos->{copyno};
545 my $item_quantity = $iteminfos->{quantity} || 1;
546 my $item_budget_code = $iteminfos->{budget_code};
548 if ( $iteminfos->{budget_code} ) {
549 my $item_budget = GetBudgetByCode( $iteminfos->{budget_code} );
550 if ( $item_budget ) {
551 $item_budget_id = $item_budget->{budget_id};
554 my $item_price = $iteminfos->{price};
555 my $item_replacement_price = $iteminfos->{replacementprice};
556 my $item_callnumber = $iteminfos->{itemcallnumber};
558 for (my $i = 0; $i < $item_quantity; $i++) {
561 'item_id' => $item_id++,
562 'biblio_count' => $biblio_count,
563 'homebranch' => $item_homebranch,
564 'holdingbranch' => $item_holdingbranch,
565 'itype' => $item_itype,
566 'nonpublic_note' => $item_nonpublic_note,
567 'public_note' => $item_public_note,
569 'ccode' => $item_ccode,
570 'notforloan' => $item_notforloan,
572 'copyno' => $item_copyno,
573 'quantity' => $item_quantity,
574 'budget_id' => $item_budget_id || $budget_id,
575 'itemprice' => $item_price || $price,
576 'replacementprice' => $item_replacement_price || $replacementprice,
577 'itemcallnumber' => $item_callnumber,
579 $all_items_quantity++;
580 push @itemlist, \%itemrecord;
585 $cellrecord{'iteminfos'} = \@itemlist;
587 $cellrecord{'item_error'} = 1;
589 push @list, \%cellrecord;
591 if ($alliteminfos == -1 || scalar(@$alliteminfos) == 0) {
592 $cellrecord{price} = $price || '';
593 $cellrecord{replacementprice} = $replacementprice || '';
594 $cellrecord{quantity} = $quantity || '';
595 $cellrecord{budget_id} = $budget_id || '';
596 $cellrecord{discount} = $discount || '';
597 $cellrecord{sort1} = $sort1 || '';
598 $cellrecord{sort2} = $sort2 || '';
600 $cellrecord{quantity} = $all_items_quantity;
604 my $num_records = $batch->{'num_records'};
605 my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
606 my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
607 my $item_action = GetImportBatchItemAction($import_batch_id);
608 my @itypes = Koha::ItemTypes->search;
609 $template->param(biblio_list => \@list,
610 num_results => $num_records,
611 import_batch_id => $import_batch_id,
612 "overlay_action_${overlay_action}" => 1,
613 overlay_action => $overlay_action,
614 "nomatch_action_${nomatch_action}" => 1,
615 nomatch_action => $nomatch_action,
616 "item_action_${item_action}" => 1,
617 item_action => $item_action,
618 item_error => $item_error,
619 libraries => scalar Koha::Libraries->search(),
620 locationloop => \@locations,
621 itypeloop => \@itypes,
622 ccodeloop => \@ccodes,
623 notforloanloop => \@notforloans,
625 batch_info($template, $batch);
629 my ($template, $batch) = @_;
630 $template->param(batch_info => 1,
631 file_name => $batch->{'file_name'},
632 comments => $batch->{'comments'},
633 import_status => $batch->{'import_status'},
634 upload_timestamp => $batch->{'upload_timestamp'},
635 num_records => $batch->{'num_records'},
636 num_items => $batch->{'num_items'});
637 if ($batch->{'num_records'} > 0) {
638 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
639 $template->param(can_commit => 1);
641 if ($batch->{'import_status'} eq 'imported') {
642 $template->param(can_revert => 1);
645 if (defined $batch->{'matcher_id'}) {
646 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
647 if (defined $matcher) {
648 $template->param('current_matcher_id' => $batch->{'matcher_id'},
649 'current_matcher_code' => $matcher->code(),
650 'current_matcher_description' => $matcher->description());
653 add_matcher_list($batch->{'matcher_id'}, $template);
656 sub add_matcher_list {
657 my ($current_matcher_id, $template) = @_;
658 my @matchers = C4::Matcher::GetMatcherList();
659 if (defined $current_matcher_id) {
660 for (my $i = 0; $i <= $#matchers; $i++) {
661 if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
662 $matchers[$i]->{'selected'} = 1;
666 $template->param(available_matchers => \@matchers);
669 sub get_infos_syspref {
670 my ($syspref_name, $record, $field_list) = @_;
671 my $syspref = C4::Context->preference($syspref_name);
672 $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
674 YAML::XS::Load(Encode::encode_utf8($syspref));
677 warn "Unable to parse $syspref syspref : $@";
681 for my $field_name ( @$field_list ) {
682 next unless exists $yaml->{$field_name};
683 my @fields = split /\|/, $yaml->{$field_name};
684 for my $field ( @fields ) {
685 my ( $f, $sf ) = split /\$/, $field;
686 next unless $f and $sf;
687 if ( my $v = $record->subfield( $f, $sf ) ) {
688 $r->{$field_name} = $v;
690 last if $yaml->{$field};
696 sub equal_number_of_fields {
697 my ($tags_list, $record) = @_;
698 my $tag_fields_count;
699 for my $tag (@$tags_list) {
700 my @fields = $record->field($tag);
701 $tag_fields_count->{$tag} = scalar @fields;
705 foreach my $key ( keys %$tag_fields_count ) {
706 if ( $tag_fields_count->{$key} > 0 ) { # Having 0 of a field is ok
707 $tags_count //= $tag_fields_count->{$key}; # Start with the count from the first occurrence
708 return -1 if $tag_fields_count->{$key} != $tags_count; # All counts of various fields should be equal if they exist
715 sub get_infos_syspref_on_item {
716 my ($syspref_name, $record, $field_list) = @_;
717 my $syspref = C4::Context->preference($syspref_name);
718 $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
720 YAML::XS::Load(Encode::encode_utf8($syspref));
723 warn "Unable to parse $syspref syspref : $@";
729 # Check tags in syspref definition
730 for my $field_name ( @$field_list ) {
731 next unless exists $yaml->{$field_name};
732 my @fields = split /\|/, $yaml->{$field_name};
733 for my $field ( @fields ) {
734 my ( $f, $sf ) = split /\$/, $field;
735 next unless $f and $sf;
739 @tags_list = List::MoreUtils::uniq(@tags_list);
741 my $tags_count = equal_number_of_fields(\@tags_list, $record);
742 # Return if the number of these fields in the record is not the same.
743 return -1 if $tags_count == -1;
747 foreach my $tag (@tags_list) {
749 foreach my $field ($record->field($tag)) {
750 push @tmp_fields, $field;
752 $fields_hash->{$tag} = \@tmp_fields;
755 for (my $i = 0; $i < $tags_count; $i++) {
757 for my $field_name ( @$field_list ) {
758 next unless exists $yaml->{$field_name};
759 my @fields = split /\|/, $yaml->{$field_name};
760 for my $field ( @fields ) {
761 my ( $f, $sf ) = split /\$/, $field;
762 next unless $f and $sf;
763 my $v = $fields_hash->{$f}[$i] ? $fields_hash->{$f}[$i]->subfield( $sf ) : undef;
764 $r->{$field_name} = $v if (defined $v);
765 last if $yaml->{$field};