]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/live_t/33-lp1905028-price-vs-cost.t
lp1905028 lost items and price versus acq cost
[Evergreen.git] / Open-ILS / src / perlmods / live_t / 33-lp1905028-price-vs-cost.t
1 #!perl
2
3 use Test::More tests => 55;
4
5 diag('Item price vs cost settings');
6
7 use constant WORKSTATION_NAME => 'BR1-test-33-lp1905028-price-vs-cost.t';
8 use constant WORKSTATION_LIB => 4;
9
10 use strict; use warnings;
11
12 use OpenILS::Utils::TestUtils;
13 use OpenILS::Utils::CStoreEditor qw/:funcs/;
14 use OpenILS::Utils::Fieldmapper;
15 use OpenILS::Const qw(:const);
16 use OpenILS::Application::AppUtils;
17 our $U = 'OpenILS::Application::AppUtils';
18
19 our $script = OpenILS::Utils::TestUtils->new();
20 $script->bootstrap;
21
22 our $e = new_editor(xact => 1);
23 $e->init;
24
25 setupLogin();
26
27 delete_setting(1, OILS_SETTING_DEF_ITEM_PRICE);
28 delete_setting(1, OILS_SETTING_MIN_ITEM_PRICE);
29 delete_setting(1, OILS_SETTING_MAX_ITEM_PRICE);
30 delete_setting(1, OILS_SETTING_CHARGE_LOST_ON_ZERO);
31 delete_setting(1, OILS_SETTING_PRIMARY_ITEM_VALUE_FIELD);
32 delete_setting(1, OILS_SETTING_SECONDARY_ITEM_VALUE_FIELD);
33 $e->commit; $e = new_editor(xact => 1); $e->init;
34
35 my $def_price;
36 my $min_price;
37 my $max_price;
38 my $charge_on_0;
39 my $primary_field;
40 my $backup_field;
41 fetchSettings();
42
43 my $price;
44 my $cost;
45 my $value;
46
47 my $copy = $e->search_asset_copy([{ id => 404 }, {} ])->[0];
48 $copy->clear_price();
49 $copy->clear_cost();
50 summarize();
51 is($value, 0, 'no settings, price = undef, cost = undef, value = 0');
52
53 $copy->price(0);
54 $copy->clear_cost();
55 summarize();
56 is($value, 0, 'no settings, price = 0, cost = undef, value = 0');
57
58 $copy->price(2);
59 $copy->clear_cost();
60 summarize();
61 is($value, 2, 'no settings, price = 2, cost = undef, value = 2');
62
63 $copy->clear_price();
64 $copy->cost(0);
65 summarize();
66 is($value, 0, 'no settings, price = undef, cost = 0, value = 0');
67
68 $copy->price(0);
69 $copy->cost(0);
70 summarize();
71 is($value, 0, 'no settings, price = 0, cost = 0, value = 0');
72
73 $copy->price(2);
74 $copy->cost(0);
75 summarize();
76 is($value, 2, 'no settings, price = 2, cost = 0, value = 2');
77
78 $copy->clear_price();
79 $copy->cost(3);
80 summarize();
81 is($value, 0, 'no settings, price = undef, cost = 3, value = 0');
82
83 $copy->price(0);
84 $copy->cost(0);
85 summarize();
86 is($value, 0, 'no settings, price = 0, cost = 3, value = 0');
87
88 $copy->price(2);
89 $copy->cost(0);
90 summarize();
91 is($value, 2, 'no settings, price = 2, cost = 3, value = 2');
92
93 adjust_setting(1, OILS_SETTING_DEF_ITEM_PRICE, 4);
94 $e->commit; $e = new_editor(xact => 1); $e->init;
95 fetchSettings();
96
97 $copy->clear_price();
98 $copy->clear_cost();
99 summarize();
100 is($value, 4, 'def item price = 4, price = undef, cost = undef, value = 4');
101
102 $copy->price(0);
103 $copy->clear_cost();
104 summarize();
105 is($value, 0, 'def item price = 4, price = 0, cost = undef, value = 0');
106
107 $copy->price(2);
108 $copy->clear_cost();
109 summarize();
110 is($value, 2, 'def item price = 4, price = 2, cost = undef, value = 2');
111
112 $copy->clear_price();
113 $copy->cost(0);
114 summarize();
115 is($value, 4, 'def item price = 4, price = undef, cost = 0, value = 4');
116
117 $copy->price(0);
118 $copy->cost(0);
119 summarize();
120 is($value, 0, 'def item price = 4, price = 0, cost = 0, value = 0');
121
122 $copy->price(2);
123 $copy->cost(0);
124 summarize();
125 is($value, 2, 'def item price = 4, price = 2, cost = 0, value = 2');
126
127 $copy->clear_price();
128 $copy->cost(3);
129 summarize();
130 is($value, 4, 'def item price = 4, price = undef, cost = 3, value = 4');
131
132 $copy->price(0);
133 $copy->cost(3);
134 summarize();
135 is($value, 0, 'def item price = 4, price = 0, cost = 3, value = 0');
136
137 $copy->price(2);
138 $copy->cost(3);
139 summarize();
140 is($value, 2, 'def item price = 4, price = 2, cost = 3, value = 2');
141
142 delete_setting(1, OILS_SETTING_DEF_ITEM_PRICE);
143 $e->commit; $e = new_editor(xact => 1); $e->init;
144 adjust_setting(1, OILS_SETTING_PRIMARY_ITEM_VALUE_FIELD, '"cost"');
145 $e->commit; $e = new_editor(xact => 1); $e->init;
146 fetchSettings();
147
148 $copy->clear_price();
149 $copy->clear_cost();
150 summarize();
151 is($value, 0, 'primary = cost, price = undef, cost = undef, value = 0');
152
153 $copy->price(0);
154 $copy->clear_cost();
155 summarize();
156 is($value, 0, 'primary = cost, price = 0, cost = undef, value = 0');
157
158 $copy->price(2);
159 $copy->clear_cost();
160 summarize();
161 is($value, 0, 'primary = cost, price = 2, cost = undef, value = 0');
162
163 $copy->clear_price();
164 $copy->cost(0);
165 summarize();
166 is($value, 0, 'primary = cost, price = undef, cost = 0, value = 0');
167
168 $copy->price(0);
169 $copy->cost(0);
170 summarize();
171 is($value, 0, 'primary = cost, price = 0, cost = 0, value = 0');
172
173 $copy->price(2);
174 $copy->cost(0);
175 summarize();
176 is($value, 0, 'primary = cost, price = 2, cost = 0, value = 0');
177
178 $copy->clear_price();
179 $copy->cost(3);
180 summarize();
181 is($value, 3, 'primary = cost, price = undef, cost = 3, value = 3');
182
183 $copy->price(0);
184 $copy->cost(3);
185 summarize();
186 is($value, 3, 'primary = cost, price = 0, cost = 3, value = 3');
187
188 $copy->price(2);
189 $copy->cost(3);
190 summarize();
191 is($value, 3, 'primary = cost, price = 2, cost = 3, value = 3');
192
193 adjust_setting(1, OILS_SETTING_DEF_ITEM_PRICE, 4);
194 $e->commit; $e = new_editor(xact => 1); $e->init;
195 adjust_setting(1, OILS_SETTING_PRIMARY_ITEM_VALUE_FIELD, '"cost"');
196 $e->commit; $e = new_editor(xact => 1); $e->init;
197 fetchSettings();
198
199 $copy->clear_price();
200 $copy->clear_cost();
201 summarize();
202 is($value, 4, 'def item price = 4, primary = cost, price = undef, cost = undef, value = 4');
203
204 $copy->price(0);
205 $copy->clear_cost();
206 summarize();
207 is($value, 4, 'def item price = 4, primary = cost, price = 0, cost = undef, value = 4');
208
209 $copy->price(2);
210 $copy->clear_cost();
211 summarize();
212 is($value, 4, 'def item price = 4, primary = cost, price = 2, cost = undef, value = 4');
213
214 $copy->clear_price();
215 $copy->cost(0);
216 summarize();
217 is($value, 0, 'def item price = 4, primary = cost, price = undef, cost = 0, value = 0');
218
219 $copy->price(0);
220 $copy->cost(0);
221 summarize();
222 is($value, 0, 'def item price = 4, primary = cost, price = 0, cost = 0, value = 0');
223
224 $copy->price(2);
225 $copy->cost(0);
226 summarize();
227 is($value, 0, 'def item price = 4, primary = cost, price = 2, cost = 0, value = 0');
228
229 $copy->clear_price();
230 $copy->cost(3);
231 summarize();
232 is($value, 3, 'def item price = 4, primary = cost, price = undef, cost = 3, value = 3');
233
234 $copy->price(0);
235 $copy->cost(3);
236 summarize();
237 is($value, 3, 'def item price = 4, primary = cost, price = 0, cost = 3, value = 3');
238
239 $copy->price(2);
240 $copy->cost(3);
241 summarize();
242 is($value, 3, 'def item price = 4, primary = cost, price = 2, cost = 3, value = 3');
243
244 adjust_setting(1, OILS_SETTING_DEF_ITEM_PRICE, 4);
245 $e->commit; $e = new_editor(xact => 1); $e->init;
246 adjust_setting(1, OILS_SETTING_PRIMARY_ITEM_VALUE_FIELD, '"cost"');
247 $e->commit; $e = new_editor(xact => 1); $e->init;
248 adjust_setting(1, OILS_SETTING_SECONDARY_ITEM_VALUE_FIELD, '"price"');
249 $e->commit; $e = new_editor(xact => 1); $e->init;
250 fetchSettings();
251
252 $copy->clear_price();
253 $copy->clear_cost();
254 summarize();
255 is($value, 4, 'def item price = 4, primary = cost, secondary = price, price = undef, cost = undef, value = 4');
256
257 $copy->price(0);
258 $copy->clear_cost();
259 summarize();
260 is($value, 0, 'def item price = 4, primary = cost, secondary = price, price = 0, cost = undef, value = 0');
261
262 $copy->price(2);
263 $copy->clear_cost();
264 summarize();
265 is($value, 2, 'def item price = 4, primary = cost, secondary = price, price = 2, cost = undef, value = 2');
266
267 $copy->clear_price();
268 $copy->cost(0);
269 summarize();
270 is($value, 0, 'def item price = 4, primary = cost, secondary = price, price = undef, cost = 0, value = 0');
271
272 $copy->price(0);
273 $copy->cost(0);
274 summarize();
275 is($value, 0, 'def item price = 4, primary = cost, secondary = price, price = 0, cost = 0, value = 0');
276
277 $copy->price(2);
278 $copy->cost(0);
279 summarize();
280 is($value, 0, 'def item price = 4, primary = cost, secondary = price, price = 2, cost = 0, value = 0');
281
282 $copy->clear_price();
283 $copy->cost(3);
284 summarize();
285 is($value, 3, 'def item price = 4, primary = cost, secondary = price, price = undef, cost = 3, value = 3');
286
287 $copy->price(0);
288 $copy->cost(3);
289 summarize();
290 is($value, 3, 'def item price = 4, primary = cost, secondary = price, price = 0, cost = 3, value = 3');
291
292 $copy->price(2);
293 $copy->cost(3);
294 summarize();
295 is($value, 3, 'def item price = 4, primary = cost, secondary = price, price = 2, cost = 3, value = 3');
296
297 adjust_setting(1, OILS_SETTING_DEF_ITEM_PRICE, 4);
298 $e->commit; $e = new_editor(xact => 1); $e->init;
299 adjust_setting(1, OILS_SETTING_PRIMARY_ITEM_VALUE_FIELD, '"cost"');
300 $e->commit; $e = new_editor(xact => 1); $e->init;
301 adjust_setting(1, OILS_SETTING_SECONDARY_ITEM_VALUE_FIELD, '"price"');
302 $e->commit; $e = new_editor(xact => 1); $e->init;
303 adjust_setting(1, OILS_SETTING_CHARGE_LOST_ON_ZERO, '"true"');
304 $e->commit; $e = new_editor(xact => 1); $e->init;
305 fetchSettings();
306
307 $copy->clear_price();
308 $copy->clear_cost();
309 summarize();
310 is($value, 4, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = undef, cost = undef, value = 4');
311
312 $copy->price(0);
313 $copy->clear_cost();
314 summarize();
315 is($value, 4, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = 0, cost = undef, value = 4');
316
317 $copy->price(2);
318 $copy->clear_cost();
319 summarize();
320 is($value, 2, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = 2, cost = undef, value = 2');
321
322 $copy->clear_price();
323 $copy->cost(0);
324 summarize();
325 is($value, 4, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = undef, cost = 0, value = 4');
326
327 $copy->price(0);
328 $copy->cost(0);
329 summarize();
330 is($value, 4, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = 0, cost = 0, value = 4');
331
332 $copy->price(2);
333 $copy->cost(0);
334 summarize();
335 is($value, 2, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = 2, cost = 0, value = 2');
336
337 $copy->clear_price();
338 $copy->cost(3);
339 summarize();
340 is($value, 3, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = undef, cost = 3, value = 3');
341
342 $copy->price(0);
343 $copy->cost(3);
344 summarize();
345 is($value, 3, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = 0, cost = 3, value = 3');
346
347 $copy->price(2);
348 $copy->cost(3);
349 summarize();
350 is($value, 3, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = 2, cost = 3, value = 3');
351
352
353 ####################
354
355 sub delete_setting {
356     my ($org, $setting) = (shift, shift);
357     my $obj = $e->search_actor_org_unit_setting([{ org_unit => $org, name => $setting }, {} ])->[0];
358     if (defined $obj) {
359         $e->delete_actor_org_unit_setting($obj);
360     }
361 }
362
363 sub adjust_setting {
364     my ($org, $setting, $value) = (shift, shift, shift);
365     my $obj = $e->search_actor_org_unit_setting([{ org_unit => $org, name => $setting }, {} ])->[0];
366     my $update = defined $obj;
367     $obj = Fieldmapper::actor::org_unit_setting->new unless $update;
368     $obj->org_unit($org);
369     $obj->name($setting);
370     $obj->value($value);
371     return $update ? $e->update_actor_org_unit_setting($obj) : $e->create_actor_org_unit_setting($obj);
372 }
373
374 sub fetchSettings {
375     $def_price = $U->ou_ancestor_setting_value(1, OILS_SETTING_DEF_ITEM_PRICE, $e);
376     $min_price = $U->ou_ancestor_setting_value(1, OILS_SETTING_MIN_ITEM_PRICE, $e);
377     $max_price = $U->ou_ancestor_setting_value(1, OILS_SETTING_MAX_ITEM_PRICE, $e);
378     $charge_on_0 = $U->ou_ancestor_setting_value(1, OILS_SETTING_CHARGE_LOST_ON_ZERO, $e);
379     $primary_field = $U->ou_ancestor_setting_value(1, OILS_SETTING_PRIMARY_ITEM_VALUE_FIELD, $e);
380     $backup_field = $U->ou_ancestor_setting_value(1, OILS_SETTING_SECONDARY_ITEM_VALUE_FIELD, $e);
381     $def_price = defined $def_price ? $def_price : '';
382     $min_price = defined $min_price ? $min_price : '';
383     $max_price = defined $max_price ? $max_price : '';
384     $charge_on_0 = defined $charge_on_0 ? $charge_on_0 : '';
385     $primary_field = defined $primary_field ? $primary_field : '';
386     $backup_field = defined $backup_field ? $backup_field : '';
387     diag("def_price = $def_price charge_on_0 = $charge_on_0 primary_field = $primary_field backup_field = $backup_field");
388 }
389
390 sub summarize {
391     $value = $U->get_copy_price($e, $copy, $copy->call_number);
392     $value = length $value ? $value : '';
393     $price = length $copy->price ? $copy->price : '';
394     $cost = length $copy->cost ? $copy->cost : '';
395     #diag("Using settings -> def_price: $def_price min_price: $min_price max_price: $max_price charge_on_0: $charge_on_0 primary: $primary_field backup: $backup_field");
396     #diag("Using copy " . $copy->id . " -> price: $price cost: $cost value: $value");
397 }
398
399 sub setupLogin {
400
401     my $workstation = $e->search_actor_workstation([ {name => WORKSTATION_NAME, owning_lib => WORKSTATION_LIB } ])->[0];
402
403     if(!$workstation )
404     {
405         $script->authenticate({
406             username => 'admin',
407             password => 'demo123',
408             type => 'staff'});
409         ok( $script->authtoken, 'Have an authtoken');
410         my $ws = $script->register_workstation(WORKSTATION_NAME,WORKSTATION_LIB);
411         ok( ! ref $ws, 'Registered a new workstation');
412         $script->logout();
413     }
414
415     $script->authenticate({
416         username => 'admin',
417         password => 'demo123',
418         type => 'staff',
419         workstation => WORKSTATION_NAME});
420     ok( $script->authtoken, 'Have an authtoken associated with the workstation');
421 }