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