Can now add an item
[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::Date;
43 use C4::Auth;
44 use C4::Acquisition;
45 use C4::Context;
46 use C4::Output;
47 use C4::Interface::CGI::Output;
48 use C4::Search;
49 use HTML::Template;
50
51 sub StringSearch  {
52         my ($env,$searchstring,$type)=@_;
53         my $dbh = C4::Context->dbh;
54         $searchstring=~ s/\'/\\\'/g;
55         my @data=split(' ',$searchstring);
56         my $count=@data;
57         my $sth=$dbh->prepare("Select aqbudgetid,bookfundid,startdate,enddate,budgetamount,branchcode from aqbudget where (bookfundid like ?) order by bookfundid,aqbudgetid");
58         $sth->execute("$data[0]%");
59         my @results;
60         my $cnt=0;
61         while (my $data=$sth->fetchrow_hashref){
62         push(@results,$data);
63         $cnt ++;
64         }
65         #  $sth->execute;
66         $sth->finish;
67         return ($cnt,\@results);
68 }
69
70 my $input = new CGI;
71 my $searchfield=$input->param('searchfield');
72 my $offset=$input->param('offset');
73 my $script_name="/cgi-bin/koha/admin/aqbudget.pl";
74 my $bookfundid=$input->param('bookfundid');
75 my $aqbudgetid=$input->param('aqbudgetid');
76 my $pagesize=20;
77 my $op = $input->param('op');
78 $searchfield=~ s/\,//g;
79
80 my ($template, $borrowernumber, $cookie)
81     = get_template_and_user({template_name => "parameters/aqbudget.tmpl",
82                              query => $input,
83                              type => "intranet",
84                              authnotrequired => 0,
85                              flagsrequired => {parameters => 1},
86                              debug => 1,
87                              });
88
89 if ($op) {
90 $template->param(script_name => $script_name,
91                                                 $op              => 1); # we show only the TMPL_VAR names $op
92 } else {
93 $template->param(script_name => $script_name,
94                                                 else              => 1); # we show only the TMPL_VAR names $op
95 }
96
97 $template->param(action => $script_name);
98
99 my $dbh = C4::Context->dbh;
100 my $sthtemp = $dbh->prepare("Select flags, branchcode from borrowers where borrowernumber = ?");
101 $sthtemp->execute($borrowernumber);
102 my ($flags, $homebranch)=$sthtemp->fetchrow;
103
104 ################## ADD_FORM ##################################
105 # called by default. Used to create form to add or  modify a record
106 if ($op eq 'add_form') {
107         #---- if primkey exists, it's a modify action, so read values to modify...
108         my $dataaqbudget;
109         my $dataaqbookfund;
110         if ($aqbudgetid) {
111                 my $dbh = C4::Context->dbh;
112 #               my $sth=$dbh->prepare("select aqbudgetid,bookfundname,aqbookfund.bookfundid,aqbookfund.branchcode as bfbranch,startdate,enddate,budgetamount,aqbudget.branchcode from aqbudget,aqbookfund where aqbudgetid=? and aqbudget.bookfundid=aqbookfund.bookfundid");
113                 my $sth=$dbh->prepare("select aqbudgetid,bookfundname,aqbookfund.bookfundid,aqbookfund.branchcode as bfbranch,startdate,enddate,budgetamount from aqbudget,aqbookfund where aqbudgetid=? and aqbudget.bookfundid=aqbookfund.bookfundid");
114                 $sth->execute($aqbudgetid);
115                 $dataaqbudget=$sth->fetchrow_hashref;
116                 $sth->finish;
117         }
118         if ($bookfundid) {
119                 my $dbh = C4::Context->dbh;
120                 my $sth=$dbh->prepare("select aqbookfund.branchcode, branches.branchname from aqbookfund LEFT JOIN branches ON aqbookfund.branchcode=branches.branchcode where bookfundid=?");
121                 $sth->execute($bookfundid);
122                 $dataaqbookfund=$sth->fetchrow_hashref;
123                 $sth->finish;
124         }
125         my $header;
126         if ($aqbudgetid) {
127                 $header = "Modify budget";
128         } else {
129                 $header = "Add budget";
130         }
131         $template->param(header => $header);
132         if ($aqbudgetid) {
133             $template->param(modify => 1);
134             $template->param(bookfundid => $dataaqbudget->{'bookfundid'});
135             $template->param(bookfundname => $dataaqbudget->{'bookfundname'});
136         } else {
137             $template->param(bookfundid => $bookfundid,
138                                                                 adding => 1);
139         }
140 #       my @select_branch;
141 #       my %select_branches;
142 #       if ($dataaqbookfund->{branchcode}){
143 #               push @select_branch,$dataaqbookfund->{'branchcode'};
144 #               $select_branches{$dataaqbookfund->{'branchcode'}}=$dataaqbookfund->{'branchname'};
145 #       }else {
146 #               my @branches;
147 #               my ($count2,@branches)=branches();
148 #               push @select_branch,"";
149 #               $select_branches{""}="";
150 #               for (my $i=0;$i<$count2;$i++){
151 #                       push @select_branch, $branches[$i]->{'branchcode'};#
152 #                       $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
153 #               }
154 #       }
155 #       my $CGIbranch=CGI::scrolling_list( -name     => 'branchcode',
156 #                               -values   => \@select_branch,
157 #                               -labels   => \%select_branches,
158 #                               -default  => ($dataaqbookfund->{branchcode}?$dataaqbookfund->{branchcode}:$dataaqbudget->{branchcode}),
159 #                               -size     => 1,
160 #                               -multiple => 0 );
161 #       $template->param(CGIbranch => $CGIbranch);
162
163         $template->param(dateformat => display_date_format(),
164                                                         aqbudgetid => $dataaqbudget->{'aqbudgetid'},
165                                                         startdate => format_date($dataaqbudget->{'startdate'}),
166                                                         enddate => format_date($dataaqbudget->{'enddate'}),
167                                                         budgetamount => $dataaqbudget->{'budgetamount'}
168         );
169                                                                                                         # END $OP eq ADD_FORM
170 ################## ADD_VALIDATE ##################################
171 # called by add_form, used to insert/modify data in DB
172 } elsif ($op eq 'add_validate') {
173         my $dbh = C4::Context->dbh;
174         my $sth=$dbh->prepare("replace aqbudget (aqbudgetid,bookfundid,startdate,enddate,budgetamount,branchcode) values (?,?,?,?,?,?)");
175         $sth->execute($input->param('aqbudgetid'),$input->param('bookfundid'),
176                                                 format_date_in_iso($input->param('startdate')),
177                                                 format_date_in_iso($input->param('enddate')),
178                                                 $input->param('budgetamount'),
179                                                 $input->param('branchcode')
180                                                 );
181         $sth->finish;
182          print $input->redirect("aqbookfund.pl");
183          exit;
184 # END $OP eq ADD_VALIDATE
185 ################## DELETE_CONFIRM ##################################
186 # called by default form, used to confirm deletion of data in DB
187 } elsif ($op eq 'delete_confirm') {
188         my $dbh = C4::Context->dbh;
189         my $sth=$dbh->prepare("select aqbudgetid,bookfundid,startdate,enddate,budgetamount,branchcode from aqbudget where aqbudgetid=?");
190         $sth->execute($aqbudgetid);
191         my $data=$sth->fetchrow_hashref;
192         $sth->finish;
193         $template->param(bookfundid => $bookfundid);
194         $template->param(aqbudgetid => $data->{'aqbudgetid'});
195         $template->param(startdate => format_date($data->{'startdate'}));
196         $template->param(enddate => format_date($data->{'enddate'}));
197         $template->param(budgetamount => $data->{'budgetamount'});
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 = C4::Context->dbh;
203         my $aqbudgetid=uc($input->param('aqbudgetid'));
204         my $sth=$dbh->prepare("delete from aqbudget where aqbudgetid=?");
205         $sth->execute($aqbudgetid);
206         $sth->finish;
207          print $input->redirect("aqbookfund.pl");
208          return;
209                                                                                                         # END $OP eq DELETE_CONFIRMED
210 ################## DEFAULT ##################################
211 } else { # DEFAULT
212         if  ($searchfield ne '') {
213                 $template->param(search => 1);
214                 $template->param(searchfield => $searchfield);
215         }
216         my $env;
217         my ($count,$results)=StringSearch($env,$searchfield,'web');
218         my $toggle="white";
219         my @loop_data =();
220         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
221                 #find out stats
222         #       my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
223         #       $fines=$fines+0;
224                 my $dataaqbookfund;
225                 my $dbh = C4::Context->dbh;
226                 my $sth=$dbh->prepare("select bookfundid,bookfundname from aqbookfund where bookfundid=?");
227                 $sth->execute($results->[$i]{'bookfundid'});
228                 $dataaqbookfund=$sth->fetchrow_hashref;
229                 $sth->finish;
230                 my @toggle = ();
231                 my @bookfundid = ();
232                 my @bookfundname = ();
233                 my @startdate = ();
234                 my @enddate = ();
235                 my @budgetamount = ();
236                 push(@toggle,$toggle);
237                 push(@bookfundid,$results->[$i]{'bookfundid'});
238                 push(@bookfundname,$dataaqbookfund->{'bookfundname'});
239                 push(@startdate,format_date($results->[$i]{'startdate'}));
240                 push(@enddate,format_date($results->[$i]{'enddate'}));
241                 push(@budgetamount,$results->[$i]{'budgetamount'});
242                 if ($toggle eq 'white'){
243                         $toggle="#ffffcc";
244                 } else {
245                         $toggle="white";
246                 }
247                 while (@toggle and @bookfundid and @bookfundname and @startdate and @enddate and @budgetamount) { 
248            my %row_data;
249            $row_data{toggle} = shift @toggle;
250            $row_data{bookfundid} = shift @bookfundid;
251            $row_data{bookfundname} = shift @bookfundname;
252            $row_data{startdate} = shift @startdate;
253            $row_data{enddate} = shift @enddate;
254            $row_data{budgetamount} = shift @budgetamount;
255            push(@loop_data, \%row_data);
256        }
257        }
258        $template->param(budget => \@loop_data);
259 } #---- END $OP eq DEFAULT
260
261 output_html_with_http_headers $input, $cookie, $template->output;
262