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