3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use C4::Budgets qw( AddBudgetPeriod AddBudget );
25 use Koha::DateUtils qw( dt_from_string );
29 use DateTime::Duration;
30 use Test::More tests => 105;
34 use_ok('C4::Suggestions');
38 my $dbh = C4::Context->dbh;
42 $dbh->{AutoCommit} = 0;
43 $dbh->{RaiseError} = 1;
45 # Reset item types to only the default ones
46 $dbh->do(q|DELETE FROM itemtypes;|);
48 INSERT INTO itemtypes (itemtype, description, rentalcharge, notforloan, imageurl, summary) VALUES
49 ('BK', 'Books',5,0,'bridge/book.gif',''),
50 ('MX', 'Mixed Materials',5,0,'bridge/kit.gif',''),
51 ('CF', 'Computer Files',5,0,'bridge/computer_file.gif',''),
52 ('MP', 'Maps',5,0,'bridge/map.gif',''),
53 ('VM', 'Visual Materials',5,1,'bridge/dvd.gif',''),
54 ('MU', 'Music',5,0,'bridge/sound.gif',''),
55 ('CR', 'Continuing Resources',5,0,'bridge/periodical.gif',''),
56 ('REF', 'Reference',0,1,'bridge/reference.gif','');";
58 $dbh->do(q|DELETE FROM suggestions|);
59 $dbh->do(q|DELETE FROM issues|);
60 $dbh->do(q|DELETE FROM borrowers|);
61 $dbh->do(q|DELETE FROM letter|);
62 $dbh->do(q|DELETE FROM message_queue|);
63 $dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|);
66 if (not defined Koha::Libraries->find('CPL')) {
67 Koha::Library->new({ branchcode => 'CPL', branchname => 'Centerville' })->store;
70 my $sth = $dbh->prepare("SELECT * FROM categories WHERE categorycode='S';");
72 if (!$sth->fetchrow_hashref) {
73 $sql = "INSERT INTO categories
74 (categorycode,description,enrolmentperiod,upperagelimit,
75 dateofbirthrequired,finetype,bulk,enrolmentfee,
76 overduenoticerequired,issuelimit,reservefee,category_type)
79 18,NULL,NULL,'0.000000',
80 0,NULL,'0.000000','S');";
85 firstname => 'my firstname',
86 surname => 'my surname',
90 my $borrowernumber = AddMember(%$member);
92 my $biblionumber1 = 1;
95 author => 'my author',
96 publishercode => 'my publishercode',
97 suggestedby => $borrowernumber,
98 biblionumber => $biblionumber1,
101 accepteddate => dt_from_string,
105 my $budgetperiod_id = AddBudgetPeriod({
106 budget_period_startdate => '2008-01-01',
107 budget_period_enddate => '2008-12-31',
108 budget_period_description => 'MAPERI',
109 budget_period_active => 1,
112 my $budget_id = AddBudget({
113 budget_code => 'ABCD',
114 budget_amount => '123.132000',
115 budget_name => 'ABCD',
116 budget_notes => 'This is a note',
117 budget_period_id => $budgetperiod_id,
119 my $my_suggestion_with_budget = {
120 title => 'my title 2',
121 author => 'my author 2',
122 publishercode => 'my publishercode 2',
123 suggestedby => $borrowernumber,
124 biblionumber => $biblionumber1,
127 accepteddate => dt_from_string,
129 budgetid => $budget_id,
133 is( CountSuggestion(), 0, 'CountSuggestion without the status returns 0' );
134 is( CountSuggestion('ASKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
135 is( CountSuggestion('CHECKED'), 0, 'CountSuggestion returns the correct number of suggestions' );
136 is( CountSuggestion('ACCEPTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
137 is( CountSuggestion('REJECTED'), 0, 'CountSuggestion returns the correct number of suggestions' );
139 my $my_suggestionid = NewSuggestion($my_suggestion);
140 isnt( $my_suggestionid, 0, 'NewSuggestion returns an not null id' );
141 my $my_suggestionid_with_budget = NewSuggestion($my_suggestion_with_budget);
143 is( GetSuggestion(), undef, 'GetSuggestion without the suggestion id returns undef' );
144 my $suggestion = GetSuggestion($my_suggestionid);
145 is( $suggestion->{title}, $my_suggestion->{title}, 'NewSuggestion stores the title correctly' );
146 is( $suggestion->{author}, $my_suggestion->{author}, 'NewSuggestion stores the author correctly' );
147 is( $suggestion->{publishercode}, $my_suggestion->{publishercode}, 'NewSuggestion stores the publishercode correctly' );
148 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'NewSuggestion stores the borrower number correctly' );
149 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'NewSuggestion stores the biblio number correctly' );
150 is( $suggestion->{STATUS}, 'ASKED', 'NewSuggestion stores a suggestion with the status ASKED by default' );
151 is( $suggestion->{managedby}, undef, 'NewSuggestion stores empty string as undef for non existent foreign key (integer)' );
152 is( $suggestion->{manageddate}, undef, 'NewSuggestion stores empty string as undef for date' );
153 is( $suggestion->{budgetid}, undef, 'NewSuggestion should set budgetid to NULL if not given' );
155 is( CountSuggestion('ASKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
158 is( ModSuggestion(), undef, 'ModSuggestion without the suggestion returns undef' );
159 my $mod_suggestion1 = {
160 title => 'my modified title',
161 author => 'my modified author',
162 publishercode => 'my modified publishercode',
166 my $status = ModSuggestion($mod_suggestion1);
167 is( $status, undef, 'ModSuggestion without the suggestion id returns undef' );
169 $mod_suggestion1->{suggestionid} = $my_suggestionid;
170 $status = ModSuggestion($mod_suggestion1);
171 is( $status, 1, 'ModSuggestion modifies one entry' );
172 $suggestion = GetSuggestion($my_suggestionid);
173 is( $suggestion->{title}, $mod_suggestion1->{title}, 'ModSuggestion modifies the title correctly' );
174 is( $suggestion->{author}, $mod_suggestion1->{author}, 'ModSuggestion modifies the author correctly' );
175 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'ModSuggestion modifies the publishercode correctly' );
176 is( $suggestion->{managedby}, undef, 'ModSuggestion stores empty string as undef for non existent foreign key (integer)' );
177 is( $suggestion->{manageddate}, undef, 'ModSuggestion stores empty string as undef for date' );
178 isnt( $suggestion->{accepteddate}, undef, 'ModSuggestion does not update a non given date value' );
179 is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not given' );
181 my $messages = C4::Letters::GetQueuedMessages({
182 borrowernumber => $borrowernumber,
184 is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
186 my $mod_suggestion2 = {
188 suggestionid => $my_suggestionid,
190 warning_is { $status = ModSuggestion($mod_suggestion2) }
191 "No suggestions STALLED letter transported by email",
192 "ModSuggestion status warning is correct";
193 is( $status, 1, "ModSuggestion Status OK");
195 my $mod_suggestion3 = {
197 suggestionid => $my_suggestionid,
199 $status = ModSuggestion($mod_suggestion3);
201 is( $status, 1, 'ModSuggestion modifies one entry' );
202 $suggestion = GetSuggestion($my_suggestionid);
203 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
204 $messages = C4::Letters::GetQueuedMessages({
205 borrowernumber => $borrowernumber,
207 is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
209 is( CountSuggestion('CHECKED'), 1, 'CountSuggestion returns the correct number of suggestions' );
212 is( GetSuggestionInfo(), undef, 'GetSuggestionInfo without the suggestion id returns undef' );
213 $suggestion = GetSuggestionInfo($my_suggestionid);
214 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfo returns the suggestion id correctly' );
215 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfo returns the title correctly' );
216 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfo returns the author correctly' );
217 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfo returns the publisher code correctly' );
218 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
219 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfo returns the biblio number correctly' );
220 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfo returns the status correctly' );
221 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfo returns the surname correctly' );
222 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfo returns the firstname correctly' );
223 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfo returns the borrower number correctly' );
226 is( GetSuggestionFromBiblionumber(), undef, 'GetSuggestionFromBiblionumber without the biblio number returns undef' );
227 is( GetSuggestionFromBiblionumber(2), undef, 'GetSuggestionFromBiblionumber with an invalid biblio number returns undef' );
228 is( GetSuggestionFromBiblionumber($biblionumber1), $my_suggestionid, 'GetSuggestionFromBiblionumber functions correctly' );
231 is( GetSuggestionInfoFromBiblionumber(), undef, 'GetSuggestionInfoFromBiblionumber without the biblio number returns undef' );
232 is( GetSuggestionInfoFromBiblionumber(2), undef, 'GetSuggestionInfoFromBiblionumber with an invalid biblio number returns undef' );
233 $suggestion = GetSuggestionInfoFromBiblionumber($biblionumber1);
234 is( $suggestion->{suggestionid}, $my_suggestionid, 'GetSuggestionInfoFromBiblionumber returns the suggestion id correctly' );
235 is( $suggestion->{title}, $mod_suggestion1->{title}, 'GetSuggestionInfoFromBiblionumber returns the title correctly' );
236 is( $suggestion->{author}, $mod_suggestion1->{author}, 'GetSuggestionInfoFromBiblionumber returns the author correctly' );
237 is( $suggestion->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionInfoFromBiblionumber returns the publisher code correctly' );
238 is( $suggestion->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumber returns the borrower number correctly' );
239 is( $suggestion->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionInfoFromBiblionumber returns the biblio number correctly' );
240 is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionInfoFromBiblionumber returns the status correctly' );
241 is( $suggestion->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionInfoFromBiblionumber returns the surname correctly' );
242 is( $suggestion->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionInfoFromBiblionumber returns the firstname correctly' );
243 is( $suggestion->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionInfoFromBiblionumeber returns the borrower number correctly' );
246 my $suggestions = GetSuggestionByStatus();
247 is( @$suggestions, 0, 'GetSuggestionByStatus without the status returns an empty array' );
248 $suggestions = GetSuggestionByStatus('CHECKED');
249 is( @$suggestions, 1, 'GetSuggestionByStatus returns the correct number of suggestions' );
250 is( $suggestions->[0]->{suggestionid}, $my_suggestionid, 'GetSuggestionByStatus returns the suggestion id correctly' );
251 is( $suggestions->[0]->{title}, $mod_suggestion1->{title}, 'GetSuggestionByStatus returns the title correctly' );
252 is( $suggestions->[0]->{author}, $mod_suggestion1->{author}, 'GetSuggestionByStatus returns the author correctly' );
253 is( $suggestions->[0]->{publishercode}, $mod_suggestion1->{publishercode}, 'GetSuggestionByStatus returns the publisher code correctly' );
254 is( $suggestions->[0]->{suggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
255 is( $suggestions->[0]->{biblionumber}, $my_suggestion->{biblionumber}, 'GetSuggestionByStatus returns the biblio number correctly' );
256 is( $suggestions->[0]->{STATUS}, $mod_suggestion3->{STATUS}, 'GetSuggestionByStatus returns the status correctly' );
257 is( $suggestions->[0]->{surnamesuggestedby}, $member->{surname}, 'GetSuggestionByStatus returns the surname correctly' );
258 is( $suggestions->[0]->{firstnamesuggestedby}, $member->{firstname}, 'GetSuggestionByStatus returns the firstname correctly' );
259 is( $suggestions->[0]->{branchcodesuggestedby}, $member->{branchcode}, 'GetSuggestionByStatus returns the branch code correctly' );
260 is( $suggestions->[0]->{borrnumsuggestedby}, $my_suggestion->{suggestedby}, 'GetSuggestionByStatus returns the borrower number correctly' );
261 is( $suggestions->[0]->{categorycodesuggestedby}, $member->{categorycode}, 'GetSuggestionByStatus returns the category code correctly' );
264 is( ConnectSuggestionAndBiblio(), '0E0', 'ConnectSuggestionAndBiblio without arguments returns 0E0' );
265 my $biblionumber2 = 2;
266 my $connect_suggestion_and_biblio = ConnectSuggestionAndBiblio($my_suggestionid, $biblionumber2);
267 is( $connect_suggestion_and_biblio, '1', 'ConnectSuggestionAndBiblio returns 1' );
268 $suggestion = GetSuggestion($my_suggestionid);
269 is( $suggestion->{biblionumber}, $biblionumber2, 'ConnectSuggestionAndBiblio updates the biblio number correctly' );
271 my $search_suggestion = SearchSuggestion();
272 is( @$search_suggestion, 2, 'SearchSuggestion without arguments returns all suggestions' );
274 $search_suggestion = SearchSuggestion({
275 title => $mod_suggestion1->{title},
277 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
278 $search_suggestion = SearchSuggestion({
279 title => 'another title',
281 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
283 $search_suggestion = SearchSuggestion({
284 author => $mod_suggestion1->{author},
286 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
287 $search_suggestion = SearchSuggestion({
288 author => 'another author',
290 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
292 $search_suggestion = SearchSuggestion({
293 publishercode => $mod_suggestion1->{publishercode},
295 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
296 $search_suggestion = SearchSuggestion({
297 publishercode => 'another publishercode',
299 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
301 $search_suggestion = SearchSuggestion({
302 STATUS => $mod_suggestion3->{STATUS},
304 is( @$search_suggestion, 1, 'SearchSuggestion returns the correct number of suggestions' );
305 $search_suggestion = SearchSuggestion({
306 STATUS => 'REJECTED',
308 is( @$search_suggestion, 0, 'SearchSuggestion returns the correct number of suggestions' );
310 $search_suggestion = SearchSuggestion({
313 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "") returns the correct number of suggestions' );
314 $search_suggestion = SearchSuggestion({
315 budgetid => $budget_id,
317 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = $budgetid) returns the correct number of suggestions' );
318 $search_suggestion = SearchSuggestion({
319 budgetid => '__NONE__',
321 is( @$search_suggestion, 1, 'SearchSuggestion (budgetid = "__NONE__") returns the correct number of suggestions' );
322 $search_suggestion = SearchSuggestion({
323 budgetid => '__ANY__',
325 is( @$search_suggestion, 2, 'SearchSuggestion (budgetid = "__ANY__") returns the correct number of suggestions' );
327 my $del_suggestion = {
328 title => 'my deleted title',
330 suggestedby => $borrowernumber,
332 my $del_suggestionid = NewSuggestion($del_suggestion);
334 is( CountSuggestion('CHECKED'), 2, 'CountSuggestion returns the correct number of suggestions' );
336 is( DelSuggestion(), '0E0', 'DelSuggestion without arguments returns 0E0' );
337 is( DelSuggestion($borrowernumber), '', 'DelSuggestion without the suggestion id returns an empty string' );
338 is( DelSuggestion(undef, $my_suggestionid), '', 'DelSuggestion with an invalid borrower number returns an empty string' );
339 $suggestion = DelSuggestion($borrowernumber, $my_suggestionid);
340 is( $suggestion, 1, 'DelSuggestion deletes one suggestion' );
342 $suggestions = GetSuggestionByStatus('CHECKED');
343 is( @$suggestions, 1, 'DelSuggestion deletes one suggestion' );
344 is( $suggestions->[0]->{title}, $del_suggestion->{title}, 'DelSuggestion deletes the correct suggestion' );
346 ## Bug 11466, making sure GetSupportList() returns itemtypes, even if AdvancedSearchTypes has multiple values
347 C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes|loc|ccode');
348 my $itemtypes1 = C4::Koha::GetSupportList();
349 is(@$itemtypes1, 8, "Purchase suggestion itemtypes collected, multiple AdvancedSearchTypes");
351 C4::Context->set_preference("AdvancedSearchTypes", 'itemtypes');
352 my $itemtypes2 = C4::Koha::GetSupportList();
353 is(@$itemtypes2, 8, "Purchase suggestion itemtypes collected, default AdvancedSearchTypes");
355 is_deeply($itemtypes1, $itemtypes2, 'same set of purchase suggestion formats retrieved');
358 $my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB
359 my $my_suggestionid_test_budgetid = NewSuggestion($my_suggestion);
360 $suggestion = GetSuggestion($my_suggestionid_test_budgetid);
361 is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
363 $my_suggestion->{budgetid} = ''; # If budgetid == '', NULL should be set in DB
364 ModSuggestion( $my_suggestion );
365 $suggestion = GetSuggestion($my_suggestionid_test_budgetid);
366 is( $suggestion->{budgetid}, undef, 'NewSuggestion Should set budgetid to NULL if equals an empty string' );
368 subtest 'GetUnprocessedSuggestions' => sub {
370 $dbh->do(q|DELETE FROM suggestions|);
371 my $my_suggestionid = NewSuggestion($my_suggestion);
372 my $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
373 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return 0 if a suggestion has been processed but not linked to a fund' );
374 my $status = ModSuggestion($mod_suggestion1);
375 my $suggestion = GetSuggestion($my_suggestionid);
376 is( $suggestion->{budgetid}, undef, 'ModSuggestion should set budgetid to NULL if not given' );
377 ModSuggestion( { suggestionid => $my_suggestionid, budgetid => $budget_id } );
378 $suggestion = GetSuggestion($my_suggestionid);
379 is( $suggestion->{budgetid}, $budget_id, 'ModSuggestion should modify budgetid if given' );
381 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
382 is( scalar(@$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
384 warning_is { ModSuggestion( { suggestionid => $my_suggestionid, STATUS => 'REJECTED' } ) }
385 'No suggestions REJECTED letter transported by email',
386 'Warning raised if no REJECTED letter by email';
387 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
388 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should return the suggestion if the suggestion is linked to a fund and has not been processed yet' );
390 warning_is { ModSuggestion( { suggestionid => $my_suggestionid, STATUS => 'ASKED', suggesteddate => dt_from_string->add_duration( DateTime::Duration->new( days => -4 ) ) } ); }
391 'No suggestions ASKED letter transported by email',
392 'Warning raised if no ASKED letter by email';
393 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions;
394 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should use 0 as default value for days' );
395 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(4);
396 is( scalar(@$unprocessed_suggestions), 1, 'GetUnprocessedSuggestions should return the suggestion suggested 4 days ago' );
397 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(3);
398 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 3 days ago' );
399 $unprocessed_suggestions = C4::Suggestions::GetUnprocessedSuggestions(5);
400 is( scalar(@$unprocessed_suggestions), 0, 'GetUnprocessedSuggestions should not return the suggestion, it has not been suggested 5 days ago' );