+
+sub NewSuggestion {
+ my ($suggestion) = @_;
+
+ for my $field ( qw(
+ suggestedby
+ managedby
+ manageddate
+ acceptedby
+ accepteddate
+ rejectedby
+ rejecteddate
+ budgetid
+ ) ) {
+ # Set the fields to NULL if not given.
+ $suggestion->{$field} ||= undef;
+ }
+
+ $suggestion->{STATUS} = "ASKED" unless $suggestion->{STATUS};
+
+ $suggestion->{suggesteddate} = dt_from_string unless $suggestion->{suggesteddate};
+
+ my $rs = Koha::Database->new->schema->resultset('Suggestion');
+ return $rs->create($suggestion)->id;
+}
+
+=head2 ModSuggestion
+
+&ModSuggestion($suggestion)
+
+Modify the suggestion according to the hash passed by ref.
+The hash HAS to contain suggestionid
+Data not defined is not updated unless it is a note or sort1
+Send a mail to notify the user that did the suggestion.
+
+Note that there is no function to modify a suggestion.
+
+=cut
+
+sub ModSuggestion {
+ my ($suggestion) = @_;
+ return unless( $suggestion and defined($suggestion->{suggestionid}) );
+
+ for my $field ( qw(
+ suggestedby
+ managedby
+ manageddate
+ acceptedby
+ accepteddate
+ rejectedby
+ rejecteddate
+ budgetid
+ ) ) {
+ # Set the fields to NULL if not given.
+ $suggestion->{$field} = undef
+ if exists $suggestion->{$field}
+ and ($suggestion->{$field} eq '0'
+ or $suggestion->{$field} eq '' );
+ }
+
+ my $rs = Koha::Database->new->schema->resultset('Suggestion')->find($suggestion->{suggestionid});
+ my $status_update_table = 1;
+ eval {
+ $rs->update($suggestion);
+ };
+ $status_update_table = 0 if( $@ );
+
+ if ( $suggestion->{STATUS} ) {
+
+ # fetch the entire updated suggestion so that we can populate the letter
+ my $full_suggestion = GetSuggestion( $suggestion->{suggestionid} );
+ if (
+ my $letter = C4::Letters::GetPreparedLetter(
+ module => 'suggestions',
+ letter_code => $full_suggestion->{STATUS},
+ branchcode => $full_suggestion->{branchcode},
+ tables => {
+ 'branches' => $full_suggestion->{branchcode},
+ 'borrowers' => $full_suggestion->{suggestedby},
+ 'suggestions' => $full_suggestion,
+ 'biblio' => $full_suggestion->{biblionumber},
+ },
+ )
+ )
+ {
+ C4::Letters::EnqueueLetter(
+ {
+ letter => $letter,
+ borrowernumber => $full_suggestion->{suggestedby},
+ suggestionid => $full_suggestion->{suggestionid},
+ LibraryName => C4::Context->preference("LibraryName"),
+ message_transport_type => 'email',
+ }
+ ) or warn "can't enqueue letter $letter";
+ }
+ }
+ return $status_update_table;
+}
+
+=head2 ConnectSuggestionAndBiblio
+
+&ConnectSuggestionAndBiblio($ordernumber,$biblionumber)
+
+connect a suggestion to an existing biblio
+
+=cut
+
+sub ConnectSuggestionAndBiblio {
+ my ( $suggestionid, $biblionumber ) = @_;
+ my $dbh = C4::Context->dbh;
+ my $query = q{
+ UPDATE suggestions
+ SET biblionumber=?
+ WHERE suggestionid=?
+ };
+ my $sth = $dbh->prepare($query);
+ $sth->execute( $biblionumber, $suggestionid );
+}
+
+=head2 DelSuggestion
+
+&DelSuggestion($borrowernumber,$ordernumber)
+
+Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
+
+=cut
+
+sub DelSuggestion {
+ my ( $borrowernumber, $suggestionid, $type ) = @_;
+ my $dbh = C4::Context->dbh;
+
+ # check that the suggestion comes from the suggestor
+ my $query = q{
+ SELECT suggestedby
+ FROM suggestions
+ WHERE suggestionid=?
+ };
+ my $sth = $dbh->prepare($query);
+ $sth->execute($suggestionid);
+ my ($suggestedby) = $sth->fetchrow;
+ if ( $type eq 'intranet' || $suggestedby eq $borrowernumber ) {
+ my $queryDelete = q{
+ DELETE FROM suggestions
+ WHERE suggestionid=?
+ };
+ $sth = $dbh->prepare($queryDelete);
+ my $suggestiondeleted = $sth->execute($suggestionid);
+ return $suggestiondeleted;
+ }
+}
+
+=head2 DelSuggestionsOlderThan
+ &DelSuggestionsOlderThan($days)
+
+ Delete all suggestions older than TODAY-$days , that have be accepted or rejected.
+
+=cut
+
+sub DelSuggestionsOlderThan {
+ my ($days) = @_;
+ return unless $days;
+ my $dbh = C4::Context->dbh;
+ my $sth = $dbh->prepare(
+ q{
+ DELETE FROM suggestions
+ WHERE STATUS<>'ASKED'
+ AND date < ADDDATE(NOW(), ?)
+ }
+ );
+ $sth->execute("-$days");
+}
+
+sub GetUnprocessedSuggestions {
+ my ( $number_of_days_since_the_last_modification ) = @_;
+
+ $number_of_days_since_the_last_modification ||= 0;
+
+ my $dbh = C4::Context->dbh;
+
+ my $s = $dbh->selectall_arrayref(q|
+ SELECT *
+ FROM suggestions
+ WHERE STATUS = 'ASKED'
+ AND budgetid IS NOT NULL
+ AND CAST(NOW() AS DATE) - INTERVAL ? DAY = CAST(suggesteddate AS DATE)
+ |, { Slice => {} }, $number_of_days_since_the_last_modification );
+ return $s;
+}
+
+1;
+__END__
+
+
+=head1 AUTHOR
+
+Koha Development Team <http://koha-community.org/>
+
+=cut
+