Adding branch Independancy support for searchsugestion.
[koha.git] / C4 / Suggestions.pm
1 package C4::Suggestions;
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
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 require Exporter;
24 use DBI;
25 use C4::Context;
26 use C4::Output;
27 use Mail::Sendmail;
28 # use C4::Interface::CGI::Output;
29 use vars qw($VERSION @ISA @EXPORT);
30
31 # set the version for version checking
32 $VERSION = 0.01;
33
34 =head1 NAME
35
36 C4::Accounts - Functions for dealing with Koha authorities
37
38 =head1 SYNOPSIS
39
40   use C4::Suggestions;
41
42 =head1 DESCRIPTION
43
44 The functions in this module deal with the suggestions :
45 * in OPAC
46 * in librarian interface
47
48 A suggestion is done in the OPAC. It has the status "ASKED"
49 When a librarian manages the suggestion, he can set the status to "REJECTED" or "ORDERED".
50 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
51 All suggestions of a borrower by the borrower itself.
52 Suggestions done by other can be seen when not "AVAILABLE"
53
54 =head1 FUNCTIONS
55
56 =over 2
57
58 =cut
59
60 @ISA = qw(Exporter);
61 @EXPORT = qw(   &newsuggestion
62                                 &searchsuggestion
63                                 &getsuggestion
64                                 &delsuggestion
65                                 &countsuggestion
66                                 &changestatus
67                         );
68
69 =item SearchSuggestion
70
71   (\@array) = &SearchSuggestion($user)
72
73   searches for a suggestion
74
75 C<$user> is the user code (used as suggestor filter)
76
77 return :
78 C<\@array> : the suggestions found. Array of hash.
79 Note the status is stored twice :
80 * in the status field
81 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
82
83 =cut
84 sub searchsuggestion  {
85         my ($user,$author,$title,$publishercode,$status,$suggestedbyme)=@_;
86         my $dbh = C4::Context->dbh;
87         my $query="Select suggestions.*,
88                                                 U1.surname as surnamesuggestedby,U1.firstname as firstnamesuggestedby,
89                                                 U2.surname as surnamemanagedby,U2.firstname as firstnamemanagedby 
90                                                 from suggestions
91                                                 left join borrowers as U1 on suggestedby=U1.borrowernumber
92                                                 left join borrowers as U2  on managedby=U2.borrowernumber
93                                                 where 1=1";
94         my @sql_params;
95         if ($author) {
96                 push @sql_params,"%".$author."%";
97                 $query .= " and author like ?";
98         }
99         if ($title) {
100                 push @sql_params,"%".$title."%";
101                 $query .= " and suggestions.title like ?";
102         }
103         if ($publishercode) {
104                 push @sql_params,"%".$publishercode."%";
105                 $query .= " and publishercode like ?";
106         }
107         if ($status) {
108                 push @sql_params,$status;
109                 $query .= " and status=?";
110         }
111         
112         if (C4::Context->preference("IndependantBranches")) {
113                 my $userenv = C4::Context->userenv;
114                 unless ($userenv->{flags} == 1){
115                         push @sql_params,$userenv->{branch};
116                         $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
117                 }
118         }
119         if ($suggestedbyme) {
120                 if ($suggestedbyme eq -1) {
121                 } else {
122                         push @sql_params,$user;
123                         $query .= " and suggestedby=?";
124                 }
125         } else {
126                 $query .= " and managedby is NULL";
127         }
128         my $sth=$dbh->prepare($query);
129         $sth->execute(@sql_params);
130         my @results;
131         my $even=1; # the even variable is used to set even / odd lines, for highlighting
132         while (my $data=$sth->fetchrow_hashref){
133                         $data->{$data->{STATUS}} = 1;
134                         if ($even) {
135                                 $even=0;
136                                 $data->{even}=1;
137                         } else {
138                                 $even=1;
139                         }
140                         push(@results,$data);
141         }
142         return (\@results);
143 }
144
145 sub newsuggestion {
146         my ($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber) = @_;
147         my $dbh = C4::Context->dbh;
148         my $sth = $dbh->prepare("insert into suggestions (status,suggestedby,title,author,publishercode,note,copyrightdate,volumedesc,publicationyear,place,isbn,biblionumber) values ('ASKED',?,?,?,?,?,?,?,?,?,?,?)");
149         $sth->execute($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber);
150 }
151
152 sub getsuggestion {
153         my ($suggestionid) = @_;
154         my $dbh = C4::Context->dbh;
155         my $sth = $dbh->prepare("select * from suggestions where suggestionid=?");
156         $sth->execute($suggestionid);
157         return($sth->fetchrow_hashref);
158 }
159
160 sub delsuggestion {
161         my ($borrowernumber,$suggestionid) = @_;
162         my $dbh = C4::Context->dbh;
163         # check that the suggestion comes from the suggestor
164         my $sth = $dbh->prepare("select suggestedby from suggestions where suggestionid=?");
165         $sth->execute($suggestionid);
166         my ($suggestedby) = $sth->fetchrow;
167         if ($suggestedby eq $borrowernumber) {
168                 $sth = $dbh->prepare("delete from suggestions where suggestionid=?");
169                 $sth->execute($suggestionid);
170         }
171 }
172
173 sub countsuggestion {
174         my ($status) = @_;
175         my $dbh = C4::Context->dbh;
176         my $sth;
177         if (C4::Context->preference("IndependantBranches")){
178                 my $userenv = C4::Context->userenv;
179                 if ($userenv->{flags} == 1){
180                         $sth = $dbh->prepare("select count(*) from suggestions where status=?");
181                         $sth->execute($status);
182                 } else {
183                         $sth = $dbh->prepare("select count(*) from suggestions,borrowers where status=? and borrowers.borrowernumber=suggestions.suggestedby and (borrowers.branchcode='' or borrowers.branchcode =?)");
184                         $sth->execute($status,$userenv->{branch});
185                 }
186         } else {
187                 $sth = $dbh->prepare("select count(*) from suggestions where status=?");
188                 $sth->execute($status);
189         }
190         my ($result) = $sth->fetchrow;
191         return $result;
192 }
193
194 sub changestatus {
195         my ($suggestionid,$status,$managedby) = @_;
196         my $dbh = C4::Context->dbh;
197         my $sth;
198         if ($managedby>0) {
199                 $sth = $dbh->prepare("update suggestions set status=?,managedby=? where suggestionid=?");
200                 $sth->execute($status,$managedby,$suggestionid);
201         } else {
202                 $sth = $dbh->prepare("update suggestions set status=? where suggestionid=?");
203                 $sth->execute($status,$suggestionid);
204
205         }
206         # check mail sending.
207         $sth = $dbh->prepare("select suggestions.*,
208                                                         boby.surname as bysurname, boby.firstname as byfirstname, boby.emailaddress as byemail,
209                                                         lib.surname as libsurname,lib.firstname as libfirstname,lib.emailaddress as libemail
210                                                 from suggestions left join borrowers as boby on boby.borrowernumber=suggestedby left join borrowers as lib on lib.borrowernumber=managedby where suggestionid=?");
211         $sth->execute($suggestionid);
212         my $emailinfo = $sth->fetchrow_hashref;
213         my $template = gettemplate("suggestion/mail_suggestion_$status.tmpl","intranet");
214 #                                query =>'',
215 #                            authnotrequired => 1,
216 #                        });
217         $template->param(byemail => $emailinfo->{byemail},
218                                         libemail => $emailinfo->{libemail},
219                                         status => $emailinfo->{status},
220                                         title => $emailinfo->{title},
221                                         author =>$emailinfo->{author},
222                                         libsurname => $emailinfo->{libsurname},
223                                         libfirstname => $emailinfo->{libfirstname},
224                                         byfirstname => $emailinfo->{byfirstname},
225                                         bysurname => $emailinfo->{bysurname},
226                                         );
227         my %mail = ( To => $emailinfo->{byemail},
228                                  From => $emailinfo->{libemail},
229                                  Subject => 'Koha suggestion',
230                                  Message => "".$template->output
231                                  );
232 sendmail(%mail);
233 #       warn "sending email to $emailinfo->{byemail} from $emailinfo->{libemail} to notice new status $emailinfo->{status} for $emailinfo->{title} / $emailinfo->{author}";
234 }
235
236 =back
237
238 =head1 SEE ALSO
239
240 =cut