Merged with arensb-context branch: use C4::Context->dbh instead of
[koha.git] / admin / aqbudget.pl
1 #!/usr/bin/perl
2
3 #script to administer the aqbudget table
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # ALGO :
8 # this script use an $op to know what to do.
9 # if $op is empty or none of the above values,
10 #       - the default screen is build (with all records, or filtered datas).
11 #       - the   user can clic on add, modify or delete record.
12 # if $op=add_form
13 #       - if primkey exists, this is a modification,so we read the $primkey record
14 #       - builds the add/modify form
15 # if $op=add_validate
16 #       - the user has just send datas, so we create/modify the record
17 # if $op=delete_form
18 #       - we show the record having primkey=$primkey and ask for deletion validation form
19 # if $op=delete_confirm
20 #       - we delete the record having primkey=$primkey
21
22
23 # Copyright 2000-2002 Katipo Communications
24 #
25 # This file is part of Koha.
26 #
27 # Koha is free software; you can redistribute it and/or modify it under the
28 # terms of the GNU General Public License as published by the Free Software
29 # Foundation; either version 2 of the License, or (at your option) any later
30 # version.
31 #
32 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
33 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
34 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License along with
37 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
38 # Suite 330, Boston, MA  02111-1307 USA
39
40 use strict;
41 use CGI;
42 use C4::Context;
43 use C4::Output;
44 use C4::Search;
45
46 sub StringSearch  {
47         my ($env,$searchstring,$type)=@_;
48         my $dbh = C4::Context->dbh;
49         $searchstring=~ s/\'/\\\'/g;
50         my @data=split(' ',$searchstring);
51         my $count=@data;
52         my $query="Select aqbudget.bookfundid,startdate,enddate,budgetamount,bookfundname from aqbudget,aqbookfund where aqbudget.bookfundid=aqbookfund.bookfundid and (aqbudget.bookfundid like \"$data[0]%\") order by bookfundid";
53         my $sth=$dbh->prepare($query);
54         $sth->execute;
55         my @results;
56         my $cnt=0;
57         while (my $data=$sth->fetchrow_hashref){
58         push(@results,$data);
59         $cnt ++;
60         }
61         #  $sth->execute;
62         $sth->finish;
63         return ($cnt,\@results);
64 }
65
66 my $input = new CGI;
67 my $searchfield=$input->param('searchfield');
68 my $offset=$input->param('offset');
69 my $script_name="/cgi-bin/koha/admin/aqbudget.pl";
70 my $bookfundid=$input->param('bookfundid');
71 my $pagesize=20;
72 my $op = $input->param('op');
73 $searchfield=~ s/\,//g;
74 print $input->header;
75
76 #start the page and read in includes
77 print startpage();
78 print startmenu('admin');
79
80 ################## ADD_FORM ##################################
81 # called by default. Used to create form to add or  modify a record
82 if ($op eq 'add_form') {
83         #---- if primkey exists, it's a modify action, so read values to modify...
84         my $data;
85         if ($bookfundid) {
86                 my $dbh = C4::Context->dbh;
87                 my $query="select aqbookfund.bookfundid,startdate,enddate,budgetamount,bookfundname from aqbudget,aqbookfund where aqbudget.bookfundid=aqbookfund.bookfundid and aqbookfund.bookfundid='$bookfundid'";
88 #               print $query;
89                 my $sth=$dbh->prepare($query);
90                 $sth->execute;
91                 $data=$sth->fetchrow_hashref;
92                 $sth->finish;
93         }
94         print <<printend
95         <script>
96         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
97         function isNotNull(f,noalert) {
98                 if (f.value.length ==0) {
99    return false;
100                 }
101                 return true;
102         }
103         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
104         function toUC(f) {
105                 var x=f.value.toUpperCase();
106                 f.value=x;
107                 return true;
108         }
109         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
110         function isNum(v,maybenull) {
111         var n = new Number(v.value);
112         if (isNaN(n)) {
113                 return false;
114                 }
115         if (maybenull==0 && v.value=='') {
116                 return false;
117         }
118         return true;
119         }
120         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
121         function isDate(f) {
122                 var t = Date.parse(f.value);
123                 if (isNaN(t)) {
124                         return false;
125                 }
126         }
127         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
128         function Check(f) {
129                 var ok=1;
130                 var _alertString="";
131                 var alertString2;
132                 if (f.bookfundid.value.length==0) {
133                         _alertString += "- bookfundid missing\\n";
134                 }
135                 if (!(isNotNull(window.document.Aform.budgetamount,1))) {
136                         _alertString += "- Budget missing\\n";
137                 }
138                 if (_alertString.length==0) {
139                         document.Aform.submit();
140                 } else {
141                         alertString2 = "Form not submitted because of the following problem(s)\\n";
142                         alertString2 += "------------------------------------------------------------------------------------\\n\\n";
143                         alertString2 += _alertString;
144                         alert(alertString2);
145                 }
146         }
147         </SCRIPT>
148 printend
149 ;#/
150         if ($bookfundid) {
151                 print "<h1>Modify budget</h1>";
152         } else {
153                 print "<h1>Add budget</h1>";
154         }
155         print "<form action='$script_name' name=Aform method=post>";
156         print "<input type=hidden name=op value='add_validate'>";
157         print "<input type=hidden name=checked value=0>";
158         print "<table>";
159         if ($bookfundid) {
160                 print "<tr><td>Book fund</td><td><input type=hidden name=bookfundid value=$bookfundid>$bookfundid</td></tr>";
161                 print "<tr><td>Book fund</td><td>$data->{'bookfundname'}</td></tr>";
162         } else {
163                 print "<tr><td>Book fund</td><td><input type=text name=bookfundid size=5 maxlength=5 onBlur=toUC(this)></td></tr>";
164         }
165         print "<tr><td>Start date</td><td><input type=text name=startdate size=40 maxlength=80 value='$data->{'startdate'}'>&nbsp;</td></tr>";
166         print "<tr><td>End date</td><td><input type=text name=enddate value='$data->{'enddate'}'></td></tr>";
167         print "<tr><td>Budget amount</td><td><input type=text name=budgetamount value='$data->{'budgetamount'}'></td></tr>";
168         print "<tr><td>&nbsp;</td><td><INPUT type=button value='OK' onClick='Check(this.form)'></td></tr>";
169 print "</table>";
170         print "</form>";
171 ;
172                                                                                                         # END $OP eq ADD_FORM
173 ################## ADD_VALIDATE ##################################
174 # called by add_form, used to insert/modify data in DB
175 } elsif ($op eq 'add_validate') {
176         my $dbh = C4::Context->dbh;
177         my $query = "replace aqbudget (bookfundid,startdate,enddate,budgetamount) values (";
178         $query.= $dbh->quote($input->param('bookfundid')).",";
179         $query.= $dbh->quote($input->param('startdate')).",";
180         $query.= $dbh->quote($input->param('enddate')).",";
181         $query.= $dbh->quote($input->param('budgetamount')).")";
182         my $sth=$dbh->prepare($query);
183         $sth->execute;
184         $sth->finish;
185         print "data recorded";
186         print "<form action='$script_name' method=post>";
187         print "<input type=submit value=OK>";
188         print "</form>";
189                                                                                                         # END $OP eq ADD_VALIDATE
190 ################## DELETE_CONFIRM ##################################
191 # called by default form, used to confirm deletion of data in DB
192 } elsif ($op eq 'delete_confirm') {
193         my $dbh = C4::Context->dbh;
194 #       my $sth=$dbh->prepare("select count(*) as total from categoryitem where itemtype='$itemtype'");
195 #       $sth->execute;
196 #       my $total = $sth->fetchrow_hashref;
197 #       $sth->finish;
198         my $sth=$dbh->prepare("select bookfundid,startdate,enddate,budgetamount from aqbudget where bookfundid='$bookfundid'");
199         $sth->execute;
200         my $data=$sth->fetchrow_hashref;
201         $sth->finish;
202         print mktablehdr;
203         print mktablerow(2,'#99cc33',bold('Book fund'),bold("$bookfundid"),'/images/background-mem.gif');
204         print "<form action='$script_name' method=post><input type=hidden name=op value=delete_confirmed><input type=hidden name=bookfundid value='$bookfundid'>";
205         print "<tr><td>Start date</td><td>$data->{'startdate'}</td></tr>";
206         print "<tr><td>End date</td><td>$data->{'enddate'}</td></tr>";
207         print "<tr><td>budgetamount</td><td>$data->{'budgetamount'}</td></tr>";
208 #       if ($total->{'total'} >0) {
209 #               print "<tr><td colspan=2 align=center><b>This record is used $total->{'total'} times. Deletion not possible</b></td></tr>";
210 #               print "<tr><td colspan=2></form><form action='$script_name' method=post><input type=submit value=OK></form></td></tr>";
211 #       } else {
212                 print "<tr><td colspan=2 align=center>CONFIRM DELETION</td></tr>";
213                 print "<tr><td><INPUT type=submit value='YES'></form></td><td><form action='$script_name' method=post><input type=submit value=NO></form></td></tr>";
214 #       }
215                                                                                                         # END $OP eq DELETE_CONFIRM
216 ################## DELETE_CONFIRMED ##################################
217 # called by delete_confirm, used to effectively confirm deletion of data in DB
218 } elsif ($op eq 'delete_confirmed') {
219         my $dbh = C4::Context->dbh;
220         my $bookfundid=uc($input->param('bookfundid'));
221         my $query = "delete from aqbudget where bookfundid='$bookfundid'";
222         my $sth=$dbh->prepare($query);
223         $sth->execute;
224         $sth->finish;
225         print "data deleted";
226         print "<form action='$script_name' method=post>";
227         print "<input type=submit value=OK>";
228         print "</form>";
229                                                                                                         # END $OP eq DELETE_CONFIRMED
230 ################## DEFAULT ##################################
231 } else { # DEFAULT
232         my @inputs=(["text","searchfield",$searchfield],
233                 ["reset","reset","clr"]);
234         print mkheadr(2,'Budget admin');
235         print mkformnotable("$script_name",@inputs);
236         print <<printend
237
238 printend
239         ;
240         if  ($searchfield ne '') {
241                 print "You Searched for <b>$searchfield<b><p>";
242         }
243         print mktablehdr;
244         print mktablerow(6,'#99cc33',bold('Book fund'),bold('Start date'),bold('End date'),bold('Budget amount'),
245         '&nbsp;','&nbsp;','/images/background-mem.gif');
246         my $env;
247         my ($count,$results)=StringSearch($env,$searchfield,'web');
248         my $toggle="white";
249         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
250                 #find out stats
251         #       my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
252         #       $fines=$fines+0;
253                 if ($toggle eq 'white'){
254                         $toggle="#ffffcc";
255                 } else {
256                         $toggle="white";
257                 }
258                 print mktablerow(6,$toggle,$results->[$i]{'bookfundid'}.' ('.$results->[$i]{'bookfundname'}.')',
259                 $results->[$i]{'startdate'},$results->[$i]{'enddate'},
260                 $results->[$i]{'budgetamount'},
261                 mklink("$script_name?op=add_form&bookfundid=".$results->[$i]{'bookfundid'},'Edit'),
262                 mklink("$script_name?op=delete_confirm&bookfundid=".$results->[$i]{'bookfundid'},'Delete','')); # FIXME - Too many arguments. Drop the 3d one?
263         }
264         print mktableft;
265         print "<form action='$script_name' method=post>";
266         print "<input type=hidden name=op value=add_form>";
267         if ($offset>0) {
268                 my $prevpage = $offset-$pagesize;
269                 print mklink("$script_name?offset=".$prevpage,'&lt;&lt; Prev');
270         }
271         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
272         if ($offset+$pagesize<$count) {
273                 my $nextpage =$offset+$pagesize;
274                 print mklink("$script_name?offset=".$nextpage,'Next &gt;&gt;');
275         }
276         print "<br><input type=image src=\"/images/button-add-new.gif\"  WIDTH=188  HEIGHT=44  ALT=\"Add budget\" BORDER=0 ></a><br>";
277         print "</form>";
278 } #---- END $OP eq DEFAULT
279 print endmenu('admin');
280 print endpage();