Bug 10480: Use the framework plugin object in cataloguing
[koha.git] / C4 / Suggestions.pm
blobf34c942618600bf4971838918b2c7b81610a9289
1 package C4::Suggestions;
3 # Copyright 2000-2002 Katipo Communications
4 # Parts Copyright Biblibre 2011
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use strict;
23 #use warnings; FIXME - Bug 2505
24 use CGI qw ( -utf8 );
26 use C4::Context;
27 use C4::Output;
28 use C4::Dates qw(format_date format_date_in_iso);
29 use C4::Debug;
30 use C4::Letters;
31 use Koha::DateUtils qw( dt_from_string );
33 use List::MoreUtils qw(any);
34 use C4::Dates qw(format_date_in_iso);
35 use base qw(Exporter);
37 our $VERSION = 3.07.00.049;
38 our @EXPORT = qw(
39 ConnectSuggestionAndBiblio
40 CountSuggestion
41 DelSuggestion
42 GetSuggestion
43 GetSuggestionByStatus
44 GetSuggestionFromBiblionumber
45 GetSuggestionInfoFromBiblionumber
46 GetSuggestionInfo
47 ModStatus
48 ModSuggestion
49 NewSuggestion
50 SearchSuggestion
51 DelSuggestionsOlderThan
54 =head1 NAME
56 C4::Suggestions - Some useful functions for dealings with aqorders.
58 =head1 SYNOPSIS
60 use C4::Suggestions;
62 =head1 DESCRIPTION
64 The functions in this module deal with the aqorders in OPAC and in librarian interface
66 A suggestion is done in the OPAC. It has the status "ASKED"
68 When a librarian manages the suggestion, he can set the status to "REJECTED" or "ACCEPTED".
70 When the book is ordered, the suggestion status becomes "ORDERED"
72 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
74 All aqorders of a borrower can be seen by the borrower itself.
75 Suggestions done by other borrowers can be seen when not "AVAILABLE"
77 =head1 FUNCTIONS
79 =head2 SearchSuggestion
81 (\@array) = &SearchSuggestion($suggestionhashref_to_search)
83 searches for a suggestion
85 return :
86 C<\@array> : the aqorders found. Array of hash.
87 Note the status is stored twice :
88 * in the status field
89 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
91 =cut
93 sub SearchSuggestion {
94 my ($suggestion) = @_;
95 my $dbh = C4::Context->dbh;
96 my @sql_params;
97 my @query = (
99 SELECT suggestions.*,
100 U1.branchcode AS branchcodesuggestedby,
101 B1.branchname AS branchnamesuggestedby,
102 U1.surname AS surnamesuggestedby,
103 U1.firstname AS firstnamesuggestedby,
104 U1.email AS emailsuggestedby,
105 U1.borrowernumber AS borrnumsuggestedby,
106 U1.categorycode AS categorycodesuggestedby,
107 C1.description AS categorydescriptionsuggestedby,
108 U2.surname AS surnamemanagedby,
109 U2.firstname AS firstnamemanagedby,
110 B2.branchname AS branchnamesuggestedby,
111 U2.email AS emailmanagedby,
112 U2.branchcode AS branchcodemanagedby,
113 U2.borrowernumber AS borrnummanagedby
114 FROM suggestions
115 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
116 LEFT JOIN branches AS B1 ON B1.branchcode=U1.branchcode
117 LEFT JOIN categories AS C1 ON C1.categorycode=U1.categorycode
118 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
119 LEFT JOIN branches AS B2 ON B2.branchcode=U2.branchcode
120 LEFT JOIN categories AS C2 ON C2.categorycode=U2.categorycode
121 WHERE 1=1
125 # filter on biblio informations
126 foreach my $field (
127 qw( title author isbn publishercode copyrightdate collectiontitle ))
129 if ( $suggestion->{$field} ) {
130 push @sql_params, '%' . $suggestion->{$field} . '%';
131 push @query, qq{ AND suggestions.$field LIKE ? };
135 # filter on user branch
136 if ( C4::Context->preference('IndependentBranches') ) {
137 my $userenv = C4::Context->userenv;
138 if ($userenv) {
139 if ( !C4::Context->IsSuperLibrarian() && !$suggestion->{branchcode} )
141 push @sql_params, $$userenv{branch};
142 push @query, q{
143 AND (suggestions.branchcode=? OR suggestions.branchcode='')
147 } else {
148 if ( defined $suggestion->{branchcode} && $suggestion->{branchcode} ) {
149 unless ( $suggestion->{branchcode} eq '__ANY__' ) {
150 push @sql_params, $suggestion->{branchcode};
151 push @query, qq{ AND suggestions.branchcode=? };
156 # filter on nillable fields
157 foreach my $field (
158 qw( STATUS itemtype suggestedby managedby acceptedby budgetid biblionumber )
161 if ( exists $suggestion->{$field}
162 and defined $suggestion->{$field}
163 and $suggestion->{$field} ne '__ANY__'
164 and $suggestion->{$field} ne q||
166 if ( $suggestion->{$field} eq '__NONE__' ) {
167 push @query, qq{ AND (suggestions.$field = '' OR suggestions.$field IS NULL) };
169 else {
170 push @sql_params, $suggestion->{$field};
171 push @query, qq{ AND suggestions.$field = ? };
176 # filter on date fields
177 my $today = C4::Dates->today('iso');
178 foreach my $field (qw( suggesteddate manageddate accepteddate )) {
179 my $from = $field . "_from";
180 my $to = $field . "_to";
181 if ( $suggestion->{$from} || $suggestion->{$to} ) {
182 push @query, qq{ AND suggestions.$field BETWEEN ? AND ? };
183 push @sql_params,
184 format_date_in_iso( $suggestion->{$from} ) || '0000-00-00';
185 push @sql_params,
186 format_date_in_iso( $suggestion->{$to} ) || $today;
190 $debug && warn "@query";
191 my $sth = $dbh->prepare("@query");
192 $sth->execute(@sql_params);
193 my @results;
195 # add status as field
196 while ( my $data = $sth->fetchrow_hashref ) {
197 $data->{ $data->{STATUS} } = 1;
198 push( @results, $data );
201 return ( \@results );
204 =head2 GetSuggestion
206 \%sth = &GetSuggestion($suggestionid)
208 this function get the detail of the suggestion $suggestionid (input arg)
210 return :
211 the result of the SQL query as a hash : $sth->fetchrow_hashref.
213 =cut
215 sub GetSuggestion {
216 my ($suggestionid) = @_;
217 my $dbh = C4::Context->dbh;
218 my $query = q{
219 SELECT *
220 FROM suggestions
221 WHERE suggestionid=?
223 my $sth = $dbh->prepare($query);
224 $sth->execute($suggestionid);
225 return ( $sth->fetchrow_hashref );
228 =head2 GetSuggestionFromBiblionumber
230 $ordernumber = &GetSuggestionFromBiblionumber($biblionumber)
232 Get a suggestion from it's biblionumber.
234 return :
235 the id of the suggestion which is related to the biblionumber given on input args.
237 =cut
239 sub GetSuggestionFromBiblionumber {
240 my ($biblionumber) = @_;
241 my $query = q{
242 SELECT suggestionid
243 FROM suggestions
244 WHERE biblionumber=? LIMIT 1
246 my $dbh = C4::Context->dbh;
247 my $sth = $dbh->prepare($query);
248 $sth->execute($biblionumber);
249 my ($suggestionid) = $sth->fetchrow;
250 return $suggestionid;
253 =head2 GetSuggestionInfoFromBiblionumber
255 Get a suggestion and borrower's informations from it's biblionumber.
257 return :
258 all informations (suggestion and borrower) of the suggestion which is related to the biblionumber given.
260 =cut
262 sub GetSuggestionInfoFromBiblionumber {
263 my ($biblionumber) = @_;
264 my $query = q{
265 SELECT suggestions.*,
266 U1.surname AS surnamesuggestedby,
267 U1.firstname AS firstnamesuggestedby,
268 U1.borrowernumber AS borrnumsuggestedby
269 FROM suggestions
270 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
271 WHERE biblionumber=?
272 LIMIT 1
274 my $dbh = C4::Context->dbh;
275 my $sth = $dbh->prepare($query);
276 $sth->execute($biblionumber);
277 return $sth->fetchrow_hashref;
280 =head2 GetSuggestionInfo
282 Get a suggestion and borrower's informations from it's suggestionid
284 return :
285 all informations (suggestion and borrower) of the suggestion which is related to the suggestionid given.
287 =cut
289 sub GetSuggestionInfo {
290 my ($suggestionid) = @_;
291 my $query = q{
292 SELECT suggestions.*,
293 U1.surname AS surnamesuggestedby,
294 U1.firstname AS firstnamesuggestedby,
295 U1.borrowernumber AS borrnumsuggestedby
296 FROM suggestions
297 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
298 WHERE suggestionid=?
299 LIMIT 1
301 my $dbh = C4::Context->dbh;
302 my $sth = $dbh->prepare($query);
303 $sth->execute($suggestionid);
304 return $sth->fetchrow_hashref;
307 =head2 GetSuggestionByStatus
309 $aqorders = &GetSuggestionByStatus($status,[$branchcode])
311 Get a suggestion from it's status
313 return :
314 all the suggestion with C<$status>
316 =cut
318 sub GetSuggestionByStatus {
319 my $status = shift;
320 my $branchcode = shift;
321 my $dbh = C4::Context->dbh;
322 my @sql_params = ($status);
323 my $query = q{
324 SELECT suggestions.*,
325 U1.surname AS surnamesuggestedby,
326 U1.firstname AS firstnamesuggestedby,
327 U1.branchcode AS branchcodesuggestedby,
328 B1.branchname AS branchnamesuggestedby,
329 U1.borrowernumber AS borrnumsuggestedby,
330 U1.categorycode AS categorycodesuggestedby,
331 C1.description AS categorydescriptionsuggestedby,
332 U2.surname AS surnamemanagedby,
333 U2.firstname AS firstnamemanagedby,
334 U2.borrowernumber AS borrnummanagedby
335 FROM suggestions
336 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
337 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
338 LEFT JOIN categories AS C1 ON C1.categorycode=U1.categorycode
339 LEFT JOIN branches AS B1 on B1.branchcode=U1.branchcode
340 WHERE status = ?
343 # filter on branch
344 if ( C4::Context->preference("IndependentBranches") || $branchcode ) {
345 my $userenv = C4::Context->userenv;
346 if ($userenv) {
347 unless ( C4::Context->IsSuperLibrarian() ) {
348 push @sql_params, $userenv->{branch};
349 $query .= q{ AND (U1.branchcode = ? OR U1.branchcode ='') };
352 if ($branchcode) {
353 push @sql_params, $branchcode;
354 $query .= q{ AND (U1.branchcode = ? OR U1.branchcode ='') };
358 my $sth = $dbh->prepare($query);
359 $sth->execute(@sql_params);
360 my $results;
361 $results = $sth->fetchall_arrayref( {} );
362 return $results;
365 =head2 CountSuggestion
367 &CountSuggestion($status)
369 Count the number of aqorders with the status given on input argument.
370 the arg status can be :
372 =over 2
374 =item * ASKED : asked by the user, not dealed by the librarian
376 =item * ACCEPTED : accepted by the librarian, but not yet ordered
378 =item * REJECTED : rejected by the librarian (definitive status)
380 =item * ORDERED : ordered by the librarian (acquisition module)
382 =back
384 return :
385 the number of suggestion with this status.
387 =cut
389 sub CountSuggestion {
390 my ($status) = @_;
391 my $dbh = C4::Context->dbh;
392 my $sth;
393 my $userenv = C4::Context->userenv;
394 if ( C4::Context->preference("IndependentBranches")
395 && !C4::Context->IsSuperLibrarian() )
397 my $query = q{
398 SELECT count(*)
399 FROM suggestions
400 LEFT JOIN borrowers ON borrowers.borrowernumber=suggestions.suggestedby
401 WHERE STATUS=?
402 AND (borrowers.branchcode='' OR borrowers.branchcode=?)
404 $sth = $dbh->prepare($query);
405 $sth->execute( $status, $userenv->{branch} );
407 else {
408 my $query = q{
409 SELECT count(*)
410 FROM suggestions
411 WHERE STATUS=?
413 $sth = $dbh->prepare($query);
414 $sth->execute($status);
416 my ($result) = $sth->fetchrow;
417 return $result;
420 =head2 NewSuggestion
423 &NewSuggestion($suggestion);
425 Insert a new suggestion on database with value given on input arg.
427 =cut
429 sub NewSuggestion {
430 my ($suggestion) = @_;
432 for my $field ( qw(
433 suggestedby
434 managedby
435 manageddate
436 acceptedby
437 accepteddate
438 rejectedby
439 rejecteddate
440 budgetid
441 ) ) {
442 # Set the fields to NULL if not given.
443 $suggestion->{$field} ||= undef;
446 $suggestion->{STATUS} = "ASKED" unless $suggestion->{STATUS};
448 $suggestion->{suggesteddate} = dt_from_string unless $suggestion->{suggesteddate};
450 my $rs = Koha::Database->new->schema->resultset('Suggestion');
451 return $rs->create($suggestion)->id;
454 =head2 ModSuggestion
456 &ModSuggestion($suggestion)
458 Modify the suggestion according to the hash passed by ref.
459 The hash HAS to contain suggestionid
460 Data not defined is not updated unless it is a note or sort1
461 Send a mail to notify the user that did the suggestion.
463 Note that there is no function to modify a suggestion.
465 =cut
467 sub ModSuggestion {
468 my ($suggestion) = @_;
469 return unless( $suggestion and defined($suggestion->{suggestionid}) );
471 for my $field ( qw(
472 suggestedby
473 managedby
474 manageddate
475 acceptedby
476 accepteddate
477 rejectedby
478 rejecteddate
479 budgetid
480 ) ) {
481 # Set the fields to NULL if not given.
482 $suggestion->{$field} = undef
483 if exists $suggestion->{$field}
484 and ($suggestion->{$field} eq '0'
485 or $suggestion->{$field} eq '' );
488 my $rs = Koha::Database->new->schema->resultset('Suggestion')->find($suggestion->{suggestionid});
489 my $status_update_table = 1;
490 eval {
491 $rs->update($suggestion);
493 $status_update_table = 0 if( $@ );
495 if ( $suggestion->{STATUS} ) {
497 # fetch the entire updated suggestion so that we can populate the letter
498 my $full_suggestion = GetSuggestion( $suggestion->{suggestionid} );
499 if (
500 my $letter = C4::Letters::GetPreparedLetter(
501 module => 'suggestions',
502 letter_code => $full_suggestion->{STATUS},
503 branchcode => $full_suggestion->{branchcode},
504 tables => {
505 'branches' => $full_suggestion->{branchcode},
506 'borrowers' => $full_suggestion->{suggestedby},
507 'suggestions' => $full_suggestion,
508 'biblio' => $full_suggestion->{biblionumber},
513 C4::Letters::EnqueueLetter(
515 letter => $letter,
516 borrowernumber => $full_suggestion->{suggestedby},
517 suggestionid => $full_suggestion->{suggestionid},
518 LibraryName => C4::Context->preference("LibraryName"),
519 message_transport_type => 'email',
521 ) or warn "can't enqueue letter $letter";
524 return $status_update_table;
527 =head2 ConnectSuggestionAndBiblio
529 &ConnectSuggestionAndBiblio($ordernumber,$biblionumber)
531 connect a suggestion to an existing biblio
533 =cut
535 sub ConnectSuggestionAndBiblio {
536 my ( $suggestionid, $biblionumber ) = @_;
537 my $dbh = C4::Context->dbh;
538 my $query = q{
539 UPDATE suggestions
540 SET biblionumber=?
541 WHERE suggestionid=?
543 my $sth = $dbh->prepare($query);
544 $sth->execute( $biblionumber, $suggestionid );
547 =head2 DelSuggestion
549 &DelSuggestion($borrowernumber,$ordernumber)
551 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
553 =cut
555 sub DelSuggestion {
556 my ( $borrowernumber, $suggestionid, $type ) = @_;
557 my $dbh = C4::Context->dbh;
559 # check that the suggestion comes from the suggestor
560 my $query = q{
561 SELECT suggestedby
562 FROM suggestions
563 WHERE suggestionid=?
565 my $sth = $dbh->prepare($query);
566 $sth->execute($suggestionid);
567 my ($suggestedby) = $sth->fetchrow;
568 if ( $type eq 'intranet' || $suggestedby eq $borrowernumber ) {
569 my $queryDelete = q{
570 DELETE FROM suggestions
571 WHERE suggestionid=?
573 $sth = $dbh->prepare($queryDelete);
574 my $suggestiondeleted = $sth->execute($suggestionid);
575 return $suggestiondeleted;
579 =head2 DelSuggestionsOlderThan
580 &DelSuggestionsOlderThan($days)
582 Delete all suggestions older than TODAY-$days , that have be accepted or rejected.
584 =cut
586 sub DelSuggestionsOlderThan {
587 my ($days) = @_;
588 return unless $days;
589 my $dbh = C4::Context->dbh;
590 my $sth = $dbh->prepare(
592 DELETE FROM suggestions
593 WHERE STATUS<>'ASKED'
594 AND date < ADDDATE(NOW(), ?)
597 $sth->execute("-$days");
601 __END__
604 =head1 AUTHOR
606 Koha Development Team <http://koha-community.org/>
608 =cut