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

(-)a/Koha/Exceptions/Item/Transfer.pm (-5 / +51 lines)
Lines 1-5 Link Here
1
package Koha::Exceptions::Item::Transfer;
1
package Koha::Exceptions::Item::Transfer;
2
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
3
use Modern::Perl;
18
use Modern::Perl;
4
19
5
use Exception::Class (
20
use Exception::Class (
Lines 8-26 use Exception::Class ( Link Here
8
        description => 'Something went wrong'
23
        description => 'Something went wrong'
9
    },
24
    },
10
    'Koha::Exceptions::Item::Transfer::Found' => {
25
    'Koha::Exceptions::Item::Transfer::Found' => {
11
        isa => 'Koha::Exceptions::Item::Transfer',
26
        isa         => 'Koha::Exceptions::Item::Transfer',
12
        description => "Active item transfer already exists",
27
        description => "Active item transfer already exists",
13
        fields => ['transfer']
28
        fields      => ['transfer']
14
    },
29
    },
15
    'Koha::Exceptions::Item::Transfer::Limit' => {
30
    'Koha::Exceptions::Item::Transfer::Limit' => {
16
        isa => 'Koha::Exceptions::Item::Transfer',
31
        isa         => 'Koha::Exceptions::Item::Transfer',
17
        description => "Transfer not allowed"
32
        description => "Transfer not allowed"
18
    },
33
    },
19
    'Koha::Exceptions::Item::Transfer::Out' => {
34
    'Koha::Exceptions::Item::Transfer::Out' => {
20
        isa => 'Koha::Exceptions::Item::Transfer',
35
        isa         => 'Koha::Exceptions::Item::Transfer',
21
        description => "Transfer item is currently checked out"
36
        description => "Transfer item is currently checked out"
37
    },
38
    'Koha::Exceptions::Item::Transfer::Transit' => {
39
        isa         => 'Koha::Exceptions::Item::Transfer',
40
        description => "Transfer item is currently in transit"
22
    }
41
    }
23
24
);
42
);
25
43
44
=head1 NAME
45
46
Koha::Exceptions::Item::Transfer - Base class for Transfer exceptions
47
48
=head1 Exceptions
49
50
=head2 Koha::Exceptions::Item::Transfer
51
52
Generic Item::Transfer exception
53
54
=head2 Koha::Exceptions::Item::Transfer::Found
55
56
Exception to be used when an active item transfer prevents a transfer action.
57
58
=head2 Koha::Exceptions::Item::Transfer::Limit
59
60
Exception to be used when transfer limits prevent a transfer action.
61
62
=head2 Koha::Exceptions::Item::Transfer::Out
63
64
Exception to be used when an active checkout prevents a transfer action.
65
66
=head2 Koha::Exceptions::Item::Transfer::Transit
67
68
Exception to be used when an in transit transfer prevents a transfer action.
69
70
=cut
71
26
1;
72
1;
(-)a/Koha/Item.pm (-3 / +7 lines)
Lines 471-480 we still expect the item to end up at a final location eventually. Link Here
471
sub get_transfer {
471
sub get_transfer {
472
    my ($self) = @_;
472
    my ($self) = @_;
473
    my $transfer_rs = $self->_result->branchtransfers->search(
473
    my $transfer_rs = $self->_result->branchtransfers->search(
474
        { datearrived => undef },
475
        {
474
        {
476
            order_by => [ { -desc => 'datesent' }, { -asc => 'daterequested' } ],
475
            datearrived   => undef,
477
            rows     => 1
476
            datecancelled => undef
477
        },
478
        {
479
            order_by =>
480
              [ { -desc => 'datesent' }, { -asc => 'daterequested' } ],
481
            rows => 1
478
        }
482
        }
479
    )->first;
483
    )->first;
480
    return unless $transfer_rs;
484
    return unless $transfer_rs;
(-)a/Koha/Item/Transfer.pm (+26 lines)
Lines 116-121 sub receive { Link Here
116
    return $self;
116
    return $self;
117
}
117
}
118
118
119
=head3 cancel
120
121
  $transfer->cancel($reason);
122
123
Cancel the transfer by setting the datecancelled time and recording the reason.
124
125
=cut
126
127
sub cancel {
128
    my ( $self, $reason ) = @_;
129
130
    Koha::Exceptions::MissingParameter->throw(
131
        error => "The 'reason' parameter is mandatory" )
132
      unless defined($reason);
133
134
    # Throw exception if item is in transit already
135
    Koha::Exceptions::Item::Transfer::Transit->throw() if ( $self->in_transit );
136
137
    # Update the cancelled date
138
    $self->set(
139
        { datecancelled => dt_from_string, cancellation_reason => $reason } )
140
      ->store;
141
142
    return $self;
143
}
144
119
=head3 type
145
=head3 type
120
146
121
=cut
147
=cut
(-)a/t/db_dependent/Koha/Item/Transfer.t (-1 / +55 lines)
Lines 24-30 use Koha::DateUtils; Link Here
24
24
25
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
26
26
27
use Test::More tests => 4;
27
use Test::More tests => 5;
28
use Test::Exception;
28
use Test::Exception;
29
29
30
my $schema  = Koha::Database->new->schema;
30
my $schema  = Koha::Database->new->schema;
Lines 204-208 subtest 'in_transit tests' => sub { Link Here
204
    ok( !$transfer->in_transit, 'in_transit returns false when datearrived is defined');
204
    ok( !$transfer->in_transit, 'in_transit returns false when datearrived is defined');
205
205
206
206
207
    $schema->storage->txn_rollback;
208
};
209
210
subtest 'cancel tests' => sub {
211
    plan tests => 6;
212
213
    $schema->storage->txn_begin;
214
215
    my $library1 = $builder->build_object( { class => 'Koha::Libraries' } );
216
    my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
217
    my $item     = $builder->build_sample_item(
218
        {
219
            homebranch    => $library1->branchcode,
220
            holdingbranch => $library2->branchcode,
221
            datelastseen  => undef
222
        }
223
    );
224
    my $cancellation_reason = 'Manual';
225
226
    my $transfer = $builder->build_object(
227
        {
228
            class => 'Koha::Item::Transfers',
229
            value => {
230
                itemnumber          => $item->itemnumber,
231
                frombranch          => $library2->branchcode,
232
                tobranch            => $library1->branchcode,
233
                datesent            => \'NOW()',
234
                datearrived         => undef,
235
                datecancelled       => undef,
236
                reason              => 'Manual',
237
                cancellation_reason => undef
238
            }
239
        }
240
    );
241
    is( ref($transfer), 'Koha::Item::Transfer', 'Mock transfer added' );
242
243
    # Missing mandatory parameter
244
    throws_ok { $transfer->cancel() } 'Koha::Exceptions::MissingParameter',
245
      'Exception thrown if a reason is not passed to cancel';
246
247
    # Item in transit should result in failure
248
    throws_ok { $transfer->cancel($cancellation_reason) }
249
    'Koha::Exceptions::Item::Transfer::Transit',
250
      'Exception thrown if item is in transit';
251
252
    $transfer->datesent(undef)->store();
253
254
    # Transit state unset
255
    $transfer->discard_changes;
256
    $transfer->cancel($cancellation_reason);
257
    ok( $transfer->datecancelled, 'Cancellation date set upon call to cancel' );
258
    is( $transfer->cancellation_reason, 'Manual', 'Cancellation reason is set');
259
    is( $transfer->reason, 'Manual', 'Reason is not updated by cancelling the transfer' );
260
207
    $schema->storage->txn_rollback;
261
    $schema->storage->txn_rollback;
208
};
262
};
(-)a/t/db_dependent/Koha/Items.t (-14 / +20 lines)
Lines 1184-1190 subtest 'store' => sub { Link Here
1184
};
1184
};
1185
1185
1186
subtest 'get_transfer' => sub {
1186
subtest 'get_transfer' => sub {
1187
    plan tests => 6;
1187
    plan tests => 7;
1188
1188
1189
    my $transfer = $new_item_1->get_transfer();
1189
    my $transfer = $new_item_1->get_transfer();
1190
    is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item is not in transit' );
1190
    is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item is not in transit' );
Lines 1201-1206 subtest 'get_transfer' => sub { Link Here
1201
                reason        => 'Manual',
1201
                reason        => 'Manual',
1202
                datesent      => undef,
1202
                datesent      => undef,
1203
                datearrived   => undef,
1203
                datearrived   => undef,
1204
                datecancelled => undef,
1204
                daterequested => \'NOW()'
1205
                daterequested => \'NOW()'
1205
            }
1206
            }
1206
        }
1207
        }
Lines 1213-1224 subtest 'get_transfer' => sub { Link Here
1213
        {
1214
        {
1214
            class => 'Koha::Item::Transfers',
1215
            class => 'Koha::Item::Transfers',
1215
            value => {
1216
            value => {
1216
                itemnumber  => $new_item_1->itemnumber,
1217
                itemnumber    => $new_item_1->itemnumber,
1217
                frombranch  => $new_item_1->holdingbranch,
1218
                frombranch    => $new_item_1->holdingbranch,
1218
                tobranch    => $library_to->{branchcode},
1219
                tobranch      => $library_to->{branchcode},
1219
                reason      => 'Manual',
1220
                reason        => 'Manual',
1220
                datesent    => undef,
1221
                datesent      => undef,
1221
                datearrived => undef,
1222
                datearrived   => undef,
1223
                datecancelled => undef,
1222
                daterequested => \'NOW()'
1224
                daterequested => \'NOW()'
1223
            }
1225
            }
1224
        }
1226
        }
Lines 1235-1246 subtest 'get_transfer' => sub { Link Here
1235
        {
1237
        {
1236
            class => 'Koha::Item::Transfers',
1238
            class => 'Koha::Item::Transfers',
1237
            value => {
1239
            value => {
1238
                itemnumber  => $new_item_1->itemnumber,
1240
                itemnumber    => $new_item_1->itemnumber,
1239
                frombranch  => $new_item_1->holdingbranch,
1241
                frombranch    => $new_item_1->holdingbranch,
1240
                tobranch    => $library_to->{branchcode},
1242
                tobranch      => $library_to->{branchcode},
1241
                reason      => 'Manual',
1243
                reason        => 'Manual',
1242
                datesent    => undef,
1244
                datesent      => undef,
1243
                datearrived => undef,
1245
                datearrived   => undef,
1246
                datecancelled => undef,
1244
                daterequested => \'NOW()'
1247
                daterequested => \'NOW()'
1245
            }
1248
            }
1246
        }
1249
        }
Lines 1250-1255 subtest 'get_transfer' => sub { Link Here
1250
    $transfer = $new_item_1->get_transfer();
1253
    $transfer = $new_item_1->get_transfer();
1251
    is( $transfer->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfer returns the next queued transfer');
1254
    is( $transfer->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfer returns the next queued transfer');
1252
    is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer returns the right items transfer' );
1255
    is( $transfer->itemnumber, $new_item_1->itemnumber, 'Koha::Item->get_transfer returns the right items transfer' );
1256
1257
    $transfer_1->datecancelled(\'NOW()')->store;
1258
    $transfer = $new_item_1->get_transfer();
1259
    is( $transfer->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfer ignores cancelled transfers');
1253
};
1260
};
1254
1261
1255
subtest 'holds' => sub {
1262
subtest 'holds' => sub {
1256
- 

Return to bug 26057