[biblibre-newacq] fix some merge errors
[wip/koha-chris_n.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 with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 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 =item neworderbiblio.pl to add an order from nothing.
34
35 =item neworderempty.pl to add an order from an existing biblio.
36
37 =item newordersuggestion.pl to add an order from an existing suggestion.
38
39 =head1 CGI PARAMETERS
40
41 All of the cgi parameters below are related to the new order.
42
43 =over 4
44
45 =item C<ordnum>
46 the number of this new order.
47
48 =item C<basketno>
49 the number of this new basket
50
51 =item C<booksellerid>
52 the bookseller the librarian has to pay.
53
54 =item C<existing>
55
56 =item C<title>
57 the title of the record ordered.
58
59 =item C<author>
60 the author of the record ordered.
61
62 =item C<copyrightdate>
63 the copyrightdate of the record ordered.
64
65 =item C<ISBN>
66 the ISBN of the record ordered.
67
68 =item C<format>
69
70 =item C<quantity>
71 the quantity to order.
72
73 =item C<list_price>
74 the price of this order.
75
76 =item C<uncertainprice>
77 uncertain price, can't close basket until prices of all orders are known.
78
79 =item C<branch>
80 the branch where this order will be received.
81
82 =item C<series>
83
84 =item C<notes>
85 Notes on this basket.
86
87 =item C<budget_id>
88 budget_id used to pay this order.
89
90 =item C<sort1> & C<sort2>
91
92 =item C<rrp>
93
94 =item C<ecost>
95
96 =item C<GST>
97
98 =item C<budget>
99
100 =item C<cost>
101
102 =item C<sub>
103
104 =item C<invoice>
105 the number of the invoice for this order.
106
107 =item C<publishercode>
108
109 =item C<suggestionid>
110 if it is an order from an existing suggestion : the id of this suggestion.
111
112 =item C<donation>
113
114 =back
115
116 =cut
117
118 use strict;
119 use warnings;
120 use CGI;
121 use C4::Auth;                   # get_template_and_user
122 use C4::Acquisition;    # NewOrder DelOrder ModOrder
123 use C4::Suggestions;    # ModStatus
124 use C4::Biblio;                 # AddBiblio TransformKohaToMarc
125 use C4::Items;
126 use C4::Output;
127
128 ### "-------------------- addorder.pl ----------"
129
130 # FIXME: This needs to do actual error checking and possibly return user to the same form,
131 # not just blindly call C4 functions and print a redirect.  
132
133 my $input = new CGI;
134 ### $input 
135
136 # get_template_and_user used only to check auth & get user id
137 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
138     {
139         template_name   => "acqui/booksellers.tmpl",
140         query           => $input,
141         type            => "intranet",
142         authnotrequired => 0,
143        flagsrequired   => { acquisition => 'order_manage' },
144         debug           => 1,
145     }
146 );
147
148 # get CGI parameters
149 my $ordnum        = $input->param('ordnum');
150 my $basketno      = $input->param('basketno');
151 my $booksellerid  = $input->param('booksellerid');
152 my $existing      = $input->param('existing');    # existing biblio, (not basket or order)
153 my $title         = $input->param('title');
154 my $author        = $input->param('author');
155 my $copyrightdate = $input->param('copyrightdate');
156 my $isbn          = $input->param('ISBN');
157 my $itemtype      = $input->param('format');
158 my $quantity      = $input->param('quantity');          # FIXME: else ERROR!
159 my $listprice     = $input->param('list_price') || 0;
160 my $branch        = $input->param('branch');
161 my $series        = $input->param('series');
162 my $notes         = $input->param('notes');
163 my $budget_id     = $input->param('budget_id');
164 my $sort1         = $input->param('sort1');
165 my $sort2         = $input->param('sort2');
166 my $rrp           = $input->param('rrp');
167 my $ecost         = $input->param('ecost');
168 my $gst           = $input->param('GST');
169 my $budget        = $input->param('budget');
170 my $cost          = $input->param('cost');
171 my $sub           = $input->param('sub');
172 my $purchaseorder = $input->param('purchaseordernumber');
173 my $invoice       = $input->param('invoice');
174 my $publishercode = $input->param('publishercode');
175 my $suggestionid  = $input->param('suggestionid');
176 my $biblionumber  = $input->param('biblionumber');
177 my $user          = $input->remote_user;
178 my $uncertainprice = $input->param('uncertainprice');
179
180 #warn "CREATEBIBITEM =  $input->param('createbibitem')";
181 #warn Dumper $input->param('createbibitem');
182 my $createbibitem = $input->param('createbibitem');
183
184 # create, modify or delete biblio
185 # create if $quantity>=0 and $existing='no'
186 # modify if $quantity>=0 and $existing='yes'
187 # delete if $quantity has been set to 0 by the librarian
188 my $bibitemnum;
189 if ( $quantity ne '0' ) {
190     #TODO:check to see if biblio exists
191     unless ( $biblionumber ) {
192
193         #if it doesnt create it
194         my $record = TransformKohaToMarc(
195             {
196                 "biblio.title"              => "$title",
197                 "biblio.author"             => "$author",
198                 "biblio.copyrightdate"      => $copyrightdate ? $copyrightdate : "",
199                 "biblio.series"             => $series        ? $series        : "",
200                 "biblioitems.itemtype"      => $itemtype      ? $itemtype      : "",
201                 "biblioitems.isbn"          => $isbn          ? $isbn          : "",
202                 "biblioitems.publishercode" => $publishercode ? $publishercode : "",
203             });
204         # create the record in catalogue, with framework ''
205         ($biblionumber,$bibitemnum) = AddBiblio($record,'');
206         # change suggestion status if applicable
207         if ($suggestionid) {
208             ModStatus( $suggestionid, 'ORDERED', '', $biblionumber );
209         }
210     }
211
212     # if we already have $ordnum, then it's an ordermodif
213     if ($ordnum) {
214         my %orderinfo = ("biblionumber", $biblionumber,
215                                     "ordernumber", $ordnum,
216                                     "basketno", $basketno,
217                                     "quantity", $quantity,
218                                     "listprice", $listprice,
219                                     "notes", $notes,
220                                     "biblioitemnumber", $bibitemnum,
221                                     "rrp", $rrp,
222                                     "ecost", $ecost,
223                                     "gst", $gst,
224                                     "unitprice", $cost,
225                                     "subscription", $sub,
226                                     "sort1", $sort1,
227                                     "sort2", $sort2,
228 #                                    "budgetdate", $budget,
229                                     "purchaseordernumber", $purchaseorder,
230                                     "branchcode", $branch,
231                                     "booksellerinvoicenumber", $invoice,
232                                     "budget_id", $budget_id,
233                                     "uncertainprice", $uncertainprice);
234         ModOrder( \%orderinfo);
235     }
236     else { # else, it's a new line
237         my %orderinfo = ("biblionumber", $biblionumber,
238                                     "ordernumber", $ordnum,
239                                     "basketno", $basketno,
240                                     "quantity", $quantity,
241                                     "listprice", $listprice,
242                                     "notes", $notes,
243                                     "biblioitemnumber", $bibitemnum,
244                                     "rrp", $rrp,
245                                     "ecost", $ecost,
246                                     "gst", $gst,
247                                     "unitprice", $cost,
248                                     "subscription", $sub,
249                                     "sort1", $sort1,
250                                     "sort2", $sort2,
251 #                                    "budgetdate", $budget,
252                                     "purchaseordernumber", $purchaseorder,
253                                     "branchcode", $branch,
254                                     "booksellerinvoicenumber", $invoice,
255                                     "budget_id", $budget_id,
256                                     "uncertainprice", $uncertainprice);
257         ( $basketno, $ordnum ) = NewOrder(\%orderinfo);
258     }
259
260     # now, add items if applicable
261     if (C4::Context->preference('AcqCreateItem') eq 'ordering') {
262
263         my @tags         = $input->param('tag');
264         my @subfields    = $input->param('subfield');
265         my @field_values = $input->param('field_value');
266         my @serials      = $input->param('serial');
267         my @itemid       = $input->param('itemid');
268         my @ind_tag      = $input->param('ind_tag');
269         my @indicator    = $input->param('indicator');
270         #Rebuilding ALL the data for items into a hash
271         # parting them on $itemid.
272
273         my %itemhash;
274         my $countdistinct;
275         my $range=scalar(@itemid);
276         for (my $i=0; $i<$range; $i++){
277             unless ($itemhash{$itemid[$i]}){
278             $countdistinct++;
279             }
280             push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
281             push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
282             push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
283             push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
284             push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
285         }
286         foreach my $item (keys %itemhash){
287
288             my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
289                                     $itemhash{$item}->{'subfields'},
290                                     $itemhash{$item}->{'field_values'},
291                                     $itemhash{$item}->{'ind_tag'},
292                                     $itemhash{$item}->{'indicator'},
293                                     'ITEM');
294             my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
295             my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
296             NewOrderItem($itemnumber, $ordnum);
297
298         }
299     }
300
301 }
302
303 else { # qty=0, delete the line
304     $biblionumber = $input->param('biblionumber');
305     DelOrder( $biblionumber, $ordnum );
306 }
307 print $input->redirect("basket.pl?basketno=$basketno");