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