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( 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;
54 use Koha::ImportBatches;
55 use Koha::Import::Records;
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
100 $template->param("batch_details" => 1,
101 "basketno" => $cgiparams->{'basketno'},
102 # get currencies (for change rates calcs if needed)
103 currencies => Koha::Acquisition::Currencies->search,
104 bookseller => $bookseller,
105 "allmatch" => $allmatch,
107 import_biblios_list($template, $cgiparams->{'import_batch_id'});
108 if ( $basket->effective_create_items eq 'ordering' && !$basket->is_standing ) {
109 # prepare empty item form
110 my $cell = PrepareItemrecordDisplay( '', '', undef, 'ACQ' );
112 # warn "==> ".Data::Dumper::Dumper($cell);
114 $cell = PrepareItemrecordDisplay( '', '', undef, '' );
115 $template->param( 'NoACQframework' => 1 );
118 push @itemloop, $cell;
120 $template->param( items => \@itemloop );
123 # 3rd step = import the records
125 } elsif ( $op eq 'import_records' ) {
126 #import selected lines
127 $template->param('basketno' => $cgiparams->{'basketno'});
128 # Budget_id is mandatory for adding an order, we just add a default, the user needs to modify this aftewards
129 my $budgets = GetBudgets();
130 if (scalar @$budgets == 0){
131 die "No budgets defined, can't continue";
133 my $budget_id = @$budgets[0]->{'budget_id'};
134 #get all records from a batch, and check their import status to see if they are checked.
135 #(default values: quantity 1, uncertainprice yes, first budget)
137 # retrieve the file you want to import
138 my $import_batch_id = $cgiparams->{'import_batch_id'};
139 my $import_batch = Koha::ImportBatches->find( $import_batch_id );
140 my $overlay_action = $import_batch->overlay_action;
141 my $import_records = Koha::Import::Records->search({
142 import_batch_id => $import_batch_id,
146 my @import_record_id_selected = $input->multi_param("import_record_id");
147 my @quantities = $input->multi_param('quantity');
148 my @prices = $input->multi_param('price');
149 my @orderreplacementprices = $input->multi_param('replacementprice');
150 my @budgets_id = $input->multi_param('budget_id');
151 my @discount = $input->multi_param('discount');
152 my @sort1 = $input->multi_param('sort1');
153 my @sort2 = $input->multi_param('sort2');
154 my $matcher_id = $input->param('matcher_id');
155 my $active_currency = Koha::Acquisition::Currencies->get_active;
156 my $biblio_count = 0;
157 while( my $import_record = $import_records->next ){
160 # Check if this import_record_id was selected
161 next if not grep { $_ eq $import_record->import_record_id } @import_record_id_selected;
162 my $marcrecord = $import_record->get_marc_record || die "couldn't translate marc information";
163 my $matches = $import_record->get_import_record_matches({ chosen => 1 });
164 my $match = $matches->count ? $matches->next : undef;
165 my $biblionumber = $match ? $match->candidate_match_id : 0;
166 my $c_quantity = shift( @quantities ) || GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour') ) || 1;
167 my $c_budget_id = shift( @budgets_id ) || $input->param('all_budget_id') || $budget_id;
168 my $c_discount = shift ( @discount);
169 my $c_sort1 = shift( @sort1 ) || $input->param('all_sort1') || '';
170 my $c_sort2 = shift( @sort2 ) || $input->param('all_sort2') || '';
171 my $c_replacement_price = shift( @orderreplacementprices );
172 my $c_price = shift( @prices ) || GetMarcPrice($marcrecord, C4::Context->preference('marcflavour'));
174 # Insert the biblio, or find it through matcher
175 if ( $biblionumber ) { # If matched during staging we can continue
176 $import_record->status('imported')->store;
177 if( $overlay_action eq 'replace' ){
178 my $biblio = Koha::Biblios->find( $biblionumber );
179 $import_record->replace({ biblio => $biblio });
181 } else { # Otherwise we check for duplicates, and skip if they exist
183 if ( $matcher_id eq '_TITLE_AUTHOR_' ) {
184 $duplifound = 1 if FindDuplicate($marcrecord);
187 my $matcher = C4::Matcher->fetch($matcher_id);
188 my @matches = $matcher->get_matches( $marcrecord, my $max_matches = 1 );
189 $duplifound = 1 if @matches;
192 $duplinbatch = $import_batch_id and next if $duplifound;
195 # remove hyphens (-) from ISBN
196 # FIXME: This should probably be optional
197 my ( $isbnfield, $isbnsubfield ) = GetMarcFromKohaField( 'biblioitems.isbn' );
198 if ( $marcrecord->field($isbnfield) ) {
199 foreach my $field ( $marcrecord->field($isbnfield) ) {
200 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
201 my $newisbn = $field->subfield($isbnsubfield);
203 $field->update( $isbnsubfield => $newisbn );
209 ( $biblionumber, undef ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' );
210 $import_record->status('imported')->store;
213 $import_record->import_biblio->matched_biblionumber($biblionumber)->store;
215 # Add items from MarcItemFieldsToOrder
216 my @homebranches = $input->multi_param('homebranch_' . $import_record->import_record_id);
217 my $count = scalar @homebranches;
218 my @holdingbranches = $input->multi_param('holdingbranch_' . $import_record->import_record_id);
219 my @itypes = $input->multi_param('itype_' . $import_record->import_record_id);
220 my @nonpublic_notes = $input->multi_param('nonpublic_note_' . $import_record->import_record_id);
221 my @public_notes = $input->multi_param('public_note_' . $import_record->import_record_id);
222 my @locs = $input->multi_param('loc_' . $import_record->import_record_id);
223 my @ccodes = $input->multi_param('ccode_' . $import_record->import_record_id);
224 my @notforloans = $input->multi_param('notforloan_' . $import_record->import_record_id);
225 my @uris = $input->multi_param('uri_' . $import_record->import_record_id);
226 my @copynos = $input->multi_param('copyno_' . $import_record->import_record_id);
227 my @budget_codes = $input->multi_param('budget_code_' . $import_record->import_record_id);
228 my @itemprices = $input->multi_param('itemprice_' . $import_record->import_record_id);
229 my @replacementprices = $input->multi_param('replacementprice_' . $import_record->import_record_id);
230 my @itemcallnumbers = $input->multi_param('itemcallnumber_' . $import_record->import_record_id);
231 my $itemcreation = 0;
234 for (my $i = 0; $i < $count; $i++) {
236 my $item = Koha::Item->new(
238 biblionumber => $biblionumber,
239 homebranch => $homebranches[$i],
240 holdingbranch => $holdingbranches[$i],
241 itemnotes_nonpublic => $nonpublic_notes[$i],
242 itemnotes => $public_notes[$i],
243 location => $locs[$i],
244 ccode => $ccodes[$i],
245 itype => $itypes[$i],
246 notforloan => $notforloans[$i],
248 copynumber => $copynos[$i],
249 price => $itemprices[$i],
250 replacementprice => $replacementprices[$i],
251 itemcallnumber => $itemcallnumbers[$i],
254 push( @itemnumbers, $item->itemnumber );
256 if ($itemcreation == 1) {
257 # Group orderlines from MarcItemFieldsToOrder
259 for (my $i = 0; $i < $count; $i++) {
260 $budget_hash->{$budget_codes[$i]}->{quantity} += 1;
261 $budget_hash->{$budget_codes[$i]}->{price} = $itemprices[$i];
262 $budget_hash->{$budget_codes[$i]}->{replacementprice} = $replacementprices[$i];
263 $budget_hash->{$budget_codes[$i]}->{itemnumbers} //= [];
264 push @{ $budget_hash->{$budget_codes[$i]}->{itemnumbers} }, $itemnumbers[$i];
267 # Create orderlines from MarcItemFieldsToOrder
268 while(my ($budget_id, $infos) = each %$budget_hash) {
271 biblionumber => $biblionumber,
272 basketno => $cgiparams->{'basketno'},
273 quantity => $infos->{quantity},
274 budget_id => $budget_id,
275 currency => $cgiparams->{'all_currency'},
278 my $price = $infos->{price};
280 # in France, the cents separator is the , but sometimes, ppl use a .
281 # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
282 $price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
283 $price = Koha::Number::Price->new($price)->unformat;
284 $orderinfo{tax_rate_on_ordering} = $bookseller->tax_rate;
285 $orderinfo{tax_rate_on_receiving} = $bookseller->tax_rate;
286 my $order_discount = $c_discount ? $c_discount : $bookseller->discount;
287 $orderinfo{discount} = $order_discount;
288 $orderinfo{rrp} = $price;
289 $orderinfo{ecost} = $order_discount ? $price * ( 1 - $order_discount / 100 ) : $price;
290 $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
291 $orderinfo{unitprice} = $orderinfo{ecost};
293 $orderinfo{listprice} = 0;
295 $orderinfo{replacementprice} = $infos->{replacementprice} || 0;
297 # remove uncertainprice flag if we have found a price in the MARC record
298 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
300 my $order = Koha::Acquisition::Order->new( \%orderinfo );
301 $order->populate_with_prices_for_ordering();
302 $order->populate_with_prices_for_receiving();
304 $order->add_item( $_ ) for @{ $budget_hash->{$budget_id}->{itemnumbers} };
309 my $patron = Koha::Patrons->find( $loggedinuser );
310 # get quantity in the MARC record (1 if none)
311 my $quantity = GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour')) || 1;
313 biblionumber => $biblionumber,
314 basketno => $cgiparams->{'basketno'},
315 quantity => $c_quantity,
316 branchcode => $patron->branchcode,
317 budget_id => $c_budget_id,
321 order_internalnote => $cgiparams->{'all_order_internalnote'},
322 order_vendornote => $cgiparams->{'all_order_vendornote'},
323 currency => $cgiparams->{'all_currency'},
324 replacementprice => $c_replacement_price,
326 # get the price if there is one.
328 # in France, the cents separator is the , but sometimes, ppl use a .
329 # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
330 $c_price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
331 $c_price = Koha::Number::Price->new($c_price)->unformat;
332 $orderinfo{tax_rate_on_ordering} = $bookseller->tax_rate;
333 $orderinfo{tax_rate_on_receiving} = $bookseller->tax_rate;
334 my $order_discount = $c_discount ? $c_discount : $bookseller->discount;
335 $orderinfo{discount} = $order_discount;
336 $orderinfo{rrp} = $c_price;
337 $orderinfo{ecost} = $order_discount ? $c_price * ( 1 - $order_discount / 100 ) : $c_price;
338 $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
339 $orderinfo{unitprice} = $orderinfo{ecost};
341 $orderinfo{listprice} = 0;
344 # remove uncertainprice flag if we have found a price in the MARC record
345 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
347 my $order = Koha::Acquisition::Order->new( \%orderinfo );
348 $order->populate_with_prices_for_ordering();
349 $order->populate_with_prices_for_receiving();
352 # 4th, add items if applicable
353 # parse the item sent by the form, and create an item just for the import_record_id we are dealing with
354 # this is not optimised, but it's working !
355 if ( $basket->effective_create_items eq 'ordering' && !$basket->is_standing ) {
356 my @tags = $input->multi_param('tag');
357 my @subfields = $input->multi_param('subfield');
358 my @field_values = $input->multi_param('field_value');
359 my @serials = $input->multi_param('serial');
360 my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values );
361 my $record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
362 for (my $qtyloop=1;$qtyloop <= $c_quantity;$qtyloop++) {
363 my ( $biblionumber, undef, $itemnumber ) = AddItemFromMarc( $record, $biblionumber );
364 $order->add_item( $itemnumber );
371 # If all bibliographic records from the batch have been imported we modifying the status of the batch accordingly
372 SetImportBatchStatus( $import_batch_id, 'imported' )
373 if Koha::Import::Records->search({import_batch_id => $import_batch_id, status => 'imported' })->count
374 == Koha::Import::Records->search({import_batch_id => $import_batch_id})->count;
378 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=".$cgiparams->{'basketno'}."&duplinbatch=$duplinbatch");
380 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=".$cgiparams->{'basketno'}."&booksellerid=$booksellerid&allmatch=1");
385 my $budgets = GetBudgets();
386 my $budget_id = @$budgets[0]->{'budget_id'};
387 # build bookfund list
388 my $patron = Koha::Patrons->find( $loggedinuser )->unblessed;
389 my $budget = GetBudget($budget_id);
392 my $budget_loop = [];
393 my $budgets_hierarchy = GetBudgetHierarchy;
394 foreach my $r ( @{$budgets_hierarchy} ) {
395 next unless (CanUserUseBudget($patron, $r, $userflags));
396 push @{$budget_loop},
397 { b_id => $r->{budget_id},
398 b_txt => $r->{budget_name},
399 b_code => $r->{budget_code},
400 b_sort1_authcat => $r->{'sort1_authcat'},
401 b_sort2_authcat => $r->{'sort2_authcat'},
402 b_active => $r->{budget_period_active},
403 b_sel => ( $r->{budget_id} == $budget_id ) ? 1 : 0,
408 sort { uc( $a->{b_txt}) cmp uc( $b->{b_txt}) } @{$budget_loop};
410 $template->param( budget_loop => $budget_loop,);
412 output_html_with_http_headers $input, $cookie, $template->output;
415 sub import_batches_list {
417 my $batches = GetImportBatchRangeDesc();
420 foreach my $batch (@$batches) {
421 if ( $batch->{'import_status'} =~ /^staged$|^reverted$/ && $batch->{'record_type'} eq 'biblio') {
422 # check if there is at least 1 line still staged
423 my $import_records_count = Koha::Import::Records->search({
424 import_batch_id => $batch->{'import_batch_id'},
425 status => $batch->{import_status}
427 if ( $import_records_count ) {
429 import_batch_id => $batch->{'import_batch_id'},
430 num_records => $batch->{'num_records'},
431 num_items => $batch->{'num_items'},
432 staged_date => $batch->{'upload_timestamp'},
433 import_status => $batch->{'import_status'},
434 file_name => $batch->{'file_name'},
435 comments => $batch->{'comments'},
438 # if there are no more line to includes, set the status to imported
439 # FIXME This should be removed in the future.
440 SetImportBatchStatus( $batch->{'import_batch_id'}, 'imported' );
444 $template->param(batch_list => \@list);
445 my $num_batches = GetNumberOfNonZ3950ImportBatches();
446 $template->param(num_results => $num_batches);
449 sub import_biblios_list {
450 my ($template, $import_batch_id) = @_;
451 my $batch = GetImportBatch($import_batch_id,'staged');
452 return () unless $batch and $batch->{import_status} =~ /^staged$|^reverted$/;
453 my $import_records = Koha::Import::Records->search({
454 import_batch_id => $import_batch_id,
455 status => $batch->{import_status}
460 my $ccodes = { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' } ) };
461 my $locations = { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) };
462 my $notforloans = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.notforloan' } ) };
465 foreach (sort keys %$locations) {
466 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
469 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
470 push @ccodes, { code => $_, description => $ccodes->{$_} };
473 foreach (sort {$notforloans->{$a} cmp $notforloans->{$b}} keys %$notforloans) {
474 push @notforloans, { code => $_, description => $notforloans->{$_} };
477 my $biblio_count = 0;
478 while ( my $import_record = $import_records->next ) {
481 my $matches = $import_record->get_import_record_matches({ chosen => 1 });
482 my $match = $matches->count ? $matches->next : undef;
483 my $match_biblio = $match ? Koha::Biblios->find({ biblionumber => $match->candidate_match_id }) : undef;
485 import_record_id => $import_record->import_record_id,
486 import_biblio => $import_record->import_biblio,
488 status => $import_record->status,
489 record_sequence => $import_record->record_sequence,
490 overlay_status => $import_record->overlay_status,
491 match_biblionumber => $match ? $match->candidate_match_id : 0,
492 match_citation => $match_biblio ? ($match_biblio->title || '') . ' ' .( $match_biblio->author || ''): '',
493 match_score => $match ? $match->score : 0,
495 my $marcrecord = $import_record->get_marc_record || die "couldn't translate marc information";
497 my $infos = get_infos_syspref('MarcFieldsToOrder', $marcrecord, ['price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2','replacementprice']);
498 my $price = $infos->{price};
499 my $replacementprice = $infos->{replacementprice};
500 my $quantity = $infos->{quantity};
501 my $budget_code = $infos->{budget_code};
502 my $discount = $infos->{discount};
503 my $sort1 = $infos->{sort1};
504 my $sort2 = $infos->{sort2};
507 my $biblio_budget = GetBudgetByCode($budget_code);
509 $budget_id = $biblio_budget->{budget_id};
515 my $all_items_quantity = 0;
516 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']);
517 if ($alliteminfos != -1) {
518 foreach my $iteminfos (@$alliteminfos) {
519 my $item_homebranch = $iteminfos->{homebranch};
520 my $item_holdingbranch = $iteminfos->{holdingbranch};
521 my $item_itype = $iteminfos->{itype};
522 my $item_nonpublic_note = $iteminfos->{nonpublic_note};
523 my $item_public_note = $iteminfos->{public_note};
524 my $item_loc = $iteminfos->{loc};
525 my $item_ccode = $iteminfos->{ccode};
526 my $item_notforloan = $iteminfos->{notforloan};
527 my $item_uri = $iteminfos->{uri};
528 my $item_copyno = $iteminfos->{copyno};
529 my $item_quantity = $iteminfos->{quantity} || 1;
530 my $item_budget_code = $iteminfos->{budget_code};
532 if ( $iteminfos->{budget_code} ) {
533 my $item_budget = GetBudgetByCode( $iteminfos->{budget_code} );
534 if ( $item_budget ) {
535 $item_budget_id = $item_budget->{budget_id};
538 my $item_price = $iteminfos->{price};
539 my $item_replacement_price = $iteminfos->{replacementprice};
540 my $item_callnumber = $iteminfos->{itemcallnumber};
542 for (my $i = 0; $i < $item_quantity; $i++) {
545 'item_id' => $item_id++,
546 'biblio_count' => $biblio_count,
547 'homebranch' => $item_homebranch,
548 'holdingbranch' => $item_holdingbranch,
549 'itype' => $item_itype,
550 'nonpublic_note' => $item_nonpublic_note,
551 'public_note' => $item_public_note,
553 'ccode' => $item_ccode,
554 'notforloan' => $item_notforloan,
556 'copyno' => $item_copyno,
557 'quantity' => $item_quantity,
558 'budget_id' => $item_budget_id || $budget_id,
559 'itemprice' => $item_price || $price,
560 'replacementprice' => $item_replacement_price || $replacementprice,
561 'itemcallnumber' => $item_callnumber,
563 $all_items_quantity++;
564 push @itemlist, \%itemrecord;
569 $cellrecord{'iteminfos'} = \@itemlist;
571 $cellrecord{'item_error'} = 1;
573 push @list, \%cellrecord;
575 if ($alliteminfos == -1 || scalar(@$alliteminfos) == 0) {
576 $cellrecord{price} = $price || '';
577 $cellrecord{replacementprice} = $replacementprice || '';
578 $cellrecord{quantity} = $quantity || '';
579 $cellrecord{budget_id} = $budget_id || '';
580 $cellrecord{discount} = $discount || '';
581 $cellrecord{sort1} = $sort1 || '';
582 $cellrecord{sort2} = $sort2 || '';
584 $cellrecord{quantity} = $all_items_quantity;
588 my $num_records = $batch->{'num_records'};
589 my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
590 my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
591 my $item_action = GetImportBatchItemAction($import_batch_id);
592 $template->param(import_biblio_list => \@list,
593 num_results => $num_records,
594 import_batch_id => $import_batch_id,
595 "overlay_action_${overlay_action}" => 1,
596 overlay_action => $overlay_action,
597 "nomatch_action_${nomatch_action}" => 1,
598 nomatch_action => $nomatch_action,
599 "item_action_${item_action}" => 1,
600 item_action => $item_action,
601 item_error => $item_error,
602 libraries => Koha::Libraries->search,
603 locationloop => \@locations,
604 itemtypes => Koha::ItemTypes->search,
605 ccodeloop => \@ccodes,
606 notforloanloop => \@notforloans,
608 batch_info($template, $batch);
612 my ($template, $batch) = @_;
613 $template->param(batch_info => 1,
614 file_name => $batch->{'file_name'},
615 comments => $batch->{'comments'},
616 import_status => $batch->{'import_status'},
617 upload_timestamp => $batch->{'upload_timestamp'},
618 num_records => $batch->{'num_records'},
619 num_items => $batch->{'num_items'});
620 if ($batch->{'num_records'} > 0) {
621 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
622 $template->param(can_commit => 1);
624 if ($batch->{'import_status'} eq 'imported') {
625 $template->param(can_revert => 1);
628 if (defined $batch->{'matcher_id'}) {
629 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
630 if (defined $matcher) {
631 $template->param('current_matcher_id' => $batch->{'matcher_id'},
632 'current_matcher_code' => $matcher->code(),
633 'current_matcher_description' => $matcher->description());
636 add_matcher_list($batch->{'matcher_id'}, $template);
639 sub add_matcher_list {
640 my ($current_matcher_id, $template) = @_;
641 my @matchers = C4::Matcher::GetMatcherList();
642 if (defined $current_matcher_id) {
643 for (my $i = 0; $i <= $#matchers; $i++) {
644 if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
645 $matchers[$i]->{'selected'} = 1;
649 $template->param(available_matchers => \@matchers);
652 sub get_infos_syspref {
653 my ($syspref_name, $record, $field_list) = @_;
654 my $syspref = C4::Context->preference($syspref_name);
655 $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
657 YAML::XS::Load(Encode::encode_utf8($syspref));
660 warn "Unable to parse $syspref syspref : $@";
664 for my $field_name ( @$field_list ) {
665 next unless exists $yaml->{$field_name};
666 my @fields = split /\|/, $yaml->{$field_name};
667 for my $field ( @fields ) {
668 my ( $f, $sf ) = split /\$/, $field;
669 next unless $f and $sf;
670 if ( my $v = $record->subfield( $f, $sf ) ) {
671 $r->{$field_name} = $v;
673 last if $yaml->{$field};
679 sub equal_number_of_fields {
680 my ($tags_list, $record) = @_;
681 my $tag_fields_count;
682 for my $tag (@$tags_list) {
683 my @fields = $record->field($tag);
684 $tag_fields_count->{$tag} = scalar @fields;
688 foreach my $key ( keys %$tag_fields_count ) {
689 if ( $tag_fields_count->{$key} > 0 ) { # Having 0 of a field is ok
690 $tags_count //= $tag_fields_count->{$key}; # Start with the count from the first occurrence
691 return -1 if $tag_fields_count->{$key} != $tags_count; # All counts of various fields should be equal if they exist
698 sub get_infos_syspref_on_item {
699 my ($syspref_name, $record, $field_list) = @_;
700 my $syspref = C4::Context->preference($syspref_name);
701 $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
703 YAML::XS::Load(Encode::encode_utf8($syspref));
706 warn "Unable to parse $syspref syspref : $@";
712 # Check tags in syspref definition
713 for my $field_name ( @$field_list ) {
714 next unless exists $yaml->{$field_name};
715 my @fields = split /\|/, $yaml->{$field_name};
716 for my $field ( @fields ) {
717 my ( $f, $sf ) = split /\$/, $field;
718 next unless $f and $sf;
722 @tags_list = List::MoreUtils::uniq(@tags_list);
724 my $tags_count = equal_number_of_fields(\@tags_list, $record);
725 # Return if the number of these fields in the record is not the same.
726 return -1 if $tags_count == -1;
730 foreach my $tag (@tags_list) {
732 foreach my $field ($record->field($tag)) {
733 push @tmp_fields, $field;
735 $fields_hash->{$tag} = \@tmp_fields;
738 for (my $i = 0; $i < $tags_count; $i++) {
740 for my $field_name ( @$field_list ) {
741 next unless exists $yaml->{$field_name};
742 my @fields = split /\|/, $yaml->{$field_name};
743 for my $field ( @fields ) {
744 my ( $f, $sf ) = split /\$/, $field;
745 next unless $f and $sf;
746 my $v = $fields_hash->{$f}[$i] ? $fields_hash->{$f}[$i]->subfield( $sf ) : undef;
747 $r->{$field_name} = $v if (defined $v);
748 last if $yaml->{$field};