New function "GetBookFundsId" : this function get the list of bookfund id into an...
[koha.git] / C4 / Suggestions.pm
1 package C4::Suggestions;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 # $Id$
21
22 use strict;
23 require Exporter;
24 use DBI;
25 use C4::Context;
26 use C4::Output;
27 use Mail::Sendmail;
28 use vars qw($VERSION @ISA @EXPORT);
29
30 # set the version for version checking
31 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
32   shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
33
34 =head1 NAME
35
36 C4::Suggestions - Some useful functions for dealings with suggestions.
37
38 =head1 SYNOPSIS
39
40 use C4::Suggestions;
41
42 =head1 DESCRIPTION
43
44 =over 4
45
46 The functions in this module deal with the suggestions in OPAC and in librarian interface
47
48 A suggestion is done in the OPAC. It has the status "ASKED"
49
50 When a librarian manages the suggestion, he can set the status to "REJECTED" or "ACCEPTED".
51
52 When the book is ordered, the suggestion status becomes "ORDERED"
53
54 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
55
56 All suggestions of a borrower can be seen by the borrower itself.
57 Suggestions done by other borrowers can be seen when not "AVAILABLE"
58
59 =back
60
61 =head1 FUNCTIONS
62
63 =cut
64
65 @ISA = qw(Exporter);
66 @EXPORT = qw(
67     &NewSuggestion
68     &SearchSuggestion
69     &GetSuggestion
70     &DelSuggestion
71     &CountSuggestion
72     &ModStatus
73     &ConnectSuggestionAndBiblio
74     &GetSuggestionFromBiblionumber
75  );
76
77 =head2 SearchSuggestion
78
79 =over 4
80
81 (\@array) = &SearchSuggestion($user,$author,$title,$publishercode,$status,$suggestedbyme)
82
83 searches for a suggestion
84
85 return :
86 C<\@array> : the suggestions found. Array of hash.
87 Note the status is stored twice :
88 * in the status field
89 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
90
91 =back
92
93 =cut
94
95 sub SearchSuggestion  {
96     my ($user,$author,$title,$publishercode,$status,$suggestedbyme)=@_;
97     my $dbh = C4::Context->dbh;
98     my $query = qq|
99     SELECT suggestions.*,
100         U1.surname   AS surnamesuggestedby,
101         U1.firstname AS firstnamesuggestedby,
102         U2.surname   AS surnamemanagedby,
103         U2.firstname AS firstnamemanagedby
104     FROM suggestions
105     LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
106     LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
107     WHERE 1=1 |;
108
109     my @sql_params;
110     if ($author) {
111        push @sql_params,"%".$author."%";
112        $query .= " and author like ?";
113     }
114     if ($title) {
115         push @sql_params,"%".$title."%";
116         $query .= " and suggestions.title like ?";
117     }
118     if ($publishercode) {
119         push @sql_params,"%".$publishercode."%";
120         $query .= " and publishercode like ?";
121     }
122     if ($status) {
123         push @sql_params,$status;
124         $query .= " and status=?";
125     }
126
127     if (C4::Context->preference("IndependantBranches")) {
128         my $userenv = C4::Context->userenv;
129         if ($userenv) {
130             unless ($userenv->{flags} == 1){
131                 push @sql_params,$userenv->{branch};
132                 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
133             }
134         }
135     }
136     if ($suggestedbyme) {
137         unless ($suggestedbyme eq -1) {
138             push @sql_params,$user;
139             $query .= " and suggestedby=?";
140         }
141     } else {
142         $query .= " and managedby is NULL";
143     }
144     my $sth=$dbh->prepare($query);
145     $sth->execute(@sql_params);
146     my @results;
147     my $even=1; # the even variable is used to set even / odd lines, for highlighting
148     while (my $data=$sth->fetchrow_hashref){
149         $data->{$data->{STATUS}} = 1;
150         if ($even) {
151             $even=0;
152             $data->{even}=1;
153         } else {
154             $even=1;
155         }
156         push(@results,$data);
157     }
158     return (\@results);
159 }
160
161 =head2 GetSuggestion
162
163 =over 4
164
165 \%sth = &GetSuggestion($suggestionid)
166
167 this function get the detail of the suggestion $suggestionid (input arg)
168
169 return :
170     the result of the SQL query as a hash : $sth->fetchrow_hashref.
171
172 =back
173
174 =cut
175 sub GetSuggestion {
176     my ($suggestionid) = @_;
177     my $dbh = C4::Context->dbh;
178     my $query = qq|
179         SELECT *
180         FROM   suggestions
181         WHERE  suggestionid=?
182     |;
183     my $sth = $dbh->prepare($query);
184     $sth->execute($suggestionid);
185     return($sth->fetchrow_hashref);
186 }
187
188 =head2 GetSuggestionFromBiblionumber
189
190 =over 4
191
192 $suggestionid = &GetSuggestionFromBiblionumber($dbh,$biblionumber)
193
194 Get a suggestion from it's biblionumber.
195
196 return :
197 the id of the suggestion which is related to the biblionumber given on input args.
198
199 =back
200
201 =cut
202 sub GetSuggestionFromBiblionumber {
203     my ($dbh,$biblionumber) = @_;
204     my $query = qq|
205         SELECT suggestionid
206         FROM   suggestions
207         WHERE  biblionumber=?
208     |;
209     my $sth = $dbh->prepare($query);
210     $sth->execute($biblionumber);
211     my ($suggestionid) = $sth->fetchrow;
212     return $suggestionid;
213 }
214
215
216 =head2 CountSuggestion
217
218 =over 4
219
220 &CountSuggestion($status)
221
222 Count the number of suggestions with the status given on input argument.
223 the arg status can be :
224
225 =over
226
227 =over
228
229 =item * ASKED : asked by the user, not dealed by the librarian
230
231 =item * ACCEPTED : accepted by the librarian, but not yet ordered
232
233 =item * REJECTED : rejected by the librarian (definitive status)
234
235 =item * ORDERED : ordered by the librarian (acquisition module)
236
237 =back
238
239 =back
240
241 return :
242 the number of suggestion with this status.
243
244 =back
245
246 =cut
247 sub CountSuggestion {
248     my ($status) = @_;
249     my $dbh = C4::Context->dbh;
250     my $sth;
251     if (C4::Context->preference("IndependantBranches")){
252         my $userenv = C4::Context->userenv;
253         if ($userenv->{flags} == 1){
254             my $query = qq |
255                 SELECT count(*)
256                 FROM   suggestions
257                 WHERE  status=?
258             |;
259             $sth = $dbh->prepare($query);
260             $sth->execute($status);
261         }
262         else {
263             my $query = qq |
264                 SELECT count(*)
265                 FROM suggestions,borrowers
266                 WHERE status=?
267                 AND borrowers.borrowernumber=suggestions.suggestedby
268                 AND (borrowers.branchcode='' OR borrowers.branchcode =?)
269             |;
270             $sth = $dbh->prepare($query);
271             $sth->execute($status,$userenv->{branch});
272         }
273     }
274     else {
275         my $query = qq |
276             SELECT count(*)
277             FROM suggestions
278             WHERE status=?
279         |;
280          $sth = $dbh->prepare($query);
281         $sth->execute($status);
282     }
283     my ($result) = $sth->fetchrow;
284     return $result;
285 }
286
287 =head2 NewSuggestion
288
289
290 =over 4
291
292 &NewSuggestion($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber)
293
294 Insert a new suggestion on database with value given on input arg.
295
296 =back
297
298 =cut
299 sub NewSuggestion {
300     my ($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber) = @_;
301     my $dbh = C4::Context->dbh;
302     my $query = qq |
303         INSERT INTO suggestions
304             (status,suggestedby,title,author,publishercode,note,copyrightdate,
305             volumedesc,publicationyear,place,isbn,biblionumber)
306         VALUES ('ASKED',?,?,?,?,?,?,?,?,?,?,?)
307     |;
308     my $sth = $dbh->prepare($query);
309     $sth->execute($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber);
310 }
311
312 =head2 ModStatus
313
314 =over 4
315
316 &ModStatus($suggestionid,$status,$managedby,$biblionumber)
317
318 Modify the status (status can be 'ASKED', 'ACCEPTED', 'REJECTED', 'ORDERED')
319 and send a mail to notify the user that did the suggestion.
320
321 Note that there is no function to modify a suggestion : only the status can be modified, thus the name of the function.
322
323 =back
324
325 =cut
326 sub ModStatus {
327     my ($suggestionid,$status,$managedby,$biblionumber) = @_;
328     my $dbh = C4::Context->dbh;
329     my $sth;
330     if ($managedby>0) {
331         if ($biblionumber) {
332         my $query = qq|
333             UPDATE suggestions
334             SET    status=?,managedby=?,biblionumber=?
335             WHERE  suggestionid=?
336         |;
337         $sth = $dbh->prepare($query);
338         $sth->execute($status,$managedby,$biblionumber,$suggestionid);
339         } else {
340             my $query = qq|
341                 UPDATE suggestions
342                 SET    status=?,managedby=?
343                 WHERE  suggestionid=?
344             |;
345             $sth = $dbh->prepare($query);
346             $sth->execute($status,$managedby,$suggestionid);
347         }
348    } else {
349         if ($biblionumber) {
350             my $query = qq|
351                 UPDATE suggestions
352                 SET    status=?,biblionumber=?
353                 WHERE  suggestionid=?
354             |;
355             $sth = $dbh->prepare($query);
356             $sth->execute($status,$biblionumber,$suggestionid);
357         }
358         else {
359             my $query = qq|
360                 UPDATE suggestions
361                 SET    status=?
362                 WHERE  suggestionid=?
363             |;
364             $sth = $dbh->prepare($query);
365             $sth->execute($status,$suggestionid);
366         }
367     }
368     # check mail sending.
369     my $queryMail = qq|
370         SELECT suggestions.*,
371             boby.surname AS bysurname,
372             boby.firstname AS byfirstname,
373             boby.emailaddress AS byemail,
374             lib.surname AS libsurname,
375             lib.firstname AS libfirstname,
376             lib.emailaddress AS libemail
377         FROM suggestions
378             LEFT JOIN borrowers AS boby ON boby.borrowernumber=suggestedby
379             LEFT JOIN borrowers AS lib ON lib.borrowernumber=managedby
380         WHERE suggestionid=?
381     |;
382     $sth = $dbh->prepare($queryMail);
383     $sth->execute($suggestionid);
384     my $emailinfo = $sth->fetchrow_hashref;
385     my $template = gettemplate("suggestion/mail_suggestion_$status.tmpl","intranet");
386
387     $template->param(
388         byemail => $emailinfo->{byemail},
389         libemail => $emailinfo->{libemail},
390         status => $emailinfo->{status},
391         title => $emailinfo->{title},
392         author =>$emailinfo->{author},
393         libsurname => $emailinfo->{libsurname},
394         libfirstname => $emailinfo->{libfirstname},
395         byfirstname => $emailinfo->{byfirstname},
396         bysurname => $emailinfo->{bysurname},
397     );
398     my %mail = (
399         To => $emailinfo->{byemail},
400         From => $emailinfo->{libemail},
401         Subject => 'Koha suggestion',
402         Message => "".$template->output
403     );
404     sendmail(%mail);
405 }
406
407 =head2 ConnectSuggestionAndBiblio
408
409 =over 4
410 &ConnectSuggestionAndBiblio($suggestionid,$biblionumber)
411
412 connect a suggestion to an existing biblio
413
414 =back
415
416 =cut
417 sub ConnectSuggestionAndBiblio {
418     my ($suggestionid,$biblionumber) = @_;
419     my $dbh=C4::Context->dbh;
420     my $query = qq |
421         UPDATE suggestions
422         SET    biblionumber=?
423         WHERE  suggestionid=?
424     |;
425     my $sth = $dbh->prepare($query);
426     $sth->execute($biblionumber,$suggestionid);
427 }
428
429 =head2 DelSuggestion
430
431 =over 4
432
433 &DelSuggestion($borrowernumber,$suggestionid)
434
435 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
436
437 =back
438
439 =cut
440
441 sub DelSuggestion {
442     my ($borrowernumber,$suggestionid) = @_;
443     my $dbh = C4::Context->dbh;
444     # check that the suggestion comes from the suggestor
445     my $query = qq |
446         SELECT suggestedby
447         FROM   suggestions
448         WHERE  suggestionid=?
449     |;
450     my $sth = $dbh->prepare($query);
451     $sth->execute($suggestionid);
452     my ($suggestedby) = $sth->fetchrow;
453     if ($suggestedby eq $borrowernumber) {
454         my $queryDelete = qq|
455             DELETE FROM suggestions
456             WHERE suggestionid=?
457         |;
458         $sth = $dbh->prepare($queryDelete);
459         $sth->execute($suggestionid);
460     }
461 }