|
Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
# Copyright PTFS Europe 2016 |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use DateTime; |
| 23 |
use Koha::Database; |
| 24 |
use Koha::Item::Transfer; |
| 25 |
use t::lib::TestBuilder; |
| 26 |
|
| 27 |
use Test::More tests => 7; |
| 28 |
|
| 29 |
my $schema = Koha::Database->new->schema; |
| 30 |
|
| 31 |
use_ok('Koha::Stockrotationitems'); |
| 32 |
use_ok('Koha::Stockrotationitem'); |
| 33 |
|
| 34 |
my $builder = t::lib::TestBuilder->new; |
| 35 |
|
| 36 |
subtest 'Basic object tests' => sub { |
| 37 |
|
| 38 |
plan tests => 5; |
| 39 |
|
| 40 |
$schema->storage->txn_begin; |
| 41 |
|
| 42 |
my $itm = $builder->build({ source => 'Item' }); |
| 43 |
my $stage = $builder->build({ source => 'Stockrotationstage' }); |
| 44 |
|
| 45 |
my $item = $builder->build({ |
| 46 |
source => 'Stockrotationitem', |
| 47 |
value => { |
| 48 |
itemnumber_id => $itm->{itemnumber}, |
| 49 |
stage_id => $stage->{stage_id}, |
| 50 |
}, |
| 51 |
}); |
| 52 |
|
| 53 |
my $sritem = Koha::Stockrotationitems->find($item->{itemnumber_id}); |
| 54 |
isa_ok( |
| 55 |
$sritem, |
| 56 |
'Koha::Stockrotationitem', |
| 57 |
"Correctly create and load a stock rotation item." |
| 58 |
); |
| 59 |
|
| 60 |
# Relationship to rota |
| 61 |
isa_ok( $sritem->itemnumber, 'Koha::Item', "Fetched related item." ); |
| 62 |
is( $sritem->itemnumber->itemnumber, $itm->{itemnumber}, "Related rota OK." ); |
| 63 |
|
| 64 |
# Relationship to stage |
| 65 |
isa_ok( $sritem->stage, 'Koha::Stockrotationstage', "Fetched related stage." ); |
| 66 |
is( $sritem->stage->stage_id, $stage->{stage_id}, "Related stage OK." ); |
| 67 |
|
| 68 |
|
| 69 |
$schema->storage->txn_rollback; |
| 70 |
}; |
| 71 |
|
| 72 |
subtest 'Tests for needs_repatriating' => sub { |
| 73 |
|
| 74 |
plan tests => 4; |
| 75 |
|
| 76 |
$schema->storage->txn_begin; |
| 77 |
|
| 78 |
# Setup a pristine stockrotation context. |
| 79 |
my $sritem = $builder->build({ source => 'Stockrotationitem' }); |
| 80 |
my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
| 81 |
$dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id); |
| 82 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id); |
| 83 |
$dbitem->stage->position(1); |
| 84 |
|
| 85 |
my $dbrota = $dbitem->stage->rota; |
| 86 |
my $newstage = $builder->build({ |
| 87 |
source => 'Stockrotationstage', |
| 88 |
values => { |
| 89 |
rota_id => $dbrota->rota_id, |
| 90 |
position => 2, |
| 91 |
} |
| 92 |
}); |
| 93 |
|
| 94 |
# - homebranch == holdingbranch [0] |
| 95 |
is( |
| 96 |
$dbitem->needs_repatriating, 0, |
| 97 |
"Homebranch == Holdingbranch." |
| 98 |
); |
| 99 |
|
| 100 |
my $branch = $builder->build({ source => 'Branch' }); |
| 101 |
$dbitem->itemnumber->holdingbranch($branch->{branchcode}); |
| 102 |
|
| 103 |
# - homebranch != holdingbranch [1] |
| 104 |
is( |
| 105 |
$dbitem->needs_repatriating, 1, |
| 106 |
"Homebranch != holdingbranch." |
| 107 |
); |
| 108 |
|
| 109 |
# Set to incorrect homebranch. |
| 110 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id); |
| 111 |
$dbitem->itemnumber->homebranch($branch->{branchcode}); |
| 112 |
# - homebranch != stockrotationstage.branch & not in transit [1] |
| 113 |
is( |
| 114 |
$dbitem->needs_repatriating, 1, |
| 115 |
"Homebranch != Stockrotationstage.Branchcode_id & not in transit." |
| 116 |
); |
| 117 |
|
| 118 |
# Set to in transit (by implication). |
| 119 |
$dbitem->stage($newstage->{stage_id}); |
| 120 |
# - homebranch != stockrotaitonstage.branch & in transit [0] |
| 121 |
is( |
| 122 |
$dbitem->needs_repatriating, 1, |
| 123 |
"homebranch != stockrotaitonstage.branch & in transit." |
| 124 |
); |
| 125 |
|
| 126 |
$schema->storage->txn_rollback; |
| 127 |
}; |
| 128 |
|
| 129 |
subtest "Tests for repatriate." => sub { |
| 130 |
plan tests => 3; |
| 131 |
$schema->storage->txn_begin; |
| 132 |
my $sritem = $builder->build({ source => 'Stockrotationitem' }); |
| 133 |
my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
| 134 |
$dbitem->stage->position(1); |
| 135 |
$dbitem->stage->duration(50); |
| 136 |
my $branch = $builder->build({ source => 'Branch' }); |
| 137 |
$dbitem->itemnumber->holdingbranch($branch->{branchcode}); |
| 138 |
|
| 139 |
# Test a straight up repatriate |
| 140 |
ok($dbitem->repatriate, "Repatriation done."); |
| 141 |
my $intransfer = $dbitem->itemnumber->get_transfer; |
| 142 |
is($intransfer->frombranch, $branch->{branchcode}, "Origin correct."); |
| 143 |
is($intransfer->tobranch, $dbitem->stage->branchcode_id, "Target Correct."); |
| 144 |
|
| 145 |
$schema->storage->txn_rollback; |
| 146 |
}; |
| 147 |
|
| 148 |
subtest "Tests for needs_advancing." => sub { |
| 149 |
plan tests => 3; |
| 150 |
$schema->storage->txn_begin; |
| 151 |
|
| 152 |
# Setup a pristine stockrotation context. |
| 153 |
my $sritem = $builder->build({ source => 'Stockrotationitem' }); |
| 154 |
my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
| 155 |
$dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id); |
| 156 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id); |
| 157 |
$dbitem->stage->position(1); |
| 158 |
$dbitem->stage->duration(50); |
| 159 |
|
| 160 |
my $dbtransfer = Koha::Item::Transfer->new({ |
| 161 |
'itemnumber' => $dbitem->itemnumber_id, |
| 162 |
'frombranch' => $dbitem->stage->branchcode_id, |
| 163 |
'tobranch' => $dbitem->stage->branchcode_id, |
| 164 |
'datesent' => DateTime->now, |
| 165 |
'datearrived' => undef, |
| 166 |
'comments' => "Test item", |
| 167 |
})->store; |
| 168 |
|
| 169 |
# Test item will not be advanced if in transit. |
| 170 |
is($dbitem->needs_advancing, 0, "Not ready to advance: in transfer."); |
| 171 |
|
| 172 |
# Test item will not be advanced if it has not spent enough time. |
| 173 |
$dbtransfer->datearrived(DateTime->now)->store; |
| 174 |
is($dbitem->needs_advancing, 0, "Not ready to advance: Not spent enough time."); |
| 175 |
|
| 176 |
# Test item will be advanced if it has spent enough time. |
| 177 |
$dbtransfer->datesent( # Item was sent 100 days ago... |
| 178 |
DateTime->now - DateTime::Duration->new( days => 100 ) |
| 179 |
)->store; |
| 180 |
$dbtransfer->datearrived( # And arrived 75 days ago. |
| 181 |
DateTime->now - DateTime::Duration->new( days => 75 ) |
| 182 |
)->store; |
| 183 |
is($dbitem->needs_advancing, 1, "Ready to be advanced."); |
| 184 |
|
| 185 |
$schema->storage->txn_rollback; |
| 186 |
}; |
| 187 |
|
| 188 |
subtest "Tests for advance." => sub { |
| 189 |
plan tests => 12; |
| 190 |
$schema->storage->txn_begin; |
| 191 |
|
| 192 |
my $sritem = $builder->build({ source => 'Stockrotationitem' }); |
| 193 |
my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
| 194 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id); |
| 195 |
my $dbstage = $dbitem->stage; |
| 196 |
$dbstage->position(1)->duration(50)->store; # Configure stage. |
| 197 |
# Configure item |
| 198 |
$dbitem->itemnumber->holdingbranch($dbstage->branchcode_id)->store; |
| 199 |
$dbitem->itemnumber->homebranch($dbstage->branchcode_id)->store; |
| 200 |
# Sanity check |
| 201 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage sanity check."); |
| 202 |
|
| 203 |
# Test cases of single stage |
| 204 |
$dbstage->rota->cyclical(1)->store; # Set Rota to cyclical. |
| 205 |
ok($dbitem->advance, "Single stage cyclical advance done."); |
| 206 |
## Refetch dbitem |
| 207 |
$dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
| 208 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Single stage cyclical stage OK."); |
| 209 |
|
| 210 |
# Test with indemand advance |
| 211 |
$dbitem->indemand(1)->store; |
| 212 |
ok($dbitem->advance, "Indemand item advance done."); |
| 213 |
## Refetch dbitem |
| 214 |
$dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
| 215 |
is($dbitem->indemand, 0, "Indemand OK."); |
| 216 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Indemand item advance stage OK."); |
| 217 |
|
| 218 |
# Multi stages |
| 219 |
my $srstage = $builder->build({ |
| 220 |
source => 'Stockrotationstage', |
| 221 |
values => { duration => 50 } |
| 222 |
}); |
| 223 |
my $dbstage2 = Koha::Stockrotationstages->find($srstage->{stage_id}); |
| 224 |
$dbstage2->move_to_group($dbitem->stage->rota_id); |
| 225 |
$dbstage2->move_last; |
| 226 |
|
| 227 |
# Test a straight up advance |
| 228 |
ok($dbitem->advance, "Advancement done."); |
| 229 |
## Refetch dbitem |
| 230 |
$dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
| 231 |
## Test results |
| 232 |
is($dbitem->stage->stage_id, $dbstage2->stage_id, "Stage updated."); |
| 233 |
my $intransfer = $dbitem->itemnumber->get_transfer; |
| 234 |
is($intransfer->frombranch, $dbstage->branchcode_id, "Origin correct."); |
| 235 |
is($intransfer->tobranch, $dbstage2->branchcode_id, "Target Correct."); |
| 236 |
|
| 237 |
$dbstage->rota->cyclical(0)->store; # Set Rota to non-cyclical. |
| 238 |
|
| 239 |
# Arrive at new branch |
| 240 |
$intransfer->datearrived(DateTime->now)->store; |
| 241 |
$dbitem->itemnumber->holdingbranch($srstage->{branchcode_id})->store; |
| 242 |
$dbitem->itemnumber->homebranch($srstage->{branchcode_id})->store; |
| 243 |
|
| 244 |
# Advance again, Remove from rota. |
| 245 |
ok($dbitem->advance, "Non-cyclical advance."); |
| 246 |
## Refetch dbitem |
| 247 |
$dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
| 248 |
is($dbitem, undef, "Stockrotationitem has been removed."); |
| 249 |
|
| 250 |
$schema->storage->txn_rollback; |
| 251 |
}; |
| 252 |
|
| 253 |
1; |