]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/live_t/lp1883171-copy-inventory.t
LP2045292 Color contrast for AngularJS patron bills
[Evergreen.git] / Open-ILS / src / perlmods / live_t / lp1883171-copy-inventory.t
1 #!perl
2 use strict; use warnings;
3 use Test::More;
4 use OpenILS::Utils::TestUtils;
5 use OpenILS::Const qw(:const);
6
7 my $script = OpenILS::Utils::TestUtils->new();
8 my $U = 'OpenILS::Application::AppUtils';
9
10 diag('LP1883171&1940663 Copy Inventory Date');
11
12 use constant {
13     BR1_ID => 4,
14     BR2_ID => 5,
15     SYS1_ID => 2,
16     SYS1_FGROUP => "Sys1 Floating Group",
17     CIRC_USER => 'br1mtownsend',
18     CIRC_USER_PWD => 'maryt1234',
19     CIRC_WORKSTATION => 'BR1-lp1883171-live_t',
20 };
21
22 # Login as staff
23 my $credentials = {
24     username => CIRC_USER,
25     password => CIRC_USER_PWD,
26     type => 'staff'
27 };
28 my $authtoken = $script->authenticate($credentials);
29 ok(
30     $authtoken,
31     'Logged in'
32 ) or BAIL_OUT('Must log in');
33
34 # Find or register the workstation:
35 my $ws = $script->find_or_register_workstation(CIRC_WORKSTATION, BR1_ID);
36 ok(
37     ! ref $ws,
38     'Found or registered workstation'
39 ) or BAIL_OUT('Need workstation');
40
41 # Logout so we can use the workstation.
42 $script->logout();
43 ok(
44     ! $script->authtoken,
45     'Logged out'
46 );
47
48 # Login with workstation
49 $credentials->{workstation} = CIRC_WORKSTATION;
50 $credentials->{password} = CIRC_USER_PWD; # Have to reset the password.
51 $authtoken = $script->authenticate($credentials);
52 ok(
53     $script->authtoken,
54     'Logged in with workstation'
55 ) or BAIL_OUT('Must log in with workstation');
56
57 # Create an cstore editor with our current authtoken
58 my $editor = $script->editor(authtoken=>$authtoken);
59
60 # Create a floating group for SYS1:
61 my $cfg = Fieldmapper::config::floating_group->new;
62 $cfg->name(SYS1_FGROUP);
63 $cfg->manual('f');
64 $editor->xact_begin();
65 $cfg = $editor->create_config_floating_group($cfg);
66 ok(
67     $cfg,
68     'Floating Group created successfully'
69 ) or BAIL_OUT('Need Floating Group');
70 $cfg = $editor->search_config_floating_group({name=>SYS1_FGROUP})->[0];
71 # Add SYS1 as a member:
72 my $cfgm = Fieldmapper::config::floating_group_member->new;
73 $cfgm->floating_group($cfg->id());
74 $cfgm->org_unit(SYS1_ID);
75 $cfgm->stop_depth(1);
76 $cfgm = $editor->create_config_floating_group_member($cfgm);
77 ok(
78     $cfgm,
79     'Floating group member created successfully'
80 ) or BAIL_OUT('Need floating group member');
81 $editor->xact_commit;
82
83 # find 2 BR1 copies checked out at BR1:
84 my $copies = $editor->search_asset_copy([
85     {
86         circ_lib => BR1_ID,
87         status => 1
88     },
89     {
90         join => {
91             circ => {
92                 filter => {
93                     circ_lib => BR1_ID,
94                     checkin_scan_time => undef
95                 }
96             }
97         },
98         limit=>2
99     }
100 ]);
101 ok(
102     $copies && scalar(@$copies) == 2,
103     'Got two checked out copies'
104 );
105 # Check first in without inventory update and the other with:
106 my $do_inventory = 0;
107 foreach my $copy (@$copies) {
108     my $args = {
109         barcode => $copy->barcode,
110         do_inventory_update => $do_inventory
111     };
112     my $resp = $script->do_checkin($args);
113     is(
114         $resp->{textcode},
115         'SUCCESS',
116         'Copy checked in'
117     );
118     my $circ = $resp->{payload}->{circ};
119     isa_ok(
120         $circ,
121         'Fieldmapper::action::circulation'
122     );
123     my $scan_time = substr($circ->checkin_scan_time, 0, 19);
124     $copy = $resp->{payload}->{copy};
125     isa_ok(
126         $copy,
127         'Fieldmapper::asset::copy'
128     );
129     my $inventory = $copy->latest_inventory();
130     if ($inventory) {
131         my $inv_time = substr($inventory->inventory_date(), 0, 19);
132         if ($do_inventory) {
133             is(
134                 $inv_time,
135                 $scan_time,
136                 'Inventory date equals checkin scan time'
137             );
138         } else {
139             isnt(
140                 $inv_time,
141                 $scan_time,
142                 'Inventory date does not equal checkin scan time'
143             );
144         }
145     } else {
146         if ($do_inventory) {
147             BAIL_OUT('Inventory not created on checkin');
148         }
149     }
150
151     $do_inventory++;
152 }
153
154 # Find 2 BR2 copies checked out at BR1:
155 $copies = $editor->search_asset_copy([
156     {
157         circ_lib => BR2_ID,
158         status => 1
159     },
160     {
161         join => {
162             circ => {
163                 circ_lib => BR1_ID,
164                 checkin_scan_time => undef
165             }
166         },
167         limit=>2
168     }
169 ]);
170 ok(
171     $copies && scalar(@$copies) == 2,
172     'Got two checked out copies'
173 );
174 # Set the first one to floating:
175 my $fcopy = $copies->[0];
176 $fcopy->floating($cfg->id());
177 $editor->xact_begin;
178 $fcopy = $editor->update_asset_copy($fcopy);
179 $editor->xact_commit;
180 ok(
181     $fcopy,
182     'First BR2 copy set to floating group'
183 );
184 # Check them both in with inventory update.
185 for (my $i = 0; $i < scalar(@$copies); $i++) {
186     my $copy = $copies->[$i];
187     my $args = {
188         barcode => $copy->barcode,
189         do_inventory_update => $do_inventory
190     };
191     my $resp = $script->do_checkin($args);
192     is(
193         $resp->{textcode},
194         ($i == 0) ? 'SUCCESS' : 'ROUTE_ITEM',
195         'Copy checked in'
196     );
197     my $circ = $resp->{payload}->{circ};
198     isa_ok(
199         $circ,
200         'Fieldmapper::action::circulation'
201     );
202     my $scan_time = substr($circ->checkin_scan_time, 0, 19);
203     $copy = $resp->{payload}->{copy};
204     isa_ok(
205         $copy,
206         'Fieldmapper::asset::copy'
207     );
208     my $inventory = $copy->latest_inventory();
209     if ($i == 0) {
210         if ($inventory) {
211             my $inv_time = substr($inventory->inventory_date(), 0, 19);
212             is(
213                 $inv_time,
214                 $scan_time,
215                 'Inventory date equals checkin scan time'
216             );
217         } else {
218             BAIL_OUT('Inventory not created on checkin');
219         }
220     } else {
221         if ($inventory) {
222             my $inv_time = substr($inventory->inventory_date(), 0, 19);
223             isnt(
224                 $inv_time,
225                 $scan_time,
226                 'Inventory date does not equal checkin scan time'
227             );
228         } else {
229             pass('Second copy does not have inventory');
230         }
231     }
232 }
233
234 # Find an available copy at BR1:
235 $copies = $editor->search_asset_copy([
236     {
237         circ_lib => BR1_ID,
238         status => 0
239     },
240     {limit=>1}
241 ]);
242 ok(
243     $copies && scalar(@$copies) == 1,
244     'Got an available copy'
245 ) or BAIL_OUT('Need an available copy');
246
247 my $resp = $U->simplereq(
248     'open-ils.circ',
249     'open-ils.circ.circulation.update_copy_inventory',
250     $authtoken,
251     {copy_list=>[$copies->[0]->id()]}
252 );
253 is(
254     $resp->[0],
255     1,
256     'Update copy inventory succeeded'
257 );
258
259 my $inventories = $editor->search_asset_copy_inventory([
260     {
261         copy => $copies->[0]->id
262     },
263     {
264         order_by => [
265             {
266                 class => 'aci',
267                 field => 'inventory_date',
268                 direction => 'desc'
269             }
270         ]
271     }
272 ]);
273 ok(
274     $inventories && scalar(@$inventories),
275     'Got copy inventory'
276 ) or BAIL_OUT('Need copy inventory');
277
278 my $aci = $inventories->[0];
279
280 my $alci = $editor->retrieve_asset_latest_inventory($aci->id());
281 ok(
282     $alci,
283     'Got latest inventory for copy'
284 );
285 is(
286     $alci->id(),
287     $aci->id(),
288     'Inventory IDs match'
289 );
290 is(
291     $alci->inventory_date(),
292     $aci->inventory_date(),
293     'Inventory dates match'
294 );
295 is(
296     $alci->inventory_workstation(),
297     $aci->inventory_workstation(),
298     'Inventory workstations match'
299 );
300 is(
301     $alci->copy(),
302     $aci->copy(),
303     'Inventory copies match'
304 );
305
306 # Now, try 2 copies at BR2
307 $copies = $editor->search_asset_copy([
308     {
309         circ_lib => BR2_ID,
310         status => 0
311     },
312     {limit=>2}
313 ]);
314 ok(
315     $copies && scalar(@$copies) == 2,
316     'Got two copies'
317 ) or BAIL_OUT('Need 2 copies');
318
319 $resp = $U->simplereq(
320     'open-ils.circ',
321     'open-ils.circ.circulation.update_copy_inventory',
322     $authtoken,
323     {copy_list=>[$copies->[0]->id()]}
324 );
325 is(
326     $resp->[0],
327     0,
328     'Update copy inventory should have 0 success'
329 );
330 is(
331     $resp->[1],
332     1,
333     'Update copy inventory should have 1 failure'
334 );
335 # Make the second one float and it should succeed.
336 $fcopy = $copies->[1];
337 $fcopy->floating($cfg->id());
338 $editor->xact_begin;
339 if ($editor->update_asset_copy($fcopy)) {
340     $editor->xact_commit;
341     $resp = $U->simplereq(
342         'open-ils.circ',
343         'open-ils.circ.circulation.update_copy_inventory',
344         $authtoken,
345         {copy_list=>[$fcopy->id()]}
346     );
347     is(
348         $resp->[0],
349         1,
350         'Update inventory for floating copy'
351     );
352 } else {
353     $editor->xact_rollback;
354     BAIL_OUT('Set copy floating failed');
355 }
356
357 # Test a batch update where some succeed and some fail.
358 $resp = $editor->search_asset_copy([
359     {circ_lib => BR2_ID, status => 0, floating => undef},
360     {limit => 5, idlist => 1}
361 ]);
362 ok(
363     $resp && scalar(@{$resp}) == 5,
364     'Got 5 copies from branch 2'
365 );
366 undef($copies);
367 push(@{$copies}, @{$resp});
368 $resp = $editor->search_asset_copy([
369     {circ_lib => BR1_ID, status => 0},
370     {limit => 5, idlist => 1}
371 ]);
372 ok(
373     $resp && scalar(@{$resp}) == 5,
374     'Got 5 copies from branch 1'
375 );
376 push(@{$copies}, @{$resp});
377 $resp = $U->simplereq(
378     'open-ils.circ',
379     'open-ils.circ.circulation.update_copy_inventory',
380     $authtoken,
381     {copy_list=>$copies}
382 );
383 is(
384     $resp->[0],
385     5,
386     'Updated inventory on 5 copies'
387 );
388 is(
389     $resp->[1],
390     5,
391     'Did not update inventory on 5 copies'
392 );
393
394 # We could run 36 or more tests depending on what we find in the
395 # database, so we don't specify a number of tests.
396 done_testing();
397
398 # Just to make sure we're done.
399 $editor->disconnect();
400 $script->logout();