Bug 4305 Get Amazon book covers for ISBN13
[koha.git] / C4 / Suggestions.pm
blobb5eb7a4303d0569a9de93d27cf8c09878db5275e
1 package C4::Suggestions;
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use CGI;
25 use C4::Context;
26 use C4::Output;
27 use C4::Dates qw(format_date);
28 use C4::SQLHelper qw(:all);
29 use C4::Debug;
30 use C4::Letters;
31 use List::MoreUtils qw<any>;
32 use base 'Exporter'; # parent would be better there
33 our $VERSION = 3.01;
34 our @EXPORT = qw<
35 &ConnectSuggestionAndBiblio
36 &CountSuggestion
37 &DelSuggestion
38 &GetSuggestion
39 &GetSuggestionByStatus
40 &GetSuggestionFromBiblionumber
41 &ModStatus
42 &ModSuggestion
43 &NewSuggestion
44 &SearchSuggestion
46 use C4::Dates qw(format_date_in_iso);
47 use vars qw($VERSION @ISA @EXPORT);
49 BEGIN {
50 # set the version for version checking
51 $VERSION = 3.01;
52 require Exporter;
53 @ISA = qw(Exporter);
54 @EXPORT = qw(
55 &NewSuggestion
56 &SearchSuggestion
57 &GetSuggestion
58 &GetSuggestionByStatus
59 &DelSuggestion
60 &CountSuggestion
61 &ModSuggestion
62 &ConnectSuggestionAndBiblio
63 &GetSuggestionFromBiblionumber
64 &ConnectSuggestionAndBiblio
65 &DelSuggestion
66 &GetSuggestion
67 &GetSuggestionByStatus
68 &GetSuggestionFromBiblionumber
69 &ModStatus
73 =head1 NAME
75 C4::Suggestions - Some useful functions for dealings with aqorders.
77 =head1 SYNOPSIS
79 use C4::Suggestions;
81 =head1 DESCRIPTION
83 The functions in this module deal with the aqorders in OPAC and in librarian interface
85 A suggestion is done in the OPAC. It has the status "ASKED"
87 When a librarian manages the suggestion, he can set the status to "REJECTED" or "ACCEPTED".
89 When the book is ordered, the suggestion status becomes "ORDERED"
91 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
93 All aqorders of a borrower can be seen by the borrower itself.
94 Suggestions done by other borrowers can be seen when not "AVAILABLE"
96 =head1 FUNCTIONS
98 =head2 SearchSuggestion
100 (\@array) = &SearchSuggestion($suggestionhashref_to_search)
102 searches for a suggestion
104 return :
105 C<\@array> : the aqorders found. Array of hash.
106 Note the status is stored twice :
107 * in the status field
108 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
110 =cut
112 sub SearchSuggestion {
113 my ($suggestion)=@_;
114 my $dbh = C4::Context->dbh;
115 my @sql_params;
116 my @query = (
117 q{ SELECT suggestions.*,
118 U1.branchcode AS branchcodesuggestedby,
119 B1.branchname AS branchnamesuggestedby,
120 U1.surname AS surnamesuggestedby,
121 U1.firstname AS firstnamesuggestedby,
122 U1.email AS emailsuggestedby,
123 U1.borrowernumber AS borrnumsuggestedby,
124 U1.categorycode AS categorycodesuggestedby,
125 C1.description AS categorydescriptionsuggestedby,
126 U2.surname AS surnamemanagedby,
127 U2.firstname AS firstnamemanagedby,
128 B2.branchname AS branchnamesuggestedby,
129 U2.email AS emailmanagedby,
130 U2.branchcode AS branchcodemanagedby,
131 U2.borrowernumber AS borrnummanagedby
132 FROM suggestions
133 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
134 LEFT JOIN branches AS B1 ON B1.branchcode=U1.branchcode
135 LEFT JOIN categories AS C1 ON C1.categorycode = U1.categorycode
136 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
137 LEFT JOIN branches AS B2 ON B2.branchcode=U2.branchcode
138 LEFT JOIN categories AS C2 ON C2.categorycode = U2.categorycode
139 WHERE STATUS NOT IN ('CLAIMED')
140 } , map {
141 if ( my $s = $suggestion->{$_} ) {
142 push @sql_params,'%'.$s.'%';
143 " and suggestions.$_ like ? ";
144 } else { () }
145 } qw( title author isbn publishercode collectiontitle )
148 my $userenv = C4::Context->userenv;
149 if (C4::Context->preference('IndependantBranches')) {
150 if ($userenv) {
151 if (($userenv->{flags} % 2) != 1 && !$suggestion->{branchcode}){
152 push @sql_params,$$userenv{branch};
153 push @query,q{ and (branchcode = ? or branchcode ='')};
158 foreach my $field (grep { my $fieldname=$_;
159 any {$fieldname eq $_ } qw<
160 STATUS branchcode itemtype suggestedby managedby acceptedby
161 bookfundid biblionumber
162 >} keys %$suggestion
164 if ($$suggestion{$field}){
165 push @sql_params,$suggestion->{$field};
166 push @query, " and suggestions.$field=?";
168 else {
169 push @query, " and (suggestions.$field='' OR suggestions.$field IS NULL)";
173 $debug && warn "@query";
174 my $sth=$dbh->prepare("@query");
175 $sth->execute(@sql_params);
176 my @results;
177 while ( my $data=$sth->fetchrow_hashref ){
178 $$data{$$data{STATUS}} = 1;
179 push(@results,$data);
181 return (\@results);
184 =head2 GetSuggestion
186 \%sth = &GetSuggestion($ordernumber)
188 this function get the detail of the suggestion $ordernumber (input arg)
190 return :
191 the result of the SQL query as a hash : $sth->fetchrow_hashref.
193 =cut
195 sub GetSuggestion {
196 my ($ordernumber) = @_;
197 my $dbh = C4::Context->dbh;
198 my $query = "
199 SELECT *
200 FROM suggestions
201 WHERE suggestionid=?
203 my $sth = $dbh->prepare($query);
204 $sth->execute($ordernumber);
205 return($sth->fetchrow_hashref);
208 =head2 GetSuggestionFromBiblionumber
210 $ordernumber = &GetSuggestionFromBiblionumber($biblionumber)
212 Get a suggestion from it's biblionumber.
214 return :
215 the id of the suggestion which is related to the biblionumber given on input args.
217 =cut
219 sub GetSuggestionFromBiblionumber {
220 my ($biblionumber) = @_;
221 my $query = q{
222 SELECT suggestionid
223 FROM suggestions
224 WHERE biblionumber=?
226 my $dbh=C4::Context->dbh;
227 my $sth = $dbh->prepare($query);
228 $sth->execute($biblionumber);
229 my ($ordernumber) = $sth->fetchrow;
230 return $ordernumber;
233 =head2 GetSuggestionByStatus
235 $aqorders = &GetSuggestionByStatus($status,[$branchcode])
237 Get a suggestion from it's status
239 return :
240 all the suggestion with C<$status>
242 =cut
244 sub GetSuggestionByStatus {
245 my $status = shift;
246 my $branchcode = shift;
247 my $dbh = C4::Context->dbh;
248 my @sql_params=($status);
249 my $query = qq(SELECT suggestions.*,
250 U1.surname AS surnamesuggestedby,
251 U1.firstname AS firstnamesuggestedby,
252 U1.branchcode AS branchcodesuggestedby,
253 B1.branchname AS branchnamesuggestedby,
254 U1.borrowernumber AS borrnumsuggestedby,
255 U1.categorycode AS categorycodesuggestedby,
256 C1.description AS categorydescriptionsuggestedby,
257 U2.surname AS surnamemanagedby,
258 U2.firstname AS firstnamemanagedby,
259 U2.borrowernumber AS borrnummanagedby
260 FROM suggestions
261 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
262 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
263 LEFT JOIN categories AS C1 ON C1.categorycode=U1.categorycode
264 LEFT JOIN branches AS B1 on B1.branchcode = U1.branchcode
265 WHERE status = ?);
266 if (C4::Context->preference("IndependantBranches") || $branchcode) {
267 my $userenv = C4::Context->userenv;
268 if ($userenv) {
269 unless ($userenv->{flags} % 2 == 1){
270 push @sql_params,$userenv->{branch};
271 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
274 if ($branchcode) {
275 push @sql_params,$branchcode;
276 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
280 my $sth = $dbh->prepare($query);
281 $sth->execute(@sql_params);
283 my $results;
284 $results= $sth->fetchall_arrayref({});
285 return $results;
288 =head2 CountSuggestion
290 &CountSuggestion($status)
292 Count the number of aqorders with the status given on input argument.
293 the arg status can be :
295 =over 2
297 =item * ASKED : asked by the user, not dealed by the librarian
299 =item * ACCEPTED : accepted by the librarian, but not yet ordered
301 =item * REJECTED : rejected by the librarian (definitive status)
303 =item * ORDERED : ordered by the librarian (acquisition module)
305 =back
307 return :
308 the number of suggestion with this status.
310 =cut
312 sub CountSuggestion {
313 my ($status) = @_;
314 my $dbh = C4::Context->dbh;
315 my $sth;
316 if (C4::Context->preference("IndependantBranches")){
317 my $userenv = C4::Context->userenv;
318 if ($userenv->{flags} % 2 == 1){
319 my $query = qq |
320 SELECT count(*)
321 FROM suggestions
322 WHERE STATUS=?
324 $sth = $dbh->prepare($query);
325 $sth->execute($status);
327 else {
328 my $query = qq |
329 SELECT count(*)
330 FROM suggestions LEFT JOIN borrowers ON borrowers.borrowernumber=suggestions.suggestedby
331 WHERE STATUS=?
332 AND (borrowers.branchcode='' OR borrowers.branchcode =?)
334 $sth = $dbh->prepare($query);
335 $sth->execute($status,$userenv->{branch});
338 else {
339 my $query = qq |
340 SELECT count(*)
341 FROM suggestions
342 WHERE STATUS=?
344 $sth = $dbh->prepare($query);
345 $sth->execute($status);
347 my ($result) = $sth->fetchrow;
348 return $result;
351 =head2 NewSuggestion
354 &NewSuggestion($suggestion);
356 Insert a new suggestion on database with value given on input arg.
358 =cut
360 sub NewSuggestion {
361 my ($suggestion) = @_;
362 $suggestion->{STATUS}="ASKED" unless $suggestion->{STATUS};
363 return InsertInTable("suggestions",$suggestion);
366 =head2 ModSuggestion
368 &ModSuggestion($suggestion)
370 Modify the suggestion according to the hash passed by ref.
371 The hash HAS to contain suggestionid
372 Data not defined is not updated unless it is a note or sort1
373 Send a mail to notify the user that did the suggestion.
375 Note that there is no function to modify a suggestion.
377 =cut
379 sub ModSuggestion {
380 my ($suggestion)=@_;
381 my $status_update_table=UpdateInTable("suggestions", $suggestion);
382 # check mail sending.
383 if ($$suggestion{STATUS}){
384 my $letter=C4::Letters::getletter('suggestions',$suggestion->{STATUS});
385 if ($letter){
386 my $enqueued = C4::Letters::EnqueueLetter({
387 letter=>$letter,
388 borrowernumber=>$suggestion->{suggestedby},
389 suggestionid=>$suggestion->{suggestionid},
390 LibraryName => C4::Context->preference("LibraryName"),
391 msg_transport_type=>'email'
393 if (!$enqueued){warn "can't enqueue letter $letter";}
396 return $status_update_table;
399 =head2 ConnectSuggestionAndBiblio
401 &ConnectSuggestionAndBiblio($ordernumber,$biblionumber)
403 connect a suggestion to an existing biblio
405 =cut
407 sub ConnectSuggestionAndBiblio {
408 my ($suggestionid,$biblionumber) = @_;
409 my $dbh=C4::Context->dbh;
410 my $query = "
411 UPDATE suggestions
412 SET biblionumber=?
413 WHERE suggestionid=?
415 my $sth = $dbh->prepare($query);
416 $sth->execute($biblionumber,$suggestionid);
419 =head2 DelSuggestion
421 &DelSuggestion($borrowernumber,$ordernumber)
423 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
425 =cut
427 sub DelSuggestion {
428 my ($borrowernumber,$suggestionid,$type) = @_;
429 my $dbh = C4::Context->dbh;
430 # check that the suggestion comes from the suggestor
431 my $query = "
432 SELECT suggestedby
433 FROM suggestions
434 WHERE suggestionid=?
436 my $sth = $dbh->prepare($query);
437 $sth->execute($suggestionid);
438 my ($suggestedby) = $sth->fetchrow;
439 if ($type eq "intranet" || $suggestedby eq $borrowernumber ) {
440 my $queryDelete = "
441 DELETE FROM suggestions
442 WHERE suggestionid=?
444 $sth = $dbh->prepare($queryDelete);
445 my $suggestiondeleted=$sth->execute($suggestionid);
446 return $suggestiondeleted;
451 __END__
454 =head1 AUTHOR
456 Koha Development Team <http://koha-community.org/>
458 =cut