POD updated.
[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 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<branch>
77 the branch where this order will be received.
78
79 =item C<series>
80
81 =item C<notes>
82 Notes on this basket.
83
84 =item C<bookfund>
85 bookfund use to pay this order.
86
87 =item C<sort1> & C<sort2>
88
89 =item C<rrp>
90
91 =item C<ecost>
92
93 =item C<GST>
94
95 =item C<budget>
96
97 =item C<cost>
98
99 =item C<sub>
100
101 =item C<invoice>
102 the number of the invoice for this order.
103
104 =item C<publishercode>
105
106 =item C<suggestionid>
107 if it is an order from an existing suggestion : the id of this suggestion.
108
109 =item C<donation>
110
111 =back
112
113 =cut
114
115 use strict;
116 use CGI;
117 use C4::Auth;
118 use C4::Output;
119 use C4::Acquisition;
120 use C4::Suggestions;
121 use C4::Biblio;
122 use C4::Output;
123 use C4::Interface::CGI::Output;
124 use C4::Database;
125 use HTML::Template;
126
127 #use Data::Dumper;
128 #use Date::Manip;
129
130 my $input = new CGI;
131
132 # get_template_and_user used only to check auth & get user id
133 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
134     {
135         template_name   => "acqui/booksellers.tmpl",
136         query           => $input,
137         type            => "intranet",
138         authnotrequired => 0,
139         flagsrequired   => { acquisition => 1 },
140         debug           => 1,
141     }
142 );
143
144 # get CGI parameters
145 my $ordnum       = $input->param('ordnum');
146 my $basketno     = $input->param('basketno');
147 my $booksellerid = $input->param('booksellerid');
148 my $existing     = $input->param('existing');    # existing biblio, (not basket or order)
149 my $title         = $input->param('title');
150 my $author        = $input->param('author');
151 my $copyrightdate = $input->param('copyrightdate');
152 my $isbn          = $input->param('ISBN');
153 my $itemtype      = $input->param('format');
154 my $quantity      = $input->param('quantity');
155 my $listprice     = $input->param('list_price');
156 my $branch        = $input->param('branch');
157 if ( $listprice eq '' ) {
158     $listprice = 0;
159 }
160 my $series = $input->param('series');
161 my $notes         = $input->param('notes');
162 my $bookfund      = $input->param('bookfund');
163 my $sort1         = $input->param('sort1');
164 my $sort2         = $input->param('sort2');
165 my $rrp           = $input->param('rrp');
166 my $ecost         = $input->param('ecost');
167 my $gst           = $input->param('GST');
168 my $budget        = $input->param('budget');
169 my $cost          = $input->param('cost');
170 my $sub           = $input->param('sub');
171 my $invoice       = $input->param('invoice');
172 my $publishercode = $input->param('publishercode');
173 my $suggestionid  = $input->param('suggestionid');
174 my $donation      = $input->param('donation');
175 my $user          = $input->remote_user;
176
177 #warn "CREATEBIBITEM =  $input->param('createbibitem')";
178 #warn Dumper $input->param('createbibitem');
179 my $createbibitem = $input->param('createbibitem');
180
181 # create, modify or delete biblio
182 # create if $quantity>=0 and $existing='no'
183 # modify if $quantity>=0 and $existing='yes'
184 # delete if $quantity has been se to 0 by the librarian
185 my $bibnum;
186 my $bibitemnum;
187 if ( $quantity ne '0' ) {
188
189     #check to see if biblio exists
190     if ( $existing eq 'no' ) {
191
192         #if it doesnt create it
193         $bibnum = &newbiblio(
194             {
195                 title         => $title         ? $title         : "",
196                 author        => $author        ? $author        : "",
197                 copyrightdate => $copyrightdate ? $copyrightdate : "",
198                 series        => $series        ? $series        : "",
199             }
200         );
201         $bibitemnum = &newbiblioitem(
202             {
203                 biblionumber  => $bibnum,
204                 itemtype      => $itemtype ? $itemtype : "",
205                 isbn          => $isbn ? $isbn : "",
206                 publishercode => $publishercode ? $publishercode : "",
207             }
208         );
209
210         # change suggestion status if applicable
211         if ($suggestionid) {
212             ModStatus( $suggestionid, 'ORDERED', '', $bibnum );
213         }
214     }
215
216     elsif ( $createbibitem eq 'YES' ) {
217         $bibnum     = $input->param('biblio');
218         $bibitemnum = $input->param('bibitemnum');
219         $bibitemnum = &newbiblioitem(
220             {
221                 biblionumber  => $bibnum,
222                 itemtype      => $itemtype ? $itemtype : "",
223                 isbn          => $isbn ? $isbn : "",
224                 publishercode => $publishercode ? $publishercode : "",
225             }
226         );
227         &modbiblio(
228             {
229                 biblionumber  => $bibnum,
230                 title         => $title ? $title : "",
231                 author        => $author ? $author : "",
232                 copyrightdate => $copyrightdate ? $copyrightdate : "",
233                 series        => $series ? $series : ""
234             }
235         );
236     }
237
238     # then attach it to an existing bib
239
240     else {
241         warn "attaching to an existing bibitem";
242
243         $bibnum = $input->param('biblio');
244
245         # if we are moddig the bibitem, not creating it createbib wont be set,
246         #
247         if ($createbibitem) {
248             $bibitemnum = $createbibitem;
249         }
250         else {
251             $bibitemnum = $input->param('bibitemnum');
252         }
253
254         my $oldtype = $input->param('oldtype');
255         &modbibitem(
256             {
257                 biblioitemnumber => $bibitemnum,
258                 isbn             => $isbn,
259                 publishercode    => $publishercode,
260                 itemtype         =>
261                   $itemtype,    # added itemtype, not prev. being changed.
262             }
263         );
264         &modbiblio(
265             {
266                 biblionumber  => $bibnum,
267                 title         => $title ? $title : "",
268                 author        => $author ? $author : "",
269                 copyrightdate => $copyrightdate ? $copyrightdate : "",
270                 series        => $series ? $series : ""
271             },
272         );
273     }
274     if ($ordnum) {
275
276         #               warn "MODORDER $title / $ordnum / $quantity / $bookfund";
277         ModOrder(
278             $title,   $ordnum,   $quantity,     $listprice,
279             $bibnum,  $basketno, $booksellerid, $loggedinuser,
280             $notes,   $bookfund, $bibitemnum,   $rrp,
281             $ecost,   $gst,      $budget,       $cost,
282             $invoice, $sort1,    $sort2
283         );
284     }
285     else {
286         ( $basketno, $ordnum ) = NewOrder(
287             $basketno,  $bibnum,       $title,        $quantity,
288             $listprice, $booksellerid, $loggedinuser, $notes,
289             $bookfund,  $bibitemnum,   $rrp,          $ecost,
290             $gst,       $budget,       $cost,         $sub,
291             $invoice,   $sort1,        $sort2
292         );
293     }
294     if ($donation) {
295         my $barcode  = $input->param('barcode');
296         my @barcodes = split( /\,| |\|/, $barcode );
297         my ($error)  = newitems(
298             {
299                 biblioitemnumber => $bibitemnum,
300                 biblionumber     => $bibnum,
301                 replacementprice => $rrp,
302                 price            => $cost,
303                 booksellerid     => $booksellerid,
304                 homebranch       => $branch,
305                 loan             => 0
306             },
307             @barcodes
308         );
309         ModReceiveOrder(
310             $bibnum,  $ordnum, $quantity, $user, $cost,
311             $invoice, 0,       $bookfund, $rrp
312         );
313     }
314 }
315 else {
316     $bibnum = $input->param('biblio');
317     DelOrder( $bibnum, $ordnum );
318 }
319 print $input->redirect("basket.pl?basket=$basketno");