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>.
21 use Test::More tests => 3;
23 use t::lib::TestBuilder;
30 use Koha::DateUtils qw( dt_from_string output_pref );
32 use DateTime::Duration;
36 my $schema = Koha::Database->schema;
37 $schema->storage->txn_begin;
38 my $dbh = C4::Context->dbh;
40 $dbh->do(q|DELETE FROM issues|);
41 $dbh->do(q|DELETE FROM borrowers|);
42 $dbh->do(q|DELETE FROM items|);
43 $dbh->do(q|DELETE FROM branches|);
44 $dbh->do(q|DELETE FROM biblio|);
45 $dbh->do(q|DELETE FROM categories|);
46 $dbh->do(q|DELETE FROM letter|);
48 my $builder = t::lib::TestBuilder->new;
50 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
51 my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
52 my $itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
55 homebranch => $branchcode,
56 holdingbranch => $branchcode,
61 my $slip_content = <<EOS;
64 Title: <<biblio.title>>
65 Barcode: <<items.barcode>>
66 Date due: <<issues.date_due>>
71 Title: <<biblio.title>>
72 Barcode: <<items.barcode>>
73 Date due: <<issues.date_due>>
78 INSERT INTO letter( module, code, branchcode, name, is_html, title, content, message_transport_type) VALUES ('circulation', 'ISSUESLIP', '', 'Issue Slip', 0, 'Issue Slip', ?, 'email')
79 |, {}, $slip_content);
81 my $quick_slip_content = <<EOS;
84 Title: <<biblio.title>>
85 Barcode: <<items.barcode>>
86 Date due: <<issues.date_due>>
91 INSERT INTO letter( module, code, branchcode, name, is_html, title, content, message_transport_type) VALUES ('circulation', 'ISSUEQSLIP', '', 'Issue Quick Slip', 0, 'Issue Quick Slip', ?, 'email')
92 |, {}, $quick_slip_content);
94 my ( $title1, $title2 ) = ( 'My title 1', 'My title 2' );
95 my ( $barcode1, $barcode2 ) = ('BC0101', 'BC0202' );
96 my $record = MARC::Record->new;
97 $record->append_fields(
103 my ($biblionumber1) = AddBiblio( $record, '' );
105 AddItem( { barcode => $barcode1, %item_infos }, $biblionumber1 );
107 $record = MARC::Record->new;
108 $record->append_fields(
114 my ($biblionumber2) = AddBiblio( $record, '' );
116 AddItem( { barcode => $barcode2, %item_infos }, $biblionumber2 );
119 AddMember( categorycode => $categorycode, branchcode => $branchcode );
120 my $borrower = GetMember( borrowernumber => $borrowernumber );
122 my $module = new Test::MockModule('C4::Context');
123 $module->mock( 'userenv', sub { { branch => $branchcode } } );
125 my $today = dt_from_string;
126 my $yesterday = dt_from_string->subtract_duration( DateTime::Duration->new( days => 1 ) );
128 subtest 'Issue slip' => sub {
131 subtest 'Empty slip' => sub {
133 my $slip = IssueSlip( $branchcode, $borrowernumber );
134 my $empty_slip = <<EOS;
141 is( $slip->{content}, $empty_slip, 'No checked out or overdues return an empty slip, it should return undef (FIXME)' );
144 subtest 'Daily loans' => sub {
146 skip "It's 23:59!", 2 if $today->hour == 23 and $today->minute == 59;
148 my $today_daily = $today->clone->set( hour => 23, minute => 59 );
149 my $today_daily_as_formatted = output_pref( $today_daily );
150 my $yesterday_daily = $yesterday->clone->set( hour => 23, minute => 59 );
151 my $yesterday_daily_as_formatted = output_pref( $yesterday_daily );
153 my ( $date_due, $issue_date, $slip, $expected_slip );
154 $date_due = $today_daily;
155 $issue_date = $today_daily->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
156 AddIssue( $borrower, $barcode1, $date_due, undef, $issue_date );
157 $date_due = $today_daily;
158 $issue_date = $yesterday_daily;
159 AddIssue( $borrower, $barcode2, $date_due, undef, $issue_date );
161 $expected_slip = <<EOS;
166 Date due: $today_daily_as_formatted
171 Date due: $today_daily_as_formatted
177 $slip = IssueSlip( $branchcode, $borrowernumber );
178 is( $slip->{content}, $expected_slip , 'IssueSlip should return a slip with 2 checkouts');
180 AddReturn( $barcode1, $branchcode );
181 AddReturn( $barcode2, $branchcode );
184 $date_due = $today_daily;
185 $issue_date = $today_daily->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
186 AddIssue( $borrower, $barcode1, $date_due, undef, $issue_date );
187 $date_due = $yesterday_daily;
188 $issue_date = $yesterday_daily;
189 AddIssue( $borrower, $barcode2, $date_due, undef, $issue_date );
191 $expected_slip = <<EOS;
196 Date due: $today_daily_as_formatted
203 Date due: $yesterday_daily_as_formatted
206 $slip = IssueSlip( $branchcode, $borrowernumber );
207 is( $slip->{content}, $expected_slip, 'IssueSlip should return a slip with 1 checkout and 1 overdue');
209 AddReturn( $barcode1, $branchcode );
210 AddReturn( $barcode2, $branchcode );
213 subtest 'Hourly loans' => sub {
215 skip "It's 23:59!", 2 if $today->hour == 23 and $today->minute == 59;
217 my ( $date_due_in_time, $date_due_in_time_as_formatted, $date_due_in_late, $date_due_in_late_as_formatted, $issue_date, $slip, $expected_slip );
218 # Assuming today is not hour = 23 and minute = 59
219 $date_due_in_time = $today->clone->set(hour => ($today->hour < 23 ? $today->hour + 1 : 23), minute => 59);
220 $date_due_in_time_as_formatted = output_pref( $date_due_in_time );
221 $issue_date = $date_due_in_time->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
222 AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
223 $issue_date = $yesterday->clone;
224 AddIssue( $borrower, $barcode2, $date_due_in_time, undef, $issue_date );
226 $expected_slip = <<EOS;
231 Date due: $date_due_in_time_as_formatted
236 Date due: $date_due_in_time_as_formatted
242 $slip = IssueSlip( $branchcode, $borrowernumber );
243 is( $slip->{content}, $expected_slip, 'IssueSlip should return a slip with 2 checkouts' );
245 AddReturn( $barcode1, $branchcode );
246 AddReturn( $barcode2, $branchcode );
249 $date_due_in_time = $today->clone->set(hour => ($today->hour < 23 ? $today->hour + 1 : 23), minute => 59);
250 $date_due_in_time_as_formatted = output_pref( $date_due_in_time );
251 $issue_date = $date_due_in_time->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
252 AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
253 $date_due_in_late = $today->clone->subtract( hours => 1 );
254 $date_due_in_late_as_formatted = output_pref( $date_due_in_late );
255 $issue_date = $yesterday->clone;
256 AddIssue( $borrower, $barcode2, $date_due_in_late, undef, $issue_date );
258 $expected_slip = <<EOS;
263 Date due: $date_due_in_time_as_formatted
270 Date due: $date_due_in_late_as_formatted
273 $slip = IssueSlip( $branchcode, $borrowernumber );
274 is( $slip->{content}, $expected_slip, 'IssueSlip should return a slip with 1 checkout and 1 overdue' );
276 AddReturn( $barcode1, $branchcode );
277 AddReturn( $barcode2, $branchcode );
282 subtest 'Quick slip' => sub {
285 subtest 'Empty slip' => sub {
287 my $slip = IssueSlip( $branchcode, $borrowernumber, 'quick slip' );
288 my $empty_slip = <<EOS;
292 is( $slip->{content}, $empty_slip, 'No checked out or overdues return an empty slip, it should return undef (FIXME)' );
295 subtest 'Daily loans' => sub {
297 skip "It's 23:59!", 2 if $today->hour == 23 and $today->minute == 59;
299 my $today_daily = $today->clone->set( hour => 23, minute => 59 );
300 my $today_daily_as_formatted = output_pref( $today_daily );
301 my $yesterday_daily = $yesterday->clone->set( hour => 23, minute => 59 );
302 my $yesterday_daily_as_formatted = output_pref( $yesterday_daily );
304 my ( $date_due, $issue_date, $slip, $expected_slip );
305 $date_due = $today_daily;
306 $issue_date = $today_daily->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
307 AddIssue( $borrower, $barcode1, $date_due, undef, $issue_date );
308 $date_due = $today_daily;
309 $issue_date = $yesterday_daily;
310 AddIssue( $borrower, $barcode2, $date_due, undef, $issue_date );
312 $expected_slip = <<EOS;
317 Date due: $today_daily_as_formatted
320 $slip = IssueSlip( $branchcode, $borrowernumber, 'quick slip' );
321 is( $slip->{content}, $expected_slip, 'IssueSlip should return 2 checkouts for today');
323 AddReturn( $barcode1, $branchcode );
324 AddReturn( $barcode2, $branchcode );
327 $date_due = $today_daily;
328 $issue_date = $today_daily->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
329 AddIssue( $borrower, $barcode1, $date_due, undef, $issue_date );
330 $date_due = $yesterday_daily;
331 $issue_date = $yesterday_daily;
332 AddIssue( $borrower, $barcode2, $date_due, undef, $issue_date );
334 $expected_slip = <<EOS;
339 Date due: $today_daily_as_formatted
342 $slip = IssueSlip( $branchcode, $borrowernumber, 'quickslip' );
343 is( $slip->{content}, $expected_slip );
346 subtest 'Hourly loans' => sub {
349 my ( $date_due_in_time, $date_due_in_time_as_formatted, $date_due_in_late, $date_due_in_late_as_formatted, $issue_date, $slip, $expected_slip );
350 # Assuming today is not hour = 23 and minute = 59
351 $date_due_in_time = $today->clone->set(hour => ($today->hour < 23 ? $today->hour + 1 : 23), minute => 59);
352 $date_due_in_time_as_formatted = output_pref( $date_due_in_time );
353 $issue_date = $date_due_in_time->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
354 AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
355 $issue_date = $yesterday->clone;
356 AddIssue( $borrower, $barcode2, $date_due_in_time, undef, $issue_date );
358 $expected_slip = <<EOS;
363 Date due: $date_due_in_time_as_formatted
366 $slip = IssueSlip( $branchcode, $borrowernumber, 'quickslip' );
367 is( $slip->{content}, $expected_slip );
369 AddReturn( $barcode1, $branchcode );
370 AddReturn( $barcode2, $branchcode );
373 $date_due_in_time = $today->clone->set(hour => ($today->hour < 23 ? $today->hour + 1 : 23), minute => 59);
374 $date_due_in_time_as_formatted = output_pref( $date_due_in_time );
375 $issue_date = $date_due_in_time->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
376 AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
377 $date_due_in_late = $today->clone->subtract( hours => 1 );
378 $date_due_in_late_as_formatted = output_pref( $date_due_in_late );
379 $issue_date = $yesterday->clone;
380 AddIssue( $borrower, $barcode2, $date_due_in_late, undef, $issue_date );
382 $expected_slip = <<EOS;
387 Date due: $date_due_in_time_as_formatted
390 $slip = IssueSlip( $branchcode, $borrowernumber, 'quickslip' );
391 is( $slip->{content}, $expected_slip );
393 AddReturn( $barcode1, $branchcode );
394 AddReturn( $barcode2, $branchcode );
399 subtest 'bad calls' => sub {
401 AddIssue( $borrower, $barcode1, $today, undef, $yesterday );
402 my $slip = IssueSlip();
403 isnt( $slip, undef, 'IssueSlip should return if no param passed FIXME, should return undef' );
404 my $empty_slip = <<EOS;
412 $slip = IssueSlip(undef, $borrowernumber+1);
413 is( $slip->{content}, $empty_slip, 'IssueSlip should not return an empty slip if the borrowernumber passed in param does not exist. But it is what it does for now (FIXME)' );
416 $schema->storage->txn_rollback;