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',
71 my $sample_supplier2 = {
73 address1 => 'address1_2',
74 address2 => 'address2-2',
75 address3 => 'address3_2',
76 address4 => 'address4_2',
79 accountnumber => 'accountnumber2',
92 my $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
93 my $id_supplier2 = C4::Bookseller::AddBookseller($sample_supplier2);
95 #my $id_bookseller3 = C4::Bookseller::AddBookseller();# NOTE : Doesn't work because the field name cannot be null
97 like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
98 like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
99 my @b = Koha::Acquisition::Bookseller->search();
101 $count + 2, "Supplier1 and Supplier2 have been added" );
104 my $del = C4::Bookseller::DelBookseller($id_supplier1);
105 is( $del, 1, "DelBookseller returns 1 - 1 supplier has been deleted " );
106 my $b = Koha::Acquisition::Bookseller->fetch({id => $id_supplier1});
108 undef, "Supplier1 has been deleted - id_supplier1 $id_supplier1 doesnt exist anymore" );
111 my @bookseller2 = Koha::Acquisition::Bookseller->search({name => $sample_supplier2->{name} });
112 is( scalar(@bookseller2), 1, "Get only Supplier2" );
113 $bookseller2[0] = field_filter( $bookseller2[0] );
115 $sample_supplier2->{id} = $id_supplier2;
116 is_deeply( $bookseller2[0], $sample_supplier2,
117 "Koha::Acquisition::Bookseller->search returns the right informations about $sample_supplier2" );
119 $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
120 my @booksellers = Koha::Acquisition::Bookseller->search(); #NOTE :without params, it returns all the booksellers
121 for my $i ( 0 .. scalar(@booksellers) - 1 ) {
122 $booksellers[$i] = field_filter( $booksellers[$i] );
125 $sample_supplier1->{id} = $id_supplier1;
126 is( scalar(@booksellers), $count + 2, "Get Supplier1 and Supplier2" );
127 my @tab = ( $sample_supplier1, $sample_supplier2 );
128 is_deeply( \@booksellers, \@tab,
129 "Returns right fields of Supplier1 and Supplier2" );
132 my @bookseller1 = Koha::Acquisition::Bookseller->search({name => $sample_supplier1->{name} });
133 is( $bookseller1[0]->basket_count, 0, 'Supplier1 has 0 basket' );
135 C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby1', 'basketname1' );
137 C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby2', 'basketname2' );
138 @bookseller1 = Koha::Acquisition::Bookseller::search({ name => $sample_supplier1->{name} });
139 is( $bookseller1[0]->basket_count, 2, 'Supplier1 has 2 baskets' );
141 #Test Koha::Acquisition::Bookseller->new using id
142 my $bookseller1fromid = Koha::Acquisition::Bookseller->fetch;
143 is( $bookseller1fromid, undef,
144 "fetch returns undef if no id given" );
145 $bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1});
146 $bookseller1fromid = field_filter($bookseller1fromid);
147 is_deeply( $bookseller1fromid, $sample_supplier1,
148 "Get Supplier1 (fetch a bookseller by id)" );
151 $bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1});
152 is( $bookseller1fromid->basket_count, 2, 'Supplier1 has 2 baskets' );
154 #Test subscription_count
155 my $dt_today = dt_from_string;
156 my $today = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
158 my $dt_today1 = dt_from_string;
159 my $dur5 = DateTime::Duration->new( days => -5 );
160 $dt_today1->add_duration($dur5);
161 my $daysago5 = output_pref({ dt => $dt_today1, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
163 my $budgetperiod = C4::Budgets::AddBudgetPeriod({
164 budget_period_startdate => $daysago5,
165 budget_period_enddate => $today,
166 budget_period_description => "budget desc"
168 my $id_budget = AddBudget({
169 budget_code => "CODE",
170 budget_amount => "123.132",
171 budget_name => "Budgetname",
172 budget_notes => "This is a note",
173 budget_period_id => $budgetperiod
175 my $bib = MARC::Record->new();
177 MARC::Field->new('245', ' ', ' ', a => 'Journal of ethnology'),
178 MARC::Field->new('500', ' ', ' ', a => 'bib notes'),
180 my ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
181 $bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1 });
182 is( $bookseller1fromid->subscription_count,
183 0, 'Supplier1 has 0 subscription' );
185 my $id_subscription1 = NewSubscription(
186 undef, 'BRANCH2', $id_supplier1, undef, $id_budget, $biblionumber,
187 '01-01-2013',undef, undef, undef, undef,
188 undef, undef, undef, undef, undef, undef,
189 1, "subscription notes",undef, '01-01-2013', undef, undef,
190 undef, 'CALL ABC', 0, "intnotes", 0,
191 undef, undef, 0, undef, '2013-11-30', 0
194 my @subscriptions = SearchSubscriptions({biblionumber => $biblionumber});
195 is($subscriptions[0]->{publicnotes}, 'subscription notes', 'subscription search results include public notes (bug 10689)');
197 my $id_subscription2 = NewSubscription(
198 undef, 'BRANCH2', $id_supplier1, undef, $id_budget, $biblionumber,
199 '01-01-2013',undef, undef, undef, undef,
200 undef, undef, undef, undef, undef, undef,
201 1, "subscription notes",undef, '01-01-2013', undef, undef,
202 undef, 'CALL DEF', 0, "intnotes", 0,
203 undef, undef, 0, undef, '2013-07-31', 0
206 $bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1 });
207 is( $bookseller1fromid->subscription_count,
208 2, 'Supplier1 has 2 subscriptions' );
211 $sample_supplier2 = {
213 name => 'Name2 modified',
214 address1 => 'address1_2 modified',
215 address2 => 'address2-2 modified',
216 address3 => 'address3_2 modified',
217 address4 => 'address4_2 modified',
218 postal => 'postal2 modified',
219 phone => 'phone2 modified',
220 accountnumber => 'accountnumber2 modified',
221 fax => 'fax2 modified',
222 url => 'url2 modified',
227 gstrate => '2.0000 ',
228 discount => '2.0000',
229 notes => 'notes2 modified',
233 my $modif1 = C4::Bookseller::ModBookseller();
235 "ModBookseller returns undef if no params given - Nothing happened" );
236 $modif1 = C4::Bookseller::ModBookseller($sample_supplier2);
237 is( $modif1, 1, "ModBookseller modifies only the supplier2" );
238 is( scalar( Koha::Acquisition::Bookseller->search ),
239 $count + 2, "Supplier2 has been modified - Nothing added" );
241 $modif1 = C4::Bookseller::ModBookseller(
248 # "ModBookseller returns OEO if the id doesnt exist - Nothing modified" );
250 #Test GetBooksellersWithLateOrders
252 my $sample_supplier3 = {
254 address1 => 'address1_3',
255 address2 => 'address1-3',
256 address3 => 'address1_3',
257 address4 => 'address1_3',
260 accountnumber => 'accountnumber3',
268 discount => '3.0000',
272 my $sample_supplier4 = {
274 address1 => 'address1_4',
275 address2 => 'address1-4',
276 address3 => 'address1_4',
277 address4 => 'address1_4',
280 accountnumber => 'accountnumber4',
288 discount => '3.0000',
291 my $id_supplier3 = C4::Bookseller::AddBookseller($sample_supplier3);
292 my $id_supplier4 = C4::Bookseller::AddBookseller($sample_supplier4);
296 C4::Acquisition::NewBasket( $id_supplier3, 'authorisedby3', 'basketname3',
299 C4::Acquisition::NewBasket( $id_supplier4, 'authorisedby4', 'basketname4',
302 #Modify the basket to add a close date
304 basketno => $basketno1,
306 booksellerid => $id_supplier1
310 basketno => $basketno2,
311 closedate => $daysago5,
312 booksellerid => $id_supplier2
315 my $dt_today2 = dt_from_string;
316 my $dur10 = DateTime::Duration->new( days => -10 );
317 $dt_today2->add_duration($dur10);
318 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
320 basketno => $basketno3,
321 closedate => $daysago10,
325 basketno => $basketno4,
328 ModBasket($basket1info);
329 ModBasket($basket2info);
330 ModBasket($basket3info);
331 ModBasket($basket4info);
334 my $id_subscription3 = NewSubscription(
335 undef, "BRANCH1", $id_supplier1, undef, $id_budget, $biblionumber,
336 '01-01-2013',undef, undef, undef, undef,
337 undef, undef, undef, undef, undef, undef,
338 1, "subscription notes",undef, '01-01-2013', undef, undef,
339 undef, undef, 0, "intnotes", 0,
340 undef, undef, 0, 'LOCA', '2013-12-31', 0
343 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
344 is(scalar(@subscriptions), 3, 'search for subscriptions by expiration date');
345 @subscriptions = SearchSubscriptions({expiration_date => '2013-08-15'});
346 is(scalar(@subscriptions), 1, 'search for subscriptions by expiration date');
347 @subscriptions = SearchSubscriptions({callnumber => 'CALL'});
348 is(scalar(@subscriptions), 2, 'search for subscriptions by call number');
349 @subscriptions = SearchSubscriptions({callnumber => 'DEF'});
350 is(scalar(@subscriptions), 1, 'search for subscriptions by call number');
351 @subscriptions = SearchSubscriptions({location => 'LOCA'});
352 is(scalar(@subscriptions), 1, 'search for subscriptions by location');
355 my $order1 = Koha::Acquisition::Order->new(
357 basketno => $basketno1,
359 biblionumber => $biblionumber,
360 budget_id => $id_budget,
361 entrydate => '01-01-2013',
363 notes => "This is a note1",
366 subscriptionid => $id_subscription1,
367 quantityreceived => 2,
370 datereceived => '01-06-2013'
373 my $ordernumber1 = $order1->{ordernumber};
375 my $order2 = Koha::Acquisition::Order->new(
377 basketno => $basketno2,
379 biblionumber => $biblionumber,
380 budget_id => $id_budget,
381 entrydate => '01-01-2013',
383 notes => "This is a note2",
386 subscriptionid => $id_subscription2,
391 my $ordernumber2 = $order2->{ordernumber};
393 my $order3 = Koha::Acquisition::Order->new(
395 basketno => $basketno3,
397 biblionumber => $biblionumber,
398 budget_id => $id_budget,
399 entrydate => '02-02-2013',
401 notes => "This is a note3",
404 subscriptionid => $id_subscription3,
409 my $ordernumber3 = $order3->{ordernumber};
411 my $order4 = Koha::Acquisition::Order->new(
413 basketno => $basketno4,
415 biblionumber => $biblionumber,
416 budget_id => $id_budget,
417 entrydate => '02-02-2013',
419 notes => "This is a note3",
422 subscriptionid => $id_subscription3,
425 quantityreceived => 20
428 my $ordernumber4 = $order4->{ordernumber};
432 # Supplier1: delivery -> undef Basket1 : closedate -> today
433 # Supplier2: delivery -> 2 Basket2 : closedate -> $daysago5
434 # Supplier3: delivery -> 3 Basket3 : closedate -> $daysago10
435 #Case 1 : Without parameters:
436 # quantityreceived < quantity AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
437 # datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
438 # datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
439 # quantityreceived = quantity -NOT LATE-
440 my %suppliers = C4::Bookseller::GetBooksellersWithLateOrders();
441 ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders" );
442 ok( exists( $suppliers{$id_supplier2} ), "Supplier2 has late orders" );
443 ok( exists( $suppliers{$id_supplier3} ), "Supplier3 has late orders" );
444 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" )
445 ; # Quantity = quantityreceived
447 #Case 2: With $delay = 4
448 # today + 0 > now-$delay -NOT LATE-
449 # (today-5) + 2 <= now() - $delay -NOT LATE-
450 # (today-10) + 3 <= now() - $delay -LATE-
451 # quantityreceived = quantity -NOT LATE-
452 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 4, undef, undef );
453 isnt( exists( $suppliers{$id_supplier1} ),
454 1, "Supplier1 has late orders but today > now() - 4 days" );
455 isnt( exists( $suppliers{$id_supplier2} ),
456 1, "Supplier2 has late orders and $daysago5 <= now() - (4 days+2)" );
457 ok( exists( $suppliers{$id_supplier3} ),
458 "Supplier3 has late orders and $daysago10 <= now() - (4 days+3)" );
459 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
461 #Case 3: With $delay = -1
464 { $bslo = C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ) }
465 qr/^WARNING: GetBooksellerWithLateOrders is called with a negative value/,
466 "GetBooksellerWithLateOrders prints a warning on negative values";
468 is( $bslo, undef, "-1 is a wrong value for a delay" );
470 #Case 4: With $delay = 0
471 # today == now-0 -LATE- (if no deliverytime or deliverytime == 0)
472 # today-5 <= now() - $delay+2 -LATE-
473 # today-10 <= now() - $delay+3 -LATE-
474 # quantityreceived = quantity -NOT LATE-
475 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 0, undef, undef );
477 ok( exists( $suppliers{$id_supplier1} ),
478 "Supplier1 has late orders but $today == now() - 0 days" )
480 ok( exists( $suppliers{$id_supplier2} ),
481 "Supplier2 has late orders and $daysago5 <= now() - 2" );
482 ok( exists( $suppliers{$id_supplier3} ),
483 "Supplier3 has late orders and $daysago10 <= now() - 3" );
484 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
486 #Case 5 : With $estimateddeliverydatefrom = today-4
487 # today >= today-4 -NOT LATE-
488 # (today-5)+ 2 days >= today-4 -LATE-
489 # (today-10) + 3 days < today-4 -NOT LATE-
490 # quantityreceived = quantity -NOT LATE-
491 my $dt_today3 = dt_from_string;
492 my $dur4 = DateTime::Duration->new( days => -4 );
493 $dt_today3->add_duration($dur4);
494 my $daysago4 = output_pref({ dt => $dt_today3, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
496 C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago4, undef );
498 ok( exists( $suppliers{$id_supplier1} ),
499 "Supplier1 has late orders and $today >= $daysago4 -deliverytime undef" );
500 ok( exists( $suppliers{$id_supplier2} ),
501 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago4 " );
502 isnt( exists( $suppliers{$id_supplier3} ),
503 1, "Supplier3 has late orders and $daysago10 + 5 days < $daysago4 " );
504 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
506 #Case 6: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 5
507 # $daysago10<$daysago5<today -NOT LATE-
508 # $daysago10<$daysago5<$daysago5 +2 -NOT lATE-
509 # $daysago10<$daysago10 +3 <$daysago5 -LATE-
510 # quantityreceived = quantity -NOT LATE-
511 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
513 isnt( exists( $suppliers{$id_supplier1} ),
514 1, "Supplier1 has late orders but $daysago10 < $daysago5 < $today" );
516 exists( $suppliers{$id_supplier2} ),
518 "Supplier2 has late orders but $daysago10 < $daysago5 < $daysago5+2"
521 exists( $suppliers{$id_supplier3} ),
522 "Supplier3 has late orders and $daysago10 <= $daysago10 +3 <= $daysago5"
524 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
526 #Case 7: With $estimateddeliverydateto = today-5
527 # $today >= $daysago5 -NOT LATE-
528 # $daysago5 + 2 days > $daysago5 -NOT LATE-
529 # $daysago10 + 3 <+ $daysago5 -LATE-
530 # quantityreceived = quantity -NOT LATE-
532 C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago5 );
533 isnt( exists( $suppliers{$id_supplier1} ),
535 "Supplier1 has late orders but $today >= $daysago5 - deliverytime undef" );
536 isnt( exists( $suppliers{$id_supplier2} ),
537 1, "Supplier2 has late orders but $daysago5 + 2 days > $daysago5 " );
538 ok( exists( $suppliers{$id_supplier3} ),
539 "Supplier3 has late orders and $daysago10 + 3 <= $daysago5" );
540 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
542 #Test with $estimateddeliverydatefrom and $estimateddeliverydateto and $delay
543 #Case 8 :With $estimateddeliverydatefrom = 2013-07-05 and $estimateddeliverydateto = 2013-07-08 and $delay =5
544 # $daysago4<today<=$today and $today<now()-3 -NOT LATE-
545 # $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days -LATE-
546 # $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days -NOT LATE-
547 # quantityreceived = quantity -NOT LATE-
549 C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago4, $today );
551 exists( $suppliers{$id_supplier1} ),
553 "Supplier1 has late orders but $daysago4<today<=$today and $today<now()-3"
556 exists( $suppliers{$id_supplier2} ),
557 "Supplier2 has late orders and $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days"
560 exists( $suppliers{$id_supplier3} ),
561 "Supplier3 has late orders but $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days"
563 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
565 #Case 9 :With $estimateddeliverydatefrom = $daysago5 and $delay = 3
566 # $today < $daysago5 and $today > $today-5 -NOT LATE-
567 # $daysago5 + 2 days >= $daysago5 and $daysago5 < today - 3+2 -LATE-
568 # $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2-NOT LATE-
569 # quantityreceived = quantity -NOT LATE-
571 C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago5, undef );
572 isnt( exists( $suppliers{$id_supplier1} ),
573 1, "$today < $daysago10 and $today > $today-3" );
575 exists( $suppliers{$id_supplier2} ),
576 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago5 and $daysago5 < today - 3+2"
579 exists( $suppliers{$id_supplier3} ),
581 "Supplier2 has late orders but $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2 "
583 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
585 #Test with $estimateddeliverydateto and $delay
586 #Case 10:With $estimateddeliverydateto = $daysago5 and $delay = 5
587 # today > $daysago5 today > now() -5 -NOT LATE-
588 # $daysago5 + 2 days > $daysago5 and $daysago5 > now() - 2+5 days -NOT LATE-
589 # $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days -LATE-
590 # quantityreceived = quantity -NOT LATE-
592 C4::Bookseller::GetBooksellersWithLateOrders( 5, undef, $daysago5 );
593 isnt( exists( $suppliers{$id_supplier1} ),
594 1, "Supplier2 has late orders but today > $daysago5 today > now() -5" );
596 exists( $suppliers{$id_supplier2} ),
598 "Supplier2 has late orders but $daysago5 + 2 days > $daysago5 and $daysago5 > now() - 2+5 days"
601 exists( $suppliers{$id_supplier3} ),
602 "Supplier2 has late orders and $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days "
604 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
606 #Case 11: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 10
607 # $daysago10==$daysago10==$daysago10 -NOT LATE-
608 # $daysago10==$daysago10<$daysago5+2-NOT lATE-
609 # $daysago10==$daysago10 <$daysago10+3-LATE-
610 # quantityreceived = quantity -NOT LATE-
612 #Basket1 closedate -> $daysago10
614 basketno => $basketno1,
615 closedate => $daysago10,
617 ModBasket($basket1info);
618 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
620 ok( exists( $suppliers{$id_supplier1} ),
621 "Supplier1 has late orders and $daysago10==$daysago10==$daysago10 " )
623 isnt( exists( $suppliers{$id_supplier2} ),
625 "Supplier2 has late orders but $daysago10==$daysago10<$daysago5+2" );
626 isnt( exists( $suppliers{$id_supplier3} ),
628 "Supplier3 has late orders but $daysago10==$daysago10 <$daysago10+3" );
629 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
631 #Case 12: closedate == $estimateddeliverydatefrom =today-10
633 C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10, undef );
634 ok( exists( $suppliers{$id_supplier1} ),
635 "Supplier1 has late orders and $daysago10==$daysago10 " );
637 #Case 13: closedate == $estimateddeliverydateto =today-10
639 C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago10 );
640 ok( exists( $suppliers{$id_supplier1} ),
641 "Supplier1 has late orders and $daysago10==$daysago10 " )
644 C4::Context->_new_userenv('DUMMY SESSION');
645 C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, '', '');
646 my $userenv = C4::Context->userenv;
648 my $module = Test::MockModule->new('C4::Auth');
652 # simulate user that has serials permissions but
654 my ($userid, $flagsrequired) = @_;
655 return 0 if 'superserials' eq ($flagsrequired->{serials} // 0);
656 return exists($flagsrequired->{serials});
660 t::lib::Mocks::mock_preference('IndependentBranches', 0);
661 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
663 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
665 'ordinary user can see all subscriptions with IndependentBranches off'
668 t::lib::Mocks::mock_preference('IndependentBranches', 1);
669 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
671 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
673 'ordinary user can see only their library\'s subscriptions with IndependentBranches on'
676 # don the cape and turn into Superlibrarian!
677 C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 1, '', '');
678 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
680 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
682 'superlibrarian can see all subscriptions with IndependentBranches on (bug 12048)'
684 #Test contact editing
685 my $booksellerid = C4::Bookseller::AddBookseller(
688 address1 => "bookseller's address",
693 { name => 'John Smith', phone => '0123456x1' },
694 { name => 'Leo Tolstoy', phone => '0123456x2' },
698 @booksellers = Koha::Acquisition::Bookseller->search({ name => 'my vendor' });
700 ( grep { $_->{'id'} == $booksellerid } @booksellers ),
701 'Koha::Acquisition::Bookseller->search returns correct record when passed a name'
704 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
705 is( $bookseller->{'id'}, $booksellerid, 'Retrieved desired record' );
706 is( $bookseller->{'phone'}, '0123456', 'New bookseller has expected phone' );
707 my $contacts = $bookseller->contacts;
708 is( ref $bookseller->contacts,
709 'ARRAY', 'Koha::Acquisition::Bookseller->fetch returns arrayref of contacts' );
711 ref $bookseller->{'contacts'}->[0],
712 'C4::Bookseller::Contact',
713 'First contact is a contact object'
715 is( $bookseller->{'contacts'}->[0]->phone,
716 '0123456x1', 'Contact has expected phone number' );
717 is( scalar @{ $bookseller->{'contacts'} }, 2, 'Saved two contacts' );
719 pop @{ $bookseller->{'contacts'} };
720 $bookseller->{'name'} = 'your vendor';
721 $bookseller->{'contacts'}->[0]->phone('654321');
722 C4::Bookseller::ModBookseller($bookseller);
724 $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
725 $contacts = $bookseller->contacts;
726 is( $bookseller->{'name'}, 'your vendor',
727 'Successfully changed name of vendor' );
728 is( $contacts->[0]->phone,
730 'Successfully changed contact phone number by modifying bookseller hash' );
731 is( scalar @$contacts,
732 1, 'Only one contact after modification' );
734 C4::Bookseller::ModBookseller( $bookseller,
735 [ { name => 'John Jacob Jingleheimer Schmidt' } ] );
737 $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
738 $contacts = $bookseller->contacts;
740 $contacts->[0]->name,
741 'John Jacob Jingleheimer Schmidt',
742 'Changed name of contact'
744 is( $contacts->[0]->phone,
745 undef, 'Removed phone number from contact' );
746 is( scalar @$contacts,
747 1, 'Only one contact after modification' );
750 $schema->storage->txn_rollback();
752 #field_filter filters the useless fields or foreign keys
753 #NOTE: all the fields of aqbookseller arent considered
754 #returns a cleaned structure
759 'bookselleremail', 'booksellerfax',
760 'booksellerurl', 'othersupplier',
761 'currency', 'invoiceprice',
762 'listprice', 'contacts'
766 if ( grep { /^$field$/ } keys %$struct ) {
767 delete $struct->{$field};