Some bug fixing, new acquisitions handling
[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 C4::Context;
25 use C4::Output;
26 use Mail::Sendmail;
27 use vars qw($VERSION @ISA @EXPORT);
28
29 # set the version for version checking
30 $VERSION = do { my @v = '$Revision$' =~ /\d+/g;
31   shift(@v) . "." . join("_", map {sprintf "%03d", $_ } @v); };
32
33 =head1 NAME
34
35 C4::Suggestions - Some useful functions for dealings with suggestions.
36
37 =head1 SYNOPSIS
38
39 use C4::Suggestions;
40
41 =head1 DESCRIPTION
42
43 =over 4
44
45 The functions in this module deal with the suggestions in OPAC and in librarian interface
46
47 A suggestion is done in the OPAC. It has the status "ASKED"
48
49 When a librarian manages the suggestion, he can set the status to "REJECTED" or "ACCEPTED".
50
51 When the book is ordered, the suggestion status becomes "ORDERED"
52
53 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
54
55 All suggestions of a borrower can be seen by the borrower itself.
56 Suggestions done by other borrowers can be seen when not "AVAILABLE"
57
58 =back
59
60 =head1 FUNCTIONS
61
62 =cut
63
64 @ISA = qw(Exporter);
65 @EXPORT = qw(
66     &NewSuggestion
67     &SearchSuggestion
68     &GetSuggestion
69     &DelSuggestion
70     &CountSuggestion
71     &ModStatus
72     &ConnectSuggestionAndBiblio
73     &GetSuggestionFromBiblionumber
74  );
75
76 =head2 SearchSuggestion
77
78 =over 4
79
80 (\@array) = &SearchSuggestion($user,$author,$title,$publishercode,$status,$suggestedbyme)
81
82 searches for a suggestion
83
84 return :
85 C<\@array> : the suggestions found. Array of hash.
86 Note the status is stored twice :
87 * in the status field
88 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
89
90 =back
91
92 =cut
93
94 sub SearchSuggestion  {
95     my ($user,$author,$title,$publishercode,$status,$suggestedbyme)=@_;
96     my $dbh = C4::Context->dbh;
97     my $query = qq|
98     SELECT suggestions.*,
99         U1.surname   AS surnamesuggestedby,
100         U1.firstname AS firstnamesuggestedby,
101         U2.surname   AS surnamemanagedby,
102         U2.firstname AS firstnamemanagedby
103     FROM suggestions
104     LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
105     LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
106     WHERE 1=1 |;
107
108     my @sql_params;
109     if ($author) {
110        push @sql_params,"%".$author."%";
111        $query .= " and author like ?";
112     }
113     if ($title) {
114         push @sql_params,"%".$title."%";
115         $query .= " and suggestions.title like ?";
116     }
117     if ($publishercode) {
118         push @sql_params,"%".$publishercode."%";
119         $query .= " and publishercode like ?";
120     }
121     if ($status) {
122         push @sql_params,$status;
123         $query .= " and status=?";
124     }
125
126     if (C4::Context->preference("IndependantBranches")) {
127         my $userenv = C4::Context->userenv;
128         if ($userenv) {
129             unless ($userenv->{flags} == 1){
130                 push @sql_params,$userenv->{branch};
131                 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
132             }
133         }
134     }
135     if ($suggestedbyme) {
136         unless ($suggestedbyme eq -1) {
137             push @sql_params,$user;
138             $query .= " and suggestedby=?";
139         }
140     } else {
141         $query .= " and managedby is NULL";
142     }
143     my $sth=$dbh->prepare($query);
144     $sth->execute(@sql_params);
145     my @results;
146     my $even=1; # the even variable is used to set even / odd lines, for highlighting
147     while (my $data=$sth->fetchrow_hashref){
148         $data->{$data->{STATUS}} = 1;
149         if ($even) {
150             $even=0;
151             $data->{even}=1;
152         } else {
153             $even=1;
154         }
155         push(@results,$data);
156     }
157     return (\@results);
158 }
159
160 =head2 GetSuggestion
161
162 =over 4
163
164 \%sth = &GetSuggestion($suggestionid)
165
166 this function get the detail of the suggestion $suggestionid (input arg)
167
168 return :
169     the result of the SQL query as a hash : $sth->fetchrow_hashref.
170
171 =back
172
173 =cut
174 sub GetSuggestion {
175     my ($suggestionid) = @_;
176     my $dbh = C4::Context->dbh;
177     my $query = qq|
178         SELECT *
179         FROM   suggestions
180         WHERE  suggestionid=?
181     |;
182     my $sth = $dbh->prepare($query);
183     $sth->execute($suggestionid);
184     return($sth->fetchrow_hashref);
185 }
186
187 =head2 GetSuggestionFromBiblionumber
188
189 =over 4
190
191 $suggestionid = &GetSuggestionFromBiblionumber($dbh,$biblionumber)
192
193 Get a suggestion from it's biblionumber.
194
195 return :
196 the id of the suggestion which is related to the biblionumber given on input args.
197
198 =back
199
200 =cut
201 sub GetSuggestionFromBiblionumber {
202     my ($dbh,$biblionumber) = @_;
203     my $query = qq|
204         SELECT suggestionid
205         FROM   suggestions
206         WHERE  biblionumber=?
207     |;
208     my $sth = $dbh->prepare($query);
209     $sth->execute($biblionumber);
210     my ($suggestionid) = $sth->fetchrow;
211     return $suggestionid;
212 }
213
214
215 =head2 CountSuggestion
216
217 =over 4
218
219 &CountSuggestion($status)
220
221 Count the number of suggestions with the status given on input argument.
222 the arg status can be :
223
224 =over
225
226 =over
227
228 =item * ASKED : asked by the user, not dealed by the librarian
229
230 =item * ACCEPTED : accepted by the librarian, but not yet ordered
231
232 =item * REJECTED : rejected by the librarian (definitive status)
233
234 =item * ORDERED : ordered by the librarian (acquisition module)
235
236 =back
237
238 =back
239
240 return :
241 the number of suggestion with this status.
242
243 =back
244
245 =cut
246 sub CountSuggestion {
247     my ($status) = @_;
248     my $dbh = C4::Context->dbh;
249     my $sth;
250     if (C4::Context->preference("IndependantBranches")){
251         my $userenv = C4::Context->userenv;
252         if ($userenv->{flags} == 1){
253             my $query = qq |
254                 SELECT count(*)
255                 FROM   suggestions
256                 WHERE  status=?
257             |;
258             $sth = $dbh->prepare($query);
259             $sth->execute($status);
260         }
261         else {
262             my $query = qq |
263                 SELECT count(*)
264                 FROM suggestions,borrowers
265                 WHERE status=?
266                 AND borrowers.borrowernumber=suggestions.suggestedby
267                 AND (borrowers.branchcode='' OR borrowers.branchcode =?)
268             |;
269             $sth = $dbh->prepare($query);
270             $sth->execute($status,$userenv->{branch});
271         }
272     }
273     else {
274         my $query = qq |
275             SELECT count(*)
276             FROM suggestions
277             WHERE status=?
278         |;
279          $sth = $dbh->prepare($query);
280         $sth->execute($status);
281     }
282     my ($result) = $sth->fetchrow;
283     return $result;
284 }
285
286 =head2 NewSuggestion
287
288
289 =over 4
290
291 &NewSuggestion($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber)
292
293 Insert a new suggestion on database with value given on input arg.
294
295 =back
296
297 =cut
298 sub NewSuggestion {
299     my ($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber) = @_;
300     my $dbh = C4::Context->dbh;
301
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,$input) = @_;
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 if ($emailinfo->{byemail}){
386     my $template = gettemplate("suggestion/mail_suggestion_$status.tmpl","intranet",$input);
387
388     $template->param(
389         byemail => $emailinfo->{byemail},
390         libemail => $emailinfo->{libemail},
391         status => $emailinfo->{status},
392         title => $emailinfo->{title},
393         author =>$emailinfo->{author},
394         libsurname => $emailinfo->{libsurname},
395         libfirstname => $emailinfo->{libfirstname},
396         byfirstname => $emailinfo->{byfirstname},
397         bysurname => $emailinfo->{bysurname},
398     );
399     my %mail = (
400         To => $emailinfo->{byemail},
401         From => $emailinfo->{libemail},
402         Subject => 'Koha suggestion',
403         Message => "".$template->output
404     );
405     sendmail(%mail);
406 }
407 }
408
409 =head2 ConnectSuggestionAndBiblio
410
411 =over 4
412 &ConnectSuggestionAndBiblio($suggestionid,$biblionumber)
413
414 connect a suggestion to an existing biblio
415
416 =back
417
418 =cut
419 sub ConnectSuggestionAndBiblio {
420     my ($suggestionid,$biblionumber) = @_;
421     my $dbh=C4::Context->dbh;
422     my $query = qq |
423         UPDATE suggestions
424         SET    biblionumber=?
425         WHERE  suggestionid=?
426     |;
427     my $sth = $dbh->prepare($query);
428     $sth->execute($biblionumber,$suggestionid);
429 }
430
431 =head2 DelSuggestion
432
433 =over 4
434
435 &DelSuggestion($borrowernumber,$suggestionid)
436
437 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
438
439 =back
440
441 =cut
442
443 sub DelSuggestion {
444     my ($borrowernumber,$suggestionid) = @_;
445     my $dbh = C4::Context->dbh;
446     # check that the suggestion comes from the suggestor
447     my $query = qq |
448         SELECT suggestedby
449         FROM   suggestions
450         WHERE  suggestionid=?
451     |;
452     my $sth = $dbh->prepare($query);
453     $sth->execute($suggestionid);
454     my ($suggestedby) = $sth->fetchrow;
455     if ($suggestedby eq $borrowernumber) {
456         my $queryDelete = qq|
457             DELETE FROM suggestions
458             WHERE suggestionid=?
459         |;
460         $sth = $dbh->prepare($queryDelete);
461         $sth->execute($suggestionid);
462     }
463 }