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