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