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