Use this script to export all your authorities so that you can build an authority...
[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                                 &findsuggestion_from_biblionumber
68                         );
69
70 =item SearchSuggestion
71
72   (\@array) = &SearchSuggestion($user)
73
74   searches for a suggestion
75
76 C<$user> is the user code (used as suggestor filter)
77
78 return :
79 C<\@array> : the suggestions found. Array of hash.
80 Note the status is stored twice :
81 * in the status field
82 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
83
84 =cut
85 sub searchsuggestion  {
86         my ($user,$author,$title,$publishercode,$status,$suggestedbyme)=@_;
87         my $dbh = C4::Context->dbh;
88         my $query="Select suggestions.*,
89                                                 U1.surname as surnamesuggestedby,U1.firstname as firstnamesuggestedby,
90                                                 U2.surname as surnamemanagedby,U2.firstname as firstnamemanagedby 
91                                                 from suggestions
92                                                 left join borrowers as U1 on suggestedby=U1.borrowernumber
93                                                 left join borrowers as U2  on managedby=U2.borrowernumber
94                                                 where 1=1";
95         my @sql_params;
96         if ($author) {
97                 push @sql_params,"%".$author."%";
98                 $query .= " and author like ?";
99         }
100         if ($title) {
101                 push @sql_params,"%".$title."%";
102                 $query .= " and suggestions.title like ?";
103         }
104         if ($publishercode) {
105                 push @sql_params,"%".$publishercode."%";
106                 $query .= " and publishercode like ?";
107         }
108         if ($status) {
109                 push @sql_params,$status;
110                 $query .= " and status=?";
111         }
112         
113         if (C4::Context->preference("IndependantBranches")) {
114                 my $userenv = C4::Context->userenv;
115                 unless ($userenv->{flags} == 1){
116                         push @sql_params,$userenv->{branch};
117                         $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
118                 }
119         }
120         if ($suggestedbyme) {
121                 if ($suggestedbyme eq -1) {
122                 } else {
123                         push @sql_params,$user;
124                         $query .= " and suggestedby=?";
125                 }
126         } else {
127                 $query .= " and managedby is NULL";
128         }
129         my $sth=$dbh->prepare($query);
130         $sth->execute(@sql_params);
131         my @results;
132         my $even=1; # the even variable is used to set even / odd lines, for highlighting
133         while (my $data=$sth->fetchrow_hashref){
134                         $data->{$data->{STATUS}} = 1;
135                         if ($even) {
136                                 $even=0;
137                                 $data->{even}=1;
138                         } else {
139                                 $even=1;
140                         }
141                         push(@results,$data);
142         }
143         return (\@results);
144 }
145
146 sub newsuggestion {
147         my ($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber) = @_;
148         my $dbh = C4::Context->dbh;
149         my $sth = $dbh->prepare("insert into suggestions (status,suggestedby,title,author,publishercode,note,copyrightdate,volumedesc,publicationyear,place,isbn,biblionumber) values ('ASKED',?,?,?,?,?,?,?,?,?,?,?)");
150         $sth->execute($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber);
151 }
152
153 sub getsuggestion {
154         my ($suggestionid) = @_;
155         my $dbh = C4::Context->dbh;
156         my $sth = $dbh->prepare("select * from suggestions where suggestionid=?");
157         $sth->execute($suggestionid);
158         return($sth->fetchrow_hashref);
159 }
160
161 sub delsuggestion {
162         my ($borrowernumber,$suggestionid) = @_;
163         my $dbh = C4::Context->dbh;
164         # check that the suggestion comes from the suggestor
165         my $sth = $dbh->prepare("select suggestedby from suggestions where suggestionid=?");
166         $sth->execute($suggestionid);
167         my ($suggestedby) = $sth->fetchrow;
168         if ($suggestedby eq $borrowernumber) {
169                 $sth = $dbh->prepare("delete from suggestions where suggestionid=?");
170                 $sth->execute($suggestionid);
171         }
172 }
173
174 sub countsuggestion {
175         my ($status) = @_;
176         my $dbh = C4::Context->dbh;
177         my $sth;
178         if (C4::Context->preference("IndependantBranches")){
179                 my $userenv = C4::Context->userenv;
180                 if ($userenv->{flags} == 1){
181                         $sth = $dbh->prepare("select count(*) from suggestions where status=?");
182                         $sth->execute($status);
183                 } else {
184                         $sth = $dbh->prepare("select count(*) from suggestions,borrowers where status=? and borrowers.borrowernumber=suggestions.suggestedby and (borrowers.branchcode='' or borrowers.branchcode =?)");
185                         $sth->execute($status,$userenv->{branch});
186                 }
187         } else {
188                 $sth = $dbh->prepare("select count(*) from suggestions where status=?");
189                 $sth->execute($status);
190         }
191         my ($result) = $sth->fetchrow;
192         return $result;
193 }
194
195 sub changestatus {
196         my ($suggestionid,$status,$managedby,$biblionumber) = @_;
197         my $dbh = C4::Context->dbh;
198         my $sth;
199         if ($managedby>0) {
200                 if ($biblionumber) {
201                         $sth = $dbh->prepare("update suggestions set status=?,managedby=?,biblionumber=? where suggestionid=?");
202                         $sth->execute($status,$managedby,$biblionumber,$suggestionid);
203                 } else {
204                         $sth = $dbh->prepare("update suggestions set status=?,managedby=? where suggestionid=?");
205                         $sth->execute($status,$managedby,$suggestionid);
206                 }
207         } else {
208                 if ($biblionumber) {
209                         $sth = $dbh->prepare("update suggestions set status=?,biblionumber=? where suggestionid=?");
210                         $sth->execute($status,$biblionumber,$suggestionid);
211                 } else {
212                         $sth = $dbh->prepare("update suggestions set status=? where suggestionid=?");
213                         $sth->execute($status,$suggestionid);
214                 }
215
216         }
217         # check mail sending.
218         $sth = $dbh->prepare("select suggestions.*,
219                                                         boby.surname as bysurname, boby.firstname as byfirstname, boby.emailaddress as byemail,
220                                                         lib.surname as libsurname,lib.firstname as libfirstname,lib.emailaddress as libemail
221                                                 from suggestions left join borrowers as boby on boby.borrowernumber=suggestedby left join borrowers as lib on lib.borrowernumber=managedby where suggestionid=?");
222         $sth->execute($suggestionid);
223         my $emailinfo = $sth->fetchrow_hashref;
224         my $template = gettemplate("suggestion/mail_suggestion_$status.tmpl","intranet");
225 #                                query =>'',
226 #                            authnotrequired => 1,
227 #                        });
228         $template->param(byemail => $emailinfo->{byemail},
229                                         libemail => $emailinfo->{libemail},
230                                         status => $emailinfo->{status},
231                                         title => $emailinfo->{title},
232                                         author =>$emailinfo->{author},
233                                         libsurname => $emailinfo->{libsurname},
234                                         libfirstname => $emailinfo->{libfirstname},
235                                         byfirstname => $emailinfo->{byfirstname},
236                                         bysurname => $emailinfo->{bysurname},
237                                         );
238         my %mail = ( To => $emailinfo->{byemail},
239                                  From => $emailinfo->{libemail},
240                                  Subject => 'Koha suggestion',
241                                  Message => "".$template->output
242                                  );
243 sendmail(%mail);
244 #       warn "sending email to $emailinfo->{byemail} from $emailinfo->{libemail} to notice new status $emailinfo->{status} for $emailinfo->{title} / $emailinfo->{author}";
245 }
246
247 sub findsuggestion_from_biblionumber {
248         my ($dbh,$biblionumber) = @_;
249         my $sth = $dbh->prepare("select suggestionid from suggestions where biblionumber=?");
250         $sth->execute($biblionumber);
251         my ($suggestionid) = $sth->fetchrow;
252         return $suggestionid;
253 }
254
255 =back
256
257 =head1 SEE ALSO
258
259 =cut