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