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::Currencies;
48 use Koha::Acquisition::Order;
49 use Koha::Acquisition::Booksellers;
52 my ($template, $loggedinuser, $cookie, $userflags) = get_template_and_user({
53 template_name => "acqui/addorderiso2709.tt",
57 flagsrequired => { acquisition => 'order_manage' },
61 my $cgiparams = $input->Vars;
62 my $op = $cgiparams->{'op'} || '';
63 my $booksellerid = $input->param('booksellerid');
64 my $allmatch = $input->param('allmatch');
65 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
67 $template->param(scriptname => "/cgi-bin/koha/acqui/addorderiso2709.pl",
68 booksellerid => $booksellerid,
69 booksellername => $bookseller->name,
72 if ($cgiparams->{'import_batch_id'} && $op eq ""){
73 $op = "batch_details";
77 if (! $cgiparams->{'basketno'}){
78 die "Basketnumber required to order from iso2709 file import";
82 # 1st step = choose the file to import into acquisition
85 $template->param("basketno" => $cgiparams->{'basketno'});
87 import_batches_list($template);
89 # 2nd step = display the content of the chosen file
91 } elsif ($op eq "batch_details"){
92 #display lines inside the selected batch
93 # get currencies (for change rates calcs if needed)
94 my @currencies = Koha::Acquisition::Currencies->search;
96 $template->param("batch_details" => 1,
97 "basketno" => $cgiparams->{'basketno'},
98 currencies => \@currencies,
99 bookseller => $bookseller,
100 "allmatch" => $allmatch,
102 import_biblios_list($template, $cgiparams->{'import_batch_id'});
103 my $basket = GetBasket($cgiparams->{basketno});
104 if ( C4::Context->preference('AcqCreateItem') eq 'ordering' && !$basket->{is_standing} ) {
105 # prepare empty item form
106 my $cell = PrepareItemrecordDisplay( '', '', '', 'ACQ' );
108 # warn "==> ".Data::Dumper::Dumper($cell);
110 $cell = PrepareItemrecordDisplay( '', '', '', '' );
111 $template->param( 'NoACQframework' => 1 );
114 push @itemloop, $cell;
116 $template->param( items => \@itemloop );
119 # 3rd step = import the records
121 } elsif ( $op eq 'import_records' ) {
122 #import selected lines
123 $template->param('basketno' => $cgiparams->{'basketno'});
124 # Budget_id is mandatory for adding an order, we just add a default, the user needs to modify this aftewards
125 my $budgets = GetBudgets();
126 if (scalar @$budgets == 0){
127 die "No budgets defined, can't continue";
129 my $budget_id = @$budgets[0]->{'budget_id'};
130 #get all records from a batch, and check their import status to see if they are checked.
131 #(default values: quantity 1, uncertainprice yes, first budget)
133 # retrieve the file you want to import
134 my $import_batch_id = $cgiparams->{'import_batch_id'};
135 my $biblios = GetImportRecordsRange($import_batch_id);
138 my @import_record_id_selected = $input->multi_param("import_record_id");
139 my @quantities = $input->multi_param('quantity');
140 my @prices = $input->multi_param('price');
141 my @budgets_id = $input->multi_param('budget_id');
142 my @discount = $input->multi_param('discount');
143 my @sort1 = $input->multi_param('sort1');
144 my @sort2 = $input->multi_param('sort2');
145 my $matcher_id = $input->param('matcher_id');
146 my $active_currency = Koha::Acquisition::Currencies->get_active;
147 my $biblio_count = 0;
148 for my $biblio (@$biblios){
150 # Check if this import_record_id was selected
151 next if not grep { $_ eq $$biblio{import_record_id} } @import_record_id_selected;
152 my ( $marcblob, $encoding ) = GetImportRecordMarc( $biblio->{'import_record_id'} );
153 my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
154 my $match = GetImportRecordMatches( $biblio->{'import_record_id'}, 1 );
155 my $biblionumber=$#$match > -1?$match->[0]->{'biblionumber'}:0;
156 my $c_quantity = shift( @quantities ) || GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour') ) || 1;
157 my $c_budget_id = shift( @budgets_id ) || $input->param('all_budget_id') || $budget_id;
158 my $c_discount = shift ( @discount);
159 $c_discount = $c_discount / 100 if $c_discount > 1;
160 my $c_sort1 = shift( @sort1 ) || $input->param('all_sort1') || '';
161 my $c_sort2 = shift( @sort2 ) || $input->param('all_sort2') || '';
163 # 1st insert the biblio, or find it through matcher
164 unless ( $biblionumber ) {
166 if ( $matcher_id eq '_TITLE_AUTHOR_' ) {
167 $duplinbatch = $import_batch_id if FindDuplicate($marcrecord);
170 my $matcher = C4::Matcher->fetch($matcher_id);
171 my @matches = $matcher->get_matches( $marcrecord, my $max_matches = 1 );
172 $duplinbatch = $import_batch_id if @matches;
175 next if $duplinbatch;
182 my ( $isbnfield, $isbnsubfield ) = GetMarcFromKohaField( 'biblioitems.isbn', '' );
183 if ( $marcrecord->field($isbnfield) ) {
184 foreach my $field ( $marcrecord->field($isbnfield) ) {
185 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
186 my $newisbn = $field->subfield($isbnsubfield);
188 $field->update( $isbnsubfield => $newisbn );
192 ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' );
193 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
194 # 2nd add authorities if applicable
195 if (C4::Context->preference("BiblioAddsAuthorities")){
196 my $headings_linked =BiblioAutoLink($marcrecord, $cgiparams->{'frameworkcode'});
199 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
202 # Add items from MarcItemFieldsToOrder
203 my @homebranches = $input->multi_param('homebranch_' . $biblio_count);
204 my $count = scalar @homebranches;
205 my @holdingbranches = $input->multi_param('holdingbranch_' . $biblio_count);
206 my @itypes = $input->multi_param('itype_' . $biblio_count);
207 my @nonpublic_notes = $input->multi_param('nonpublic_note_' . $biblio_count);
208 my @public_notes = $input->multi_param('public_note_' . $biblio_count);
209 my @locs = $input->multi_param('loc_' . $biblio_count);
210 my @ccodes = $input->multi_param('ccode_' . $biblio_count);
211 my @notforloans = $input->multi_param('notforloan_' . $biblio_count);
212 my @uris = $input->multi_param('uri_' . $biblio_count);
213 my @copynos = $input->multi_param('copyno_' . $biblio_count);
214 my @budget_codes = $input->multi_param('budget_code_' . $biblio_count);
215 my @itemprices = $input->multi_param('itemprice_' . $biblio_count);
216 my @replacementprices = $input->multi_param('replacementprice_' . $biblio_count);
217 my @itemcallnumbers = $input->multi_param('itemcallnumber_' . $biblio_count);
218 my $itemcreation = 0;
219 for (my $i = 0; $i < $count; $i++) {
221 my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({
222 homebranch => $homebranches[$i],
223 holdingbranch => $holdingbranches[$i],
224 itemnotes_nonpublic => $nonpublic_notes[$i],
225 itemnotes => $public_notes[$i],
226 location => $locs[$i],
227 ccode => $ccodes[$i],
228 itype => $itypes[$i],
229 notforloan => $notforloans[$i],
231 copynumber => $copynos[$i],
232 price => $itemprices[$i],
233 replacementprice => $replacementprices[$i],
234 itemcallnumber => $itemcallnumbers[$i],
237 if ($itemcreation == 1) {
238 # Group orderlines from MarcItemFieldsToOrder
240 for (my $i = 0; $i < $count; $i++) {
241 $budget_hash->{$budget_codes[$i]}->{quantity} += 1;
242 $budget_hash->{$budget_codes[$i]}->{price} = $itemprices[$i];
245 # Create orderlines from MarcItemFieldsToOrder
246 while(my ($budget_id, $infos) = each %$budget_hash) {
249 biblionumber => $biblionumber,
250 basketno => $cgiparams->{'basketno'},
251 quantity => $infos->{quantity},
252 budget_id => $budget_id,
253 currency => $cgiparams->{'all_currency'},
256 my $price = $infos->{price};
258 # in France, the cents separator is the , but sometimes, ppl use a .
259 # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
260 $price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
261 $price = Koha::Number::Price->new($price)->unformat;
262 $orderinfo{tax_rate} = $bookseller->tax_rate;
263 my $c = $c_discount ? $c_discount : $bookseller->discount / 100;
264 $orderinfo{discount} = $c;
265 if ( $bookseller->listincgst ) {
267 $orderinfo{ecost} = $price;
268 $orderinfo{rrp} = $orderinfo{ecost} / ( 1 - $c );
270 $orderinfo{ecost} = $price * ( 1 - $c );
271 $orderinfo{rrp} = $price;
275 $orderinfo{ecost} = $price / ( 1 + $orderinfo{tax_rate} );
276 $orderinfo{rrp} = $orderinfo{ecost} / ( 1 - $c );
278 $orderinfo{rrp} = $price / ( 1 + $orderinfo{tax_rate} );
279 $orderinfo{ecost} = $orderinfo{rrp} * ( 1 - $c );
282 $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
283 $orderinfo{unitprice} = $orderinfo{ecost};
284 $orderinfo{total} = $orderinfo{ecost} * $infos->{quantity};
286 $orderinfo{listprice} = 0;
289 # remove uncertainprice flag if we have found a price in the MARC record
290 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
293 C4::Acquisition::populate_order_with_prices(
295 order => \%orderinfo,
296 booksellerid => $booksellerid,
303 my $order = Koha::Acquisition::Order->new( \%orderinfo )->insert;
308 my $patron = C4::Members::GetMember( borrowernumber => $loggedinuser );
309 # get quantity in the MARC record (1 if none)
310 my $quantity = GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour')) || 1;
312 biblionumber => $biblionumber,
313 basketno => $cgiparams->{'basketno'},
314 quantity => $c_quantity,
315 branchcode => $patron->{branchcode},
316 budget_id => $c_budget_id,
320 order_internalnote => $cgiparams->{'all_order_internalnote'},
321 order_vendornote => $cgiparams->{'all_order_vendornote'},
322 currency => $cgiparams->{'all_currency'},
324 # get the price if there is one.
325 my $price= shift( @prices ) || GetMarcPrice($marcrecord, C4::Context->preference('marcflavour'));
327 # in France, the cents separator is the , but sometimes, ppl use a .
328 # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
329 $price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
330 $price = Koha::Number::Price->new($price)->unformat;
331 $orderinfo{tax_rate} = $bookseller->tax_rate;
332 my $c = $c_discount ? $c_discount : $bookseller->discount / 100;
333 $orderinfo{discount} = $c;
334 if ( $bookseller->listincgst ) {
336 $orderinfo{ecost} = $price;
337 $orderinfo{rrp} = $orderinfo{ecost} / ( 1 - $c );
339 $orderinfo{ecost} = $price * ( 1 - $c );
340 $orderinfo{rrp} = $price;
344 $orderinfo{ecost} = $price / ( 1 + $orderinfo{tax_rate} );
345 $orderinfo{rrp} = $orderinfo{ecost} / ( 1 - $c );
347 $orderinfo{rrp} = $price / ( 1 + $orderinfo{tax_rate} );
348 $orderinfo{ecost} = $orderinfo{rrp} * ( 1 - $c );
351 $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
352 $orderinfo{unitprice} = $orderinfo{ecost};
353 $orderinfo{total} = $orderinfo{ecost} * $c_quantity;
355 $orderinfo{listprice} = 0;
358 # remove uncertainprice flag if we have found a price in the MARC record
359 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
362 C4::Acquisition::populate_order_with_prices(
364 order => \%orderinfo,
365 booksellerid => $booksellerid,
372 my $order = Koha::Acquisition::Order->new( \%orderinfo )->insert;
374 # 4th, add items if applicable
375 # parse the item sent by the form, and create an item just for the import_record_id we are dealing with
376 # this is not optimised, but it's working !
377 my $basket = GetBasket($cgiparams->{basketno});
378 if ( C4::Context->preference('AcqCreateItem') eq 'ordering' && !$basket->{is_standing} ) {
379 my @tags = $input->multi_param('tag');
380 my @subfields = $input->multi_param('subfield');
381 my @field_values = $input->multi_param('field_value');
382 my @serials = $input->multi_param('serial');
383 my @ind_tag = $input->multi_param('ind_tag');
384 my @indicator = $input->multi_param('indicator');
386 push @{ $item->{tags} }, $tags[0];
387 push @{ $item->{subfields} }, $subfields[0];
388 push @{ $item->{field_values} }, $field_values[0];
389 push @{ $item->{ind_tag} }, $ind_tag[0];
390 push @{ $item->{indicator} }, $indicator[0];
391 my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values, \@indicator, \@ind_tag );
392 my $record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
393 for (my $qtyloop=1;$qtyloop <= $c_quantity;$qtyloop++) {
394 my ( $biblionumber, $bibitemnum, $itemnumber ) = AddItemFromMarc( $record, $biblionumber );
395 $order->add_item( $itemnumber );
398 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
405 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=".$cgiparams->{'basketno'}."&duplinbatch=$duplinbatch");
407 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=".$cgiparams->{'basketno'}."&booksellerid=$booksellerid&allmatch=1");
412 my $budgets = GetBudgets();
413 my $budget_id = @$budgets[0]->{'budget_id'};
414 # build bookfund list
415 my $borrower = GetMember( 'borrowernumber' => $loggedinuser );
416 my ( $flags, $homebranch ) = ( $borrower->{'flags'}, $borrower->{'branchcode'} );
417 my $budget = GetBudget($budget_id);
420 my $budget_loop = [];
421 my $budgets_hierarchy = GetBudgetHierarchy;
422 foreach my $r ( @{$budgets_hierarchy} ) {
423 next unless (CanUserUseBudget($borrower, $r, $userflags));
424 if ( !defined $r->{budget_amount} || $r->{budget_amount} == 0 ) {
427 push @{$budget_loop},
428 { b_id => $r->{budget_id},
429 b_txt => $r->{budget_name},
430 b_code => $r->{budget_code},
431 b_sort1_authcat => $r->{'sort1_authcat'},
432 b_sort2_authcat => $r->{'sort2_authcat'},
433 b_active => $r->{budget_period_active},
434 b_sel => ( $r->{budget_id} == $budget_id ) ? 1 : 0,
439 sort { uc( $a->{b_txt}) cmp uc( $b->{b_txt}) } @{$budget_loop};
441 $template->param( budget_loop => $budget_loop,);
443 output_html_with_http_headers $input, $cookie, $template->output;
446 sub import_batches_list {
448 my $batches = GetImportBatchRangeDesc();
451 foreach my $batch (@$batches) {
452 if ( $batch->{'import_status'} =~ /^staged$|^reverted$/ && $batch->{'record_type'} eq 'biblio') {
453 # check if there is at least 1 line still staged
454 my $stagedList=GetImportRecordsRange($batch->{'import_batch_id'}, undef, 1, $batch->{import_status}, { order_by_direction => 'ASC' });
455 if (scalar @$stagedList) {
457 import_batch_id => $batch->{'import_batch_id'},
458 num_records => $batch->{'num_records'},
459 num_items => $batch->{'num_items'},
460 staged_date => $batch->{'upload_timestamp'},
461 import_status => $batch->{'import_status'},
462 file_name => $batch->{'file_name'},
463 comments => $batch->{'comments'},
466 # if there are no more line to includes, set the status to imported
467 SetImportBatchStatus( $batch->{'import_batch_id'}, 'imported' );
471 $template->param(batch_list => \@list);
472 my $num_batches = GetNumberOfNonZ3950ImportBatches();
473 $template->param(num_results => $num_batches);
476 sub import_biblios_list {
477 my ($template, $import_batch_id) = @_;
478 my $batch = GetImportBatch($import_batch_id,'staged');
479 return () unless $batch and $batch->{import_status} =~ /^staged$|^reverted$/;
480 my $biblios = GetImportRecordsRange($import_batch_id,'','',$batch->{import_status});
484 my $ccodes = { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' } ) };
485 my $locations = { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) };
486 my $notforloans = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.notforloan' } ) };
489 foreach (sort keys %$locations) {
490 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
493 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
494 push @ccodes, { code => $_, description => $ccodes->{$_} };
497 foreach (sort {$notforloans->{$a} cmp $notforloans->{$b}} keys %$notforloans) {
498 push @notforloans, { code => $_, description => $notforloans->{$_} };
501 my $biblio_count = 0;
502 foreach my $biblio (@$biblios) {
505 my $citation = $biblio->{'title'};
506 $citation .= " $biblio->{'author'}" if $biblio->{'author'};
507 $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
508 $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
509 $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
510 $citation .= $biblio->{'issn'} if $biblio->{'issn'};
511 $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
512 my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
514 import_record_id => $biblio->{'import_record_id'},
515 citation => $citation,
517 status => $biblio->{'status'},
518 record_sequence => $biblio->{'record_sequence'},
519 overlay_status => $biblio->{'overlay_status'},
520 match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
521 match_citation => $#$match > -1 ? $match->[0]->{'title'} || '' . ' ' . $match->[0]->{'author'} || '': '',
522 match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
524 my ( $marcblob, $encoding ) = GetImportRecordMarc( $biblio->{'import_record_id'} );
525 my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
527 my $infos = get_infos_syspref('MarcFieldsToOrder', $marcrecord, ['price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2']);
528 my $price = $infos->{price};
529 my $quantity = $infos->{quantity};
530 my $budget_code = $infos->{budget_code};
531 my $discount = $infos->{discount};
532 my $sort1 = $infos->{sort1};
533 my $sort2 = $infos->{sort2};
536 my $biblio_budget = GetBudgetByCode($budget_code);
538 $budget_id = $biblio_budget->{budget_id};
544 my $all_items_quantity = 0;
545 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']);
546 if ($alliteminfos != -1) {
547 foreach my $iteminfos (@$alliteminfos) {
548 my $item_homebranch = $iteminfos->{homebranch};
549 my $item_holdingbranch = $iteminfos->{holdingbranch};
550 my $item_itype = $iteminfos->{itype};
551 my $item_nonpublic_note = $iteminfos->{nonpublic_note};
552 my $item_public_note = $iteminfos->{public_note};
553 my $item_loc = $iteminfos->{loc};
554 my $item_ccode = $iteminfos->{ccode};
555 my $item_notforloan = $iteminfos->{notforloan};
556 my $item_uri = $iteminfos->{uri};
557 my $item_copyno = $iteminfos->{copyno};
558 my $item_quantity = $iteminfos->{quantity} || 1;
559 my $item_budget_code = $iteminfos->{budget_code};
560 my $item_price = $iteminfos->{price};
561 my $item_replacement_price = $iteminfos->{replacementprice};
562 my $item_callnumber = $iteminfos->{itemcallnumber};
564 for (my $i = 0; $i < $item_quantity; $i++) {
567 'item_id' => $item_id++,
568 'biblio_count' => $biblio_count,
569 'homebranch' => $item_homebranch,
570 'holdingbranch' => $item_holdingbranch,
571 'itype' => $item_itype,
572 'nonpublic_note' => $item_nonpublic_note,
573 'public_note' => $item_public_note,
575 'ccode' => $item_ccode,
576 'notforloan' => $item_notforloan,
578 'copyno' => $item_copyno,
579 'quantity' => $item_quantity,
580 'budget_code' => $item_budget_code || $budget_code,
581 'itemprice' => $item_price || $price,
582 'replacementprice' => $item_replacement_price,
583 'itemcallnumber' => $item_callnumber,
585 $all_items_quantity++;
586 push @itemlist, \%itemrecord;
591 $cellrecord{'iteminfos'} = \@itemlist;
593 $cellrecord{'item_error'} = 1;
595 push @list, \%cellrecord;
597 if ($alliteminfos == -1 || scalar(@$alliteminfos) == 0) {
598 $cellrecord{price} = $price || '';
599 $cellrecord{quantity} = $quantity || '';
600 $cellrecord{budget_id} = $budget_id || '';
601 $cellrecord{discount} = $discount || '';
602 $cellrecord{sort1} = $sort1 || '';
603 $cellrecord{sort2} = $sort2 || '';
605 $cellrecord{quantity} = $all_items_quantity;
609 my $num_records = $batch->{'num_records'};
610 my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
611 my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
612 my $item_action = GetImportBatchItemAction($import_batch_id);
613 my @itypes = Koha::ItemTypes->search;
614 $template->param(biblio_list => \@list,
615 num_results => $num_records,
616 import_batch_id => $import_batch_id,
617 "overlay_action_${overlay_action}" => 1,
618 overlay_action => $overlay_action,
619 "nomatch_action_${nomatch_action}" => 1,
620 nomatch_action => $nomatch_action,
621 "item_action_${item_action}" => 1,
622 item_action => $item_action,
623 item_error => $item_error,
624 libraries => scalar Koha::Libraries->search(),
625 locationloop => \@locations,
626 itypeloop => \@itypes,
627 ccodeloop => \@ccodes,
628 notforloanloop => \@notforloans,
630 batch_info($template, $batch);
634 my ($template, $batch) = @_;
635 $template->param(batch_info => 1,
636 file_name => $batch->{'file_name'},
637 comments => $batch->{'comments'},
638 import_status => $batch->{'import_status'},
639 upload_timestamp => $batch->{'upload_timestamp'},
640 num_records => $batch->{'num_records'},
641 num_items => $batch->{'num_items'});
642 if ($batch->{'num_records'} > 0) {
643 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
644 $template->param(can_commit => 1);
646 if ($batch->{'import_status'} eq 'imported') {
647 $template->param(can_revert => 1);
650 if (defined $batch->{'matcher_id'}) {
651 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
652 if (defined $matcher) {
653 $template->param('current_matcher_id' => $batch->{'matcher_id'},
654 'current_matcher_code' => $matcher->code(),
655 'current_matcher_description' => $matcher->description());
658 add_matcher_list($batch->{'matcher_id'}, $template);
661 sub add_matcher_list {
662 my ($current_matcher_id, $template) = @_;
663 my @matchers = C4::Matcher::GetMatcherList();
664 if (defined $current_matcher_id) {
665 for (my $i = 0; $i <= $#matchers; $i++) {
666 if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
667 $matchers[$i]->{'selected'} = 1;
671 $template->param(available_matchers => \@matchers);
674 sub get_infos_syspref {
675 my ($syspref_name, $record, $field_list) = @_;
676 my $syspref = C4::Context->preference($syspref_name);
677 $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
679 YAML::Load($syspref);
682 warn "Unable to parse $syspref syspref : $@";
686 for my $field_name ( @$field_list ) {
687 next unless exists $yaml->{$field_name};
688 my @fields = split /\|/, $yaml->{$field_name};
689 for my $field ( @fields ) {
690 my ( $f, $sf ) = split /\$/, $field;
691 next unless $f and $sf;
692 if ( my $v = $record->subfield( $f, $sf ) ) {
693 $r->{$field_name} = $v;
695 last if $yaml->{$field};
701 sub equal_number_of_fields {
702 my ($tags_list, $record) = @_;
705 for my $tag (@$tags_list) {
706 return -1 if $count != $refcount;
708 for my $field ($record->field($tag)) {
711 $refcount = $count if ($refcount == 0);
713 return -1 if $count != $refcount;
717 sub get_infos_syspref_on_item {
718 my ($syspref_name, $record, $field_list) = @_;
719 my $syspref = C4::Context->preference($syspref_name);
720 $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
722 YAML::Load($syspref);
725 warn "Unable to parse $syspref syspref : $@";
731 # Check tags in syspref definition
732 for my $field_name ( @$field_list ) {
733 next unless exists $yaml->{$field_name};
734 my @fields = split /\|/, $yaml->{$field_name};
735 for my $field ( @fields ) {
736 my ( $f, $sf ) = split /\$/, $field;
737 next unless $f and $sf;
741 @tags_list = List::MoreUtils::uniq(@tags_list);
743 my $tags_count = equal_number_of_fields(\@tags_list, $record);
744 # Return if the number of theses fields in the record is not the same.
745 return -1 if $tags_count == -1;
749 foreach my $tag (@tags_list) {
751 foreach my $field ($record->field($tag)) {
752 push @tmp_fields, $field;
754 $fields_hash->{$tag} = \@tmp_fields;
757 for (my $i = 0; $i < $tags_count; $i++) {
759 for my $field_name ( @$field_list ) {
760 next unless exists $yaml->{$field_name};
761 my @fields = split /\|/, $yaml->{$field_name};
762 for my $field ( @fields ) {
763 my ( $f, $sf ) = split /\$/, $field;
764 next unless $f and $sf;
765 my $v = $fields_hash->{$f}[$i]->subfield( $sf );
766 $r->{$field_name} = $v if (defined $v);
767 last if $yaml->{$field};