5 use Test::More tests => 88;
11 use DateTime::Duration;
18 use Koha::Acquisition::Order;
22 use_ok('C4::Bookseller');
23 use_ok('Koha::Acquisition::Bookseller');
31 GetBooksellersWithLateOrders
36 my $dbh = C4::Context->dbh;
37 my $database = Koha::Database->new();
38 my $schema = $database->schema();
39 $schema->storage->txn_begin();
41 $dbh->{RaiseError} = 1;
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|);
50 my $count = scalar( Koha::Acquisition::Bookseller->search() );
51 my $sample_supplier1 = {
53 address1 => 'address1_1',
54 address2 => 'address1-2',
55 address3 => 'address1_2',
56 address4 => 'address1_2',
59 accountnumber => 'accountnumber1',
69 deliverytime => undef,
71 subscriptioncount => 0
73 my $sample_supplier2 = {
75 address1 => 'address1_2',
76 address2 => 'address2-2',
77 address3 => 'address3_2',
78 address4 => 'address4_2',
81 accountnumber => 'accountnumber2',
93 subscriptioncount => 0
96 my $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
97 my $id_supplier2 = C4::Bookseller::AddBookseller($sample_supplier2);
99 #my $id_bookseller3 = C4::Bookseller::AddBookseller();# NOTE : Doesn't work because the field name cannot be null
101 like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
102 like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
103 my @b = Koha::Acquisition::Bookseller->search();
105 $count + 2, "Supplier1 and Supplier2 have been added" );
108 my $del = C4::Bookseller::DelBookseller($id_supplier1);
109 is( $del, 1, "DelBookseller returns 1 - 1 supplier has been deleted " );
110 my $b = Koha::Acquisition::Bookseller->fetch({id => $id_supplier1});
112 undef, "Supplier1 has been deleted - id_supplier1 $id_supplier1 doesnt exist anymore" );
115 my @bookseller2 = Koha::Acquisition::Bookseller->search({name => $sample_supplier2->{name} });
116 is( scalar(@bookseller2), 1, "Get only Supplier2" );
117 $bookseller2[0] = field_filter( $bookseller2[0] );
119 $sample_supplier2->{id} = $id_supplier2;
120 is_deeply( $bookseller2[0], $sample_supplier2,
121 "Koha::Acquisition::Bookseller->search returns the right informations about $sample_supplier2" );
123 $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
124 my @booksellers = Koha::Acquisition::Bookseller->search(); #NOTE :without params, it returns all the booksellers
125 for my $i ( 0 .. scalar(@booksellers) - 1 ) {
126 $booksellers[$i] = field_filter( $booksellers[$i] );
129 $sample_supplier1->{id} = $id_supplier1;
130 is( scalar(@booksellers), $count + 2, "Get Supplier1 and Supplier2" );
131 my @tab = ( $sample_supplier1, $sample_supplier2 );
132 is_deeply( \@booksellers, \@tab,
133 "Returns right fields of Supplier1 and Supplier2" );
136 my @bookseller1 = Koha::Acquisition::Bookseller->search({name => $sample_supplier1->{name} });
137 is( $bookseller1[0]->basket_count, 0, 'Supplier1 has 0 basket' );
139 C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby1', 'basketname1' );
141 C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby2', 'basketname2' );
142 @bookseller1 = Koha::Acquisition::Bookseller::search({ name => $sample_supplier1->{name} });
143 is( $bookseller1[0]->basket_count, 2, 'Supplier1 has 2 baskets' );
145 #Test Koha::Acquisition::Bookseller->new using id
146 my $bookseller1fromid = Koha::Acquisition::Bookseller->fetch;
147 is( $bookseller1fromid, undef,
148 "fetch returns undef if no id given" );
149 $bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1});
150 $bookseller1fromid = field_filter($bookseller1fromid);
151 $sample_supplier1->{basketcount}=2;
152 is_deeply( $bookseller1fromid, $sample_supplier1,
153 "Get Supplier1 (fetch a bookseller by id)" );
156 $bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1});
157 is( $bookseller1fromid->basket_count, 2, 'Supplier1 has 2 baskets' );
159 #Test subscription_count
160 my $dt_today = dt_from_string;
161 my $today = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
163 my $dt_today1 = dt_from_string;
164 my $dur5 = DateTime::Duration->new( days => -5 );
165 $dt_today1->add_duration($dur5);
166 my $daysago5 = output_pref({ dt => $dt_today1, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
168 my $budgetperiod = C4::Budgets::AddBudgetPeriod({
169 budget_period_startdate => $daysago5,
170 budget_period_enddate => $today,
171 budget_period_description => "budget desc"
173 my $id_budget = AddBudget({
174 budget_code => "CODE",
175 budget_amount => "123.132",
176 budget_name => "Budgetname",
177 budget_notes => "This is a note",
178 budget_period_id => $budgetperiod
180 my $bib = MARC::Record->new();
182 MARC::Field->new('245', ' ', ' ', a => 'Journal of ethnology'),
183 MARC::Field->new('500', ' ', ' ', a => 'bib notes'),
185 my ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
186 $bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1 });
187 is( $bookseller1fromid->subscription_count,
188 0, 'Supplier1 has 0 subscription' );
190 my $id_subscription1 = NewSubscription(
191 undef, 'BRANCH2', $id_supplier1, undef, $id_budget, $biblionumber,
192 '01-01-2013',undef, undef, undef, undef,
193 undef, undef, undef, undef, undef, undef,
194 1, "subscription notes",undef, '01-01-2013', undef, undef,
195 undef, 'CALL ABC', 0, "intnotes", 0,
196 undef, undef, 0, undef, '2013-11-30', 0
199 my @subscriptions = SearchSubscriptions({biblionumber => $biblionumber});
200 is($subscriptions[0]->{publicnotes}, 'subscription notes', 'subscription search results include public notes (bug 10689)');
202 my $id_subscription2 = NewSubscription(
203 undef, 'BRANCH2', $id_supplier1, undef, $id_budget, $biblionumber,
204 '01-01-2013',undef, undef, undef, undef,
205 undef, undef, undef, undef, undef, undef,
206 1, "subscription notes",undef, '01-01-2013', undef, undef,
207 undef, 'CALL DEF', 0, "intnotes", 0,
208 undef, undef, 0, undef, '2013-07-31', 0
211 $bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1 });
212 is( $bookseller1fromid->subscription_count,
213 2, 'Supplier1 has 2 subscriptions' );
216 $sample_supplier2 = {
218 name => 'Name2 modified',
219 address1 => 'address1_2 modified',
220 address2 => 'address2-2 modified',
221 address3 => 'address3_2 modified',
222 address4 => 'address4_2 modified',
223 postal => 'postal2 modified',
224 phone => 'phone2 modified',
225 accountnumber => 'accountnumber2 modified',
226 fax => 'fax2 modified',
227 url => 'url2 modified',
232 tax_rate => '2.0000 ',
233 discount => '2.0000',
234 notes => 'notes2 modified',
238 my $modif1 = C4::Bookseller::ModBookseller();
240 "ModBookseller returns undef if no params given - Nothing happened" );
241 $modif1 = C4::Bookseller::ModBookseller($sample_supplier2);
242 is( $modif1, 1, "ModBookseller modifies only the supplier2" );
243 is( scalar( Koha::Acquisition::Bookseller->search ),
244 $count + 2, "Supplier2 has been modified - Nothing added" );
246 $modif1 = C4::Bookseller::ModBookseller(
253 # "ModBookseller returns OEO if the id doesnt exist - Nothing modified" );
255 #Test GetBooksellersWithLateOrders
257 my $sample_supplier3 = {
259 address1 => 'address1_3',
260 address2 => 'address1-3',
261 address3 => 'address1_3',
262 address4 => 'address1_3',
265 accountnumber => 'accountnumber3',
272 tax_rate => '3.0000',
273 discount => '3.0000',
277 my $sample_supplier4 = {
279 address1 => 'address1_4',
280 address2 => 'address1-4',
281 address3 => 'address1_4',
282 address4 => 'address1_4',
285 accountnumber => 'accountnumber4',
292 tax_rate => '3.0000',
293 discount => '3.0000',
296 my $id_supplier3 = C4::Bookseller::AddBookseller($sample_supplier3);
297 my $id_supplier4 = C4::Bookseller::AddBookseller($sample_supplier4);
301 C4::Acquisition::NewBasket( $id_supplier3, 'authorisedby3', 'basketname3',
304 C4::Acquisition::NewBasket( $id_supplier4, 'authorisedby4', 'basketname4',
307 #Modify the basket to add a close date
309 basketno => $basketno1,
311 booksellerid => $id_supplier1
315 basketno => $basketno2,
316 closedate => $daysago5,
317 booksellerid => $id_supplier2
320 my $dt_today2 = dt_from_string;
321 my $dur10 = DateTime::Duration->new( days => -10 );
322 $dt_today2->add_duration($dur10);
323 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
325 basketno => $basketno3,
326 closedate => $daysago10,
330 basketno => $basketno4,
333 ModBasket($basket1info);
334 ModBasket($basket2info);
335 ModBasket($basket3info);
336 ModBasket($basket4info);
339 my $id_subscription3 = NewSubscription(
340 undef, "BRANCH1", $id_supplier1, undef, $id_budget, $biblionumber,
341 '01-01-2013',undef, undef, undef, undef,
342 undef, undef, undef, undef, undef, undef,
343 1, "subscription notes",undef, '01-01-2013', undef, undef,
344 undef, undef, 0, "intnotes", 0,
345 undef, undef, 0, 'LOCA', '2013-12-31', 0
348 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
349 is(scalar(@subscriptions), 3, 'search for subscriptions by expiration date');
350 @subscriptions = SearchSubscriptions({expiration_date => '2013-08-15'});
351 is(scalar(@subscriptions), 1, 'search for subscriptions by expiration date');
352 @subscriptions = SearchSubscriptions({callnumber => 'CALL'});
353 is(scalar(@subscriptions), 2, 'search for subscriptions by call number');
354 @subscriptions = SearchSubscriptions({callnumber => 'DEF'});
355 is(scalar(@subscriptions), 1, 'search for subscriptions by call number');
356 @subscriptions = SearchSubscriptions({location => 'LOCA'});
357 is(scalar(@subscriptions), 1, 'search for subscriptions by location');
360 my $order1 = Koha::Acquisition::Order->new(
362 basketno => $basketno1,
364 biblionumber => $biblionumber,
365 budget_id => $id_budget,
366 entrydate => '01-01-2013',
368 notes => "This is a note1",
371 subscriptionid => $id_subscription1,
372 quantityreceived => 2,
375 datereceived => '01-06-2013'
378 my $ordernumber1 = $order1->{ordernumber};
380 my $order2 = Koha::Acquisition::Order->new(
382 basketno => $basketno2,
384 biblionumber => $biblionumber,
385 budget_id => $id_budget,
386 entrydate => '01-01-2013',
388 notes => "This is a note2",
391 subscriptionid => $id_subscription2,
396 my $ordernumber2 = $order2->{ordernumber};
398 my $order3 = Koha::Acquisition::Order->new(
400 basketno => $basketno3,
402 biblionumber => $biblionumber,
403 budget_id => $id_budget,
404 entrydate => '02-02-2013',
406 notes => "This is a note3",
409 subscriptionid => $id_subscription3,
414 my $ordernumber3 = $order3->{ordernumber};
416 my $order4 = Koha::Acquisition::Order->new(
418 basketno => $basketno4,
420 biblionumber => $biblionumber,
421 budget_id => $id_budget,
422 entrydate => '02-02-2013',
424 notes => "This is a note3",
427 subscriptionid => $id_subscription3,
430 quantityreceived => 20
433 my $ordernumber4 = $order4->{ordernumber};
437 # Supplier1: delivery -> undef Basket1 : closedate -> today
438 # Supplier2: delivery -> 2 Basket2 : closedate -> $daysago5
439 # Supplier3: delivery -> 3 Basket3 : closedate -> $daysago10
440 #Case 1 : Without parameters:
441 # quantityreceived < quantity AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
442 # datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
443 # datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
444 # quantityreceived = quantity -NOT LATE-
445 my %suppliers = C4::Bookseller::GetBooksellersWithLateOrders();
446 ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders" );
447 ok( exists( $suppliers{$id_supplier2} ), "Supplier2 has late orders" );
448 ok( exists( $suppliers{$id_supplier3} ), "Supplier3 has late orders" );
449 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" )
450 ; # Quantity = quantityreceived
452 #Case 2: With $delay = 4
453 # today + 0 > now-$delay -NOT LATE-
454 # (today-5) + 2 <= now() - $delay -NOT LATE-
455 # (today-10) + 3 <= now() - $delay -LATE-
456 # quantityreceived = quantity -NOT LATE-
457 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 4, undef, undef );
458 isnt( exists( $suppliers{$id_supplier1} ),
459 1, "Supplier1 has late orders but today > now() - 4 days" );
460 isnt( exists( $suppliers{$id_supplier2} ),
461 1, "Supplier2 has late orders and $daysago5 <= now() - (4 days+2)" );
462 ok( exists( $suppliers{$id_supplier3} ),
463 "Supplier3 has late orders and $daysago10 <= now() - (4 days+3)" );
464 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
466 #Case 3: With $delay = -1
469 { $bslo = C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ) }
470 qr/^WARNING: GetBooksellerWithLateOrders is called with a negative value/,
471 "GetBooksellerWithLateOrders prints a warning on negative values";
473 is( $bslo, undef, "-1 is a wrong value for a delay" );
475 #Case 4: With $delay = 0
476 # today == now-0 -LATE- (if no deliverytime or deliverytime == 0)
477 # today-5 <= now() - $delay+2 -LATE-
478 # today-10 <= now() - $delay+3 -LATE-
479 # quantityreceived = quantity -NOT LATE-
480 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 0, undef, undef );
482 ok( exists( $suppliers{$id_supplier1} ),
483 "Supplier1 has late orders but $today == now() - 0 days" )
485 ok( exists( $suppliers{$id_supplier2} ),
486 "Supplier2 has late orders and $daysago5 <= now() - 2" );
487 ok( exists( $suppliers{$id_supplier3} ),
488 "Supplier3 has late orders and $daysago10 <= now() - 3" );
489 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
491 #Case 5 : With $estimateddeliverydatefrom = today-4
492 # today >= today-4 -NOT LATE-
493 # (today-5)+ 2 days >= today-4 -LATE-
494 # (today-10) + 3 days < today-4 -NOT LATE-
495 # quantityreceived = quantity -NOT LATE-
496 my $dt_today3 = dt_from_string;
497 my $dur4 = DateTime::Duration->new( days => -4 );
498 $dt_today3->add_duration($dur4);
499 my $daysago4 = output_pref({ dt => $dt_today3, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
501 C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago4, undef );
503 ok( exists( $suppliers{$id_supplier1} ),
504 "Supplier1 has late orders and $today >= $daysago4 -deliverytime undef" );
505 ok( exists( $suppliers{$id_supplier2} ),
506 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago4 " );
507 isnt( exists( $suppliers{$id_supplier3} ),
508 1, "Supplier3 has late orders and $daysago10 + 5 days < $daysago4 " );
509 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
511 #Case 6: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 5
512 # $daysago10<$daysago5<today -NOT LATE-
513 # $daysago10<$daysago5<$daysago5 +2 -NOT lATE-
514 # $daysago10<$daysago10 +3 <$daysago5 -LATE-
515 # quantityreceived = quantity -NOT LATE-
516 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
518 isnt( exists( $suppliers{$id_supplier1} ),
519 1, "Supplier1 has late orders but $daysago10 < $daysago5 < $today" );
521 exists( $suppliers{$id_supplier2} ),
523 "Supplier2 has late orders but $daysago10 < $daysago5 < $daysago5+2"
526 exists( $suppliers{$id_supplier3} ),
527 "Supplier3 has late orders and $daysago10 <= $daysago10 +3 <= $daysago5"
529 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
531 #Case 7: With $estimateddeliverydateto = today-5
532 # $today >= $daysago5 -NOT LATE-
533 # $daysago5 + 2 days > $daysago5 -NOT LATE-
534 # $daysago10 + 3 <+ $daysago5 -LATE-
535 # quantityreceived = quantity -NOT LATE-
537 C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago5 );
538 isnt( exists( $suppliers{$id_supplier1} ),
540 "Supplier1 has late orders but $today >= $daysago5 - deliverytime undef" );
541 isnt( exists( $suppliers{$id_supplier2} ),
542 1, "Supplier2 has late orders but $daysago5 + 2 days > $daysago5 " );
543 ok( exists( $suppliers{$id_supplier3} ),
544 "Supplier3 has late orders and $daysago10 + 3 <= $daysago5" );
545 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
547 #Test with $estimateddeliverydatefrom and $estimateddeliverydateto and $delay
548 #Case 8 :With $estimateddeliverydatefrom = 2013-07-05 and $estimateddeliverydateto = 2013-07-08 and $delay =5
549 # $daysago4<today<=$today and $today<now()-3 -NOT LATE-
550 # $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days -LATE-
551 # $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days -NOT LATE-
552 # quantityreceived = quantity -NOT LATE-
554 C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago4, $today );
556 exists( $suppliers{$id_supplier1} ),
558 "Supplier1 has late orders but $daysago4<today<=$today and $today<now()-3"
561 exists( $suppliers{$id_supplier2} ),
562 "Supplier2 has late orders and $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days"
565 exists( $suppliers{$id_supplier3} ),
566 "Supplier3 has late orders but $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days"
568 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
570 #Case 9 :With $estimateddeliverydatefrom = $daysago5 and $delay = 3
571 # $today < $daysago5 and $today > $today-5 -NOT LATE-
572 # $daysago5 + 2 days >= $daysago5 and $daysago5 < today - 3+2 -LATE-
573 # $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2-NOT LATE-
574 # quantityreceived = quantity -NOT LATE-
576 C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago5, undef );
577 isnt( exists( $suppliers{$id_supplier1} ),
578 1, "$today < $daysago10 and $today > $today-3" );
580 exists( $suppliers{$id_supplier2} ),
581 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago5 and $daysago5 < today - 3+2"
584 exists( $suppliers{$id_supplier3} ),
586 "Supplier2 has late orders but $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2 "
588 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
590 #Test with $estimateddeliverydateto and $delay
591 #Case 10:With $estimateddeliverydateto = $daysago5 and $delay = 5
592 # today > $daysago5 today > now() -5 -NOT LATE-
593 # $daysago5 + 2 days > $daysago5 and $daysago5 > now() - 2+5 days -NOT LATE-
594 # $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days -LATE-
595 # quantityreceived = quantity -NOT LATE-
597 C4::Bookseller::GetBooksellersWithLateOrders( 5, undef, $daysago5 );
598 isnt( exists( $suppliers{$id_supplier1} ),
599 1, "Supplier2 has late orders but today > $daysago5 today > now() -5" );
601 exists( $suppliers{$id_supplier2} ),
603 "Supplier2 has late orders but $daysago5 + 2 days > $daysago5 and $daysago5 > now() - 2+5 days"
606 exists( $suppliers{$id_supplier3} ),
607 "Supplier2 has late orders and $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days "
609 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
611 #Case 11: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 10
612 # $daysago10==$daysago10==$daysago10 -NOT LATE-
613 # $daysago10==$daysago10<$daysago5+2-NOT lATE-
614 # $daysago10==$daysago10 <$daysago10+3-LATE-
615 # quantityreceived = quantity -NOT LATE-
617 #Basket1 closedate -> $daysago10
619 basketno => $basketno1,
620 closedate => $daysago10,
622 ModBasket($basket1info);
623 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
625 ok( exists( $suppliers{$id_supplier1} ),
626 "Supplier1 has late orders and $daysago10==$daysago10==$daysago10 " )
628 isnt( exists( $suppliers{$id_supplier2} ),
630 "Supplier2 has late orders but $daysago10==$daysago10<$daysago5+2" );
631 isnt( exists( $suppliers{$id_supplier3} ),
633 "Supplier3 has late orders but $daysago10==$daysago10 <$daysago10+3" );
634 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
636 #Case 12: closedate == $estimateddeliverydatefrom =today-10
638 C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10, undef );
639 ok( exists( $suppliers{$id_supplier1} ),
640 "Supplier1 has late orders and $daysago10==$daysago10 " );
642 #Case 13: closedate == $estimateddeliverydateto =today-10
644 C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago10 );
645 ok( exists( $suppliers{$id_supplier1} ),
646 "Supplier1 has late orders and $daysago10==$daysago10 " )
649 C4::Context->_new_userenv('DUMMY SESSION');
650 C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, '', '');
651 my $userenv = C4::Context->userenv;
653 my $module = Test::MockModule->new('C4::Auth');
657 # simulate user that has serials permissions but
659 my ($userid, $flagsrequired) = @_;
660 return 0 if 'superserials' eq ($flagsrequired->{serials} // 0);
661 return exists($flagsrequired->{serials});
665 t::lib::Mocks::mock_preference('IndependentBranches', 0);
666 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
668 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
670 'ordinary user can see all subscriptions with IndependentBranches off'
673 t::lib::Mocks::mock_preference('IndependentBranches', 1);
674 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
676 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
678 'ordinary user can see only their library\'s subscriptions with IndependentBranches on'
681 # don the cape and turn into Superlibrarian!
682 C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 1, '', '');
683 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
685 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
687 'superlibrarian can see all subscriptions with IndependentBranches on (bug 12048)'
689 #Test contact editing
690 my $booksellerid = C4::Bookseller::AddBookseller(
693 address1 => "bookseller's address",
698 { name => 'John Smith', phone => '0123456x1' },
699 { name => 'Leo Tolstoy', phone => '0123456x2' },
703 @booksellers = Koha::Acquisition::Bookseller->search({ name => 'my vendor' });
705 ( grep { $_->{'id'} == $booksellerid } @booksellers ),
706 'Koha::Acquisition::Bookseller->search returns correct record when passed a name'
709 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $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( ref $bookseller->contacts,
714 'ARRAY', 'Koha::Acquisition::Bookseller->fetch returns arrayref of contacts' );
716 ref $bookseller->{'contacts'}->[0],
717 'C4::Bookseller::Contact',
718 'First contact is a contact object'
720 is( $bookseller->{'contacts'}->[0]->phone,
721 '0123456x1', 'Contact has expected phone number' );
722 is( scalar @{ $bookseller->{'contacts'} }, 2, 'Saved two contacts' );
724 pop @{ $bookseller->{'contacts'} };
725 $bookseller->{'name'} = 'your vendor';
726 $bookseller->{'contacts'}->[0]->phone('654321');
727 C4::Bookseller::ModBookseller($bookseller);
729 $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
730 $contacts = $bookseller->contacts;
731 is( $bookseller->{'name'}, 'your vendor',
732 'Successfully changed name of vendor' );
733 is( $contacts->[0]->phone,
735 'Successfully changed contact phone number by modifying bookseller hash' );
736 is( scalar @$contacts,
737 1, 'Only one contact after modification' );
739 C4::Bookseller::ModBookseller( $bookseller,
740 [ { name => 'John Jacob Jingleheimer Schmidt' } ] );
742 $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
743 $contacts = $bookseller->contacts;
745 $contacts->[0]->name,
746 'John Jacob Jingleheimer Schmidt',
747 'Changed name of contact'
749 is( $contacts->[0]->phone,
750 undef, 'Removed phone number from contact' );
751 is( scalar @$contacts,
752 1, 'Only one contact after modification' );
755 $schema->storage->txn_rollback();
757 #field_filter filters the useless fields or foreign keys
758 #NOTE: all the fields of aqbookseller arent considered
759 #returns a cleaned structure
764 'bookselleremail', 'booksellerfax',
765 'booksellerurl', 'othersupplier',
766 'currency', 'invoiceprice',
767 'listprice', 'contacts'
771 if ( grep { /^$field$/ } keys %$struct ) {
772 delete $struct->{$field};