big commit, still breaking things...
[koha.git] / acqui / newbiblio.pl
1 #!/usr/bin/perl
2
3 #script to show display basket of orders
4 #written by chris@katipo.co.nz 24/2/2000
5
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along with
21 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
22 # Suite 330, Boston, MA  02111-1307 USA
23
24 use strict;
25 use CGI;
26 use C4::Context;
27 use C4::Input;
28 use C4::Database;
29 use C4::Auth;
30 use C4::Acquisition;
31 use C4::Suggestions;
32 use C4::Search;
33 use C4::Output;
34 use C4::Input;
35 use C4::Interface::CGI::Output;
36 use HTML::Template;
37
38 my $input=new CGI;
39 my $booksellerid=$input->param('booksellerid');
40 my $title=$input->param('title');
41 my $author=$input->param('author');
42 my $copyright=$input->param('copyright');
43 my ($count,@booksellers)=bookseller($booksellerid);
44 my $ordnum=$input->param('ordnum');
45 my $biblio=$input->param('biblio');
46 my $basketno=$input->param('basketno');
47 my $suggestionid = $input->param('suggestionid');
48 my $data;
49 my $new;
50 my $dbh = C4::Context->dbh;
51 if ($ordnum eq ''){ # create order
52         $new='yes';
53 #       $ordnum=newordernum;
54         if ($biblio && !$suggestionid) {
55                         $data=bibdata($biblio);
56         }
57         # get suggestion fields if applicable. If it's a subscription renewal, then the biblio already exists
58         # otherwise, retrieve suggestion information.
59         if ($suggestionid) {
60                 if ($biblio) {
61                         $data=bibdata($biblio);
62                 } else {
63                         $data = getsuggestion($suggestionid);
64                 }
65         }
66         if ($data->{'title'} eq ''){
67                 $data->{'title'}=$title;
68                 $data->{'author'}=$author;
69                 $data->{'copyrightdate'}=$copyright;
70         }
71 }else { #modify order
72         $data=getsingleorder($ordnum);
73         $biblio=$data->{'biblionumber'};
74 }
75 my ($template, $loggedinuser, $cookie)
76     = get_template_and_user({template_name => "acqui/newbiblio.tmpl",
77                              query => $input,
78                              type => "intranet",
79                              authnotrequired => 0,
80                              flagsrequired => {acquisition => 1},
81                              debug => 1,
82                              });
83
84
85 # get currencies (for change rates calcs if needed
86 my ($count,$rates)=getcurrencies();
87 my @loop_currency = ();
88 for (my $i=0;$i<$count;$i++){
89         my %line;
90         $line{currency} = $rates->[$i]->{'currency'};
91         $line{rate} = $rates->[$i]->{'rate'};
92         push @loop_currency, \%line;
93 }
94
95 # build itemtype list
96 my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
97 $sth->execute;
98 my  @itemtype;
99 my %itemtypes;
100 while (my ($value,$lib) = $sth->fetchrow_array) {
101         push @itemtype, $value;
102         $itemtypes{$value}=$lib;
103 }
104 my $CGIitemtype=CGI::scrolling_list( -name     => 'format',
105                         -values   => \@itemtype,
106                         -default  => $data->{'itemtype'},
107                         -labels   => \%itemtypes,
108                         -size     => 1,
109                         -multiple => 0 );
110 $sth->finish;
111
112 # build branches list
113 my @branches;
114 my @select_branch;
115 my %select_branches;
116 my ($count2,@branches)=branches();
117 for (my $i=0;$i<$count2;$i++){
118         if ((!C4::Context->preference('IndependantBranches'))||(C4::Context->preference('IndependantBranches') && (C4::Context->userenv->{flags} ==1))
119         ||((C4::Context->preference('IndependantBranches') && (C4::Context->userenv->{flags} !=1) && (C4::Context->userenv->{branch} eq $branches[$i]->{'branchcode'})))){
120                 push @select_branch, $branches[$i]->{'branchcode'};#
121                 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
122         }
123 }
124 my $CGIbranch=CGI::scrolling_list( -name     => 'branch',
125                         -values   => \@select_branch,
126                         -default  => $data->{'branchcode'},
127                         -labels   => \%select_branches,
128                         -size     => 1,
129                         -multiple => 0 );
130
131 # build bookfund list
132 my $sthtemp = $dbh->prepare("Select flags, branchcode from borrowers where borrowernumber = ?");
133 $sthtemp->execute($loggedinuser);
134 my ($flags, $homebranch)=$sthtemp->fetchrow;
135
136 my @bookfund;
137 my @select_bookfund;
138 my %select_bookfunds;
139 ($count2,@bookfund)=bookfunds($homebranch);
140 for (my $i=0;$i<$count2;$i++){
141         push @select_bookfund, $bookfund[$i]->{'bookfundid'};
142         $select_bookfunds{$bookfund[$i]->{'bookfundid'}} = $bookfund[$i]->{'bookfundname'}
143 }
144 my $CGIbookfund=CGI::scrolling_list( -name     => 'bookfund',
145                         -values   => \@select_bookfund,
146                         -default  => $data->{'bookfundid'},
147                         -labels   => \%select_bookfunds,
148                         -size     => 1,
149                         -multiple => 0 );
150
151 #Build sort lists
152 my $CGIsort1 = buildCGIsort("Asort1","sort1",$data->{'sort1'});
153 if ($CGIsort1) {
154         $template->param(CGIsort1 => $CGIsort1);
155 } else {
156         $template->param( sort1 => $data->{'sort1'});
157 }
158
159 my $CGIsort2 = buildCGIsort("Asort2","sort2",$data->{'sort2'});
160 if ($CGIsort2) {
161         $template->param(CGIsort2 =>$CGIsort2);
162 } else {
163         $template->param( sort2 => $data->{'sort2'});
164 }
165
166 # fill template
167 $template->param( existing => $biblio,
168                                                 title => $title,
169                                                 ordnum => $ordnum,
170                                                 basketno => $basketno,
171                                                 booksellerid => $booksellerid,
172                                                 suggestionid => $suggestionid,
173                                                 biblio => $biblio,
174                                                 biblioitemnumber => $data->{'biblioitemnumber'},
175                                                 itemtype => $data->{'itemtype'},
176                                                 discount => $booksellers[0]->{'discount'},
177                                                 listincgst => $booksellers[0]->{'listincgst'},
178                                                 listprice => $booksellers[0]->{'listprice'},
179                                                 gstreg => $booksellers[0]->{'gstreg'},
180                                                 invoiceinc => $booksellers[0]->{'invoiceincgst'},                              
181                                                 invoicedisc => $booksellers[0]->{'invoicedisc'},                               
182                                                 nocalc => $booksellers[0]->{'nocalc'},
183                                                 name => $booksellers[0]->{'name'},
184                                                 currency => $booksellers[0]->{'listprice'},
185                                                 gstrate => C4::Context->preference("gist") ,
186                                                 loop_currencies => \@loop_currency,
187                                                 orderexists => ($new eq 'yes')?0:1,
188                                                 title => $data->{'title'},
189                                                 author => $data->{'author'},
190                                                 copyrightdate => $data->{'copyrightdate'},
191                                                 CGIitemtype => $CGIitemtype,
192                                                 CGIbranch => $CGIbranch,
193                                                 CGIbookfund => $CGIbookfund,
194                                                 isbn => $data->{'isbn'},
195                                                 seriestitle => $data->{'seriestitle'},
196                                                 quantity => $data->{'quantity'},
197                                                 listprice => $data->{'listprice'},
198                                                 rrp => $data->{'rrp'},
199                                                 invoice => $data->{'booksellerinvoicenumber'},
200                                                 ecost => $data->{'ecost'},
201                                                 notes => $data->{'notes'},
202                                                 publishercode => $data->{'publishercode'});
203
204 output_html_with_http_headers $input, $cookie, $template->output;