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

(-)a/Koha/Checkout.pm (-1 / +47 lines)
Lines 22-32 use Modern::Perl; Link Here
22
22
23
use Carp;
23
use Carp;
24
use DateTime;
24
use DateTime;
25
use Scalar::Util qw( looks_like_number );
25
use Try::Tiny;
26
use Try::Tiny;
26
27
27
use Koha::Checkouts::ReturnClaims;
28
use Koha::Checkouts::ReturnClaims;
28
use Koha::Database;
29
use Koha::Database;
29
use Koha::DateUtils;
30
use Koha::DateUtils qw(dt_from_string);
31
use Koha::Exceptions;
30
use Koha::Items;
32
use Koha::Items;
31
33
32
use base qw(Koha::Object);
34
use base qw(Koha::Object);
Lines 161-166 sub claim_returned { Link Here
161
    };
163
    };
162
}
164
}
163
165
166
=head3 shift_due_date
167
168
    $checkout->shift_due_date({ hard_due_date => $dt });
169
    $checkout->shift_due_date({ days => $days_count });
170
171
=cut
172
173
sub shift_due_date {
174
    my ($self, $params) = @_;
175
176
    my $hard_due_date = $params->{hard_due_date};
177
    my $days          = $params->{days};
178
179
    if ( $hard_due_date and $days ) {
180
        Koha::Exceptions::BadParameter->throw( "Called with both 'days' and 'hard_due_date'. Only one can be used." );
181
    }
182
183
    if ( !( $days or $hard_due_date ) ) {
184
        Koha::Exceptions::BadParameter->throw( "At least one parameter needs to be passed" );
185
    }
186
187
    my $due_date = dt_from_string($self->date_due);
188
    if ( $hard_due_date ) {
189
        # We require a DateTime
190
        Koha::Exceptions::WrongParameter->throw(
191
            "'hard_due_date' must be a DateTime object"
192
        ) unless $hard_due_date->isa('DateTime');
193
194
        $due_date = $hard_due_date->clone->set(
195
            hour   => $due_date->hour,
196
            minute => $due_date->minute,
197
            second => $due_date->second
198
        );
199
    }
200
    else {
201
        Koha::Exceptions::WrongParameter->throw(
202
            "'days' must be an integer"
203
        ) unless looks_like_number($days);
204
        $due_date->add( days => $days );
205
    }
206
207
    return $self->date_due( $due_date );
208
}
209
164
=head2 Internal methods
210
=head2 Internal methods
165
211
166
=head3 _type
212
=head3 _type
(-)a/t/db_dependent/Koha/Checkout.t (+88 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
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.
9
#
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.
14
#
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>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 1;
21
use Test::Exception;
22
23
use t::lib::TestBuilder;
24
25
use Koha::DateUtils qw(dt_from_string output_pref);
26
27
my $schema  = Koha::Database->new->schema;
28
my $builder = t::lib::TestBuilder->new;
29
30
subtest 'shift_due_date() tests' => sub {
31
32
    plan tests => 10;
33
34
    $schema->storage->txn_begin;
35
36
    my $checkout = $builder->build_object(
37
        {
38
            class => 'Koha::Checkouts',
39
            value => {
40
                date_due => '2020-04-01 13:06:16'
41
            }
42
        }
43
    );
44
45
    throws_ok
46
        { $checkout->shift_due_date({ hard_due_date => 'cat', days => 'dog' }) }
47
        'Koha::Exceptions::BadParameter',
48
        'Passing both params makes it raise an exception';
49
50
    is( "$@", "Called with both 'days' and 'hard_due_date'. Only one can be used.", 'Exception stringified correctly' );
51
52
    throws_ok
53
        { $checkout->shift_due_date({ }) }
54
        'Koha::Exceptions::BadParameter',
55
        'Passing no params makes it raise an exception';
56
57
    is( "$@", "At least one parameter needs to be passed", 'Exception stringified correctly' );
58
59
    throws_ok
60
        { $checkout->shift_due_date({ hard_due_date => 'cat' }) }
61
        'Koha::Exceptions::WrongParameter',
62
        'Passing an invalid param type makes it raise an exception';
63
64
    is( "$@", "'hard_due_date' must be a DateTime object", 'Exception stringified correctly' );
65
66
    throws_ok
67
        { $checkout->shift_due_date({ days => 'dog' }) }
68
        'Koha::Exceptions::WrongParameter',
69
        'Passing an invalid param type makes it raise an exception';
70
71
    is( "$@", "'days' must be an integer", 'Exception stringified correctly' );
72
73
    my $hard_due_date = dt_from_string( '2020-04-13' );
74
    $checkout->shift_due_date({ hard_due_date => $hard_due_date });
75
76
    is(
77
        output_pref({ dt => dt_from_string($checkout->date_due) }),
78
        output_pref({ dt => dt_from_string('2020-04-13 13:06:16') })
79
    );
80
81
    $checkout->shift_due_date({ days => 3 });
82
    is(
83
        output_pref({ dt => dt_from_string($checkout->date_due) }),
84
        output_pref({ dt => dt_from_string('2020-04-16 13:06:16') })
85
    );
86
87
    $schema->storage->txn_rollback;
88
};
(-)a/tools/batch_extend_due_dates.pl (-38 / +18 lines)
Lines 101-119 elsif ( $op eq 'list' ) { Link Here
101
    );
101
    );
102
102
103
    my @new_due_dates;
103
    my @new_due_dates;
104
    my @checkouts;
104
    while ( my $checkout = $checkouts->next ) {
105
    while ( my $checkout = $checkouts->next ) {
105
        push @new_due_dates,
106
106
          output_pref({ dt => calc_new_due_date(
107
        # Update checkout's due date
107
            {
108
        if ( $new_hard_due_date ) {
108
                due_date          => dt_from_string($checkout->date_due),
109
            $checkout->shift_due_date({ hard_due_date => $new_hard_due_date });
109
                new_hard_due_date => $new_hard_due_date,
110
        }
110
                add_days          => $due_date_days
111
        else {
111
            }
112
            $checkout->shift_due_date({ days => $due_date_days });
112
          ), dateformat => 'iso' });
113
        }
114
        push @checkouts, $checkout;
113
    }
115
    }
114
116
115
    $template->param(
117
    $template->param(
116
        checkouts         => $checkouts,
118
        checkouts         => \@checkouts,
117
        new_hard_due_date => $new_hard_due_date
119
        new_hard_due_date => $new_hard_due_date
118
        ? dt_from_string($new_hard_due_date)
120
        ? dt_from_string($new_hard_due_date)
119
        : undef,
121
        : undef,
Lines 133-151 elsif ( $op eq 'modify' ) { Link Here
133
    my $checkouts =
135
    my $checkouts =
134
      Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } );
136
      Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } );
135
    while ( my $checkout = $checkouts->next ) {
137
    while ( my $checkout = $checkouts->next ) {
136
        my $new_due_date = calc_new_due_date(
137
            {
138
                due_date          => dt_from_string($checkout->date_due),
139
                new_hard_due_date => $new_hard_due_date,
140
                add_days          => $due_date_days
141
            }
142
        );
143
138
144
        # Update checkout's due date
139
        # Update checkout's due date
145
        $checkout->date_due($new_due_date)->store;
140
        if ( $new_hard_due_date ) {
141
            $checkout->shift_due_date({ hard_due_date => $new_hard_due_date })->store;
142
        }
143
        else {
144
            $checkout->shift_due_date({ days => $due_date_days })->store;
145
        }
146
146
147
        # Update items.onloan
147
        # Update items.onloan
148
        $checkout->item->onloan($new_due_date)->store;
148
        $checkout->item->onloan( $checkout->date_due )->store;
149
    }
149
    }
150
150
151
    $template->param(
151
    $template->param(
Lines 154-176 elsif ( $op eq 'modify' ) { Link Here
154
    );
154
    );
155
}
155
}
156
156
157
sub calc_new_due_date {
158
    my ($params)          = @_;
159
    my $due_date          = $params->{due_date};
160
    my $new_hard_due_date = $params->{new_hard_due_date};
161
    my $add_days          = $params->{add_days};
162
163
    my $new;
164
    if ( $new_hard_due_date ) {
165
      $new = $new_hard_due_date->clone->set(
166
        hour   => $due_date->hour,
167
        minute => $due_date->minute,
168
        second => $due_date->second,
169
      )
170
  } else {
171
      $new = $due_date->clone->add( days => $add_days );
172
  }
173
  return $new;
174
}
175
176
output_html_with_http_headers $input, $cookie, $template->output;
157
output_html_with_http_headers $input, $cookie, $template->output;
177
- 

Return to bug 25020