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