5 use Test
::More tests
=> 3;
13 use Koha
::DateUtils
qw( dt_from_string output_pref );
15 use DateTime
::Duration
;
19 my $dbh = C4
::Context
->dbh;
20 $dbh->{AutoCommit
} = 0;
21 $dbh->{RaiseError
} = 1;
23 $dbh->do(q
|DELETE FROM issues
|);
24 $dbh->do(q
|DELETE FROM borrowers
|);
25 $dbh->do(q
|DELETE FROM items
|);
26 $dbh->do(q
|DELETE FROM branches
|);
27 $dbh->do(q
|DELETE FROM biblio
|);
28 $dbh->do(q
|DELETE FROM categories
|);
29 $dbh->do(q
|DELETE FROM letter
|);
32 Koha
::Library
->new( { branchcode
=> $branchcode, branchname
=> 'Branch' } )->store;
34 my $categorycode = 'C';
35 $dbh->do( "INSERT INTO categories(categorycode) VALUES(?)",
36 undef, $categorycode );
38 my %item_branch_infos = (
39 homebranch
=> $branchcode,
40 holdingbranch
=> $branchcode,
43 my $slip_content = <<EOS;
46 Title: <<biblio.title>>
47 Barcode: <<items.barcode>>
48 Date due: <<issues.date_due>>
53 Title: <<biblio.title>>
54 Barcode: <<items.barcode>>
55 Date due: <<issues.date_due>>
60 INSERT INTO letter( module, code, branchcode, name, is_html, title, content, message_transport_type) VALUES ('circulation', 'ISSUESLIP', '', 'Issue Slip', 0, 'Issue Slip', ?, 'email')
61 |, {}, $slip_content);
63 my $quick_slip_content = <<EOS;
66 Title
: <<biblio.title>>
67 Barcode: <<items.barcode>>
68 Date due: <<issues.date_due>>
73 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')
74 |, {}, $quick_slip_content);
76 my ( $title1, $title2 ) = ( 'My title 1', 'My title 2' );
77 my ( $barcode1, $barcode2 ) = ('BC0101', 'BC0202' );
78 my $record = MARC::Record->new;
79 $record->append_fields(
85 my ($biblionumber1) = AddBiblio( $record, '' );
87 AddItem( { barcode => $barcode1, %item_branch_infos }, $biblionumber1 );
89 $record = MARC::Record->new;
90 $record->append_fields(
96 my ($biblionumber2) = AddBiblio( $record, '' );
98 AddItem( { barcode => $barcode2, %item_branch_infos }, $biblionumber2 );
101 AddMember( categorycode => $categorycode, branchcode => $branchcode );
102 my $borrower = GetMember( borrowernumber => $borrowernumber );
104 my $module = new Test::MockModule('C4::Context');
105 $module->mock( 'userenv', sub { { branch => $branchcode } } );
107 my $today = dt_from_string;
108 my $yesterday = dt_from_string->subtract_duration( DateTime::Duration->new( days => 1 ) );
110 subtest 'Issue slip' => sub {
113 subtest 'Empty slip' => sub {
115 my $slip = IssueSlip( $branchcode, $borrowernumber );
116 my $empty_slip = <<EOS;
123 is
( $slip->{content
}, $empty_slip, 'No checked out or overdues return an empty slip, it should return undef (FIXME)' );
126 subtest
'Daily loans' => sub {
128 skip
"It's 23:59!", 2 if $today->hour == 23 and $today->minute == 59;
130 my $today_daily = $today->clone->set( hour
=> 23, minute
=> 59 );
131 my $today_daily_as_formatted = output_pref
( $today_daily );
132 my $yesterday_daily = $yesterday->clone->set( hour
=> 23, minute
=> 59 );
133 my $yesterday_daily_as_formatted = output_pref
( $yesterday_daily );
135 my ( $date_due, $issue_date, $slip, $expected_slip );
136 $date_due = $today_daily;
137 $issue_date = $today_daily->clone->subtract_duration( DateTime
::Duration
->new( minutes
=> 1 ) );
138 AddIssue
( $borrower, $barcode1, $date_due, undef, $issue_date );
139 $date_due = $today_daily;
140 $issue_date = $yesterday_daily;
141 AddIssue
( $borrower, $barcode2, $date_due, undef, $issue_date );
143 $expected_slip = <<EOS;
148 Date due: $today_daily_as_formatted
153 Date due: $today_daily_as_formatted
159 $slip = IssueSlip
( $branchcode, $borrowernumber );
160 is
( $slip->{content
}, $expected_slip , 'IssueSlip should return a slip with 2 checkouts');
162 AddReturn
( $barcode1, $branchcode );
163 AddReturn
( $barcode2, $branchcode );
166 $date_due = $today_daily;
167 $issue_date = $today_daily->clone->subtract_duration( DateTime
::Duration
->new( minutes
=> 1 ) );
168 AddIssue
( $borrower, $barcode1, $date_due, undef, $issue_date );
169 $date_due = $yesterday_daily;
170 $issue_date = $yesterday_daily;
171 AddIssue
( $borrower, $barcode2, $date_due, undef, $issue_date );
173 $expected_slip = <<EOS;
178 Date due: $today_daily_as_formatted
185 Date due: $yesterday_daily_as_formatted
188 $slip = IssueSlip
( $branchcode, $borrowernumber );
189 is
( $slip->{content
}, $expected_slip, 'IssueSlip should return a slip with 1 checkout and 1 overdue');
191 AddReturn
( $barcode1, $branchcode );
192 AddReturn
( $barcode2, $branchcode );
195 subtest
'Hourly loans' => sub {
197 skip
"It's 23:59!", 2 if $today->hour == 23 and $today->minute == 59;
199 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 );
200 # Assuming today is not hour = 23 and minute = 59
201 $date_due_in_time = $today->clone->set(hour
=> ($today->hour < 23 ?
$today->hour + 1 : 23), minute
=> 59);
202 $date_due_in_time_as_formatted = output_pref
( $date_due_in_time );
203 $issue_date = $date_due_in_time->clone->subtract_duration( DateTime
::Duration
->new( minutes
=> 1 ) );
204 AddIssue
( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
205 $issue_date = $yesterday->clone;
206 AddIssue
( $borrower, $barcode2, $date_due_in_time, undef, $issue_date );
208 $expected_slip = <<EOS;
213 Date due: $date_due_in_time_as_formatted
218 Date due: $date_due_in_time_as_formatted
224 $slip = IssueSlip
( $branchcode, $borrowernumber );
225 is
( $slip->{content
}, $expected_slip, 'IssueSlip should return a slip with 2 checkouts' );
227 AddReturn
( $barcode1, $branchcode );
228 AddReturn
( $barcode2, $branchcode );
231 $date_due_in_time = $today->clone->set(hour
=> ($today->hour < 23 ?
$today->hour + 1 : 23), minute
=> 59);
232 $date_due_in_time_as_formatted = output_pref
( $date_due_in_time );
233 $issue_date = $date_due_in_time->clone->subtract_duration( DateTime
::Duration
->new( minutes
=> 1 ) );
234 AddIssue
( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
235 $date_due_in_late = $today->clone->set(hour
=> $today->hour - 1);
236 $date_due_in_late_as_formatted = output_pref
( $date_due_in_late );
237 $issue_date = $yesterday->clone;
238 AddIssue
( $borrower, $barcode2, $date_due_in_late, undef, $issue_date );
240 $expected_slip = <<EOS;
245 Date due: $date_due_in_time_as_formatted
252 Date due: $date_due_in_late_as_formatted
255 $slip = IssueSlip
( $branchcode, $borrowernumber );
256 is
( $slip->{content
}, $expected_slip, 'IssueSlip should return a slip with 1 checkout and 1 overdue' );
258 AddReturn
( $barcode1, $branchcode );
259 AddReturn
( $barcode2, $branchcode );
264 subtest
'Quick slip' => sub {
267 subtest
'Empty slip' => sub {
269 my $slip = IssueSlip
( $branchcode, $borrowernumber, 'quick slip' );
270 my $empty_slip = <<EOS;
274 is
( $slip->{content
}, $empty_slip, 'No checked out or overdues return an empty slip, it should return undef (FIXME)' );
277 subtest
'Daily loans' => sub {
279 skip
"It's 23:59!", 2 if $today->hour == 23 and $today->minute == 59;
281 my $today_daily = $today->clone->set( hour
=> 23, minute
=> 59 );
282 my $today_daily_as_formatted = output_pref
( $today_daily );
283 my $yesterday_daily = $yesterday->clone->set( hour
=> 23, minute
=> 59 );
284 my $yesterday_daily_as_formatted = output_pref
( $yesterday_daily );
286 my ( $date_due, $issue_date, $slip, $expected_slip );
287 $date_due = $today_daily;
288 $issue_date = $today_daily->clone->subtract_duration( DateTime
::Duration
->new( minutes
=> 1 ) );
289 AddIssue
( $borrower, $barcode1, $date_due, undef, $issue_date );
290 $date_due = $today_daily;
291 $issue_date = $yesterday_daily;
292 AddIssue
( $borrower, $barcode2, $date_due, undef, $issue_date );
294 $expected_slip = <<EOS;
299 Date due: $today_daily_as_formatted
302 $slip = IssueSlip
( $branchcode, $borrowernumber, 'quick slip' );
303 is
( $slip->{content
}, $expected_slip, 'IssueSlip should return 2 checkouts for today');
305 AddReturn
( $barcode1, $branchcode );
306 AddReturn
( $barcode2, $branchcode );
309 $date_due = $today_daily;
310 $issue_date = $today_daily->clone->subtract_duration( DateTime
::Duration
->new( minutes
=> 1 ) );
311 AddIssue
( $borrower, $barcode1, $date_due, undef, $issue_date );
312 $date_due = $yesterday_daily;
313 $issue_date = $yesterday_daily;
314 AddIssue
( $borrower, $barcode2, $date_due, undef, $issue_date );
316 $expected_slip = <<EOS;
321 Date due: $today_daily_as_formatted
324 $slip = IssueSlip
( $branchcode, $borrowernumber, 'quickslip' );
325 is
( $slip->{content
}, $expected_slip );
328 subtest
'Hourly loans' => sub {
331 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 );
332 # Assuming today is not hour = 23 and minute = 59
333 $date_due_in_time = $today->clone->set(hour
=> ($today->hour < 23 ?
$today->hour + 1 : 23), minute
=> 59);
334 $date_due_in_time_as_formatted = output_pref
( $date_due_in_time );
335 $issue_date = $date_due_in_time->clone->subtract_duration( DateTime
::Duration
->new( minutes
=> 1 ) );
336 AddIssue
( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
337 $issue_date = $yesterday->clone;
338 AddIssue
( $borrower, $barcode2, $date_due_in_time, undef, $issue_date );
340 $expected_slip = <<EOS;
345 Date due: $date_due_in_time_as_formatted
348 $slip = IssueSlip
( $branchcode, $borrowernumber, 'quickslip' );
349 is
( $slip->{content
}, $expected_slip );
351 AddReturn
( $barcode1, $branchcode );
352 AddReturn
( $barcode2, $branchcode );
355 $date_due_in_time = $today->clone->set(hour
=> ($today->hour < 23 ?
$today->hour + 1 : 23), minute
=> 59);
356 $date_due_in_time_as_formatted = output_pref
( $date_due_in_time );
357 $issue_date = $date_due_in_time->clone->subtract_duration( DateTime
::Duration
->new( minutes
=> 1 ) );
358 AddIssue
( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
359 $date_due_in_late = $today->clone->set(hour
=> $today->hour - 1);
360 $date_due_in_late_as_formatted = output_pref
( $date_due_in_late );
361 $issue_date = $yesterday->clone;
362 AddIssue
( $borrower, $barcode2, $date_due_in_late, undef, $issue_date );
364 $expected_slip = <<EOS;
369 Date due: $date_due_in_time_as_formatted
372 $slip = IssueSlip
( $branchcode, $borrowernumber, 'quickslip' );
373 is
( $slip->{content
}, $expected_slip );
375 AddReturn
( $barcode1, $branchcode );
376 AddReturn
( $barcode2, $branchcode );
381 subtest
'bad calls' => sub {
383 AddIssue
( $borrower, $barcode1, $today, undef, $yesterday );
384 my $slip = IssueSlip
();
385 isnt
( $slip, undef, 'IssueSlip should return if no param passed FIXME, should return undef' );
386 my $empty_slip = <<EOS;
394 $slip = IssueSlip
(undef, $borrowernumber+1);
395 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)' );