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