Subs renamed
[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 use strict;
24 use CGI;
25 use C4::Auth;
26 use C4::Output;
27 use C4::Acquisition;
28 use C4::Suggestions;
29 use C4::Biblio;
30 use C4::Output;
31 use C4::Interface::CGI::Output;
32 use C4::Database;
33 use HTML::Template;
34
35 #use Data::Dumper;
36 #use Date::Manip;
37
38 my $input = new CGI;
39
40 # get_template_and_user used only to check auth & get user id
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {
43         template_name   => "acqui/order.tmpl",
44         query           => $input,
45         type            => "intranet",
46         authnotrequired => 0,
47         flagsrequired   => { acquisition => 1 },
48         debug           => 1,
49     }
50 );
51
52 # get CGI parameters
53 my $ordnum       = $input->param('ordnum');
54 my $basketno     = $input->param('basketno');
55 my $booksellerid = $input->param('booksellerid');
56 my $existing     = $input->param('existing');    # existing biblio, (not basket or order)
57 my $title         = $input->param('title');
58 my $author        = $input->param('author');
59 my $copyrightdate = $input->param('copyrightdate');
60 my $isbn          = $input->param('ISBN');
61 my $itemtype      = $input->param('format');
62 my $quantity      = $input->param('quantity');
63 my $listprice     = $input->param('list_price');
64 my $branch        = $input->param('branch');
65 if ( $listprice eq '' ) {
66     $listprice = 0;
67 }
68 my $series = $input->param('series');
69 my $notes         = $input->param('notes');
70 my $bookfund      = $input->param('bookfund');
71 my $sort1         = $input->param('sort1');
72 my $sort2         = $input->param('sort2');
73 my $rrp           = $input->param('rrp');
74 my $ecost         = $input->param('ecost');
75 my $gst           = $input->param('GST');
76 my $budget        = $input->param('budget');
77 my $cost          = $input->param('cost');
78 my $sub           = $input->param('sub');
79 my $invoice       = $input->param('invoice');
80 my $publishercode = $input->param('publishercode');
81 my $suggestionid  = $input->param('suggestionid');
82 my $donation      = $input->param('donation');
83 my $user          = $input->remote_user;
84
85 #warn "CREATEBIBITEM =  $input->param('createbibitem')";
86 #warn Dumper $input->param('createbibitem');
87 my $createbibitem = $input->param('createbibitem');
88
89 # create, modify or delete biblio
90 # create if $quantity>=0 and $existing='no'
91 # modify if $quantity>=0 and $existing='yes'
92 # delete if $quantity has been se to 0 by the librarian
93 my $bibnum;
94 my $bibitemnum;
95 if ( $quantity ne '0' ) {
96
97     #check to see if biblio exists
98     if ( $existing eq 'no' ) {
99
100         #if it doesnt create it
101         $bibnum = &newbiblio(
102             {
103                 title         => $title         ? $title         : "",
104                 author        => $author        ? $author        : "",
105                 copyrightdate => $copyrightdate ? $copyrightdate : "",
106                 series        => $series        ? $series        : "",
107             }
108         );
109         $bibitemnum = &newbiblioitem(
110             {
111                 biblionumber  => $bibnum,
112                 itemtype      => $itemtype ? $itemtype : "",
113                 isbn          => $isbn ? $isbn : "",
114                 publishercode => $publishercode ? $publishercode : "",
115             }
116         );
117
118         # change suggestion status if applicable
119         if ($suggestionid) {
120             ModStatus( $suggestionid, 'ORDERED', '', $bibnum );
121         }
122     }
123
124     elsif ( $createbibitem eq 'YES' ) {
125         $bibnum     = $input->param('biblio');
126         $bibitemnum = $input->param('bibitemnum');
127         $bibitemnum = &newbiblioitem(
128             {
129                 biblionumber  => $bibnum,
130                 itemtype      => $itemtype ? $itemtype : "",
131                 isbn          => $isbn ? $isbn : "",
132                 publishercode => $publishercode ? $publishercode : "",
133             }
134         );
135         &modbiblio(
136             {
137                 biblionumber  => $bibnum,
138                 title         => $title ? $title : "",
139                 author        => $author ? $author : "",
140                 copyrightdate => $copyrightdate ? $copyrightdate : "",
141                 series        => $series ? $series : ""
142             }
143         );
144     }
145
146     # then attach it to an existing bib
147
148     else {
149         warn "attaching to an existing bibitem";
150
151         $bibnum = $input->param('biblio');
152
153         # if we are moddig the bibitem, not creating it createbib wont be set,
154         #
155         if ($createbibitem) {
156             $bibitemnum = $createbibitem;
157         }
158         else {
159             $bibitemnum = $input->param('bibitemnum');
160         }
161
162         my $oldtype = $input->param('oldtype');
163         &modbibitem(
164             {
165                 biblioitemnumber => $bibitemnum,
166                 isbn             => $isbn,
167                 publishercode    => $publishercode,
168                 itemtype         =>
169                   $itemtype,    # added itemtype, not prev. being changed.
170             }
171         );
172         &modbiblio(
173             {
174                 biblionumber  => $bibnum,
175                 title         => $title ? $title : "",
176                 author        => $author ? $author : "",
177                 copyrightdate => $copyrightdate ? $copyrightdate : "",
178                 series        => $series ? $series : ""
179             },
180         );
181     }
182     if ($ordnum) {
183
184         #               warn "MODORDER $title / $ordnum / $quantity / $bookfund";
185         ModOrder(
186             $title,   $ordnum,   $quantity,     $listprice,
187             $bibnum,  $basketno, $booksellerid, $loggedinuser,
188             $notes,   $bookfund, $bibitemnum,   $rrp,
189             $ecost,   $gst,      $budget,       $cost,
190             $invoice, $sort1,    $sort2
191         );
192     }
193     else {
194         ( $basketno, $ordnum ) = NewOrder(
195             $basketno,  $bibnum,       $title,        $quantity,
196             $listprice, $booksellerid, $loggedinuser, $notes,
197             $bookfund,  $bibitemnum,   $rrp,          $ecost,
198             $gst,       $budget,       $cost,         $sub,
199             $invoice,   $sort1,        $sort2
200         );
201     }
202     if ($donation) {
203         my $barcode  = $input->param('barcode');
204         my @barcodes = split( /\,| |\|/, $barcode );
205         my ($error)  = newitems(
206             {
207                 biblioitemnumber => $bibitemnum,
208                 biblionumber     => $bibnum,
209                 replacementprice => $rrp,
210                 price            => $cost,
211                 booksellerid     => $booksellerid,
212                 homebranch       => $branch,
213                 loan             => 0
214             },
215             @barcodes
216         );
217         ModReceiveOrder(
218             $bibnum,  $ordnum, $quantity, $user, $cost,
219             $invoice, 0,       $bookfund, $rrp
220         );
221     }
222 }
223 else {
224     $bibnum = $input->param('biblio');
225     DelOrder( $bibnum, $ordnum );
226 }
227 print $input->redirect("basket.pl?basket=$basketno");