View | Details | Raw Unified | Return to bug 12847
Collapse All | Expand All

(-)a/C4/Members.pm (-12 / +20 lines)
Lines 1200-1205 sub GetPendingIssues { Link Here
1200
            $_->{issuedate} = dt_from_string($_->{issuedate}, 'sql');
1200
            $_->{issuedate} = dt_from_string($_->{issuedate}, 'sql');
1201
        }
1201
        }
1202
        $_->{date_due} or next;
1202
        $_->{date_due} or next;
1203
        $_->{date_due_sql} = $_->{date_due};
1204
        # FIXME no need to have this value
1203
        $_->{date_due} = dt_from_string($_->{date_due}, 'sql');
1205
        $_->{date_due} = dt_from_string($_->{date_due}, 'sql');
1204
        if ( DateTime->compare($_->{date_due}, $today) == -1 ) {
1206
        if ( DateTime->compare($_->{date_due}, $today) == -1 ) {
1205
            $_->{overdue} = 1;
1207
            $_->{overdue} = 1;
Lines 2439-2460 sub DeleteMessage { Link Here
2439
sub IssueSlip {
2441
sub IssueSlip {
2440
    my ($branch, $borrowernumber, $quickslip) = @_;
2442
    my ($branch, $borrowernumber, $quickslip) = @_;
2441
2443
2442
#   return unless ( C4::Context->boolean_preference('printcirculationslips') );
2444
    # FIXME Check callers before removing this statement
2445
    #return unless $borrowernumber;
2443
2446
2444
    my $now       = POSIX::strftime("%Y-%m-%d", localtime);
2447
    my @issues = @{ GetPendingIssues($borrowernumber) };
2445
2448
2446
    my $issueslist = GetPendingIssues($borrowernumber);
2449
    for my $issue (@issues) {
2447
    foreach my $it (@$issueslist){
2450
        $issue->{date_due} = $issue->{date_due_sql};
2448
        if ((substr $it->{'issuedate'}, 0, 10) eq $now || (substr $it->{'lastreneweddate'}, 0, 10) eq $now) {
2451
        if ($quickslip) {
2449
            $it->{'now'} = 1;
2452
            my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
2453
            if ( substr( $issue->{issuedate}, 0, 10 ) eq $today
2454
                or substr( $issue->{lastreneweddate}, 0, 10 ) eq $today ) {
2455
                  $issue->{now} = 1;
2456
            };
2450
        }
2457
        }
2451
        elsif ((substr $it->{'date_due'}, 0, 10) le $now) {
2452
            $it->{'overdue'} = 1;
2453
        }
2454
        my $dt = dt_from_string( $it->{'date_due'} );
2455
        $it->{'date_due'} = output_pref( $dt );;
2456
    }
2458
    }
2457
    my @issues = sort { $b->{'timestamp'} <=> $a->{'timestamp'} } @$issueslist;
2459
2460
    # Sort on timestamp then on issuedate (useful for tests and could be if modified in a batch
2461
    @issues = sort {
2462
        my $s = $b->{timestamp} <=> $a->{timestamp};
2463
        $s == 0 ?
2464
             $b->{issuedate} <=> $a->{issuedate} : $s;
2465
    } @issues;
2458
2466
2459
    my ($letter_code, %repeat);
2467
    my ($letter_code, %repeat);
2460
    if ( $quickslip ) {
2468
    if ( $quickslip ) {
(-)a/t/db_dependent/Members/IssueSlip.t (-1 / +394 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use Test::More tests => 3;
6
use Test::MockModule;
7
8
use C4::Biblio;
9
use C4::Items;
10
use C4::Members;
11
use C4::Branch;
12
use C4::Category;
13
use C4::Circulation;
14
15
use Koha::DateUtils qw( dt_from_string output_pref );
16
use DateTime::Duration;
17
18
use MARC::Record;
19
20
my $dbh = C4::Context->dbh;
21
$dbh->{AutoCommit} = 0;
22
$dbh->{RaiseError} = 1;
23
24
$dbh->do(q|DELETE FROM issues|);
25
$dbh->do(q|DELETE FROM borrowers|);
26
$dbh->do(q|DELETE FROM items|);
27
$dbh->do(q|DELETE FROM branches|);
28
$dbh->do(q|DELETE FROM biblio|);
29
$dbh->do(q|DELETE FROM categories|);
30
$dbh->do(q|DELETE FROM letter|);
31
32
my $branchcode = 'B';
33
ModBranch( { add => 1, branchcode => $branchcode, branchname => 'Branch' } );
34
35
my $categorycode = 'C';
36
$dbh->do( "INSERT INTO categories(categorycode) VALUES(?)",
37
    undef, $categorycode );
38
39
my %item_branch_infos = (
40
    homebranch    => $branchcode,
41
    holdingbranch => $branchcode,
42
);
43
44
my $slip_content = <<EOS;
45
Checked out:
46
<checkedout>
47
Title: <<biblio.title>>
48
Barcode: <<items.barcode>>
49
Date due: <<issues.date_due>>
50
</checkedout>
51
52
Overdues:
53
<overdue>
54
Title: <<biblio.title>>
55
Barcode: <<items.barcode>>
56
Date due: <<issues.date_due>>
57
</overdue>
58
EOS
59
60
$dbh->do(q|
61
    INSERT INTO letter( module, code, branchcode, name, is_html, title, content, message_transport_type) VALUES ('circulation', 'ISSUESLIP', '', 'Issue Slip', 0, 'Issue Slip', ?, 'email')
62
|, {}, $slip_content);
63
64
my $quick_slip_content = <<EOS;
65
Checked out today:
66
<checkedout>
67
Title: <<biblio.title>>
68
Barcode: <<items.barcode>>
69
Date due: <<issues.date_due>>
70
</checkedout>
71
EOS
72
73
$dbh->do(q|
74
    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')
75
|, {}, $quick_slip_content);
76
77
my ( $title1, $title2 ) = ( 'My title 1', 'My title 2' );
78
my ( $barcode1, $barcode2 ) = ('BC0101', 'BC0202' );
79
my $record = MARC::Record->new;
80
$record->append_fields(
81
    MARC::Field->new(
82
        245, '1', '4',
83
            a => $title1,
84
    ),
85
);
86
my ($biblionumber1) = AddBiblio( $record, '' );
87
my $itemnumber1 =
88
  AddItem( { barcode => $barcode1, %item_branch_infos }, $biblionumber1 );
89
90
$record = MARC::Record->new;
91
$record->append_fields(
92
    MARC::Field->new(
93
        245, '1', '4',
94
            a => $title2,
95
    ),
96
);
97
my ($biblionumber2) = AddBiblio( $record, '' );
98
my $itemnumber2 =
99
  AddItem( { barcode => $barcode2, %item_branch_infos }, $biblionumber2 );
100
101
my $borrowernumber =
102
  AddMember( categorycode => $categorycode, branchcode => $branchcode );
103
my $borrower = GetMember( borrowernumber => $borrowernumber );
104
105
my $module = new Test::MockModule('C4::Context');
106
$module->mock( 'userenv', sub { { branch => $branchcode } } );
107
108
my $today = dt_from_string;
109
my $yesterday = dt_from_string->subtract_duration( DateTime::Duration->new( days => 1 ) );
110
111
subtest 'Issue slip' => sub {
112
    plan tests => 3;
113
114
    subtest 'Empty slip' => sub {
115
        plan tests => 1;
116
        my $slip = IssueSlip( $branchcode, $borrowernumber );
117
        my $empty_slip = <<EOS;
118
Checked out:
119
120
121
Overdues:
122
123
EOS
124
        is( $slip->{content}, $empty_slip, 'No checked out or overdues return an empty slip, it should return undef (FIXME)' );
125
    };
126
127
    subtest 'Daily loans' => sub {
128
        plan tests => 2;
129
        # Test 1: No overdue
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 );
134
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 );
142
143
        $expected_slip = <<EOS;
144
Checked out:
145
146
Title: $title1
147
Barcode: $barcode1
148
Date due: $today_daily_as_formatted
149
150
151
Title: $title2
152
Barcode: $barcode2
153
Date due: $today_daily_as_formatted
154
155
156
Overdues:
157
158
EOS
159
        $slip = IssueSlip( $branchcode, $borrowernumber );
160
        is( $slip->{content}, $expected_slip , 'IssueSlip should return a slip with 2 checkouts');
161
162
        AddReturn( $barcode1, $branchcode );
163
        AddReturn( $barcode2, $branchcode );
164
165
        # Test 2: 1 Overdue
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 );
172
173
        $expected_slip = <<EOS;
174
Checked out:
175
176
Title: $title1
177
Barcode: $barcode1
178
Date due: $today_daily_as_formatted
179
180
181
Overdues:
182
183
Title: $title2
184
Barcode: $barcode2
185
Date due: $yesterday_daily_as_formatted
186
187
EOS
188
        $slip = IssueSlip( $branchcode, $borrowernumber );
189
        is( $slip->{content}, $expected_slip, 'IssueSlip should return a slip with 1 checkout and 1 overdue');
190
191
        AddReturn( $barcode1, $branchcode );
192
        AddReturn( $barcode2, $branchcode );
193
    };
194
195
    subtest 'Hourly loans' => sub {
196
        plan tests => 2;
197
        # Test 1: No overdue
198
        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 );
199
        # Assuming today is not hour = 23 and minute = 59
200
        $date_due_in_time = $today->clone->set(hour => $today->hour + 1);
201
        $date_due_in_time_as_formatted = output_pref( $date_due_in_time );
202
        $issue_date = $today->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
203
        AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
204
        $issue_date = $yesterday->clone;
205
        AddIssue( $borrower, $barcode2, $date_due_in_time, undef, $issue_date );
206
207
        $expected_slip = <<EOS;
208
Checked out:
209
210
Title: $title1
211
Barcode: $barcode1
212
Date due: $date_due_in_time_as_formatted
213
214
215
Title: $title2
216
Barcode: $barcode2
217
Date due: $date_due_in_time_as_formatted
218
219
220
Overdues:
221
222
EOS
223
        $slip = IssueSlip( $branchcode, $borrowernumber );
224
        is( $slip->{content}, $expected_slip, 'IssueSlip should return a slip with 2 checkouts' );
225
226
        AddReturn( $barcode1, $branchcode );
227
        AddReturn( $barcode2, $branchcode );
228
229
        # Test 2: 1 Overdue
230
        $date_due_in_time = $today->clone->set(hour => $today->hour + 1);
231
        $date_due_in_time_as_formatted = output_pref( $date_due_in_time );
232
        $issue_date = $today->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
233
        AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
234
        $date_due_in_late = $today->clone->set(hour => $today->hour - 1);
235
        $date_due_in_late_as_formatted = output_pref( $date_due_in_late );
236
        $issue_date = $yesterday->clone;
237
        AddIssue( $borrower, $barcode2, $date_due_in_late, undef, $issue_date );
238
239
        $expected_slip = <<EOS;
240
Checked out:
241
242
Title: $title1
243
Barcode: $barcode1
244
Date due: $date_due_in_time_as_formatted
245
246
247
Overdues:
248
249
Title: $title2
250
Barcode: $barcode2
251
Date due: $date_due_in_late_as_formatted
252
253
EOS
254
        $slip = IssueSlip( $branchcode, $borrowernumber );
255
        is( $slip->{content}, $expected_slip, 'IssueSlip should return a slip with 1 checkout and 1 overdue' );
256
257
        AddReturn( $barcode1, $branchcode );
258
        AddReturn( $barcode2, $branchcode );
259
    };
260
261
};
262
263
subtest 'Quick slip' => sub {
264
    plan tests => 3;
265
266
    subtest 'Empty slip' => sub {
267
        plan tests => 1;
268
        my $slip = IssueSlip( $branchcode, $borrowernumber, 'quick slip' );
269
        my $empty_slip = <<EOS;
270
Checked out today:
271
272
EOS
273
        is( $slip->{content}, $empty_slip, 'No checked out or overdues return an empty slip, it should return undef (FIXME)' );
274
    };
275
276
    subtest 'Daily loans' => sub {
277
        plan tests => 2;
278
        # Test 1: No overdue
279
        my $today_daily = $today->clone->set( hour => 23, minute => 59 );
280
        my $today_daily_as_formatted = output_pref( $today_daily );
281
        my $yesterday_daily = $yesterday->clone->set( hour => 23, minute => 59 );
282
        my $yesterday_daily_as_formatted = output_pref( $yesterday_daily );
283
284
        my ( $date_due, $issue_date, $slip, $expected_slip );
285
        $date_due = $today_daily;
286
        $issue_date = $today_daily->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
287
        AddIssue( $borrower, $barcode1, $date_due, undef, $issue_date );
288
        $date_due = $today_daily;
289
        $issue_date = $yesterday_daily;
290
        AddIssue( $borrower, $barcode2, $date_due, undef, $issue_date );
291
292
        $expected_slip = <<EOS;
293
Checked out today:
294
295
Title: $title1
296
Barcode: $barcode1
297
Date due: $today_daily_as_formatted
298
299
EOS
300
        $slip = IssueSlip( $branchcode, $borrowernumber, 'quick slip' );
301
        is( $slip->{content}, $expected_slip, 'IssueSlip should return 2 checkouts for today');
302
303
        AddReturn( $barcode1, $branchcode );
304
        AddReturn( $barcode2, $branchcode );
305
306
        # Test 2: 1 Overdue
307
        $date_due = $today_daily;
308
        $issue_date = $today_daily->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
309
        AddIssue( $borrower, $barcode1, $date_due, undef, $issue_date );
310
        $date_due = $yesterday_daily;
311
        $issue_date = $yesterday_daily;
312
        AddIssue( $borrower, $barcode2, $date_due, undef, $issue_date );
313
314
        $expected_slip = <<EOS;
315
Checked out today:
316
317
Title: $title1
318
Barcode: $barcode1
319
Date due: $today_daily_as_formatted
320
321
EOS
322
        $slip = IssueSlip( $branchcode, $borrowernumber, 'quickslip' );
323
        is( $slip->{content}, $expected_slip );
324
    };
325
326
    subtest 'Hourly loans' => sub {
327
        plan tests => 2;
328
        # Test 1: No overdue
329
        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 );
330
        # Assuming today is not hour = 23 and minute = 59
331
        $date_due_in_time = $today->clone->set(hour => $today->hour + 1);
332
        $date_due_in_time_as_formatted = output_pref( $date_due_in_time );
333
        $issue_date = $today->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
334
        AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
335
        $issue_date = $yesterday->clone;
336
        AddIssue( $borrower, $barcode2, $date_due_in_time, undef, $issue_date );
337
338
        $expected_slip = <<EOS;
339
Checked out today:
340
341
Title: $title1
342
Barcode: $barcode1
343
Date due: $date_due_in_time_as_formatted
344
345
EOS
346
        $slip = IssueSlip( $branchcode, $borrowernumber, 'quickslip' );
347
        is( $slip->{content}, $expected_slip );
348
349
        AddReturn( $barcode1, $branchcode );
350
        AddReturn( $barcode2, $branchcode );
351
352
        # Test 2: 1 Overdue
353
        $date_due_in_time = $today->clone->set(hour => $today->hour + 1);
354
        $date_due_in_time_as_formatted = output_pref( $date_due_in_time );
355
        $issue_date = $today->clone->subtract_duration( DateTime::Duration->new( minutes => 1 ) );
356
        AddIssue( $borrower, $barcode1, $date_due_in_time, undef, $issue_date );
357
        $date_due_in_late = $today->clone->set(hour => $today->hour - 1);
358
        $date_due_in_late_as_formatted = output_pref( $date_due_in_late );
359
        $issue_date = $yesterday->clone;
360
        AddIssue( $borrower, $barcode2, $date_due_in_late, undef, $issue_date );
361
362
        $expected_slip = <<EOS;
363
Checked out today:
364
365
Title: $title1
366
Barcode: $barcode1
367
Date due: $date_due_in_time_as_formatted
368
369
EOS
370
        $slip = IssueSlip( $branchcode, $borrowernumber, 'quickslip' );
371
        is( $slip->{content}, $expected_slip );
372
373
        AddReturn( $barcode1, $branchcode );
374
        AddReturn( $barcode2, $branchcode );
375
    };
376
377
};
378
379
subtest 'bad calls' => sub {
380
    plan tests => 2;
381
    AddIssue( $borrower, $barcode1, $today, undef, $yesterday );
382
    my $slip = IssueSlip();
383
    isnt( $slip, undef, 'IssueSlip should return if no param passed FIXME, should return undef' );
384
    my $empty_slip = <<EOS;
385
Checked out:
386
387
388
Overdues:
389
390
EOS
391
392
    $slip = IssueSlip(undef, $borrowernumber+1);
393
    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)' );
394
};

Return to bug 12847