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