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