(mantis 1475) uncertains price, don't set if we find a price in MARC
[koha.git] / acqui / addorderiso2709.pl
1 #!/usr/bin/perl
2
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
7
8 # Copyright 2008 - 2009 BibLibre SARL
9 #
10 # This file is part of Koha.
11 #
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.
16 #
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.
20 #
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
24
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/;
41
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 $template->param(scriptname => "/cgi-bin/koha/acqui/addorderiso2709.pl");
54 my $ordnum;
55
56 if ($cgiparams->{'import_batch_id'} && $op eq ""){
57     $op = "batch_details";
58 }
59
60 #Needed parameters:
61 if (! $cgiparams->{'basketno'}){
62     die "Basketnumber required to order from iso2709 file import";
63 }
64
65 if ($op eq ""){
66     $template->param("basketno" => $cgiparams->{'basketno'});
67 #display batches
68     import_batches_list($template);
69 } elsif ($op eq "batch_details"){
70 #display lines inside the selected batch
71     $template->param("batch_details" => 1,
72                      "basketno"      => $cgiparams->{'basketno'});
73     import_biblios_list($template, $cgiparams->{'import_batch_id'});
74     
75 } elsif ($op eq 'import_records'){
76 #import selected lines
77     $template->param('basketno' => $cgiparams->{'basketno'});
78 # Budget_id is mandatory for adding an order, we just add a default, the user needs to modify this aftewards
79     my $budgets = GetBudgets();
80     if (scalar @$budgets == 0){
81         die "No budgets defined, can't continue";
82     }
83     my $budget_id = @$budgets[0]->{'budget_id'};
84 #get all records from a batch, and check their import status to see if they are checked.
85 #(default values: quantity 1, uncertainprice yes, first budget)
86
87     # retrieve the file you want to import
88     my $import_batch_id = $cgiparams->{'import_batch_id'};
89     my $biblios = GetImportBibliosRange($import_batch_id);
90     for my $biblio (@$biblios){
91         if($cgiparams->{'order-'.$biblio->{'import_record_id'}}){
92             my ($marcblob, $encoding) = GetImportRecordMarc($biblio->{'import_record_id'});
93             my $marcrecord = MARC::Record->new_from_usmarc($marcblob) || die "couldn't translate marc information";
94             my ($duplicatetitle, $biblionumber);
95             if(!(($biblionumber,$duplicatetitle) = FindDuplicate($marcrecord))){
96 #FIXME: missing: marc21 support (should be same with different field)
97                 if ( C4::Context->preference("marcflavour") eq 'UNIMARC' ) {
98                     my $itemtypeid = "itemtype-" . $biblio->{'import_record_id'};
99                     $marcrecord->field(200)->update("b" => $cgiparams->{$itemtypeid});
100                 }
101                 # add the biblio
102                 my $bibitemnum;
103                 # remove ISBN -
104                 my ($isbnfield,$isbnsubfield) = GetMarcFromKohaField('biblioitems.isbn','');
105                 if ( $marcrecord->field($isbnfield) ) {
106                     foreach my $field ( $marcrecord->field($isbnfield) ) {
107                         foreach my $subfield ( $field->subfield($isbnsubfield) ) {
108                             my $newisbn = $field->subfield($isbnsubfield);
109                             $newisbn =~ s/-//g;
110                             $field->update( $isbnsubfield => $newisbn );
111                         }
112                     }
113                 }
114
115                 ( $biblionumber, $bibitemnum ) = AddBiblio( $marcrecord, $cgiparams->{'frameworkcode'} || '' );
116             } else {
117                 warn("Duplicate item found: ", $biblionumber, "; Duplicate: ", $duplicatetitle);
118             }
119             if (C4::Context->preference("BiblioAddsAuthorities")){
120                 my ($countlinked,$countcreated)=BiblioAddAuthorities($marcrecord, $cgiparams->{'frameworkcode'});
121             }
122             my $patron = C4::Members->GetMember($loggedinuser);
123             my $branch = C4::Branch->GetBranchDetail($patron->{branchcode});
124             my ($invoice);
125             my %orderinfo = ("biblionumber", $biblionumber,
126                             "basketno", $cgiparams->{'basketno'},
127                             "quantity", $cgiparams->{'quantityrec-' . $biblio->{'import_record_id'}},
128                             "branchcode", $branch,
129                             "booksellerinvoicenumber", $invoice,
130                             "budget_id", $budget_id,
131                             "uncertainprice", 1,
132                             );
133             # get the price if there is one.
134             # filter by storing only the 1st number
135             # we suppose the currency is correct, as we have no possibilities to get it.
136             if ($marcrecord->subfield("345","d")) {
137               $orderinfo{'listprice'} = $marcrecord->subfield("345","d");
138               if ($orderinfo{'listprice'} =~ /^([\d\.,]*)/) {
139                   $orderinfo{'listprice'} = $1;
140                   $orderinfo{'listprice'} =~ s/,/\./;
141                   eval "use C4::Acquisition qw/GetBasket/;";
142                   eval "use C4::Bookseller qw/GetBookSellerFromId/;";
143                   my $basket = GetBasket($orderinfo{basketno});
144                   my $bookseller = GetBookSellerFromId($basket->{booksellerid});
145                   my $gst = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
146                   $orderinfo{'unitprice'} = $orderinfo{listprice} - ($orderinfo{listprice} * ($bookseller->{discount} / 100));
147                   $orderinfo{'ecost'} = $orderinfo{unitprice};
148               } else {
149                   $orderinfo{'listprice'} = 0;
150               }
151               $orderinfo{'rrp'} = $orderinfo{'listprice'};
152             }
153             elsif ($marcrecord->subfield("010","d")) {
154               $orderinfo{'listprice'} = $marcrecord->subfield("010","d");
155               if ($orderinfo{'listprice'} =~ /^([\d\.,]*)/) {
156                   $orderinfo{'listprice'} = $1;
157                   $orderinfo{'listprice'} =~ s/,/\./;
158                   eval "use C4::Acquisition qw/GetBasket/;";
159                   eval "use C4::Bookseller qw/GetBookSellerFromId/;";
160                   my $basket = GetBasket($orderinfo{basketno});
161                   my $bookseller = GetBookSellerFromId($basket->{booksellerid});
162                   my $gst = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
163                   $orderinfo{'unitprice'} = $orderinfo{listprice} - ($orderinfo{listprice} * ($bookseller->{discount} / 100));
164                   $orderinfo{'ecost'} = $orderinfo{unitprice};
165               } else {
166                   $orderinfo{'listprice'} = 0;
167               }
168               $orderinfo{'rrp'} = $orderinfo{'listprice'};
169             }
170             # remove uncertainprice flag if we have found a price in the MARC record
171             $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
172             my $basketno;
173             ( $basketno, $ordnum ) = NewOrder(\%orderinfo);
174
175             # now, add items if applicable
176             # parse all items sent by the form, and create an item just for the import_record_id we are dealing with
177             # this is not optimised, but it's working !
178             if (C4::Context->preference('AcqCreateItem') eq 'ordering') {
179                 my @tags         = $input->param('tag');
180                 my @subfields    = $input->param('subfield');
181                 my @field_values = $input->param('field_value');
182                 my @serials      = $input->param('serial');
183                 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.
184                 my @ind_tag      = $input->param('ind_tag');
185                 my @indicator    = $input->param('indicator');
186                 #Rebuilding ALL the data for items into a hash
187                 # parting them on $itemid.
188                 my %itemhash;
189                 my $range=scalar(@itemids);
190                 
191                 my $i = 0;
192                 my @items;
193                 for my $itemid (@itemids){
194                     my $realitemid;     #javascript generated random itemids, in the form itemid-randomnumber, $realitemid is the itemid, while $itemid is the itemide parsed from the html
195                     if ($itemid =~ m/(\d+)-.*/){
196                         my @splits = split(/-/, $itemid);
197                         $realitemid = $splits[0];
198                     }
199                     if ( ( $realitemid && $cgiparams->{'order-'. $realitemid} && $realitemid eq $biblio->{import_record_id}) || ($itemid && $cgiparams->{'order-'. $itemid} && $itemid eq $biblio->{import_record_id}) ){
200                         my ($item, $found);
201                         for my $tmpitem (@items){
202                             if ($tmpitem->{itemid} eq $itemid){
203                                 $item = $tmpitem;
204                                 $found = 1;
205                             }
206                         }
207                         push @{$item->{tags}}, @tags[$i];
208                         push @{$item->{subfields}}, @subfields[$i];
209                         push @{$item->{field_values}}, @field_values[$i];
210                         push @{$item->{ind_tag}}, @ind_tag[$i];
211                         push @{$item->{indicator}}, @indicator[$i];
212                         $item->{itemid} = $itemid;
213                         if (! $found){
214                              push @items, $item;
215                         }
216                     }
217                     ++$i
218                 }
219                 foreach my $item (@items){
220                         my $xml = TransformHtmlToXml( $item->{'tags'},
221                                                 $item->{'subfields'},
222                                                 $item->{'field_values'},
223                                                 $item->{'ind_tag'},
224                                                 $item->{'indicator'});
225                         my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
226                         my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
227                         NewOrderItem( $itemnumber, $ordnum);
228                 }
229             }
230         }
231     }
232     # go to basket page
233     print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=".$cgiparams->{'basketno'});
234     exit;
235 }
236 output_html_with_http_headers $input, $cookie, $template->output;
237
238
239 sub import_batches_list {
240     my ($template) = @_;
241     my $batches = GetImportBatchRangeDesc();
242
243     my @list = ();
244     foreach my $batch (@$batches) {
245         if ($batch->{'import_status'} eq "staged") {
246         push @list, {
247                 import_batch_id => $batch->{'import_batch_id'},
248                 num_biblios => $batch->{'num_biblios'},
249                 num_items => $batch->{'num_items'},
250                 upload_timestamp => $batch->{'upload_timestamp'},
251                 import_status => $batch->{'import_status'},
252                 file_name => $batch->{'file_name'},
253                 comments => $batch->{'comments'},
254             };
255         }
256     }
257     $template->param(batch_list => \@list); 
258     my $num_batches = GetNumberOfNonZ3950ImportBatches();
259     $template->param(num_results => $num_batches);
260 }
261
262 sub import_biblios_list {
263     my ($template, $import_batch_id) = @_;
264     my $batch = GetImportBatch($import_batch_id);
265     my $biblios = GetImportBibliosRange($import_batch_id);
266     my @list = ();
267 # Itemtype is mandatory for adding a biblioitem, we just add a default, the user needs to modify this aftewards
268     my $itemtypehash = GetItemTypes();
269     my @itemtypes;
270     for my $key (sort { $itemtypehash->{$a}->{description} cmp $itemtypehash->{$b}->{description} } keys %$itemtypehash) {
271         push(@itemtypes, $itemtypehash->{$key});
272     }
273     foreach my $biblio (@$biblios) {
274         my $citation = $biblio->{'title'};
275         $citation .= " $biblio->{'author'}" if $biblio->{'author'};
276         $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
277         $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
278         $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
279         $citation .= $biblio->{'issn'} if $biblio->{'issn'};
280         $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
281         my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
282         my %cellrecord = (
283             import_record_id => $biblio->{'import_record_id'},
284             citation => $citation,
285             import  => 1,
286             status => $biblio->{'status'},
287             record_sequence => $biblio->{'record_sequence'},
288             overlay_status => $biblio->{'overlay_status'},
289             match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
290             match_citation => $#$match > -1 ? $match->[0]->{'title'} . ' ' . $match->[0]->{'author'} : '',
291             match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
292             itemtypes => \@itemtypes,
293         );
294         if (C4::Context->preference('AcqCreateItem') eq 'ordering' && !$ordnum) {
295             # prepare empty item form
296             my $cell = PrepareItemrecordDisplay();
297             my @itemloop;
298             push @itemloop,$cell;
299             $cellrecord{'items'} = \@itemloop;
300         }
301         push @list, \%cellrecord;
302
303
304     }
305     my $num_biblios = $batch->{'num_biblios'};
306     my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
307     my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
308     my $item_action = GetImportBatchItemAction($import_batch_id);
309     $template->param(biblio_list => \@list,
310                                       num_results => $num_biblios,
311                                       import_batch_id => $import_batch_id,
312                                       "overlay_action_${overlay_action}" => 1,
313                                       overlay_action => $overlay_action,
314                                       "nomatch_action_${nomatch_action}" => 1,
315                                       nomatch_action => $nomatch_action,
316                                       "item_action_${item_action}" => 1,
317                                       item_action => $item_action
318                                      );
319     batch_info($template, $batch);
320 }
321
322 sub batch_info {
323     my ($template, $batch) = @_;
324     $template->param(batch_info => 1,
325                                       file_name => $batch->{'file_name'},
326                                           comments => $batch->{'comments'},
327                                           import_status => $batch->{'import_status'},
328                                           upload_timestamp => $batch->{'upload_timestamp'},
329                                           num_biblios => $batch->{'num_biblios'},
330                                           num_items => $batch->{'num_biblios'});
331     if ($batch->{'num_biblios'} > 0) {
332         if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
333             $template->param(can_commit => 1);
334         }
335         if ($batch->{'import_status'} eq 'imported') {
336             $template->param(can_revert => 1);
337         }
338     }
339     if (defined $batch->{'matcher_id'}) {
340         my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
341         if (defined $matcher) {
342             $template->param('current_matcher_id' => $batch->{'matcher_id'},
343                                               'current_matcher_code' => $matcher->code(),
344                                               'current_matcher_description' => $matcher->description());
345         }
346     }
347     add_matcher_list($batch->{'matcher_id'});
348 }
349
350 sub add_matcher_list {
351     my $current_matcher_id = shift;
352     my @matchers = C4::Matcher::GetMatcherList();
353     if (defined $current_matcher_id) {
354         for (my $i = 0; $i <= $#matchers; $i++) {
355             if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
356                 $matchers[$i]->{'selected'} = 1;
357             }
358         }
359     }
360     $template->param(available_matchers => \@matchers);
361 }