Changes to the appearance and behavior of the cart in the staff client (see Bug 3871)
[koha.git] / acqui / addorderiso2709.pl
blob3c05ca0ae75b2f9a4c6d3520ab16b70fac3af09e
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
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
15 # version.
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 with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA 02111-1307 USA
25 use strict;
26 use warnings;
27 use CGI;
28 use C4::Context;
29 use C4::Auth;
30 use C4::Input;
31 use C4::Output;
32 use C4::ImportBatch qw/GetImportBatchRangeDesc GetNumberOfNonZ3950ImportBatches GetImportRecordMatches GetImportBibliosRange GetImportBatchOverlayAction GetImportBatchNoMatchAction GetImportBatchItemAction GetImportRecordMarc GetImportBatch/;
33 use C4::Matcher;
34 use C4::Search qw/FindDuplicate BiblioAddAuthorities/;
35 use C4::Acquisition qw/NewOrder/;
36 use C4::Biblio;
37 use C4::Items;
38 use C4::Koha qw/GetItemTypes/;
39 use C4::Budgets qw/GetBudgets/;
40 use C4::Acquisition qw/NewOrderItem/;
42 my $input = new CGI;
43 my ($template, $loggedinuser, $cookie) = get_template_and_user({
44 template_name => "acqui/addorderiso2709.tmpl",
45 query => $input,
46 type => "intranet",
47 authnotrequired => 0,
48 flagsrequired => { acquisition => 'order_manage' },
49 debug => 1,
50 });
51 my $cgiparams = $input->Vars;
52 my $op = $cgiparams->{'op'};
53 my $booksellerid = $input->param('booksellerid');
55 $template->param(scriptname => "/cgi-bin/koha/acqui/addorderiso2709.pl",
56 booksellerid => $booksellerid,
58 my $ordernumber;
60 if ($cgiparams->{'import_batch_id'} && $op eq ""){
61 $op = "batch_details";
64 #Needed parameters:
65 if (! $cgiparams->{'basketno'}){
66 die "Basketnumber required to order from iso2709 file import";
69 if ($op eq ""){
70 $template->param("basketno" => $cgiparams->{'basketno'});
71 #display batches
72 import_batches_list($template);
73 } elsif ($op eq "batch_details"){
74 #display lines inside the selected batch
75 $template->param("batch_details" => 1,
76 "basketno" => $cgiparams->{'basketno'});
77 import_biblios_list($template, $cgiparams->{'import_batch_id'});
79 } elsif ($op eq 'import_records'){
80 #import selected lines
81 $template->param('basketno' => $cgiparams->{'basketno'});
82 # Budget_id is mandatory for adding an order, we just add a default, the user needs to modify this aftewards
83 my $budgets = GetBudgets();
84 if (scalar @$budgets == 0){
85 die "No budgets defined, can't continue";
87 my $budget_id = @$budgets[0]->{'budget_id'};
88 #get all records from a batch, and check their import status to see if they are checked.
89 #(default values: quantity 1, uncertainprice yes, first budget)
91 # retrieve the file you want to import
92 my $import_batch_id = $cgiparams->{'import_batch_id'};
93 my $biblios = GetImportBibliosRange($import_batch_id);
94 for my $biblio (@$biblios){
95 if($cgiparams->{'order-'.$biblio->{'import_record_id'}}){
96 my ($marcblob, $encoding) = GetImportRecordMarc($biblio->{'import_record_id'});
97 my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
98 my ($duplicatetitle, $biblionumber);
99 if(!(($biblionumber,$duplicatetitle) = FindDuplicate($marcrecord))){
100 #FIXME: missing: marc21 support (should be same with different field)
101 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
102 my $itemtypeid = "itemtype-" . $biblio->{'import_record_id'};
103 $marcrecord->field(200)->update("b" => $cgiparams->{$itemtypeid});
105 # add the biblio
106 my $bibitemnum;
107 # remove ISBN -
108 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
109 if ( $marcrecord->field($isbnfield) ) {
110 foreach my $field ( $marcrecord->field($isbnfield) ) {
111 foreach my $subfield ( $field->subfield($isbnsubfield) ) {
112 my $newisbn = $field->subfield($isbnsubfield);
113 $newisbn =~ s/-//g;
114 $field->update( $isbnsubfield => $newisbn );
119 ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' );
120 } else {
121 warn("Duplicate item found: ", $biblionumber, "; Duplicate: ", $duplicatetitle);
123 if (C4::Context->preference("BiblioAddsAuthorities")){
124 my ($countlinked,$countcreated)=BiblioAddAuthorities($marcrecord, $cgiparams->{'frameworkcode'});
126 my $patron = C4::Members->GetMember($loggedinuser);
127 my $branch = C4::Branch->GetBranchDetail($patron->{branchcode});
128 my ($invoice);
129 my %orderinfo = ("biblionumber", $biblionumber,
130 "basketno", $cgiparams->{'basketno'},
131 "quantity", $cgiparams->{'quantityrec-' . $biblio->{'import_record_id'}},
132 "branchcode", $branch,
133 "booksellerinvoicenumber", $invoice,
134 "budget_id", $budget_id,
135 "uncertainprice", 1,
137 # get the price if there is one.
138 # filter by storing only the 1st number
139 # we suppose the currency is correct, as we have no possibilities to get it.
140 if ($marcrecord->subfield("345","d")) {
141 $orderinfo{'listprice'} = $marcrecord->subfield("345","d");
142 if ($orderinfo{'listprice'} =~ /^([\d\.,]*)/) {
143 $orderinfo{'listprice'} = $1;
144 $orderinfo{'listprice'} =~ s/,/\./;
145 eval "use C4::Acquisition qw/GetBasket/;";
146 eval "use C4::Bookseller qw/GetBookSellerFromId/;";
147 my $basket = GetBasket($orderinfo{basketno});
148 my $bookseller = GetBookSellerFromId($basket->{booksellerid});
149 my $gst = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
150 $orderinfo{'unitprice'} = $orderinfo{listprice} - ($orderinfo{listprice} * ($bookseller->{discount} / 100));
151 $orderinfo{'ecost'} = $orderinfo{unitprice};
152 } else {
153 $orderinfo{'listprice'} = 0;
155 $orderinfo{'rrp'} = $orderinfo{'listprice'};
157 elsif ($marcrecord->subfield("010","d")) {
158 $orderinfo{'listprice'} = $marcrecord->subfield("010","d");
159 if ($orderinfo{'listprice'} =~ /^([\d\.,]*)/) {
160 $orderinfo{'listprice'} = $1;
161 $orderinfo{'listprice'} =~ s/,/\./;
162 eval "use C4::Acquisition qw/GetBasket/;";
163 eval "use C4::Bookseller qw/GetBookSellerFromId/;";
164 my $basket = GetBasket($orderinfo{basketno});
165 my $bookseller = GetBookSellerFromId($basket->{booksellerid});
166 my $gst = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
167 $orderinfo{'unitprice'} = $orderinfo{listprice} - ($orderinfo{listprice} * ($bookseller->{discount} / 100));
168 $orderinfo{'ecost'} = $orderinfo{unitprice};
169 } else {
170 $orderinfo{'listprice'} = 0;
172 $orderinfo{'rrp'} = $orderinfo{'listprice'};
174 # remove uncertainprice flag if we have found a price in the MARC record
175 $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
176 my $basketno;
177 ( $basketno, $ordernumber ) = NewOrder(\%orderinfo);
179 # now, add items if applicable
180 # parse all items sent by the form, and create an item just for the import_record_id we are dealing with
181 # this is not optimised, but it's working !
182 if (C4::Context->preference('AcqCreateItem') eq 'ordering') {
183 my @tags = $input->param('tag');
184 my @subfields = $input->param('subfield');
185 my @field_values = $input->param('field_value');
186 my @serials = $input->param('serial');
187 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.
188 my @ind_tag = $input->param('ind_tag');
189 my @indicator = $input->param('indicator');
190 #Rebuilding ALL the data for items into a hash
191 # parting them on $itemid.
192 my %itemhash;
193 my $range=scalar(@itemids);
195 my $i = 0;
196 my @items;
197 for my $itemid (@itemids){
198 my $realitemid; #javascript generated random itemids, in the form itemid-randomnumber, $realitemid is the itemid, while $itemid is the itemide parsed from the html
199 if ($itemid =~ m/(\d+)-.*/){
200 my @splits = split(/-/, $itemid);
201 $realitemid = $splits[0];
203 if ( ( $realitemid && $cgiparams->{'order-'. $realitemid} && $realitemid eq $biblio->{import_record_id}) || ($itemid && $cgiparams->{'order-'. $itemid} && $itemid eq $biblio->{import_record_id}) ){
204 my ($item, $found);
205 for my $tmpitem (@items){
206 if ($tmpitem->{itemid} eq $itemid){
207 $item = $tmpitem;
208 $found = 1;
211 push @{$item->{tags}}, $tags[$i];
212 push @{$item->{subfields}}, $subfields[$i];
213 push @{$item->{field_values}}, $field_values[$i];
214 push @{$item->{ind_tag}}, $ind_tag[$i];
215 push @{$item->{indicator}}, $indicator[$i];
216 $item->{itemid} = $itemid;
217 if (! $found){
218 push @items, $item;
221 ++$i
223 foreach my $item (@items){
224 my $xml = TransformHtmlToXml( $item->{'tags'},
225 $item->{'subfields'},
226 $item->{'field_values'},
227 $item->{'ind_tag'},
228 $item->{'indicator'});
229 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
230 my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
231 NewOrderItem( $itemnumber, $ordernumber);
236 # go to basket page
237 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=".$cgiparams->{'basketno'});
238 exit;
240 output_html_with_http_headers $input, $cookie, $template->output;
243 sub import_batches_list {
244 my ($template) = @_;
245 my $batches = GetImportBatchRangeDesc();
247 my @list = ();
248 foreach my $batch (@$batches) {
249 if ($batch->{'import_status'} eq "staged") {
250 push @list, {
251 import_batch_id => $batch->{'import_batch_id'},
252 num_biblios => $batch->{'num_biblios'},
253 num_items => $batch->{'num_items'},
254 upload_timestamp => $batch->{'upload_timestamp'},
255 import_status => $batch->{'import_status'},
256 file_name => $batch->{'file_name'},
257 comments => $batch->{'comments'},
261 $template->param(batch_list => \@list);
262 my $num_batches = GetNumberOfNonZ3950ImportBatches();
263 $template->param(num_results => $num_batches);
266 sub import_biblios_list {
267 my ($template, $import_batch_id) = @_;
268 my $batch = GetImportBatch($import_batch_id,'staged');
269 my $biblios = GetImportBibliosRange($import_batch_id,'','','staged');
270 my @list = ();
271 # # Itemtype is mandatory for adding a biblioitem, we just add a default, the user needs to modify this aftewards
272 # my $itemtypehash = GetItemTypes();
273 # my @itemtypes;
274 # for my $key (sort { $itemtypehash->{$a}->{description} cmp $itemtypehash->{$b}->{description} } keys %$itemtypehash) {
275 # push(@itemtypes, $itemtypehash->{$key});
277 foreach my $biblio (@$biblios) {
278 my $citation = $biblio->{'title'};
279 $citation .= " $biblio->{'author'}" if $biblio->{'author'};
280 $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
281 $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
282 $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
283 $citation .= $biblio->{'issn'} if $biblio->{'issn'};
284 $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
285 my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
286 my %cellrecord = (
287 import_record_id => $biblio->{'import_record_id'},
288 citation => $citation,
289 import => 1,
290 status => $biblio->{'status'},
291 record_sequence => $biblio->{'record_sequence'},
292 overlay_status => $biblio->{'overlay_status'},
293 match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
294 match_citation => $#$match > -1 ? $match->[0]->{'title'} . ' ' . $match->[0]->{'author'} : '',
295 match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
296 # itemtypes => \@itemtypes,
298 # if (C4::Context->preference('AcqCreateItem') eq 'ordering' && !$ordernumber) {
299 # # prepare empty item form
300 # my $cell = PrepareItemrecordDisplay();
301 # my @itemloop;
302 # push @itemloop,$cell;
303 # $cellrecord{'items'} = \@itemloop;
305 push @list, \%cellrecord;
309 my $num_biblios = $batch->{'num_biblios'};
310 my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
311 my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
312 my $item_action = GetImportBatchItemAction($import_batch_id);
313 $template->param(biblio_list => \@list,
314 num_results => $num_biblios,
315 import_batch_id => $import_batch_id,
316 "overlay_action_${overlay_action}" => 1,
317 overlay_action => $overlay_action,
318 "nomatch_action_${nomatch_action}" => 1,
319 nomatch_action => $nomatch_action,
320 "item_action_${item_action}" => 1,
321 item_action => $item_action
323 batch_info($template, $batch);
326 sub batch_info {
327 my ($template, $batch) = @_;
328 $template->param(batch_info => 1,
329 file_name => $batch->{'file_name'},
330 comments => $batch->{'comments'},
331 import_status => $batch->{'import_status'},
332 upload_timestamp => $batch->{'upload_timestamp'},
333 num_biblios => $batch->{'num_biblios'},
334 num_items => $batch->{'num_biblios'});
335 if ($batch->{'num_biblios'} > 0) {
336 if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
337 $template->param(can_commit => 1);
339 if ($batch->{'import_status'} eq 'imported') {
340 $template->param(can_revert => 1);
343 if (defined $batch->{'matcher_id'}) {
344 my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
345 if (defined $matcher) {
346 $template->param('current_matcher_id' => $batch->{'matcher_id'},
347 'current_matcher_code' => $matcher->code(),
348 'current_matcher_description' => $matcher->description());
351 add_matcher_list($batch->{'matcher_id'});
354 sub add_matcher_list {
355 my $current_matcher_id = shift;
356 my @matchers = C4::Matcher::GetMatcherList();
357 if (defined $current_matcher_id) {
358 for (my $i = 0; $i <= $#matchers; $i++) {
359 if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
360 $matchers[$i]->{'selected'} = 1;
364 $template->param(available_matchers => \@matchers);