Code cleaned. POD added. Subs renamed/rewrited/added....
[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     =
57   $input->param('existing');    # existing biblio, (not basket or order)
58 my $title         = $input->param('title');
59 my $author        = $input->param('author');
60 my $copyrightdate = $input->param('copyrightdate');
61 my $isbn          = $input->param('ISBN');
62 my $itemtype      = $input->param('format');
63 my $quantity      = $input->param('quantity');
64 my $listprice     = $input->param('list_price');
65 my $branch        = $input->param('branch');
66 if ( $listprice eq '' ) {
67     $listprice = 0;
68 }
69 my $series = $input->param('series');
70
71 # my $supplier=$input->param('supplier');
72 my $notes         = $input->param('notes');
73 my $bookfund      = $input->param('bookfund');
74 my $sort1         = $input->param('sort1');
75 my $sort2         = $input->param('sort2');
76 my $rrp           = $input->param('rrp');
77 my $ecost         = $input->param('ecost');
78 my $gst           = $input->param('GST');
79 my $budget        = $input->param('budget');
80 my $cost          = $input->param('cost');
81 my $sub           = $input->param('sub');
82 my $invoice       = $input->param('invoice');
83 my $publishercode = $input->param('publishercode');
84 my $suggestionid  = $input->param('suggestionid');
85 my $donation      = $input->param('donation');
86 my $user          = $input->remote_user;
87
88 #warn "CREATEBIBITEM =  $input->param('createbibitem')";
89 #warn Dumper $input->param('createbibitem');
90 my $createbibitem = $input->param('createbibitem');
91
92 # create, modify or delete biblio
93 # create if $quantity>=0 and $existing='no'
94 # modify if $quantity>=0 and $existing='yes'
95 # delete if $quantity has been se to 0 by the librarian
96 my $bibnum;
97 my $bibitemnum;
98 if ( $quantity ne '0' ) {
99
100     #check to see if biblio exists
101     if ( $existing eq 'no' ) {
102
103         #if it doesnt create it
104         $bibnum = &newbiblio(
105             {
106                 title         => $title         ? $title         : "",
107                 author        => $author        ? $author        : "",
108                 copyrightdate => $copyrightdate ? $copyrightdate : "",
109                 series        => $series        ? $series        : "",
110             }
111         );
112         $bibitemnum = &newbiblioitem(
113             {
114                 biblionumber  => $bibnum,
115                 itemtype      => $itemtype ? $itemtype : "",
116                 isbn          => $isbn ? $isbn : "",
117                 publishercode => $publishercode ? $publishercode : "",
118             }
119         );
120
121         # change suggestion status if applicable
122         if ($suggestionid) {
123             ModStatus( $suggestionid, 'ORDERED', '', $bibnum );
124         }
125     }
126
127     elsif ( $createbibitem eq 'YES' ) {
128         $bibnum     = $input->param('biblio');
129         $bibitemnum = $input->param('bibitemnum');
130         $bibitemnum = &newbiblioitem(
131             {
132                 biblionumber  => $bibnum,
133                 itemtype      => $itemtype ? $itemtype : "",
134                 isbn          => $isbn ? $isbn : "",
135                 publishercode => $publishercode ? $publishercode : "",
136             }
137         );
138         &modbiblio(
139             {
140                 biblionumber  => $bibnum,
141                 title         => $title ? $title : "",
142                 author        => $author ? $author : "",
143                 copyrightdate => $copyrightdate ? $copyrightdate : "",
144                 series        => $series ? $series : ""
145             }
146         );
147     }
148
149     # then attach it to an existing bib
150
151     else {
152         warn "attaching to an existing bibitem";
153
154         $bibnum = $input->param('biblio');
155
156         # if we are moddig the bibitem, not creating it createbib wont be set,
157         #
158         if ($createbibitem) {
159             $bibitemnum = $createbibitem;
160         }
161         else {
162             $bibitemnum = $input->param('bibitemnum');
163         }
164
165         my $oldtype = $input->param('oldtype');
166         &modbibitem(
167             {
168                 biblioitemnumber => $bibitemnum,
169                 isbn             => $isbn,
170                 publishercode    => $publishercode,
171                 itemtype         =>
172                   $itemtype,    # added itemtype, not prev. being changed.
173             }
174         );
175         &modbiblio(
176             {
177                 biblionumber  => $bibnum,
178                 title         => $title ? $title : "",
179                 author        => $author ? $author : "",
180                 copyrightdate => $copyrightdate ? $copyrightdate : "",
181                 series        => $series ? $series : ""
182             },
183         );
184     }
185     if ($ordnum) {
186
187         #               warn "MODORDER $title / $ordnum / $quantity / $bookfund";
188         modorder(
189             $title,   $ordnum,   $quantity,     $listprice,
190             $bibnum,  $basketno, $booksellerid, $loggedinuser,
191             $notes,   $bookfund, $bibitemnum,   $rrp,
192             $ecost,   $gst,      $budget,       $cost,
193             $invoice, $sort1,    $sort2
194         );
195     }
196     else {
197         ( $basketno, $ordnum ) = neworder(
198             $basketno,  $bibnum,       $title,        $quantity,
199             $listprice, $booksellerid, $loggedinuser, $notes,
200             $bookfund,  $bibitemnum,   $rrp,          $ecost,
201             $gst,       $budget,       $cost,         $sub,
202             $invoice,   $sort1,        $sort2
203         );
204     }
205     if ($donation) {
206         my $barcode  = $input->param('barcode');
207         my @barcodes = split( /\,| |\|/, $barcode );
208         my ($error)  = newitems(
209             {
210                 biblioitemnumber => $bibitemnum,
211                 biblionumber     => $bibnum,
212                 replacementprice => $rrp,
213                 price            => $cost,
214                 booksellerid     => $booksellerid,
215                 homebranch       => $branch,
216                 loan             => 0
217             },
218             @barcodes
219         );
220         receiveorder(
221             $bibnum,  $ordnum, $quantity, $user, $cost,
222             $invoice, 0,       $bookfund, $rrp
223         );
224     }
225 }
226 else {
227     $bibnum = $input->param('biblio');
228     delorder( $bibnum, $ordnum );
229 }
230 print $input->redirect("basket.pl?basket=$basketno");