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 );
43 use C4::Items qw( PrepareItemrecordDisplay AddItemFromMarc );
44 use C4::Budgets qw( GetBudget GetBudgets GetBudgetHierarchy CanUserUseBudget GetBudgetByCode );
45 use C4::Suggestions; # GetSuggestion
48 use Koha::Number::Price;
50 use Koha::Acquisition::Baskets;
51 use Koha::Acquisition::Currencies;
52 use Koha::Acquisition::Orders;
53 use Koha::Acquisition::Booksellers;
57 my ($template, $loggedinuser, $cookie, $userflags) = get_template_and_user({
58 template_name => "acqui/addorderiso2709.tt",
61 flagsrequired => { acquisition => 'order_manage' },
64 my $cgiparams = $input->Vars;
65 my $op = $cgiparams->{'op'} || '';
66 my $booksellerid = $input->param('booksellerid');
67 my $allmatch = $input->param('allmatch');
68 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
70 $template->param(scriptname => "/cgi-bin/koha/acqui/addorderiso2709.pl",
71 booksellerid => $booksellerid,
72 booksellername => $bookseller->name,
75 if ($cgiparams->{'import_batch_id'} && $op eq ""){
76 $op = "batch_details";
80 if (! $cgiparams->{'basketno'}){
81 die "Basketnumber required to order from iso2709 file import";
83 my $basket = Koha::Acquisition::Baskets->find( $cgiparams->{basketno} );
86 # 1st step = choose the file to import into acquisition
89 $template->param("basketno" => $cgiparams->{'basketno'});
91 import_batches_list($template);
93 # 2nd step = display the content of the chosen file
95 } elsif ($op eq "batch_details"){
96 #display lines inside the selected batch
98 $template->param("batch_details" => 1,
99 "basketno" => $cgiparams->{'basketno'},
100 # get currencies (for change rates calcs if needed)
101 currencies => Koha::Acquisition::Currencies->search,
102 bookseller => $bookseller,
103 "allmatch" => $allmatch,
105 import_biblios_list($template, $cgiparams->{'import_batch_id'});
106 if ( $basket->effective_create_items eq 'ordering' && !$basket->is_standing ) {
107 # prepare empty item form
108 my $cell = PrepareItemrecordDisplay( '', '', undef, 'ACQ' );
110 # warn "==> ".Data::Dumper::Dumper($cell);
112 $cell = PrepareItemrecordDisplay( '', '', undef, '' );
113 $template->param( 'NoACQframework' => 1 );
116 push @itemloop, $cell;
118 $template->param( items => \@itemloop );
121 # 3rd step = import the records
123 } elsif ( $op eq 'import_records' ) {
124 #import selected lines
125 $template->param('basketno' => $cgiparams->{'basketno'});
126 # Budget_id is mandatory for adding an order, we just add a default, the user needs to modify this aftewards
127 my $budgets = GetBudgets();
128 if (scalar @$budgets == 0){
129 die "No budgets defined, can't continue";
131 my $budget_id = @$budgets[0]->{'budget_id'};
132 #get all records from a batch, and check their import status to see if they are checked.
133 #(default values: quantity 1, uncertainprice yes, first budget)
135 # retrieve the file you want to import
136 my $import_batch_id = $cgiparams->{'import_batch_id'};
137 my $biblios = GetImportRecordsRange($import_batch_id);
140 my @import_record_id_selected = $input->multi_param("import_record_id");
141 my @quantities = $input->multi_param('quantity');
142 my @prices = $input->multi_param('price');
143 my @orderreplacementprices = $input->multi_param('replacementprice');
144 my @budgets_id = $input->multi_param('budget_id');
145 my @discount = $input->multi_param('discount');
146 my @sort1 = $input->multi_param('sort1');
147 my @sort2 = $input->multi_param('sort2');
148 my $matcher_id = $input->param('matcher_id');
149 my $active_currency = Koha::Acquisition::Currencies->get_active;
150 my $biblio_count = 0;
151 for my $biblio (@$biblios){
154 # Check if this import_record_id was selected
155 next if not grep { $_ eq $$biblio{import_record_id} } @import_record_id_selected;
156 my ( $marcblob, $encoding ) = GetImportRecordMarc( $biblio->{'import_record_id'} );
157 my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
158 my $match = GetImportRecordMatches( $biblio->{'import_record_id'}, 1 );
159 my $biblionumber=$#$match > -1?$match->[0]->{'biblionumber'}:0;
160 my $c_quantity = shift( @quantities ) || GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour') ) || 1;
161 my $c_budget_id = shift( @budgets_id ) || $input->param('all_budget_id') || $budget_id;
162 my $c_discount = shift ( @discount);
163 my $c_sort1 = shift( @sort1 ) || $input->param('all_sort1') || '';
164 my $c_sort2 = shift( @sort2 ) || $input->param('all_sort2') || '';
165 my $c_replacement_price = shift( @orderreplacementprices );
166 my $c_price = shift( @prices ) || GetMarcPrice($marcrecord, C4::Context->preference('marcflavour'));
168 # Insert the biblio, or find it through matcher
169 unless ( $biblionumber ) {
171 if ( $matcher_id eq '_TITLE_AUTHOR_' ) {
172 $duplifound = 1 if FindDuplicate($marcrecord);
175 my $matcher = C4::Matcher->fetch($matcher_id);
176 my @matches = $matcher->get_matches( $marcrecord, my $max_matches = 1 );
177 $duplifound = 1 if @matches;
180 $duplinbatch = $import_batch_id and next if $duplifound;
187 my ( $isbnfield, $isbnsubfield ) = GetMarcFromKohaField( 'biblioitems.isbn' );
188 if ( $marcrecord->field($isbnfield) ) {
189 foreach my $field ( $marcrecord->field($isbnfield) ) {
190 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
191 my $newisbn = $field->subfield($isbnsubfield);
193 $field->update( $isbnsubfield => $newisbn );
197 ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' );
198 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
200 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
203 SetMatchedBiblionumber( $biblio->{import_record_id}, $biblionumber );
205 # Add items from MarcItemFieldsToOrder
206 my @homebranches = $input->multi_param('homebranch_' . $biblio_count);
207 my $count = scalar @homebranches;
208 my @holdingbranches = $input->multi_param('holdingbranch_' . $biblio_count);
209 my @itypes = $input->multi_param('itype_' . $biblio_count);
210 my @nonpublic_notes = $input->multi_param('nonpublic_note_' . $biblio_count);
211 my @public_notes = $input->multi_param('public_note_' . $biblio_count);
212 my @locs = $input->multi_param('loc_' . $biblio_count);
213 my @ccodes = $input->multi_param('ccode_' . $biblio_count);
214 my @notforloans = $input->multi_param('notforloan_' . $biblio_count);
215 my @uris = $input->multi_param('uri_' . $biblio_count);
216 my @copynos = $input->multi_param('copyno_' . $biblio_count);
217 my @budget_codes = $input->multi_param('budget_code_' . $biblio_count);
218 my @itemprices = $input->multi_param('itemprice_' . $biblio_count);
219 my @replacementprices = $input->multi_param('replacementprice_' . $biblio_count);
220 my @itemcallnumbers = $input->multi_param('itemcallnumber_' . $biblio_count);
221 my $itemcreation = 0;
224 for (my $i = 0; $i < $count; $i++) {
226 my $item = Koha::Item->new(
228 biblionumber => $biblionumber,
229 homebranch => $homebranches[$i],
230 holdingbranch => $holdingbranches[$i],
231 itemnotes_nonpublic => $nonpublic_notes[$i],
232 itemnotes => $public_notes[$i],
233 location => $locs[$i],
234 ccode => $ccodes[$i],
235 itype => $itypes[$i],
236 notforloan => $notforloans[$i],
238 copynumber => $copynos[$i],
239 price => $itemprices[$i],
240 replacementprice => $replacementprices[$i],
241 itemcallnumber => $itemcallnumbers[$i],
244 push( @itemnumbers, $item->itemnumber );
246 if ($itemcreation == 1) {
247 # Group orderlines from MarcItemFieldsToOrder
249 for (my $i = 0; $i < $count; $i++) {
250 $budget_hash->{$budget_codes[$i]}->{quantity} += 1;
251 $budget_hash->{$budget_codes[$i]}->{price} = $itemprices[$i];
252 $budget_hash->{$budget_codes[$i]}->{replacementprice} = $replacementprices[$i];
253 $budget_hash->{$budget_codes[$i]}->{itemnumbers} //= [];
254 push @{ $budget_hash->{$budget_codes[$i]}->{itemnumbers} }, $itemnumbers[$i];
257 # Create orderlines from MarcItemFieldsToOrder
258 while(my ($budget_id, $infos) = each %$budget_hash) {
261 biblionumber => $biblionumber,
262 basketno => $cgiparams->{'basketno'},
263 quantity => $infos->{quantity},
264 budget_id => $budget_id,
265 currency => $cgiparams->{'all_currency'},
268 my $price = $infos->{price};
270 # in France, the cents separator is the , but sometimes, ppl use a .
271 # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
272 $price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
273 $price = Koha::Number::Price->new($price)->unformat;
274 $orderinfo{tax_rate_on_ordering} = $bookseller->tax_rate;
275 $orderinfo{tax_rate_on_receiving} = $bookseller->tax_rate;
276 my $c = $c_discount ? $c_discount : $bookseller->discount;
277 $orderinfo{discount} = $c;
279 $orderinfo{ecost} = $price * ( 1 - $c / 100 );
280 $orderinfo{rrp} = $price;
282 $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
283 $orderinfo{unitprice} = $orderinfo{ecost};
285 $orderinfo{listprice} = 0;
287 $orderinfo{replacementprice} = $infos->{replacementprice} || 0;
289 # remove uncertainprice flag if we have found a price in the MARC record
290 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
292 my $order = Koha::Acquisition::Order->new( \%orderinfo )->store;
293 $order->populate_with_prices_for_ordering();
294 $order->populate_with_prices_for_receiving();
295 $order->add_item( $_ ) for @{ $budget_hash->{$budget_id}->{itemnumbers} };
300 my $patron = Koha::Patrons->find( $loggedinuser );
301 # get quantity in the MARC record (1 if none)
302 my $quantity = GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour')) || 1;
304 biblionumber => $biblionumber,
305 basketno => $cgiparams->{'basketno'},
306 quantity => $c_quantity,
307 branchcode => $patron->branchcode,
308 budget_id => $c_budget_id,
312 order_internalnote => $cgiparams->{'all_order_internalnote'},
313 order_vendornote => $cgiparams->{'all_order_vendornote'},
314 currency => $cgiparams->{'all_currency'},
315 replacementprice => $c_replacement_price,
317 # get the price if there is one.
319 # in France, the cents separator is the , but sometimes, ppl use a .
320 # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
321 $c_price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
322 $c_price = Koha::Number::Price->new($c_price)->unformat;
323 $orderinfo{tax_rate_on_ordering} = $bookseller->tax_rate;
324 $orderinfo{tax_rate_on_receiving} = $bookseller->tax_rate;
325 my $c = $c_discount ? $c_discount : $bookseller->discount;
326 $orderinfo{discount} = $c;
328 $orderinfo{ecost} = $c_price * ( 1 - $c / 100 );
329 $orderinfo{rrp} = $c_price;
331 $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
332 $orderinfo{unitprice} = $orderinfo{ecost};
334 $orderinfo{listprice} = 0;
337 # remove uncertainprice flag if we have found a price in the MARC record
338 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
340 my $order = Koha::Acquisition::Order->new( \%orderinfo );
341 $order->populate_with_prices_for_ordering();
342 $order->populate_with_prices_for_receiving();
345 # 4th, add items if applicable
346 # parse the item sent by the form, and create an item just for the import_record_id we are dealing with
347 # this is not optimised, but it's working !
348 if ( $basket->effective_create_items eq 'ordering' && !$basket->is_standing ) {
349 my @tags = $input->multi_param('tag');
350 my @subfields = $input->multi_param('subfield');
351 my @field_values = $input->multi_param('field_value');
352 my @serials = $input->multi_param('serial');
353 my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values );
354 my $record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
355 for (my $qtyloop=1;$qtyloop <= $c_quantity;$qtyloop++) {
356 my ( $biblionumber, $bibitemnum, $itemnumber ) = AddItemFromMarc( $record, $biblionumber );
357 $order->add_item( $itemnumber );
360 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
366 # If all bibliographic records from the batch have been imported we modifying the status of the batch accordingly
367 SetImportBatchStatus( $import_batch_id, 'imported' )
368 if @{ GetImportRecordsRange( $import_batch_id, undef, undef, 'imported' )}
369 == @{ GetImportRecordsRange( $import_batch_id )};
373 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=".$cgiparams->{'basketno'}."&duplinbatch=$duplinbatch");
375 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=".$cgiparams->{'basketno'}."&booksellerid=$booksellerid&allmatch=1");
380 my $budgets = GetBudgets();
381 my $budget_id = @$budgets[0]->{'budget_id'};
382 # build bookfund list
383 my $patron = Koha::Patrons->find( $loggedinuser )->unblessed;
384 my $budget = GetBudget($budget_id);
387 my $budget_loop = [];
388 my $budgets_hierarchy = GetBudgetHierarchy;
389 foreach my $r ( @{$budgets_hierarchy} ) {
390 next unless (CanUserUseBudget($patron, $r, $userflags));
391 push @{$budget_loop},
392 { b_id => $r->{budget_id},
393 b_txt => $r->{budget_name},
394 b_code => $r->{budget_code},
395 b_sort1_authcat => $r->{'sort1_authcat'},
396 b_sort2_authcat => $r->{'sort2_authcat'},
397 b_active => $r->{budget_period_active},
398 b_sel => ( $r->{budget_id} == $budget_id ) ? 1 : 0,
403 sort { uc( $a->{b_txt}) cmp uc( $b->{b_txt}) } @{$budget_loop};
405 $template->param( budget_loop => $budget_loop,);
407 output_html_with_http_headers $input, $cookie, $template->output;
410 sub import_batches_list {
412 my $batches = GetImportBatchRangeDesc();
415 foreach my $batch (@$batches) {
416 if ( $batch->{'import_status'} =~ /^staged$|^reverted$/ && $batch->{'record_type'} eq 'biblio') {
417 # check if there is at least 1 line still staged
418 my $stagedList=GetImportRecordsRange($batch->{'import_batch_id'}, undef, 1, $batch->{import_status}, { order_by_direction => 'ASC' });
419 if (scalar @$stagedList) {
421 import_batch_id => $batch->{'import_batch_id'},
422 num_records => $batch->{'num_records'},
423 num_items => $batch->{'num_items'},
424 staged_date => $batch->{'upload_timestamp'},
425 import_status => $batch->{'import_status'},
426 file_name => $batch->{'file_name'},
427 comments => $batch->{'comments'},
430 # if there are no more line to includes, set the status to imported
431 # FIXME This should be removed in the future.
432 SetImportBatchStatus( $batch->{'import_batch_id'}, 'imported' );
436 $template->param(batch_list => \@list);
437 my $num_batches = GetNumberOfNonZ3950ImportBatches();
438 $template->param(num_results => $num_batches);
441 sub import_biblios_list {
442 my ($template, $import_batch_id) = @_;
443 my $batch = GetImportBatch($import_batch_id,'staged');
444 return () unless $batch and $batch->{import_status} =~ /^staged$|^reverted$/;
445 my $biblios = GetImportRecordsRange($import_batch_id,'','',$batch->{import_status});
449 my $ccodes = { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' } ) };
450 my $locations = { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) };
451 my $notforloans = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.notforloan' } ) };
454 foreach (sort keys %$locations) {
455 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
458 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
459 push @ccodes, { code => $_, description => $ccodes->{$_} };
462 foreach (sort {$notforloans->{$a} cmp $notforloans->{$b}} keys %$notforloans) {
463 push @notforloans, { code => $_, description => $notforloans->{$_} };
466 my $biblio_count = 0;
467 foreach my $biblio (@$biblios) {
470 my $citation = $biblio->{'title'};
471 $citation .= " $biblio->{'author'}" if $biblio->{'author'};
472 $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
473 $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
474 $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
475 $citation .= $biblio->{'issn'} if $biblio->{'issn'};
476 $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
477 my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
479 import_record_id => $biblio->{'import_record_id'},
480 citation => $citation,
482 status => $biblio->{'status'},
483 record_sequence => $biblio->{'record_sequence'},
484 overlay_status => $biblio->{'overlay_status'},
485 match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
486 match_citation => $#$match > -1 ? $match->[0]->{'title'} || '' . ' ' . $match->[0]->{'author'} || '': '',
487 match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
489 my ( $marcblob, $encoding ) = GetImportRecordMarc( $biblio->{'import_record_id'} );
490 my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
492 my $infos = get_infos_syspref('MarcFieldsToOrder', $marcrecord, ['price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2','replacementprice']);
493 my $price = $infos->{price};
494 my $replacementprice = $infos->{replacementprice};
495 my $quantity = $infos->{quantity};
496 my $budget_code = $infos->{budget_code};
497 my $discount = $infos->{discount};
498 my $sort1 = $infos->{sort1};
499 my $sort2 = $infos->{sort2};
502 my $biblio_budget = GetBudgetByCode($budget_code);
504 $budget_id = $biblio_budget->{budget_id};
510 my $all_items_quantity = 0;
511 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']);
512 if ($alliteminfos != -1) {
513 foreach my $iteminfos (@$alliteminfos) {
514 my $item_homebranch = $iteminfos->{homebranch};
515 my $item_holdingbranch = $iteminfos->{holdingbranch};
516 my $item_itype = $iteminfos->{itype};
517 my $item_nonpublic_note = $iteminfos->{nonpublic_note};
518 my $item_public_note = $iteminfos->{public_note};
519 my $item_loc = $iteminfos->{loc};
520 my $item_ccode = $iteminfos->{ccode};
521 my $item_notforloan = $iteminfos->{notforloan};
522 my $item_uri = $iteminfos->{uri};
523 my $item_copyno = $iteminfos->{copyno};
524 my $item_quantity = $iteminfos->{quantity} || 1;
525 my $item_budget_code = $iteminfos->{budget_code};
527 if ( $iteminfos->{budget_code} ) {
528 my $item_budget = GetBudgetByCode( $iteminfos->{budget_code} );
529 if ( $item_budget ) {
530 $item_budget_id = $item_budget->{budget_id};
533 my $item_price = $iteminfos->{price};
534 my $item_replacement_price = $iteminfos->{replacementprice};
535 my $item_callnumber = $iteminfos->{itemcallnumber};
537 for (my $i = 0; $i < $item_quantity; $i++) {
540 'item_id' => $item_id++,
541 'biblio_count' => $biblio_count,
542 'homebranch' => $item_homebranch,
543 'holdingbranch' => $item_holdingbranch,
544 'itype' => $item_itype,
545 'nonpublic_note' => $item_nonpublic_note,
546 'public_note' => $item_public_note,
548 'ccode' => $item_ccode,
549 'notforloan' => $item_notforloan,
551 'copyno' => $item_copyno,
552 'quantity' => $item_quantity,
553 'budget_id' => $item_budget_id || $budget_id,
554 'itemprice' => $item_price || $price,
555 'replacementprice' => $item_replacement_price || $replacementprice,
556 'itemcallnumber' => $item_callnumber,
558 $all_items_quantity++;
559 push @itemlist, \%itemrecord;
564 $cellrecord{'iteminfos'} = \@itemlist;
566 $cellrecord{'item_error'} = 1;
568 push @list, \%cellrecord;
570 if ($alliteminfos == -1 || scalar(@$alliteminfos) == 0) {
571 $cellrecord{price} = $price || '';
572 $cellrecord{replacementprice} = $replacementprice || '';
573 $cellrecord{quantity} = $quantity || '';
574 $cellrecord{budget_id} = $budget_id || '';
575 $cellrecord{discount} = $discount || '';
576 $cellrecord{sort1} = $sort1 || '';
577 $cellrecord{sort2} = $sort2 || '';
579 $cellrecord{quantity} = $all_items_quantity;
583 my $num_records = $batch->{'num_records'};
584 my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
585 my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
586 my $item_action = GetImportBatchItemAction($import_batch_id);
587 $template->param(biblio_list => \@list,
588 num_results => $num_records,
589 import_batch_id => $import_batch_id,
590 "overlay_action_${overlay_action}" => 1,
591 overlay_action => $overlay_action,
592 "nomatch_action_${nomatch_action}" => 1,
593 nomatch_action => $nomatch_action,
594 "item_action_${item_action}" => 1,
595 item_action => $item_action,
596 item_error => $item_error,
597 libraries => Koha::Libraries->search,
598 locationloop => \@locations,
599 itemtypes => Koha::ItemTypes->search,
600 ccodeloop => \@ccodes,
601 notforloanloop => \@notforloans,
603 batch_info($template, $batch);
607 my ($template, $batch) = @_;
608 $template->param(batch_info => 1,
609 file_name => $batch->{'file_name'},
610 comments => $batch->{'comments'},
611 import_status => $batch->{'import_status'},
612 upload_timestamp => $batch->{'upload_timestamp'},
613 num_records => $batch->{'num_records'},
614 num_items => $batch->{'num_items'});
615 if ($batch->{'num_records'} > 0) {
616 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
617 $template->param(can_commit => 1);
619 if ($batch->{'import_status'} eq 'imported') {
620 $template->param(can_revert => 1);
623 if (defined $batch->{'matcher_id'}) {
624 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
625 if (defined $matcher) {
626 $template->param('current_matcher_id' => $batch->{'matcher_id'},
627 'current_matcher_code' => $matcher->code(),
628 'current_matcher_description' => $matcher->description());
631 add_matcher_list($batch->{'matcher_id'}, $template);
634 sub add_matcher_list {
635 my ($current_matcher_id, $template) = @_;
636 my @matchers = C4::Matcher::GetMatcherList();
637 if (defined $current_matcher_id) {
638 for (my $i = 0; $i <= $#matchers; $i++) {
639 if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
640 $matchers[$i]->{'selected'} = 1;
644 $template->param(available_matchers => \@matchers);
647 sub get_infos_syspref {
648 my ($syspref_name, $record, $field_list) = @_;
649 my $syspref = C4::Context->preference($syspref_name);
650 $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
652 YAML::XS::Load(Encode::encode_utf8($syspref));
655 warn "Unable to parse $syspref syspref : $@";
659 for my $field_name ( @$field_list ) {
660 next unless exists $yaml->{$field_name};
661 my @fields = split /\|/, $yaml->{$field_name};
662 for my $field ( @fields ) {
663 my ( $f, $sf ) = split /\$/, $field;
664 next unless $f and $sf;
665 if ( my $v = $record->subfield( $f, $sf ) ) {
666 $r->{$field_name} = $v;
668 last if $yaml->{$field};
674 sub equal_number_of_fields {
675 my ($tags_list, $record) = @_;
676 my $tag_fields_count;
677 for my $tag (@$tags_list) {
678 my @fields = $record->field($tag);
679 $tag_fields_count->{$tag} = scalar @fields;
683 foreach my $key ( keys %$tag_fields_count ) {
684 if ( $tag_fields_count->{$key} > 0 ) { # Having 0 of a field is ok
685 $tags_count //= $tag_fields_count->{$key}; # Start with the count from the first occurrence
686 return -1 if $tag_fields_count->{$key} != $tags_count; # All counts of various fields should be equal if they exist
693 sub get_infos_syspref_on_item {
694 my ($syspref_name, $record, $field_list) = @_;
695 my $syspref = C4::Context->preference($syspref_name);
696 $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
698 YAML::XS::Load(Encode::encode_utf8($syspref));
701 warn "Unable to parse $syspref syspref : $@";
707 # Check tags in syspref definition
708 for my $field_name ( @$field_list ) {
709 next unless exists $yaml->{$field_name};
710 my @fields = split /\|/, $yaml->{$field_name};
711 for my $field ( @fields ) {
712 my ( $f, $sf ) = split /\$/, $field;
713 next unless $f and $sf;
717 @tags_list = List::MoreUtils::uniq(@tags_list);
719 my $tags_count = equal_number_of_fields(\@tags_list, $record);
720 # Return if the number of these fields in the record is not the same.
721 return -1 if $tags_count == -1;
725 foreach my $tag (@tags_list) {
727 foreach my $field ($record->field($tag)) {
728 push @tmp_fields, $field;
730 $fields_hash->{$tag} = \@tmp_fields;
733 for (my $i = 0; $i < $tags_count; $i++) {
735 for my $field_name ( @$field_list ) {
736 next unless exists $yaml->{$field_name};
737 my @fields = split /\|/, $yaml->{$field_name};
738 for my $field ( @fields ) {
739 my ( $f, $sf ) = split /\$/, $field;
740 next unless $f and $sf;
741 my $v = $fields_hash->{$f}[$i] ? $fields_hash->{$f}[$i]->subfield( $sf ) : undef;
742 $r->{$field_name} = $v if (defined $v);
743 last if $yaml->{$field};