Bug 10694: (follow-up) remove truncate table from test case
[koha_fer] / t / db_dependent / Bookseller.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 72;
6 use Test::MockModule;
7 use C4::Context;
8 use Koha::DateUtils;
9 use DateTime::Duration;
10 use C4::Acquisition;
11 use C4::Serials;
12 use C4::Budgets;
13 use C4::Biblio;
14
15 BEGIN {
16     use_ok('C4::Bookseller');
17 }
18
19 can_ok(
20
21     'C4::Bookseller', qw(
22       AddBookseller
23       DelBookseller
24       GetBookSeller
25       GetBookSellerFromId
26       GetBooksellersWithLateOrders
27       ModBookseller )
28 );
29
30 #Start transaction
31 my $dbh = C4::Context->dbh;
32 $dbh->{RaiseError} = 1;
33 $dbh->{AutoCommit} = 0;
34
35 #Start tests
36 $dbh->do(q|DELETE FROM aqorders|);
37 $dbh->do(q|DELETE FROM aqbasket|);
38 $dbh->do(q|DELETE FROM aqbooksellers|);
39 $dbh->do(q|DELETE FROM subscription|);
40
41 #Test AddBookseller
42 my $count            = scalar( C4::Bookseller::GetBookSeller('') );
43 my $sample_supplier1 = {
44     name          => 'Name1',
45     address1      => 'address1_1',
46     address2      => 'address1-2',
47     address3      => 'address1_2',
48     address4      => 'address1_2',
49     postal        => 'postal1',
50     phone         => 'phone1',
51     accountnumber => 'accountnumber1',
52     fax           => 'fax1',
53     url           => 'url1',
54     contact       => 'contact1',
55     contpos       => 'contpos1',
56     contphone     => 'contphone1',
57     contfax       => 'contefax1',
58     contaltphone  => 'contaltphone1',
59     contemail     => 'contemail1',
60     contnotes     => 'contnotes1',
61     active        => 1,
62     gstreg        => 1,
63     listincgst    => 1,
64     invoiceincgst => 1,
65     gstrate       => '1.0000',
66     discount      => '1.0000',
67     notes         => 'notes1',
68     deliverytime  => undef
69 };
70 my $sample_supplier2 = {
71     name          => 'Name2',
72     address1      => 'address1_2',
73     address2      => 'address2-2',
74     address3      => 'address3_2',
75     address4      => 'address4_2',
76     postal        => 'postal2',
77     phone         => 'phone2',
78     accountnumber => 'accountnumber2',
79     fax           => 'fax2',
80     url           => 'url2',
81     contact       => 'contact2',
82     contpos       => 'contpos2',
83     contphone     => 'contphone2',
84     contfax       => 'contefax2',
85     contaltphone  => 'contaltphone2',
86     contemail     => 'contemail2',
87     contnotes     => 'contnotes2',
88     active        => 1,
89     gstreg        => 1,
90     listincgst    => 1,
91     invoiceincgst => 1,
92     gstrate       => '2.0000',
93     discount      => '2.0000',
94     notes         => 'notes2',
95     deliverytime  => 2,
96 };
97
98 my $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
99 my $id_supplier2 = C4::Bookseller::AddBookseller($sample_supplier2);
100
101 #my $id_bookseller3 = C4::Bookseller::AddBookseller();# NOTE : Doesn't work because the field name cannot be null
102
103 like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
104 like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
105 is( scalar( C4::Bookseller::GetBookSeller('') ),
106     $count + 2, "Supplier1 and Supplier2 have been added" );
107
108 #Test DelBookseller
109 my $del = C4::Bookseller::DelBookseller($id_supplier1);
110 is( $del, 1, "DelBookseller returns 1 - 1 supplier has been deleted " );
111 is( C4::Bookseller::GetBookSellerFromId($id_supplier1),
112     undef, "Supplier1  has been deleted - id_supplier1 doesnt exist anymore" );
113
114 #Test GetBookSeller
115 my @bookseller2 = C4::Bookseller::GetBookSeller( $sample_supplier2->{name} );
116 is( scalar(@bookseller2), 1, "Get only  Supplier2" );
117 $bookseller2[0] = field_filter( $bookseller2[0] );
118 delete $bookseller2[0]->{basketcount};
119
120 $sample_supplier2->{id} = $id_supplier2;
121 is_deeply( $bookseller2[0], $sample_supplier2,
122     "GetBookSeller returns the right informations about $sample_supplier2" );
123
124 $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
125 my @booksellers = C4::Bookseller::GetBookSeller('')
126   ;    #NOTE :without params, it returns all the booksellers
127 for my $i ( 0 .. scalar(@booksellers) - 1 ) {
128     $booksellers[$i] = field_filter( $booksellers[$i] );
129     delete $booksellers[$i]->{basketcount};
130 }
131
132 $sample_supplier1->{id} = $id_supplier1;
133 is( scalar(@booksellers), $count + 2, "Get  Supplier1 and Supplier2" );
134 my @tab = ( $sample_supplier1, $sample_supplier2 );
135 is_deeply( \@booksellers, \@tab,
136     "Returns right fields of Supplier1 and Supplier2" );
137
138 #Test basketcount
139 my @bookseller1 = C4::Bookseller::GetBookSeller( $sample_supplier1->{name} );
140 #FIXME : if there is 0 basket, GetBookSeller returns 1 as basketcount
141 #is( $bookseller1[0]->{basketcount}, 0, 'Supplier1 has 0 basket' );
142 my $sample_basket1 =
143   C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby1', 'basketname1' );
144 my $sample_basket2 =
145   C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby2', 'basketname2' );
146 @bookseller1 = C4::Bookseller::GetBookSeller( $sample_supplier1->{name} );
147 is( $bookseller1[0]->{basketcount}, 2, 'Supplier1 has 2 baskets' );
148
149 #Test GetBookSellerFromId
150 my $bookseller1fromid = C4::Bookseller::GetBookSellerFromId();
151 is( $bookseller1fromid, undef,
152     "GetBookSellerFromId returns undef if no id given" );
153 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
154 $bookseller1fromid = field_filter($bookseller1fromid);
155 delete $bookseller1fromid->{basketcount};
156 delete $bookseller1fromid->{subscriptioncount};
157 is_deeply( $bookseller1fromid, $sample_supplier1,
158     "Get Supplier1 (GetBookSellerFromId)" );
159
160 #Test basketcount
161 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
162 is( $bookseller1fromid->{basketcount}, 2, 'Supplier1 has 2 baskets' );
163
164 #Test subscriptioncount
165 my $dt_today    = dt_from_string;
166 my $today       = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
167
168 my $dt_today1 = dt_from_string;
169 my $dur5 = DateTime::Duration->new( days => -5 );
170 $dt_today1->add_duration($dur5);
171 my $daysago5 = output_pref({ dt => $dt_today1, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
172
173 my $budgetperiod = C4::Budgets::AddBudgetPeriod({
174     budget_period_startdate => $daysago5,
175     budget_period_enddate   => $today,
176     budget_description      => "budget desc"
177 });
178 my $id_budget = AddBudget({
179     budget_code        => "CODE",
180     budget_amount      => "123.132",
181     budget_name        => "Budgetname",
182     budget_notes       => "This is a note",
183     budget_description => "BudgetDescription",
184     budget_active      => 1,
185     budget_period_id   => $budgetperiod
186 });
187 my $bib = MARC::Record->new();
188 $bib->append_fields(
189     MARC::Field->new('245', ' ', ' ', a => 'Journal of ethnology'),
190     MARC::Field->new('500', ' ', ' ', a => 'bib notes'),
191 );
192 my ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
193 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
194 is( $bookseller1fromid->{subscriptioncount},
195     0, 'Supplier1 has 0 subscription' );
196
197 my $id_subscription1 = NewSubscription(
198     undef,      'BRANCH2',     $id_supplier1, undef, $id_budget, $biblionumber,
199     '01-01-2013',undef, undef, undef,  undef,
200     undef,      undef,  undef, undef, undef, undef,
201     1,          "subscription notes",undef, '01-01-2013', undef, undef,
202     undef, 'CALL ABC',  0,    "intnotes",  0,
203     undef, undef, 0,          undef,         '2013-11-30', 0
204 );
205
206 my @subscriptions = SearchSubscriptions({biblionumber => $biblionumber});
207 is($subscriptions[0]->{publicnotes}, 'subscription notes', 'subscription search results include public notes (bug 10689)');
208
209 my $id_subscription2 = NewSubscription(
210     undef,      'BRANCH2',     $id_supplier1, undef, $id_budget, $biblionumber,
211     '01-01-2013',undef, undef, undef,  undef,
212     undef,      undef,  undef, undef, undef, undef,
213     1,          "subscription notes",undef, '01-01-2013', undef, undef,
214     undef, 'CALL DEF',  0,    "intnotes",  0,
215     undef, undef, 0,          undef,         '2013-07-31', 0
216 );
217
218 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
219 is( $bookseller1fromid->{subscriptioncount},
220     2, 'Supplier1 has 2 subscriptions' );
221
222 #Test ModBookseller
223 $sample_supplier2 = {
224     id            => $id_supplier2,
225     name          => 'Name2 modified',
226     address1      => 'address1_2 modified',
227     address2      => 'address2-2 modified',
228     address3      => 'address3_2 modified',
229     address4      => 'address4_2 modified',
230     postal        => 'postal2 modified',
231     phone         => 'phone2 modified',
232     accountnumber => 'accountnumber2 modified',
233     fax           => 'fax2 modified',
234     url           => 'url2 modified',
235     contact       => 'contact2 modified',
236     contpos       => 'contpos2 modified',
237     contphone     => 'contphone2 modified',
238     contfax       => 'contefax2 modified',
239     contaltphone  => 'contaltphone2 modified',
240     contemail     => 'contemail2 modified',
241     contnotes     => 'contnotes2 modified',
242     active        => 1,
243     gstreg        => 1,
244     listincgst    => 1,
245     invoiceincgst => 1,
246     gstrate       => '2.0000 ',
247     discount      => '2.0000',
248     notes         => 'notes2 modified',
249     deliverytime  => 2,
250 };
251
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 => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 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 = NewSubscription(
368     undef,      "BRANCH1",     $id_supplier1, undef, $id_budget, $biblionumber,
369     '01-01-2013',undef, undef, undef,  undef,
370     undef,      undef,  undef, undef, undef, undef,
371     1,          "subscription notes",undef, '01-01-2013', undef, undef,
372     undef,       undef,  0,    "intnotes",  0,
373     undef, undef, 0,          'LOCA',         '2013-12-31', 0
374 );
375
376 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
377 is(scalar(@subscriptions), 3, 'search for subscriptions by expiration date');
378 @subscriptions = SearchSubscriptions({expiration_date => '2013-08-15'});
379 is(scalar(@subscriptions), 1, 'search for subscriptions by expiration date');
380 @subscriptions = SearchSubscriptions({callnumber => 'CALL'});
381 is(scalar(@subscriptions), 2, 'search for subscriptions by call number');
382 @subscriptions = SearchSubscriptions({callnumber => 'DEF'});
383 is(scalar(@subscriptions), 1, 'search for subscriptions by call number');
384 @subscriptions = SearchSubscriptions({location => 'LOCA'});
385 is(scalar(@subscriptions), 1, 'search for subscriptions by location');
386
387 #Add 4 orders
388 my ( $ordernumber1, $ordernumber2, $ordernumber3, $ordernumber4 );
389 my ( $basketno1,    $basketno2,    $basketno3,    $basketno4 );
390 ( $basketno1, $ordernumber1 ) = C4::Acquisition::NewOrder(
391     {
392         basketno         => $sample_basket1,
393         quantity         => 24,
394         biblionumber     => $biblionumber,
395         budget_id        => $id_budget,
396         entrydate        => '01-01-2013',
397         currency         => 'EUR',
398         notes            => "This is a note1",
399         gstrate          => 0.0500,
400         orderstatus      => 1,
401         subscriptionid   => $id_subscription1,
402         quantityreceived => 2,
403         rrp              => 10,
404         ecost            => 10,
405         datereceived     => '01-06-2013'
406     }
407 );
408 ( $basketno2, $ordernumber2 ) = C4::Acquisition::NewOrder(
409     {
410         basketno       => $sample_basket2,
411         quantity       => 20,
412         biblionumber   => $biblionumber,
413         budget_id      => $id_budget,
414         entrydate      => '01-01-2013',
415         currency       => 'EUR',
416         notes          => "This is a note2",
417         gstrate        => 0.0500,
418         orderstatus    => 1,
419         subscriptionid => $id_subscription2,
420         rrp            => 10,
421         ecost          => 10,
422     }
423 );
424 ( $basketno3, $ordernumber3 ) = C4::Acquisition::NewOrder(
425     {
426         basketno       => $sample_basket3,
427         quantity       => 20,
428         biblionumber   => $biblionumber,
429         budget_id      => $id_budget,
430         entrydate      => '02-02-2013',
431         currency       => 'EUR',
432         notes          => "This is a note3",
433         gstrate        => 0.0500,
434         orderstatus    => 2,
435         subscriptionid => $id_subscription3,
436         rrp            => 11,
437         ecost          => 11,
438     }
439 );
440 ( $basketno4, $ordernumber4 ) = C4::Acquisition::NewOrder(
441     {
442         basketno         => $sample_basket4,
443         quantity         => 20,
444         biblionumber     => $biblionumber,
445         budget_id        => $id_budget,
446         entrydate        => '02-02-2013',
447         currency         => 'EUR',
448         notes            => "This is a note3",
449         gstrate          => 0.0500,
450         orderstatus      => 2,
451         subscriptionid   => $id_subscription3,
452         rrp              => 11,
453         ecost            => 11,
454         quantityreceived => 20
455     }
456 );
457
458 #Test cases:
459 # Sample datas :
460 #   Supplier1: delivery -> undef Basket1 : closedate -> today
461 #   Supplier2: delivery -> 2     Basket2 : closedate -> $daysago5
462 #   Supplier3: delivery -> 3     Basket3 : closedate -> $daysago10
463 #Case 1 : Without parameters:
464 #   quantityreceived < quantity 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 #   datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
467 #   quantityreceived = quantity -NOT LATE-
468 my %suppliers = C4::Bookseller::GetBooksellersWithLateOrders();
469 ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders" );
470 ok( exists( $suppliers{$id_supplier2} ), "Supplier2 has late orders" );
471 ok( exists( $suppliers{$id_supplier3} ), "Supplier3 has late orders" );
472 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" )
473   ;    # Quantity = quantityreceived
474
475 #Case 2: With $delay = 4
476 #    today + 0 > now-$delay -NOT LATE-
477 #    (today-5) + 2   <= now() - $delay -NOT LATE-
478 #    (today-10) + 3  <= now() - $delay -LATE-
479 #    quantityreceived = quantity -NOT LATE-
480 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 4, undef, undef );
481 isnt( exists( $suppliers{$id_supplier1} ),
482     1, "Supplier1 has late orders but  today  > now() - 4 days" );
483 isnt( exists( $suppliers{$id_supplier2} ),
484     1, "Supplier2 has late orders and $daysago5 <= now() - (4 days+2)" );
485 ok( exists( $suppliers{$id_supplier3} ),
486     "Supplier3 has late orders and $daysago10  <= now() - (4 days+3)" );
487 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
488
489 #Case 3: With $delay = -1
490 is( C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ),
491     undef, "-1 is a wrong value for a delay" );
492
493 #Case 4: With $delay = 0
494 #    today  == now-0 -LATE- (if no deliverytime or deliverytime == 0)
495 #    today-5   <= now() - $delay+2 -LATE-
496 #    today-10  <= now() - $delay+3 -LATE-
497 #    quantityreceived = quantity -NOT LATE-
498 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 0, undef, undef );
499
500 ok( exists( $suppliers{$id_supplier1} ),
501     "Supplier1 has late orders but $today == now() - 0 days" )
502   ;
503 ok( exists( $suppliers{$id_supplier2} ),
504     "Supplier2 has late orders and $daysago5 <= now() - 2" );
505 ok( exists( $suppliers{$id_supplier3} ),
506     "Supplier3 has late orders and $daysago10 <= now() - 3" );
507 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
508
509 #Case 5 : With $estimateddeliverydatefrom = today-4
510 #    today >= today-4 -NOT LATE-
511 #    (today-5)+ 2 days >= today-4  -LATE-
512 #    (today-10) + 3 days < today-4   -NOT LATE-
513 #    quantityreceived = quantity -NOT LATE-
514 my $dt_today3 = dt_from_string;
515 my $dur4 = DateTime::Duration->new( days => -4 );
516 $dt_today3->add_duration($dur4);
517 my $daysago4 =  output_pref({ dt => $dt_today3, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
518 %suppliers =
519   C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago4, undef );
520
521 ok( exists( $suppliers{$id_supplier1} ),
522     "Supplier1 has late orders and $today >= $daysago4 -deliverytime undef" );
523 ok( exists( $suppliers{$id_supplier2} ),
524     "Supplier2 has late orders and $daysago5 + 2 days >= $daysago4 " );
525 isnt( exists( $suppliers{$id_supplier3} ),
526     1, "Supplier3 has late orders and $daysago10 + 5 days < $daysago4 " );
527 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
528
529 #Case 6: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 5
530 #    $daysago10<$daysago5<today -NOT LATE-
531 #    $daysago10<$daysago5<$daysago5 +2 -NOT lATE-
532 #    $daysago10<$daysago10 +3 <$daysago5 -LATE-
533 #    quantityreceived = quantity -NOT LATE-
534 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
535     $daysago5 );
536 isnt( exists( $suppliers{$id_supplier1} ),
537     1, "Supplier1 has late orders but $daysago10 < $daysago5 < $today" );
538 isnt(
539     exists( $suppliers{$id_supplier2} ),
540     1,
541     "Supplier2 has late orders but $daysago10 < $daysago5 < $daysago5+2"
542 );
543 ok(
544     exists( $suppliers{$id_supplier3} ),
545 "Supplier3 has late orders and $daysago10 <= $daysago10 +3 <= $daysago5"
546 );
547 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
548
549 #Case 7: With $estimateddeliverydateto = today-5
550 #    $today >= $daysago5  -NOT LATE-
551 #    $daysago5 + 2 days  > $daysago5 -NOT LATE-
552 #    $daysago10 + 3  <+ $daysago5  -LATE-
553 #    quantityreceived = quantity -NOT LATE-
554 %suppliers =
555   C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago5 );
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 isnt(
619     exists( $suppliers{$id_supplier2} ),
620     1,
621 "Supplier2 has late orders but $daysago5 + 2 days > $daysago5  and $daysago5 > now() - 2+5 days"
622 );
623 ok(
624     exists( $suppliers{$id_supplier3} ),
625 "Supplier2 has late orders and $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days "
626 );
627 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
628
629 #Case 11: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 10
630 #    $daysago10==$daysago10==$daysago10 -NOT LATE-
631 #    $daysago10==$daysago10<$daysago5+2-NOT lATE-
632 #    $daysago10==$daysago10 <$daysago10+3-LATE-
633 #    quantityreceived = quantity -NOT LATE-
634
635 #Basket1 closedate -> $daysago10
636 $basket1info = {
637     basketno  => $sample_basket1,
638     closedate => $daysago10,
639 };
640 ModBasket($basket1info);
641 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
642     $daysago10 );
643 ok( exists( $suppliers{$id_supplier1} ),
644     "Supplier1 has late orders and $daysago10==$daysago10==$daysago10 " )
645   ;
646 isnt( exists( $suppliers{$id_supplier2} ),
647     1,
648     "Supplier2 has late orders but $daysago10==$daysago10<$daysago5+2" );
649 isnt( exists( $suppliers{$id_supplier3} ),
650     1,
651     "Supplier3 has late orders but $daysago10==$daysago10 <$daysago10+3" );
652 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
653
654 #Case 12: closedate == $estimateddeliverydatefrom =today-10
655 %suppliers =
656   C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10, undef );
657 ok( exists( $suppliers{$id_supplier1} ),
658     "Supplier1 has late orders and $daysago10==$daysago10 " );
659
660 #Case 13: closedate == $estimateddeliverydateto =today-10
661 %suppliers =
662   C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago10 );
663 ok( exists( $suppliers{$id_supplier1} ),
664     "Supplier1 has late orders and $daysago10==$daysago10 " )
665   ;
666
667 C4::Context->_new_userenv('DUMMY SESSION');
668 C4::Context::set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, '', '');
669 my $userenv = C4::Context->userenv;
670
671 my $module = Test::MockModule->new('C4::Auth');
672 $module->mock(
673     'haspermission',
674     sub {
675         # simulate user that has serials permissions but
676         # NOT superserials
677         my ($userid, $flagsrequired) = @_;
678         return 0 if 'superserials' eq ($flagsrequired->{serials} // 0);
679         return exists($flagsrequired->{serials});
680     }
681 );
682
683 C4::Context->set_preference('IndependentBranches', 0);
684 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
685 is(
686     scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
687     3,
688     'ordinary user can see all subscriptions with IndependentBranches off'
689 );
690
691 C4::Context->set_preference('IndependentBranches', 1);
692 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
693 is(
694     scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
695     1,
696     'ordinary user can see only their library\'s subscriptions with IndependentBranches on'
697 );
698
699 # don the cape and turn into Superlibrarian!
700 C4::Context::set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 1, '', '');
701 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
702 is(
703     scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
704     3,
705     'superlibrarian can see all subscriptions with IndependentBranches on (bug 12048)'
706 );
707
708 #End transaction
709 $dbh->rollback;
710
711 #field_filter filters the useless fields or foreign keys
712 #NOTE: all the fields of aqbookseller arent considered
713 #returns a cleaned structure
714 sub field_filter {
715     my ($struct) = @_;
716
717     for my $field (
718         'bookselleremail', 'booksellerfax',
719         'booksellerurl',   'othersupplier',
720         'currency',        'invoiceprice',
721         'listprice'
722       )
723     {
724
725         if ( grep { /^$field$/ } keys %$struct ) {
726             delete $struct->{$field};
727         }
728     }
729     return $struct;
730 }