Bug 19893: Support for joined subfields in mappings
[koha.git] / t / db_dependent / Bookseller.t
blob48b2cef53ef4747503ba483f602c71077ce4abf0
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use Test::More tests => 86;
6 use Test::MockModule;
7 use Test::Warn;
9 use t::lib::TestBuilder;
11 use C4::Context;
12 use Koha::DateUtils;
13 use DateTime::Duration;
14 use t::lib::Mocks;
15 use C4::Acquisition;
16 use C4::Serials;
17 use C4::Budgets;
18 use C4::Biblio;
20 use Koha::Acquisition::Booksellers;
21 use Koha::Acquisition::Orders;
22 use Koha::Database;
24 BEGIN {
25 use_ok('C4::Bookseller');
28 can_ok(
30 'C4::Bookseller', qw(
31 GetBooksellersWithLateOrders
35 #Start transaction
36 my $dbh = C4::Context->dbh;
37 my $database = Koha::Database->new();
38 my $schema = $database->schema();
39 $schema->storage->txn_begin();
40 $dbh->{RaiseError} = 1;
41 my $builder = t::lib::TestBuilder->new;
43 #Start tests
44 $dbh->do(q|DELETE FROM aqorders|);
45 $dbh->do(q|DELETE FROM aqbasket|);
46 $dbh->do(q|DELETE FROM aqbooksellers|);
47 $dbh->do(q|DELETE FROM subscription|);
49 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
50 # Add currency
51 my $curcode = $builder->build({ source => 'Currency' })->{currencycode};
53 #Test AddBookseller
54 my $count = Koha::Acquisition::Booksellers->search()->count();
55 my $sample_supplier1 = {
56 name => 'Name1',
57 address1 => 'address1_1',
58 address2 => 'address1-2',
59 address3 => 'address1_2',
60 address4 => 'address1_2',
61 postal => 'postal1',
62 phone => 'phone1',
63 accountnumber => 'accountnumber1',
64 fax => 'fax1',
65 url => 'url1',
66 active => 1,
67 gstreg => 1,
68 listincgst => 1,
69 invoiceincgst => 1,
70 tax_rate => '1.0000',
71 discount => 1.0000,
72 notes => 'notes1',
73 deliverytime => undef
75 my $sample_supplier2 = {
76 name => 'Name2',
77 address1 => 'address1_2',
78 address2 => 'address2-2',
79 address3 => 'address3_2',
80 address4 => 'address4_2',
81 postal => 'postal2',
82 phone => 'phone2',
83 accountnumber => 'accountnumber2',
84 fax => 'fax2',
85 url => 'url2',
86 active => 1,
87 gstreg => 1,
88 listincgst => 1,
89 invoiceincgst => 1,
90 tax_rate => '2.0000',
91 discount => 2.0000,
92 notes => 'notes2',
93 deliverytime => 2
96 my $supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
97 my $id_supplier1 = $supplier1->id;
98 my $supplier2 = Koha::Acquisition::Bookseller->new($sample_supplier2)->store;
99 my $id_supplier2 = $supplier2->id;
101 like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
102 like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
103 is( Koha::Acquisition::Booksellers->search()->count,
104 $count + 2, "Supplier1 and Supplier2 have been added" );
106 #Test DelBookseller
107 my $del = $supplier1->delete;
108 is( $del, 1, "DelBookseller returns 1 - 1 supplier has been deleted " );
109 my $b = Koha::Acquisition::Booksellers->find( $id_supplier1 );
110 is( $b,
111 undef, "Supplier1 has been deleted - id_supplier1 $id_supplier1 doesnt exist anymore" );
113 #Test get bookseller
114 my @bookseller2 = Koha::Acquisition::Booksellers->search({name => $sample_supplier2->{name} });
115 is( scalar(@bookseller2), 1, "Get only Supplier2" );
116 for my $bookseller ( @bookseller2 ) {
117 $bookseller = field_filter( $bookseller->unblessed );
120 $sample_supplier2->{id} = $id_supplier2;
121 is_deeply( cast_precision($bookseller2[0]), $sample_supplier2,
122 "Koha::Acquisition::Booksellers->search returns the right informations about supplier $sample_supplier2->{name}" );
124 $supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
125 $id_supplier1 = $supplier1->id;
126 my @booksellers = Koha::Acquisition::Booksellers->search();
127 for my $bookseller ( @booksellers ) {
128 $bookseller = field_filter( $bookseller->unblessed );
129 $bookseller = cast_precision($bookseller);
132 $sample_supplier1->{id} = $id_supplier1;
133 is( scalar(@booksellers), $count + 2, "Get Supplier1 and Supplier2" );
134 my @tab = ( $sample_supplier2, $sample_supplier1 );
135 is_deeply( \@booksellers, \@tab,
136 "Returns right fields of Supplier1 and Supplier2" );
138 #Test baskets
139 my @bookseller1 = Koha::Acquisition::Booksellers->search({name => $sample_supplier1->{name} });
140 is( $bookseller1[0]->baskets->count, 0, 'Supplier1 has 0 basket' );
141 my $basketno1 =
142 C4::Acquisition::NewBasket( $id_supplier1, $patron->borrowernumber, 'basketname1' );
143 my $basketno2 =
144 C4::Acquisition::NewBasket( $id_supplier1, $patron->borrowernumber, 'basketname2' );
145 @bookseller1 = Koha::Acquisition::Booksellers->search({ name => $sample_supplier1->{name} });
146 is( $bookseller1[0]->baskets->count, 2, 'Supplier1 has 2 baskets' );
148 #Test Koha::Acquisition::Bookseller->new using id
149 my $bookseller1fromid = Koha::Acquisition::Booksellers->find;
150 is( $bookseller1fromid, undef,
151 "find returns undef if no id given" );
152 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
153 $bookseller1fromid = field_filter($bookseller1fromid->unblessed);
154 is_deeply( cast_precision($bookseller1fromid), $sample_supplier1,
155 "Get Supplier1 (find a bookseller by id)" );
157 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
158 is( $bookseller1fromid->baskets->count, 2, 'Supplier1 has 2 baskets' );
160 #Test subscriptions
161 my $dt_today = dt_from_string;
162 my $today = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
164 my $dt_today1 = dt_from_string;
165 my $dur5 = DateTime::Duration->new( days => -5 );
166 $dt_today1->add_duration($dur5);
167 my $daysago5 = output_pref({ dt => $dt_today1, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
169 my $budgetperiod = C4::Budgets::AddBudgetPeriod({
170 budget_period_startdate => $daysago5,
171 budget_period_enddate => $today,
172 budget_period_description => "budget desc"
174 my $id_budget = AddBudget({
175 budget_code => "CODE",
176 budget_amount => "123.132",
177 budget_name => "Budgetname",
178 budget_notes => "This is a note",
179 budget_period_id => $budgetperiod
181 my $bib = MARC::Record->new();
182 $bib->append_fields(
183 MARC::Field->new('245', ' ', ' ', a => 'Journal of ethnology'),
184 MARC::Field->new('500', ' ', ' ', a => 'bib notes'),
186 my ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
187 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
188 is( $bookseller1fromid->subscriptions->count,
189 0, 'Supplier1 has 0 subscription' );
191 my $id_subscription1 = NewSubscription(
192 undef, 'BRANCH2', $id_supplier1, undef, $id_budget, $biblionumber,
193 '2013-01-01',undef, undef, undef, undef,
194 undef, undef, undef, undef, undef, undef,
195 1, "subscription notes",undef, '2013-01-01', undef, undef,
196 undef, 'CALL ABC', 0, "intnotes", 0,
197 undef, undef, 0, undef, '2013-11-30', 0
200 my @subscriptions = SearchSubscriptions({biblionumber => $biblionumber});
201 is($subscriptions[0]->{publicnotes}, 'subscription notes', 'subscription search results include public notes (bug 10689)');
203 my $id_subscription2 = NewSubscription(
204 undef, 'BRANCH2', $id_supplier1, undef, $id_budget, $biblionumber,
205 '2013-01-01',undef, undef, undef, undef,
206 undef, undef, undef, undef, undef, undef,
207 1, "subscription notes",undef, '2013-01-01', undef, undef,
208 undef, 'CALL DEF', 0, "intnotes", 0,
209 undef, undef, 0, undef, '2013-07-31', 0
212 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
213 is( $bookseller1fromid->subscriptions->count,
214 2, 'Supplier1 has 2 subscriptions' );
216 #Test ModBookseller
217 $sample_supplier2 = {
218 id => $id_supplier2,
219 name => 'Name2 modified',
220 address1 => 'address1_2 modified',
221 address2 => 'address2-2 modified',
222 address3 => 'address3_2 modified',
223 address4 => 'address4_2 modified',
224 postal => 'postal2 modified',
225 phone => 'phone2 modified',
226 accountnumber => 'accountnumber2 modified',
227 fax => 'fax2 modified',
228 url => 'url2 modified',
229 active => 1,
230 gstreg => 1,
231 listincgst => 1,
232 invoiceincgst => 1,
233 tax_rate => '2.0000',
234 discount => 2.0000,
235 notes => 'notes2 modified',
236 deliverytime => 2,
239 my $modif1 = Koha::Acquisition::Booksellers->find($id_supplier2)->set($sample_supplier2)->store;
240 is( ref $modif1, 'Koha::Acquisition::Bookseller', "ModBookseller has updated the bookseller" );
241 is( Koha::Acquisition::Booksellers->search->count,
242 $count + 2, "Supplier2 has been modified - Nothing added" );
243 $supplier2 = Koha::Acquisition::Booksellers->find($id_supplier2);
244 is( $supplier2->name, 'Name2 modified', "supplier's name should have been modified" );
246 #Test GetBooksellersWithLateOrders
247 #Add 2 suppliers
248 my $sample_supplier3 = {
249 name => 'Name3',
250 address1 => 'address1_3',
251 address2 => 'address1-3',
252 address3 => 'address1_3',
253 address4 => 'address1_3',
254 postal => 'postal3',
255 phone => 'phone3',
256 accountnumber => 'accountnumber3',
257 fax => 'fax3',
258 url => 'url3',
259 active => 1,
260 gstreg => 1,
261 listincgst => 1,
262 invoiceincgst => 1,
263 tax_rate => '3.0000',
264 discount => 3.0000,
265 notes => 'notes3',
266 deliverytime => 3
268 my $sample_supplier4 = {
269 name => 'Name4',
270 address1 => 'address1_4',
271 address2 => 'address1-4',
272 address3 => 'address1_4',
273 address4 => 'address1_4',
274 postal => 'postal4',
275 phone => 'phone4',
276 accountnumber => 'accountnumber4',
277 fax => 'fax4',
278 url => 'url4',
279 active => 1,
280 gstreg => 1,
281 listincgst => 1,
282 invoiceincgst => 1,
283 tax_rate => '3.0000',
284 discount => 3.0000,
285 notes => 'notes3',
287 my $supplier3 = Koha::Acquisition::Bookseller->new($sample_supplier3)->store;
288 my $id_supplier3 = $supplier3->id;
289 my $supplier4 = Koha::Acquisition::Bookseller->new($sample_supplier4)->store;
290 my $id_supplier4 = $supplier4->id;
292 #Add 2 baskets
293 my $basketno3 =
294 C4::Acquisition::NewBasket( $id_supplier3, $patron->borrowernumber, 'basketname3',
295 'basketnote3' );
296 my $basketno4 =
297 C4::Acquisition::NewBasket( $id_supplier4, $patron->borrowernumber, 'basketname4',
298 'basketnote4' );
300 #Modify the basket to add a close date
301 my $basket1info = {
302 basketno => $basketno1,
303 closedate => $today,
304 booksellerid => $id_supplier1
307 my $basket2info = {
308 basketno => $basketno2,
309 closedate => $daysago5,
310 booksellerid => $id_supplier2
313 my $dt_today2 = dt_from_string;
314 my $dur10 = DateTime::Duration->new( days => -10 );
315 $dt_today2->add_duration($dur10);
316 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
317 my $basket3info = {
318 basketno => $basketno3,
319 closedate => $daysago10,
322 my $basket4info = {
323 basketno => $basketno4,
324 closedate => $today,
326 ModBasket($basket1info);
327 ModBasket($basket2info);
328 ModBasket($basket3info);
329 ModBasket($basket4info);
331 #Add 1 subscription
332 my $id_subscription3 = NewSubscription(
333 undef, "BRANCH1", $id_supplier1, undef, $id_budget, $biblionumber,
334 '2013-01-01',undef, undef, undef, undef,
335 undef, undef, undef, undef, undef, undef,
336 1, "subscription notes",undef, '2013-01-01', undef, undef,
337 undef, undef, 0, "intnotes", 0,
338 undef, undef, 0, 'LOCA', '2013-12-31', 0
341 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
342 is(scalar(@subscriptions), 3, 'search for subscriptions by expiration date');
343 @subscriptions = SearchSubscriptions({expiration_date => '2013-08-15'});
344 is(scalar(@subscriptions), 1, 'search for subscriptions by expiration date');
345 @subscriptions = SearchSubscriptions({callnumber => 'CALL'});
346 is(scalar(@subscriptions), 2, 'search for subscriptions by call number');
347 @subscriptions = SearchSubscriptions({callnumber => 'DEF'});
348 is(scalar(@subscriptions), 1, 'search for subscriptions by call number');
349 @subscriptions = SearchSubscriptions({location => 'LOCA'});
350 is(scalar(@subscriptions), 1, 'search for subscriptions by location');
352 #Add 4 orders
353 my $order1 = Koha::Acquisition::Order->new(
355 basketno => $basketno1,
356 quantity => 24,
357 biblionumber => $biblionumber,
358 budget_id => $id_budget,
359 entrydate => '2013-01-01',
360 currency => $curcode,
361 notes => "This is a note1",
362 tax_rate => 0.0500,
363 orderstatus => 1,
364 subscriptionid => $id_subscription1,
365 quantityreceived => 2,
366 rrp => 10,
367 ecost => 10,
368 datereceived => '2013-06-01'
370 )->store;
371 my $ordernumber1 = $order1->ordernumber;
373 my $order2 = Koha::Acquisition::Order->new(
375 basketno => $basketno2,
376 quantity => 20,
377 biblionumber => $biblionumber,
378 budget_id => $id_budget,
379 entrydate => '2013-01-01',
380 currency => $curcode,
381 notes => "This is a note2",
382 tax_rate => 0.0500,
383 orderstatus => 1,
384 subscriptionid => $id_subscription2,
385 rrp => 10,
386 ecost => 10,
388 )->store;
389 my $ordernumber2 = $order2->ordernumber;
391 my $order3 = Koha::Acquisition::Order->new(
393 basketno => $basketno3,
394 quantity => 20,
395 biblionumber => $biblionumber,
396 budget_id => $id_budget,
397 entrydate => '2013-02-02',
398 currency => $curcode,
399 notes => "This is a note3",
400 tax_rate => 0.0500,
401 orderstatus => 2,
402 subscriptionid => $id_subscription3,
403 rrp => 11,
404 ecost => 11,
406 )->store;
407 my $ordernumber3 = $order3->ordernumber;
409 my $order4 = Koha::Acquisition::Order->new(
411 basketno => $basketno4,
412 quantity => 20,
413 biblionumber => $biblionumber,
414 budget_id => $id_budget,
415 entrydate => '2013-02-02',
416 currency => $curcode,
417 notes => "This is a note3",
418 tax_rate => 0.0500,
419 orderstatus => 2,
420 subscriptionid => $id_subscription3,
421 rrp => 11,
422 ecost => 11,
423 quantityreceived => 20
425 )->store;
426 my $ordernumber4 = $order4->ordernumber;
428 #Test cases:
429 # Sample datas :
430 # Supplier1: delivery -> undef Basket1 : closedate -> today
431 # Supplier2: delivery -> 2 Basket2 : closedate -> $daysago5
432 # Supplier3: delivery -> 3 Basket3 : closedate -> $daysago10
433 #Case 1 : Without parameters:
434 # quantityreceived < quantity AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
435 # datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
436 # datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
437 # quantityreceived = quantity -NOT LATE-
438 my %suppliers = C4::Bookseller::GetBooksellersWithLateOrders();
439 ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders" );
440 ok( exists( $suppliers{$id_supplier2} ), "Supplier2 has late orders" );
441 ok( exists( $suppliers{$id_supplier3} ), "Supplier3 has late orders" );
442 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" )
443 ; # Quantity = quantityreceived
445 #Case 2: With $delay = 4
446 # today + 0 > now-$delay -NOT LATE-
447 # (today-5) + 2 <= now() - $delay -NOT LATE-
448 # (today-10) + 3 <= now() - $delay -LATE-
449 # quantityreceived = quantity -NOT LATE-
450 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 4, undef, undef );
451 isnt( exists( $suppliers{$id_supplier1} ),
452 1, "Supplier1 has late orders but today > now() - 4 days" );
453 isnt( exists( $suppliers{$id_supplier2} ),
454 1, "Supplier2 has late orders and $daysago5 <= now() - (4 days+2)" );
455 ok( exists( $suppliers{$id_supplier3} ),
456 "Supplier3 has late orders and $daysago10 <= now() - (4 days+3)" );
457 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
459 #Case 3: With $delay = -1
460 my $bslo;
461 warning_like
462 { $bslo = C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ) }
463 qr/^WARNING: GetBooksellerWithLateOrders is called with a negative value/,
464 "GetBooksellerWithLateOrders prints a warning on negative values";
466 is( $bslo, undef, "-1 is a wrong value for a delay" );
468 #Case 4: With $delay = 0
469 # today == now-0 -LATE- (if no deliverytime or deliverytime == 0)
470 # today-5 <= now() - $delay+2 -LATE-
471 # today-10 <= now() - $delay+3 -LATE-
472 # quantityreceived = quantity -NOT LATE-
473 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 0, undef, undef );
475 ok( exists( $suppliers{$id_supplier1} ),
476 "Supplier1 has late orders but $today == now() - 0 days" )
478 ok( exists( $suppliers{$id_supplier2} ),
479 "Supplier2 has late orders and $daysago5 <= now() - 2" );
480 ok( exists( $suppliers{$id_supplier3} ),
481 "Supplier3 has late orders and $daysago10 <= now() - 3" );
482 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
484 #Case 5 : With $estimateddeliverydatefrom = today-4
485 # today >= today-4 -NOT LATE-
486 # (today-5)+ 2 days >= today-4 -LATE-
487 # (today-10) + 3 days < today-4 -NOT LATE-
488 # quantityreceived = quantity -NOT LATE-
489 my $dt_today3 = dt_from_string;
490 my $dur4 = DateTime::Duration->new( days => -4 );
491 $dt_today3->add_duration($dur4);
492 my $daysago4 = output_pref({ dt => $dt_today3, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
493 %suppliers =
494 C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago4, undef );
496 ok( exists( $suppliers{$id_supplier1} ),
497 "Supplier1 has late orders and $today >= $daysago4 -deliverytime undef" );
498 ok( exists( $suppliers{$id_supplier2} ),
499 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago4 " );
500 isnt( exists( $suppliers{$id_supplier3} ),
501 1, "Supplier3 has late orders and $daysago10 + 5 days < $daysago4 " );
502 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
504 #Case 6: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 5
505 # $daysago10<$daysago5<today -NOT LATE-
506 # $daysago10<$daysago5<$daysago5 +2 -NOT lATE-
507 # $daysago10<$daysago10 +3 <$daysago5 -LATE-
508 # quantityreceived = quantity -NOT LATE-
509 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
510 $daysago5 );
511 isnt( exists( $suppliers{$id_supplier1} ),
512 1, "Supplier1 has late orders but $daysago10 < $daysago5 < $today" );
513 isnt(
514 exists( $suppliers{$id_supplier2} ),
516 "Supplier2 has late orders but $daysago10 < $daysago5 < $daysago5+2"
519 exists( $suppliers{$id_supplier3} ),
520 "Supplier3 has late orders and $daysago10 <= $daysago10 +3 <= $daysago5"
522 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
524 #Case 7: With $estimateddeliverydateto = today-5
525 # $today >= $daysago5 -NOT LATE-
526 # $daysago5 + 2 days > $daysago5 -NOT LATE-
527 # $daysago10 + 3 <+ $daysago5 -LATE-
528 # quantityreceived = quantity -NOT LATE-
529 %suppliers =
530 C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago5 );
531 isnt( exists( $suppliers{$id_supplier1} ),
533 "Supplier1 has late orders but $today >= $daysago5 - deliverytime undef" );
534 isnt( exists( $suppliers{$id_supplier2} ),
535 1, "Supplier2 has late orders but $daysago5 + 2 days > $daysago5 " );
536 ok( exists( $suppliers{$id_supplier3} ),
537 "Supplier3 has late orders and $daysago10 + 3 <= $daysago5" );
538 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
540 #Test with $estimateddeliverydatefrom and $estimateddeliverydateto and $delay
541 #Case 8 :With $estimateddeliverydatefrom = 2013-07-05 and $estimateddeliverydateto = 2013-07-08 and $delay =5
542 # $daysago4<today<=$today and $today<now()-3 -NOT LATE-
543 # $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days -LATE-
544 # $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days -NOT LATE-
545 # quantityreceived = quantity -NOT LATE-
546 %suppliers =
547 C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago4, $today );
548 isnt(
549 exists( $suppliers{$id_supplier1} ),
551 "Supplier1 has late orders but $daysago4<today<=$today and $today<now()-3"
554 exists( $suppliers{$id_supplier2} ),
555 "Supplier2 has late orders and $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days"
557 isnt(
558 exists( $suppliers{$id_supplier3} ),
559 "Supplier3 has late orders but $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days"
561 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
563 #Case 9 :With $estimateddeliverydatefrom = $daysago5 and $delay = 3
564 # $today < $daysago5 and $today > $today-5 -NOT LATE-
565 # $daysago5 + 2 days >= $daysago5 and $daysago5 < today - 3+2 -LATE-
566 # $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2-NOT LATE-
567 # quantityreceived = quantity -NOT LATE-
568 %suppliers =
569 C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago5, undef );
570 isnt( exists( $suppliers{$id_supplier1} ),
571 1, "$today < $daysago10 and $today > $today-3" );
573 exists( $suppliers{$id_supplier2} ),
574 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago5 and $daysago5 < today - 3+2"
576 isnt(
577 exists( $suppliers{$id_supplier3} ),
579 "Supplier2 has late orders but $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2 "
581 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
583 #Test with $estimateddeliverydateto and $delay
584 #Case 10:With $estimateddeliverydateto = $daysago5 and $delay = 5
585 # today > $daysago5 today > now() -5 -NOT LATE-
586 # $daysago5 + 2 days > $daysago5 and $daysago5 > now() - 2+5 days -NOT LATE-
587 # $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days -LATE-
588 # quantityreceived = quantity -NOT LATE-
589 %suppliers =
590 C4::Bookseller::GetBooksellersWithLateOrders( 5, undef, $daysago5 );
591 isnt( exists( $suppliers{$id_supplier1} ),
592 1, "Supplier2 has late orders but today > $daysago5 today > now() -5" );
593 isnt(
594 exists( $suppliers{$id_supplier2} ),
596 "Supplier2 has late orders but $daysago5 + 2 days > $daysago5 and $daysago5 > now() - 2+5 days"
599 exists( $suppliers{$id_supplier3} ),
600 "Supplier2 has late orders and $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days "
602 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
604 #Case 11: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 10
605 # $daysago10==$daysago10==$daysago10 -NOT LATE-
606 # $daysago10==$daysago10<$daysago5+2-NOT lATE-
607 # $daysago10==$daysago10 <$daysago10+3-LATE-
608 # quantityreceived = quantity -NOT LATE-
610 #Basket1 closedate -> $daysago10
611 $basket1info = {
612 basketno => $basketno1,
613 closedate => $daysago10,
615 ModBasket($basket1info);
616 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
617 $daysago10 );
618 ok( exists( $suppliers{$id_supplier1} ),
619 "Supplier1 has late orders and $daysago10==$daysago10==$daysago10 " )
621 isnt( exists( $suppliers{$id_supplier2} ),
623 "Supplier2 has late orders but $daysago10==$daysago10<$daysago5+2" );
624 isnt( exists( $suppliers{$id_supplier3} ),
626 "Supplier3 has late orders but $daysago10==$daysago10 <$daysago10+3" );
627 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
629 #Case 12: closedate == $estimateddeliverydatefrom =today-10
630 %suppliers =
631 C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10, undef );
632 ok( exists( $suppliers{$id_supplier1} ),
633 "Supplier1 has late orders and $daysago10==$daysago10 " );
635 #Case 13: closedate == $estimateddeliverydateto =today-10
636 %suppliers =
637 C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago10 );
638 ok( exists( $suppliers{$id_supplier1} ),
639 "Supplier1 has late orders and $daysago10==$daysago10 " )
642 C4::Context->_new_userenv('DUMMY SESSION');
643 C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, '', '');
644 my $userenv = C4::Context->userenv;
646 my $module = Test::MockModule->new('C4::Auth');
647 $module->mock(
648 'haspermission',
649 sub {
650 # simulate user that has serials permissions but
651 # NOT superserials
652 my ($userid, $flagsrequired) = @_;
653 return 0 if 'superserials' eq ($flagsrequired->{serials} // 0);
654 return exists($flagsrequired->{serials});
658 t::lib::Mocks::mock_preference('IndependentBranches', 0);
659 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
661 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
663 'ordinary user can see all subscriptions with IndependentBranches off'
666 t::lib::Mocks::mock_preference('IndependentBranches', 1);
667 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
669 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
671 'ordinary user can see only their library\'s subscriptions with IndependentBranches on'
674 # don the cape and turn into Superlibrarian!
675 C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 1, '', '');
676 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
678 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
680 'superlibrarian can see all subscriptions with IndependentBranches on (bug 12048)'
683 #Test contact editing
684 my $sample_supplier = {
685 name => "my vendor",
686 address1 => "bookseller's address",
687 phone => "0123456",
688 active => 1
690 my $supplier = Koha::Acquisition::Bookseller->new($sample_supplier)->store;
691 my $booksellerid = $supplier->id;
692 my $contact1 = Koha::Acquisition::Bookseller::Contact->new({
693 name => 'John Smith',
694 phone => '0123456x1',
695 booksellerid => $booksellerid,
696 })->store;
697 my $contact2 = Koha::Acquisition::Bookseller::Contact->new({
698 name => 'Leo Tolstoy',
699 phone => '0123456x2',
700 booksellerid => $booksellerid,
701 })->store;
703 @booksellers = Koha::Acquisition::Booksellers->search({ name => 'my vendor' });
705 ( grep { $_->id == $booksellerid } @booksellers ),
706 'Koha::Acquisition::Booksellers->search returns correct record when passed a name'
709 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
710 is( $bookseller->id, $booksellerid, 'Retrieved desired record' );
711 is( $bookseller->phone, '0123456', 'New bookseller has expected phone' );
712 my $contacts = $bookseller->contacts;
713 is( $contacts->count,
714 2, 'bookseller should have 2 contacts' );
715 my $first_contact = $contacts->next;
717 ref $first_contact,
718 'Koha::Acquisition::Bookseller::Contact',
719 'First contact is a AqContact'
721 is( $first_contact->phone,
722 '0123456x1', 'Contact has expected phone number' );
724 my $second_contact = $contacts->next;
725 $second_contact->delete;
726 $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
727 $bookseller->name('your vendor')->store;
728 $contacts = $bookseller->contacts;
729 $first_contact = $contacts->next;
730 $first_contact->phone('654321');
731 $first_contact->store;
733 $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
734 is( $bookseller->name, 'your vendor',
735 'Successfully changed name of vendor' );
736 $contacts = $bookseller->contacts;
737 is( $contacts->count,
738 1, 'Only one contact after modification' );
739 $first_contact = $contacts->next;
740 is( $first_contact->phone,
741 '654321',
742 'Successfully changed contact phone number by modifying bookseller hash' );
744 $first_contact->name( 'John Jacob Jingleheimer Schmidt' );
745 $first_contact->phone(undef);
746 $first_contact->store;
748 $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
749 $contacts = $bookseller->contacts;
750 $first_contact = $contacts->next;
752 $first_contact->name,
753 'John Jacob Jingleheimer Schmidt',
754 'Changed name of contact'
756 is( $first_contact->phone,
757 undef, 'Removed phone number from contact' );
758 is( $contacts->count,
759 1, 'Only one contact after modification' );
761 #End transaction
762 $schema->storage->txn_rollback();
764 #field_filter filters the useless fields or foreign keys
765 #NOTE: all the fields of aqbookseller arent considered
766 #returns a cleaned structure
767 sub field_filter {
768 my ($struct) = @_;
770 for my $field (
771 'bookselleremail', 'booksellerfax',
772 'booksellerurl', 'othersupplier',
773 'currency', 'invoiceprice',
774 'listprice', 'contacts'
778 if ( grep { /^$field$/ } keys %$struct ) {
779 delete $struct->{$field};
782 return $struct;
785 # ensure numbers are actually tested as numbers to prevent
786 # precision changes causing test failures (D8->D9 Upgrades)
787 sub cast_precision {
788 my ($struct) = @_;
789 my @cast = ('discount');
790 for my $cast (@cast) {
791 $struct->{$cast} = $struct->{$cast}+0;
793 return $struct;