Bug 8315 - remove use C4::* version
[koha.git] / acqui / addorder.pl
blob01febba05d7cfadd81e08f38c861ce47b61ac1ab
1 #!/usr/bin/perl
3 #script to add an order into the system
4 #written 29/2/00 by chris@katipo.co.nz
6 # Copyright 2000-2002 Katipo Communications
8 # This file is part of Koha.
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.
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.
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.
24 =head1 NAME
26 addorder.pl
28 =head1 DESCRIPTION
30 this script allows to add an order.
31 It is called by :
33 =over
35 =item neworderbiblio.pl to add an order from nothing.
37 =item neworderempty.pl to add an order from an existing biblio.
39 =item newordersuggestion.pl to add an order from an existing suggestion.
41 =back
43 =head1 CGI PARAMETERS
45 All of the cgi parameters below are related to the new order.
47 =over
49 =item C<ordernumber>
50 the number of this new order.
52 =item C<basketno>
53 the number of this new basket
55 =item C<booksellerid>
56 the bookseller the librarian has to pay.
58 =item C<existing>
60 =item C<title>
61 the title of the record ordered.
63 =item C<author>
64 the author of the record ordered.
66 =item C<copyrightdate>
67 the copyrightdate of the record ordered.
69 =item C<ISBN>
70 the ISBN of the record ordered.
72 =item C<format>
74 =item C<quantity>
75 the quantity to order.
77 =item C<list_price>
78 the price of this order.
80 =item C<uncertainprice>
81 uncertain price, can't close basket until prices of all orders are known.
83 =item C<branch>
84 the branch where this order will be received.
86 =item C<series>
88 =item C<notes>
89 Notes on this basket.
91 =item C<budget_id>
92 budget_id used to pay this order.
94 =item C<sort1> & C<sort2>
96 =item C<rrp>
98 =item C<ecost>
100 =item C<GST>
102 =item C<budget>
104 =item C<cost>
106 =item C<sub>
108 =item C<invoice>
109 the number of the invoice for this order.
111 =item C<publishercode>
113 =item C<suggestionid>
114 if it is an order from an existing suggestion : the id of this suggestion.
116 =item C<donation>
118 =back
120 =cut
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;
132 ### "-------------------- addorder.pl ----------"
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.
137 my $input = new CGI;
138 ### $input
140 # get_template_and_user used only to check auth & get user id
141 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
143 template_name => "acqui/booksellers.tmpl",
144 query => $input,
145 type => "intranet",
146 authnotrequired => 0,
147 flagsrequired => { acquisition => 'order_manage' },
148 debug => 1,
152 # get CGI parameters
153 my $orderinfo = $input->Vars;
154 $orderinfo->{'list_price'} ||= 0;
155 $orderinfo->{'uncertainprice'} ||= 0;
156 #my $ordernumber = $input->param('ordernumber');
157 #my $basketno = $input->param('basketno');
158 #my $booksellerid = $input->param('booksellerid');
159 #my $existing = $input->param('existing'); # existing biblio, (not basket or order)
160 #my $title = $input->param('title');
161 #my $author = $input->param('author');
162 #my $publicationyear= $input->param('publicationyear');
163 #my $isbn = $input->param('ISBN');
164 #my $itemtype = $input->param('format');
165 #my $quantity = $input->param('quantity'); # FIXME: else ERROR!
166 #my $branch = $input->param('branch');
167 #my $series = $input->param('series');
168 #my $notes = $input->param('notes');
169 #my $budget_id = $input->param('budget_id');
170 #my $sort1 = $input->param('sort1');
171 #my $sort2 = $input->param('sort2');
172 #my $rrp = $input->param('rrp');
173 #my $ecost = $input->param('ecost');
174 #my $gst = $input->param('GST');
175 #my $budget = $input->param('budget');
176 #my $cost = $input->param('cost');
177 #my $sub = $input->param('sub');
178 #my $purchaseorder = $input->param('purchaseordernumber');
179 #my $invoice = $input->param('invoice');
180 #my $publishercode = $input->param('publishercode');
181 #my $suggestionid = $input->param('suggestionid');
182 #my $biblionumber = $input->param('biblionumber');
183 #my $uncertainprice = $input->param('uncertainprice');
184 #my $import_batch_id= $input->param('import_batch_id');
186 #my $createbibitem = $input->param('createbibitem');
188 my $user = $input->remote_user;
189 # create, modify or delete biblio
190 # create if $quantity>=0 and $existing='no'
191 # modify if $quantity>=0 and $existing='yes'
192 # delete if $quantity has been set to 0 by the librarian
193 # delete biblio if delbiblio has been set to 1 by the librarian
194 my $bibitemnum;
195 if ( $orderinfo->{quantity} ne '0' ) {
196 #TODO:check to see if biblio exists
197 unless ( $$orderinfo{biblionumber} ) {
198 #if it doesnt create it
199 my $record = TransformKohaToMarc(
201 "biblio.title" => "$$orderinfo{title}",
202 "biblio.author" => $$orderinfo{author} ? $$orderinfo{author} : "",
203 "biblio.seriestitle" => $$orderinfo{series} ? $$orderinfo{series} : "",
204 "biblioitems.isbn" => $$orderinfo{isbn} ? $$orderinfo{isbn} : "",
205 "biblioitems.ean" => $$orderinfo{ean} ? $$orderinfo{ean} : "",
206 "biblioitems.publishercode" => $$orderinfo{publishercode} ? $$orderinfo{publishercode} : "",
207 "biblioitems.publicationyear" => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "",
208 "biblio.copyrightdate" => $$orderinfo{publicationyear} ? $$orderinfo{publicationyear}: "",
209 "biblioitems.itemtype" => $$orderinfo{itemtype} ? $$orderinfo{itemtype} : "",
210 "biblioitems.editionstatement"=> $$orderinfo{editionstatement} ? $$orderinfo{editionstatement} : "",
213 # create the record in catalogue, with framework ''
214 my ($biblionumber,$bibitemnum) = AddBiblio($record,'');
215 # change suggestion status if applicable
216 if ($$orderinfo{suggestionid}) {
217 ModSuggestion( {suggestionid=>$$orderinfo{suggestionid}, STATUS=>'ORDERED', biblionumber=>$biblionumber} );
219 $orderinfo->{biblioitemnumber}=$bibitemnum;
220 $orderinfo->{biblionumber}=$biblionumber;
223 # if we already have $ordernumber, then it's an ordermodif
224 if ($$orderinfo{ordernumber}) {
225 ModOrder( $orderinfo);
227 else { # else, it's a new line
228 @$orderinfo{qw(basketno ordernumber )} = NewOrder($orderinfo);
231 # now, add items if applicable
232 if (C4::Context->preference('AcqCreateItem') eq 'ordering') {
234 my @tags = $input->param('tag');
235 my @subfields = $input->param('subfield');
236 my @field_values = $input->param('field_value');
237 my @serials = $input->param('serial');
238 my @itemid = $input->param('itemid');
239 my @ind_tag = $input->param('ind_tag');
240 my @indicator = $input->param('indicator');
241 #Rebuilding ALL the data for items into a hash
242 # parting them on $itemid.
244 my %itemhash;
245 my $countdistinct;
246 my $range=scalar(@itemid);
247 for (my $i=0; $i<$range; $i++){
248 unless ($itemhash{$itemid[$i]}){
249 $countdistinct++;
251 push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
252 push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
253 push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
254 push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
255 push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
257 foreach my $item (keys %itemhash){
259 my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
260 $itemhash{$item}->{'subfields'},
261 $itemhash{$item}->{'field_values'},
262 $itemhash{$item}->{'ind_tag'},
263 $itemhash{$item}->{'indicator'},
264 'ITEM');
265 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
266 my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$$orderinfo{biblionumber});
267 NewOrderItem($itemnumber, $$orderinfo{ordernumber});
274 else { # qty=0, delete the line
275 my $biblionumber = $input->param('biblionumber');
276 DelOrder( $biblionumber, $$orderinfo{ordernumber} );
277 if ($orderinfo->{delbiblio} == 1){
278 DelBiblio($biblionumber);
281 my $basketno=$$orderinfo{basketno};
282 my $booksellerid=$$orderinfo{booksellerid};
283 if (my $import_batch_id=$$orderinfo{import_batch_id}) {
284 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=$basketno&booksellerid=$booksellerid");
285 } else {
286 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");