BUG 8010: Correct a few syntax errors, and moved tests
[koha.git] / acqui / addorderiso2709.pl
blob24dc8e5b4a7f7a26aa1d718f59e0ce6e517e51fa
1 #!/usr/bin/perl
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 he wants 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>.
24 use Modern::Perl;
25 use CGI qw ( -utf8 );
26 use Carp;
27 use YAML qw/Load/;
28 use List::MoreUtils qw/uniq/;
30 use C4::Context;
31 use C4::Auth;
32 use C4::Output;
33 use C4::ImportBatch;
34 use C4::Matcher;
35 use C4::Search qw/FindDuplicate/;
36 use C4::Acquisition;
37 use C4::Biblio;
38 use C4::Items;
39 use C4::Koha;
40 use C4::Budgets;
41 use C4::Acquisition;
42 use C4::Suggestions; # GetSuggestion
43 use C4::Members;
45 use Koha::Number::Price;
46 use Koha::Libraries;
47 use Koha::Acquisition::Currencies;
48 use Koha::Acquisition::Order;
49 use Koha::Acquisition::Booksellers;
51 my $input = new CGI;
52 my ($template, $loggedinuser, $cookie, $userflags) = get_template_and_user({
53 template_name => "acqui/addorderiso2709.tt",
54 query => $input,
55 type => "intranet",
56 authnotrequired => 0,
57 flagsrequired => { acquisition => 'order_manage' },
58 debug => 1,
59 });
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";
76 #Needed parameters:
77 if (! $cgiparams->{'basketno'}){
78 die "Basketnumber required to order from iso2709 file import";
82 # 1st step = choose the file to import into acquisition
84 if ($op eq ""){
85 $template->param("basketno" => $cgiparams->{'basketno'});
86 #display batches
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);
109 unless ($cell) {
110 $cell = PrepareItemrecordDisplay( '', '', '', '' );
111 $template->param( 'NoACQframework' => 1 );
113 my @itemloop;
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);
136 my $duplinbatch;
137 my $imported = 0;
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){
149 $biblio_count++;
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 ) {
165 if ($matcher_id) {
166 if ( $matcher_id eq '_TITLE_AUTHOR_' ) {
167 $duplinbatch = $import_batch_id if FindDuplicate($marcrecord);
169 else {
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;
178 # add the biblio
179 my $bibitemnum;
181 # remove ISBN -
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);
187 $newisbn =~ s/-//g;
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'});
198 } else {
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++) {
220 $itemcreation = 1;
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],
230 uri => $uris[$i],
231 copynumber => $copynos[$i],
232 price => $itemprices[$i],
233 replacementprice => $replacementprices[$i],
234 itemcallnumber => $itemcallnumbers[$i],
235 }, $biblionumber);
237 if ($itemcreation == 1) {
238 # Group orderlines from MarcItemFieldsToOrder
239 my $budget_hash;
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) {
247 if ($budget_id) {
248 my %orderinfo = (
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};
257 if ($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 if ( $bookseller->listincgst ) {
265 if ( $c_discount ) {
266 $orderinfo{ecost} = $price;
267 $orderinfo{rrp} = $orderinfo{ecost} / ( 1 - $c );
268 } else {
269 $orderinfo{ecost} = $price * ( 1 - $c );
270 $orderinfo{rrp} = $price;
272 } else {
273 if ( $c_discount ) {
274 $orderinfo{ecost} = $price / ( 1 + $orderinfo{tax_rate} );
275 $orderinfo{rrp} = $orderinfo{ecost} / ( 1 - $c );
276 } else {
277 $orderinfo{rrp} = $price / ( 1 + $orderinfo{tax_rate} );
278 $orderinfo{ecost} = $orderinfo{rrp} * ( 1 - $c );
281 $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
282 $orderinfo{unitprice} = $orderinfo{ecost};
283 $orderinfo{total} = $orderinfo{ecost} * $infos->{quantity};
284 } else {
285 $orderinfo{listprice} = 0;
288 # remove uncertainprice flag if we have found a price in the MARC record
289 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
291 %orderinfo = %{
292 C4::Acquisition::populate_order_with_prices(
294 order => \%orderinfo,
295 booksellerid => $booksellerid,
296 ordering => 1,
297 receiving => 1,
302 my $order = Koha::Acquisition::Order->new( \%orderinfo )->insert;
305 } else {
306 # 3rd add order
307 my $patron = C4::Members::GetMember( borrowernumber => $loggedinuser );
308 # get quantity in the MARC record (1 if none)
309 my $quantity = GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour')) || 1;
310 my %orderinfo = (
311 biblionumber => $biblionumber,
312 basketno => $cgiparams->{'basketno'},
313 quantity => $c_quantity,
314 branchcode => $patron->{branchcode},
315 budget_id => $c_budget_id,
316 uncertainprice => 1,
317 sort1 => $c_sort1,
318 sort2 => $c_sort2,
319 order_internalnote => $cgiparams->{'all_order_internalnote'},
320 order_vendornote => $cgiparams->{'all_order_vendornote'},
321 currency => $cgiparams->{'all_currency'},
323 # get the price if there is one.
324 my $price= shift( @prices ) || GetMarcPrice($marcrecord, C4::Context->preference('marcflavour'));
325 if ($price){
326 # in France, the cents separator is the , but sometimes, ppl use a .
327 # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
328 $price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
329 $price = Koha::Number::Price->new($price)->unformat;
330 $orderinfo{tax_rate} = $bookseller->tax_rate;
331 my $c = $c_discount ? $c_discount : $bookseller->discount / 100;
332 if ( $bookseller->listincgst ) {
333 if ( $c_discount ) {
334 $orderinfo{ecost} = $price;
335 $orderinfo{rrp} = $orderinfo{ecost} / ( 1 - $c );
336 } else {
337 $orderinfo{ecost} = $price * ( 1 - $c );
338 $orderinfo{rrp} = $price;
340 } else {
341 if ( $c_discount ) {
342 $orderinfo{ecost} = $price / ( 1 + $orderinfo{tax_rate} );
343 $orderinfo{rrp} = $orderinfo{ecost} / ( 1 - $c );
344 } else {
345 $orderinfo{rrp} = $price / ( 1 + $orderinfo{tax_rate} );
346 $orderinfo{ecost} = $orderinfo{rrp} * ( 1 - $c );
349 $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
350 $orderinfo{unitprice} = $orderinfo{ecost};
351 $orderinfo{total} = $orderinfo{ecost} * $c_quantity;
352 } else {
353 $orderinfo{listprice} = 0;
356 # remove uncertainprice flag if we have found a price in the MARC record
357 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
359 %orderinfo = %{
360 C4::Acquisition::populate_order_with_prices(
362 order => \%orderinfo,
363 booksellerid => $booksellerid,
364 ordering => 1,
365 receiving => 1,
370 my $order = Koha::Acquisition::Order->new( \%orderinfo )->insert;
372 # 4th, add items if applicable
373 # parse the item sent by the form, and create an item just for the import_record_id we are dealing with
374 # this is not optimised, but it's working !
375 my $basket = GetBasket($cgiparams->{basketno});
376 if ( C4::Context->preference('AcqCreateItem') eq 'ordering' && !$basket->{is_standing} ) {
377 my @tags = $input->multi_param('tag');
378 my @subfields = $input->multi_param('subfield');
379 my @field_values = $input->multi_param('field_value');
380 my @serials = $input->multi_param('serial');
381 my @ind_tag = $input->multi_param('ind_tag');
382 my @indicator = $input->multi_param('indicator');
383 my $item;
384 push @{ $item->{tags} }, $tags[0];
385 push @{ $item->{subfields} }, $subfields[0];
386 push @{ $item->{field_values} }, $field_values[0];
387 push @{ $item->{ind_tag} }, $ind_tag[0];
388 push @{ $item->{indicator} }, $indicator[0];
389 my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values, \@indicator, \@ind_tag );
390 my $record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
391 for (my $qtyloop=1;$qtyloop <= $c_quantity;$qtyloop++) {
392 my ( $biblionumber, $bibitemnum, $itemnumber ) = AddItemFromMarc( $record, $biblionumber );
393 $order->add_item( $itemnumber );
395 } else {
396 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
399 $imported++;
401 # go to basket page
402 if ( $imported ) {
403 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=".$cgiparams->{'basketno'}."&amp;duplinbatch=$duplinbatch");
404 } else {
405 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&amp;basketno=".$cgiparams->{'basketno'}."&amp;booksellerid=$booksellerid&amp;allmatch=1");
407 exit;
410 my $budgets = GetBudgets();
411 my $budget_id = @$budgets[0]->{'budget_id'};
412 # build bookfund list
413 my $borrower = GetMember( 'borrowernumber' => $loggedinuser );
414 my ( $flags, $homebranch ) = ( $borrower->{'flags'}, $borrower->{'branchcode'} );
415 my $budget = GetBudget($budget_id);
417 # build budget list
418 my $budget_loop = [];
419 my $budgets_hierarchy = GetBudgetHierarchy;
420 foreach my $r ( @{$budgets_hierarchy} ) {
421 next unless (CanUserUseBudget($borrower, $r, $userflags));
422 if ( !defined $r->{budget_amount} || $r->{budget_amount} == 0 ) {
423 next;
425 push @{$budget_loop},
426 { b_id => $r->{budget_id},
427 b_txt => $r->{budget_name},
428 b_code => $r->{budget_code},
429 b_sort1_authcat => $r->{'sort1_authcat'},
430 b_sort2_authcat => $r->{'sort2_authcat'},
431 b_active => $r->{budget_period_active},
432 b_sel => ( $r->{budget_id} == $budget_id ) ? 1 : 0,
436 @{$budget_loop} =
437 sort { uc( $a->{b_txt}) cmp uc( $b->{b_txt}) } @{$budget_loop};
439 $template->param( budget_loop => $budget_loop,);
441 output_html_with_http_headers $input, $cookie, $template->output;
444 sub import_batches_list {
445 my ($template) = @_;
446 my $batches = GetImportBatchRangeDesc();
448 my @list = ();
449 foreach my $batch (@$batches) {
450 if ( $batch->{'import_status'} =~ /^staged$|^reverted$/ && $batch->{'record_type'} eq 'biblio') {
451 # check if there is at least 1 line still staged
452 my $stagedList=GetImportRecordsRange($batch->{'import_batch_id'}, undef, 1, $batch->{import_status}, { order_by_direction => 'ASC' });
453 if (scalar @$stagedList) {
454 push @list, {
455 import_batch_id => $batch->{'import_batch_id'},
456 num_records => $batch->{'num_records'},
457 num_items => $batch->{'num_items'},
458 staged_date => $batch->{'upload_timestamp'},
459 import_status => $batch->{'import_status'},
460 file_name => $batch->{'file_name'},
461 comments => $batch->{'comments'},
463 } else {
464 # if there are no more line to includes, set the status to imported
465 SetImportBatchStatus( $batch->{'import_batch_id'}, 'imported' );
469 $template->param(batch_list => \@list);
470 my $num_batches = GetNumberOfNonZ3950ImportBatches();
471 $template->param(num_results => $num_batches);
474 sub import_biblios_list {
475 my ($template, $import_batch_id) = @_;
476 my $batch = GetImportBatch($import_batch_id,'staged');
477 return () unless $batch and $batch->{import_status} =~ /^staged$|^reverted$/;
478 my $biblios = GetImportRecordsRange($import_batch_id,'','',$batch->{import_status});
479 my @list = ();
480 my $item_error = 0;
482 my $ccodes = { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.ccode' } ) };
483 my $locations = { map { $_->{authorised_value} => $_->{opac_description} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.location' } ) };
484 my $notforloans = { map { $_->{authorised_value} => $_->{lib} } Koha::AuthorisedValues->get_descriptions_by_koha_field( { frameworkcode => '', kohafield => 'items.notforloan' } ) };
485 # location list
486 my @locations;
487 foreach (sort keys %$locations) {
488 push @locations, { code => $_, description => "$_ - " . $locations->{$_} };
490 my @ccodes;
491 foreach (sort {$ccodes->{$a} cmp $ccodes->{$b}} keys %$ccodes) {
492 push @ccodes, { code => $_, description => $ccodes->{$_} };
494 my @notforloans;
495 foreach (sort {$notforloans->{$a} cmp $notforloans->{$b}} keys %$notforloans) {
496 push @notforloans, { code => $_, description => $notforloans->{$_} };
499 my $biblio_count = 0;
500 foreach my $biblio (@$biblios) {
501 my $item_id = 1;
502 $biblio_count++;
503 my $citation = $biblio->{'title'};
504 $citation .= " $biblio->{'author'}" if $biblio->{'author'};
505 $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
506 $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
507 $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
508 $citation .= $biblio->{'issn'} if $biblio->{'issn'};
509 $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
510 my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
511 my %cellrecord = (
512 import_record_id => $biblio->{'import_record_id'},
513 citation => $citation,
514 import => 1,
515 status => $biblio->{'status'},
516 record_sequence => $biblio->{'record_sequence'},
517 overlay_status => $biblio->{'overlay_status'},
518 match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
519 match_citation => $#$match > -1 ? $match->[0]->{'title'} || '' . ' ' . $match->[0]->{'author'} || '': '',
520 match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
522 my ( $marcblob, $encoding ) = GetImportRecordMarc( $biblio->{'import_record_id'} );
523 my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
525 my $infos = get_infos_syspref('MarcFieldsToOrder', $marcrecord, ['price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2']);
526 my $price = $infos->{price};
527 my $quantity = $infos->{quantity};
528 my $budget_code = $infos->{budget_code};
529 my $discount = $infos->{discount};
530 my $sort1 = $infos->{sort1};
531 my $sort2 = $infos->{sort2};
532 my $budget_id;
533 if($budget_code) {
534 my $biblio_budget = GetBudgetByCode($budget_code);
535 if($biblio_budget) {
536 $budget_id = $biblio_budget->{budget_id};
540 # Items
541 my @itemlist = ();
542 my $all_items_quantity = 0;
543 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']);
544 if ($alliteminfos != -1) {
545 foreach my $iteminfos (@$alliteminfos) {
546 my $item_homebranch = $iteminfos->{homebranch};
547 my $item_holdingbranch = $iteminfos->{holdingbranch};
548 my $item_itype = $iteminfos->{itype};
549 my $item_nonpublic_note = $iteminfos->{nonpublic_note};
550 my $item_public_note = $iteminfos->{public_note};
551 my $item_loc = $iteminfos->{loc};
552 my $item_ccode = $iteminfos->{ccode};
553 my $item_notforloan = $iteminfos->{notforloan};
554 my $item_uri = $iteminfos->{uri};
555 my $item_copyno = $iteminfos->{copyno};
556 my $item_quantity = $iteminfos->{quantity} || 1;
557 my $item_budget_code = $iteminfos->{budget_code};
558 my $item_price = $iteminfos->{price};
559 my $item_replacement_price = $iteminfos->{replacementprice};
560 my $item_callnumber = $iteminfos->{itemcallnumber};
562 for (my $i = 0; $i < $item_quantity; $i++) {
564 my %itemrecord = (
565 'item_id' => $item_id++,
566 'biblio_count' => $biblio_count,
567 'homebranch' => $item_homebranch,
568 'holdingbranch' => $item_holdingbranch,
569 'itype' => $item_itype,
570 'nonpublic_note' => $item_nonpublic_note,
571 'public_note' => $item_public_note,
572 'loc' => $item_loc,
573 'ccode' => $item_ccode,
574 'notforloan' => $item_notforloan,
575 'uri' => $item_uri,
576 'copyno' => $item_copyno,
577 'quantity' => $item_quantity,
578 'budget_code' => $item_budget_code || $budget_code,
579 'itemprice' => $item_price || $price,
580 'replacementprice' => $item_replacement_price,
581 'itemcallnumber' => $item_callnumber,
583 $all_items_quantity++;
584 push @itemlist, \%itemrecord;
589 $cellrecord{'iteminfos'} = \@itemlist;
590 } else {
591 $cellrecord{'item_error'} = 1;
593 push @list, \%cellrecord;
595 if ($alliteminfos == -1 || scalar(@$alliteminfos) == 0) {
596 $cellrecord{price} = $price || '';
597 $cellrecord{quantity} = $quantity || '';
598 $cellrecord{budget_id} = $budget_id || '';
599 $cellrecord{discount} = $discount || '';
600 $cellrecord{sort1} = $sort1 || '';
601 $cellrecord{sort2} = $sort2 || '';
602 } else {
603 $cellrecord{quantity} = $all_items_quantity;
607 my $num_records = $batch->{'num_records'};
608 my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
609 my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
610 my $item_action = GetImportBatchItemAction($import_batch_id);
611 my @itypes = Koha::ItemTypes->search;
612 $template->param(biblio_list => \@list,
613 num_results => $num_records,
614 import_batch_id => $import_batch_id,
615 "overlay_action_${overlay_action}" => 1,
616 overlay_action => $overlay_action,
617 "nomatch_action_${nomatch_action}" => 1,
618 nomatch_action => $nomatch_action,
619 "item_action_${item_action}" => 1,
620 item_action => $item_action,
621 item_error => $item_error,
622 libraries => scalar Koha::Libraries->search(),
623 locationloop => \@locations,
624 itypeloop => \@itypes,
625 ccodeloop => \@ccodes,
626 notforloanloop => \@notforloans,
628 batch_info($template, $batch);
631 sub batch_info {
632 my ($template, $batch) = @_;
633 $template->param(batch_info => 1,
634 file_name => $batch->{'file_name'},
635 comments => $batch->{'comments'},
636 import_status => $batch->{'import_status'},
637 upload_timestamp => $batch->{'upload_timestamp'},
638 num_records => $batch->{'num_records'},
639 num_items => $batch->{'num_items'});
640 if ($batch->{'num_records'} > 0) {
641 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
642 $template->param(can_commit => 1);
644 if ($batch->{'import_status'} eq 'imported') {
645 $template->param(can_revert => 1);
648 if (defined $batch->{'matcher_id'}) {
649 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
650 if (defined $matcher) {
651 $template->param('current_matcher_id' => $batch->{'matcher_id'},
652 'current_matcher_code' => $matcher->code(),
653 'current_matcher_description' => $matcher->description());
656 add_matcher_list($batch->{'matcher_id'}, $template);
659 sub add_matcher_list {
660 my ($current_matcher_id, $template) = @_;
661 my @matchers = C4::Matcher::GetMatcherList();
662 if (defined $current_matcher_id) {
663 for (my $i = 0; $i <= $#matchers; $i++) {
664 if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
665 $matchers[$i]->{'selected'} = 1;
669 $template->param(available_matchers => \@matchers);
672 sub get_infos_syspref {
673 my ($syspref_name, $record, $field_list) = @_;
674 my $syspref = C4::Context->preference($syspref_name);
675 $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
676 my $yaml = eval {
677 YAML::Load($syspref);
679 if ( $@ ) {
680 warn "Unable to parse $syspref syspref : $@";
681 return ();
683 my $r;
684 for my $field_name ( @$field_list ) {
685 next unless exists $yaml->{$field_name};
686 my @fields = split /\|/, $yaml->{$field_name};
687 for my $field ( @fields ) {
688 my ( $f, $sf ) = split /\$/, $field;
689 next unless $f and $sf;
690 if ( my $v = $record->subfield( $f, $sf ) ) {
691 $r->{$field_name} = $v;
693 last if $yaml->{$field};
696 return $r;
699 sub equal_number_of_fields {
700 my ($tags_list, $record) = @_;
701 my $refcount = 0;
702 my $count = 0;
703 for my $tag (@$tags_list) {
704 return -1 if $count != $refcount;
705 $count = 0;
706 for my $field ($record->field($tag)) {
707 $count++;
709 $refcount = $count if ($refcount == 0);
711 return -1 if $count != $refcount;
712 return $count;
715 sub get_infos_syspref_on_item {
716 my ($syspref_name, $record, $field_list) = @_;
717 my $syspref = C4::Context->preference($syspref_name);
718 $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
719 my $yaml = eval {
720 YAML::Load($syspref);
722 if ( $@ ) {
723 warn "Unable to parse $syspref syspref : $@";
724 return ();
726 my @result;
727 my @tags_list;
729 # Check tags in syspref definition
730 for my $field_name ( @$field_list ) {
731 next unless exists $yaml->{$field_name};
732 my @fields = split /\|/, $yaml->{$field_name};
733 for my $field ( @fields ) {
734 my ( $f, $sf ) = split /\$/, $field;
735 next unless $f and $sf;
736 push @tags_list, $f;
739 @tags_list = List::MoreUtils::uniq(@tags_list);
741 my $tags_count = equal_number_of_fields(\@tags_list, $record);
742 # Return if the number of theses fields in the record is not the same.
743 return -1 if $tags_count == -1;
745 # Gather the fields
746 my $fields_hash;
747 foreach my $tag (@tags_list) {
748 my @tmp_fields;
749 foreach my $field ($record->field($tag)) {
750 push @tmp_fields, $field;
752 $fields_hash->{$tag} = \@tmp_fields;
755 for (my $i = 0; $i < $tags_count; $i++) {
756 my $r;
757 for my $field_name ( @$field_list ) {
758 next unless exists $yaml->{$field_name};
759 my @fields = split /\|/, $yaml->{$field_name};
760 for my $field ( @fields ) {
761 my ( $f, $sf ) = split /\$/, $field;
762 next unless $f and $sf;
763 my $v = $fields_hash->{$f}[$i]->subfield( $sf );
764 $r->{$field_name} = $v if (defined $v);
765 last if $yaml->{$field};
768 push @result, $r;
770 return \@result;