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>.
28 use List::MoreUtils qw/uniq/;
35 use C4::Search qw/FindDuplicate/;
42 use C4::Suggestions; # GetSuggestion
45 use Koha::Number::Price;
47 use Koha::Acquisition::Baskets;
48 use Koha::Acquisition::Currencies;
49 use Koha::Acquisition::Orders;
50 use Koha::Acquisition::Booksellers;
54 my ($template, $loggedinuser, $cookie, $userflags) = get_template_and_user({
55 template_name => "acqui/addorderiso2709.tt",
58 flagsrequired => { acquisition => 'order_manage' },
62 my $cgiparams = $input->Vars;
63 my $op = $cgiparams->{'op'} || '';
64 my $booksellerid = $input->param('booksellerid');
65 my $allmatch = $input->param('allmatch');
66 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
68 $template->param(scriptname => "/cgi-bin/koha/acqui/addorderiso2709.pl",
69 booksellerid => $booksellerid,
70 booksellername => $bookseller->name,
73 if ($cgiparams->{'import_batch_id'} && $op eq ""){
74 $op = "batch_details";
78 if (! $cgiparams->{'basketno'}){
79 die "Basketnumber required to order from iso2709 file import";
81 my $basket = Koha::Acquisition::Baskets->find( $cgiparams->{basketno} );
84 # 1st step = choose the file to import into acquisition
87 $template->param("basketno" => $cgiparams->{'basketno'});
89 import_batches_list($template);
91 # 2nd step = display the content of the chosen file
93 } elsif ($op eq "batch_details"){
94 #display lines inside the selected batch
95 # get currencies (for change rates calcs if needed)
96 my @currencies = Koha::Acquisition::Currencies->search;
98 $template->param("batch_details" => 1,
99 "basketno" => $cgiparams->{'basketno'},
100 currencies => \@currencies,
101 bookseller => $bookseller,
102 "allmatch" => $allmatch,
104 import_biblios_list($template, $cgiparams->{'import_batch_id'});
105 if ( $basket->effective_create_items eq 'ordering' && !$basket->is_standing ) {
106 # prepare empty item form
107 my $cell = PrepareItemrecordDisplay( '', '', '', 'ACQ' );
109 # warn "==> ".Data::Dumper::Dumper($cell);
111 $cell = PrepareItemrecordDisplay( '', '', '', '' );
112 $template->param( 'NoACQframework' => 1 );
115 push @itemloop, $cell;
117 $template->param( items => \@itemloop );
120 # 3rd step = import the records
122 } elsif ( $op eq 'import_records' ) {
123 #import selected lines
124 $template->param('basketno' => $cgiparams->{'basketno'});
125 # Budget_id is mandatory for adding an order, we just add a default, the user needs to modify this aftewards
126 my $budgets = GetBudgets();
127 if (scalar @$budgets == 0){
128 die "No budgets defined, can't continue";
130 my $budget_id = @$budgets[0]->{'budget_id'};
131 #get all records from a batch, and check their import status to see if they are checked.
132 #(default values: quantity 1, uncertainprice yes, first budget)
134 # retrieve the file you want to import
135 my $import_batch_id = $cgiparams->{'import_batch_id'};
136 my $biblios = GetImportRecordsRange($import_batch_id);
139 my @import_record_id_selected = $input->multi_param("import_record_id");
140 my @quantities = $input->multi_param('quantity');
141 my @prices = $input->multi_param('price');
142 my @orderreplacementprices = $input->multi_param('replacementprice');
143 my @budgets_id = $input->multi_param('budget_id');
144 my @discount = $input->multi_param('discount');
145 my @sort1 = $input->multi_param('sort1');
146 my @sort2 = $input->multi_param('sort2');
147 my $matcher_id = $input->param('matcher_id');
148 my $active_currency = Koha::Acquisition::Currencies->get_active;
149 my $biblio_count = 0;
150 for my $biblio (@$biblios){
153 # Check if this import_record_id was selected
154 next if not grep { $_ eq $$biblio{import_record_id} } @import_record_id_selected;
155 my ( $marcblob, $encoding ) = GetImportRecordMarc( $biblio->{'import_record_id'} );
156 my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
157 my $match = GetImportRecordMatches( $biblio->{'import_record_id'}, 1 );
158 my $biblionumber=$#$match > -1?$match->[0]->{'biblionumber'}:0;
159 my $c_quantity = shift( @quantities ) || GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour') ) || 1;
160 my $c_budget_id = shift( @budgets_id ) || $input->param('all_budget_id') || $budget_id;
161 my $c_discount = shift ( @discount);
162 $c_discount = $c_discount / 100 if $c_discount > 1;
163 my $c_sort1 = shift( @sort1 ) || $input->param('all_sort1') || '';
164 my $c_sort2 = shift( @sort2 ) || $input->param('all_sort2') || '';
166 # Insert the biblio, or find it through matcher
167 unless ( $biblionumber ) {
169 if ( $matcher_id eq '_TITLE_AUTHOR_' ) {
170 $duplifound = 1 if FindDuplicate($marcrecord);
173 my $matcher = C4::Matcher->fetch($matcher_id);
174 my @matches = $matcher->get_matches( $marcrecord, my $max_matches = 1 );
175 $duplifound = 1 if @matches;
178 $duplinbatch = $import_batch_id and next if $duplifound;
185 my ( $isbnfield, $isbnsubfield ) = GetMarcFromKohaField( 'biblioitems.isbn' );
186 if ( $marcrecord->field($isbnfield) ) {
187 foreach my $field ( $marcrecord->field($isbnfield) ) {
188 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
189 my $newisbn = $field->subfield($isbnsubfield);
191 $field->update( $isbnsubfield => $newisbn );
195 ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' );
196 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
198 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
201 SetMatchedBiblionumber( $biblio->{import_record_id}, $biblionumber );
203 # Add items from MarcItemFieldsToOrder
204 my @homebranches = $input->multi_param('homebranch_' . $biblio_count);
205 my $count = scalar @homebranches;
206 my @holdingbranches = $input->multi_param('holdingbranch_' . $biblio_count);
207 my @itypes = $input->multi_param('itype_' . $biblio_count);
208 my @nonpublic_notes = $input->multi_param('nonpublic_note_' . $biblio_count);
209 my @public_notes = $input->multi_param('public_note_' . $biblio_count);
210 my @locs = $input->multi_param('loc_' . $biblio_count);
211 my @ccodes = $input->multi_param('ccode_' . $biblio_count);
212 my @notforloans = $input->multi_param('notforloan_' . $biblio_count);
213 my @uris = $input->multi_param('uri_' . $biblio_count);
214 my @copynos = $input->multi_param('copyno_' . $biblio_count);
215 my @budget_codes = $input->multi_param('budget_code_' . $biblio_count);
216 my @itemprices = $input->multi_param('itemprice_' . $biblio_count);
217 my @replacementprices = $input->multi_param('replacementprice_' . $biblio_count);
218 my @itemcallnumbers = $input->multi_param('itemcallnumber_' . $biblio_count);
219 my $itemcreation = 0;
222 for (my $i = 0; $i < $count; $i++) {
224 my $item = Koha::Item->new(
226 biblionumber => $biblionumber,
227 homebranch => $homebranches[$i],
228 holdingbranch => $holdingbranches[$i],
229 itemnotes_nonpublic => $nonpublic_notes[$i],
230 itemnotes => $public_notes[$i],
231 location => $locs[$i],
232 ccode => $ccodes[$i],
233 itype => $itypes[$i],
234 notforloan => $notforloans[$i],
236 copynumber => $copynos[$i],
237 price => $itemprices[$i],
238 replacementprice => $replacementprices[$i],
239 itemcallnumber => $itemcallnumbers[$i],
242 push( @itemnumbers, $item->itemnumber );
244 if ($itemcreation == 1) {
245 # Group orderlines from MarcItemFieldsToOrder
247 for (my $i = 0; $i < $count; $i++) {
248 $budget_hash->{$budget_codes[$i]}->{quantity} += 1;
249 $budget_hash->{$budget_codes[$i]}->{price} = $itemprices[$i];
250 $budget_hash->{$budget_codes[$i]}->{replacementprice} = $replacementprices[$i];
251 $budget_hash->{$budget_codes[$i]}->{itemnumbers} //= [];
252 push @{ $budget_hash->{$budget_codes[$i]}->{itemnumbers} }, $itemnumbers[$i];
255 # Create orderlines from MarcItemFieldsToOrder
256 while(my ($budget_id, $infos) = each %$budget_hash) {
259 biblionumber => $biblionumber,
260 basketno => $cgiparams->{'basketno'},
261 quantity => $infos->{quantity},
262 budget_id => $budget_id,
263 currency => $cgiparams->{'all_currency'},
266 my $price = $infos->{price};
268 # in France, the cents separator is the , but sometimes, ppl use a .
269 # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
270 $price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
271 $price = Koha::Number::Price->new($price)->unformat;
272 $orderinfo{tax_rate} = $bookseller->tax_rate;
273 my $c = $c_discount ? $c_discount : $bookseller->discount / 100;
274 $orderinfo{discount} = $c;
276 $orderinfo{ecost} = $price;
277 $orderinfo{rrp} = $orderinfo{ecost} / ( 1 - $c );
279 $orderinfo{ecost} = $price * ( 1 - $c );
280 $orderinfo{rrp} = $price;
282 $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
283 $orderinfo{unitprice} = $orderinfo{ecost};
284 $orderinfo{total} = $orderinfo{ecost} * $infos->{quantity};
286 $orderinfo{listprice} = 0;
288 $orderinfo{replacementprice} = $infos->{replacementprice} || 0;
290 # remove uncertainprice flag if we have found a price in the MARC record
291 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
294 C4::Acquisition::populate_order_with_prices(
296 order => \%orderinfo,
297 booksellerid => $booksellerid,
304 my $order = Koha::Acquisition::Order->new( \%orderinfo )->store;
305 $order->add_item( $_ ) for @{ $budget_hash->{$budget_id}->{itemnumbers} };
310 my $patron = Koha::Patrons->find( $loggedinuser );
311 # get quantity in the MARC record (1 if none)
312 my $quantity = GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour')) || 1;
314 biblionumber => $biblionumber,
315 basketno => $cgiparams->{'basketno'},
316 quantity => $c_quantity,
317 branchcode => $patron->branchcode,
318 budget_id => $c_budget_id,
322 order_internalnote => $cgiparams->{'all_order_internalnote'},
323 order_vendornote => $cgiparams->{'all_order_vendornote'},
324 currency => $cgiparams->{'all_currency'},
325 replacementprice => shift( @orderreplacementprices ),
327 # get the price if there is one.
328 my $price= shift( @prices ) || GetMarcPrice($marcrecord, C4::Context->preference('marcflavour'));
330 # in France, the cents separator is the , but sometimes, ppl use a .
331 # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
332 $price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
333 $price = Koha::Number::Price->new($price)->unformat;
334 $orderinfo{tax_rate} = $bookseller->tax_rate;
335 my $c = $c_discount ? $c_discount : $bookseller->discount / 100;
336 $orderinfo{discount} = $c;
338 $orderinfo{ecost} = $price;
339 $orderinfo{rrp} = $orderinfo{ecost} / ( 1 - $c );
341 $orderinfo{ecost} = $price * ( 1 - $c );
342 $orderinfo{rrp} = $price;
344 $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
345 $orderinfo{unitprice} = $orderinfo{ecost};
346 $orderinfo{total} = $orderinfo{ecost} * $c_quantity;
348 $orderinfo{listprice} = 0;
351 # remove uncertainprice flag if we have found a price in the MARC record
352 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
355 C4::Acquisition::populate_order_with_prices(
357 order => \%orderinfo,
358 booksellerid => $booksellerid,
365 my $order = Koha::Acquisition::Order->new( \%orderinfo )->store;
367 # 4th, add items if applicable
368 # parse the item sent by the form, and create an item just for the import_record_id we are dealing with
369 # this is not optimised, but it's working !
370 if ( $basket->effective_create_items eq 'ordering' && !$basket->is_standing ) {
371 my @tags = $input->multi_param('tag');
372 my @subfields = $input->multi_param('subfield');
373 my @field_values = $input->multi_param('field_value');
374 my @serials = $input->multi_param('serial');
375 my @ind_tag = $input->multi_param('ind_tag');
376 my @indicator = $input->multi_param('indicator');
378 push @{ $item->{tags} }, $tags[0];
379 push @{ $item->{subfields} }, $subfields[0];
380 push @{ $item->{field_values} }, $field_values[0];
381 push @{ $item->{ind_tag} }, $ind_tag[0];
382 push @{ $item->{indicator} }, $indicator[0];
383 my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values, \@indicator, \@ind_tag );
384 my $record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
385 for (my $qtyloop=1;$qtyloop <= $c_quantity;$qtyloop++) {
386 my ( $biblionumber, $bibitemnum, $itemnumber ) = AddItemFromMarc( $record, $biblionumber );
387 $order->add_item( $itemnumber );
390 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
396 # If all bibliographic records from the batch have been imported we modifying the status of the batch accordingly
397 SetImportBatchStatus( $import_batch_id, 'imported' )
398 if @{ GetImportRecordsRange( $import_batch_id, undef, undef, 'imported' )}
399 == @{ GetImportRecordsRange( $import_batch_id )};
403 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=".$cgiparams->{'basketno'}."&duplinbatch=$duplinbatch");
405 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=".$cgiparams->{'basketno'}."&booksellerid=$booksellerid&allmatch=1");
410 my $budgets = GetBudgets();
411 my $budget_id = @$budgets[0]->{'budget_id'};
412 # build bookfund list
413 my $patron = Koha::Patrons->find( $loggedinuser )->unblessed;
414 my $budget = GetBudget($budget_id);
417 my $budget_loop = [];
418 my $budgets_hierarchy = GetBudgetHierarchy;
419 foreach my $r ( @{$budgets_hierarchy} ) {
420 next unless (CanUserUseBudget($patron, $r, $userflags));
421 push @{$budget_loop},
422 { b_id => $r->{budget_id},
423 b_txt => $r->{budget_name},
424 b_code => $r->{budget_code},
425 b_sort1_authcat => $r->{'sort1_authcat'},
426 b_sort2_authcat => $r->{'sort2_authcat'},
427 b_active => $r->{budget_period_active},
428 b_sel => ( $r->{budget_id} == $budget_id ) ? 1 : 0,
433 sort { uc( $a->{b_txt}) cmp uc( $b->{b_txt}) } @{$budget_loop};
435 $template->param( budget_loop => $budget_loop,);
437 output_html_with_http_headers $input, $cookie, $template->output;
440 sub import_batches_list {
442 my $batches = GetImportBatchRangeDesc();
445 foreach my $batch (@$batches) {
446 if ( $batch->{'import_status'} =~ /^staged$|^reverted$/ && $batch->{'record_type'} eq 'biblio') {
447 # check if there is at least 1 line still staged
448 my $stagedList=GetImportRecordsRange($batch->{'import_batch_id'}, undef, 1, $batch->{import_status}, { order_by_direction => 'ASC' });
449 if (scalar @$stagedList) {
451 import_batch_id => $batch->{'import_batch_id'},
452 num_records => $batch->{'num_records'},
453 num_items => $batch->{'num_items'},
454 staged_date => $batch->{'upload_timestamp'},
455 import_status => $batch->{'import_status'},
456 file_name => $batch->{'file_name'},
457 comments => $batch->{'comments'},
460 # if there are no more line to includes, set the status to imported
461 # FIXME This should be removed in the future.
462 SetImportBatchStatus( $batch->{'import_batch_id'}, 'imported' );
466 $template->param(batch_list => \@list);
467 my $num_batches = GetNumberOfNonZ3950ImportBatches();
468 $template->param(num_results => $num_batches);
471 sub import_biblios_list {
472 my ($template, $import_batch_id) = @_;
473 my $batch = GetImportBatch($import_batch_id,'staged');
474 return () unless $batch and $batch->{import_status} =~ /^staged$|^reverted$/;
475 my $biblios = GetImportRecordsRange($import_batch_id,'','',$batch->{import_status});
479 my $ccodes = { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' } ) };
480 my $locations = { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) };
481 my $notforloans = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.notforloan' } ) };
484 foreach (sort keys %$locations) {
485 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
488 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
489 push @ccodes, { code => $_, description => $ccodes->{$_} };
492 foreach (sort {$notforloans->{$a} cmp $notforloans->{$b}} keys %$notforloans) {
493 push @notforloans, { code => $_, description => $notforloans->{$_} };
496 my $biblio_count = 0;
497 foreach my $biblio (@$biblios) {
500 my $citation = $biblio->{'title'};
501 $citation .= " $biblio->{'author'}" if $biblio->{'author'};
502 $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
503 $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
504 $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
505 $citation .= $biblio->{'issn'} if $biblio->{'issn'};
506 $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
507 my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
509 import_record_id => $biblio->{'import_record_id'},
510 citation => $citation,
512 status => $biblio->{'status'},
513 record_sequence => $biblio->{'record_sequence'},
514 overlay_status => $biblio->{'overlay_status'},
515 match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
516 match_citation => $#$match > -1 ? $match->[0]->{'title'} || '' . ' ' . $match->[0]->{'author'} || '': '',
517 match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
519 my ( $marcblob, $encoding ) = GetImportRecordMarc( $biblio->{'import_record_id'} );
520 my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
522 my $infos = get_infos_syspref('MarcFieldsToOrder', $marcrecord, ['price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2','replacementprice']);
523 my $price = $infos->{price};
524 my $replacementprice = $infos->{replacementprice};
525 my $quantity = $infos->{quantity};
526 my $budget_code = $infos->{budget_code};
527 my $discount = $infos->{discount};
528 my $sort1 = $infos->{sort1};
529 my $sort2 = $infos->{sort2};
532 my $biblio_budget = GetBudgetByCode($budget_code);
534 $budget_id = $biblio_budget->{budget_id};
540 my $all_items_quantity = 0;
541 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']);
542 if ($alliteminfos != -1) {
543 foreach my $iteminfos (@$alliteminfos) {
544 my $item_homebranch = $iteminfos->{homebranch};
545 my $item_holdingbranch = $iteminfos->{holdingbranch};
546 my $item_itype = $iteminfos->{itype};
547 my $item_nonpublic_note = $iteminfos->{nonpublic_note};
548 my $item_public_note = $iteminfos->{public_note};
549 my $item_loc = $iteminfos->{loc};
550 my $item_ccode = $iteminfos->{ccode};
551 my $item_notforloan = $iteminfos->{notforloan};
552 my $item_uri = $iteminfos->{uri};
553 my $item_copyno = $iteminfos->{copyno};
554 my $item_quantity = $iteminfos->{quantity} || 1;
555 my $item_budget_code = $iteminfos->{budget_code};
557 if ( $iteminfos->{budget_code} ) {
558 my $item_budget = GetBudgetByCode( $iteminfos->{budget_code} );
559 if ( $item_budget ) {
560 $item_budget_id = $item_budget->{budget_id};
563 my $item_price = $iteminfos->{price};
564 my $item_replacement_price = $iteminfos->{replacementprice};
565 my $item_callnumber = $iteminfos->{itemcallnumber};
567 for (my $i = 0; $i < $item_quantity; $i++) {
570 'item_id' => $item_id++,
571 'biblio_count' => $biblio_count,
572 'homebranch' => $item_homebranch,
573 'holdingbranch' => $item_holdingbranch,
574 'itype' => $item_itype,
575 'nonpublic_note' => $item_nonpublic_note,
576 'public_note' => $item_public_note,
578 'ccode' => $item_ccode,
579 'notforloan' => $item_notforloan,
581 'copyno' => $item_copyno,
582 'quantity' => $item_quantity,
583 'budget_id' => $item_budget_id || $budget_id,
584 'itemprice' => $item_price || $price,
585 'replacementprice' => $item_replacement_price || $replacementprice,
586 'itemcallnumber' => $item_callnumber,
588 $all_items_quantity++;
589 push @itemlist, \%itemrecord;
594 $cellrecord{'iteminfos'} = \@itemlist;
596 $cellrecord{'item_error'} = 1;
598 push @list, \%cellrecord;
600 if ($alliteminfos == -1 || scalar(@$alliteminfos) == 0) {
601 $cellrecord{price} = $price || '';
602 $cellrecord{replacementprice} = $replacementprice || '';
603 $cellrecord{quantity} = $quantity || '';
604 $cellrecord{budget_id} = $budget_id || '';
605 $cellrecord{discount} = $discount || '';
606 $cellrecord{sort1} = $sort1 || '';
607 $cellrecord{sort2} = $sort2 || '';
609 $cellrecord{quantity} = $all_items_quantity;
613 my $num_records = $batch->{'num_records'};
614 my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
615 my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
616 my $item_action = GetImportBatchItemAction($import_batch_id);
617 my @itypes = Koha::ItemTypes->search;
618 $template->param(biblio_list => \@list,
619 num_results => $num_records,
620 import_batch_id => $import_batch_id,
621 "overlay_action_${overlay_action}" => 1,
622 overlay_action => $overlay_action,
623 "nomatch_action_${nomatch_action}" => 1,
624 nomatch_action => $nomatch_action,
625 "item_action_${item_action}" => 1,
626 item_action => $item_action,
627 item_error => $item_error,
628 libraries => scalar Koha::Libraries->search(),
629 locationloop => \@locations,
630 itypeloop => \@itypes,
631 ccodeloop => \@ccodes,
632 notforloanloop => \@notforloans,
634 batch_info($template, $batch);
638 my ($template, $batch) = @_;
639 $template->param(batch_info => 1,
640 file_name => $batch->{'file_name'},
641 comments => $batch->{'comments'},
642 import_status => $batch->{'import_status'},
643 upload_timestamp => $batch->{'upload_timestamp'},
644 num_records => $batch->{'num_records'},
645 num_items => $batch->{'num_items'});
646 if ($batch->{'num_records'} > 0) {
647 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
648 $template->param(can_commit => 1);
650 if ($batch->{'import_status'} eq 'imported') {
651 $template->param(can_revert => 1);
654 if (defined $batch->{'matcher_id'}) {
655 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
656 if (defined $matcher) {
657 $template->param('current_matcher_id' => $batch->{'matcher_id'},
658 'current_matcher_code' => $matcher->code(),
659 'current_matcher_description' => $matcher->description());
662 add_matcher_list($batch->{'matcher_id'}, $template);
665 sub add_matcher_list {
666 my ($current_matcher_id, $template) = @_;
667 my @matchers = C4::Matcher::GetMatcherList();
668 if (defined $current_matcher_id) {
669 for (my $i = 0; $i <= $#matchers; $i++) {
670 if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
671 $matchers[$i]->{'selected'} = 1;
675 $template->param(available_matchers => \@matchers);
678 sub get_infos_syspref {
679 my ($syspref_name, $record, $field_list) = @_;
680 my $syspref = C4::Context->preference($syspref_name);
681 $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
683 YAML::Load($syspref);
686 warn "Unable to parse $syspref syspref : $@";
690 for my $field_name ( @$field_list ) {
691 next unless exists $yaml->{$field_name};
692 my @fields = split /\|/, $yaml->{$field_name};
693 for my $field ( @fields ) {
694 my ( $f, $sf ) = split /\$/, $field;
695 next unless $f and $sf;
696 if ( my $v = $record->subfield( $f, $sf ) ) {
697 $r->{$field_name} = $v;
699 last if $yaml->{$field};
705 sub equal_number_of_fields {
706 my ($tags_list, $record) = @_;
707 my $tag_fields_count;
708 for my $tag (@$tags_list) {
709 my @fields = $record->field($tag);
710 $tag_fields_count->{$tag} = scalar @fields;
714 foreach my $key ( keys %$tag_fields_count ) {
715 if ( $tag_fields_count->{$key} > 0 ) { # Having 0 of a field is ok
716 $tags_count //= $tag_fields_count->{$key}; # Start with the count from the first occurrence
717 return -1 if $tag_fields_count->{$key} != $tags_count; # All counts of various fields should be equal if they exist
724 sub get_infos_syspref_on_item {
725 my ($syspref_name, $record, $field_list) = @_;
726 my $syspref = C4::Context->preference($syspref_name);
727 $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
729 YAML::Load($syspref);
732 warn "Unable to parse $syspref syspref : $@";
738 # Check tags in syspref definition
739 for my $field_name ( @$field_list ) {
740 next unless exists $yaml->{$field_name};
741 my @fields = split /\|/, $yaml->{$field_name};
742 for my $field ( @fields ) {
743 my ( $f, $sf ) = split /\$/, $field;
744 next unless $f and $sf;
748 @tags_list = List::MoreUtils::uniq(@tags_list);
750 my $tags_count = equal_number_of_fields(\@tags_list, $record);
751 # Return if the number of these fields in the record is not the same.
752 return -1 if $tags_count == -1;
756 foreach my $tag (@tags_list) {
758 foreach my $field ($record->field($tag)) {
759 push @tmp_fields, $field;
761 $fields_hash->{$tag} = \@tmp_fields;
764 for (my $i = 0; $i < $tags_count; $i++) {
766 for my $field_name ( @$field_list ) {
767 next unless exists $yaml->{$field_name};
768 my @fields = split /\|/, $yaml->{$field_name};
769 for my $field ( @fields ) {
770 my ( $f, $sf ) = split /\$/, $field;
771 next unless $f and $sf;
772 my $v = $fields_hash->{$f}[$i] ? $fields_hash->{$f}[$i]->subfield( $sf ) : undef;
773 $r->{$field_name} = $v if (defined $v);
774 last if $yaml->{$field};