addorderiso2709
[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 #                                           "budgetdate", $budget,
129                                            "branchcode", $branch,
130                                            "booksellerinvoicenumber", $invoice,
131                                            "budget_id", $budget_id,
132                                            "uncertainprice", 1,
133                                            );
134             # get the price if there is one.
135             # filter by storing only the 1st number
136             # we suppose the currency is correct, as we have no possibilities to get it.
137             if ($marcrecord->subfield("345","d")) {
138               $orderinfo{'listprice'} = $marcrecord->subfield("345","d");
139               if ($orderinfo{'listprice'} =~ /^([\d\.,]*)/) {
140                   $orderinfo{'listprice'} = $1;
141                   $orderinfo{'listprice'} =~ s/,/\./;
142                   eval "use C4::Acquisition qw/GetBasket/;";
143                   eval "use C4::Bookseller qw/GetBookSellerFromId/;";
144                   my $basket = GetBasket($orderinfo{basketno});
145                   my $bookseller = GetBookSellerFromId($basket->{booksellerid});
146                   my $gst = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
147                   $orderinfo{'unitprice'} = $orderinfo{listprice} - ($orderinfo{listprice} * ($bookseller->{discount} / 100));
148                   $orderinfo{'ecost'} = $orderinfo{unitprice};
149               } else {
150                   $orderinfo{'listprice'} = 0;
151               }
152               $orderinfo{'rrp'} = $orderinfo{'listprice'};
153             }
154             elsif ($marcrecord->subfield("010","d")) {
155               $orderinfo{'listprice'} = $marcrecord->subfield("010","d");
156               if ($orderinfo{'listprice'} =~ /^([\d\.,]*)/) {
157                   $orderinfo{'listprice'} = $1;
158                   $orderinfo{'listprice'} =~ s/,/\./;
159                   eval "use C4::Acquisition qw/GetBasket/;";
160                   eval "use C4::Bookseller qw/GetBookSellerFromId/;";
161                   my $basket = GetBasket($orderinfo{basketno});
162                   my $bookseller = GetBookSellerFromId($basket->{booksellerid});
163                   my $gst = $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
164                   $orderinfo{'unitprice'} = $orderinfo{listprice} - ($orderinfo{listprice} * ($bookseller->{discount} / 100));
165                   $orderinfo{'ecost'} = $orderinfo{unitprice};
166               } else {
167                   $orderinfo{'listprice'} = 0;
168               }
169               $orderinfo{'rrp'} = $orderinfo{'listprice'};
170             }
171             my $basketno;
172             ( $basketno, $ordnum ) = NewOrder(\%orderinfo);
173
174             # now, add items if applicable
175             # parse all items sent by the form, and create an item just for the import_record_id we are dealing with
176             # this is not optimised, but it's working !
177             if (C4::Context->preference('AcqCreateItem') eq 'ordering') {
178                 my @tags         = $input->param('tag');
179                 my @subfields    = $input->param('subfield');
180                 my @field_values = $input->param('field_value');
181                 my @serials      = $input->param('serial');
182                 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.
183                 my @ind_tag      = $input->param('ind_tag');
184                 my @indicator    = $input->param('indicator');
185                 #Rebuilding ALL the data for items into a hash
186                 # parting them on $itemid.
187                 my %itemhash;
188                 my $range=scalar(@itemids);
189                 
190                 my $i = 0;
191                 my @items;
192                 for my $itemid (@itemids){
193                     my $realitemid;     #javascript generated random itemids, in the form itemid-randomnumber, $realitemid is the itemid, while $itemid is the itemide parsed from the html
194                     if ($itemid =~ m/(\d+)-.*/){
195                         my @splits = split(/-/, $itemid);
196                         $realitemid = $splits[0];
197                     }
198                     if ( ( $realitemid && $cgiparams->{'order-'. $realitemid} && $realitemid eq $biblio->{import_record_id}) || ($itemid && $cgiparams->{'order-'. $itemid} && $itemid eq $biblio->{import_record_id}) ){
199                         my ($item, $found);
200                         for my $tmpitem (@items){
201                             if ($tmpitem->{itemid} eq $itemid){
202                                 $item = $tmpitem;
203                                 $found = 1;
204                             }
205                         }
206                         push @{$item->{tags}}, @tags[$i];
207                         push @{$item->{subfields}}, @subfields[$i];
208                         push @{$item->{field_values}}, @field_values[$i];
209                         push @{$item->{ind_tag}}, @ind_tag[$i];
210                         push @{$item->{indicator}}, @indicator[$i];
211                         $item->{itemid} = $itemid;
212                         if (! $found){
213                              push @items, $item;
214                         }
215                     }
216                     ++$i
217                 }
218                 foreach my $item (@items){
219                         my $xml = TransformHtmlToXml( $item->{'tags'},
220                                                 $item->{'subfields'},
221                                                 $item->{'field_values'},
222                                                 $item->{'ind_tag'},
223                                                 $item->{'indicator'});
224                         my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
225                         my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
226                         NewOrderItem( $itemnumber, $ordnum);
227                 }
228             }
229         }
230     }
231     # go to basket page
232     print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=".$cgiparams->{'basketno'});
233     exit;
234 }
235 output_html_with_http_headers $input, $cookie, $template->output;
236
237
238 sub import_batches_list {
239     my ($template) = @_;
240     my $batches = GetImportBatchRangeDesc();
241
242     my @list = ();
243     foreach my $batch (@$batches) {
244         if ($batch->{'import_status'} eq "staged") {
245         push @list, {
246                 import_batch_id => $batch->{'import_batch_id'},
247                 num_biblios => $batch->{'num_biblios'},
248                 num_items => $batch->{'num_items'},
249                 upload_timestamp => $batch->{'upload_timestamp'},
250                 import_status => $batch->{'import_status'},
251                 file_name => $batch->{'file_name'},
252                 comments => $batch->{'comments'},
253             };
254         }
255     }
256     $template->param(batch_list => \@list); 
257     my $num_batches = GetNumberOfNonZ3950ImportBatches();
258     $template->param(num_results => $num_batches);
259 }
260
261 sub import_biblios_list {
262     my ($template, $import_batch_id) = @_;
263     my $batch = GetImportBatch($import_batch_id);
264     my $biblios = GetImportBibliosRange($import_batch_id);
265     my @list = ();
266 # Itemtype is mandatory for adding a biblioitem, we just add a default, the user needs to modify this aftewards
267     my $itemtypehash = GetItemTypes();
268     my @itemtypes;
269     for my $key (keys %$itemtypehash) {
270         push(@itemtypes, $itemtypehash->{$key});
271     }
272     foreach my $biblio (@$biblios) {
273         my $citation = $biblio->{'title'};
274         $citation .= " $biblio->{'author'}" if $biblio->{'author'};
275         $citation .= " (" if $biblio->{'issn'} or $biblio->{'isbn'};
276         $citation .= $biblio->{'isbn'} if $biblio->{'isbn'};
277         $citation .= ", " if $biblio->{'issn'} and $biblio->{'isbn'};
278         $citation .= $biblio->{'issn'} if $biblio->{'issn'};
279         $citation .= ")" if $biblio->{'issn'} or $biblio->{'isbn'};
280         my $match = GetImportRecordMatches($biblio->{'import_record_id'}, 1);
281         my %cellrecord = (
282             import_record_id => $biblio->{'import_record_id'},
283             citation => $citation,
284             import  => 1,
285             status => $biblio->{'status'},
286             record_sequence => $biblio->{'record_sequence'},
287             overlay_status => $biblio->{'overlay_status'},
288             match_biblionumber => $#$match > -1 ? $match->[0]->{'biblionumber'} : 0,
289             match_citation => $#$match > -1 ? $match->[0]->{'title'} . ' ' . $match->[0]->{'author'} : '',
290             match_score => $#$match > -1 ? $match->[0]->{'score'} : 0,
291             itemtypes => \@itemtypes,
292         );
293         if (C4::Context->preference('AcqCreateItem') eq 'ordering' && !$ordnum) {
294             # prepare empty item form
295             my $cell = PrepareItemrecordDisplay();
296             my @itemloop;
297             push @itemloop,$cell;
298             $cellrecord{'items'} = \@itemloop;
299         }
300         push @list, \%cellrecord;
301
302
303     }
304     my $num_biblios = $batch->{'num_biblios'};
305     my $overlay_action = GetImportBatchOverlayAction($import_batch_id);
306     my $nomatch_action = GetImportBatchNoMatchAction($import_batch_id);
307     my $item_action = GetImportBatchItemAction($import_batch_id);
308     $template->param(biblio_list => \@list,
309                                       num_results => $num_biblios,
310                                       import_batch_id => $import_batch_id,
311                                       "overlay_action_${overlay_action}" => 1,
312                                       overlay_action => $overlay_action,
313                                       "nomatch_action_${nomatch_action}" => 1,
314                                       nomatch_action => $nomatch_action,
315                                       "item_action_${item_action}" => 1,
316                                       item_action => $item_action
317                                      );
318     batch_info($template, $batch);
319 }
320
321 sub batch_info {
322     my ($template, $batch) = @_;
323     $template->param(batch_info => 1,
324                                       file_name => $batch->{'file_name'},
325                                           comments => $batch->{'comments'},
326                                           import_status => $batch->{'import_status'},
327                                           upload_timestamp => $batch->{'upload_timestamp'},
328                                           num_biblios => $batch->{'num_biblios'},
329                                           num_items => $batch->{'num_biblios'});
330     if ($batch->{'num_biblios'} > 0) {
331         if ($batch->{'import_status'} eq 'staged' or $batch->{'import_status'} eq 'reverted') {
332             $template->param(can_commit => 1);
333         }
334         if ($batch->{'import_status'} eq 'imported') {
335             $template->param(can_revert => 1);
336         }
337     }
338     if (defined $batch->{'matcher_id'}) {
339         my $matcher = C4::Matcher->fetch($batch->{'matcher_id'});
340         if (defined $matcher) {
341             $template->param('current_matcher_id' => $batch->{'matcher_id'},
342                                               'current_matcher_code' => $matcher->code(),
343                                               'current_matcher_description' => $matcher->description());
344         }
345     }
346     add_matcher_list($batch->{'matcher_id'});
347 }
348
349 sub add_matcher_list {
350     my $current_matcher_id = shift;
351     my @matchers = C4::Matcher::GetMatcherList();
352     if (defined $current_matcher_id) {
353         for (my $i = 0; $i <= $#matchers; $i++) {
354             if ($matchers[$i]->{'matcher_id'} == $current_matcher_id) {
355                 $matchers[$i]->{'selected'} = 1;
356             }
357         }
358     }
359     $template->param(available_matchers => \@matchers);
360 }