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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #use warnings; FIXME - Bug 2505
27 use C4
::Dates
qw(format_date);
28 use C4
::SQLHelper
qw(:all);
31 use List
::MoreUtils qw
<any
>;
32 use base
'Exporter'; # parent would be better there
35 &ConnectSuggestionAndBiblio
39 &GetSuggestionByStatus
40 &GetSuggestionFromBiblionumber
46 use C4
::Dates
qw(format_date_in_iso);
47 use vars
qw($VERSION @ISA @EXPORT);
50 # set the version for version checking
58 &GetSuggestionByStatus
62 &ConnectSuggestionAndBiblio
63 &GetSuggestionFromBiblionumber
64 &ConnectSuggestionAndBiblio
67 &GetSuggestionByStatus
68 &GetSuggestionFromBiblionumber
75 C4::Suggestions - Some useful functions for dealings with aqorders.
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"
98 =head2 SearchSuggestion
100 (\@array) = &SearchSuggestion($suggestionhashref_to_search)
102 searches for a suggestion
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.
112 sub SearchSuggestion
{
114 my $dbh = C4
::Context
->dbh;
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
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')
141 if ( my $s = $suggestion->{$_} ) {
142 push @sql_params,'%'.$s.'%';
143 " and suggestions.$_ like ? ";
145 } qw( title author isbn publishercode collectiontitle )
148 my $userenv = C4
::Context
->userenv;
149 if (C4
::Context
->preference('IndependantBranches')) {
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
164 if ($$suggestion{$field}){
165 push @sql_params,$suggestion->{$field};
166 push @query, " and suggestions.$field=?";
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);
177 while ( my $data=$sth->fetchrow_hashref ){
178 $$data{$$data{STATUS
}} = 1;
179 push(@results,$data);
186 \%sth = &GetSuggestion($ordernumber)
188 this function get the detail of the suggestion $ordernumber (input arg)
191 the result of the SQL query as a hash : $sth->fetchrow_hashref.
196 my ($ordernumber) = @_;
197 my $dbh = C4
::Context
->dbh;
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.
215 the id of the suggestion which is related to the biblionumber given on input args.
219 sub GetSuggestionFromBiblionumber
{
220 my ($biblionumber) = @_;
226 my $dbh=C4
::Context
->dbh;
227 my $sth = $dbh->prepare($query);
228 $sth->execute($biblionumber);
229 my ($ordernumber) = $sth->fetchrow;
233 =head2 GetSuggestionByStatus
235 $aqorders = &GetSuggestionByStatus($status,[$branchcode])
237 Get a suggestion from it's status
240 all the suggestion with C<$status>
244 sub GetSuggestionByStatus
{
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
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
266 if (C4
::Context
->preference("IndependantBranches") || $branchcode) {
267 my $userenv = C4
::Context
->userenv;
269 unless ($userenv->{flags
} % 2 == 1){
270 push @sql_params,$userenv->{branch
};
271 $query .= " and (U1.branchcode = ? or U1.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);
284 $results= $sth->fetchall_arrayref({});
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 :
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)
308 the number of suggestion with this status.
312 sub CountSuggestion
{
314 my $dbh = C4
::Context
->dbh;
316 if (C4
::Context
->preference("IndependantBranches")){
317 my $userenv = C4
::Context
->userenv;
318 if ($userenv->{flags
} % 2 == 1){
324 $sth = $dbh->prepare($query);
325 $sth->execute($status);
330 FROM suggestions LEFT JOIN borrowers ON borrowers
.borrowernumber
=suggestions
.suggestedby
332 AND
(borrowers
.branchcode
='' OR borrowers
.branchcode
=?
)
334 $sth = $dbh->prepare($query);
335 $sth->execute($status,$userenv->{branch
});
344 $sth = $dbh->prepare($query);
345 $sth->execute($status);
347 my ($result) = $sth->fetchrow;
354 &NewSuggestion($suggestion);
356 Insert a new suggestion on database with value given on input arg.
361 my ($suggestion) = @_;
362 $suggestion->{STATUS
}="ASKED" unless $suggestion->{STATUS
};
363 return InsertInTable
("suggestions",$suggestion);
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.
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
});
386 my $enqueued = C4
::Letters
::EnqueueLetter
({
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
407 sub ConnectSuggestionAndBiblio
{
408 my ($suggestionid,$biblionumber) = @_;
409 my $dbh=C4
::Context
->dbh;
415 my $sth = $dbh->prepare($query);
416 $sth->execute($biblionumber,$suggestionid);
421 &DelSuggestion($borrowernumber,$ordernumber)
423 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
428 my ($borrowernumber,$suggestionid,$type) = @_;
429 my $dbh = C4
::Context
->dbh;
430 # check that the suggestion comes from the suggestor
436 my $sth = $dbh->prepare($query);
437 $sth->execute($suggestionid);
438 my ($suggestedby) = $sth->fetchrow;
439 if ($type eq "intranet" || $suggestedby eq $borrowernumber ) {
441 DELETE FROM suggestions
444 $sth = $dbh->prepare($queryDelete);
445 my $suggestiondeleted=$sth->execute($suggestionid);
446 return $suggestiondeleted;
456 Koha Developement team <info@koha.org>