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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 #use warnings; FIXME - Bug 2505
31 use Koha
::Suggestions
;
33 use List
::MoreUtils
qw(any);
34 use base
qw(Exporter);
37 ConnectSuggestionAndBiblio
42 GetSuggestionFromBiblionumber
43 GetSuggestionInfoFromBiblionumber
49 DelSuggestionsOlderThan
50 GetUnprocessedSuggestions
55 C4::Suggestions - Some useful functions for dealings with aqorders.
63 The functions in this module deal with the aqorders in OPAC and in librarian interface
65 A suggestion is done in the OPAC. It has the status "ASKED"
67 When a librarian manages the suggestion, they can set the status to "REJECTED" or "ACCEPTED".
69 When the book is ordered, the suggestion status becomes "ORDERED"
71 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
73 All aqorders of a borrower can be seen by the borrower itself.
74 Suggestions done by other borrowers can be seen when not "AVAILABLE"
78 =head2 SearchSuggestion
80 (\@array) = &SearchSuggestion($suggestionhashref_to_search)
82 searches for a suggestion
85 C<\@array> : the aqorders found. Array of hash.
86 Note the status is stored twice :
88 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
92 sub SearchSuggestion
{
93 my ($suggestion) = @_;
94 my $dbh = C4
::Context
->dbh;
99 U1.branchcode AS branchcodesuggestedby,
100 B1.branchname AS branchnamesuggestedby,
101 U1.surname AS surnamesuggestedby,
102 U1.firstname AS firstnamesuggestedby,
103 U1.cardnumber AS cardnumbersuggestedby,
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 BU.budget_name AS budget_name
116 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
117 LEFT JOIN branches AS B1 ON B1.branchcode=U1.branchcode
118 LEFT JOIN categories AS C1 ON C1.categorycode=U1.categorycode
119 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
120 LEFT JOIN branches AS B2 ON B2.branchcode=U2.branchcode
121 LEFT JOIN categories AS C2 ON C2.categorycode=U2.categorycode
122 LEFT JOIN aqbudgets AS BU ON budgetid=BU.budget_id
127 # filter on biblio informations
129 qw( title author isbn publishercode copyrightdate collectiontitle ))
131 if ( $suggestion->{$field} ) {
132 push @sql_params, '%' . $suggestion->{$field} . '%';
133 push @query, qq{ AND suggestions
.$field LIKE ?
};
137 # filter on user branch
138 if ( C4
::Context
->preference('IndependentBranches') ) {
139 my $userenv = C4
::Context
->userenv;
141 if ( !C4
::Context
->IsSuperLibrarian() && !$suggestion->{branchcode
} )
143 push @sql_params, $$userenv{branch
};
145 AND (suggestions.branchcode=? OR suggestions.branchcode='')
150 if ( defined $suggestion->{branchcode
} && $suggestion->{branchcode
} ) {
151 unless ( $suggestion->{branchcode
} eq '__ANY__' ) {
152 push @sql_params, $suggestion->{branchcode
};
153 push @query, qq{ AND suggestions
.branchcode
=?
};
158 # filter on nillable fields
160 qw( STATUS itemtype suggestedby managedby acceptedby budgetid biblionumber )
163 if ( exists $suggestion->{$field}
164 and defined $suggestion->{$field}
165 and $suggestion->{$field} ne '__ANY__'
167 $suggestion->{$field} ne q
||
168 or $field eq 'STATUS'
171 if ( $suggestion->{$field} eq '__NONE__' ) {
172 push @query, qq{ AND
(suggestions
.$field = '' OR suggestions
.$field IS NULL
) };
175 push @sql_params, $suggestion->{$field};
176 push @query, qq{ AND suggestions
.$field = ?
};
181 # filter on date fields
182 foreach my $field (qw( suggesteddate manageddate accepteddate )) {
183 my $from = $field . "_from";
184 my $to = $field . "_to";
186 $from_dt = eval { dt_from_string
( $suggestion->{$from} ) } if ( $suggestion->{$from} );
187 my $from_sql = '0000-00-00';
188 $from_sql = output_pref
({ dt
=> $from_dt, dateformat
=> 'iso', dateonly
=> 1 })
190 $debug && warn "SQL for start date ($field): $from_sql";
191 if ( $suggestion->{$from} || $suggestion->{$to} ) {
192 push @query, qq{ AND suggestions
.$field BETWEEN ? AND ?
};
193 push @sql_params, $from_sql;
195 output_pref
({ dt
=> dt_from_string
( $suggestion->{$to} ), dateformat
=> 'iso', dateonly
=> 1 }) || output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 });
199 $debug && warn "@query";
200 my $sth = $dbh->prepare("@query");
201 $sth->execute(@sql_params);
204 # add status as field
205 while ( my $data = $sth->fetchrow_hashref ) {
206 $data->{ $data->{STATUS
} } = 1;
207 push( @results, $data );
210 return ( \
@results );
215 \%sth = &GetSuggestion($suggestionid)
217 this function get the detail of the suggestion $suggestionid (input arg)
220 the result of the SQL query as a hash : $sth->fetchrow_hashref.
225 my ($suggestionid) = @_;
226 my $dbh = C4
::Context
->dbh;
232 my $sth = $dbh->prepare($query);
233 $sth->execute($suggestionid);
234 return ( $sth->fetchrow_hashref );
237 =head2 GetSuggestionFromBiblionumber
239 $ordernumber = &GetSuggestionFromBiblionumber($biblionumber)
241 Get a suggestion from it's biblionumber.
244 the id of the suggestion which is related to the biblionumber given on input args.
248 sub GetSuggestionFromBiblionumber
{
249 my ($biblionumber) = @_;
253 WHERE biblionumber=? LIMIT 1
255 my $dbh = C4
::Context
->dbh;
256 my $sth = $dbh->prepare($query);
257 $sth->execute($biblionumber);
258 my ($suggestionid) = $sth->fetchrow;
259 return $suggestionid;
262 =head2 GetSuggestionInfoFromBiblionumber
264 Get a suggestion and borrower's informations from it's biblionumber.
267 all informations (suggestion and borrower) of the suggestion which is related to the biblionumber given.
271 sub GetSuggestionInfoFromBiblionumber
{
272 my ($biblionumber) = @_;
274 SELECT suggestions.*,
275 U1.surname AS surnamesuggestedby,
276 U1.firstname AS firstnamesuggestedby,
277 U1.borrowernumber AS borrnumsuggestedby
279 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
283 my $dbh = C4
::Context
->dbh;
284 my $sth = $dbh->prepare($query);
285 $sth->execute($biblionumber);
286 return $sth->fetchrow_hashref;
289 =head2 GetSuggestionInfo
291 Get a suggestion and borrower's informations from it's suggestionid
294 all informations (suggestion and borrower) of the suggestion which is related to the suggestionid given.
298 sub GetSuggestionInfo
{
299 my ($suggestionid) = @_;
301 SELECT suggestions.*,
302 U1.surname AS surnamesuggestedby,
303 U1.firstname AS firstnamesuggestedby,
304 U1.borrowernumber AS borrnumsuggestedby
306 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
310 my $dbh = C4
::Context
->dbh;
311 my $sth = $dbh->prepare($query);
312 $sth->execute($suggestionid);
313 return $sth->fetchrow_hashref;
316 =head2 GetSuggestionByStatus
318 $aqorders = &GetSuggestionByStatus($status,[$branchcode])
320 Get a suggestion from it's status
323 all the suggestion with C<$status>
327 sub GetSuggestionByStatus
{
329 my $branchcode = shift;
330 my $dbh = C4
::Context
->dbh;
331 my @sql_params = ($status);
333 SELECT suggestions.*,
334 U1.surname AS surnamesuggestedby,
335 U1.firstname AS firstnamesuggestedby,
336 U1.branchcode AS branchcodesuggestedby,
337 B1.branchname AS branchnamesuggestedby,
338 U1.borrowernumber AS borrnumsuggestedby,
339 U1.categorycode AS categorycodesuggestedby,
340 C1.description AS categorydescriptionsuggestedby,
341 U2.surname AS surnamemanagedby,
342 U2.firstname AS firstnamemanagedby,
343 U2.borrowernumber AS borrnummanagedby
345 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
346 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
347 LEFT JOIN categories AS C1 ON C1.categorycode=U1.categorycode
348 LEFT JOIN branches AS B1 on B1.branchcode=U1.branchcode
353 if ( C4
::Context
->preference("IndependentBranches") || $branchcode ) {
354 my $userenv = C4
::Context
->userenv;
356 unless ( C4
::Context
->IsSuperLibrarian() ) {
357 push @sql_params, $userenv->{branch
};
358 $query .= q{ AND (U1.branchcode = ? OR U1.branchcode ='') };
362 push @sql_params, $branchcode;
363 $query .= q{ AND (U1.branchcode = ? OR U1.branchcode ='') };
367 my $sth = $dbh->prepare($query);
368 $sth->execute(@sql_params);
370 $results = $sth->fetchall_arrayref( {} );
374 =head2 CountSuggestion
376 &CountSuggestion($status)
378 Count the number of aqorders with the status given on input argument.
379 the arg status can be :
383 =item * ASKED : asked by the user, not dealed by the librarian
385 =item * ACCEPTED : accepted by the librarian, but not yet ordered
387 =item * REJECTED : rejected by the librarian (definitive status)
389 =item * ORDERED : ordered by the librarian (acquisition module)
394 the number of suggestion with this status.
398 sub CountSuggestion
{
400 my $dbh = C4
::Context
->dbh;
402 my $userenv = C4
::Context
->userenv;
403 if ( C4
::Context
->preference("IndependentBranches")
404 && !C4
::Context
->IsSuperLibrarian() )
409 LEFT JOIN borrowers ON borrowers.borrowernumber=suggestions.suggestedby
411 AND (borrowers.branchcode='' OR borrowers.branchcode=?)
413 $sth = $dbh->prepare($query);
414 $sth->execute( $status, $userenv->{branch
} );
422 $sth = $dbh->prepare($query);
423 $sth->execute($status);
425 my ($result) = $sth->fetchrow;
432 &NewSuggestion($suggestion);
434 Insert a new suggestion on database with value given on input arg.
439 my ($suggestion) = @_;
441 $suggestion->{STATUS
} = "ASKED" unless $suggestion->{STATUS
};
443 $suggestion->{suggesteddate
} = dt_from_string
unless $suggestion->{suggesteddate
};
445 my $suggestion_object = Koha
::Suggestion
->new( $suggestion )->store;
446 my $suggestion_id = $suggestion_object->suggestionid;
448 my $emailpurchasesuggestions = C4
::Context
->preference("EmailPurchaseSuggestions");
449 if ($emailpurchasesuggestions) {
450 my $full_suggestion = GetSuggestion
( $suggestion_id); # We should not need to refetch it!
452 my $letter = C4
::Letters
::GetPreparedLetter
(
453 module
=> 'suggestions',
454 letter_code
=> 'NEW_SUGGESTION',
456 'branches' => $full_suggestion->{branchcode
},
457 'borrowers' => $full_suggestion->{suggestedby
},
458 'suggestions' => $full_suggestion,
464 if ( $emailpurchasesuggestions eq "BranchEmailAddress" ) {
466 Koha
::Libraries
->find( $full_suggestion->{branchcode
} );
468 $library->branchreplyto
469 || $library->branchemail
470 || C4
::Context
->preference('ReplytoDefault')
471 || C4
::Context
->preference('KohaAdminEmailAddress');
473 elsif ( $emailpurchasesuggestions eq "KohaAdminEmailAddress" ) {
474 $toaddress = C4
::Context
->preference('ReplytoDefault')
475 || C4
::Context
->preference('KohaAdminEmailAddress');
479 C4
::Context
->preference($emailpurchasesuggestions)
480 || C4
::Context
->preference('ReplytoDefault')
481 || C4
::Context
->preference('KohaAdminEmailAddress');
484 C4
::Letters
::EnqueueLetter
(
487 borrowernumber
=> $full_suggestion->{suggestedby
},
488 suggestionid
=> $full_suggestion->{suggestionid
},
489 to_address
=> $toaddress,
490 message_transport_type
=> 'email',
492 ) or warn "can't enqueue letter $letter";
496 return $suggestion_id;
501 &ModSuggestion($suggestion)
503 Modify the suggestion according to the hash passed by ref.
504 The hash HAS to contain suggestionid
505 Data not defined is not updated unless it is a note or sort1
506 Send a mail to notify the user that did the suggestion.
508 Note that there is no function to modify a suggestion.
513 my ($suggestion) = @_;
514 return unless( $suggestion and defined($suggestion->{suggestionid
}) );
516 my $suggestion_object = Koha
::Suggestions
->find( $suggestion->{suggestionid
} );
517 eval { # FIXME Must raise an exception instead
518 $suggestion_object->set($suggestion)->store;
522 if ( $suggestion->{STATUS
} ) {
524 # fetch the entire updated suggestion so that we can populate the letter
525 my $full_suggestion = GetSuggestion
( $suggestion->{suggestionid
} );
526 my $patron = Koha
::Patrons
->find( $full_suggestion->{suggestedby
} );
528 my $transport = (C4
::Context
->preference("FallbackToSMSIfNoEmail")) && ($patron->smsalertnumber) && (!$patron->email) ?
'sms' : 'email';
531 my $letter = C4
::Letters
::GetPreparedLetter
(
532 module
=> 'suggestions',
533 letter_code
=> $full_suggestion->{STATUS
},
534 branchcode
=> $full_suggestion->{branchcode
},
535 lang
=> $patron->lang,
537 'branches' => $full_suggestion->{branchcode
},
538 'borrowers' => $full_suggestion->{suggestedby
},
539 'suggestions' => $full_suggestion,
540 'biblio' => $full_suggestion->{biblionumber
},
545 C4
::Letters
::EnqueueLetter
(
548 borrowernumber
=> $full_suggestion->{suggestedby
},
549 suggestionid
=> $full_suggestion->{suggestionid
},
550 LibraryName
=> C4
::Context
->preference("LibraryName"),
551 message_transport_type
=> $transport,
553 ) or warn "can't enqueue letter $letter";
556 return 1; # No useful if the exception is raised earlier
559 =head2 ConnectSuggestionAndBiblio
561 &ConnectSuggestionAndBiblio($ordernumber,$biblionumber)
563 connect a suggestion to an existing biblio
567 sub ConnectSuggestionAndBiblio
{
568 my ( $suggestionid, $biblionumber ) = @_;
569 my $dbh = C4
::Context
->dbh;
575 my $sth = $dbh->prepare($query);
576 $sth->execute( $biblionumber, $suggestionid );
581 &DelSuggestion($borrowernumber,$ordernumber)
583 Delete a suggestion. A borrower can delete a suggestion only if they are its owner.
588 my ( $borrowernumber, $suggestionid, $type ) = @_;
589 my $dbh = C4
::Context
->dbh;
591 # check that the suggestion comes from the suggestor
597 my $sth = $dbh->prepare($query);
598 $sth->execute($suggestionid);
599 my ($suggestedby) = $sth->fetchrow;
600 if ( $type eq 'intranet' || $suggestedby eq $borrowernumber ) {
602 DELETE FROM suggestions
605 $sth = $dbh->prepare($queryDelete);
606 my $suggestiondeleted = $sth->execute($suggestionid);
607 return $suggestiondeleted;
611 =head2 DelSuggestionsOlderThan
612 &DelSuggestionsOlderThan($days)
614 Delete all suggestions older than TODAY-$days , that have be accepted or rejected.
615 We do now allow a negative number. If you want to delete all suggestions, just use Koha::Suggestions->delete or so.
619 sub DelSuggestionsOlderThan
{
621 return unless $days && $days > 0;
622 my $dbh = C4
::Context
->dbh;
623 my $sth = $dbh->prepare(
625 DELETE FROM suggestions
626 WHERE STATUS<>'ASKED'
627 AND date < ADDDATE(NOW(), ?)
630 $sth->execute("-$days");
633 sub GetUnprocessedSuggestions
{
634 my ( $number_of_days_since_the_last_modification ) = @_;
636 $number_of_days_since_the_last_modification ||= 0;
638 my $dbh = C4
::Context
->dbh;
640 my $s = $dbh->selectall_arrayref(q
|
643 WHERE STATUS
= 'ASKED'
644 AND budgetid IS NOT NULL
645 AND CAST
(NOW
() AS DATE
) - INTERVAL ? DAY
= CAST
(suggesteddate AS DATE
)
646 |, { Slice
=> {} }, $number_of_days_since_the_last_modification );
656 Koha Development Team <http://koha-community.org/>