Editing C4::Suggestions.pm
[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
21 use strict;
22 use CGI;
23
24 use C4::Context;
25 use C4::Output;
26 use C4::Dates qw(format_date);
27 use C4::SQLHelper qw(:all);
28 use C4::Debug;
29 use C4::Letters;
30 use List::MoreUtils qw(any);
31 use vars qw($VERSION @ISA @EXPORT);
32
33 BEGIN {
34         # set the version for version checking
35         $VERSION = 3.01;
36         require Exporter;
37         @ISA = qw(Exporter);
38         @EXPORT = qw(
39                 &NewSuggestion
40                 &SearchSuggestion
41                 &GetSuggestion
42                 &GetSuggestionByStatus
43                 &DelSuggestion
44                 &CountSuggestion
45                 &ModSuggestion
46                 &ConnectSuggestionAndBiblio
47                 &GetSuggestionFromBiblionumber
48         );
49 }
50
51 =head1 NAME
52
53 C4::Suggestions - Some useful functions for dealings with suggestions.
54
55 =head1 SYNOPSIS
56
57 use C4::Suggestions;
58
59 =head1 DESCRIPTION
60
61 The functions in this module deal with the suggestions in OPAC and in librarian interface
62
63 A suggestion is done in the OPAC. It has the status "ASKED"
64
65 When a librarian manages the suggestion, he can set the status to "REJECTED" or "ACCEPTED".
66
67 When the book is ordered, the suggestion status becomes "ORDERED"
68
69 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
70
71 All suggestions of a borrower can be seen by the borrower itself.
72 Suggestions done by other borrowers can be seen when not "AVAILABLE"
73
74 =head1 FUNCTIONS
75
76 =head2 SearchSuggestion
77
78 (\@array) = &SearchSuggestion($suggestionhashref_to_search)
79
80 searches for a suggestion
81
82 return :
83 C<\@array> : the suggestions found. Array of hash.
84 Note the status is stored twice :
85 * in the status field
86 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
87
88 =cut
89
90 sub SearchSuggestion  {
91     my ($suggestion)=@_;
92     my $dbh = C4::Context->dbh;
93         my @sql_params;
94     my @query =(q{ 
95     SELECT suggestions.*,
96         U1.branchcode   AS branchcodesuggestedby,
97         B1.branchname AS branchnamesuggestedby,
98         U1.surname   AS surnamesuggestedby,
99         U1.firstname AS firstnamesuggestedby,
100         U1.borrowernumber AS borrnumsuggestedby,
101         U1.categorycode AS categorycodesuggestedby,
102         C1.description AS categorydescriptionsuggestedby,
103         U2.branchcode AS branchcodemanagedby,
104         B2.branchname AS branchnamemanagedby,
105         U2.surname   AS surnamemanagedby,
106         U2.firstname AS firstnamemanagedby,
107         U2.borrowernumber AS borrnummanagedby
108     FROM suggestions
109     LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
110     LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
111     LEFT JOIN categories AS C1 ON C1.categorycode = U1.categorycode
112     LEFT JOIN branches AS B1 ON B1.branchcode = U1.branchcode
113     LEFT JOIN branches AS B2 ON B2.branchcode = U2.branchcode
114         WHERE status NOT IN ('CLAIMED')
115         } , map {
116             if ( my $s = $$suggestion{$_} ) {
117                 push @sql_params,'%'.$s.'%'; 
118                 " and suggestions.$_ like ? ";
119             } else { () }
120         } qw( title author isbn publishercode collectiontitle )
121         );
122
123         my $userenv = C4::Context->userenv;
124     if (C4::Context->preference('IndependantBranches')) {
125                         if ($userenv) {
126                                 if (($userenv->{flags} % 2) != 1 && !$$suggestion{branchcode}){
127                                    push @sql_params,$$userenv{branch};
128                                    push @query,q{ and (branchcode = ? or branchcode ='')};
129                                 }
130                         }
131     }
132
133     foreach my $field (grep { my $fieldname=$_;
134                 any {$fieldname eq $_ } qw<
135         status branchcode itemtype suggestedby managedby acceptedby
136         bookfundid biblionumber
137         >} keys %$suggestion
138     ) {
139                 if ($$suggestion{$field}){
140                         push @sql_params,$$suggestion{$field};
141                         push @query, " and suggestions.$field=?";
142                 } 
143                 else {
144                         push @query, " and (suggestions.$field='' OR suggestions.$field IS NULL)";
145                 }
146     }
147
148         $debug && warn "@query";
149     my $sth=$dbh->prepare("@query");
150     $sth->execute(@sql_params);
151         return ($sth->fetchall_arrayref({}));
152 }
153
154 =head2 GetSuggestion
155
156 \%sth = &GetSuggestion($suggestionid)
157
158 this function get the detail of the suggestion $suggestionid (input arg)
159
160 return :
161     the result of the SQL query as a hash : $sth->fetchrow_hashref.
162
163 =cut
164
165 sub GetSuggestion {
166     my ($suggestionid) = @_;
167     my $dbh = C4::Context->dbh;
168     my $query = "
169         SELECT *
170         FROM   suggestions
171         WHERE  suggestionid=?
172     ";
173     my $sth = $dbh->prepare($query);
174     $sth->execute($suggestionid);
175     return($sth->fetchrow_hashref);
176 }
177
178 =head2 GetSuggestionFromBiblionumber
179
180 $suggestionid = &GetSuggestionFromBiblionumber($dbh,$biblionumber)
181
182 Get a suggestion from it's biblionumber.
183
184 return :
185 the id of the suggestion which is related to the biblionumber given on input args.
186
187 =cut
188
189 sub GetSuggestionFromBiblionumber {
190     my ($dbh,$biblionumber) = @_;
191     my $query = qq|
192         SELECT suggestionid
193         FROM   suggestions
194         WHERE  biblionumber=?
195     |;
196     my $sth = $dbh->prepare($query);
197     $sth->execute($biblionumber);
198     my ($suggestionid) = $sth->fetchrow;
199     return $suggestionid;
200 }
201
202 =head2 GetSuggestionByStatus
203
204 $suggestions = &GetSuggestionByStatus($status,[$branchcode])
205
206 Get a suggestion from it's status
207
208 return :
209 all the suggestion with C<$status>
210
211 =cut
212
213 sub GetSuggestionByStatus {
214     my $status = shift;
215     my $branchcode = shift;
216     my $dbh = C4::Context->dbh;
217     my @sql_params=($status);  
218     my $query = qq(SELECT suggestions.*,
219                         U1.surname   AS surnamesuggestedby,
220                         U1.firstname AS firstnamesuggestedby,
221                         U1.branchcode AS branchcodesuggestedby,
222                         B1.branchname AS branchnamesuggestedby,
223                                                 U1.borrowernumber AS borrnumsuggestedby,
224                                                 U1.categorycode AS categorycodesuggestedby,
225                         C1.description AS categorydescriptionsuggestedby,
226                         U2.surname   AS surnamemanagedby,
227                         U2.firstname AS firstnamemanagedby,
228                         U2.borrowernumber AS borrnummanagedby
229                         FROM suggestions
230                         LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
231                         LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
232                         LEFT JOIN categories AS C1 ON C1.categorycode=U1.categorycode
233                         LEFT JOIN branches AS B1 on B1.branchcode = U1.branchcode
234                         WHERE status = ?);
235     if (C4::Context->preference("IndependantBranches") || $branchcode) {
236         my $userenv = C4::Context->userenv;
237         if ($userenv) {
238             unless ($userenv->{flags} % 2 == 1){
239                 push @sql_params,$userenv->{branch};
240                 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
241             }
242         }
243         if ($branchcode) {
244             push @sql_params,$branchcode;
245             $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
246         }
247     }
248     
249     my $sth = $dbh->prepare($query);
250     $sth->execute(@sql_params);
251     
252     my $results;
253     $results=  $sth->fetchall_arrayref({});
254     return $results;
255 }
256
257 =head2 CountSuggestion
258
259 &CountSuggestion($status)
260
261 Count the number of suggestions with the status given on input argument.
262 the arg status can be :
263
264 =over 2
265
266 =item * ASKED : asked by the user, not dealed by the librarian
267
268 =item * ACCEPTED : accepted by the librarian, but not yet ordered
269
270 =item * REJECTED : rejected by the librarian (definitive status)
271
272 =item * ORDERED : ordered by the librarian (acquisition module)
273
274 =back
275
276 return :
277 the number of suggestion with this status.
278
279 =cut
280
281 sub CountSuggestion {
282     my ($status) = @_;
283     my $dbh = C4::Context->dbh;
284     my $sth;
285     if (C4::Context->preference("IndependantBranches")){
286         my $userenv = C4::Context->userenv;
287         if ($userenv->{flags} % 2 == 1){
288             my $query = qq |
289                 SELECT count(*)
290                 FROM   suggestions
291                 WHERE  status=?
292             |;
293             $sth = $dbh->prepare($query);
294             $sth->execute($status);
295         }
296         else {
297             my $query = qq |
298                 SELECT count(*)
299                 FROM suggestions LEFT JOIN borrowers ON borrowers.borrowernumber=suggestions.suggestedby
300                 WHERE status=?
301                 AND (borrowers.branchcode='' OR borrowers.branchcode =?)
302             |;
303             $sth = $dbh->prepare($query);
304             $sth->execute($status,$userenv->{branch});
305         }
306     }
307     else {
308         my $query = qq |
309             SELECT count(*)
310             FROM suggestions
311             WHERE status=?
312         |;
313          $sth = $dbh->prepare($query);
314         $sth->execute($status);
315     }
316     my ($result) = $sth->fetchrow;
317     return $result;
318 }
319
320 =head2 NewSuggestion
321
322
323 &NewSuggestion($suggestion)
324
325 Insert a new suggestion on database with value given on input arg.
326
327 =cut
328
329 sub NewSuggestion {
330     my ($suggestion) = @_;
331         return InsertInTable("suggestions",$suggestion); 
332 }
333
334 =head2 ModSuggestion
335
336 &ModSuggestion($suggestion)
337
338 Modify the suggestion according to the hash passed by ref.
339 The hash HAS to contain suggestionid
340 Data not defined is not updated unless it is a note or sort1 
341 Send a mail to notify the user that did the suggestion.
342
343 Note that there is no function to modify a suggestion. 
344
345 =cut
346
347 sub ModSuggestion {
348     my ($suggestion)=@_;
349         my $status_update_table=UpdateInTable("suggestions", $suggestion);
350     # check mail sending.
351     if ($$suggestion{STATUS}){
352         my $letter=C4::Letters::getletter('suggestions',$$suggestion{STATUS});
353         if ($letter){
354         my $enqueued = C4::Letters::EnqueueLetter({
355             letter=>$letter,
356             borrowernumber=>$$suggestion{suggestedby},
357             suggestionid=>$$suggestion{suggestionid},
358             msg_transport_type=>'email'
359             });
360         if (!$enqueued){warn "can't enqueue letter $letter";}
361         }
362     }
363         return $status_update_table;
364 }
365
366 =head2 ConnectSuggestionAndBiblio
367
368 &ConnectSuggestionAndBiblio($suggestionid,$biblionumber)
369
370 connect a suggestion to an existing biblio
371
372 =cut
373
374 sub ConnectSuggestionAndBiblio {
375     my ($suggestionid,$biblionumber) = @_;
376     my $dbh=C4::Context->dbh;
377     my $query = "
378         UPDATE suggestions
379         SET    biblionumber=?
380         WHERE  suggestionid=?
381     ";
382     my $sth = $dbh->prepare($query);
383     $sth->execute($biblionumber,$suggestionid);
384 }
385
386 =head2 DelSuggestion
387
388 &DelSuggestion($borrowernumber,$suggestionid)
389
390 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
391
392 =cut
393
394 sub DelSuggestion {
395     my ($borrowernumber,$suggestionid,$type) = @_;
396     my $dbh = C4::Context->dbh;
397     # check that the suggestion comes from the suggestor
398     my $query = "
399         SELECT suggestedby
400         FROM   suggestions
401         WHERE  suggestionid=?
402     ";
403     my $sth = $dbh->prepare($query);
404     $sth->execute($suggestionid);
405     my ($suggestedby) = $sth->fetchrow;
406     if ($type eq "intranet" || $suggestedby eq $borrowernumber ) {
407         my $queryDelete = "
408             DELETE FROM suggestions
409             WHERE suggestionid=?
410         ";
411         $sth = $dbh->prepare($queryDelete);
412         my $suggestiondeleted=$sth->execute($suggestionid);
413         return $suggestiondeleted;  
414     }
415 }
416
417 1;
418 __END__
419
420
421 =head1 AUTHOR
422
423 Koha Developement team <info@koha.org>
424
425 =cut
426