Bug 25417: Add unit test
[koha.git] / t / db_dependent / Bookseller.t
blobc0db4fae6c16b3997ec508f581fdea6a6bc60803
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 my $builder = t::lib::TestBuilder->new;
42 #Start tests
43 $dbh->do(q|DELETE FROM aqorders|);
44 $dbh->do(q|DELETE FROM aqbasket|);
45 $dbh->do(q|DELETE FROM aqbooksellers|);
46 $dbh->do(q|DELETE FROM subscription|);
48 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
49 # Add currency
50 my $curcode = $builder->build({ source => 'Currency' })->{currencycode};
52 #Test AddBookseller
53 my $count = Koha::Acquisition::Booksellers->search()->count();
54 my $sample_supplier1 = {
55 name => 'Name1',
56 address1 => 'address1_1',
57 address2 => 'address1-2',
58 address3 => 'address1_2',
59 address4 => 'address1_2',
60 postal => 'postal1',
61 phone => 'phone1',
62 accountnumber => 'accountnumber1',
63 fax => 'fax1',
64 url => 'url1',
65 active => 1,
66 gstreg => 1,
67 listincgst => 1,
68 invoiceincgst => 1,
69 tax_rate => '1.0000',
70 discount => 1.0000,
71 notes => 'notes1',
72 deliverytime => undef
74 my $sample_supplier2 = {
75 name => 'Name2',
76 address1 => 'address1_2',
77 address2 => 'address2-2',
78 address3 => 'address3_2',
79 address4 => 'address4_2',
80 postal => 'postal2',
81 phone => 'phone2',
82 accountnumber => 'accountnumber2',
83 fax => 'fax2',
84 url => 'url2',
85 active => 1,
86 gstreg => 1,
87 listincgst => 1,
88 invoiceincgst => 1,
89 tax_rate => '2.0000',
90 discount => 2.0000,
91 notes => 'notes2',
92 deliverytime => 2
95 my $supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
96 my $id_supplier1 = $supplier1->id;
97 my $supplier2 = Koha::Acquisition::Bookseller->new($sample_supplier2)->store;
98 my $id_supplier2 = $supplier2->id;
100 like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
101 like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
102 is( Koha::Acquisition::Booksellers->search()->count,
103 $count + 2, "Supplier1 and Supplier2 have been added" );
105 #Test DelBookseller
106 my $del = $supplier1->delete;
107 is( ref($del), 'Koha::Acquisition::Bookseller', "DelBookseller returns the supplier object - supplier has been deleted " );
108 my $b = Koha::Acquisition::Booksellers->find( $id_supplier1 );
109 is( $b,
110 undef, "Supplier1 has been deleted - id_supplier1 $id_supplier1 doesnt exist anymore" );
112 #Test get bookseller
113 my @bookseller2 = Koha::Acquisition::Booksellers->search({name => $sample_supplier2->{name} });
114 is( scalar(@bookseller2), 1, "Get only Supplier2" );
115 for my $bookseller ( @bookseller2 ) {
116 $bookseller = field_filter( $bookseller->unblessed );
119 $sample_supplier2->{id} = $id_supplier2;
120 is_deeply( cast_precision($bookseller2[0]), $sample_supplier2,
121 "Koha::Acquisition::Booksellers->search returns the right informations about supplier $sample_supplier2->{name}" );
123 $supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
124 $id_supplier1 = $supplier1->id;
125 my @booksellers = Koha::Acquisition::Booksellers->search();
126 for my $bookseller ( @booksellers ) {
127 $bookseller = field_filter( $bookseller->unblessed );
128 $bookseller = cast_precision($bookseller);
131 $sample_supplier1->{id} = $id_supplier1;
132 is( scalar(@booksellers), $count + 2, "Get Supplier1 and Supplier2" );
133 my @tab = ( $sample_supplier2, $sample_supplier1 );
134 is_deeply( \@booksellers, \@tab,
135 "Returns right fields of Supplier1 and Supplier2" );
137 #Test baskets
138 my @bookseller1 = Koha::Acquisition::Booksellers->search({name => $sample_supplier1->{name} });
139 is( $bookseller1[0]->baskets->count, 0, 'Supplier1 has 0 basket' );
140 my $basketno1 =
141 C4::Acquisition::NewBasket( $id_supplier1, $patron->borrowernumber, 'basketname1' );
142 my $basketno2 =
143 C4::Acquisition::NewBasket( $id_supplier1, $patron->borrowernumber, 'basketname2' );
144 @bookseller1 = Koha::Acquisition::Booksellers->search({ name => $sample_supplier1->{name} });
145 is( $bookseller1[0]->baskets->count, 2, 'Supplier1 has 2 baskets' );
147 #Test Koha::Acquisition::Bookseller->new using id
148 my $bookseller1fromid = Koha::Acquisition::Booksellers->find;
149 is( $bookseller1fromid, undef,
150 "find returns undef if no id given" );
151 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
152 $bookseller1fromid = field_filter($bookseller1fromid->unblessed);
153 is_deeply( cast_precision($bookseller1fromid), $sample_supplier1,
154 "Get Supplier1 (find a bookseller by id)" );
156 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
157 is( $bookseller1fromid->baskets->count, 2, 'Supplier1 has 2 baskets' );
159 #Test subscriptions
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();
181 $bib->append_fields(
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::Booksellers->find( $id_supplier1 );
187 is( $bookseller1fromid->subscriptions->count,
188 0, 'Supplier1 has 0 subscription' );
190 my $id_subscription1 = NewSubscription(
191 undef, 'BRANCH2', $id_supplier1, undef, $id_budget, $biblionumber,
192 '2013-01-01',undef, undef, undef, undef,
193 undef, undef, undef, undef, undef, undef,
194 1, "subscription notes",undef, '2013-01-01', 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 '2013-01-01',undef, undef, undef, undef,
205 undef, undef, undef, undef, undef, undef,
206 1, "subscription notes",undef, '2013-01-01', undef, undef,
207 undef, 'CALL DEF', 0, "intnotes", 0,
208 undef, undef, 0, undef, '2013-07-31', 0
211 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
212 is( $bookseller1fromid->subscriptions->count,
213 2, 'Supplier1 has 2 subscriptions' );
215 #Test ModBookseller
216 $sample_supplier2 = {
217 id => $id_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',
228 active => 1,
229 gstreg => 1,
230 listincgst => 1,
231 invoiceincgst => 1,
232 tax_rate => '2.0000',
233 discount => 2.0000,
234 notes => 'notes2 modified',
235 deliverytime => 2,
238 my $modif1 = Koha::Acquisition::Booksellers->find($id_supplier2)->set($sample_supplier2)->store;
239 is( ref $modif1, 'Koha::Acquisition::Bookseller', "ModBookseller has updated the bookseller" );
240 is( Koha::Acquisition::Booksellers->search->count,
241 $count + 2, "Supplier2 has been modified - Nothing added" );
242 $supplier2 = Koha::Acquisition::Booksellers->find($id_supplier2);
243 is( $supplier2->name, 'Name2 modified', "supplier's name should have been modified" );
245 #Test GetBooksellersWithLateOrders
246 #Add 2 suppliers
247 my $sample_supplier3 = {
248 name => 'Name3',
249 address1 => 'address1_3',
250 address2 => 'address1-3',
251 address3 => 'address1_3',
252 address4 => 'address1_3',
253 postal => 'postal3',
254 phone => 'phone3',
255 accountnumber => 'accountnumber3',
256 fax => 'fax3',
257 url => 'url3',
258 active => 1,
259 gstreg => 1,
260 listincgst => 1,
261 invoiceincgst => 1,
262 tax_rate => '3.0000',
263 discount => 3.0000,
264 notes => 'notes3',
265 deliverytime => 3
267 my $sample_supplier4 = {
268 name => 'Name4',
269 address1 => 'address1_4',
270 address2 => 'address1-4',
271 address3 => 'address1_4',
272 address4 => 'address1_4',
273 postal => 'postal4',
274 phone => 'phone4',
275 accountnumber => 'accountnumber4',
276 fax => 'fax4',
277 url => 'url4',
278 active => 1,
279 gstreg => 1,
280 listincgst => 1,
281 invoiceincgst => 1,
282 tax_rate => '3.0000',
283 discount => 3.0000,
284 notes => 'notes3',
286 my $supplier3 = Koha::Acquisition::Bookseller->new($sample_supplier3)->store;
287 my $id_supplier3 = $supplier3->id;
288 my $supplier4 = Koha::Acquisition::Bookseller->new($sample_supplier4)->store;
289 my $id_supplier4 = $supplier4->id;
291 #Add 2 baskets
292 my $basketno3 =
293 C4::Acquisition::NewBasket( $id_supplier3, $patron->borrowernumber, 'basketname3',
294 'basketnote3' );
295 my $basketno4 =
296 C4::Acquisition::NewBasket( $id_supplier4, $patron->borrowernumber, 'basketname4',
297 'basketnote4' );
299 #Modify the basket to add a close date
300 my $basket1info = {
301 basketno => $basketno1,
302 closedate => $today,
303 booksellerid => $id_supplier1
306 my $basket2info = {
307 basketno => $basketno2,
308 closedate => $daysago5,
309 booksellerid => $id_supplier2
312 my $dt_today2 = dt_from_string;
313 my $dur10 = DateTime::Duration->new( days => -10 );
314 $dt_today2->add_duration($dur10);
315 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
316 my $basket3info = {
317 basketno => $basketno3,
318 closedate => $daysago10,
321 my $basket4info = {
322 basketno => $basketno4,
323 closedate => $today,
325 ModBasket($basket1info);
326 ModBasket($basket2info);
327 ModBasket($basket3info);
328 ModBasket($basket4info);
330 #Add 1 subscription
331 my $id_subscription3 = NewSubscription(
332 undef, "BRANCH1", $id_supplier1, undef, $id_budget, $biblionumber,
333 '2013-01-01',undef, undef, undef, undef,
334 undef, undef, undef, undef, undef, undef,
335 1, "subscription notes",undef, '2013-01-01', undef, undef,
336 undef, undef, 0, "intnotes", 0,
337 undef, undef, 0, 'LOCA', '2013-12-31', 0
340 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
341 is(scalar(@subscriptions), 3, 'search for subscriptions by expiration date');
342 @subscriptions = SearchSubscriptions({expiration_date => '2013-08-15'});
343 is(scalar(@subscriptions), 1, 'search for subscriptions by expiration date');
344 @subscriptions = SearchSubscriptions({callnumber => 'CALL'});
345 is(scalar(@subscriptions), 2, 'search for subscriptions by call number');
346 @subscriptions = SearchSubscriptions({callnumber => 'DEF'});
347 is(scalar(@subscriptions), 1, 'search for subscriptions by call number');
348 @subscriptions = SearchSubscriptions({location => 'LOCA'});
349 is(scalar(@subscriptions), 1, 'search for subscriptions by location');
351 #Add 4 orders
352 my $order1 = Koha::Acquisition::Order->new(
354 basketno => $basketno1,
355 quantity => 24,
356 biblionumber => $biblionumber,
357 budget_id => $id_budget,
358 entrydate => '2013-01-01',
359 currency => $curcode,
360 notes => "This is a note1",
361 tax_rate => 0.0500,
362 orderstatus => 1,
363 subscriptionid => $id_subscription1,
364 quantityreceived => 2,
365 rrp => 10,
366 ecost => 10,
367 datereceived => '2013-06-01'
369 )->store;
370 my $ordernumber1 = $order1->ordernumber;
372 my $order2 = Koha::Acquisition::Order->new(
374 basketno => $basketno2,
375 quantity => 20,
376 biblionumber => $biblionumber,
377 budget_id => $id_budget,
378 entrydate => '2013-01-01',
379 currency => $curcode,
380 notes => "This is a note2",
381 tax_rate => 0.0500,
382 orderstatus => 1,
383 subscriptionid => $id_subscription2,
384 rrp => 10,
385 ecost => 10,
387 )->store;
388 my $ordernumber2 = $order2->ordernumber;
390 my $order3 = Koha::Acquisition::Order->new(
392 basketno => $basketno3,
393 quantity => 20,
394 biblionumber => $biblionumber,
395 budget_id => $id_budget,
396 entrydate => '2013-02-02',
397 currency => $curcode,
398 notes => "This is a note3",
399 tax_rate => 0.0500,
400 orderstatus => 2,
401 subscriptionid => $id_subscription3,
402 rrp => 11,
403 ecost => 11,
405 )->store;
406 my $ordernumber3 = $order3->ordernumber;
408 my $order4 = Koha::Acquisition::Order->new(
410 basketno => $basketno4,
411 quantity => 20,
412 biblionumber => $biblionumber,
413 budget_id => $id_budget,
414 entrydate => '2013-02-02',
415 currency => $curcode,
416 notes => "This is a note3",
417 tax_rate => 0.0500,
418 orderstatus => 2,
419 subscriptionid => $id_subscription3,
420 rrp => 11,
421 ecost => 11,
422 quantityreceived => 20
424 )->store;
425 my $ordernumber4 = $order4->ordernumber;
427 #Test cases:
428 # Sample datas :
429 # Supplier1: delivery -> undef Basket1 : closedate -> today
430 # Supplier2: delivery -> 2 Basket2 : closedate -> $daysago5
431 # Supplier3: delivery -> 3 Basket3 : closedate -> $daysago10
432 #Case 1 : Without parameters:
433 # quantityreceived < quantity AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
434 # datereceived !null 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 # quantityreceived = quantity -NOT LATE-
437 my %suppliers = C4::Bookseller::GetBooksellersWithLateOrders();
438 ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders" );
439 ok( exists( $suppliers{$id_supplier2} ), "Supplier2 has late orders" );
440 ok( exists( $suppliers{$id_supplier3} ), "Supplier3 has late orders" );
441 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" )
442 ; # Quantity = quantityreceived
444 #Case 2: With $delay = 4
445 # today + 0 > now-$delay -NOT LATE-
446 # (today-5) + 2 <= now() - $delay -NOT LATE-
447 # (today-10) + 3 <= now() - $delay -LATE-
448 # quantityreceived = quantity -NOT LATE-
449 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 4, undef, undef );
450 isnt( exists( $suppliers{$id_supplier1} ),
451 1, "Supplier1 has late orders but today > now() - 4 days" );
452 isnt( exists( $suppliers{$id_supplier2} ),
453 1, "Supplier2 has late orders and $daysago5 <= now() - (4 days+2)" );
454 ok( exists( $suppliers{$id_supplier3} ),
455 "Supplier3 has late orders and $daysago10 <= now() - (4 days+3)" );
456 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
458 #Case 3: With $delay = -1
459 my $bslo;
460 warning_like
461 { $bslo = C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ) }
462 qr/^WARNING: GetBooksellerWithLateOrders is called with a negative value/,
463 "GetBooksellerWithLateOrders prints a warning on negative values";
465 is( $bslo, undef, "-1 is a wrong value for a delay" );
467 #Case 4: With $delay = 0
468 # today == now-0 -LATE- (if no deliverytime or deliverytime == 0)
469 # today-5 <= now() - $delay+2 -LATE-
470 # today-10 <= now() - $delay+3 -LATE-
471 # quantityreceived = quantity -NOT LATE-
472 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 0, undef, undef );
474 ok( exists( $suppliers{$id_supplier1} ),
475 "Supplier1 has late orders but $today == now() - 0 days" )
477 ok( exists( $suppliers{$id_supplier2} ),
478 "Supplier2 has late orders and $daysago5 <= now() - 2" );
479 ok( exists( $suppliers{$id_supplier3} ),
480 "Supplier3 has late orders and $daysago10 <= now() - 3" );
481 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
483 #Case 5 : With $estimateddeliverydatefrom = today-4
484 # today >= today-4 -NOT LATE-
485 # (today-5)+ 2 days >= today-4 -LATE-
486 # (today-10) + 3 days < today-4 -NOT LATE-
487 # quantityreceived = quantity -NOT LATE-
488 my $dt_today3 = dt_from_string;
489 my $dur4 = DateTime::Duration->new( days => -4 );
490 $dt_today3->add_duration($dur4);
491 my $daysago4 = output_pref({ dt => $dt_today3, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
492 %suppliers =
493 C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago4, undef );
495 ok( exists( $suppliers{$id_supplier1} ),
496 "Supplier1 has late orders and $today >= $daysago4 -deliverytime undef" );
497 ok( exists( $suppliers{$id_supplier2} ),
498 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago4 " );
499 isnt( exists( $suppliers{$id_supplier3} ),
500 1, "Supplier3 has late orders and $daysago10 + 5 days < $daysago4 " );
501 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
503 #Case 6: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 5
504 # $daysago10<$daysago5<today -NOT LATE-
505 # $daysago10<$daysago5<$daysago5 +2 -NOT lATE-
506 # $daysago10<$daysago10 +3 <$daysago5 -LATE-
507 # quantityreceived = quantity -NOT LATE-
508 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
509 $daysago5 );
510 isnt( exists( $suppliers{$id_supplier1} ),
511 1, "Supplier1 has late orders but $daysago10 < $daysago5 < $today" );
512 isnt(
513 exists( $suppliers{$id_supplier2} ),
515 "Supplier2 has late orders but $daysago10 < $daysago5 < $daysago5+2"
518 exists( $suppliers{$id_supplier3} ),
519 "Supplier3 has late orders and $daysago10 <= $daysago10 +3 <= $daysago5"
521 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
523 #Case 7: With $estimateddeliverydateto = today-5
524 # $today >= $daysago5 -NOT LATE-
525 # $daysago5 + 2 days > $daysago5 -NOT LATE-
526 # $daysago10 + 3 <+ $daysago5 -LATE-
527 # quantityreceived = quantity -NOT LATE-
528 %suppliers =
529 C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago5 );
530 isnt( exists( $suppliers{$id_supplier1} ),
532 "Supplier1 has late orders but $today >= $daysago5 - deliverytime undef" );
533 isnt( exists( $suppliers{$id_supplier2} ),
534 1, "Supplier2 has late orders but $daysago5 + 2 days > $daysago5 " );
535 ok( exists( $suppliers{$id_supplier3} ),
536 "Supplier3 has late orders and $daysago10 + 3 <= $daysago5" );
537 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
539 #Test with $estimateddeliverydatefrom and $estimateddeliverydateto and $delay
540 #Case 8 :With $estimateddeliverydatefrom = 2013-07-05 and $estimateddeliverydateto = 2013-07-08 and $delay =5
541 # $daysago4<today<=$today and $today<now()-3 -NOT LATE-
542 # $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days -LATE-
543 # $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days -NOT LATE-
544 # quantityreceived = quantity -NOT LATE-
545 %suppliers =
546 C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago4, $today );
547 isnt(
548 exists( $suppliers{$id_supplier1} ),
550 "Supplier1 has late orders but $daysago4<today<=$today and $today<now()-3"
553 exists( $suppliers{$id_supplier2} ),
554 "Supplier2 has late orders and $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days"
556 isnt(
557 exists( $suppliers{$id_supplier3} ),
558 "Supplier3 has late orders but $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days"
560 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
562 #Case 9 :With $estimateddeliverydatefrom = $daysago5 and $delay = 3
563 # $today < $daysago5 and $today > $today-5 -NOT LATE-
564 # $daysago5 + 2 days >= $daysago5 and $daysago5 < today - 3+2 -LATE-
565 # $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2-NOT LATE-
566 # quantityreceived = quantity -NOT LATE-
567 %suppliers =
568 C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago5, undef );
569 isnt( exists( $suppliers{$id_supplier1} ),
570 1, "$today < $daysago10 and $today > $today-3" );
572 exists( $suppliers{$id_supplier2} ),
573 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago5 and $daysago5 < today - 3+2"
575 isnt(
576 exists( $suppliers{$id_supplier3} ),
578 "Supplier2 has late orders but $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2 "
580 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
582 #Test with $estimateddeliverydateto and $delay
583 #Case 10:With $estimateddeliverydateto = $daysago5 and $delay = 5
584 # today > $daysago5 today > now() -5 -NOT LATE-
585 # $daysago5 + 2 days > $daysago5 and $daysago5 > now() - 2+5 days -NOT LATE-
586 # $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days -LATE-
587 # quantityreceived = quantity -NOT LATE-
588 %suppliers =
589 C4::Bookseller::GetBooksellersWithLateOrders( 5, undef, $daysago5 );
590 isnt( exists( $suppliers{$id_supplier1} ),
591 1, "Supplier2 has late orders but today > $daysago5 today > now() -5" );
592 isnt(
593 exists( $suppliers{$id_supplier2} ),
595 "Supplier2 has late orders but $daysago5 + 2 days > $daysago5 and $daysago5 > now() - 2+5 days"
598 exists( $suppliers{$id_supplier3} ),
599 "Supplier2 has late orders and $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days "
601 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
603 #Case 11: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 10
604 # $daysago10==$daysago10==$daysago10 -NOT LATE-
605 # $daysago10==$daysago10<$daysago5+2-NOT lATE-
606 # $daysago10==$daysago10 <$daysago10+3-LATE-
607 # quantityreceived = quantity -NOT LATE-
609 #Basket1 closedate -> $daysago10
610 $basket1info = {
611 basketno => $basketno1,
612 closedate => $daysago10,
614 ModBasket($basket1info);
615 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
616 $daysago10 );
617 ok( exists( $suppliers{$id_supplier1} ),
618 "Supplier1 has late orders and $daysago10==$daysago10==$daysago10 " )
620 isnt( exists( $suppliers{$id_supplier2} ),
622 "Supplier2 has late orders but $daysago10==$daysago10<$daysago5+2" );
623 isnt( exists( $suppliers{$id_supplier3} ),
625 "Supplier3 has late orders but $daysago10==$daysago10 <$daysago10+3" );
626 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
628 #Case 12: closedate == $estimateddeliverydatefrom =today-10
629 %suppliers =
630 C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10, undef );
631 ok( exists( $suppliers{$id_supplier1} ),
632 "Supplier1 has late orders and $daysago10==$daysago10 " );
634 #Case 13: closedate == $estimateddeliverydateto =today-10
635 %suppliers =
636 C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago10 );
637 ok( exists( $suppliers{$id_supplier1} ),
638 "Supplier1 has late orders and $daysago10==$daysago10 " )
641 t::lib::Mocks::mock_userenv({ flags => 0, branchcode => 'BRANCH1' });
643 my $module = Test::MockModule->new('C4::Auth');
644 $module->mock(
645 'haspermission',
646 sub {
647 # simulate user that has serials permissions but
648 # NOT superserials
649 my ($userid, $flagsrequired) = @_;
650 return 0 if 'superserials' eq ($flagsrequired->{serials} // 0);
651 return exists($flagsrequired->{serials});
655 t::lib::Mocks::mock_preference('IndependentBranches', 0);
656 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
658 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
660 'ordinary user can see all subscriptions with IndependentBranches off'
663 t::lib::Mocks::mock_preference('IndependentBranches', 1);
664 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
666 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
668 'ordinary user can see only their library\'s subscriptions with IndependentBranches on'
671 # don the cape and turn into Superlibrarian!
672 t::lib::Mocks::mock_userenv({ flags => 1, branchcode => 'BRANCH1' });
673 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
675 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
677 'superlibrarian can see all subscriptions with IndependentBranches on (bug 12048)'
680 #Test contact editing
681 my $sample_supplier = {
682 name => "my vendor",
683 address1 => "bookseller's address",
684 phone => "0123456",
685 active => 1
687 my $supplier = Koha::Acquisition::Bookseller->new($sample_supplier)->store;
688 my $booksellerid = $supplier->id;
689 my $contact1 = Koha::Acquisition::Bookseller::Contact->new({
690 name => 'John Smith',
691 phone => '0123456x1',
692 booksellerid => $booksellerid,
693 })->store;
694 my $contact2 = Koha::Acquisition::Bookseller::Contact->new({
695 name => 'Leo Tolstoy',
696 phone => '0123456x2',
697 booksellerid => $booksellerid,
698 })->store;
700 @booksellers = Koha::Acquisition::Booksellers->search({ name => 'my vendor' });
702 ( grep { $_->id == $booksellerid } @booksellers ),
703 'Koha::Acquisition::Booksellers->search returns correct record when passed a name'
706 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
707 is( $bookseller->id, $booksellerid, 'Retrieved desired record' );
708 is( $bookseller->phone, '0123456', 'New bookseller has expected phone' );
709 my $contacts = $bookseller->contacts;
710 is( $contacts->count,
711 2, 'bookseller should have 2 contacts' );
712 my $first_contact = $contacts->next;
714 ref $first_contact,
715 'Koha::Acquisition::Bookseller::Contact',
716 'First contact is a AqContact'
718 is( $first_contact->phone,
719 '0123456x1', 'Contact has expected phone number' );
721 my $second_contact = $contacts->next;
722 $second_contact->delete;
723 $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
724 $bookseller->name('your vendor')->store;
725 $contacts = $bookseller->contacts;
726 $first_contact = $contacts->next;
727 $first_contact->phone('654321');
728 $first_contact->store;
730 $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
731 is( $bookseller->name, 'your vendor',
732 'Successfully changed name of vendor' );
733 $contacts = $bookseller->contacts;
734 is( $contacts->count,
735 1, 'Only one contact after modification' );
736 $first_contact = $contacts->next;
737 is( $first_contact->phone,
738 '654321',
739 'Successfully changed contact phone number by modifying bookseller hash' );
741 $first_contact->name( 'John Jacob Jingleheimer Schmidt' );
742 $first_contact->phone(undef);
743 $first_contact->store;
745 $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
746 $contacts = $bookseller->contacts;
747 $first_contact = $contacts->next;
749 $first_contact->name,
750 'John Jacob Jingleheimer Schmidt',
751 'Changed name of contact'
753 is( $first_contact->phone,
754 undef, 'Removed phone number from contact' );
755 is( $contacts->count,
756 1, 'Only one contact after modification' );
758 #End transaction
759 $schema->storage->txn_rollback();
761 #field_filter filters the useless fields or foreign keys
762 #NOTE: all the fields of aqbookseller arent considered
763 #returns a cleaned structure
764 sub field_filter {
765 my ($struct) = @_;
767 for my $field ( qw(invoiceprice listprice contacts) )
770 if ( grep { /^$field$/ } keys %$struct ) {
771 delete $struct->{$field};
774 return $struct;
777 # ensure numbers are actually tested as numbers to prevent
778 # precision changes causing test failures (D8->D9 Upgrades)
779 sub cast_precision {
780 my ($struct) = @_;
781 my @cast = ('discount');
782 for my $cast (@cast) {
783 $struct->{$cast} = $struct->{$cast}+0;
785 return $struct;