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