View | Details | Raw Unified | Return to bug 25039
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 162-167 sub claim_returned { Link Here
162
    };
164
    };
163
}
165
}
164
166
167
=head3 shift_due_date
168
169
    $checkout->shift_due_date({ hard_due_date => $dt });
170
    $checkout->shift_due_date({ days => $days_count });
171
172
=cut
173
174
sub shift_due_date {
175
    my ($self, $params) = @_;
176
177
    my $hard_due_date = $params->{hard_due_date};
178
    my $days          = $params->{days};
179
180
    if ( $hard_due_date and $days ) {
181
        Koha::Exceptions::BadParameter->throw( "Called with both 'days' and 'hard_due_date'. Only one can be used." );
182
    }
183
184
    if ( !( $days or $hard_due_date ) ) {
185
        Koha::Exceptions::BadParameter->throw( "At least one parameter needs to be passed" );
186
    }
187
188
    my $due_date = dt_from_string($self->date_due);
189
    if ( $hard_due_date ) {
190
        # We require a DateTime
191
        Koha::Exceptions::WrongParameter->throw(
192
            "'hard_due_date' must be a DateTime object"
193
        ) unless $hard_due_date->isa('DateTime');
194
195
        $due_date = $hard_due_date->clone->set(
196
            hour   => $due_date->hour,
197
            minute => $due_date->minute,
198
            second => $due_date->second
199
        );
200
    }
201
    else {
202
        Koha::Exceptions::WrongParameter->throw(
203
            "'days' must be an integer"
204
        ) unless looks_like_number($days);
205
        $due_date->add( days => $days );
206
    }
207
208
    return $self->date_due( $due_date );
209
}
210
165
=head2 Internal methods
211
=head2 Internal methods
166
212
167
=head3 _type
213
=head3 _type
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/batch_extend_due_dates.tt (-9 / +9 lines)
Lines 114-120 Link Here
114
                            </fieldset>
114
                            </fieldset>
115
                        </form> <!-- /#extend_due_dates_form -->
115
                        </form> <!-- /#extend_due_dates_form -->
116
                    [% ELSIF view == 'list' %]
116
                    [% ELSIF view == 'list' %]
117
                        [% IF checkouts.count %]
117
                        [% IF checkouts.size > 0 %]
118
                            <form action="/cgi-bin/koha/tools/batch_extend_due_dates.pl" method="post" id="process">
118
                            <form action="/cgi-bin/koha/tools/batch_extend_due_dates.pl" method="post" id="process">
119
                                <div id="toolbar">
119
                                <div id="toolbar">
120
                                    <a id="selectall" href="#"><i class="fa fa-check"></i> Select all</a>
120
                                    <a id="selectall" href="#"><i class="fa fa-check"></i> Select all</a>
Lines 136-150 Link Here
136
                                    <tbody>
136
                                    <tbody>
137
                                        [% FOR checkout IN checkouts %]
137
                                        [% FOR checkout IN checkouts %]
138
                                            <tr>
138
                                            <tr>
139
                                                <td><input type="checkbox" name="issue_id" value="[% checkout.issue_id | html %]" /></td>
139
                                                <td><input type="checkbox" name="issue_id" value="[% checkout.current.issue_id | html %]" /></td>
140
                                                <td>[% checkout.date_due | $KohaDates as_due_date => 1 %]</td>
140
                                                <td>[% checkout.current.date_due | $KohaDates as_due_date => 1 %]</td>
141
                                                <td><a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% checkout.item.biblio.biblionumber | uri %]">[% checkout.item.biblio.title | html %]</a></td>
141
                                                <td><a href="/cgi-bin/koha/catalogue/detail.pl?biblionumber=[% checkout.current.item.biblio.biblionumber | uri %]">[% checkout.current.item.biblio.title | html %]</a></td>
142
                                                <td>[% ItemTypes.GetDescription( checkout.item.effective_itemtype ) | html %]</td>
142
                                                <td>[% ItemTypes.GetDescription( checkout.current.item.effective_itemtype ) | html %]</td>
143
                                                <td>[% checkout.item.home_branch.branchname | html %]</td>
143
                                                <td>[% checkout.current.item.home_branch.branchname | html %]</td>
144
                                                <td>[% checkout.issuedate | $KohaDates %]</td>
144
                                                <td>[% checkout.current.issuedate | $KohaDates %]</td>
145
                                                <td>[% Branches.GetName( checkout.branchcode ) | html %]</td>
145
                                                <td>[% Branches.GetName( checkout.current.branchcode ) | html %]</td>
146
                                                <td>
146
                                                <td>
147
                                                    [% new_due_dates.shift | $KohaDates as_due_date => 1 %]
147
                                                    [% checkout.updated.date_due | $KohaDates as_due_date => 1 %]
148
                                                </td>
148
                                                </td>
149
                                            </tr>
149
                                            </tr>
150
                                        [% END %]
150
                                        [% END %]
(-)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 (-50 / +26 lines)
Lines 103-139 elsif ( $op eq 'list' ) { Link Here
103
    );
103
    );
104
104
105
    my @new_due_dates;
105
    my @new_due_dates;
106
    my @checkouts;
107
106
    while ( my $checkout = $checkouts->next ) {
108
    while ( my $checkout = $checkouts->next ) {
109
        my $updated_checkout = $checkout->get_from_storage;
107
        if ($preview_results) {
110
        if ($preview_results) {
108
            push(
111
            if ( $new_hard_due_date ) {
109
                @new_due_dates,
112
                $updated_checkout->shift_due_date({ hard_due_date => $new_hard_due_date });
110
                output_pref(
113
            }
111
                    {
114
            else {
112
                        dt => calc_new_due_date(
115
                $updated_checkout->shift_due_date({ days => $due_date_days });
113
                            {
116
            }
114
                                due_date =>
115
                                  dt_from_string( $checkout->date_due ),
116
                                new_hard_due_date => $new_hard_due_date,
117
                                add_days          => $due_date_days
118
                            }
119
                        ),
120
                        dateformat => 'iso'
121
                    }
122
                )
123
            );
124
        } else {
117
        } else {
125
            push( @issue_ids, $checkout->id );
118
            push( @issue_ids, $checkout->id );
126
        }
119
        }
120
121
        push @checkouts, { current => $checkout, updated => $updated_checkout };
127
    }
122
    }
128
123
129
    if ( $preview_results ) {
124
    if ( $preview_results ) {
130
        $template->param(
125
        $template->param(
131
            checkouts         => $checkouts,
126
            checkouts         => \@checkouts,
132
            new_hard_due_date => $new_hard_due_date
127
            new_hard_due_date => $new_hard_due_date
133
            ? dt_from_string($new_hard_due_date)
128
            ? dt_from_string($new_hard_due_date)
134
            : undef,
129
            : undef,
135
            due_date_days => $due_date_days,
130
            due_date_days => $due_date_days,
136
            new_due_dates => \@new_due_dates,
137
            view          => 'list',
131
            view          => 'list',
138
        );
132
        );
139
    } else {
133
    } else {
Lines 151-197 if ( $op eq 'modify' ) { Link Here
151
    @issue_ids = $input->multi_param('issue_id') unless @issue_ids;
145
    @issue_ids = $input->multi_param('issue_id') unless @issue_ids;
152
146
153
    $new_hard_due_date &&= dt_from_string($new_hard_due_date);
147
    $new_hard_due_date &&= dt_from_string($new_hard_due_date);
154
    my $checkouts =
148
    my @checkouts;
155
      Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } );
149
    my $checkouts = Koha::Checkouts->search( { issue_id => { -in => \@issue_ids } } );
156
    while ( my $checkout = $checkouts->next ) {
150
    while ( my $checkout = $checkouts->next ) {
157
        my $new_due_date = calc_new_due_date(
158
            {
159
                due_date          => dt_from_string($checkout->date_due),
160
                new_hard_due_date => $new_hard_due_date,
161
                add_days          => $due_date_days
162
            }
163
        );
164
151
165
        # Update checkout's due date
152
        # Update checkout's due date
166
        $checkout->date_due($new_due_date)->store;
153
        if ( $new_hard_due_date ) {
154
            $checkout->shift_due_date({ hard_due_date => $new_hard_due_date });
155
        }
156
        else {
157
            $checkout->shift_due_date({ days => $due_date_days });
158
        }
167
159
168
        # Update items.onloan
160
        # Update items.onloan
169
        $checkout->item->onloan($new_due_date)->store;
161
        $checkout->item->onloan($checkout->date_due)->store;
162
163
        push @checkouts, $checkout;
170
    }
164
    }
171
165
172
    $template->param(
166
    $template->param(
173
        view      => 'report',
167
        view      => 'report',
174
        checkouts => $checkouts,
168
        checkouts => \@checkouts,
175
    );
169
    );
176
}
170
}
177
171
178
sub calc_new_due_date {
179
    my ($params)          = @_;
180
    my $due_date          = $params->{due_date};
181
    my $new_hard_due_date = $params->{new_hard_due_date};
182
    my $add_days          = $params->{add_days};
183
184
    my $new;
185
    if ( $new_hard_due_date ) {
186
      $new = $new_hard_due_date->clone->set(
187
        hour   => $due_date->hour,
188
        minute => $due_date->minute,
189
        second => $due_date->second,
190
      )
191
  } else {
192
      $new = $due_date->clone->add( days => $add_days );
193
  }
194
  return $new;
195
}
196
197
output_html_with_http_headers $input, $cookie, $template->output;
172
output_html_with_http_headers $input, $cookie, $template->output;
198
- 
173
174
1;

Return to bug 25039