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
6 #written by john.soros@biblibre.com 01/12/2008
8 # Copyright 2008 - 2009 BibLibre SARL
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along
22 # with Koha; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 use C4::ImportBatch qw/GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches GetImportRecordMatches GetImportBibliosRange GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction GetImportRecordMarc GetImportBatch/;
34 use C4::Search qw/FindDuplicate BiblioAddAuthorities/;
35 use C4::Acquisition qw/NewOrder/;
38 use C4::Koha qw/GetItemTypes/;
39 use C4::Budgets qw/GetBudgets/;
40 use C4::Acquisition qw/NewOrderItem GetBasket/;
41 use C4::Bookseller qw/GetBookSellerFromId/;
44 my ($template, $loggedinuser, $cookie) = get_template_and_user({
45 template_name => "acqui/addorderiso2709.tmpl",
49 flagsrequired => { acquisition => 'order_manage' },
52 my $cgiparams = $input->Vars;
53 my $op = $cgiparams->{'op'};
54 my $booksellerid = $input->param('booksellerid');
55 my $bookseller = GetBookSellerFromId($booksellerid);
57 $template->param(scriptname => "/cgi-bin/koha/acqui/addorderiso2709.pl",
58 booksellerid => $booksellerid,
59 booksellername => $bookseller->{name},
63 if ($cgiparams->{'import_batch_id'} && $op eq ""){
64 $op = "batch_details";
68 if (! $cgiparams->{'basketno'}){
69 die "Basketnumber required to order from iso2709 file import";
73 $template->param("basketno" => $cgiparams->{'basketno'});
75 import_batches_list($template);
76 } elsif ($op eq "batch_details"){
77 #display lines inside the selected batch
78 $template->param("batch_details" => 1,
79 "basketno" => $cgiparams->{'basketno'});
80 import_biblios_list($template, $cgiparams->{'import_batch_id'});
82 } elsif ($op eq 'import_records'){
83 #import selected lines
84 $template->param('basketno' => $cgiparams->{'basketno'});
85 # Budget_id is mandatory for adding an order, we just add a default, the user needs to modify this aftewards
86 my $budgets = GetBudgets();
87 if (scalar @$budgets == 0){
88 die "No budgets defined, can't continue";
90 my $budget_id = @$budgets[0]->{'budget_id'};
91 #get all records from a batch, and check their import status to see if they are checked.
92 #(default values: quantity 1, uncertainprice yes, first budget)
94 # retrieve the file you want to import
95 my $import_batch_id = $cgiparams->{'import_batch_id'};
96 my $biblios = GetImportBibliosRange($import_batch_id);
97 for my $biblio (@$biblios){
98 if($cgiparams->{'order-'.$biblio->{'import_record_id'}}){
99 my ($marcblob, $encoding) = GetImportRecordMarc($biblio->{'import_record_id'});
100 my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
101 my ($duplicatetitle, $biblionumber);
102 if(!(($biblionumber,$duplicatetitle) = FindDuplicate($marcrecord))){
103 #FIXME: missing: marc21 support (should be same with different field)
104 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
105 my $itemtypeid = "itemtype-" . $biblio->{'import_record_id'};
106 $marcrecord->field(200)->update("b" => $cgiparams->{$itemtypeid});
111 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
112 if ( $marcrecord->field($isbnfield) ) {
113 foreach my $field ( $marcrecord->field($isbnfield) ) {
114 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
115 my $newisbn = $field->subfield($isbnsubfield);
117 $field->update( $isbnsubfield => $newisbn );
122 ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' );
124 warn("Duplicate item found: ", $biblionumber, "; Duplicate: ", $duplicatetitle);
126 if (C4::Context->preference("BiblioAddsAuthorities")){
127 my ($countlinked,$countcreated)=BiblioAddAuthorities($marcrecord, $cgiparams->{'frameworkcode'});
129 my $patron = C4::Members->GetMember(borrowernumber => $loggedinuser);
130 my $branch = C4::Branch->GetBranchDetail($patron->{branchcode});
132 my %orderinfo = ("biblionumber", $biblionumber,
133 "basketno", $cgiparams->{'basketno'},
134 "quantity", $cgiparams->{'quantityrec-' . $biblio->{'import_record_id'}},
135 "branchcode", $branch,
136 "booksellerinvoicenumber", $invoice,
137 "budget_id", $budget_id,
140 # get the price if there is one.
141 # filter by storing only the 1st number
142 # we suppose the currency is correct, as we have no possibilities to get it.
143 if ($marcrecord->subfield("345","d")) {
144 $orderinfo{'listprice'} = $marcrecord->subfield("345","d");
145 if ($orderinfo{'listprice'} =~ /^([\d\.,]*)/) {
146 $orderinfo{'listprice'} = $1;
147 $orderinfo{'listprice'} =~ s/,/\./;
148 my $basket = GetBasket($orderinfo{basketno});
149 my $bookseller = GetBookSellerFromId($basket->{booksellerid});
150 my $gst = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
151 $orderinfo{'unitprice'} = $orderinfo{listprice} - ($orderinfo{listprice} * ($bookseller->{discount} / 100));
152 $orderinfo{'ecost'} = $orderinfo{unitprice};
154 $orderinfo{'listprice'} = 0;
156 $orderinfo{'rrp'} = $orderinfo{'listprice'};
158 elsif ($marcrecord->subfield("010","d")) {
159 $orderinfo{'listprice'} = $marcrecord->subfield("010","d");
160 if ($orderinfo{'listprice'} =~ /^([\d\.,]*)/) {
161 $orderinfo{'listprice'} = $1;
162 $orderinfo{'listprice'} =~ s/,/\./;
163 my $basket = GetBasket($orderinfo{basketno});
164 my $bookseller = GetBookSellerFromId($basket->{booksellerid});
165 my $gst = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
166 $orderinfo{'unitprice'} = $orderinfo{listprice} - ($orderinfo{listprice} * ($bookseller->{discount} / 100));
167 $orderinfo{'ecost'} = $orderinfo{unitprice};
169 $orderinfo{'listprice'} = 0;
171 $orderinfo{'rrp'} = $orderinfo{'listprice'};
173 # remove uncertainprice flag if we have found a price in the MARC record
174 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
176 ( $basketno, $ordernumber ) = NewOrder(\%orderinfo);
178 # now, add items if applicable
179 # parse all items sent by the form, and create an item just for the import_record_id we are dealing with
180 # this is not optimised, but it's working !
181 if (C4::Context->preference('AcqCreateItem') eq 'ordering') {
182 my @tags = $input->param('tag');
183 my @subfields = $input->param('subfield');
184 my @field_values = $input->param('field_value');
185 my @serials = $input->param('serial');
186 my @itemids = $input->param('itemid'); # hint : in iso2709, the itemid contains the import_record_id, not an item id. It is used to get the right item, as we have X biblios.
187 my @ind_tag = $input->param('ind_tag');
188 my @indicator = $input->param('indicator');
189 #Rebuilding ALL the data for items into a hash
190 # parting them on $itemid.
192 my $range=scalar(@itemids);
196 for my $itemid (@itemids){
197 my $realitemid; #javascript generated random itemids, in the form itemid-randomnumber, $realitemid is the itemid, while $itemid is the itemide parsed from the html
198 if ($itemid =~ m/(\d+)-.*/){
199 my @splits = split(/-/, $itemid);
200 $realitemid = $splits[0];
202 if ( ( $realitemid && $cgiparams->{'order-'. $realitemid} && $realitemid eq $biblio->{import_record_id}) || ($itemid && $cgiparams->{'order-'. $itemid} && $itemid eq $biblio->{import_record_id}) ){
204 for my $tmpitem (@items){
205 if ($tmpitem->{itemid} eq $itemid){
210 push @{$item->{tags}}, $tags[$i];
211 push @{$item->{subfields}}, $subfields[$i];
212 push @{$item->{field_values}}, $field_values[$i];
213 push @{$item->{ind_tag}}, $ind_tag[$i];
214 push @{$item->{indicator}}, $indicator[$i];
215 $item->{itemid} = $itemid;
222 foreach my $item (@items){
223 my $xml = TransformHtmlToXml( $item->{'tags'},
224 $item->{'subfields'},
225 $item->{'field_values'},
227 $item->{'indicator'});
228 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
229 my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
230 NewOrderItem( $itemnumber, $ordernumber);
236 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=".$cgiparams->{'basketno'});
239 output_html_with_http_headers $input, $cookie, $template->output;
242 sub import_batches_list {
244 my $batches = GetImportBatchRangeDesc();
247 foreach my $batch (@$batches) {
248 if ($batch->{'import_status'} eq "staged") {
250 import_batch_id => $batch->{'import_batch_id'},
251 num_biblios => $batch->{'num_biblios'},
252 num_items => $batch->{'num_items'},
253 upload_timestamp => $batch->{'upload_timestamp'},
254 import_status => $batch->{'import_status'},
255 file_name => $batch->{'file_name'},
256 comments => $batch->{'comments'},
260 $template->param(batch_list => \@list);
261 my $num_batches = GetNumberOfNonZ3950ImportBatches();
262 $template->param(num_results => $num_batches);
265 sub import_biblios_list {
266 my ($template, $import_batch_id) = @_;
267 my $batch = GetImportBatch($import_batch_id,'staged');
268 my $biblios = GetImportBibliosRange($import_batch_id,'','','staged');
270 # # Itemtype is mandatory for adding a biblioitem, we just add a default, the user needs to modify this aftewards
271 # my $itemtypehash = GetItemTypes();
273 # for my $key (sort { $itemtypehash->{$a}->{description} cmp $itemtypehash->{$b}->{description} } keys %$itemtypehash) {
274 # push(@itemtypes, $itemtypehash->{$key});
276 foreach my $biblio (@$biblios) {
277 my $citation = $biblio->{'title'};
278 $citation .= " $biblio->{'author'}" if $biblio->{'author'};
279 $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
280 $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
281 $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
282 $citation .= $biblio->{'issn'} if $biblio->{'issn'};
283 $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
284 my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
286 import_record_id => $biblio->{'import_record_id'},
287 citation => $citation,
289 status => $biblio->{'status'},
290 record_sequence => $biblio->{'record_sequence'},
291 overlay_status => $biblio->{'overlay_status'},
292 match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
293 match_citation => $#$match > -1 ? $match->[0]->{'title'} . ' ' . $match->[0]->{'author'} : '',
294 match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
295 # itemtypes => \@itemtypes,
297 # if (C4::Context->preference('AcqCreateItem') eq 'ordering' && !$ordernumber) {
298 # # prepare empty item form
299 # my $cell = PrepareItemrecordDisplay();
301 # push @itemloop,$cell;
302 # $cellrecord{'items'} = \@itemloop;
304 push @list, \%cellrecord;
308 my $num_biblios = $batch->{'num_biblios'};
309 my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
310 my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
311 my $item_action = GetImportBatchItemAction($import_batch_id);
312 $template->param(biblio_list => \@list,
313 num_results => $num_biblios,
314 import_batch_id => $import_batch_id,
315 "overlay_action_${overlay_action}" => 1,
316 overlay_action => $overlay_action,
317 "nomatch_action_${nomatch_action}" => 1,
318 nomatch_action => $nomatch_action,
319 "item_action_${item_action}" => 1,
320 item_action => $item_action
322 batch_info($template, $batch);
326 my ($template, $batch) = @_;
327 $template->param(batch_info => 1,
328 file_name => $batch->{'file_name'},
329 comments => $batch->{'comments'},
330 import_status => $batch->{'import_status'},
331 upload_timestamp => $batch->{'upload_timestamp'},
332 num_biblios => $batch->{'num_biblios'},
333 num_items => $batch->{'num_biblios'});
334 if ($batch->{'num_biblios'} > 0) {
335 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
336 $template->param(can_commit => 1);
338 if ($batch->{'import_status'} eq 'imported') {
339 $template->param(can_revert => 1);
342 if (defined $batch->{'matcher_id'}) {
343 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
344 if (defined $matcher) {
345 $template->param('current_matcher_id' => $batch->{'matcher_id'},
346 'current_matcher_code' => $matcher->code(),
347 'current_matcher_description' => $matcher->description());
350 add_matcher_list($batch->{'matcher_id'});
353 sub add_matcher_list {
354 my $current_matcher_id = shift;
355 my @matchers = C4::Matcher::GetMatcherList();
356 if (defined $current_matcher_id) {
357 for (my $i = 0; $i <= $#matchers; $i++) {
358 if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
359 $matchers[$i]->{'selected'} = 1;
363 $template->param(available_matchers => \@matchers);