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
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.
23 #use warnings; FIXME - Bug 2505
28 use C4
::Dates
qw(format_date format_date_in_iso);
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;
39 ConnectSuggestionAndBiblio
44 GetSuggestionFromBiblionumber
45 GetSuggestionInfoFromBiblionumber
51 DelSuggestionsOlderThan
56 C4::Suggestions - Some useful functions for dealings with aqorders.
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"
79 =head2 SearchSuggestion
81 (\@array) = &SearchSuggestion($suggestionhashref_to_search)
83 searches for a suggestion
86 C<\@array> : the aqorders found. Array of hash.
87 Note the status is stored twice :
89 * as parameter ( for example ASKED => 1, or REJECTED => 1) . This is for template & translation purposes.
93 sub SearchSuggestion
{
94 my ($suggestion) = @_;
95 my $dbh = C4
::Context
->dbh;
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
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
125 # filter on biblio informations
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;
139 if ( !C4
::Context
->IsSuperLibrarian() && !$suggestion->{branchcode
} )
141 push @sql_params, $$userenv{branch
};
143 AND (suggestions.branchcode=? OR suggestions.branchcode='')
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
158 qw( STATUS itemtype suggestedby managedby acceptedby budgetid biblionumber )
161 if ( exists $suggestion->{$field} ) {
162 if ( defined $suggestion->{$field} and $suggestion->{$field} ne '' )
164 push @sql_params, $suggestion->{$field};
165 push @query, qq{ AND suggestions
.$field=?
};
169 AND
(suggestions
.$field='' OR suggestions
.$field IS NULL
)
175 # filter on date fields
176 my $today = C4
::Dates
->today('iso');
177 foreach my $field (qw( suggesteddate manageddate accepteddate )) {
178 my $from = $field . "_from";
179 my $to = $field . "_to";
180 if ( $suggestion->{$from} || $suggestion->{$to} ) {
181 push @query, qq{ AND suggestions
.$field BETWEEN ? AND ?
};
183 format_date_in_iso
( $suggestion->{$from} ) || '0000-00-00';
185 format_date_in_iso
( $suggestion->{$to} ) || $today;
189 $debug && warn "@query";
190 my $sth = $dbh->prepare("@query");
191 $sth->execute(@sql_params);
194 # add status as field
195 while ( my $data = $sth->fetchrow_hashref ) {
196 $data->{ $data->{STATUS
} } = 1;
197 push( @results, $data );
200 return ( \
@results );
205 \%sth = &GetSuggestion($ordernumber)
207 this function get the detail of the suggestion $ordernumber (input arg)
210 the result of the SQL query as a hash : $sth->fetchrow_hashref.
215 my ($ordernumber) = @_;
216 my $dbh = C4
::Context
->dbh;
222 my $sth = $dbh->prepare($query);
223 $sth->execute($ordernumber);
224 return ( $sth->fetchrow_hashref );
227 =head2 GetSuggestionFromBiblionumber
229 $ordernumber = &GetSuggestionFromBiblionumber($biblionumber)
231 Get a suggestion from it's biblionumber.
234 the id of the suggestion which is related to the biblionumber given on input args.
238 sub GetSuggestionFromBiblionumber
{
239 my ($biblionumber) = @_;
243 WHERE biblionumber=? LIMIT 1
245 my $dbh = C4
::Context
->dbh;
246 my $sth = $dbh->prepare($query);
247 $sth->execute($biblionumber);
248 my ($suggestionid) = $sth->fetchrow;
249 return $suggestionid;
252 =head2 GetSuggestionInfoFromBiblionumber
254 Get a suggestion and borrower's informations from it's biblionumber.
257 all informations (suggestion and borrower) of the suggestion which is related to the biblionumber given.
261 sub GetSuggestionInfoFromBiblionumber
{
262 my ($biblionumber) = @_;
264 SELECT suggestions.*,
265 U1.surname AS surnamesuggestedby,
266 U1.firstname AS firstnamesuggestedby,
267 U1.borrowernumber AS borrnumsuggestedby
269 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
273 my $dbh = C4
::Context
->dbh;
274 my $sth = $dbh->prepare($query);
275 $sth->execute($biblionumber);
276 return $sth->fetchrow_hashref;
279 =head2 GetSuggestionInfo
281 Get a suggestion and borrower's informations from it's suggestionid
284 all informations (suggestion and borrower) of the suggestion which is related to the suggestionid given.
288 sub GetSuggestionInfo
{
289 my ($suggestionid) = @_;
291 SELECT suggestions.*,
292 U1.surname AS surnamesuggestedby,
293 U1.firstname AS firstnamesuggestedby,
294 U1.borrowernumber AS borrnumsuggestedby
296 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
300 my $dbh = C4
::Context
->dbh;
301 my $sth = $dbh->prepare($query);
302 $sth->execute($suggestionid);
303 return $sth->fetchrow_hashref;
306 =head2 GetSuggestionByStatus
308 $aqorders = &GetSuggestionByStatus($status,[$branchcode])
310 Get a suggestion from it's status
313 all the suggestion with C<$status>
317 sub GetSuggestionByStatus
{
319 my $branchcode = shift;
320 my $dbh = C4
::Context
->dbh;
321 my @sql_params = ($status);
323 SELECT suggestions.*,
324 U1.surname AS surnamesuggestedby,
325 U1.firstname AS firstnamesuggestedby,
326 U1.branchcode AS branchcodesuggestedby,
327 B1.branchname AS branchnamesuggestedby,
328 U1.borrowernumber AS borrnumsuggestedby,
329 U1.categorycode AS categorycodesuggestedby,
330 C1.description AS categorydescriptionsuggestedby,
331 U2.surname AS surnamemanagedby,
332 U2.firstname AS firstnamemanagedby,
333 U2.borrowernumber AS borrnummanagedby
335 LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
336 LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
337 LEFT JOIN categories AS C1 ON C1.categorycode=U1.categorycode
338 LEFT JOIN branches AS B1 on B1.branchcode=U1.branchcode
343 if ( C4
::Context
->preference("IndependentBranches") || $branchcode ) {
344 my $userenv = C4
::Context
->userenv;
346 unless ( C4
::Context
->IsSuperLibrarian() ) {
347 push @sql_params, $userenv->{branch
};
348 $query .= q{ AND (U1.branchcode = ? OR U1.branchcode ='') };
352 push @sql_params, $branchcode;
353 $query .= q{ AND (U1.branchcode = ? OR U1.branchcode ='') };
357 my $sth = $dbh->prepare($query);
358 $sth->execute(@sql_params);
360 $results = $sth->fetchall_arrayref( {} );
364 =head2 CountSuggestion
366 &CountSuggestion($status)
368 Count the number of aqorders with the status given on input argument.
369 the arg status can be :
373 =item * ASKED : asked by the user, not dealed by the librarian
375 =item * ACCEPTED : accepted by the librarian, but not yet ordered
377 =item * REJECTED : rejected by the librarian (definitive status)
379 =item * ORDERED : ordered by the librarian (acquisition module)
384 the number of suggestion with this status.
388 sub CountSuggestion
{
390 my $dbh = C4
::Context
->dbh;
392 my $userenv = C4
::Context
->userenv;
393 if ( C4
::Context
->preference("IndependentBranches")
394 && !C4
::Context
->IsSuperLibrarian() )
399 LEFT JOIN borrowers ON borrowers.borrowernumber=suggestions.suggestedby
401 AND (borrowers.branchcode='' OR borrowers.branchcode=?)
403 $sth = $dbh->prepare($query);
404 $sth->execute( $status, $userenv->{branch
} );
412 $sth = $dbh->prepare($query);
413 $sth->execute($status);
415 my ($result) = $sth->fetchrow;
422 &NewSuggestion($suggestion);
424 Insert a new suggestion on database with value given on input arg.
429 my ($suggestion) = @_;
440 # Set the fields to NULL if not given.
441 $suggestion->{$field} ||= undef;
444 $suggestion->{STATUS
} = "ASKED" unless $suggestion->{STATUS
};
446 $suggestion->{suggesteddate
} = dt_from_string
unless $suggestion->{suggesteddate
};
448 my $rs = Koha
::Database
->new->schema->resultset('Suggestion');
449 return $rs->create($suggestion)->id;
454 &ModSuggestion($suggestion)
456 Modify the suggestion according to the hash passed by ref.
457 The hash HAS to contain suggestionid
458 Data not defined is not updated unless it is a note or sort1
459 Send a mail to notify the user that did the suggestion.
461 Note that there is no function to modify a suggestion.
466 my ($suggestion) = @_;
467 return unless( $suggestion and defined($suggestion->{suggestionid
}) );
478 # Set the fields to NULL if not given.
479 $suggestion->{$field} = undef
480 if exists $suggestion->{$field}
481 and ($suggestion->{$field} eq '0'
482 or $suggestion->{$field} eq '' );
485 my $rs = Koha
::Database
->new->schema->resultset('Suggestion')->find($suggestion->{suggestionid
});
486 my $status_update_table = 1;
488 $rs->update($suggestion);
490 $status_update_table = 0 if( $@
);
492 if ( $suggestion->{STATUS
} ) {
494 # fetch the entire updated suggestion so that we can populate the letter
495 my $full_suggestion = GetSuggestion
( $suggestion->{suggestionid
} );
497 my $letter = C4
::Letters
::GetPreparedLetter
(
498 module
=> 'suggestions',
499 letter_code
=> $full_suggestion->{STATUS
},
500 branchcode
=> $full_suggestion->{branchcode
},
502 'branches' => $full_suggestion->{branchcode
},
503 'borrowers' => $full_suggestion->{suggestedby
},
504 'suggestions' => $full_suggestion,
505 'biblio' => $full_suggestion->{biblionumber
},
510 C4
::Letters
::EnqueueLetter
(
513 borrowernumber
=> $full_suggestion->{suggestedby
},
514 suggestionid
=> $full_suggestion->{suggestionid
},
515 LibraryName
=> C4
::Context
->preference("LibraryName"),
516 message_transport_type
=> 'email',
518 ) or warn "can't enqueue letter $letter";
521 return $status_update_table;
524 =head2 ConnectSuggestionAndBiblio
526 &ConnectSuggestionAndBiblio($ordernumber,$biblionumber)
528 connect a suggestion to an existing biblio
532 sub ConnectSuggestionAndBiblio
{
533 my ( $suggestionid, $biblionumber ) = @_;
534 my $dbh = C4
::Context
->dbh;
540 my $sth = $dbh->prepare($query);
541 $sth->execute( $biblionumber, $suggestionid );
546 &DelSuggestion($borrowernumber,$ordernumber)
548 Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
553 my ( $borrowernumber, $suggestionid, $type ) = @_;
554 my $dbh = C4
::Context
->dbh;
556 # check that the suggestion comes from the suggestor
562 my $sth = $dbh->prepare($query);
563 $sth->execute($suggestionid);
564 my ($suggestedby) = $sth->fetchrow;
565 if ( $type eq 'intranet' || $suggestedby eq $borrowernumber ) {
567 DELETE FROM suggestions
570 $sth = $dbh->prepare($queryDelete);
571 my $suggestiondeleted = $sth->execute($suggestionid);
572 return $suggestiondeleted;
576 =head2 DelSuggestionsOlderThan
577 &DelSuggestionsOlderThan($days)
579 Delete all suggestions older than TODAY-$days , that have be accepted or rejected.
583 sub DelSuggestionsOlderThan
{
586 my $dbh = C4
::Context
->dbh;
587 my $sth = $dbh->prepare(
589 DELETE FROM suggestions
590 WHERE STATUS<>'ASKED'
591 AND date < ADDDATE(NOW(), ?)
594 $sth->execute("-$days");
603 Koha Development Team <http://koha-community.org/>