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