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