Merge branch 'bug_7368' into 3.14-master
[koha.git] / acqui / addorder.pl
1 #!/usr/bin/perl
2
3 #script to add an order into the system
4 #written 29/2/00 by chris@katipo.co.nz
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23
24 =head1 NAME
25
26 addorder.pl
27
28 =head1 DESCRIPTION
29
30 this script allows to add an order.
31 It is called by :
32
33 =over
34
35 =item neworderbiblio.pl to add an order from nothing.
36
37 =item neworderempty.pl to add an order from an existing biblio.
38
39 =item newordersuggestion.pl to add an order from an existing suggestion.
40
41 =back
42
43 =head1 CGI PARAMETERS
44
45 All of the cgi parameters below are related to the new order.
46
47 =over
48
49 =item C<ordernumber>
50 the number of this new order.
51
52 =item C<basketno>
53 the number of this new basket
54
55 =item C<booksellerid>
56 the bookseller the librarian has to pay.
57
58 =item C<existing>
59
60 =item C<title>
61 the title of the record ordered.
62
63 =item C<author>
64 the author of the record ordered.
65
66 =item C<copyrightdate>
67 the copyrightdate of the record ordered.
68
69 =item C<ISBN>
70 the ISBN of the record ordered.
71
72 =item C<format>
73
74 =item C<quantity>
75 the quantity to order.
76
77 =item C<list_price>
78 the price of this order.
79
80 =item C<uncertainprice>
81 uncertain price, can't close basket until prices of all orders are known.
82
83 =item C<branch>
84 the branch where this order will be received.
85
86 =item C<series>
87
88 =item C<notes>
89 Notes on this basket.
90
91 =item C<budget_id>
92 budget_id used to pay this order.
93
94 =item C<sort1> & C<sort2>
95
96 =item C<rrp>
97
98 =item C<ecost>
99
100 =item C<GST>
101
102 =item C<budget>
103
104 =item C<cost>
105
106 =item C<sub>
107
108 =item C<invoice>
109 the number of the invoice for this order.
110
111 =item C<publishercode>
112
113 =item C<suggestionid>
114 if it is an order from an existing suggestion : the id of this suggestion.
115
116 =item C<donation>
117
118 =back
119
120 =cut
121
122 use strict;
123 use warnings;
124 use CGI;
125 use C4::Auth;                   # get_template_and_user
126 use C4::Acquisition;    # NewOrder DelOrder ModOrder
127 use C4::Suggestions;    # ModStatus
128 use C4::Biblio;                 # AddBiblio TransformKohaToMarc
129 use C4::Items;
130 use C4::Output;
131
132 ### "-------------------- addorder.pl ----------"
133
134 # FIXME: This needs to do actual error checking and possibly return user to the same form,
135 # not just blindly call C4 functions and print a redirect.  
136
137 my $input = new CGI;
138 ### $input 
139
140 # get_template_and_user used only to check auth & get user id
141 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
142     {
143         template_name   => "acqui/booksellers.tmpl",
144         query           => $input,
145         type            => "intranet",
146         authnotrequired => 0,
147         flagsrequired   => { acquisition => 'order_manage' },
148         debug           => 1,
149     }
150 );
151
152 # get CGI parameters
153 my $orderinfo                                   = $input->Vars;
154 $orderinfo->{'list_price'}    ||=  0;
155 $orderinfo->{'uncertainprice'} ||= 0;
156 $orderinfo->{subscriptionid} ||= undef;
157
158 my $user = $input->remote_user;
159
160 # create, modify or delete biblio
161 # create if $quantity>=0 and $existing='no'
162 # modify if $quantity>=0 and $existing='yes'
163 # delete if $quantity has been set to 0 by the librarian
164 # delete biblio if delbiblio has been set to 1 by the librarian
165 my $bibitemnum;
166 if ( $orderinfo->{quantity} ne '0' ) {
167     #TODO:check to see if biblio exists
168     unless ( $$orderinfo{biblionumber} ) {
169         #if it doesnt create it
170         my $record = TransformKohaToMarc(
171             {
172                 "biblio.title"                => "$$orderinfo{title}",
173                 "biblio.author"               => $$orderinfo{author}          ? $$orderinfo{author}        : "",
174                 "biblio.seriestitle"          => $$orderinfo{series}          ? $$orderinfo{series}        : "",
175                 "biblioitems.isbn"            => $$orderinfo{isbn}            ? $$orderinfo{isbn}          : "",
176                 "biblioitems.ean"             => $$orderinfo{ean}             ? $$orderinfo{ean}           : "",
177                 "biblioitems.publishercode"   => $$orderinfo{publishercode}   ? $$orderinfo{publishercode} : "",
178                 "biblioitems.publicationyear" => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "",
179                 "biblio.copyrightdate"        => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "",
180                 "biblioitems.itemtype"        => $$orderinfo{itemtype} ? $$orderinfo{itemtype} : "",
181                 "biblioitems.editionstatement"=> $$orderinfo{editionstatement} ? $$orderinfo{editionstatement} : "",
182                 "aqorders.branchcode"         => $$orderinfo{branchcode} ? $$orderinfo{branchcode} : "",
183                 "aqorders.quantity"           => $$orderinfo{quantity} ? $$orderinfo{quantity} : "",
184                 "aqorders.listprice"          => $$orderinfo{listprice} ? $$orderinfo{listprice} : "",
185                 "aqorders.uncertainprice"     => $$orderinfo{uncertainprice} ? $$orderinfo{uncertainprice} : "",
186                 "aqorders.rrp"                => $$orderinfo{rrp} ? $$orderinfo{rrp} : "",
187                 "aqorders.ecost"              => $$orderinfo{ecost} ? $$orderinfo{ecost} : "",
188                 "aqorders.discount"           => $$orderinfo{discount} ? $$orderinfo{discount} : "",
189             });
190
191         # create the record in catalogue, with framework ''
192         my ($biblionumber,$bibitemnum) = AddBiblio($record,'');
193         # change suggestion status if applicable
194         if ($$orderinfo{suggestionid}) {
195             ModSuggestion( {suggestionid=>$$orderinfo{suggestionid}, STATUS=>'ORDERED', biblionumber=>$biblionumber} );
196         }
197                 $orderinfo->{biblioitemnumber}=$bibitemnum;
198                 $orderinfo->{biblionumber}=$biblionumber;
199     }
200
201     $orderinfo->{unitprice} = $orderinfo->{ecost} if not defined $orderinfo->{unitprice} or $orderinfo->{unitprice} eq '';
202
203     # if we already have $ordernumber, then it's an ordermodif
204     if ($$orderinfo{ordernumber}) {
205         ModOrder( $orderinfo);
206     }
207     else { # else, it's a new line
208         @$orderinfo{qw(basketno ordernumber )} = NewOrder($orderinfo);
209     }
210
211     # now, add items if applicable
212     if (C4::Context->preference('AcqCreateItem') eq 'ordering') {
213
214         my @tags         = $input->param('tag');
215         my @subfields    = $input->param('subfield');
216         my @field_values = $input->param('field_value');
217         my @serials      = $input->param('serial');
218         my @itemid       = $input->param('itemid');
219         my @ind_tag      = $input->param('ind_tag');
220         my @indicator    = $input->param('indicator');
221         #Rebuilding ALL the data for items into a hash
222         # parting them on $itemid.
223
224         my %itemhash;
225         my $countdistinct;
226         my $range=scalar(@itemid);
227         for (my $i=0; $i<$range; $i++){
228             unless ($itemhash{$itemid[$i]}){
229             $countdistinct++;
230             }
231             push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
232             push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
233             push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
234             push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
235             push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
236         }
237         foreach my $item (keys %itemhash){
238
239             my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
240                                     $itemhash{$item}->{'subfields'},
241                                     $itemhash{$item}->{'field_values'},
242                                     $itemhash{$item}->{'ind_tag'},
243                                     $itemhash{$item}->{'indicator'},
244                                     'ITEM');
245             my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
246             my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$$orderinfo{biblionumber});
247             NewOrderItem($itemnumber, $$orderinfo{ordernumber});
248
249         }
250     }
251
252 }
253
254 else { # qty=0, delete the line
255     my $biblionumber = $input->param('biblionumber');
256     DelOrder( $biblionumber, $$orderinfo{ordernumber} );
257     if ($orderinfo->{delbiblio} == 1){
258      DelBiblio($biblionumber);
259     }
260 }
261 my $basketno=$$orderinfo{basketno};
262 my $booksellerid=$$orderinfo{booksellerid};
263 if (my $import_batch_id=$$orderinfo{import_batch_id}) {
264     print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=$basketno&booksellerid=$booksellerid");
265 } elsif ( defined $orderinfo->{invoiceid} ) {
266     print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=" . $orderinfo->{invoiceid});
267 } else {
268     print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");
269 }