5 use Test::More tests => 85;
9 use DateTime::Duration;
16 use_ok('C4::Bookseller');
26 GetBooksellersWithLateOrders
31 my $dbh = C4::Context->dbh;
32 $dbh->{RaiseError} = 1;
33 $dbh->{AutoCommit} = 0;
36 $dbh->do(q|DELETE FROM aqorders|);
37 $dbh->do(q|DELETE FROM aqbasket|);
38 $dbh->do(q|DELETE FROM aqbooksellers|);
39 $dbh->do(q|DELETE FROM subscription|);
42 my $count = scalar( C4::Bookseller::GetBookSeller('') );
43 my $sample_supplier1 = {
45 address1 => 'address1_1',
46 address2 => 'address1-2',
47 address3 => 'address1_2',
48 address4 => 'address1_2',
51 accountnumber => 'accountnumber1',
63 my $sample_supplier2 = {
65 address1 => 'address1_2',
66 address2 => 'address2-2',
67 address3 => 'address3_2',
68 address4 => 'address4_2',
71 accountnumber => 'accountnumber2',
84 my $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
85 my $id_supplier2 = C4::Bookseller::AddBookseller($sample_supplier2);
87 #my $id_bookseller3 = C4::Bookseller::AddBookseller();# NOTE : Doesn't work because the field name cannot be null
89 like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
90 like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
91 is( scalar( C4::Bookseller::GetBookSeller('') ),
92 $count + 2, "Supplier1 and Supplier2 have been added" );
95 my $del = C4::Bookseller::DelBookseller($id_supplier1);
96 is( $del, 1, "DelBookseller returns 1 - 1 supplier has been deleted " );
97 is( C4::Bookseller::GetBookSellerFromId($id_supplier1),
98 undef, "Supplier1 has been deleted - id_supplier1 doesnt exist anymore" );
101 my @bookseller2 = C4::Bookseller::GetBookSeller( $sample_supplier2->{name} );
102 is( scalar(@bookseller2), 1, "Get only Supplier2" );
103 $bookseller2[0] = field_filter( $bookseller2[0] );
104 delete $bookseller2[0]->{basketcount};
106 $sample_supplier2->{id} = $id_supplier2;
107 is_deeply( $bookseller2[0], $sample_supplier2,
108 "GetBookSeller returns the right informations about $sample_supplier2" );
110 $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
111 my @booksellers = C4::Bookseller::GetBookSeller('')
112 ; #NOTE :without params, it returns all the booksellers
113 for my $i ( 0 .. scalar(@booksellers) - 1 ) {
114 $booksellers[$i] = field_filter( $booksellers[$i] );
115 delete $booksellers[$i]->{basketcount};
118 $sample_supplier1->{id} = $id_supplier1;
119 is( scalar(@booksellers), $count + 2, "Get Supplier1 and Supplier2" );
120 my @tab = ( $sample_supplier1, $sample_supplier2 );
121 is_deeply( \@booksellers, \@tab,
122 "Returns right fields of Supplier1 and Supplier2" );
125 my @bookseller1 = C4::Bookseller::GetBookSeller( $sample_supplier1->{name} );
126 #FIXME : if there is 0 basket, GetBookSeller returns 1 as basketcount
127 #is( $bookseller1[0]->{basketcount}, 0, 'Supplier1 has 0 basket' );
129 C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby1', 'basketname1' );
131 C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby2', 'basketname2' );
132 @bookseller1 = C4::Bookseller::GetBookSeller( $sample_supplier1->{name} );
133 is( $bookseller1[0]->{basketcount}, 2, 'Supplier1 has 2 baskets' );
135 #Test GetBookSellerFromId
136 my $bookseller1fromid = C4::Bookseller::GetBookSellerFromId();
137 is( $bookseller1fromid, undef,
138 "GetBookSellerFromId returns undef if no id given" );
139 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
140 $bookseller1fromid = field_filter($bookseller1fromid);
141 delete $bookseller1fromid->{basketcount};
142 delete $bookseller1fromid->{subscriptioncount};
143 is_deeply( $bookseller1fromid, $sample_supplier1,
144 "Get Supplier1 (GetBookSellerFromId)" );
147 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
148 is( $bookseller1fromid->{basketcount}, 2, 'Supplier1 has 2 baskets' );
150 #Test subscriptioncount
151 my $dt_today = dt_from_string;
152 my $today = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
154 my $dt_today1 = dt_from_string;
155 my $dur5 = DateTime::Duration->new( days => -5 );
156 $dt_today1->add_duration($dur5);
157 my $daysago5 = output_pref({ dt => $dt_today1, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
159 my $budgetperiod = C4::Budgets::AddBudgetPeriod({
160 budget_period_startdate => $daysago5,
161 budget_period_enddate => $today,
162 budget_period_description => "budget desc"
164 my $id_budget = AddBudget({
165 budget_code => "CODE",
166 budget_amount => "123.132",
167 budget_name => "Budgetname",
168 budget_notes => "This is a note",
169 budget_period_id => $budgetperiod
171 my $bib = MARC::Record->new();
173 MARC::Field->new('245', ' ', ' ', a => 'Journal of ethnology'),
174 MARC::Field->new('500', ' ', ' ', a => 'bib notes'),
176 my ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
177 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
178 is( $bookseller1fromid->{subscriptioncount},
179 0, 'Supplier1 has 0 subscription' );
181 my $id_subscription1 = NewSubscription(
182 undef, 'BRANCH2', $id_supplier1, undef, $id_budget, $biblionumber,
183 '01-01-2013',undef, undef, undef, undef,
184 undef, undef, undef, undef, undef, undef,
185 1, "subscription notes",undef, '01-01-2013', undef, undef,
186 undef, 'CALL ABC', 0, "intnotes", 0,
187 undef, undef, 0, undef, '2013-11-30', 0
190 my @subscriptions = SearchSubscriptions({biblionumber => $biblionumber});
191 is($subscriptions[0]->{publicnotes}, 'subscription notes', 'subscription search results include public notes (bug 10689)');
193 my $id_subscription2 = NewSubscription(
194 undef, 'BRANCH2', $id_supplier1, undef, $id_budget, $biblionumber,
195 '01-01-2013',undef, undef, undef, undef,
196 undef, undef, undef, undef, undef, undef,
197 1, "subscription notes",undef, '01-01-2013', undef, undef,
198 undef, 'CALL DEF', 0, "intnotes", 0,
199 undef, undef, 0, undef, '2013-07-31', 0
202 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
203 is( $bookseller1fromid->{subscriptioncount},
204 2, 'Supplier1 has 2 subscriptions' );
207 $sample_supplier2 = {
209 name => 'Name2 modified',
210 address1 => 'address1_2 modified',
211 address2 => 'address2-2 modified',
212 address3 => 'address3_2 modified',
213 address4 => 'address4_2 modified',
214 postal => 'postal2 modified',
215 phone => 'phone2 modified',
216 accountnumber => 'accountnumber2 modified',
217 fax => 'fax2 modified',
218 url => 'url2 modified',
223 gstrate => '2.0000 ',
224 discount => '2.0000',
225 notes => 'notes2 modified',
229 my $modif1 = C4::Bookseller::ModBookseller();
231 "ModBookseller returns undef if no params given - Nothing happened" );
232 $modif1 = C4::Bookseller::ModBookseller($sample_supplier2);
233 is( $modif1, 1, "ModBookseller modifies only the supplier2" );
234 is( scalar( C4::Bookseller::GetBookSeller('') ),
235 $count + 2, "Supplier2 has been modified - Nothing added" );
237 $modif1 = C4::Bookseller::ModBookseller(
244 # "ModBookseller returns OEO if the id doesnt exist - Nothing modified" );
246 #Test GetBooksellersWithLateOrders
248 my $sample_supplier3 = {
250 address1 => 'address1_3',
251 address2 => 'address1-3',
252 address3 => 'address1_3',
253 address4 => 'address1_3',
256 accountnumber => 'accountnumber3',
264 discount => '3.0000',
268 my $sample_supplier4 = {
270 address1 => 'address1_4',
271 address2 => 'address1-4',
272 address3 => 'address1_4',
273 address4 => 'address1_4',
276 accountnumber => 'accountnumber4',
284 discount => '3.0000',
287 my $id_supplier3 = C4::Bookseller::AddBookseller($sample_supplier3);
288 my $id_supplier4 = C4::Bookseller::AddBookseller($sample_supplier4);
292 C4::Acquisition::NewBasket( $id_supplier3, 'authorisedby3', 'basketname3',
295 C4::Acquisition::NewBasket( $id_supplier4, 'authorisedby4', 'basketname4',
298 #Modify the basket to add a close date
300 basketno => $sample_basket1,
302 booksellerid => $id_supplier1
306 basketno => $sample_basket2,
307 closedate => $daysago5,
308 booksellerid => $id_supplier2
311 my $dt_today2 = dt_from_string;
312 my $dur10 = DateTime::Duration->new( days => -10 );
313 $dt_today2->add_duration($dur10);
314 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
316 basketno => $sample_basket3,
317 closedate => $daysago10,
321 basketno => $sample_basket4,
324 ModBasket($basket1info);
325 ModBasket($basket2info);
326 ModBasket($basket3info);
327 ModBasket($basket4info);
330 my $id_subscription3 = NewSubscription(
331 undef, "BRANCH1", $id_supplier1, undef, $id_budget, $biblionumber,
332 '01-01-2013',undef, undef, undef, undef,
333 undef, undef, undef, undef, undef, undef,
334 1, "subscription notes",undef, '01-01-2013', undef, undef,
335 undef, undef, 0, "intnotes", 0,
336 undef, undef, 0, 'LOCA', '2013-12-31', 0
339 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
340 is(scalar(@subscriptions), 3, 'search for subscriptions by expiration date');
341 @subscriptions = SearchSubscriptions({expiration_date => '2013-08-15'});
342 is(scalar(@subscriptions), 1, 'search for subscriptions by expiration date');
343 @subscriptions = SearchSubscriptions({callnumber => 'CALL'});
344 is(scalar(@subscriptions), 2, 'search for subscriptions by call number');
345 @subscriptions = SearchSubscriptions({callnumber => 'DEF'});
346 is(scalar(@subscriptions), 1, 'search for subscriptions by call number');
347 @subscriptions = SearchSubscriptions({location => 'LOCA'});
348 is(scalar(@subscriptions), 1, 'search for subscriptions by location');
351 my ( $ordernumber1, $ordernumber2, $ordernumber3, $ordernumber4 );
352 my ( $basketno1, $basketno2, $basketno3, $basketno4 );
353 ( $basketno1, $ordernumber1 ) = C4::Acquisition::NewOrder(
355 basketno => $sample_basket1,
357 biblionumber => $biblionumber,
358 budget_id => $id_budget,
359 entrydate => '01-01-2013',
361 notes => "This is a note1",
364 subscriptionid => $id_subscription1,
365 quantityreceived => 2,
368 datereceived => '01-06-2013'
371 ( $basketno2, $ordernumber2 ) = C4::Acquisition::NewOrder(
373 basketno => $sample_basket2,
375 biblionumber => $biblionumber,
376 budget_id => $id_budget,
377 entrydate => '01-01-2013',
379 notes => "This is a note2",
382 subscriptionid => $id_subscription2,
387 ( $basketno3, $ordernumber3 ) = C4::Acquisition::NewOrder(
389 basketno => $sample_basket3,
391 biblionumber => $biblionumber,
392 budget_id => $id_budget,
393 entrydate => '02-02-2013',
395 notes => "This is a note3",
398 subscriptionid => $id_subscription3,
403 ( $basketno4, $ordernumber4 ) = C4::Acquisition::NewOrder(
405 basketno => $sample_basket4,
407 biblionumber => $biblionumber,
408 budget_id => $id_budget,
409 entrydate => '02-02-2013',
411 notes => "This is a note3",
414 subscriptionid => $id_subscription3,
417 quantityreceived => 20
423 # Supplier1: delivery -> undef Basket1 : closedate -> today
424 # Supplier2: delivery -> 2 Basket2 : closedate -> $daysago5
425 # Supplier3: delivery -> 3 Basket3 : closedate -> $daysago10
426 #Case 1 : Without parameters:
427 # quantityreceived < quantity AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
428 # datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
429 # datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
430 # quantityreceived = quantity -NOT LATE-
431 my %suppliers = C4::Bookseller::GetBooksellersWithLateOrders();
432 ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders" );
433 ok( exists( $suppliers{$id_supplier2} ), "Supplier2 has late orders" );
434 ok( exists( $suppliers{$id_supplier3} ), "Supplier3 has late orders" );
435 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" )
436 ; # Quantity = quantityreceived
438 #Case 2: With $delay = 4
439 # today + 0 > now-$delay -NOT LATE-
440 # (today-5) + 2 <= now() - $delay -NOT LATE-
441 # (today-10) + 3 <= now() - $delay -LATE-
442 # quantityreceived = quantity -NOT LATE-
443 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 4, undef, undef );
444 isnt( exists( $suppliers{$id_supplier1} ),
445 1, "Supplier1 has late orders but today > now() - 4 days" );
446 isnt( exists( $suppliers{$id_supplier2} ),
447 1, "Supplier2 has late orders and $daysago5 <= now() - (4 days+2)" );
448 ok( exists( $suppliers{$id_supplier3} ),
449 "Supplier3 has late orders and $daysago10 <= now() - (4 days+3)" );
450 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
452 #Case 3: With $delay = -1
453 is( C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ),
454 undef, "-1 is a wrong value for a delay" );
456 #Case 4: With $delay = 0
457 # today == now-0 -LATE- (if no deliverytime or deliverytime == 0)
458 # today-5 <= now() - $delay+2 -LATE-
459 # today-10 <= now() - $delay+3 -LATE-
460 # quantityreceived = quantity -NOT LATE-
461 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 0, undef, undef );
463 ok( exists( $suppliers{$id_supplier1} ),
464 "Supplier1 has late orders but $today == now() - 0 days" )
466 ok( exists( $suppliers{$id_supplier2} ),
467 "Supplier2 has late orders and $daysago5 <= now() - 2" );
468 ok( exists( $suppliers{$id_supplier3} ),
469 "Supplier3 has late orders and $daysago10 <= now() - 3" );
470 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
472 #Case 5 : With $estimateddeliverydatefrom = today-4
473 # today >= today-4 -NOT LATE-
474 # (today-5)+ 2 days >= today-4 -LATE-
475 # (today-10) + 3 days < today-4 -NOT LATE-
476 # quantityreceived = quantity -NOT LATE-
477 my $dt_today3 = dt_from_string;
478 my $dur4 = DateTime::Duration->new( days => -4 );
479 $dt_today3->add_duration($dur4);
480 my $daysago4 = output_pref({ dt => $dt_today3, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
482 C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago4, undef );
484 ok( exists( $suppliers{$id_supplier1} ),
485 "Supplier1 has late orders and $today >= $daysago4 -deliverytime undef" );
486 ok( exists( $suppliers{$id_supplier2} ),
487 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago4 " );
488 isnt( exists( $suppliers{$id_supplier3} ),
489 1, "Supplier3 has late orders and $daysago10 + 5 days < $daysago4 " );
490 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
492 #Case 6: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 5
493 # $daysago10<$daysago5<today -NOT LATE-
494 # $daysago10<$daysago5<$daysago5 +2 -NOT lATE-
495 # $daysago10<$daysago10 +3 <$daysago5 -LATE-
496 # quantityreceived = quantity -NOT LATE-
497 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
499 isnt( exists( $suppliers{$id_supplier1} ),
500 1, "Supplier1 has late orders but $daysago10 < $daysago5 < $today" );
502 exists( $suppliers{$id_supplier2} ),
504 "Supplier2 has late orders but $daysago10 < $daysago5 < $daysago5+2"
507 exists( $suppliers{$id_supplier3} ),
508 "Supplier3 has late orders and $daysago10 <= $daysago10 +3 <= $daysago5"
510 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
512 #Case 7: With $estimateddeliverydateto = today-5
513 # $today >= $daysago5 -NOT LATE-
514 # $daysago5 + 2 days > $daysago5 -NOT LATE-
515 # $daysago10 + 3 <+ $daysago5 -LATE-
516 # quantityreceived = quantity -NOT LATE-
518 C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago5 );
519 isnt( exists( $suppliers{$id_supplier1} ),
521 "Supplier1 has late orders but $today >= $daysago5 - deliverytime undef" );
522 isnt( exists( $suppliers{$id_supplier2} ),
523 1, "Supplier2 has late orders but $daysago5 + 2 days > $daysago5 " );
524 ok( exists( $suppliers{$id_supplier3} ),
525 "Supplier3 has late orders and $daysago10 + 3 <= $daysago5" );
526 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
528 #Test with $estimateddeliverydatefrom and $estimateddeliverydateto and $delay
529 #Case 8 :With $estimateddeliverydatefrom = 2013-07-05 and $estimateddeliverydateto = 2013-07-08 and $delay =5
530 # $daysago4<today<=$today and $today<now()-3 -NOT LATE-
531 # $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days -LATE-
532 # $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days -NOT LATE-
533 # quantityreceived = quantity -NOT LATE-
535 C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago4, $today );
537 exists( $suppliers{$id_supplier1} ),
539 "Supplier1 has late orders but $daysago4<today<=$today and $today<now()-3"
542 exists( $suppliers{$id_supplier2} ),
543 "Supplier2 has late orders and $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days"
546 exists( $suppliers{$id_supplier3} ),
547 "Supplier3 has late orders but $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days"
549 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
551 #Case 9 :With $estimateddeliverydatefrom = $daysago5 and $delay = 3
552 # $today < $daysago5 and $today > $today-5 -NOT LATE-
553 # $daysago5 + 2 days >= $daysago5 and $daysago5 < today - 3+2 -LATE-
554 # $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2-NOT LATE-
555 # quantityreceived = quantity -NOT LATE-
557 C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago5, undef );
558 isnt( exists( $suppliers{$id_supplier1} ),
559 1, "$today < $daysago10 and $today > $today-3" );
561 exists( $suppliers{$id_supplier2} ),
562 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago5 and $daysago5 < today - 3+2"
565 exists( $suppliers{$id_supplier3} ),
567 "Supplier2 has late orders but $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2 "
569 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
571 #Test with $estimateddeliverydateto and $delay
572 #Case 10:With $estimateddeliverydateto = $daysago5 and $delay = 5
573 # today > $daysago5 today > now() -5 -NOT LATE-
574 # $daysago5 + 2 days > $daysago5 and $daysago5 > now() - 2+5 days -NOT LATE-
575 # $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days -LATE-
576 # quantityreceived = quantity -NOT LATE-
578 C4::Bookseller::GetBooksellersWithLateOrders( 5, undef, $daysago5 );
579 isnt( exists( $suppliers{$id_supplier1} ),
580 1, "Supplier2 has late orders but today > $daysago5 today > now() -5" );
582 exists( $suppliers{$id_supplier2} ),
584 "Supplier2 has late orders but $daysago5 + 2 days > $daysago5 and $daysago5 > now() - 2+5 days"
587 exists( $suppliers{$id_supplier3} ),
588 "Supplier2 has late orders and $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days "
590 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
592 #Case 11: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 10
593 # $daysago10==$daysago10==$daysago10 -NOT LATE-
594 # $daysago10==$daysago10<$daysago5+2-NOT lATE-
595 # $daysago10==$daysago10 <$daysago10+3-LATE-
596 # quantityreceived = quantity -NOT LATE-
598 #Basket1 closedate -> $daysago10
600 basketno => $sample_basket1,
601 closedate => $daysago10,
603 ModBasket($basket1info);
604 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
606 ok( exists( $suppliers{$id_supplier1} ),
607 "Supplier1 has late orders and $daysago10==$daysago10==$daysago10 " )
609 isnt( exists( $suppliers{$id_supplier2} ),
611 "Supplier2 has late orders but $daysago10==$daysago10<$daysago5+2" );
612 isnt( exists( $suppliers{$id_supplier3} ),
614 "Supplier3 has late orders but $daysago10==$daysago10 <$daysago10+3" );
615 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
617 #Case 12: closedate == $estimateddeliverydatefrom =today-10
619 C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10, undef );
620 ok( exists( $suppliers{$id_supplier1} ),
621 "Supplier1 has late orders and $daysago10==$daysago10 " );
623 #Case 13: closedate == $estimateddeliverydateto =today-10
625 C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago10 );
626 ok( exists( $suppliers{$id_supplier1} ),
627 "Supplier1 has late orders and $daysago10==$daysago10 " )
630 C4::Context->_new_userenv('DUMMY SESSION');
631 C4::Context::set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, '', '');
632 my $userenv = C4::Context->userenv;
634 my $module = Test::MockModule->new('C4::Auth');
638 # simulate user that has serials permissions but
640 my ($userid, $flagsrequired) = @_;
641 return 0 if 'superserials' eq ($flagsrequired->{serials} // 0);
642 return exists($flagsrequired->{serials});
646 C4::Context->set_preference('IndependentBranches', 0);
647 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
649 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
651 'ordinary user can see all subscriptions with IndependentBranches off'
654 C4::Context->set_preference('IndependentBranches', 1);
655 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
657 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
659 'ordinary user can see only their library\'s subscriptions with IndependentBranches on'
662 # don the cape and turn into Superlibrarian!
663 C4::Context::set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 1, '', '');
664 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
666 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
668 'superlibrarian can see all subscriptions with IndependentBranches on (bug 12048)'
670 #Test contact editing
671 my $booksellerid = C4::Bookseller::AddBookseller(
674 address1 => "bookseller's address",
679 { name => 'John Smith', phone => '0123456x1' },
680 { name => 'Leo Tolstoy', phone => '0123456x2' },
684 @booksellers = C4::Bookseller::GetBookSeller('my vendor');
686 ( grep { $_->{'id'} == $booksellerid } @booksellers ),
687 'GetBookSeller returns correct record when passed a name'
690 my $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
691 is( $bookseller->{'id'}, $booksellerid, 'Retrieved desired record' );
692 is( $bookseller->{'phone'}, '0123456', 'New bookseller has expected phone' );
693 is( ref $bookseller->{'contacts'},
694 'ARRAY', 'GetBookSellerFromId returns arrayref of contacts' );
696 ref $bookseller->{'contacts'}->[0],
697 'C4::Bookseller::Contact',
698 'First contact is a contact object'
700 is( $bookseller->{'contacts'}->[0]->phone,
701 '0123456x1', 'Contact has expected phone number' );
702 is( scalar @{ $bookseller->{'contacts'} }, 2, 'Saved two contacts' );
704 pop @{ $bookseller->{'contacts'} };
705 $bookseller->{'name'} = 'your vendor';
706 $bookseller->{'contacts'}->[0]->phone('654321');
707 C4::Bookseller::ModBookseller($bookseller);
709 $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
710 is( $bookseller->{'name'}, 'your vendor',
711 'Successfully changed name of vendor' );
712 is( $bookseller->{'contacts'}->[0]->phone,
714 'Successfully changed contact phone number by modifying bookseller hash' );
715 is( scalar @{ $bookseller->{'contacts'} },
716 1, 'Only one contact after modification' );
718 C4::Bookseller::ModBookseller( $bookseller,
719 [ { name => 'John Jacob Jingleheimer Schmidt' } ] );
721 $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
723 $bookseller->{'contacts'}->[0]->name,
724 'John Jacob Jingleheimer Schmidt',
725 'Changed name of contact'
727 is( $bookseller->{'contacts'}->[0]->phone,
728 undef, 'Removed phone number from contact' );
729 is( scalar @{ $bookseller->{'contacts'} },
730 1, 'Only one contact after modification' );
735 #field_filter filters the useless fields or foreign keys
736 #NOTE: all the fields of aqbookseller arent considered
737 #returns a cleaned structure
742 'bookselleremail', 'booksellerfax',
743 'booksellerurl', 'othersupplier',
744 'currency', 'invoiceprice',
745 'listprice', 'contacts'
749 if ( grep { /^$field$/ } keys %$struct ) {
750 delete $struct->{$field};