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

(-)a/Koha/Hold.pm (-5 / +35 lines)
Lines 572-589 expirationdate for holds. Link Here
572
sub store {
572
sub store {
573
    my ($self) = @_;
573
    my ($self) = @_;
574
574
575
    if ( C4::Context->preference('DefaultHoldExpirationdate')
575
    if ( !$self->in_storage ) {
576
        and ( not defined $self->expirationdate or $self->expirationdate eq '' ) ){
576
        if (
577
            C4::Context->preference('DefaultHoldExpirationdate')
578
            and ( not defined $self->expirationdate
579
                or $self->expirationdate eq '' )
580
          )
581
        {
582
            $self->_set_default_expirationdate;
583
        }
584
    }
585
    else {
577
586
578
        my $period = C4::Context->preference('DefaultHoldExpirationdatePeriod');
587
        my %updated_columns = $self->_result->get_dirty_columns;
579
        my $timeunit = C4::Context->preference('DefaultHoldExpirationdateUnitOfTime');
588
        return $self->SUPER::store unless %updated_columns;
580
589
581
        $self->expirationdate( dt_from_string( $self->reservedate )->add( $timeunit => $period ) );
590
        if ( exists $updated_columns{reservedate} ) {
591
            if (
592
                C4::Context->preference('DefaultHoldExpirationdate')
593
                and ( not exists $updated_columns{expirationdate}
594
                    or exists $updated_columns{expirationdate}
595
                    and $updated_columns{expirationdate} eq '' )
596
              )
597
            {
598
                $self->_set_default_expirationdate;
599
            }
600
        }
582
    }
601
    }
583
602
584
    $self = $self->SUPER::store;
603
    $self = $self->SUPER::store;
585
}
604
}
586
605
606
sub _set_default_expirationdate {
607
    my $self = shift;
608
609
    my $period = C4::Context->preference('DefaultHoldExpirationdatePeriod');
610
    my $timeunit =
611
      C4::Context->preference('DefaultHoldExpirationdateUnitOfTime');
612
613
    $self->expirationdate(
614
        dt_from_string( $self->reservedate )->add( $timeunit => $period ) );
615
}
616
587
=head3 _move_to_old
617
=head3 _move_to_old
588
618
589
my $is_moved = $hold->_move_to_old;
619
my $is_moved = $hold->_move_to_old;
(-)a/t/db_dependent/Hold.t (-20 / +79 lines)
Lines 142-169 ok( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the Link Here
142
$item->holdingbranch( $branches[1]->{branchcode} );
142
$item->holdingbranch( $branches[1]->{branchcode} );
143
ok( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
143
ok( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
144
144
145
# Test setting expiration date
145
$schema->storage->txn_rollback();
146
t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdate', 1 );
147
t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdatePeriod', 2 );
148
t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdateUnitOfTime', 'years' );
149
146
150
my $hold_2 = Koha::Hold->new(
147
subtest "store() tests" => sub {
151
    {
152
        biblionumber   => $biblionumber,
153
        itemnumber     => $item->id(),
154
        reservedate    => '2020-11-30',
155
        waitingdate    => '2020-11-30',
156
        borrowernumber => $borrower->{borrowernumber},
157
        branchcode     => $branches[1]->{branchcode},
158
        suspend        => 0,
159
    }
160
);
161
$hold_2->store();
162
148
163
my $expected_date = dt_from_string( $hold_2->reservedate )->add( years => 2);
149
    plan tests => 5;
164
is($hold_2->expirationdate->ymd, $expected_date->ymd,'Expiration date is correctly set.');
165
150
166
$schema->storage->txn_rollback();
151
    $schema->storage->txn_begin();
152
153
    my $item     = $builder->build_sample_item;
154
    my $biblio   = $item->biblio;
155
    my $borrower = $builder->build_object( { class => 'Koha::Patrons' } );
156
    my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
157
158
    # Test setting expiration date
159
    t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdate',       1 );
160
    t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdatePeriod', 2 );
161
    t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdateUnitOfTime',
162
        'years' );
163
164
    my $hold = Koha::Hold->new(
165
        {
166
            biblionumber   => $biblio->biblionumber,
167
            itemnumber     => $item->id,
168
            reservedate    => '2020-11-30',
169
            waitingdate    => '2020-11-30',
170
            borrowernumber => $borrower->borrowernumber,
171
            branchcode     => $library->branchcode,
172
            suspend        => 0,
173
        }
174
    )->store;
175
    $hold->discard_changes;
176
177
    my $expected_date =
178
      dt_from_string( $hold->reservedate )->add( years => 2 );
179
    is( $hold->expirationdate,
180
        $expected_date->ymd, 'Default expiration date for new hold is correctly set.' );
181
182
    my $passed_date =
183
      dt_from_string( $hold->reservedate )->add( months => 2 );
184
    $hold = Koha::Hold->new(
185
        {
186
            biblionumber   => $biblio->biblionumber,
187
            itemnumber     => $item->id,
188
            reservedate    => '2020-11-30',
189
            expirationdate => $passed_date->ymd,
190
            waitingdate    => '2020-11-30',
191
            borrowernumber => $borrower->borrowernumber,
192
            branchcode     => $library->branchcode,
193
            suspend        => 0,
194
        }
195
    )->store;
196
    $hold->discard_changes;
197
198
    is( $hold->expirationdate,
199
        $passed_date->ymd, 'Passed expiration date for new hold is correctly set.' );
200
201
    $passed_date->add( months => 1 );
202
    $hold->expirationdate($passed_date->ymd)->store();
203
    $hold->discard_changes;
204
205
    is( $hold->expirationdate,
206
        $passed_date->ymd, 'Passed expiration date when updating hold is correctly set.' );
207
208
    $hold->reservedate($passed_date->ymd)->store();
209
    $hold->discard_changes;
210
211
    is( $hold->expirationdate,
212
        $passed_date->add( years => 2 )->ymd, 'Default expiration date correctly set on reservedate update.' );
213
214
    $hold->set(
215
        {
216
            reservedate    => $passed_date->ymd,
217
            expirationdate => $passed_date->add( weeks => 2 )->ymd
218
        }
219
    )->store();
220
    $hold->discard_changes;
221
222
    is( $hold->expirationdate,
223
        $passed_date->ymd, 'Passed expiration date when updating hold correctly set (Passing both reservedate and expirationdate.' );
224
225
    $schema->storage->txn_rollback();
226
};
167
227
168
subtest "delete() tests" => sub {
228
subtest "delete() tests" => sub {
169
229
170
- 

Return to bug 26498