3 use Test::More tests => 55;
5 diag('Item price vs cost settings');
7 use constant WORKSTATION_NAME => 'BR1-test-33-lp1905028-price-vs-cost.t';
8 use constant WORKSTATION_LIB => 4;
10 use strict; use warnings;
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';
19 our $script = OpenILS::Utils::TestUtils->new();
22 our $e = new_editor(xact => 1);
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;
47 my $copy = $e->search_asset_copy([{ id => 404 }, {} ])->[0];
51 is($value, 0, 'no settings, price = undef, cost = undef, value = 0');
56 is($value, 0, 'no settings, price = 0, cost = undef, value = 0');
61 is($value, 2, 'no settings, price = 2, cost = undef, value = 2');
66 is($value, 0, 'no settings, price = undef, cost = 0, value = 0');
71 is($value, 0, 'no settings, price = 0, cost = 0, value = 0');
76 is($value, 2, 'no settings, price = 2, cost = 0, value = 2');
81 is($value, 0, 'no settings, price = undef, cost = 3, value = 0');
86 is($value, 0, 'no settings, price = 0, cost = 3, value = 0');
91 is($value, 2, 'no settings, price = 2, cost = 3, value = 2');
93 adjust_setting(1, OILS_SETTING_DEF_ITEM_PRICE, 4);
94 $e->commit; $e = new_editor(xact => 1); $e->init;
100 is($value, 4, 'def item price = 4, price = undef, cost = undef, value = 4');
105 is($value, 0, 'def item price = 4, price = 0, cost = undef, value = 0');
110 is($value, 2, 'def item price = 4, price = 2, cost = undef, value = 2');
112 $copy->clear_price();
115 is($value, 4, 'def item price = 4, price = undef, cost = 0, value = 4');
120 is($value, 0, 'def item price = 4, price = 0, cost = 0, value = 0');
125 is($value, 2, 'def item price = 4, price = 2, cost = 0, value = 2');
127 $copy->clear_price();
130 is($value, 4, 'def item price = 4, price = undef, cost = 3, value = 4');
135 is($value, 0, 'def item price = 4, price = 0, cost = 3, value = 0');
140 is($value, 2, 'def item price = 4, price = 2, cost = 3, value = 2');
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;
148 $copy->clear_price();
151 is($value, 0, 'primary = cost, price = undef, cost = undef, value = 0');
156 is($value, 0, 'primary = cost, price = 0, cost = undef, value = 0');
161 is($value, 0, 'primary = cost, price = 2, cost = undef, value = 0');
163 $copy->clear_price();
166 is($value, 0, 'primary = cost, price = undef, cost = 0, value = 0');
171 is($value, 0, 'primary = cost, price = 0, cost = 0, value = 0');
176 is($value, 0, 'primary = cost, price = 2, cost = 0, value = 0');
178 $copy->clear_price();
181 is($value, 3, 'primary = cost, price = undef, cost = 3, value = 3');
186 is($value, 3, 'primary = cost, price = 0, cost = 3, value = 3');
191 is($value, 3, 'primary = cost, price = 2, cost = 3, value = 3');
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;
199 $copy->clear_price();
202 is($value, 4, 'def item price = 4, primary = cost, price = undef, cost = undef, value = 4');
207 is($value, 4, 'def item price = 4, primary = cost, price = 0, cost = undef, value = 4');
212 is($value, 4, 'def item price = 4, primary = cost, price = 2, cost = undef, value = 4');
214 $copy->clear_price();
217 is($value, 0, 'def item price = 4, primary = cost, price = undef, cost = 0, value = 0');
222 is($value, 0, 'def item price = 4, primary = cost, price = 0, cost = 0, value = 0');
227 is($value, 0, 'def item price = 4, primary = cost, price = 2, cost = 0, value = 0');
229 $copy->clear_price();
232 is($value, 3, 'def item price = 4, primary = cost, price = undef, cost = 3, value = 3');
237 is($value, 3, 'def item price = 4, primary = cost, price = 0, cost = 3, value = 3');
242 is($value, 3, 'def item price = 4, primary = cost, price = 2, cost = 3, value = 3');
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;
252 $copy->clear_price();
255 is($value, 4, 'def item price = 4, primary = cost, secondary = price, price = undef, cost = undef, value = 4');
260 is($value, 0, 'def item price = 4, primary = cost, secondary = price, price = 0, cost = undef, value = 0');
265 is($value, 2, 'def item price = 4, primary = cost, secondary = price, price = 2, cost = undef, value = 2');
267 $copy->clear_price();
270 is($value, 0, 'def item price = 4, primary = cost, secondary = price, price = undef, cost = 0, value = 0');
275 is($value, 0, 'def item price = 4, primary = cost, secondary = price, price = 0, cost = 0, value = 0');
280 is($value, 0, 'def item price = 4, primary = cost, secondary = price, price = 2, cost = 0, value = 0');
282 $copy->clear_price();
285 is($value, 3, 'def item price = 4, primary = cost, secondary = price, price = undef, cost = 3, value = 3');
290 is($value, 3, 'def item price = 4, primary = cost, secondary = price, price = 0, cost = 3, value = 3');
295 is($value, 3, 'def item price = 4, primary = cost, secondary = price, price = 2, cost = 3, value = 3');
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;
307 $copy->clear_price();
310 is($value, 4, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = undef, cost = undef, value = 4');
315 is($value, 4, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = 0, cost = undef, value = 4');
320 is($value, 2, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = 2, cost = undef, value = 2');
322 $copy->clear_price();
325 is($value, 4, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = undef, cost = 0, value = 4');
330 is($value, 4, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = 0, cost = 0, value = 4');
335 is($value, 2, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = 2, cost = 0, value = 2');
337 $copy->clear_price();
340 is($value, 3, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = undef, cost = 3, value = 3');
345 is($value, 3, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = 0, cost = 3, value = 3');
350 is($value, 3, 'charge_on_zero = true, def item price = 4, primary = cost, secondary = price, price = 2, cost = 3, value = 3');
356 my ($org, $setting) = (shift, shift);
357 my $obj = $e->search_actor_org_unit_setting([{ org_unit => $org, name => $setting }, {} ])->[0];
359 $e->delete_actor_org_unit_setting($obj);
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);
371 return $update ? $e->update_actor_org_unit_setting($obj) : $e->create_actor_org_unit_setting($obj);
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");
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");
401 my $workstation = $e->search_actor_workstation([ {name => WORKSTATION_NAME, owning_lib => WORKSTATION_LIB } ])->[0];
405 $script->authenticate({
407 password => 'demo123',
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');
415 $script->authenticate({
417 password => 'demo123',
419 workstation => WORKSTATION_NAME});
420 ok( $script->authtoken, 'Have an authtoken associated with the workstation');