2 use strict; use warnings;
3 use Test::More tests => 26;
4 use OpenILS::Utils::TestUtils;
5 use OpenILS::Const qw(:const);
7 diag("Test LP 1306666 abort transit copy status fix.");
9 my $script = OpenILS::Utils::TestUtils->new();
10 my $apputils = 'OpenILS::Application::AppUtils';
13 BR1_WORKSTATION => 'BR1-test-lp1306666-abort-transit-copyt-status.t',
15 BR3_WORKSTATION => 'BR3-test-lp1306666-abort-transit-copyt-status.t',
17 PBARCODE => '99999378730',
18 CBARCODE => 'CONC4000036'
24 # Login as staff at BR1
25 $authtoken[0] = $script->authenticate({
26 username => 'br1vcampbell',
27 password => 'vincentc1234',
31 # Register workstation at BR1.
32 unless ($script->find_workstation(BR1_WORKSTATION, BR1_ID)) {
33 $script->register_workstation(BR1_WORKSTATION, BR1_ID);
37 $script->logout($authtoken[0]);
39 # Login as staff at BR1 using workstation.
40 $authtoken[0] = $script->authenticate({
41 username => 'br1vcampbell',
42 password => 'vincentc1234',
44 workstation => BR1_WORKSTATION
47 # Login as staff at BR3
48 $authtoken[1] = $script->authenticate({
49 username => 'br3sforbes',
50 password => 'samuelf1234',
54 # Register workstation at BR3.
55 unless ($script->find_workstation(BR3_WORKSTATION, BR3_ID)) {
56 $script->register_workstation(BR3_WORKSTATION, BR3_ID);
60 $script->logout($authtoken[1]);
62 # Login as staff at BR3 using workstation.
63 $authtoken[1] = $script->authenticate({
64 username => 'br3sforbes',
65 password => 'samuelf1234',
67 workstation => BR3_WORKSTATION
70 # Retrieve copy at BR1
71 my $copy = $apputils->simplereq(
73 'open-ils.search.asset.copy.find_by_barcode',
77 # Check that copy exists.
78 isa_ok(ref($copy), 'Fieldmapper::asset::copy', 'Got copy') or BAIL_OUT('Need copy');
80 # Retrieve patron at BR3
81 my $patron = $apputils->simplereq(
83 'open-ils.actor.user.fleshed.retrieve_by_barcode',
88 # Check that patron exists.
89 isa_ok(ref($patron), 'Fieldmapper::actor::user', 'Got patron') or BAIL_OUT('Need patron');
91 # Place copy hold for patron pickup at BR3.
92 my $hold = $apputils->simplereq(
94 'open-ils.circ.holds.test_and_create.batch',
98 patronid => $patron->id(),
103 if (ref($hold->{result})) {
104 my $event = (ref($hold->{result}) eq 'ARRAY') ? $hold->{result}->[0] : $hold->{result};
105 if ($event->{textcode} eq 'HOLD_EXISTS') {
106 my $target = $hold->{target};
107 $hold = $apputils->simplereq(
109 'open-ils.pcrud.search.ahr',
111 {target => $target, usr => $patron->id(), fulfillment_time => undef, cancel_time => undef}
114 BAIL_OUT('Cannot place hold');
117 $hold = $apputils->simplereq(
119 'open-ils.pcrud.retrieve.ahr',
125 # Check that hold exists.
126 isa_ok(ref($hold), 'Fieldmapper::action::hold_request', 'Got hold') or BAIL_OUT('Need hold');
128 # Check copy in at BR1
129 my $checkin = $apputils->simplereq(
131 'open-ils.circ.checkin',
133 {barcode => CBARCODE}
135 subtest 'Got ROUTE_ITEM event 1' => sub {
137 is(ref($checkin), 'HASH', 'Got event');
138 is($checkin->{textcode}, 'ROUTE_ITEM', 'Route item event');
139 is($checkin->{org}, BR3_ID, 'ROUTE_ITEM event destination');
142 # Check copy transit.
143 my $transit = $apputils->simplereq(
145 'open-ils.pcrud.search.ahtc',
147 {target_copy => $copy->id(), hold => $hold->id(), dest_recv_time => undef}
149 subtest 'Got hold transit 1' => sub {
151 isa_ok(ref($transit), 'Fieldmapper::action::hold_transit_copy', 'Got hold transit copy');
152 is($transit->dest(), BR3_ID, 'Transit destination');
153 is($transit->source(), BR1_ID, 'Transit source');
154 is($transit->copy_status(), OILS_COPY_STATUS_ON_HOLDS_SHELF, 'Transit copy status');
158 $copy = $apputils->simplereq(
160 'open-ils.pcrud.retrieve.acp',
164 is($copy->status(), OILS_COPY_STATUS_IN_TRANSIT, 'Copy in transit');
167 my $abort = $apputils->simplereq(
169 'open-ils.circ.transit.abort',
171 {transitid => $transit->id()}
173 is($abort, 1, 'Transit aborted');
176 $copy = $apputils->simplereq(
178 'open-ils.pcrud.retrieve.acp',
182 is($copy->status(), OILS_COPY_STATUS_CANCELED_TRANSIT, 'Copy in Canceled Transit status');
184 # Check copy in at BR1
185 $checkin = $apputils->simplereq(
187 'open-ils.circ.checkin',
189 {barcode => CBARCODE}
191 subtest 'Got ROUTE_ITEM event 2' => sub {
193 is(ref($checkin), 'HASH', 'Got event');
194 is($checkin->{textcode}, 'ROUTE_ITEM', 'Route item event');
195 is($checkin->{org}, BR3_ID, 'ROUTE_ITEM event destination');
198 # Check copy transit.
199 $transit = $apputils->simplereq(
201 'open-ils.pcrud.search.ahtc',
203 {target_copy => $copy->id(), hold => $hold->id(), dest_recv_time => undef}
205 subtest 'Got hold transit 2' => sub {
207 isa_ok(ref($transit), 'Fieldmapper::action::hold_transit_copy', 'Got hold transit copy');
208 is($transit->dest(), BR3_ID, 'Transit destination');
209 is($transit->source(), BR1_ID, 'Transit source');
210 is($transit->copy_status(), OILS_COPY_STATUS_ON_HOLDS_SHELF, 'Transit copy status');
214 $copy = $apputils->simplereq(
216 'open-ils.pcrud.retrieve.acp',
220 is($copy->status(), OILS_COPY_STATUS_IN_TRANSIT, 'Copy in transit 2');
222 # Check copy in at BR3.
223 $checkin = $apputils->simplereq(
225 'open-ils.circ.checkin',
227 {barcode => CBARCODE}
229 subtest 'Checkin at destination' => sub{
231 is(ref($checkin), 'HASH', 'Got event');
232 is($checkin->{textcode}, 'SUCCESS', 'Event was successful');
233 is($checkin->{ishold}, 1, 'Check In filled hold');
236 # Check hold is on shelf.
237 $hold = $apputils->simplereq(
239 'open-ils.pcrud.retrieve.ahr',
243 ok(defined($hold->shelf_time()), 'Hold has shelf time');
246 $copy = $apputils->simplereq(
248 'open-ils.pcrud.retrieve.acp',
252 is($copy->status(), OILS_COPY_STATUS_ON_HOLDS_SHELF, 'Copy on holds shelf');
254 # Check copy out to patron at BR3.
255 my $checkout = $apputils->simplereq(
257 'open-ils.circ.checkout.full',
259 {copy_id => $copy->id(), patron_id => $patron->id()}
261 subtest 'Checkout to patron' => sub {
263 is(ref($checkout), 'HASH', 'Got checkout event');
264 is($checkout->{textcode}, 'SUCCESS', 'Checkout succeeded');
267 # Check the hold is fulfilled.
268 $hold = $apputils->simplereq(
270 'open-ils.pcrud.retrieve.ahr',
274 ok(defined($hold->fulfillment_time()), 'Hold was fulfilled');
277 $copy = $apputils->simplereq(
279 'open-ils.pcrud.retrieve.acp',
283 is($copy->status(), OILS_COPY_STATUS_CHECKED_OUT, 'Copy is checked out');
285 # Make a transit from BR3 to BR1 for the copy.
286 # We make the transit with pcrud, because
287 # open-ils.circ.copy_transit.create changes the copy status.
288 my $new_transit = Fieldmapper::action::transit_copy->new();
289 $new_transit->source(BR3_ID);
290 $new_transit->dest(BR1_ID);
291 $new_transit->target_copy($copy->id());
292 $new_transit->copy_status(OILS_COPY_STATUS_RESHELVING);
293 $new_transit->source_send_time('now');
294 my $pcrud_ses = $script->session('open-ils.pcrud');
295 $pcrud_ses->connect();
296 my $xact = $pcrud_ses->request(
297 'open-ils.pcrud.transaction.begin',
300 $transit = $pcrud_ses->request(
301 'open-ils.pcrud.create.atc',
306 'open-ils.pcrud.transaction.commit',
309 $pcrud_ses->disconnect();
311 isa_ok(ref($transit), 'Fieldmapper::action::transit_copy', 'Transit created');
314 $transit = $apputils->simplereq(
316 'open-ils.pcrud.search.atc',
318 {target_copy => $copy->id(), source => BR3_ID, dest => BR1_ID, dest_recv_time => undef}
320 subtest 'Got transit 1' => sub {
322 isa_ok(ref($transit), 'Fieldmapper::action::transit_copy', 'Got transit copy');
323 is($transit->dest(), BR1_ID, 'Transit destination');
324 is($transit->source(), BR3_ID, 'Transit source');
325 is($transit->copy_status(), OILS_COPY_STATUS_RESHELVING, 'Transit copy status');
329 $abort = $apputils->simplereq(
331 'open-ils.circ.transit.abort',
333 {transitid => $transit->id()}
335 is($abort, 1, 'Transit 1 aborted');
338 $copy = $apputils->simplereq(
340 'open-ils.pcrud.retrieve.acp',
344 is($copy->status(), OILS_COPY_STATUS_CHECKED_OUT, 'Copy is checked out after transit abort');
346 # Check copy in at BR3.
347 $checkin = $apputils->simplereq(
349 'open-ils.circ.checkin',
351 {barcode => CBARCODE}
353 subtest 'Check in after circulation' => sub{
355 is(ref($checkin), 'HASH', 'Got event');
356 is($checkin->{textcode}, 'ROUTE_ITEM', 'Route item event');
357 is($checkin->{org}, BR1_ID, 'ROUTE_ITEM event destination');
361 $transit = $apputils->simplereq(
363 'open-ils.pcrud.search.atc',
365 {target_copy => $copy->id(), source => BR3_ID, dest => BR1_ID, dest_recv_time => undef}
367 subtest 'Got transit after check in' => sub {
369 isa_ok(ref($transit), 'Fieldmapper::action::transit_copy', 'Got transit copy');
370 is($transit->dest(), BR1_ID, 'Transit destination');
371 is($transit->source(), BR3_ID, 'Transit source');
372 is($transit->copy_status(), OILS_COPY_STATUS_RESHELVING, 'Transit copy status');
376 $copy = $apputils->simplereq(
378 'open-ils.pcrud.retrieve.acp',
382 is($copy->status(), OILS_COPY_STATUS_IN_TRANSIT, 'Copy in transit after check in');
385 $abort = $apputils->simplereq(
387 'open-ils.circ.transit.abort',
389 {transitid => $transit->id()}
391 is($abort, 1, 'Transit aborted');
394 $copy = $apputils->simplereq(
396 'open-ils.pcrud.retrieve.acp',
400 is($copy->status(), OILS_COPY_STATUS_CANCELED_TRANSIT, 'Copy is in Canceled Transit status');
403 $script->logout($authtoken[0]);
406 $script->logout($authtoken[1]);