Bug 10402: (QA followup) amend unit tests
[koha.git] / t / db_dependent / Bookseller.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 86;
6 use Test::MockModule;
7 use Test::Warn;
8
9 use C4::Context;
10 use Koha::DateUtils;
11 use DateTime::Duration;
12 use C4::Acquisition;
13 use C4::Serials;
14 use C4::Budgets;
15 use C4::Biblio;
16
17 BEGIN {
18     use_ok('C4::Bookseller');
19 }
20
21 can_ok(
22
23     'C4::Bookseller', qw(
24       AddBookseller
25       DelBookseller
26       GetBookSeller
27       GetBookSellerFromId
28       GetBooksellersWithLateOrders
29       ModBookseller )
30 );
31
32 #Start transaction
33 my $dbh = C4::Context->dbh;
34 $dbh->{RaiseError} = 1;
35 $dbh->{AutoCommit} = 0;
36
37 #Start tests
38 $dbh->do(q|DELETE FROM aqorders|);
39 $dbh->do(q|DELETE FROM aqbasket|);
40 $dbh->do(q|DELETE FROM aqbooksellers|);
41 $dbh->do(q|DELETE FROM subscription|);
42
43 #Test AddBookseller
44 my $count            = scalar( C4::Bookseller::GetBookSeller('') );
45 my $sample_supplier1 = {
46     name          => 'Name1',
47     address1      => 'address1_1',
48     address2      => 'address1-2',
49     address3      => 'address1_2',
50     address4      => 'address1_2',
51     postal        => 'postal1',
52     phone         => 'phone1',
53     accountnumber => 'accountnumber1',
54     fax           => 'fax1',
55     url           => 'url1',
56     active        => 1,
57     gstreg        => 1,
58     listincgst    => 1,
59     invoiceincgst => 1,
60     gstrate       => '1.0000',
61     discount      => '1.0000',
62     notes         => 'notes1',
63     deliverytime  => undef
64 };
65 my $sample_supplier2 = {
66     name          => 'Name2',
67     address1      => 'address1_2',
68     address2      => 'address2-2',
69     address3      => 'address3_2',
70     address4      => 'address4_2',
71     postal        => 'postal2',
72     phone         => 'phone2',
73     accountnumber => 'accountnumber2',
74     fax           => 'fax2',
75     url           => 'url2',
76     active        => 1,
77     gstreg        => 1,
78     listincgst    => 1,
79     invoiceincgst => 1,
80     gstrate       => '2.0000',
81     discount      => '2.0000',
82     notes         => 'notes2',
83     deliverytime  => 2
84 };
85
86 my $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
87 my $id_supplier2 = C4::Bookseller::AddBookseller($sample_supplier2);
88
89 #my $id_bookseller3 = C4::Bookseller::AddBookseller();# NOTE : Doesn't work because the field name cannot be null
90
91 like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
92 like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
93 is( scalar( C4::Bookseller::GetBookSeller('') ),
94     $count + 2, "Supplier1 and Supplier2 have been added" );
95
96 #Test DelBookseller
97 my $del = C4::Bookseller::DelBookseller($id_supplier1);
98 is( $del, 1, "DelBookseller returns 1 - 1 supplier has been deleted " );
99 is( C4::Bookseller::GetBookSellerFromId($id_supplier1),
100     undef, "Supplier1  has been deleted - id_supplier1 doesnt exist anymore" );
101
102 #Test GetBookSeller
103 my @bookseller2 = C4::Bookseller::GetBookSeller( $sample_supplier2->{name} );
104 is( scalar(@bookseller2), 1, "Get only  Supplier2" );
105 $bookseller2[0] = field_filter( $bookseller2[0] );
106 delete $bookseller2[0]->{basketcount};
107
108 $sample_supplier2->{id} = $id_supplier2;
109 is_deeply( $bookseller2[0], $sample_supplier2,
110     "GetBookSeller returns the right informations about $sample_supplier2" );
111
112 $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
113 my @booksellers = C4::Bookseller::GetBookSeller('')
114   ;    #NOTE :without params, it returns all the booksellers
115 for my $i ( 0 .. scalar(@booksellers) - 1 ) {
116     $booksellers[$i] = field_filter( $booksellers[$i] );
117     delete $booksellers[$i]->{basketcount};
118 }
119
120 $sample_supplier1->{id} = $id_supplier1;
121 is( scalar(@booksellers), $count + 2, "Get  Supplier1 and Supplier2" );
122 my @tab = ( $sample_supplier1, $sample_supplier2 );
123 is_deeply( \@booksellers, \@tab,
124     "Returns right fields of Supplier1 and Supplier2" );
125
126 #Test basketcount
127 my @bookseller1 = C4::Bookseller::GetBookSeller( $sample_supplier1->{name} );
128 #FIXME : if there is 0 basket, GetBookSeller returns 1 as basketcount
129 #is( $bookseller1[0]->{basketcount}, 0, 'Supplier1 has 0 basket' );
130 my $sample_basket1 =
131   C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby1', 'basketname1' );
132 my $sample_basket2 =
133   C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby2', 'basketname2' );
134 @bookseller1 = C4::Bookseller::GetBookSeller( $sample_supplier1->{name} );
135 is( $bookseller1[0]->{basketcount}, 2, 'Supplier1 has 2 baskets' );
136
137 #Test GetBookSellerFromId
138 my $bookseller1fromid = C4::Bookseller::GetBookSellerFromId();
139 is( $bookseller1fromid, undef,
140     "GetBookSellerFromId returns undef if no id given" );
141 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
142 $bookseller1fromid = field_filter($bookseller1fromid);
143 delete $bookseller1fromid->{basketcount};
144 delete $bookseller1fromid->{subscriptioncount};
145 is_deeply( $bookseller1fromid, $sample_supplier1,
146     "Get Supplier1 (GetBookSellerFromId)" );
147
148 #Test basketcount
149 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
150 is( $bookseller1fromid->{basketcount}, 2, 'Supplier1 has 2 baskets' );
151
152 #Test subscriptioncount
153 my $dt_today    = dt_from_string;
154 my $today       = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
155
156 my $dt_today1 = dt_from_string;
157 my $dur5 = DateTime::Duration->new( days => -5 );
158 $dt_today1->add_duration($dur5);
159 my $daysago5 = output_pref({ dt => $dt_today1, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
160
161 my $budgetperiod = C4::Budgets::AddBudgetPeriod({
162     budget_period_startdate   => $daysago5,
163     budget_period_enddate     => $today,
164     budget_period_description => "budget desc"
165 });
166 my $id_budget = AddBudget({
167     budget_code        => "CODE",
168     budget_amount      => "123.132",
169     budget_name        => "Budgetname",
170     budget_notes       => "This is a note",
171     budget_period_id   => $budgetperiod
172 });
173 my $bib = MARC::Record->new();
174 $bib->append_fields(
175     MARC::Field->new('245', ' ', ' ', a => 'Journal of ethnology'),
176     MARC::Field->new('500', ' ', ' ', a => 'bib notes'),
177 );
178 my ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
179 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
180 is( $bookseller1fromid->{subscriptioncount},
181     0, 'Supplier1 has 0 subscription' );
182
183 my $id_subscription1 = NewSubscription(
184     undef,      'BRANCH2',     $id_supplier1, undef, $id_budget, $biblionumber,
185     '01-01-2013',undef, undef, undef,  undef,
186     undef,      undef,  undef, undef, undef, undef,
187     1,          "subscription notes",undef, '01-01-2013', undef, undef,
188     undef, 'CALL ABC',  0,    "intnotes",  0,
189     undef, undef, 0,          undef,         '2013-11-30', 0
190 );
191
192 my @subscriptions = SearchSubscriptions({biblionumber => $biblionumber});
193 is($subscriptions[0]->{publicnotes}, 'subscription notes', 'subscription search results include public notes (bug 10689)');
194
195 my $id_subscription2 = 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 DEF',  0,    "intnotes",  0,
201     undef, undef, 0,          undef,         '2013-07-31', 0
202 );
203
204 $bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
205 is( $bookseller1fromid->{subscriptioncount},
206     2, 'Supplier1 has 2 subscriptions' );
207
208 #Test ModBookseller
209 $sample_supplier2 = {
210     id            => $id_supplier2,
211     name          => 'Name2 modified',
212     address1      => 'address1_2 modified',
213     address2      => 'address2-2 modified',
214     address3      => 'address3_2 modified',
215     address4      => 'address4_2 modified',
216     postal        => 'postal2 modified',
217     phone         => 'phone2 modified',
218     accountnumber => 'accountnumber2 modified',
219     fax           => 'fax2 modified',
220     url           => 'url2 modified',
221     active        => 1,
222     gstreg        => 1,
223     listincgst    => 1,
224     invoiceincgst => 1,
225     gstrate       => '2.0000 ',
226     discount      => '2.0000',
227     notes         => 'notes2 modified',
228     deliverytime  => 2,
229 };
230
231 my $modif1 = C4::Bookseller::ModBookseller();
232 is( $modif1, undef,
233     "ModBookseller returns undef if no params given - Nothing happened" );
234 $modif1 = C4::Bookseller::ModBookseller($sample_supplier2);
235 is( $modif1, 1, "ModBookseller modifies only the supplier2" );
236 is( scalar( C4::Bookseller::GetBookSeller('') ),
237     $count + 2, "Supplier2 has been modified - Nothing added" );
238
239 $modif1 = C4::Bookseller::ModBookseller(
240     {
241         id   => -1,
242         name => 'name3'
243     }
244 );
245 #is( $modif1, '0E0',
246 #    "ModBookseller returns OEO if the id doesnt exist - Nothing modified" );
247
248 #Test GetBooksellersWithLateOrders
249 #Add 2 suppliers
250 my $sample_supplier3 = {
251     name          => 'Name3',
252     address1      => 'address1_3',
253     address2      => 'address1-3',
254     address3      => 'address1_3',
255     address4      => 'address1_3',
256     postal        => 'postal3',
257     phone         => 'phone3',
258     accountnumber => 'accountnumber3',
259     fax           => 'fax3',
260     url           => 'url3',
261     active        => 1,
262     gstreg        => 1,
263     listincgst    => 1,
264     invoiceincgst => 1,
265     gstrate       => '3.0000',
266     discount      => '3.0000',
267     notes         => 'notes3',
268     deliverytime  => 3
269 };
270 my $sample_supplier4 = {
271     name          => 'Name4',
272     address1      => 'address1_4',
273     address2      => 'address1-4',
274     address3      => 'address1_4',
275     address4      => 'address1_4',
276     postal        => 'postal4',
277     phone         => 'phone4',
278     accountnumber => 'accountnumber4',
279     fax           => 'fax4',
280     url           => 'url4',
281     active        => 1,
282     gstreg        => 1,
283     listincgst    => 1,
284     invoiceincgst => 1,
285     gstrate       => '3.0000',
286     discount      => '3.0000',
287     notes         => 'notes3',
288 };
289 my $id_supplier3 = C4::Bookseller::AddBookseller($sample_supplier3);
290 my $id_supplier4 = C4::Bookseller::AddBookseller($sample_supplier4);
291
292 #Add 2 baskets
293 my $sample_basket3 =
294   C4::Acquisition::NewBasket( $id_supplier3, 'authorisedby3', 'basketname3',
295     'basketnote3' );
296 my $sample_basket4 =
297   C4::Acquisition::NewBasket( $id_supplier4, 'authorisedby4', 'basketname4',
298     'basketnote4' );
299
300 #Modify the basket to add a close date
301 my $basket1info = {
302     basketno     => $sample_basket1,
303     closedate    => $today,
304     booksellerid => $id_supplier1
305 };
306
307 my $basket2info = {
308     basketno     => $sample_basket2,
309     closedate    => $daysago5,
310     booksellerid => $id_supplier2
311 };
312
313 my $dt_today2 = dt_from_string;
314 my $dur10 = DateTime::Duration->new( days => -10 );
315 $dt_today2->add_duration($dur10);
316 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
317 my $basket3info = {
318     basketno  => $sample_basket3,
319     closedate => $daysago10,
320 };
321
322 my $basket4info = {
323     basketno  => $sample_basket4,
324     closedate => $today,
325 };
326 ModBasket($basket1info);
327 ModBasket($basket2info);
328 ModBasket($basket3info);
329 ModBasket($basket4info);
330
331 #Add 1 subscription
332 my $id_subscription3 = NewSubscription(
333     undef,      "BRANCH1",     $id_supplier1, undef, $id_budget, $biblionumber,
334     '01-01-2013',undef, undef, undef,  undef,
335     undef,      undef,  undef, undef, undef, undef,
336     1,          "subscription notes",undef, '01-01-2013', undef, undef,
337     undef,       undef,  0,    "intnotes",  0,
338     undef, undef, 0,          'LOCA',         '2013-12-31', 0
339 );
340
341 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
342 is(scalar(@subscriptions), 3, 'search for subscriptions by expiration date');
343 @subscriptions = SearchSubscriptions({expiration_date => '2013-08-15'});
344 is(scalar(@subscriptions), 1, 'search for subscriptions by expiration date');
345 @subscriptions = SearchSubscriptions({callnumber => 'CALL'});
346 is(scalar(@subscriptions), 2, 'search for subscriptions by call number');
347 @subscriptions = SearchSubscriptions({callnumber => 'DEF'});
348 is(scalar(@subscriptions), 1, 'search for subscriptions by call number');
349 @subscriptions = SearchSubscriptions({location => 'LOCA'});
350 is(scalar(@subscriptions), 1, 'search for subscriptions by location');
351
352 #Add 4 orders
353 my ( $ordernumber1, $ordernumber2, $ordernumber3, $ordernumber4 );
354 my ( $basketno1,    $basketno2,    $basketno3,    $basketno4 );
355 ( $basketno1, $ordernumber1 ) = C4::Acquisition::NewOrder(
356     {
357         basketno         => $sample_basket1,
358         quantity         => 24,
359         biblionumber     => $biblionumber,
360         budget_id        => $id_budget,
361         entrydate        => '01-01-2013',
362         currency         => 'EUR',
363         notes            => "This is a note1",
364         gstrate          => 0.0500,
365         orderstatus      => 1,
366         subscriptionid   => $id_subscription1,
367         quantityreceived => 2,
368         rrp              => 10,
369         ecost            => 10,
370         datereceived     => '01-06-2013'
371     }
372 );
373 ( $basketno2, $ordernumber2 ) = C4::Acquisition::NewOrder(
374     {
375         basketno       => $sample_basket2,
376         quantity       => 20,
377         biblionumber   => $biblionumber,
378         budget_id      => $id_budget,
379         entrydate      => '01-01-2013',
380         currency       => 'EUR',
381         notes          => "This is a note2",
382         gstrate        => 0.0500,
383         orderstatus    => 1,
384         subscriptionid => $id_subscription2,
385         rrp            => 10,
386         ecost          => 10,
387     }
388 );
389 ( $basketno3, $ordernumber3 ) = C4::Acquisition::NewOrder(
390     {
391         basketno       => $sample_basket3,
392         quantity       => 20,
393         biblionumber   => $biblionumber,
394         budget_id      => $id_budget,
395         entrydate      => '02-02-2013',
396         currency       => 'EUR',
397         notes          => "This is a note3",
398         gstrate        => 0.0500,
399         orderstatus    => 2,
400         subscriptionid => $id_subscription3,
401         rrp            => 11,
402         ecost          => 11,
403     }
404 );
405 ( $basketno4, $ordernumber4 ) = C4::Acquisition::NewOrder(
406     {
407         basketno         => $sample_basket4,
408         quantity         => 20,
409         biblionumber     => $biblionumber,
410         budget_id        => $id_budget,
411         entrydate        => '02-02-2013',
412         currency         => 'EUR',
413         notes            => "This is a note3",
414         gstrate          => 0.0500,
415         orderstatus      => 2,
416         subscriptionid   => $id_subscription3,
417         rrp              => 11,
418         ecost            => 11,
419         quantityreceived => 20
420     }
421 );
422
423 #Test cases:
424 # Sample datas :
425 #   Supplier1: delivery -> undef Basket1 : closedate -> today
426 #   Supplier2: delivery -> 2     Basket2 : closedate -> $daysago5
427 #   Supplier3: delivery -> 3     Basket3 : closedate -> $daysago10
428 #Case 1 : Without parameters:
429 #   quantityreceived < quantity AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
430 #   datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
431 #   datereceived !null AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
432 #   quantityreceived = quantity -NOT LATE-
433 my %suppliers = C4::Bookseller::GetBooksellersWithLateOrders();
434 ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders" );
435 ok( exists( $suppliers{$id_supplier2} ), "Supplier2 has late orders" );
436 ok( exists( $suppliers{$id_supplier3} ), "Supplier3 has late orders" );
437 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" )
438   ;    # Quantity = quantityreceived
439
440 #Case 2: With $delay = 4
441 #    today + 0 > now-$delay -NOT LATE-
442 #    (today-5) + 2   <= now() - $delay -NOT LATE-
443 #    (today-10) + 3  <= now() - $delay -LATE-
444 #    quantityreceived = quantity -NOT LATE-
445 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 4, undef, undef );
446 isnt( exists( $suppliers{$id_supplier1} ),
447     1, "Supplier1 has late orders but  today  > now() - 4 days" );
448 isnt( exists( $suppliers{$id_supplier2} ),
449     1, "Supplier2 has late orders and $daysago5 <= now() - (4 days+2)" );
450 ok( exists( $suppliers{$id_supplier3} ),
451     "Supplier3 has late orders and $daysago10  <= now() - (4 days+3)" );
452 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
453
454 #Case 3: With $delay = -1
455 my $bslo;
456 warning_like
457   { $bslo = C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ) }
458     qr/^WARNING: GetBooksellerWithLateOrders is called with a negative value/,
459     "GetBooksellerWithLateOrders prints a warning on negative values";
460
461 is( $bslo, undef, "-1 is a wrong value for a delay" );
462
463 #Case 4: With $delay = 0
464 #    today  == now-0 -LATE- (if no deliverytime or deliverytime == 0)
465 #    today-5   <= now() - $delay+2 -LATE-
466 #    today-10  <= now() - $delay+3 -LATE-
467 #    quantityreceived = quantity -NOT LATE-
468 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 0, undef, undef );
469
470 ok( exists( $suppliers{$id_supplier1} ),
471     "Supplier1 has late orders but $today == now() - 0 days" )
472   ;
473 ok( exists( $suppliers{$id_supplier2} ),
474     "Supplier2 has late orders and $daysago5 <= now() - 2" );
475 ok( exists( $suppliers{$id_supplier3} ),
476     "Supplier3 has late orders and $daysago10 <= now() - 3" );
477 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
478
479 #Case 5 : With $estimateddeliverydatefrom = today-4
480 #    today >= today-4 -NOT LATE-
481 #    (today-5)+ 2 days >= today-4  -LATE-
482 #    (today-10) + 3 days < today-4   -NOT LATE-
483 #    quantityreceived = quantity -NOT LATE-
484 my $dt_today3 = dt_from_string;
485 my $dur4 = DateTime::Duration->new( days => -4 );
486 $dt_today3->add_duration($dur4);
487 my $daysago4 =  output_pref({ dt => $dt_today3, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
488 %suppliers =
489   C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago4, undef );
490
491 ok( exists( $suppliers{$id_supplier1} ),
492     "Supplier1 has late orders and $today >= $daysago4 -deliverytime undef" );
493 ok( exists( $suppliers{$id_supplier2} ),
494     "Supplier2 has late orders and $daysago5 + 2 days >= $daysago4 " );
495 isnt( exists( $suppliers{$id_supplier3} ),
496     1, "Supplier3 has late orders and $daysago10 + 5 days < $daysago4 " );
497 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
498
499 #Case 6: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 5
500 #    $daysago10<$daysago5<today -NOT LATE-
501 #    $daysago10<$daysago5<$daysago5 +2 -NOT lATE-
502 #    $daysago10<$daysago10 +3 <$daysago5 -LATE-
503 #    quantityreceived = quantity -NOT LATE-
504 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
505     $daysago5 );
506 isnt( exists( $suppliers{$id_supplier1} ),
507     1, "Supplier1 has late orders but $daysago10 < $daysago5 < $today" );
508 isnt(
509     exists( $suppliers{$id_supplier2} ),
510     1,
511     "Supplier2 has late orders but $daysago10 < $daysago5 < $daysago5+2"
512 );
513 ok(
514     exists( $suppliers{$id_supplier3} ),
515 "Supplier3 has late orders and $daysago10 <= $daysago10 +3 <= $daysago5"
516 );
517 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
518
519 #Case 7: With $estimateddeliverydateto = today-5
520 #    $today >= $daysago5  -NOT LATE-
521 #    $daysago5 + 2 days  > $daysago5 -NOT LATE-
522 #    $daysago10 + 3  <+ $daysago5  -LATE-
523 #    quantityreceived = quantity -NOT LATE-
524 %suppliers =
525   C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago5 );
526 isnt( exists( $suppliers{$id_supplier1} ),
527     1,
528     "Supplier1 has late orders but $today >= $daysago5 - deliverytime undef" );
529 isnt( exists( $suppliers{$id_supplier2} ),
530     1, "Supplier2 has late orders but  $daysago5 + 2 days  > $daysago5 " );
531 ok( exists( $suppliers{$id_supplier3} ),
532     "Supplier3 has late orders and $daysago10 + 3  <= $daysago5" );
533 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
534
535 #Test with $estimateddeliverydatefrom and  $estimateddeliverydateto and $delay
536 #Case 8 :With $estimateddeliverydatefrom = 2013-07-05 and  $estimateddeliverydateto = 2013-07-08 and $delay =5
537 #    $daysago4<today<=$today and $today<now()-3  -NOT LATE-
538 #    $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days -LATE-
539 #    $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days -NOT LATE-
540 #    quantityreceived = quantity -NOT LATE-
541 %suppliers =
542   C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago4, $today );
543 isnt(
544     exists( $suppliers{$id_supplier1} ),
545     1,
546     "Supplier1 has late orders but $daysago4<today<=$today and $today<now()-3"
547 );
548 ok(
549     exists( $suppliers{$id_supplier2} ),
550 "Supplier2 has late orders and $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days"
551 );
552 isnt(
553     exists( $suppliers{$id_supplier3} ),
554 "Supplier3 has late orders but $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days"
555 );
556 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
557
558 #Case 9 :With $estimateddeliverydatefrom = $daysago5  and $delay = 3
559 #   $today < $daysago5 and $today > $today-5 -NOT LATE-
560 #   $daysago5 + 2 days >= $daysago5  and $daysago5 < today - 3+2 -LATE-
561 #   $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2-NOT LATE-
562 #   quantityreceived = quantity -NOT LATE-
563 %suppliers =
564   C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago5, undef );
565 isnt( exists( $suppliers{$id_supplier1} ),
566     1, "$today < $daysago10 and $today > $today-3" );
567 ok(
568     exists( $suppliers{$id_supplier2} ),
569 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago5  and $daysago5 < today - 3+2"
570 );
571 isnt(
572     exists( $suppliers{$id_supplier3} ),
573     1,
574 "Supplier2 has late orders but $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2 "
575 );
576 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
577
578 #Test with $estimateddeliverydateto  and $delay
579 #Case 10:With $estimateddeliverydateto = $daysago5 and $delay = 5
580 #    today > $daysago5 today > now() -5 -NOT LATE-
581 #    $daysago5 + 2 days > $daysago5  and $daysago5 > now() - 2+5 days -NOT LATE-
582 #    $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days -LATE-
583 #    quantityreceived = quantity -NOT LATE-
584 %suppliers =
585   C4::Bookseller::GetBooksellersWithLateOrders( 5, undef, $daysago5 );
586 isnt( exists( $suppliers{$id_supplier1} ),
587     1, "Supplier2 has late orders but today > $daysago5 today > now() -5" );
588 isnt(
589     exists( $suppliers{$id_supplier2} ),
590     1,
591 "Supplier2 has late orders but $daysago5 + 2 days > $daysago5  and $daysago5 > now() - 2+5 days"
592 );
593 ok(
594     exists( $suppliers{$id_supplier3} ),
595 "Supplier2 has late orders and $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days "
596 );
597 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
598
599 #Case 11: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 10
600 #    $daysago10==$daysago10==$daysago10 -NOT LATE-
601 #    $daysago10==$daysago10<$daysago5+2-NOT lATE-
602 #    $daysago10==$daysago10 <$daysago10+3-LATE-
603 #    quantityreceived = quantity -NOT LATE-
604
605 #Basket1 closedate -> $daysago10
606 $basket1info = {
607     basketno  => $sample_basket1,
608     closedate => $daysago10,
609 };
610 ModBasket($basket1info);
611 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
612     $daysago10 );
613 ok( exists( $suppliers{$id_supplier1} ),
614     "Supplier1 has late orders and $daysago10==$daysago10==$daysago10 " )
615   ;
616 isnt( exists( $suppliers{$id_supplier2} ),
617     1,
618     "Supplier2 has late orders but $daysago10==$daysago10<$daysago5+2" );
619 isnt( exists( $suppliers{$id_supplier3} ),
620     1,
621     "Supplier3 has late orders but $daysago10==$daysago10 <$daysago10+3" );
622 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
623
624 #Case 12: closedate == $estimateddeliverydatefrom =today-10
625 %suppliers =
626   C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10, undef );
627 ok( exists( $suppliers{$id_supplier1} ),
628     "Supplier1 has late orders and $daysago10==$daysago10 " );
629
630 #Case 13: closedate == $estimateddeliverydateto =today-10
631 %suppliers =
632   C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago10 );
633 ok( exists( $suppliers{$id_supplier1} ),
634     "Supplier1 has late orders and $daysago10==$daysago10 " )
635   ;
636
637 C4::Context->_new_userenv('DUMMY SESSION');
638 C4::Context::set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, '', '');
639 my $userenv = C4::Context->userenv;
640
641 my $module = Test::MockModule->new('C4::Auth');
642 $module->mock(
643     'haspermission',
644     sub {
645         # simulate user that has serials permissions but
646         # NOT superserials
647         my ($userid, $flagsrequired) = @_;
648         return 0 if 'superserials' eq ($flagsrequired->{serials} // 0);
649         return exists($flagsrequired->{serials});
650     }
651 );
652
653 C4::Context->set_preference('IndependentBranches', 0);
654 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
655 is(
656     scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
657     3,
658     'ordinary user can see all subscriptions with IndependentBranches off'
659 );
660
661 C4::Context->set_preference('IndependentBranches', 1);
662 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
663 is(
664     scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
665     1,
666     'ordinary user can see only their library\'s subscriptions with IndependentBranches on'
667 );
668
669 # don the cape and turn into Superlibrarian!
670 C4::Context::set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 1, '', '');
671 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
672 is(
673     scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
674     3,
675     'superlibrarian can see all subscriptions with IndependentBranches on (bug 12048)'
676 );
677 #Test contact editing
678 my $booksellerid = C4::Bookseller::AddBookseller(
679     {
680         name     => "my vendor",
681         address1 => "bookseller's address",
682         phone    => "0123456",
683         active   => 1
684     },
685     [
686         { name => 'John Smith',  phone => '0123456x1' },
687         { name => 'Leo Tolstoy', phone => '0123456x2' },
688     ]
689 );
690
691 @booksellers = C4::Bookseller::GetBookSeller('my vendor');
692 ok(
693     ( grep { $_->{'id'} == $booksellerid } @booksellers ),
694     'GetBookSeller returns correct record when passed a name'
695 );
696
697 my $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
698 is( $bookseller->{'id'}, $booksellerid, 'Retrieved desired record' );
699 is( $bookseller->{'phone'}, '0123456', 'New bookseller has expected phone' );
700 is( ref $bookseller->{'contacts'},
701     'ARRAY', 'GetBookSellerFromId returns arrayref of contacts' );
702 is(
703     ref $bookseller->{'contacts'}->[0],
704     'C4::Bookseller::Contact',
705     'First contact is a contact object'
706 );
707 is( $bookseller->{'contacts'}->[0]->phone,
708     '0123456x1', 'Contact has expected phone number' );
709 is( scalar @{ $bookseller->{'contacts'} }, 2, 'Saved two contacts' );
710
711 pop @{ $bookseller->{'contacts'} };
712 $bookseller->{'name'} = 'your vendor';
713 $bookseller->{'contacts'}->[0]->phone('654321');
714 C4::Bookseller::ModBookseller($bookseller);
715
716 $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
717 is( $bookseller->{'name'}, 'your vendor',
718     'Successfully changed name of vendor' );
719 is( $bookseller->{'contacts'}->[0]->phone,
720     '654321',
721     'Successfully changed contact phone number by modifying bookseller hash' );
722 is( scalar @{ $bookseller->{'contacts'} },
723     1, 'Only one contact after modification' );
724
725 C4::Bookseller::ModBookseller( $bookseller,
726     [ { name => 'John Jacob Jingleheimer Schmidt' } ] );
727
728 $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
729 is(
730     $bookseller->{'contacts'}->[0]->name,
731     'John Jacob Jingleheimer Schmidt',
732     'Changed name of contact'
733 );
734 is( $bookseller->{'contacts'}->[0]->phone,
735     undef, 'Removed phone number from contact' );
736 is( scalar @{ $bookseller->{'contacts'} },
737     1, 'Only one contact after modification' );
738
739 #End transaction
740 $dbh->rollback;
741
742 #field_filter filters the useless fields or foreign keys
743 #NOTE: all the fields of aqbookseller arent considered
744 #returns a cleaned structure
745 sub field_filter {
746     my ($struct) = @_;
747
748     for my $field (
749         'bookselleremail', 'booksellerfax',
750         'booksellerurl',   'othersupplier',
751         'currency',        'invoiceprice',
752         'listprice',       'contacts'
753       )
754     {
755
756         if ( grep { /^$field$/ } keys %$struct ) {
757             delete $struct->{$field};
758         }
759     }
760     return $struct;
761 }