Budget management
[koha.git] / admin / aqbudget_owner_search.pl
1 #!/usr/bin/perl
2
3 # script to find a guarantor
4
5 # Copyright 2008-2009 BibLibre SARL
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use C4::Auth ;
24 use C4::Output;
25 use CGI;
26 use C4::Dates qw/format_date/;
27 use C4::Members;
28
29 my $input = new CGI;
30
31 my $dbh = C4::Context->dbh;
32
33 my ( $template, $loggedinuser, $cookie, $staff_flags ) = get_template_and_user(
34     {   template_name   => "admin/aqbudget_owner_search.tmpl",
35         query           => $input,
36         type            => "intranet",
37         authnotrequired => 0,
38         flagsrequired   => { acquisition => 'budget_modify'  },
39         debug           => 1,
40     }
41 );
42
43 my $theme = $input->param('theme') || "default";
44
45 # only used if allowthemeoverride is set
46 my $member  = $input->param('member');
47 my $orderby = $input->param('orderby');
48
49 my $op = $input->param('op');
50 $template->param( $op || else => 1, );
51
52 $orderby = "surname,firstname" unless $orderby;
53 $member =~ s/,//g;     #remove any commas from search string
54 $member =~ s/\*/%/g;
55 if ( $member eq '' ) {
56     $template->param( results => 0 );
57 } else {
58     $template->param( results => 1 );
59 }
60
61 my ( $count, $count2, $results );
62 my @resultsdata;
63 my $toggle = 0;
64
65 if ( $member ) {
66     my $dbh = C4::Context->dbh;
67     my $sth = $dbh->prepare(
68         qq|       SELECT  * from borrowers where surname like ? or firstname like ? or cardnumber like ? |
69     );
70
71     $sth->execute( "$member%", "$member%", "$member%", );
72     my $results = $sth->fetchall_arrayref({});      
73
74     foreach my $res (@$results) {
75
76         my $perms = haspermission( $dbh, $res->{'userid'} );
77         my $subperms =  get_user_subpermissions  ($res->{'userid'} );
78
79
80         # if the member has 'acqui' permission set, then display to table.
81         if (    $perms->{superlibrarian} == 1  || 
82                 $perms->{acquisition} == 1  || 
83                 $subperms->{acquisition}->{'budget_manage'} || 
84                 $subperms->{acquisition}->{'budget_modify'} || 
85                 $subperms->{acquisition}->{'budget_add_del'}  ) {
86
87             $count2++;
88             #find out stats
89             my ( $od, $issue, $fines ) = GetMemberIssuesAndFines( $res->{'borrowerid'} );
90             my $guarantorinfo = uc( $res->{'surname'} ) . " , " . ucfirst( $res->{'firstname'} );
91             my $budget_owner_name = $res->{'firstname'} . ' ' . $res->{'surname'}, my $budget_owner_id = $res->{'borrowernumber'};
92
93             my %row = (
94                 toggle            => $toggle,
95                 count             => 1,
96                 borrowernumber    => $res->{'borrowernumber'},
97                 cardnumber        => $res->{'cardnumber'},
98                 surname           => $res->{'surname'},
99                 firstname         => $res->{'firstname'},
100                 categorycode      => $res->{'categorycode'},
101                 branchcode        => $res->{'branchcode'},
102                 guarantorinfo     => $guarantorinfo,
103                 budget_owner_id   => $budget_owner_id,
104                 budget_owner_name => $budget_owner_name,
105                 odissue           => "$od/$issue",
106                 fines             => $fines,
107 #                borrowernotes     => $res->{'borrowernotes'}
108             );
109             $toggle = ( $toggle++ % 2 eq 0 ? 1 : 0 );
110             push( @resultsdata, \%row );
111         }
112     }
113 }
114
115 $template->param(
116     member => $member,
117     numres => $count2,
118     resultsloop => \@resultsdata
119 );
120
121 output_html_with_http_headers $input, $cookie, $template->output;