Bug 8798: moving code to Koha::Database and adding tests
[koha.git] / t / db_dependent / Bookseller.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 64;
6 use C4::Context;
7 use Koha::DateUtils;
8 use DateTime::Duration;
9 use C4::Acquisition;
10 use C4::Serials;
11 use C4::Budgets;
12 use C4::Biblio;
13
14 BEGIN {
15     use_ok('C4::Bookseller');
16 }
17
18 can_ok(
19
20     'C4::Bookseller', qw(
21       AddBookseller
22       DelBookseller
23       GetBookSeller
24       GetBookSellerFromId
25       GetBooksellersWithLateOrders
26       ModBookseller )
27 );
28
29 #Start transaction
30 my $dbh = C4::Context->dbh;
31 $dbh->{RaiseError} = 1;
32 $dbh->{AutoCommit} = 0;
33
34 #Start tests
35 $dbh->do(q|DELETE FROM aqorders|);
36 $dbh->do(q|DELETE FROM aqbasket|);
37 $dbh->do(q|DELETE FROM aqbooksellers|);
38 #Test AddBookseller
39 my $count            = scalar( C4::Bookseller::GetBookSeller('') );
40 my $sample_supplier1 = {
41     name          => 'Name1',
42     address1      => 'address1_1',
43     address2      => 'address1-2',
44     address3      => 'address1_2',
45     address4      => 'address1_2',
46     postal        => 'postal1',
47     phone         => 'phone1',
48     accountnumber => 'accountnumber1',
49     fax           => 'fax1',
50     url           => 'url1',
51     contact       => 'contact1',
52     contpos       => 'contpos1',
53     contphone     => 'contphone1',
54     contfax       => 'contefax1',
55     contaltphone  => 'contaltphone1',
56     contemail     => 'contemail1',
57     contnotes     => 'contnotes1',
58     active        => 1,
59     gstreg        => 1,
60     listincgst    => 1,
61     invoiceincgst => 1,
62     gstrate       => '1.0000',
63     discount      => '1.0000',
64     notes         => 'notes1',
65     deliverytime  => undef
66 };
67 my $sample_supplier2 = {
68     name          => 'Name2',
69     address1      => 'address1_2',
70     address2      => 'address2-2',
71     address3      => 'address3_2',
72     address4      => 'address4_2',
73     postal        => 'postal2',
74     phone         => 'phone2',
75     accountnumber => 'accountnumber2',
76     fax           => 'fax2',
77     url           => 'url2',
78     contact       => 'contact2',
79     contpos       => 'contpos2',
80     contphone     => 'contphone2',
81     contfax       => 'contefax2',
82     contaltphone  => 'contaltphone2',
83     contemail     => 'contemail2',
84     contnotes     => 'contnotes2',
85     active        => 1,
86     gstreg        => 1,
87     listincgst    => 1,
88     invoiceincgst => 1,
89     gstrate       => '2.0000',
90     discount      => '2.0000',
91     notes         => 'notes2',
92     deliverytime  => 2,
93 };
94
95 my $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
96 my $id_supplier2 = C4::Bookseller::AddBookseller($sample_supplier2);
97
98 #my $id_bookseller3 = C4::Bookseller::AddBookseller();# NOTE : Doesn't work because the field name cannot be null
99
100 like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
101 like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
102 is( scalar( C4::Bookseller::GetBookSeller('') ),
103     $count + 2, "Supplier1 and Supplier2 have been added" );
104
105 #Test DelBookseller
106 my $del = C4::Bookseller::DelBookseller($id_supplier1);
107 is( $del, 1, "DelBookseller returns 1 - 1 supplier has been deleted " );
108 is( C4::Bookseller::GetBookSellerFromId($id_supplier1),
109     undef, "Supplier1  has been deleted - id_supplier1 doesnt exist anymore" );
110
111 #Test GetBookSeller
112 my @bookseller2 = C4::Bookseller::GetBookSeller( $sample_supplier2->{name} );
113 is( scalar(@bookseller2), 1, "Get only  Supplier2" );
114 $bookseller2[0] = field_filter( $bookseller2[0] );
115 delete $bookseller2[0]->{basketcount};
116
117 $sample_supplier2->{id} = $id_supplier2;
118 is_deeply( $bookseller2[0], $sample_supplier2,
119     "GetBookSeller returns the right informations about $sample_supplier2" );
120
121 $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
122 my @booksellers = C4::Bookseller::GetBookSeller('')
123   ;    #NOTE :without params, it returns all the booksellers
124 for my $i ( 0 .. scalar(@booksellers) - 1 ) {
125     $booksellers[$i] = field_filter( $booksellers[$i] );
126     delete $booksellers[$i]->{basketcount};
127 }
128
129 $sample_supplier1->{id} = $id_supplier1;
130 is( scalar(@booksellers), $count + 2, "Get  Supplier1 and Supplier2" );
131 my @tab = ( $sample_supplier1, $sample_supplier2 );
132 is_deeply( \@booksellers, \@tab,
133     "Returns right fields of Supplier1 and Supplier2" );
134
135 #Test basketcount
136 my @bookseller1 = C4::Bookseller::GetBookSeller( $sample_supplier1->{name} );
137 #FIXME : if there is 0 basket, GetBookSeller returns 1 as basketcount
138 #is( $bookseller1[0]->{basketcount}, 0, 'Supplier1 has 0 basket' );
139 my $sample_basket1 =
140   C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby1', 'basketname1' );
141 my $sample_basket2 =
142   C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby2', 'basketname2' );
143 @bookseller1 = C4::Bookseller::GetBookSeller( $sample_supplier1->{name} );
144 is( $bookseller1[0]->{basketcount}, 2, 'Supplier1 has 2 baskets' );
145
146 #Test GetBookSellerFromId
147 my $bookseller1fromid = C4::Bookseller::GetBookSellerFromId();
148 is( $bookseller1fromid, undef,
149     "GetBookSellerFromId returns undef if no id given" );
150 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
151 $bookseller1fromid = field_filter($bookseller1fromid);
152 delete $bookseller1fromid->{basketcount};
153 delete $bookseller1fromid->{subscriptioncount};
154 is_deeply( $bookseller1fromid, $sample_supplier1,
155     "Get Supplier1 (GetBookSellerFromId)" );
156
157 #Test basketcount
158 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
159 is( $bookseller1fromid->{basketcount}, 2, 'Supplier1 has 2 baskets' );
160
161 #Test subscriptioncount
162 my $dt_today    = dt_from_string;
163 my $today       = output_pref( $dt_today, 'iso', '24hr', 1 );
164
165 my $dt_today1 = dt_from_string;
166 my $dur5 = DateTime::Duration->new( days => -5 );
167 $dt_today1->add_duration($dur5);
168 my $daysago5 = output_pref( $dt_today1, 'iso', '24hr', 1 );
169
170 my $budgetperiod = C4::Budgets::AddBudgetPeriod({
171     budget_period_startdate => $daysago5,
172     budget_period_enddate   => $today,
173     budget_description      => "budget desc"
174 });
175 my $id_budget = AddBudget({
176     budget_code        => "CODE",
177     budget_amount      => "123.132",
178     budget_name        => "Budgetname",
179     budget_notes       => "This is a note",
180     budget_description => "BudgetDescription",
181     budget_active      => 1,
182     budget_period_id   => $budgetperiod
183 });
184 my $bib = MARC::Record->new();
185 $bib->append_fields(
186     MARC::Field->new('245', ' ', ' ', a => 'Journal of ethnology'),
187     MARC::Field->new('500', ' ', ' ', a => 'bib notes'),
188 );
189 my ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
190 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
191 is( $bookseller1fromid->{subscriptioncount},
192     0, 'Supplier1 has 0 subscription' );
193 my $id_subscription1 = C4::Serials::NewSubscription(
194     undef,      "",            $id_supplier1, undef,
195     $id_budget, $biblionumber, '01-01-2013',  undef,
196     undef,      undef,         undef,         undef,
197     undef,      undef,         undef,         undef,
198     undef,      undef,         undef,         undef,
199     undef,      undef,         undef,         undef,
200     undef,      undef,         undef,         undef,
201     undef,      undef,         undef,         1,
202     "subscription notes",    undef,         undef,         undef,
203     undef,      undef,         undef,         0,
204     "intnotes", 0,             undef,         undef,
205     0,          undef,         '31-12-2013',
206 );
207
208 my @subscriptions = SearchSubscriptions({biblionumber => $biblionumber});
209 is($subscriptions[0]->{publicnotes}, 'subscription notes', 'subscription search results include public notes (bug 10689)');
210
211 my $id_subscription2 = C4::Serials::NewSubscription(
212     undef,      "",            $id_supplier1, undef,
213     $id_budget, $biblionumber, '01-01-2013',  undef,
214     undef,      undef,         undef,         undef,
215     undef,      undef,         undef,         undef,
216     undef,      undef,         undef,         undef,
217     undef,      undef,         undef,         undef,
218     undef,      undef,         undef,         undef,
219     undef,      undef,         undef,         1,
220     "subscription notes",    undef,         undef,         undef,
221     undef,      undef,         undef,         0,
222     "intnotes", 0,             undef,         undef,
223     0,          undef,         '31-12-2013',
224 );
225 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
226 is( $bookseller1fromid->{subscriptioncount},
227     2, 'Supplier1 has 2 subscriptions' );
228
229 #Test ModBookseller
230 $sample_supplier2 = {
231     id            => $id_supplier2,
232     name          => 'Name2 modified',
233     address1      => 'address1_2 modified',
234     address2      => 'address2-2 modified',
235     address3      => 'address3_2 modified',
236     address4      => 'address4_2 modified',
237     postal        => 'postal2 modified',
238     phone         => 'phone2 modified',
239     accountnumber => 'accountnumber2 modified',
240     fax           => 'fax2 modified',
241     url           => 'url2 modified',
242     contact       => 'contact2 modified',
243     contpos       => 'contpos2 modified',
244     contphone     => 'contphone2 modified',
245     contfax       => 'contefax2 modified',
246     contaltphone  => 'contaltphone2 modified',
247     contemail     => 'contemail2 modified',
248     contnotes     => 'contnotes2 modified',
249     active        => 1,
250     gstreg        => 1,
251     listincgst    => 1,
252     invoiceincgst => 1,
253     gstrate       => '2.0000 ',
254     discount      => '2.0000',
255     notes         => 'notes2 modified',
256     deliverytime  => 2,
257 };
258
259 my $modif1 = C4::Bookseller::ModBookseller();
260 is( $modif1, undef,
261     "ModBookseller returns undef if no params given - Nothing happened" );
262 $modif1 = C4::Bookseller::ModBookseller($sample_supplier2);
263 is( $modif1, 1, "ModBookseller modifies only the supplier2" );
264 is( scalar( C4::Bookseller::GetBookSeller('') ),
265     $count + 2, "Supplier2 has been modified - Nothing added" );
266
267 $modif1 = C4::Bookseller::ModBookseller(
268     {
269         id   => -1,
270         name => 'name3'
271     }
272 );
273 #is( $modif1, '0E0',
274 #    "ModBookseller returns OEO if the id doesnt exist - Nothing modified" );
275
276 #Test GetBooksellersWithLateOrders
277 #Add 2 suppliers
278 my $sample_supplier3 = {
279     name          => 'Name3',
280     address1      => 'address1_3',
281     address2      => 'address1-3',
282     address3      => 'address1_3',
283     address4      => 'address1_3',
284     postal        => 'postal3',
285     phone         => 'phone3',
286     accountnumber => 'accountnumber3',
287     fax           => 'fax3',
288     url           => 'url3',
289     contact       => 'contact3',
290     contpos       => 'contpos3',
291     contphone     => 'contphone3',
292     contfax       => 'contefax3',
293     contaltphone  => 'contaltphone3',
294     contemail     => 'contemail3',
295     contnotes     => 'contnotes3',
296     active        => 1,
297     gstreg        => 1,
298     listincgst    => 1,
299     invoiceincgst => 1,
300     gstrate       => '3.0000',
301     discount      => '3.0000',
302     notes         => 'notes3',
303     deliverytime  => 3
304 };
305 my $sample_supplier4 = {
306     name          => 'Name4',
307     address1      => 'address1_4',
308     address2      => 'address1-4',
309     address3      => 'address1_4',
310     address4      => 'address1_4',
311     postal        => 'postal4',
312     phone         => 'phone4',
313     accountnumber => 'accountnumber4',
314     fax           => 'fax4',
315     url           => 'url4',
316     contact       => 'contact4',
317     contpos       => 'contpos4',
318     contphone     => 'contphone4',
319     contfax       => 'contefax4',
320     contaltphone  => 'contaltphone4',
321     contemail     => 'contemail4',
322     contnotes     => 'contnotes4',
323     active        => 1,
324     gstreg        => 1,
325     listincgst    => 1,
326     invoiceincgst => 1,
327     gstrate       => '3.0000',
328     discount      => '3.0000',
329     notes         => 'notes3',
330 };
331 my $id_supplier3 = C4::Bookseller::AddBookseller($sample_supplier3);
332 my $id_supplier4 = C4::Bookseller::AddBookseller($sample_supplier4);
333
334 #Add 2 baskets
335 my $sample_basket3 =
336   C4::Acquisition::NewBasket( $id_supplier3, 'authorisedby3', 'basketname3',
337     'basketnote3' );
338 my $sample_basket4 =
339   C4::Acquisition::NewBasket( $id_supplier4, 'authorisedby4', 'basketname4',
340     'basketnote4' );
341
342 #Modify the basket to add a close date
343 my $basket1info = {
344     basketno     => $sample_basket1,
345     closedate    => $today,
346     booksellerid => $id_supplier1
347 };
348
349 my $basket2info = {
350     basketno     => $sample_basket2,
351     closedate    => $daysago5,
352     booksellerid => $id_supplier2
353 };
354
355 my $dt_today2 = dt_from_string;
356 my $dur10 = DateTime::Duration->new( days => -10 );
357 $dt_today2->add_duration($dur10);
358 my $daysago10 = output_pref( $dt_today2, 'iso', '24hr', 1 );
359 my $basket3info = {
360     basketno  => $sample_basket3,
361     closedate => $daysago10,
362 };
363
364 my $basket4info = {
365     basketno  => $sample_basket4,
366     closedate => $today,
367 };
368 ModBasket($basket1info);
369 ModBasket($basket2info);
370 ModBasket($basket3info);
371 ModBasket($basket4info);
372
373 #Add 1 subscription
374 my $id_subscription3 = C4::Serials::NewSubscription(
375     undef,      "",            $id_supplier3, undef,
376     $id_budget, $biblionumber, '01-01-2013',  undef,
377     undef,      undef,         undef,         undef,
378     undef,      undef,         undef,         undef,
379     undef,      undef,         undef,         undef,
380     undef,      undef,         undef,         undef,
381     undef,      undef,         undef,         undef,
382     undef,      undef,         undef,         1,
383     "notes",    undef,         undef,         undef,
384     undef,      undef,         undef,         0,
385     "intnotes", 0,             undef,         undef,
386     0,          undef,         '31-12-2013',
387 );
388
389 #Add 4 orders
390 my ( $ordernumber1, $ordernumber2, $ordernumber3, $ordernumber4 );
391 my ( $basketno1,    $basketno2,    $basketno3,    $basketno4 );
392 ( $basketno1, $ordernumber1 ) = C4::Acquisition::NewOrder(
393     {
394         basketno         => $sample_basket1,
395         quantity         => 24,
396         biblionumber     => $biblionumber,
397         budget_id        => $id_budget,
398         entrydate        => '01-01-2013',
399         currency         => 'EUR',
400         notes            => "This is a note1",
401         gstrate          => 0.0500,
402         orderstatus      => 1,
403         subscriptionid   => $id_subscription1,
404         quantityreceived => 2,
405         rrp              => 10,
406         ecost            => 10,
407         datereceived     => '01-06-2013'
408     }
409 );
410 ( $basketno2, $ordernumber2 ) = C4::Acquisition::NewOrder(
411     {
412         basketno       => $sample_basket2,
413         quantity       => 20,
414         biblionumber   => $biblionumber,
415         budget_id      => $id_budget,
416         entrydate      => '01-01-2013',
417         currency       => 'EUR',
418         notes          => "This is a note2",
419         gstrate        => 0.0500,
420         orderstatus    => 1,
421         subscriptionid => $id_subscription2,
422         rrp            => 10,
423         ecost          => 10,
424     }
425 );
426 ( $basketno3, $ordernumber3 ) = C4::Acquisition::NewOrder(
427     {
428         basketno       => $sample_basket3,
429         quantity       => 20,
430         biblionumber   => $biblionumber,
431         budget_id      => $id_budget,
432         entrydate      => '02-02-2013',
433         currency       => 'EUR',
434         notes          => "This is a note3",
435         gstrate        => 0.0500,
436         orderstatus    => 2,
437         subscriptionid => $id_subscription3,
438         rrp            => 11,
439         ecost          => 11,
440     }
441 );
442 ( $basketno4, $ordernumber4 ) = C4::Acquisition::NewOrder(
443     {
444         basketno         => $sample_basket4,
445         quantity         => 20,
446         biblionumber     => $biblionumber,
447         budget_id        => $id_budget,
448         entrydate        => '02-02-2013',
449         currency         => 'EUR',
450         notes            => "This is a note3",
451         gstrate          => 0.0500,
452         orderstatus      => 2,
453         subscriptionid   => $id_subscription3,
454         rrp              => 11,
455         ecost            => 11,
456         quantityreceived => 20
457     }
458 );
459
460 #Test cases:
461 # Sample datas :
462 #   Supplier1: delivery -> undef Basket1 : closedate -> today
463 #   Supplier2: delivery -> 2     Basket2 : closedate -> $daysago5
464 #   Supplier3: delivery -> 3     Basket3 : closedate -> $daysago10
465 #Case 1 : Without parameters:
466 #   quantityreceived < quantity AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
467 #   datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
468 #   datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
469 #   quantityreceived = quantity -NOT LATE-
470 my %suppliers = C4::Bookseller::GetBooksellersWithLateOrders();
471 ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders" );
472 ok( exists( $suppliers{$id_supplier2} ), "Supplier2 has late orders" );
473 ok( exists( $suppliers{$id_supplier3} ), "Supplier3 has late orders" );
474 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" )
475   ;    # Quantity = quantityreceived
476
477 #Case 2: With $delay = 4
478 #    today + 0 > now-$delay -NOT LATE-
479 #    (today-5) + 2   <= now() - $delay -NOT LATE-
480 #    (today-10) + 3  <= now() - $delay -LATE-
481 #    quantityreceived = quantity -NOT LATE-
482 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 4, undef, undef );
483 isnt( exists( $suppliers{$id_supplier1} ),
484     1, "Supplier1 has late orders but  today  > now() - 4 days" );
485 isnt( exists( $suppliers{$id_supplier2} ),
486     1, "Supplier2 has late orders and $daysago5 <= now() - (4 days+2)" );
487 ok( exists( $suppliers{$id_supplier3} ),
488     "Supplier3 has late orders and $daysago10  <= now() - (4 days+3)" );
489 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
490
491 #Case 3: With $delay = -1
492 is( C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ),
493     undef, "-1 is a wrong value for a delay" );
494
495 #Case 4: With $delay = 0
496 #    today  == now-0 -LATE- (if no deliverytime or deliverytime == 0)
497 #    today-5   <= now() - $delay+2 -LATE-
498 #    today-10  <= now() - $delay+3 -LATE-
499 #    quantityreceived = quantity -NOT LATE-
500 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 0, undef, undef );
501
502 ok( exists( $suppliers{$id_supplier1} ),
503     "Supplier1 has late orders but $today == now() - 0 days" )
504   ;
505 ok( exists( $suppliers{$id_supplier2} ),
506     "Supplier2 has late orders and $daysago5 <= now() - 2" );
507 ok( exists( $suppliers{$id_supplier3} ),
508     "Supplier3 has late orders and $daysago10 <= now() - 3" );
509 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
510
511 #Case 5 : With $estimateddeliverydatefrom = today-4
512 #    today >= today-4 -NOT LATE-
513 #    (today-5)+ 2 days >= today-4  -LATE-
514 #    (today-10) + 3 days < today-4   -NOT LATE-
515 #    quantityreceived = quantity -NOT LATE-
516 my $dt_today3 = dt_from_string;
517 my $dur4 = DateTime::Duration->new( days => -4 );
518 $dt_today3->add_duration($dur4);
519 my $daysago4 = output_pref( $dt_today3, 'iso', '24hr', 1 );
520 %suppliers =
521   C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago4, undef );
522
523 ok( exists( $suppliers{$id_supplier1} ),
524     "Supplier1 has late orders and $today >= $daysago4 -deliverytime undef" );
525 ok( exists( $suppliers{$id_supplier2} ),
526     "Supplier2 has late orders and $daysago5 + 2 days >= $daysago4 " );
527 isnt( exists( $suppliers{$id_supplier3} ),
528     1, "Supplier3 has late orders and $daysago10 + 5 days < $daysago4 " );
529 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
530
531 #Case 6: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 5
532 #    $daysago10<$daysago5<today -NOT LATE-
533 #    $daysago10<$daysago5<$daysago5 +2 -NOT lATE-
534 #    $daysago10<$daysago10 +3 <$daysago5 -LATE-
535 #    quantityreceived = quantity -NOT LATE-
536 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
537     $daysago5 );
538 isnt( exists( $suppliers{$id_supplier1} ),
539     1, "Supplier1 has late orders but $daysago10 < $daysago5 < $today" );
540 isnt(
541     exists( $suppliers{$id_supplier2} ),
542     1,
543     "Supplier2 has late orders but $daysago10 < $daysago5 < $daysago5+2"
544 );
545 ok(
546     exists( $suppliers{$id_supplier3} ),
547 "Supplier3 has late orders and $daysago10 <= $daysago10 +3 <= $daysago5"
548 );
549 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
550
551 #Case 7: With $estimateddeliverydateto = today-5
552 #    $today >= $daysago5  -NOT LATE-
553 #    $daysago5 + 2 days  > $daysago5 -NOT LATE-
554 #    $daysago10 + 3  <+ $daysago5  -LATE-
555 #    quantityreceived = quantity -NOT LATE-
556 %suppliers =
557   C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago5 );
558 isnt( exists( $suppliers{$id_supplier1} ),
559     1,
560     "Supplier1 has late orders but $today >= $daysago5 - deliverytime undef" );
561 isnt( exists( $suppliers{$id_supplier2} ),
562     1, "Supplier2 has late orders but  $daysago5 + 2 days  > $daysago5 " );
563 ok( exists( $suppliers{$id_supplier3} ),
564     "Supplier3 has late orders and $daysago10 + 3  <= $daysago5" );
565 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
566
567 #Test with $estimateddeliverydatefrom and  $estimateddeliverydateto and $delay
568 #Case 8 :With $estimateddeliverydatefrom = 2013-07-05 and  $estimateddeliverydateto = 2013-07-08 and $delay =5
569 #    $daysago4<today<=$today and $today<now()-3  -NOT LATE-
570 #    $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days -LATE-
571 #    $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days -NOT LATE-
572 #    quantityreceived = quantity -NOT LATE-
573 %suppliers =
574   C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago4, $today );
575 isnt(
576     exists( $suppliers{$id_supplier1} ),
577     1,
578     "Supplier1 has late orders but $daysago4<today<=$today and $today<now()-3"
579 );
580 ok(
581     exists( $suppliers{$id_supplier2} ),
582 "Supplier2 has late orders and $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days"
583 );
584 isnt(
585     exists( $suppliers{$id_supplier3} ),
586 "Supplier3 has late orders but $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days"
587 );
588 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
589
590 #Case 9 :With $estimateddeliverydatefrom = $daysago5  and $delay = 3
591 #   $today < $daysago5 and $today > $today-5 -NOT LATE-
592 #   $daysago5 + 2 days >= $daysago5  and $daysago5 < today - 3+2 -LATE-
593 #   $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2-NOT LATE-
594 #   quantityreceived = quantity -NOT LATE-
595 %suppliers =
596   C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago5, undef );
597 isnt( exists( $suppliers{$id_supplier1} ),
598     1, "$today < $daysago10 and $today > $today-3" );
599 ok(
600     exists( $suppliers{$id_supplier2} ),
601 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago5  and $daysago5 < today - 3+2"
602 );
603 isnt(
604     exists( $suppliers{$id_supplier3} ),
605     1,
606 "Supplier2 has late orders but $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2 "
607 );
608 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
609
610 #Test with $estimateddeliverydateto  and $delay
611 #Case 10:With $estimateddeliverydateto = $daysago5 and $delay = 5
612 #    today > $daysago5 today > now() -5 -NOT LATE-
613 #    $daysago5 + 2 days > $daysago5  and $daysago5 > now() - 2+5 days -NOT LATE-
614 #    $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days -LATE-
615 #    quantityreceived = quantity -NOT LATE-
616 %suppliers =
617   C4::Bookseller::GetBooksellersWithLateOrders( 5, undef, $daysago5 );
618 isnt( exists( $suppliers{$id_supplier1} ),
619     1, "Supplier2 has late orders but today > $daysago5 today > now() -5" );
620 isnt(
621     exists( $suppliers{$id_supplier2} ),
622     1,
623 "Supplier2 has late orders but $daysago5 + 2 days > $daysago5  and $daysago5 > now() - 2+5 days"
624 );
625 ok(
626     exists( $suppliers{$id_supplier3} ),
627 "Supplier2 has late orders and $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days "
628 );
629 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
630
631 #Case 11: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 10
632 #    $daysago10==$daysago10==$daysago10 -NOT LATE-
633 #    $daysago10==$daysago10<$daysago5+2-NOT lATE-
634 #    $daysago10==$daysago10 <$daysago10+3-LATE-
635 #    quantityreceived = quantity -NOT LATE-
636
637 #Basket1 closedate -> $daysago10
638 $basket1info = {
639     basketno  => $sample_basket1,
640     closedate => $daysago10,
641 };
642 ModBasket($basket1info);
643 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
644     $daysago10 );
645 ok( exists( $suppliers{$id_supplier1} ),
646     "Supplier1 has late orders and $daysago10==$daysago10==$daysago10 " )
647   ;
648 isnt( exists( $suppliers{$id_supplier2} ),
649     1,
650     "Supplier2 has late orders but $daysago10==$daysago10<$daysago5+2" );
651 isnt( exists( $suppliers{$id_supplier3} ),
652     1,
653     "Supplier3 has late orders but $daysago10==$daysago10 <$daysago10+3" );
654 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
655
656 #Case 12: closedate == $estimateddeliverydatefrom =today-10
657 %suppliers =
658   C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10, undef );
659 ok( exists( $suppliers{$id_supplier1} ),
660     "Supplier1 has late orders and $daysago10==$daysago10 " );
661
662 #Case 13: closedate == $estimateddeliverydateto =today-10
663 %suppliers =
664   C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago10 );
665 ok( exists( $suppliers{$id_supplier1} ),
666     "Supplier1 has late orders and $daysago10==$daysago10 " )
667   ;
668
669 #End transaction
670 $dbh->rollback;
671
672 #field_filter filters the useless fields or foreign keys
673 #NOTE: all the fields of aqbookseller arent considered
674 #returns a cleaned structure
675 sub field_filter {
676     my ($struct) = @_;
677
678     for my $field (
679         'bookselleremail', 'booksellerfax',
680         'booksellerurl',   'othersupplier',
681         'currency',        'invoiceprice',
682         'listprice'
683       )
684     {
685
686         if ( grep { /^$field$/ } keys %$struct ) {
687             delete $struct->{$field};
688         }
689     }
690     return $struct;
691 }