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 under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 use Number::Format qw(:all);
36 use C4::Search qw/FindDuplicate/;
43 use C4::Bookseller qw/GetBookSellerFromId/;
44 use C4::Suggestions; # GetSuggestion
45 use C4::Branch; # GetBranches
49 my ($template, $loggedinuser, $cookie, $userflags) = get_template_and_user({
50 template_name => "acqui/addorderiso2709.tmpl",
54 flagsrequired => { acquisition => 'order_manage' },
58 my $cgiparams = $input->Vars;
59 my $op = $cgiparams->{'op'};
60 my $booksellerid = $input->param('booksellerid');
61 my $bookseller = GetBookSellerFromId($booksellerid);
64 $template->param(scriptname => "/cgi-bin/koha/acqui/addorderiso2709.pl",
65 booksellerid => $booksellerid,
66 booksellername => $bookseller->{name},
70 if ($cgiparams->{'import_batch_id'} && $op eq ""){
71 $op = "batch_details";
75 if (! $cgiparams->{'basketno'}){
76 die "Basketnumber required to order from iso2709 file import";
80 # 1st step = choose the file to import into acquisition
83 $template->param("basketno" => $cgiparams->{'basketno'});
85 import_batches_list($template);
87 # 2nd step = display the content of the choosen file
89 } elsif ($op eq "batch_details"){
90 #display lines inside the selected batch
91 # get currencies (for change rates calcs if needed)
92 my $active_currency = GetCurrency();
94 if (! $data->{currency} ) { # New order no currency set
95 if ( $bookseller->{listprice} ) {
96 $default_currency = $bookseller->{listprice};
99 $default_currency = $active_currency->{currency};
102 my @rates = GetCurrencies();
106 my @loop_currency = ();
107 for my $curr ( @rates ) {
109 if ($data->{currency} ) {
110 $selected = $curr->{currency} eq $data->{currency};
113 $selected = $curr->{currency} eq $default_currency;
115 push @loop_currency, {
116 currcode => $curr->{currency},
117 rate => $curr->{rate},
118 selected => $selected,
122 $template->param("batch_details" => 1,
123 "basketno" => $cgiparams->{'basketno'},
124 loop_currencies => \@loop_currency,
126 import_biblios_list($template, $cgiparams->{'import_batch_id'});
127 if ( C4::Context->preference('AcqCreateItem') eq 'ordering' && !$ordernumber ) {
128 # prepare empty item form
129 my $cell = PrepareItemrecordDisplay( '', '', '', 'ACQ' );
131 # warn "==> ".Data::Dumper::Dumper($cell);
133 $cell = PrepareItemrecordDisplay( '', '', '', '' );
134 $template->param( 'NoACQframework' => 1 );
137 push @itemloop, $cell;
139 $template->param( items => \@itemloop );
142 # 3rd step = import the records
144 } elsif ( $op eq 'import_records' ) {
145 my $num=FormatNumber();
146 #import selected lines
147 $template->param('basketno' => $cgiparams->{'basketno'});
148 # Budget_id is mandatory for adding an order, we just add a default, the user needs to modify this aftewards
149 my $budgets = GetBudgets();
150 if (scalar @$budgets == 0){
151 die "No budgets defined, can't continue";
153 my $budget_id = @$budgets[0]->{'budget_id'};
154 #get all records from a batch, and check their import status to see if they are checked.
155 #(default values: quantity 1, uncertainprice yes, first budget)
157 # retrieve the file you want to import
158 my $import_batch_id = $cgiparams->{'import_batch_id'};
159 my $biblios = GetImportRecordsRange($import_batch_id);
160 for my $biblio (@$biblios){
161 # 1st insert the biblio, or find it through matcher
162 my ( $marcblob, $encoding ) = GetImportRecordMarc( $biblio->{'import_record_id'} );
163 my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
164 my $match = GetImportRecordMatches( $biblio->{'import_record_id'}, 1 );
165 my $biblionumber=$#$match > -1?$match->[0]->{'biblionumber'}:0;
167 unless ( $biblionumber ) {
172 my ( $isbnfield, $isbnsubfield ) = GetMarcFromKohaField( 'biblioitems.isbn', '' );
173 if ( $marcrecord->field($isbnfield) ) {
174 foreach my $field ( $marcrecord->field($isbnfield) ) {
175 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
176 my $newisbn = $field->subfield($isbnsubfield);
178 $field->update( $isbnsubfield => $newisbn );
182 ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' );
183 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
184 # 2nd add authorities if applicable
185 if (C4::Context->preference("BiblioAddsAuthorities")){
186 my $headings_linked =BiblioAutoLink($marcrecord, $cgiparams->{'frameworkcode'});
189 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
192 my $patron = C4::Members->GetMember( borrowernumber => $loggedinuser );
193 my $branch = C4::Branch->GetBranchDetail( $patron->{branchcode} );
194 # get quantity in the MARC record (1 if none)
195 my $quantity = GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour')) || 1;
197 "biblionumber", $biblionumber, "basketno", $cgiparams->{'basketno'},
198 "quantity", $quantity, "branchcode", $branch,
199 "budget_id", $budget_id, "uncertainprice", 1,
200 "sort1", $cgiparams->{'sort1'},"sort2", $cgiparams->{'sort2'},
201 "notes", $cgiparams->{'notes'}, "budget_id", $cgiparams->{'budget_id'},
202 "currency",$cgiparams->{'currency'},
205 my $price = GetMarcPrice($marcrecord, C4::Context->preference('marcflavour'));
209 require C4::Acquisition;
210 import C4::Acquisition qw/GetBasket/;
216 require C4::Bookseller;
217 import C4::Bookseller qw/GetBookSellerFromId/;
222 my $basket = GetBasket( $orderinfo{basketno} );
223 my $bookseller = GetBookSellerFromId( $basket->{booksellerid} );
224 $orderinfo{gstrate} = $bookseller->{gstrate};
225 if ( $bookseller->{listincgst} ) {
226 $orderinfo{ecost} = $price;
228 $orderinfo{ecost} = $price * ( 1 + $orderinfo{gstrate} );
230 $orderinfo{rrp} = ( $orderinfo{ecost} * 100 ) / ( 100 - $bookseller->{discount} );
231 $orderinfo{listprice} = $orderinfo{rrp};
232 $orderinfo{unitprice} = $orderinfo{ecost};
233 $orderinfo{total} = $orderinfo{ecost};
235 $orderinfo{'listprice'} = 0;
238 # remove uncertainprice flag if we have found a price in the MARC record
239 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
241 ( $basketno, $ordernumber ) = NewOrder( \%orderinfo );
243 # 4th, add items if applicable
244 # parse the item sent by the form, and create an item just for the import_record_id we are dealing with
245 # this is not optimised, but it's working !
246 if ( C4::Context->preference('AcqCreateItem') eq 'ordering' ) {
247 my @tags = $input->param('tag');
248 my @subfields = $input->param('subfield');
249 my @field_values = $input->param('field_value');
250 my @serials = $input->param('serial');
251 my @ind_tag = $input->param('ind_tag');
252 my @indicator = $input->param('indicator');
254 push @{ $item->{tags} }, $tags[0];
255 push @{ $item->{subfields} }, $subfields[0];
256 push @{ $item->{field_values} }, $field_values[0];
257 push @{ $item->{ind_tag} }, $ind_tag[0];
258 push @{ $item->{indicator} }, $indicator[0];
259 my $xml = TransformHtmlToXml( \@tags, \@subfields, \@field_values, \@ind_tag, \@indicator );
260 my $record = MARC::Record::new_from_xml( $xml, 'UTF-8' );
261 for (my $qtyloop=1;$qtyloop <=$quantity;$qtyloop++) {
262 my ( $biblionumber, $bibitemnum, $itemnumber ) = AddItemFromMarc( $record, $biblionumber );
263 NewOrderItem( $itemnumber, $ordernumber );
266 SetImportRecordStatus( $biblio->{'import_record_id'}, 'imported' );
270 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=".$cgiparams->{'basketno'});
274 my $budgets = GetBudgets();
275 my $budget_id = @$budgets[0]->{'budget_id'};
276 # build bookfund list
277 my $borrower = GetMember( 'borrowernumber' => $loggedinuser );
278 my ( $flags, $homebranch ) = ( $borrower->{'flags'}, $borrower->{'branchcode'} );
279 my $budget = GetBudget($budget_id);
282 my $budget_loop = [];
283 $budgets = GetBudgetHierarchy;
284 foreach my $r ( @{$budgets} ) {
285 next unless (CanUserUseBudget($borrower, $r, $userflags));
286 if ( !defined $r->{budget_amount} || $r->{budget_amount} == 0 ) {
289 push @{$budget_loop},
290 { b_id => $r->{budget_id},
291 b_txt => $r->{budget_name},
292 b_sel => ( $r->{budget_id} == $budget_id ) ? 1 : 0,
295 $template->param( budget_loop => $budget_loop,);
298 if ($budget) { # its a mod ..
299 if ( defined $budget->{'sort1_authcat'} ) { # with custom Asort* planning values
300 $CGIsort1 = GetAuthvalueDropbox( $budget->{'sort1_authcat'}, $data->{'sort1'} );
302 } elsif ( scalar(@$budgets) ) {
303 $CGIsort1 = GetAuthvalueDropbox( @$budgets[0]->{'sort1_authcat'}, '' );
305 # if CGIsort is successfully fetched, the use it
306 # else - failback to plain input-field
308 $template->param( CGIsort1 => $CGIsort1 );
310 $template->param( sort1 => $data->{'sort1'} );
315 if ( defined $budget->{'sort2_authcat'} ) {
316 $CGIsort2 = GetAuthvalueDropbox( $budget->{'sort2_authcat'}, $data->{'sort2'} );
318 } elsif ( scalar(@$budgets) ) {
319 $CGIsort2 = GetAuthvalueDropbox( @$budgets[0]->{sort2_authcat}, '' );
322 $template->param( CGIsort2 => $CGIsort2 );
324 $template->param( sort2 => $data->{'sort2'} );
327 output_html_with_http_headers $input, $cookie, $template->output;
330 sub import_batches_list {
332 my $batches = GetImportBatchRangeDesc();
335 foreach my $batch (@$batches) {
336 if ($batch->{'import_status'} eq "staged") {
337 # check if there is at least 1 line still staged
338 my $stagedList=GetImportRecordsRange($batch->{'import_batch_id'}, undef, undef, 'staged');
339 if (scalar @$stagedList) {
340 my ($staged_date, $staged_hour) = split (/ /, $batch->{'upload_timestamp'});
342 import_batch_id => $batch->{'import_batch_id'},
343 num_biblios => $batch->{'num_biblios'},
344 num_items => $batch->{'num_items'},
345 staged_date => $staged_date,
346 staged_hour => $staged_hour,
347 import_status => $batch->{'import_status'},
348 file_name => $batch->{'file_name'},
349 comments => $batch->{'comments'},
352 # if there are no more line to includes, set the status to imported
353 SetImportBatchStatus( $batch->{'import_batch_id'}, 'imported' );
357 $template->param(batch_list => \@list);
358 my $num_batches = GetNumberOfNonZ3950ImportBatches();
359 $template->param(num_results => $num_batches);
362 sub import_biblios_list {
363 my ($template, $import_batch_id) = @_;
364 my $batch = GetImportBatch($import_batch_id,'staged');
365 my $biblios = GetImportRecordsRange($import_batch_id,'','','staged');
368 foreach my $biblio (@$biblios) {
369 my $citation = $biblio->{'title'};
370 $citation .= " $biblio->{'author'}" if $biblio->{'author'};
371 $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
372 $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
373 $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
374 $citation .= $biblio->{'issn'} if $biblio->{'issn'};
375 $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
376 my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
378 import_record_id => $biblio->{'import_record_id'},
379 citation => $citation,
381 status => $biblio->{'status'},
382 record_sequence => $biblio->{'record_sequence'},
383 overlay_status => $biblio->{'overlay_status'},
384 match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
385 match_citation => $#$match > -1 ? $match->[0]->{'title'} . ' ' . $match->[0]->{'author'} : '',
386 match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
388 push @list, \%cellrecord;
390 my $num_biblios = $batch->{'num_biblios'};
391 my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
392 my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
393 my $item_action = GetImportBatchItemAction($import_batch_id);
394 $template->param(biblio_list => \@list,
395 num_results => $num_biblios,
396 import_batch_id => $import_batch_id,
397 "overlay_action_${overlay_action}" => 1,
398 overlay_action => $overlay_action,
399 "nomatch_action_${nomatch_action}" => 1,
400 nomatch_action => $nomatch_action,
401 "item_action_${item_action}" => 1,
402 item_action => $item_action
404 batch_info($template, $batch);
408 my ($template, $batch) = @_;
409 $template->param(batch_info => 1,
410 file_name => $batch->{'file_name'},
411 comments => $batch->{'comments'},
412 import_status => $batch->{'import_status'},
413 upload_timestamp => $batch->{'upload_timestamp'},
414 num_biblios => $batch->{'num_biblios'},
415 num_items => $batch->{'num_biblios'});
416 if ($batch->{'num_biblios'} > 0) {
417 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
418 $template->param(can_commit => 1);
420 if ($batch->{'import_status'} eq 'imported') {
421 $template->param(can_revert => 1);
424 if (defined $batch->{'matcher_id'}) {
425 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
426 if (defined $matcher) {
427 $template->param('current_matcher_id' => $batch->{'matcher_id'},
428 'current_matcher_code' => $matcher->code(),
429 'current_matcher_description' => $matcher->description());
432 add_matcher_list($batch->{'matcher_id'});
435 sub add_matcher_list {
436 my $current_matcher_id = shift;
437 my @matchers = C4::Matcher::GetMatcherList();
438 if (defined $current_matcher_id) {
439 for (my $i = 0; $i <= $#matchers; $i++) {
440 if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
441 $matchers[$i]->{'selected'} = 1;
445 $template->param(available_matchers => \@matchers);