Added copyright statement to all .pl and .pm files
[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 C4::Output;
42 use CGI;
43 use C4::Search;
44 use C4::Database;
45
46 sub StringSearch  {
47         my ($env,$searchstring,$type)=@_;
48         my $dbh = &C4Connect;
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         $dbh->disconnect;
64         return ($cnt,\@results);
65 }
66
67 my $input = new CGI;
68 my $searchfield=$input->param('searchfield');
69 my $offset=$input->param('offset');
70 my $script_name="/cgi-bin/koha/admin/aqbudget.pl";
71 my $bookfundid=$input->param('bookfundid');
72 my $pagesize=20;
73 my $op = $input->param('op');
74 $searchfield=~ s/\,//g;
75 print $input->header;
76
77 #start the page and read in includes
78 print startpage();
79 print startmenu('admin');
80
81 ################## ADD_FORM ##################################
82 # called by default. Used to create form to add or  modify a record
83 if ($op eq 'add_form') {
84         #---- if primkey exists, it's a modify action, so read values to modify...
85         my $data;
86         if ($bookfundid) {
87                 my $dbh = &C4Connect;
88                 my $query="select aqbookfund.bookfundid,startdate,enddate,budgetamount,bookfundname from aqbudget,aqbookfund where aqbudget.bookfundid=aqbookfund.bookfundid and aqbookfund.bookfundid='$bookfundid'";
89 #               print $query;
90                 my $sth=$dbh->prepare($query);
91                 $sth->execute;
92                 $data=$sth->fetchrow_hashref;
93                 $sth->finish;
94         }
95         print <<printend
96         <script>
97         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
98         function isNotNull(f,noalert) {
99                 if (f.value.length ==0) {
100    return false;
101                 }
102                 return true;
103         }
104         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
105         function toUC(f) {
106                 var x=f.value.toUpperCase();
107                 f.value=x;
108                 return true;
109         }
110         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
111         function isNum(v,maybenull) {
112         var n = new Number(v.value);
113         if (isNaN(n)) {
114                 return false;
115                 }
116         if (maybenull==0 && v.value=='') {
117                 return false;
118         }
119         return true;
120         }
121         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
122         function isDate(f) {
123                 var t = Date.parse(f.value);
124                 if (isNaN(t)) {
125                         return false;
126                 }
127         }
128         /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
129         function Check(f) {
130                 var ok=1;
131                 var _alertString="";
132                 var alertString2;
133                 if (f.bookfundid.value.length==0) {
134                         _alertString += "- bookfundid missing\\n";
135                 }
136                 if (!(isNotNull(window.document.Aform.budgetamount,1))) {
137                         _alertString += "- Budget missing\\n";
138                 }
139                 if (_alertString.length==0) {
140                         document.Aform.submit();
141                 } else {
142                         alertString2 = "Form not submitted because of the following problem(s)\\n";
143                         alertString2 += "------------------------------------------------------------------------------------\\n\\n";
144                         alertString2 += _alertString;
145                         alert(alertString2);
146                 }
147         }
148         </SCRIPT>
149 printend
150 ;#/
151         if ($bookfundid) {
152                 print "<h1>Modify budget</h1>";
153         } else {
154                 print "<h1>Add budget</h1>";
155         }
156         print "<form action='$script_name' name=Aform method=post>";
157         print "<input type=hidden name=op value='add_validate'>";
158         print "<input type=hidden name=checked value=0>";
159         print "<table>";
160         if ($bookfundid) {
161                 print "<tr><td>Book fund</td><td><input type=hidden name=bookfundid value=$bookfundid>$bookfundid</td></tr>";
162                 print "<tr><td>Book fund</td><td>$data->{'bookfundname'}</td></tr>";
163         } else {
164                 print "<tr><td>Book fund</td><td><input type=text name=bookfundid size=5 maxlength=5 onBlur=toUC(this)></td></tr>";
165         }
166         print "<tr><td>Start date</td><td><input type=text name=startdate size=40 maxlength=80 value='$data->{'startdate'}'>&nbsp;</td></tr>";
167         print "<tr><td>End date</td><td><input type=text name=enddate value='$data->{'enddate'}'></td></tr>";
168         print "<tr><td>Budget amount</td><td><input type=text name=budgetamount value='$data->{'budgetamount'}'></td></tr>";
169         print "<tr><td>&nbsp;</td><td><INPUT type=button value='OK' onClick='Check(this.form)'></td></tr>";
170 print "</table>";
171         print "</form>";
172 ;
173                                                                                                         # END $OP eq ADD_FORM
174 ################## ADD_VALIDATE ##################################
175 # called by add_form, used to insert/modify data in DB
176 } elsif ($op eq 'add_validate') {
177         my $dbh=C4Connect;
178         my $query = "replace aqbudget (bookfundid,startdate,enddate,budgetamount) values (";
179         $query.= $dbh->quote($input->param('bookfundid')).",";
180         $query.= $dbh->quote($input->param('startdate')).",";
181         $query.= $dbh->quote($input->param('enddate')).",";
182         $query.= $dbh->quote($input->param('budgetamount')).")";
183         my $sth=$dbh->prepare($query);
184         $sth->execute;
185         $sth->finish;
186         print "data recorded";
187         print "<form action='$script_name' method=post>";
188         print "<input type=submit value=OK>";
189         print "</form>";
190                                                                                                         # END $OP eq ADD_VALIDATE
191 ################## DELETE_CONFIRM ##################################
192 # called by default form, used to confirm deletion of data in DB
193 } elsif ($op eq 'delete_confirm') {
194         my $dbh = &C4Connect;
195 #       my $sth=$dbh->prepare("select count(*) as total from categoryitem where itemtype='$itemtype'");
196 #       $sth->execute;
197 #       my $total = $sth->fetchrow_hashref;
198 #       $sth->finish;
199         my $sth=$dbh->prepare("select bookfundid,startdate,enddate,budgetamount from aqbudget where bookfundid='$bookfundid'");
200         $sth->execute;
201         my $data=$sth->fetchrow_hashref;
202         $sth->finish;
203         print mktablehdr;
204         print mktablerow(2,'#99cc33',bold('Book fund'),bold("$bookfundid"),'/images/background-mem.gif');
205         print "<form action='$script_name' method=post><input type=hidden name=op value=delete_confirmed><input type=hidden name=bookfundid value='$bookfundid'>";
206         print "<tr><td>Start date</td><td>$data->{'startdate'}</td></tr>";
207         print "<tr><td>End date</td><td>$data->{'enddate'}</td></tr>";
208         print "<tr><td>budgetamount</td><td>$data->{'budgetamount'}</td></tr>";
209 #       if ($total->{'total'} >0) {
210 #               print "<tr><td colspan=2 align=center><b>This record is used $total->{'total'} times. Deletion not possible</b></td></tr>";
211 #               print "<tr><td colspan=2></form><form action='$script_name' method=post><input type=submit value=OK></form></td></tr>";
212 #       } else {
213                 print "<tr><td colspan=2 align=center>CONFIRM DELETION</td></tr>";
214                 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>";
215 #       }
216                                                                                                         # END $OP eq DELETE_CONFIRM
217 ################## DELETE_CONFIRMED ##################################
218 # called by delete_confirm, used to effectively confirm deletion of data in DB
219 } elsif ($op eq 'delete_confirmed') {
220         my $dbh=C4Connect;
221         my $bookfundid=uc($input->param('bookfundid'));
222         my $query = "delete from aqbudget where bookfundid='$bookfundid'";
223         my $sth=$dbh->prepare($query);
224         $sth->execute;
225         $sth->finish;
226         print "data deleted";
227         print "<form action='$script_name' method=post>";
228         print "<input type=submit value=OK>";
229         print "</form>";
230                                                                                                         # END $OP eq DELETE_CONFIRMED
231 ################## DEFAULT ##################################
232 } else { # DEFAULT
233         my @inputs=(["text","searchfield",$searchfield],
234                 ["reset","reset","clr"]);
235         print mkheadr(2,'Budget admin');
236         print mkformnotable("$script_name",@inputs);
237         print <<printend
238
239 printend
240         ;
241         if  ($searchfield ne '') {
242                 print "You Searched for <b>$searchfield<b><p>";
243         }
244         print mktablehdr;
245         print mktablerow(6,'#99cc33',bold('Book fund'),bold('Start date'),bold('End date'),bold('Budget amount'),
246         '&nbsp;','&nbsp;','/images/background-mem.gif');
247         my $env;
248         my ($count,$results)=StringSearch($env,$searchfield,'web');
249         my $toggle="white";
250         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
251                 #find out stats
252         #       my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
253         #       $fines=$fines+0;
254                 if ($toggle eq 'white'){
255                         $toggle="#ffffcc";
256                 } else {
257                         $toggle="white";
258                 }
259                 print mktablerow(6,$toggle,$results->[$i]{'bookfundid'}.' ('.$results->[$i]{'bookfundname'}.')',
260                 $results->[$i]{'startdate'},$results->[$i]{'enddate'},
261                 $results->[$i]{'budgetamount'},
262                 mklink("$script_name?op=add_form&bookfundid=".$results->[$i]{'bookfundid'},'Edit'),
263                 mklink("$script_name?op=delete_confirm&bookfundid=".$results->[$i]{'bookfundid'},'Delete',''));
264         }
265         print mktableft;
266         print "<form action='$script_name' method=post>";
267         print "<input type=hidden name=op value=add_form>";
268         if ($offset>0) {
269                 my $prevpage = $offset-$pagesize;
270                 print mklink("$script_name?offset=".$prevpage,'&lt;&lt; Prev');
271         }
272         print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
273         if ($offset+$pagesize<$count) {
274                 my $nextpage =$offset+$pagesize;
275                 print mklink("$script_name?offset=".$nextpage,'Next &gt;&gt;');
276         }
277         print "<br><input type=image src=\"/images/button-add-new.gif\"  WIDTH=188  HEIGHT=44  ALT=\"Add budget\" BORDER=0 ></a><br>";
278         print "</form>";
279 } #---- END $OP eq DEFAULT
280 print endmenu('admin');
281 print endpage();