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
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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
26 use C4::Dates qw(format_date);
27 use C4::SQLHelper qw(:all);
30 use List::MoreUtils qw(any);
31 use base 'Exporter'; # parent would be better there
34 &ConnectSuggestionAndBiblio
38 &GetSuggestionByStatus
39 &GetSuggestionFromBiblionumber
45 use C4::Dates qw(format_date_in_iso);
46 >>>>>>> Suggestions.pm, probably useless & not working (check with hdl):C4/Suggestions.pm
47 >>>>>>> Suggestions.pm, probably useless & not working (check with hdl):C4/Suggestions.pm
48 use vars qw($VERSION @ISA @EXPORT);
51 # set the version for version checking
59 &GetSuggestionByStatus
63 &ConnectSuggestionAndBiblio
64 &GetSuggestionFromBiblionumber
70 C4::Suggestions - Some useful functions for dealings with aqorders.
78 The functions in this module deal with the aqorders in OPAC and in librarian interface
80 A suggestion is done in the OPAC. It has the status "ASKED"
82 When a librarian manages the suggestion, he can set the status to "REJECTED" or "ACCEPTED".
84 When the book is ordered, the suggestion status becomes "ORDERED"
86 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
88 All aqorders of a borrower can be seen by the borrower itself.
89 Suggestions done by other borrowers can be seen when not "AVAILABLE"
93 =head2 SearchSuggestion
95 (\@array) = &SearchSuggestion($suggestionhashref_to_search)
97 searches for a suggestion
100 C<\@array> : the aqorders found. Array of hash.
101 Note the status is stored twice :
102 * in the status field
103 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
107 sub SearchSuggestion {
109 my $dbh = C4::Context->dbh;
112 SELECT suggestions.*,
113 U1.branchcode AS branchcodesuggestedby,
114 B1.branchname AS branchnamesuggestedby,
115 U1.surname AS surnamesuggestedby,
116 U1.firstname AS firstnamesuggestedby,
117 U1.borrowernumber AS borrnumsuggestedby,
118 U1.categorycode AS categorycodesuggestedby,
119 C1.description AS categorydescriptionsuggestedby,
120 U2.branchcode AS branchcodemanagedby,
121 B2.branchname AS branchnamemanagedby,
122 U2.surname AS surnamemanagedby,
123 U2.firstname AS firstnamemanagedby,
124 U2.borrowernumber AS borrnummanagedby
126 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
127 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
128 LEFT JOIN categories AS C1 ON C1.categorycode = U1.categorycode
129 LEFT JOIN branches AS B1 ON B1.branchcode = U1.branchcode
130 LEFT JOIN branches AS B2 ON B2.branchcode = U2.branchcode
131 WHERE STATUS NOT IN ('CLAIMED')
133 if ( my $s = $$suggestion{$_} ) {
134 push @sql_params,'%'.$s.'%';
135 " and suggestions.$_ like ? ";
137 } qw( title author isbn publishercode collectiontitle )
140 my $userenv = C4::Context->userenv;
141 if (C4::Context->preference('IndependantBranches')) {
143 if (($userenv->{flags} % 2) != 1 && !$$suggestion{branchcode}){
144 push @sql_params,$$userenv{branch};
145 push @query,q{ and (branchcode = ? or branchcode ='')};
150 foreach my $field (grep { my $fieldname=$_;
151 any {$fieldname eq $_ } qw<
152 STATUS branchcode itemtype suggestedby managedby acceptedby
153 bookfundid biblionumber
156 if ($$suggestion{$field}){
157 push @sql_params,$$suggestion{$field};
158 push @query, " and suggestions.$field=?";
161 push @query, " and (suggestions.$field='' OR suggestions.$field IS NULL)";
165 $debug && warn "@query";
166 my $sth=$dbh->prepare("@query");
167 $sth->execute(@sql_params);
168 return ($sth->fetchall_arrayref({}));
173 \%sth = &GetSuggestion($ordernumber)
175 this function get the detail of the suggestion $ordernumber (input arg)
178 the result of the SQL query as a hash : $sth->fetchrow_hashref.
183 my ($ordernumber) = @_;
184 my $dbh = C4::Context->dbh;
190 my $sth = $dbh->prepare($query);
191 $sth->execute($ordernumber);
192 return($sth->fetchrow_hashref);
195 =head2 GetSuggestionFromBiblionumber
197 $ordernumber = &GetSuggestionFromBiblionumber($dbh,$biblionumber)
199 Get a suggestion from it's biblionumber.
202 the id of the suggestion which is related to the biblionumber given on input args.
206 sub GetSuggestionFromBiblionumber {
207 my ($dbh,$biblionumber) = @_;
213 my $sth = $dbh->prepare($query);
214 $sth->execute($biblionumber);
215 my ($ordernumber) = $sth->fetchrow;
219 =head2 GetSuggestionByStatus
221 $aqorders = &GetSuggestionByStatus($status,[$branchcode])
223 Get a suggestion from it's status
226 all the suggestion with C<$status>
230 sub GetSuggestionByStatus {
232 my $branchcode = shift;
233 my $dbh = C4::Context->dbh;
234 my @sql_params=($status);
235 my $query = qq(SELECT suggestions.*,
236 U1.surname AS surnamesuggestedby,
237 U1.firstname AS firstnamesuggestedby,
238 U1.branchcode AS branchcodesuggestedby,
239 B1.branchname AS branchnamesuggestedby,
240 U1.borrowernumber AS borrnumsuggestedby,
241 U1.categorycode AS categorycodesuggestedby,
242 C1.description AS categorydescriptionsuggestedby,
243 U2.surname AS surnamemanagedby,
244 U2.firstname AS firstnamemanagedby,
245 U2.borrowernumber AS borrnummanagedby
247 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
248 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
249 LEFT JOIN categories AS C1 ON C1.categorycode=U1.categorycode
250 LEFT JOIN branches AS B1 on B1.branchcode = U1.branchcode
252 if (C4::Context->preference("IndependantBranches") || $branchcode) {
253 my $userenv = C4::Context->userenv;
255 unless ($userenv->{flags} % 2 == 1){
256 push @sql_params,$userenv->{branch};
257 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
261 push @sql_params,$branchcode;
262 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
266 my $sth = $dbh->prepare($query);
267 $sth->execute(@sql_params);
270 $results= $sth->fetchall_arrayref({});
274 =head2 CountSuggestion
276 &CountSuggestion($status)
278 Count the number of aqorders with the status given on input argument.
279 the arg status can be :
283 =item * ASKED : asked by the user, not dealed by the librarian
285 =item * ACCEPTED : accepted by the librarian, but not yet ordered
287 =item * REJECTED : rejected by the librarian (definitive status)
289 =item * ORDERED : ordered by the librarian (acquisition module)
294 the number of suggestion with this status.
298 sub CountSuggestion {
300 my $dbh = C4::Context->dbh;
302 if (C4::Context->preference("IndependantBranches")){
303 my $userenv = C4::Context->userenv;
304 if ($userenv->{flags} % 2 == 1){
310 $sth = $dbh->prepare($query);
311 $sth->execute($status);
316 FROM suggestions LEFT JOIN borrowers ON borrowers.borrowernumber=suggestions.suggestedby
318 AND (borrowers.branchcode='' OR borrowers.branchcode =?)
320 $sth = $dbh->prepare($query);
321 $sth->execute($status,$userenv->{branch});
330 $sth = $dbh->prepare($query);
331 $sth->execute($status);
333 my ($result) = $sth->fetchrow;
340 &NewSuggestion($suggestion)
342 Insert a new suggestion on database with value given on input arg.
347 my ($suggestion) = @_;
348 $suggestion->{STATUS}="ASKED" unless $suggestion->{STATUS};
349 return InsertInTable("suggestions",$suggestion);
354 &ModSuggestion($suggestion)
356 Modify the suggestion according to the hash passed by ref.
357 The hash HAS to contain suggestionid
358 Data not defined is not updated unless it is a note or sort1
359 Send a mail to notify the user that did the suggestion.
361 Note that there is no function to modify a suggestion.
367 my $status_update_table=UpdateInTable("suggestions", $suggestion);
368 # check mail sending.
369 if ($$suggestion{STATUS}){
370 my $letter=C4::Letters::getletter('suggestions',$$suggestion{STATUS});
372 my $enqueued = C4::Letters::EnqueueLetter({
374 borrowernumber=>$$suggestion{suggestedby},
375 suggestionid=>$$suggestion{suggestionid},
376 msg_transport_type=>'email'
378 if (!$enqueued){warn "can't enqueue letter $letter";}
381 return $status_update_table;
384 =head2 ConnectSuggestionAndBiblio
386 &ConnectSuggestionAndBiblio($ordernumber,$biblionumber)
388 connect a suggestion to an existing biblio
392 sub ConnectSuggestionAndBiblio {
393 my ($ordernumber,$biblionumber) = @_;
394 my $dbh=C4::Context->dbh;
400 my $sth = $dbh->prepare($query);
401 $sth->execute($biblionumber,$suggestionid);
406 &DelSuggestion($borrowernumber,$ordernumber)
408 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
413 my ($borrowernumber,$ordernumber,$type) = @_;
414 my $dbh = C4::Context->dbh;
415 # check that the suggestion comes from the suggestor
421 my $sth = $dbh->prepare($query);
422 $sth->execute($ordernumber);
423 my ($suggestedby) = $sth->fetchrow;
424 if ($type eq "intranet" || $suggestedby eq $borrowernumber ) {
426 DELETE FROM suggestions
429 $sth = $dbh->prepare($queryDelete);
430 my $suggestiondeleted=$sth->execute($suggestionid);
431 return $suggestiondeleted;
441 Koha Developement team <info@koha.org>