Bug 12482: removing the use of the module C4::SQLHelper.pm, it is replaced by DBIx...
[koha.git] / 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_period_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_period_id   => $budgetperiod
184 });
185 my $bib = MARC::Record->new();
186 $bib->append_fields(
187     MARC::Field->new('245', ' ', ' ', a => 'Journal of ethnology'),
188     MARC::Field->new('500', ' ', ' ', a => 'bib notes'),
189 );
190 my ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
191 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
192 is( $bookseller1fromid->{subscriptioncount},
193     0, 'Supplier1 has 0 subscription' );
194
195 my $id_subscription1 = NewSubscription(
196     undef,      'BRANCH2',     $id_supplier1, undef, $id_budget, $biblionumber,
197     '01-01-2013',undef, undef, undef,  undef,
198     undef,      undef,  undef, undef, undef, undef,
199     1,          "subscription notes",undef, '01-01-2013', undef, undef,
200     undef, 'CALL ABC',  0,    "intnotes",  0,
201     undef, undef, 0,          undef,         '2013-11-30', 0
202 );
203
204 my @subscriptions = SearchSubscriptions({biblionumber => $biblionumber});
205 is($subscriptions[0]->{publicnotes}, 'subscription notes', 'subscription search results include public notes (bug 10689)');
206
207 my $id_subscription2 = NewSubscription(
208     undef,      'BRANCH2',     $id_supplier1, undef, $id_budget, $biblionumber,
209     '01-01-2013',undef, undef, undef,  undef,
210     undef,      undef,  undef, undef, undef, undef,
211     1,          "subscription notes",undef, '01-01-2013', undef, undef,
212     undef, 'CALL DEF',  0,    "intnotes",  0,
213     undef, undef, 0,          undef,         '2013-07-31', 0
214 );
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 my $modif1 = C4::Bookseller::ModBookseller();
251 is( $modif1, undef,
252     "ModBookseller returns undef if no params given - Nothing happened" );
253 $modif1 = C4::Bookseller::ModBookseller($sample_supplier2);
254 is( $modif1, 1, "ModBookseller modifies only the supplier2" );
255 is( scalar( C4::Bookseller::GetBookSeller('') ),
256     $count + 2, "Supplier2 has been modified - Nothing added" );
257
258 $modif1 = C4::Bookseller::ModBookseller(
259     {
260         id   => -1,
261         name => 'name3'
262     }
263 );
264 #is( $modif1, '0E0',
265 #    "ModBookseller returns OEO if the id doesnt exist - Nothing modified" );
266
267 #Test GetBooksellersWithLateOrders
268 #Add 2 suppliers
269 my $sample_supplier3 = {
270     name          => 'Name3',
271     address1      => 'address1_3',
272     address2      => 'address1-3',
273     address3      => 'address1_3',
274     address4      => 'address1_3',
275     postal        => 'postal3',
276     phone         => 'phone3',
277     accountnumber => 'accountnumber3',
278     fax           => 'fax3',
279     url           => 'url3',
280     contact       => 'contact3',
281     contpos       => 'contpos3',
282     contphone     => 'contphone3',
283     contfax       => 'contefax3',
284     contaltphone  => 'contaltphone3',
285     contemail     => 'contemail3',
286     contnotes     => 'contnotes3',
287     active        => 1,
288     gstreg        => 1,
289     listincgst    => 1,
290     invoiceincgst => 1,
291     gstrate       => '3.0000',
292     discount      => '3.0000',
293     notes         => 'notes3',
294     deliverytime  => 3
295 };
296 my $sample_supplier4 = {
297     name          => 'Name4',
298     address1      => 'address1_4',
299     address2      => 'address1-4',
300     address3      => 'address1_4',
301     address4      => 'address1_4',
302     postal        => 'postal4',
303     phone         => 'phone4',
304     accountnumber => 'accountnumber4',
305     fax           => 'fax4',
306     url           => 'url4',
307     contact       => 'contact4',
308     contpos       => 'contpos4',
309     contphone     => 'contphone4',
310     contfax       => 'contefax4',
311     contaltphone  => 'contaltphone4',
312     contemail     => 'contemail4',
313     contnotes     => 'contnotes4',
314     active        => 1,
315     gstreg        => 1,
316     listincgst    => 1,
317     invoiceincgst => 1,
318     gstrate       => '3.0000',
319     discount      => '3.0000',
320     notes         => 'notes3',
321 };
322 my $id_supplier3 = C4::Bookseller::AddBookseller($sample_supplier3);
323 my $id_supplier4 = C4::Bookseller::AddBookseller($sample_supplier4);
324
325 #Add 2 baskets
326 my $sample_basket3 =
327   C4::Acquisition::NewBasket( $id_supplier3, 'authorisedby3', 'basketname3',
328     'basketnote3' );
329 my $sample_basket4 =
330   C4::Acquisition::NewBasket( $id_supplier4, 'authorisedby4', 'basketname4',
331     'basketnote4' );
332
333 #Modify the basket to add a close date
334 my $basket1info = {
335     basketno     => $sample_basket1,
336     closedate    => $today,
337     booksellerid => $id_supplier1
338 };
339
340 my $basket2info = {
341     basketno     => $sample_basket2,
342     closedate    => $daysago5,
343     booksellerid => $id_supplier2
344 };
345
346 my $dt_today2 = dt_from_string;
347 my $dur10 = DateTime::Duration->new( days => -10 );
348 $dt_today2->add_duration($dur10);
349 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
350 my $basket3info = {
351     basketno  => $sample_basket3,
352     closedate => $daysago10,
353 };
354
355 my $basket4info = {
356     basketno  => $sample_basket4,
357     closedate => $today,
358 };
359 ModBasket($basket1info);
360 ModBasket($basket2info);
361 ModBasket($basket3info);
362 ModBasket($basket4info);
363
364 #Add 1 subscription
365 my $id_subscription3 = NewSubscription(
366     undef,      "BRANCH1",     $id_supplier1, undef, $id_budget, $biblionumber,
367     '01-01-2013',undef, undef, undef,  undef,
368     undef,      undef,  undef, undef, undef, undef,
369     1,          "subscription notes",undef, '01-01-2013', undef, undef,
370     undef,       undef,  0,    "intnotes",  0,
371     undef, undef, 0,          'LOCA',         '2013-12-31', 0
372 );
373
374 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
375 is(scalar(@subscriptions), 3, 'search for subscriptions by expiration date');
376 @subscriptions = SearchSubscriptions({expiration_date => '2013-08-15'});
377 is(scalar(@subscriptions), 1, 'search for subscriptions by expiration date');
378 @subscriptions = SearchSubscriptions({callnumber => 'CALL'});
379 is(scalar(@subscriptions), 2, 'search for subscriptions by call number');
380 @subscriptions = SearchSubscriptions({callnumber => 'DEF'});
381 is(scalar(@subscriptions), 1, 'search for subscriptions by call number');
382 @subscriptions = SearchSubscriptions({location => 'LOCA'});
383 is(scalar(@subscriptions), 1, 'search for subscriptions by location');
384
385 #Add 4 orders
386 my ( $ordernumber1, $ordernumber2, $ordernumber3, $ordernumber4 );
387 my ( $basketno1,    $basketno2,    $basketno3,    $basketno4 );
388 ( $basketno1, $ordernumber1 ) = C4::Acquisition::NewOrder(
389     {
390         basketno         => $sample_basket1,
391         quantity         => 24,
392         biblionumber     => $biblionumber,
393         budget_id        => $id_budget,
394         entrydate        => '01-01-2013',
395         currency         => 'EUR',
396         notes            => "This is a note1",
397         gstrate          => 0.0500,
398         orderstatus      => 1,
399         subscriptionid   => $id_subscription1,
400         quantityreceived => 2,
401         rrp              => 10,
402         ecost            => 10,
403         datereceived     => '01-06-2013'
404     }
405 );
406 ( $basketno2, $ordernumber2 ) = C4::Acquisition::NewOrder(
407     {
408         basketno       => $sample_basket2,
409         quantity       => 20,
410         biblionumber   => $biblionumber,
411         budget_id      => $id_budget,
412         entrydate      => '01-01-2013',
413         currency       => 'EUR',
414         notes          => "This is a note2",
415         gstrate        => 0.0500,
416         orderstatus    => 1,
417         subscriptionid => $id_subscription2,
418         rrp            => 10,
419         ecost          => 10,
420     }
421 );
422 ( $basketno3, $ordernumber3 ) = C4::Acquisition::NewOrder(
423     {
424         basketno       => $sample_basket3,
425         quantity       => 20,
426         biblionumber   => $biblionumber,
427         budget_id      => $id_budget,
428         entrydate      => '02-02-2013',
429         currency       => 'EUR',
430         notes          => "This is a note3",
431         gstrate        => 0.0500,
432         orderstatus    => 2,
433         subscriptionid => $id_subscription3,
434         rrp            => 11,
435         ecost          => 11,
436     }
437 );
438 ( $basketno4, $ordernumber4 ) = C4::Acquisition::NewOrder(
439     {
440         basketno         => $sample_basket4,
441         quantity         => 20,
442         biblionumber     => $biblionumber,
443         budget_id        => $id_budget,
444         entrydate        => '02-02-2013',
445         currency         => 'EUR',
446         notes            => "This is a note3",
447         gstrate          => 0.0500,
448         orderstatus      => 2,
449         subscriptionid   => $id_subscription3,
450         rrp              => 11,
451         ecost            => 11,
452         quantityreceived => 20
453     }
454 );
455
456 #Test cases:
457 # Sample datas :
458 #   Supplier1: delivery -> undef Basket1 : closedate -> today
459 #   Supplier2: delivery -> 2     Basket2 : closedate -> $daysago5
460 #   Supplier3: delivery -> 3     Basket3 : closedate -> $daysago10
461 #Case 1 : Without parameters:
462 #   quantityreceived < quantity AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
463 #   datereceived !null 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 #   quantityreceived = quantity -NOT LATE-
466 my %suppliers = C4::Bookseller::GetBooksellersWithLateOrders();
467 ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders" );
468 ok( exists( $suppliers{$id_supplier2} ), "Supplier2 has late orders" );
469 ok( exists( $suppliers{$id_supplier3} ), "Supplier3 has late orders" );
470 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" )
471   ;    # Quantity = quantityreceived
472
473 #Case 2: With $delay = 4
474 #    today + 0 > now-$delay -NOT LATE-
475 #    (today-5) + 2   <= now() - $delay -NOT LATE-
476 #    (today-10) + 3  <= now() - $delay -LATE-
477 #    quantityreceived = quantity -NOT LATE-
478 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 4, undef, undef );
479 isnt( exists( $suppliers{$id_supplier1} ),
480     1, "Supplier1 has late orders but  today  > now() - 4 days" );
481 isnt( exists( $suppliers{$id_supplier2} ),
482     1, "Supplier2 has late orders and $daysago5 <= now() - (4 days+2)" );
483 ok( exists( $suppliers{$id_supplier3} ),
484     "Supplier3 has late orders and $daysago10  <= now() - (4 days+3)" );
485 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
486
487 #Case 3: With $delay = -1
488 is( C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ),
489     undef, "-1 is a wrong value for a delay" );
490
491 #Case 4: With $delay = 0
492 #    today  == now-0 -LATE- (if no deliverytime or deliverytime == 0)
493 #    today-5   <= now() - $delay+2 -LATE-
494 #    today-10  <= now() - $delay+3 -LATE-
495 #    quantityreceived = quantity -NOT LATE-
496 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 0, undef, undef );
497
498 ok( exists( $suppliers{$id_supplier1} ),
499     "Supplier1 has late orders but $today == now() - 0 days" )
500   ;
501 ok( exists( $suppliers{$id_supplier2} ),
502     "Supplier2 has late orders and $daysago5 <= now() - 2" );
503 ok( exists( $suppliers{$id_supplier3} ),
504     "Supplier3 has late orders and $daysago10 <= now() - 3" );
505 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
506
507 #Case 5 : With $estimateddeliverydatefrom = today-4
508 #    today >= today-4 -NOT LATE-
509 #    (today-5)+ 2 days >= today-4  -LATE-
510 #    (today-10) + 3 days < today-4   -NOT LATE-
511 #    quantityreceived = quantity -NOT LATE-
512 my $dt_today3 = dt_from_string;
513 my $dur4 = DateTime::Duration->new( days => -4 );
514 $dt_today3->add_duration($dur4);
515 my $daysago4 =  output_pref({ dt => $dt_today3, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
516 %suppliers =
517   C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago4, undef );
518
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 isnt( exists( $suppliers{$id_supplier1} ),
555     1,
556     "Supplier1 has late orders but $today >= $daysago5 - deliverytime undef" );
557 isnt( exists( $suppliers{$id_supplier2} ),
558     1, "Supplier2 has late orders but  $daysago5 + 2 days  > $daysago5 " );
559 ok( exists( $suppliers{$id_supplier3} ),
560     "Supplier3 has late orders and $daysago10 + 3  <= $daysago5" );
561 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
562
563 #Test with $estimateddeliverydatefrom and  $estimateddeliverydateto and $delay
564 #Case 8 :With $estimateddeliverydatefrom = 2013-07-05 and  $estimateddeliverydateto = 2013-07-08 and $delay =5
565 #    $daysago4<today<=$today and $today<now()-3  -NOT LATE-
566 #    $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days -LATE-
567 #    $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days -NOT LATE-
568 #    quantityreceived = quantity -NOT LATE-
569 %suppliers =
570   C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago4, $today );
571 isnt(
572     exists( $suppliers{$id_supplier1} ),
573     1,
574     "Supplier1 has late orders but $daysago4<today<=$today and $today<now()-3"
575 );
576 ok(
577     exists( $suppliers{$id_supplier2} ),
578 "Supplier2 has late orders and $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days"
579 );
580 isnt(
581     exists( $suppliers{$id_supplier3} ),
582 "Supplier3 has late orders but $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days"
583 );
584 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
585
586 #Case 9 :With $estimateddeliverydatefrom = $daysago5  and $delay = 3
587 #   $today < $daysago5 and $today > $today-5 -NOT LATE-
588 #   $daysago5 + 2 days >= $daysago5  and $daysago5 < today - 3+2 -LATE-
589 #   $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2-NOT LATE-
590 #   quantityreceived = quantity -NOT LATE-
591 %suppliers =
592   C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago5, undef );
593 isnt( exists( $suppliers{$id_supplier1} ),
594     1, "$today < $daysago10 and $today > $today-3" );
595 ok(
596     exists( $suppliers{$id_supplier2} ),
597 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago5  and $daysago5 < today - 3+2"
598 );
599 isnt(
600     exists( $suppliers{$id_supplier3} ),
601     1,
602 "Supplier2 has late orders but $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2 "
603 );
604 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
605
606 #Test with $estimateddeliverydateto  and $delay
607 #Case 10:With $estimateddeliverydateto = $daysago5 and $delay = 5
608 #    today > $daysago5 today > now() -5 -NOT LATE-
609 #    $daysago5 + 2 days > $daysago5  and $daysago5 > now() - 2+5 days -NOT LATE-
610 #    $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days -LATE-
611 #    quantityreceived = quantity -NOT LATE-
612 %suppliers =
613   C4::Bookseller::GetBooksellersWithLateOrders( 5, undef, $daysago5 );
614 isnt( exists( $suppliers{$id_supplier1} ),
615     1, "Supplier2 has late orders but today > $daysago5 today > now() -5" );
616 isnt(
617     exists( $suppliers{$id_supplier2} ),
618     1,
619 "Supplier2 has late orders but $daysago5 + 2 days > $daysago5  and $daysago5 > now() - 2+5 days"
620 );
621 ok(
622     exists( $suppliers{$id_supplier3} ),
623 "Supplier2 has late orders and $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days "
624 );
625 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
626
627 #Case 11: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 10
628 #    $daysago10==$daysago10==$daysago10 -NOT LATE-
629 #    $daysago10==$daysago10<$daysago5+2-NOT lATE-
630 #    $daysago10==$daysago10 <$daysago10+3-LATE-
631 #    quantityreceived = quantity -NOT LATE-
632
633 #Basket1 closedate -> $daysago10
634 $basket1info = {
635     basketno  => $sample_basket1,
636     closedate => $daysago10,
637 };
638 ModBasket($basket1info);
639 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
640     $daysago10 );
641 ok( exists( $suppliers{$id_supplier1} ),
642     "Supplier1 has late orders and $daysago10==$daysago10==$daysago10 " )
643   ;
644 isnt( exists( $suppliers{$id_supplier2} ),
645     1,
646     "Supplier2 has late orders but $daysago10==$daysago10<$daysago5+2" );
647 isnt( exists( $suppliers{$id_supplier3} ),
648     1,
649     "Supplier3 has late orders but $daysago10==$daysago10 <$daysago10+3" );
650 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
651
652 #Case 12: closedate == $estimateddeliverydatefrom =today-10
653 %suppliers =
654   C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10, undef );
655 ok( exists( $suppliers{$id_supplier1} ),
656     "Supplier1 has late orders and $daysago10==$daysago10 " );
657
658 #Case 13: closedate == $estimateddeliverydateto =today-10
659 %suppliers =
660   C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago10 );
661 ok( exists( $suppliers{$id_supplier1} ),
662     "Supplier1 has late orders and $daysago10==$daysago10 " )
663   ;
664
665 C4::Context->_new_userenv('DUMMY SESSION');
666 C4::Context::set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, '', '');
667 my $userenv = C4::Context->userenv;
668
669 my $module = Test::MockModule->new('C4::Auth');
670 $module->mock(
671     'haspermission',
672     sub {
673         # simulate user that has serials permissions but
674         # NOT superserials
675         my ($userid, $flagsrequired) = @_;
676         return 0 if 'superserials' eq ($flagsrequired->{serials} // 0);
677         return exists($flagsrequired->{serials});
678     }
679 );
680
681 C4::Context->set_preference('IndependentBranches', 0);
682 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
683 is(
684     scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
685     3,
686     'ordinary user can see all subscriptions with IndependentBranches off'
687 );
688
689 C4::Context->set_preference('IndependentBranches', 1);
690 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
691 is(
692     scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
693     1,
694     'ordinary user can see only their library\'s subscriptions with IndependentBranches on'
695 );
696
697 # don the cape and turn into Superlibrarian!
698 C4::Context::set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 1, '', '');
699 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
700 is(
701     scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
702     3,
703     'superlibrarian can see all subscriptions with IndependentBranches on (bug 12048)'
704 );
705
706 #End transaction
707 $dbh->rollback;
708
709 #field_filter filters the useless fields or foreign keys
710 #NOTE: all the fields of aqbookseller arent considered
711 #returns a cleaned structure
712 sub field_filter {
713     my ($struct) = @_;
714
715     for my $field (
716         'bookselleremail', 'booksellerfax',
717         'booksellerurl',   'othersupplier',
718         'currency',        'invoiceprice',
719         'listprice'
720       )
721     {
722
723         if ( grep { /^$field$/ } keys %$struct ) {
724             delete $struct->{$field};
725         }
726     }
727     return $struct;
728 }