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