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
32 use List
::MoreUtils
qw(any);
33 use base
qw(Exporter);
36 ConnectSuggestionAndBiblio
41 GetSuggestionFromBiblionumber
42 GetSuggestionInfoFromBiblionumber
48 DelSuggestionsOlderThan
49 GetUnprocessedSuggestions
54 C4::Suggestions - Some useful functions for dealings with aqorders.
62 The functions in this module deal with the aqorders in OPAC and in librarian interface
64 A suggestion is done in the OPAC. It has the status "ASKED"
66 When a librarian manages the suggestion, he can set the status to "REJECTED" or "ACCEPTED".
68 When the book is ordered, the suggestion status becomes "ORDERED"
70 When a book is ordered and arrived in the library, the status becomes "AVAILABLE"
72 All aqorders of a borrower can be seen by the borrower itself.
73 Suggestions done by other borrowers can be seen when not "AVAILABLE"
77 =head2 SearchSuggestion
79 (\@array) = &SearchSuggestion($suggestionhashref_to_search)
81 searches for a suggestion
84 C<\@array> : the aqorders found. Array of hash.
85 Note the status is stored twice :
87 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
91 sub SearchSuggestion
{
92 my ($suggestion) = @_;
93 my $dbh = C4
::Context
->dbh;
98 U1.branchcode AS branchcodesuggestedby,
99 B1.branchname AS branchnamesuggestedby,
100 U1.surname AS surnamesuggestedby,
101 U1.firstname AS firstnamesuggestedby,
102 U1.cardnumber AS cardnumbersuggestedby,
103 U1.email AS emailsuggestedby,
104 U1.borrowernumber AS borrnumsuggestedby,
105 U1.categorycode AS categorycodesuggestedby,
106 C1.description AS categorydescriptionsuggestedby,
107 U2.surname AS surnamemanagedby,
108 U2.firstname AS firstnamemanagedby,
109 B2.branchname AS branchnamesuggestedby,
110 U2.email AS emailmanagedby,
111 U2.branchcode AS branchcodemanagedby,
112 U2.borrowernumber AS borrnummanagedby
114 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
115 LEFT JOIN branches AS B1 ON B1.branchcode=U1.branchcode
116 LEFT JOIN categories AS C1 ON C1.categorycode=U1.categorycode
117 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
118 LEFT JOIN branches AS B2 ON B2.branchcode=U2.branchcode
119 LEFT JOIN categories AS C2 ON C2.categorycode=U2.categorycode
124 # filter on biblio informations
126 qw( title author isbn publishercode copyrightdate collectiontitle ))
128 if ( $suggestion->{$field} ) {
129 push @sql_params, '%' . $suggestion->{$field} . '%';
130 push @query, qq{ AND suggestions
.$field LIKE ?
};
134 # filter on user branch
135 if ( C4
::Context
->preference('IndependentBranches') ) {
136 my $userenv = C4
::Context
->userenv;
138 if ( !C4
::Context
->IsSuperLibrarian() && !$suggestion->{branchcode
} )
140 push @sql_params, $$userenv{branch
};
142 AND (suggestions.branchcode=? OR suggestions.branchcode='')
147 if ( defined $suggestion->{branchcode
} && $suggestion->{branchcode
} ) {
148 unless ( $suggestion->{branchcode
} eq '__ANY__' ) {
149 push @sql_params, $suggestion->{branchcode
};
150 push @query, qq{ AND suggestions
.branchcode
=?
};
155 # filter on nillable fields
157 qw( STATUS itemtype suggestedby managedby acceptedby budgetid biblionumber )
160 if ( exists $suggestion->{$field}
161 and defined $suggestion->{$field}
162 and $suggestion->{$field} ne '__ANY__'
163 and $suggestion->{$field} ne q
||
165 if ( $suggestion->{$field} eq '__NONE__' ) {
166 push @query, qq{ AND
(suggestions
.$field = '' OR suggestions
.$field IS NULL
) };
169 push @sql_params, $suggestion->{$field};
170 push @query, qq{ AND suggestions
.$field = ?
};
175 # filter on date fields
176 foreach my $field (qw( suggesteddate manageddate accepteddate )) {
177 my $from = $field . "_from";
178 my $to = $field . "_to";
180 $from_dt = eval { dt_from_string
( $suggestion->{$from} ) } if ( $suggestion->{$from} );
181 my $from_sql = '0000-00-00';
182 $from_sql = output_pref
({ dt
=> $from_dt, dateformat
=> 'iso', dateonly
=> 1 })
184 $debug && warn "SQL for start date ($field): $from_sql";
185 if ( $suggestion->{$from} || $suggestion->{$to} ) {
186 push @query, qq{ AND suggestions
.$field BETWEEN ? AND ?
};
187 push @sql_params, $from_sql;
189 output_pref
({ dt
=> dt_from_string
( $suggestion->{$to} ), dateformat
=> 'iso', dateonly
=> 1 }) || output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 });
193 $debug && warn "@query";
194 my $sth = $dbh->prepare("@query");
195 $sth->execute(@sql_params);
198 # add status as field
199 while ( my $data = $sth->fetchrow_hashref ) {
200 $data->{ $data->{STATUS
} } = 1;
201 push( @results, $data );
204 return ( \
@results );
209 \%sth = &GetSuggestion($suggestionid)
211 this function get the detail of the suggestion $suggestionid (input arg)
214 the result of the SQL query as a hash : $sth->fetchrow_hashref.
219 my ($suggestionid) = @_;
220 my $dbh = C4
::Context
->dbh;
226 my $sth = $dbh->prepare($query);
227 $sth->execute($suggestionid);
228 return ( $sth->fetchrow_hashref );
231 =head2 GetSuggestionFromBiblionumber
233 $ordernumber = &GetSuggestionFromBiblionumber($biblionumber)
235 Get a suggestion from it's biblionumber.
238 the id of the suggestion which is related to the biblionumber given on input args.
242 sub GetSuggestionFromBiblionumber
{
243 my ($biblionumber) = @_;
247 WHERE biblionumber=? LIMIT 1
249 my $dbh = C4
::Context
->dbh;
250 my $sth = $dbh->prepare($query);
251 $sth->execute($biblionumber);
252 my ($suggestionid) = $sth->fetchrow;
253 return $suggestionid;
256 =head2 GetSuggestionInfoFromBiblionumber
258 Get a suggestion and borrower's informations from it's biblionumber.
261 all informations (suggestion and borrower) of the suggestion which is related to the biblionumber given.
265 sub GetSuggestionInfoFromBiblionumber
{
266 my ($biblionumber) = @_;
268 SELECT suggestions.*,
269 U1.surname AS surnamesuggestedby,
270 U1.firstname AS firstnamesuggestedby,
271 U1.borrowernumber AS borrnumsuggestedby
273 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
277 my $dbh = C4
::Context
->dbh;
278 my $sth = $dbh->prepare($query);
279 $sth->execute($biblionumber);
280 return $sth->fetchrow_hashref;
283 =head2 GetSuggestionInfo
285 Get a suggestion and borrower's informations from it's suggestionid
288 all informations (suggestion and borrower) of the suggestion which is related to the suggestionid given.
292 sub GetSuggestionInfo
{
293 my ($suggestionid) = @_;
295 SELECT suggestions.*,
296 U1.surname AS surnamesuggestedby,
297 U1.firstname AS firstnamesuggestedby,
298 U1.borrowernumber AS borrnumsuggestedby
300 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
304 my $dbh = C4
::Context
->dbh;
305 my $sth = $dbh->prepare($query);
306 $sth->execute($suggestionid);
307 return $sth->fetchrow_hashref;
310 =head2 GetSuggestionByStatus
312 $aqorders = &GetSuggestionByStatus($status,[$branchcode])
314 Get a suggestion from it's status
317 all the suggestion with C<$status>
321 sub GetSuggestionByStatus
{
323 my $branchcode = shift;
324 my $dbh = C4
::Context
->dbh;
325 my @sql_params = ($status);
327 SELECT suggestions.*,
328 U1.surname AS surnamesuggestedby,
329 U1.firstname AS firstnamesuggestedby,
330 U1.branchcode AS branchcodesuggestedby,
331 B1.branchname AS branchnamesuggestedby,
332 U1.borrowernumber AS borrnumsuggestedby,
333 U1.categorycode AS categorycodesuggestedby,
334 C1.description AS categorydescriptionsuggestedby,
335 U2.surname AS surnamemanagedby,
336 U2.firstname AS firstnamemanagedby,
337 U2.borrowernumber AS borrnummanagedby
339 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
340 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
341 LEFT JOIN categories AS C1 ON C1.categorycode=U1.categorycode
342 LEFT JOIN branches AS B1 on B1.branchcode=U1.branchcode
347 if ( C4
::Context
->preference("IndependentBranches") || $branchcode ) {
348 my $userenv = C4
::Context
->userenv;
350 unless ( C4
::Context
->IsSuperLibrarian() ) {
351 push @sql_params, $userenv->{branch
};
352 $query .= q{ AND (U1.branchcode = ? OR U1.branchcode ='') };
356 push @sql_params, $branchcode;
357 $query .= q{ AND (U1.branchcode = ? OR U1.branchcode ='') };
361 my $sth = $dbh->prepare($query);
362 $sth->execute(@sql_params);
364 $results = $sth->fetchall_arrayref( {} );
368 =head2 CountSuggestion
370 &CountSuggestion($status)
372 Count the number of aqorders with the status given on input argument.
373 the arg status can be :
377 =item * ASKED : asked by the user, not dealed by the librarian
379 =item * ACCEPTED : accepted by the librarian, but not yet ordered
381 =item * REJECTED : rejected by the librarian (definitive status)
383 =item * ORDERED : ordered by the librarian (acquisition module)
388 the number of suggestion with this status.
392 sub CountSuggestion
{
394 my $dbh = C4
::Context
->dbh;
396 my $userenv = C4
::Context
->userenv;
397 if ( C4
::Context
->preference("IndependentBranches")
398 && !C4
::Context
->IsSuperLibrarian() )
403 LEFT JOIN borrowers ON borrowers.borrowernumber=suggestions.suggestedby
405 AND (borrowers.branchcode='' OR borrowers.branchcode=?)
407 $sth = $dbh->prepare($query);
408 $sth->execute( $status, $userenv->{branch
} );
416 $sth = $dbh->prepare($query);
417 $sth->execute($status);
419 my ($result) = $sth->fetchrow;
426 &NewSuggestion($suggestion);
428 Insert a new suggestion on database with value given on input arg.
433 my ($suggestion) = @_;
445 # Set the fields to NULL if not given.
446 $suggestion->{$field} ||= undef;
449 $suggestion->{STATUS
} = "ASKED" unless $suggestion->{STATUS
};
451 $suggestion->{suggesteddate
} = dt_from_string
unless $suggestion->{suggesteddate
};
453 my $rs = Koha
::Database
->new->schema->resultset('Suggestion');
454 return $rs->create($suggestion)->id;
459 &ModSuggestion($suggestion)
461 Modify the suggestion according to the hash passed by ref.
462 The hash HAS to contain suggestionid
463 Data not defined is not updated unless it is a note or sort1
464 Send a mail to notify the user that did the suggestion.
466 Note that there is no function to modify a suggestion.
471 my ($suggestion) = @_;
472 return unless( $suggestion and defined($suggestion->{suggestionid
}) );
484 # Set the fields to NULL if not given.
485 $suggestion->{$field} = undef
486 if exists $suggestion->{$field}
487 and ($suggestion->{$field} eq '0'
488 or $suggestion->{$field} eq '' );
491 my $rs = Koha
::Database
->new->schema->resultset('Suggestion')->find($suggestion->{suggestionid
});
492 my $status_update_table = 1;
494 $rs->update($suggestion);
496 $status_update_table = 0 if( $@
);
498 if ( $suggestion->{STATUS
} ) {
500 # fetch the entire updated suggestion so that we can populate the letter
501 my $full_suggestion = GetSuggestion
( $suggestion->{suggestionid
} );
503 my $letter = C4
::Letters
::GetPreparedLetter
(
504 module
=> 'suggestions',
505 letter_code
=> $full_suggestion->{STATUS
},
506 branchcode
=> $full_suggestion->{branchcode
},
508 'branches' => $full_suggestion->{branchcode
},
509 'borrowers' => $full_suggestion->{suggestedby
},
510 'suggestions' => $full_suggestion,
511 'biblio' => $full_suggestion->{biblionumber
},
516 C4
::Letters
::EnqueueLetter
(
519 borrowernumber
=> $full_suggestion->{suggestedby
},
520 suggestionid
=> $full_suggestion->{suggestionid
},
521 LibraryName
=> C4
::Context
->preference("LibraryName"),
522 message_transport_type
=> 'email',
524 ) or warn "can't enqueue letter $letter";
527 return $status_update_table;
530 =head2 ConnectSuggestionAndBiblio
532 &ConnectSuggestionAndBiblio($ordernumber,$biblionumber)
534 connect a suggestion to an existing biblio
538 sub ConnectSuggestionAndBiblio
{
539 my ( $suggestionid, $biblionumber ) = @_;
540 my $dbh = C4
::Context
->dbh;
546 my $sth = $dbh->prepare($query);
547 $sth->execute( $biblionumber, $suggestionid );
552 &DelSuggestion($borrowernumber,$ordernumber)
554 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
559 my ( $borrowernumber, $suggestionid, $type ) = @_;
560 my $dbh = C4
::Context
->dbh;
562 # check that the suggestion comes from the suggestor
568 my $sth = $dbh->prepare($query);
569 $sth->execute($suggestionid);
570 my ($suggestedby) = $sth->fetchrow;
571 if ( $type eq 'intranet' || $suggestedby eq $borrowernumber ) {
573 DELETE FROM suggestions
576 $sth = $dbh->prepare($queryDelete);
577 my $suggestiondeleted = $sth->execute($suggestionid);
578 return $suggestiondeleted;
582 =head2 DelSuggestionsOlderThan
583 &DelSuggestionsOlderThan($days)
585 Delete all suggestions older than TODAY-$days , that have be accepted or rejected.
589 sub DelSuggestionsOlderThan
{
592 my $dbh = C4
::Context
->dbh;
593 my $sth = $dbh->prepare(
595 DELETE FROM suggestions
596 WHERE STATUS<>'ASKED'
597 AND date < ADDDATE(NOW(), ?)
600 $sth->execute("-$days");
603 sub GetUnprocessedSuggestions
{
604 my ( $number_of_days_since_the_last_modification ) = @_;
606 $number_of_days_since_the_last_modification ||= 0;
608 my $dbh = C4
::Context
->dbh;
610 my $s = $dbh->selectall_arrayref(q
|
613 WHERE STATUS
= 'ASKED'
614 AND budgetid IS NOT NULL
615 AND CAST
(NOW
() AS DATE
) - INTERVAL ? DAY
= CAST
(suggesteddate AS DATE
)
616 |, { Slice
=> {} }, $number_of_days_since_the_last_modification );
626 Koha Development Team <http://koha-community.org/>