Bug 17917: [QA Follow-up] Reprove Search.t
[koha.git] / t / db_dependent / Bookseller.t
blob77cf3efc07761713524dcca24f3f3d5e0ac41765
1 #!/usr/bin/perl
3 use Modern::Perl;
5 use Test::More tests => 86;
6 use Test::MockModule;
7 use Test::Warn;
9 use C4::Context;
10 use Koha::DateUtils;
11 use DateTime::Duration;
12 use t::lib::Mocks;
13 use C4::Acquisition;
14 use C4::Serials;
15 use C4::Budgets;
16 use C4::Biblio;
18 use Koha::Acquisition::Booksellers;
19 use Koha::Acquisition::Order;
20 use Koha::Database;
22 BEGIN {
23 use_ok('C4::Bookseller');
26 can_ok(
28 'C4::Bookseller', qw(
29 GetBooksellersWithLateOrders
33 #Start transaction
34 my $dbh = C4::Context->dbh;
35 my $database = Koha::Database->new();
36 my $schema = $database->schema();
37 $schema->storage->txn_begin();
39 $dbh->{RaiseError} = 1;
41 #Start tests
42 $dbh->do(q|DELETE FROM aqorders|);
43 $dbh->do(q|DELETE FROM aqbasket|);
44 $dbh->do(q|DELETE FROM aqbooksellers|);
45 $dbh->do(q|DELETE FROM subscription|);
47 #Test AddBookseller
48 my $count = Koha::Acquisition::Booksellers->search()->count();
49 my $sample_supplier1 = {
50 name => 'Name1',
51 address1 => 'address1_1',
52 address2 => 'address1-2',
53 address3 => 'address1_2',
54 address4 => 'address1_2',
55 postal => 'postal1',
56 phone => 'phone1',
57 accountnumber => 'accountnumber1',
58 fax => 'fax1',
59 url => 'url1',
60 active => 1,
61 gstreg => 1,
62 listincgst => 1,
63 invoiceincgst => 1,
64 tax_rate => '1.0000',
65 discount => '1.0000',
66 notes => 'notes1',
67 deliverytime => undef
69 my $sample_supplier2 = {
70 name => 'Name2',
71 address1 => 'address1_2',
72 address2 => 'address2-2',
73 address3 => 'address3_2',
74 address4 => 'address4_2',
75 postal => 'postal2',
76 phone => 'phone2',
77 accountnumber => 'accountnumber2',
78 fax => 'fax2',
79 url => 'url2',
80 active => 1,
81 gstreg => 1,
82 listincgst => 1,
83 invoiceincgst => 1,
84 tax_rate => '2.0000',
85 discount => '2.0000',
86 notes => 'notes2',
87 deliverytime => 2
90 my $supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
91 my $id_supplier1 = $supplier1->id;
92 my $supplier2 = Koha::Acquisition::Bookseller->new($sample_supplier2)->store;
93 my $id_supplier2 = $supplier2->id;
95 like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
96 like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
97 is( Koha::Acquisition::Booksellers->search()->count,
98 $count + 2, "Supplier1 and Supplier2 have been added" );
100 #Test DelBookseller
101 my $del = $supplier1->delete;
102 is( $del, 1, "DelBookseller returns 1 - 1 supplier has been deleted " );
103 my $b = Koha::Acquisition::Booksellers->find( $id_supplier1 );
104 is( $b,
105 undef, "Supplier1 has been deleted - id_supplier1 $id_supplier1 doesnt exist anymore" );
107 #Test get bookseller
108 my @bookseller2 = Koha::Acquisition::Booksellers->search({name => $sample_supplier2->{name} });
109 is( scalar(@bookseller2), 1, "Get only Supplier2" );
110 for my $bookseller ( @bookseller2 ) {
111 $bookseller = field_filter( $bookseller->unblessed );
114 $sample_supplier2->{id} = $id_supplier2;
115 is_deeply( $bookseller2[0], $sample_supplier2,
116 "Koha::Acquisition::Booksellers->search returns the right informations about supplier $sample_supplier2->{name}" );
118 $supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
119 $id_supplier1 = $supplier1->id;
120 my @booksellers = Koha::Acquisition::Booksellers->search();
121 for my $bookseller ( @booksellers ) {
122 $bookseller = field_filter( $bookseller->unblessed );
125 $sample_supplier1->{id} = $id_supplier1;
126 is( scalar(@booksellers), $count + 2, "Get Supplier1 and Supplier2" );
127 my @tab = ( $sample_supplier1, $sample_supplier2 );
128 is_deeply( \@booksellers, \@tab,
129 "Returns right fields of Supplier1 and Supplier2" );
131 #Test baskets
132 my @bookseller1 = Koha::Acquisition::Booksellers->search({name => $sample_supplier1->{name} });
133 is( $bookseller1[0]->baskets->count, 0, 'Supplier1 has 0 basket' );
134 my $basketno1 =
135 C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby1', 'basketname1' );
136 my $basketno2 =
137 C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby2', 'basketname2' );
138 @bookseller1 = Koha::Acquisition::Booksellers->search({ name => $sample_supplier1->{name} });
139 is( $bookseller1[0]->baskets->count, 2, 'Supplier1 has 2 baskets' );
141 #Test Koha::Acquisition::Bookseller->new using id
142 my $bookseller1fromid = Koha::Acquisition::Booksellers->find;
143 is( $bookseller1fromid, undef,
144 "find returns undef if no id given" );
145 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
146 $bookseller1fromid = field_filter($bookseller1fromid->unblessed);
147 is_deeply( $bookseller1fromid, $sample_supplier1,
148 "Get Supplier1 (find a bookseller by id)" );
150 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
151 is( $bookseller1fromid->baskets->count, 2, 'Supplier1 has 2 baskets' );
153 #Test subscriptions
154 my $dt_today = dt_from_string;
155 my $today = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
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 });
162 my $budgetperiod = C4::Budgets::AddBudgetPeriod({
163 budget_period_startdate => $daysago5,
164 budget_period_enddate => $today,
165 budget_period_description => "budget desc"
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
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'),
179 my ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
180 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
181 is( $bookseller1fromid->subscriptions->count,
182 0, 'Supplier1 has 0 subscription' );
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
193 my @subscriptions = SearchSubscriptions({biblionumber => $biblionumber});
194 is($subscriptions[0]->{publicnotes}, 'subscription notes', 'subscription search results include public notes (bug 10689)');
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
205 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
206 is( $bookseller1fromid->subscriptions->count,
207 2, 'Supplier1 has 2 subscriptions' );
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 tax_rate => '2.0000 ',
227 discount => '2.0000',
228 notes => 'notes2 modified',
229 deliverytime => 2,
232 my $modif1 = Koha::Acquisition::Booksellers->find($id_supplier2)->set($sample_supplier2)->store;
233 is( ref $modif1, 'Koha::Acquisition::Bookseller', "ModBookseller has updated the bookseller" );
234 is( Koha::Acquisition::Booksellers->search->count,
235 $count + 2, "Supplier2 has been modified - Nothing added" );
236 $supplier2 = Koha::Acquisition::Booksellers->find($id_supplier2);
237 is( $supplier2->name, 'Name2 modified', "supplier's name should have been modified" );
239 #Test GetBooksellersWithLateOrders
240 #Add 2 suppliers
241 my $sample_supplier3 = {
242 name => 'Name3',
243 address1 => 'address1_3',
244 address2 => 'address1-3',
245 address3 => 'address1_3',
246 address4 => 'address1_3',
247 postal => 'postal3',
248 phone => 'phone3',
249 accountnumber => 'accountnumber3',
250 fax => 'fax3',
251 url => 'url3',
252 active => 1,
253 gstreg => 1,
254 listincgst => 1,
255 invoiceincgst => 1,
256 tax_rate => '3.0000',
257 discount => '3.0000',
258 notes => 'notes3',
259 deliverytime => 3
261 my $sample_supplier4 = {
262 name => 'Name4',
263 address1 => 'address1_4',
264 address2 => 'address1-4',
265 address3 => 'address1_4',
266 address4 => 'address1_4',
267 postal => 'postal4',
268 phone => 'phone4',
269 accountnumber => 'accountnumber4',
270 fax => 'fax4',
271 url => 'url4',
272 active => 1,
273 gstreg => 1,
274 listincgst => 1,
275 invoiceincgst => 1,
276 tax_rate => '3.0000',
277 discount => '3.0000',
278 notes => 'notes3',
280 my $supplier3 = Koha::Acquisition::Bookseller->new($sample_supplier3)->store;
281 my $id_supplier3 = $supplier3->id;
282 my $supplier4 = Koha::Acquisition::Bookseller->new($sample_supplier4)->store;
283 my $id_supplier4 = $supplier4->id;
285 #Add 2 baskets
286 my $basketno3 =
287 C4::Acquisition::NewBasket( $id_supplier3, 'authorisedby3', 'basketname3',
288 'basketnote3' );
289 my $basketno4 =
290 C4::Acquisition::NewBasket( $id_supplier4, 'authorisedby4', 'basketname4',
291 'basketnote4' );
293 #Modify the basket to add a close date
294 my $basket1info = {
295 basketno => $basketno1,
296 closedate => $today,
297 booksellerid => $id_supplier1
300 my $basket2info = {
301 basketno => $basketno2,
302 closedate => $daysago5,
303 booksellerid => $id_supplier2
306 my $dt_today2 = dt_from_string;
307 my $dur10 = DateTime::Duration->new( days => -10 );
308 $dt_today2->add_duration($dur10);
309 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
310 my $basket3info = {
311 basketno => $basketno3,
312 closedate => $daysago10,
315 my $basket4info = {
316 basketno => $basketno4,
317 closedate => $today,
319 ModBasket($basket1info);
320 ModBasket($basket2info);
321 ModBasket($basket3info);
322 ModBasket($basket4info);
324 #Add 1 subscription
325 my $id_subscription3 = NewSubscription(
326 undef, "BRANCH1", $id_supplier1, undef, $id_budget, $biblionumber,
327 '01-01-2013',undef, undef, undef, undef,
328 undef, undef, undef, undef, undef, undef,
329 1, "subscription notes",undef, '01-01-2013', undef, undef,
330 undef, undef, 0, "intnotes", 0,
331 undef, undef, 0, 'LOCA', '2013-12-31', 0
334 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
335 is(scalar(@subscriptions), 3, 'search for subscriptions by expiration date');
336 @subscriptions = SearchSubscriptions({expiration_date => '2013-08-15'});
337 is(scalar(@subscriptions), 1, 'search for subscriptions by expiration date');
338 @subscriptions = SearchSubscriptions({callnumber => 'CALL'});
339 is(scalar(@subscriptions), 2, 'search for subscriptions by call number');
340 @subscriptions = SearchSubscriptions({callnumber => 'DEF'});
341 is(scalar(@subscriptions), 1, 'search for subscriptions by call number');
342 @subscriptions = SearchSubscriptions({location => 'LOCA'});
343 is(scalar(@subscriptions), 1, 'search for subscriptions by location');
345 #Add 4 orders
346 my $order1 = Koha::Acquisition::Order->new(
348 basketno => $basketno1,
349 quantity => 24,
350 biblionumber => $biblionumber,
351 budget_id => $id_budget,
352 entrydate => '01-01-2013',
353 currency => 'EUR',
354 notes => "This is a note1",
355 tax_rate => 0.0500,
356 orderstatus => 1,
357 subscriptionid => $id_subscription1,
358 quantityreceived => 2,
359 rrp => 10,
360 ecost => 10,
361 datereceived => '01-06-2013'
363 )->insert;
364 my $ordernumber1 = $order1->{ordernumber};
366 my $order2 = Koha::Acquisition::Order->new(
368 basketno => $basketno2,
369 quantity => 20,
370 biblionumber => $biblionumber,
371 budget_id => $id_budget,
372 entrydate => '01-01-2013',
373 currency => 'EUR',
374 notes => "This is a note2",
375 tax_rate => 0.0500,
376 orderstatus => 1,
377 subscriptionid => $id_subscription2,
378 rrp => 10,
379 ecost => 10,
381 )->insert;
382 my $ordernumber2 = $order2->{ordernumber};
384 my $order3 = Koha::Acquisition::Order->new(
386 basketno => $basketno3,
387 quantity => 20,
388 biblionumber => $biblionumber,
389 budget_id => $id_budget,
390 entrydate => '02-02-2013',
391 currency => 'EUR',
392 notes => "This is a note3",
393 tax_rate => 0.0500,
394 orderstatus => 2,
395 subscriptionid => $id_subscription3,
396 rrp => 11,
397 ecost => 11,
399 )->insert;
400 my $ordernumber3 = $order3->{ordernumber};
402 my $order4 = Koha::Acquisition::Order->new(
404 basketno => $basketno4,
405 quantity => 20,
406 biblionumber => $biblionumber,
407 budget_id => $id_budget,
408 entrydate => '02-02-2013',
409 currency => 'EUR',
410 notes => "This is a note3",
411 tax_rate => 0.0500,
412 orderstatus => 2,
413 subscriptionid => $id_subscription3,
414 rrp => 11,
415 ecost => 11,
416 quantityreceived => 20
418 )->insert;
419 my $ordernumber4 = $order4->{ordernumber};
421 #Test cases:
422 # Sample datas :
423 # Supplier1: delivery -> undef Basket1 : closedate -> today
424 # Supplier2: delivery -> 2 Basket2 : closedate -> $daysago5
425 # Supplier3: delivery -> 3 Basket3 : closedate -> $daysago10
426 #Case 1 : Without parameters:
427 # quantityreceived < quantity AND rrp <> 0 AND ecost <> 0 AND quantity - COALESCE(quantityreceived,0) <> 0 AND closedate IS NOT NULL -LATE-
428 # datereceived !null 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 # quantityreceived = quantity -NOT LATE-
431 my %suppliers = C4::Bookseller::GetBooksellersWithLateOrders();
432 ok( exists( $suppliers{$id_supplier1} ), "Supplier1 has late orders" );
433 ok( exists( $suppliers{$id_supplier2} ), "Supplier2 has late orders" );
434 ok( exists( $suppliers{$id_supplier3} ), "Supplier3 has late orders" );
435 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" )
436 ; # Quantity = quantityreceived
438 #Case 2: With $delay = 4
439 # today + 0 > now-$delay -NOT LATE-
440 # (today-5) + 2 <= now() - $delay -NOT LATE-
441 # (today-10) + 3 <= now() - $delay -LATE-
442 # quantityreceived = quantity -NOT LATE-
443 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 4, undef, undef );
444 isnt( exists( $suppliers{$id_supplier1} ),
445 1, "Supplier1 has late orders but today > now() - 4 days" );
446 isnt( exists( $suppliers{$id_supplier2} ),
447 1, "Supplier2 has late orders and $daysago5 <= now() - (4 days+2)" );
448 ok( exists( $suppliers{$id_supplier3} ),
449 "Supplier3 has late orders and $daysago10 <= now() - (4 days+3)" );
450 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
452 #Case 3: With $delay = -1
453 my $bslo;
454 warning_like
455 { $bslo = C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ) }
456 qr/^WARNING: GetBooksellerWithLateOrders is called with a negative value/,
457 "GetBooksellerWithLateOrders prints a warning on negative values";
459 is( $bslo, undef, "-1 is a wrong value for a delay" );
461 #Case 4: With $delay = 0
462 # today == now-0 -LATE- (if no deliverytime or deliverytime == 0)
463 # today-5 <= now() - $delay+2 -LATE-
464 # today-10 <= now() - $delay+3 -LATE-
465 # quantityreceived = quantity -NOT LATE-
466 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( 0, undef, undef );
468 ok( exists( $suppliers{$id_supplier1} ),
469 "Supplier1 has late orders but $today == now() - 0 days" )
471 ok( exists( $suppliers{$id_supplier2} ),
472 "Supplier2 has late orders and $daysago5 <= now() - 2" );
473 ok( exists( $suppliers{$id_supplier3} ),
474 "Supplier3 has late orders and $daysago10 <= now() - 3" );
475 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
477 #Case 5 : With $estimateddeliverydatefrom = today-4
478 # today >= today-4 -NOT LATE-
479 # (today-5)+ 2 days >= today-4 -LATE-
480 # (today-10) + 3 days < today-4 -NOT LATE-
481 # quantityreceived = quantity -NOT LATE-
482 my $dt_today3 = dt_from_string;
483 my $dur4 = DateTime::Duration->new( days => -4 );
484 $dt_today3->add_duration($dur4);
485 my $daysago4 = output_pref({ dt => $dt_today3, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
486 %suppliers =
487 C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago4, undef );
489 ok( exists( $suppliers{$id_supplier1} ),
490 "Supplier1 has late orders and $today >= $daysago4 -deliverytime undef" );
491 ok( exists( $suppliers{$id_supplier2} ),
492 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago4 " );
493 isnt( exists( $suppliers{$id_supplier3} ),
494 1, "Supplier3 has late orders and $daysago10 + 5 days < $daysago4 " );
495 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
497 #Case 6: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 5
498 # $daysago10<$daysago5<today -NOT LATE-
499 # $daysago10<$daysago5<$daysago5 +2 -NOT lATE-
500 # $daysago10<$daysago10 +3 <$daysago5 -LATE-
501 # quantityreceived = quantity -NOT LATE-
502 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
503 $daysago5 );
504 isnt( exists( $suppliers{$id_supplier1} ),
505 1, "Supplier1 has late orders but $daysago10 < $daysago5 < $today" );
506 isnt(
507 exists( $suppliers{$id_supplier2} ),
509 "Supplier2 has late orders but $daysago10 < $daysago5 < $daysago5+2"
512 exists( $suppliers{$id_supplier3} ),
513 "Supplier3 has late orders and $daysago10 <= $daysago10 +3 <= $daysago5"
515 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
517 #Case 7: With $estimateddeliverydateto = today-5
518 # $today >= $daysago5 -NOT LATE-
519 # $daysago5 + 2 days > $daysago5 -NOT LATE-
520 # $daysago10 + 3 <+ $daysago5 -LATE-
521 # quantityreceived = quantity -NOT LATE-
522 %suppliers =
523 C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago5 );
524 isnt( exists( $suppliers{$id_supplier1} ),
526 "Supplier1 has late orders but $today >= $daysago5 - deliverytime undef" );
527 isnt( exists( $suppliers{$id_supplier2} ),
528 1, "Supplier2 has late orders but $daysago5 + 2 days > $daysago5 " );
529 ok( exists( $suppliers{$id_supplier3} ),
530 "Supplier3 has late orders and $daysago10 + 3 <= $daysago5" );
531 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
533 #Test with $estimateddeliverydatefrom and $estimateddeliverydateto and $delay
534 #Case 8 :With $estimateddeliverydatefrom = 2013-07-05 and $estimateddeliverydateto = 2013-07-08 and $delay =5
535 # $daysago4<today<=$today and $today<now()-3 -NOT LATE-
536 # $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days -LATE-
537 # $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days -NOT LATE-
538 # quantityreceived = quantity -NOT LATE-
539 %suppliers =
540 C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago4, $today );
541 isnt(
542 exists( $suppliers{$id_supplier1} ),
544 "Supplier1 has late orders but $daysago4<today<=$today and $today<now()-3"
547 exists( $suppliers{$id_supplier2} ),
548 "Supplier2 has late orders and $daysago4 < $daysago5 + 2days <= today and $daysago5 <= now()-3+2 days"
550 isnt(
551 exists( $suppliers{$id_supplier3} ),
552 "Supplier3 has late orders but $daysago4 > $daysago10 + 3days < today and $daysago10 <= now()-3+3 days"
554 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
556 #Case 9 :With $estimateddeliverydatefrom = $daysago5 and $delay = 3
557 # $today < $daysago5 and $today > $today-5 -NOT LATE-
558 # $daysago5 + 2 days >= $daysago5 and $daysago5 < today - 3+2 -LATE-
559 # $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2-NOT LATE-
560 # quantityreceived = quantity -NOT LATE-
561 %suppliers =
562 C4::Bookseller::GetBooksellersWithLateOrders( 3, $daysago5, undef );
563 isnt( exists( $suppliers{$id_supplier1} ),
564 1, "$today < $daysago10 and $today > $today-3" );
566 exists( $suppliers{$id_supplier2} ),
567 "Supplier2 has late orders and $daysago5 + 2 days >= $daysago5 and $daysago5 < today - 3+2"
569 isnt(
570 exists( $suppliers{$id_supplier3} ),
572 "Supplier2 has late orders but $daysago10 + 3 days < $daysago5 and $daysago10 < today -3+2 "
574 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
576 #Test with $estimateddeliverydateto and $delay
577 #Case 10:With $estimateddeliverydateto = $daysago5 and $delay = 5
578 # today > $daysago5 today > now() -5 -NOT LATE-
579 # $daysago5 + 2 days > $daysago5 and $daysago5 > now() - 2+5 days -NOT LATE-
580 # $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days -LATE-
581 # quantityreceived = quantity -NOT LATE-
582 %suppliers =
583 C4::Bookseller::GetBooksellersWithLateOrders( 5, undef, $daysago5 );
584 isnt( exists( $suppliers{$id_supplier1} ),
585 1, "Supplier2 has late orders but today > $daysago5 today > now() -5" );
586 isnt(
587 exists( $suppliers{$id_supplier2} ),
589 "Supplier2 has late orders but $daysago5 + 2 days > $daysago5 and $daysago5 > now() - 2+5 days"
592 exists( $suppliers{$id_supplier3} ),
593 "Supplier2 has late orders and $daysago10 + 3 days <= $daysago5 and $daysago10 <= now() - 3+5 days "
595 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
597 #Case 11: With $estimateddeliverydatefrom =today-10 and $estimateddeliverydateto = today - 10
598 # $daysago10==$daysago10==$daysago10 -NOT LATE-
599 # $daysago10==$daysago10<$daysago5+2-NOT lATE-
600 # $daysago10==$daysago10 <$daysago10+3-LATE-
601 # quantityreceived = quantity -NOT LATE-
603 #Basket1 closedate -> $daysago10
604 $basket1info = {
605 basketno => $basketno1,
606 closedate => $daysago10,
608 ModBasket($basket1info);
609 %suppliers = C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10,
610 $daysago10 );
611 ok( exists( $suppliers{$id_supplier1} ),
612 "Supplier1 has late orders and $daysago10==$daysago10==$daysago10 " )
614 isnt( exists( $suppliers{$id_supplier2} ),
616 "Supplier2 has late orders but $daysago10==$daysago10<$daysago5+2" );
617 isnt( exists( $suppliers{$id_supplier3} ),
619 "Supplier3 has late orders but $daysago10==$daysago10 <$daysago10+3" );
620 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
622 #Case 12: closedate == $estimateddeliverydatefrom =today-10
623 %suppliers =
624 C4::Bookseller::GetBooksellersWithLateOrders( undef, $daysago10, undef );
625 ok( exists( $suppliers{$id_supplier1} ),
626 "Supplier1 has late orders and $daysago10==$daysago10 " );
628 #Case 13: closedate == $estimateddeliverydateto =today-10
629 %suppliers =
630 C4::Bookseller::GetBooksellersWithLateOrders( undef, undef, $daysago10 );
631 ok( exists( $suppliers{$id_supplier1} ),
632 "Supplier1 has late orders and $daysago10==$daysago10 " )
635 C4::Context->_new_userenv('DUMMY SESSION');
636 C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, '', '');
637 my $userenv = C4::Context->userenv;
639 my $module = Test::MockModule->new('C4::Auth');
640 $module->mock(
641 'haspermission',
642 sub {
643 # simulate user that has serials permissions but
644 # NOT superserials
645 my ($userid, $flagsrequired) = @_;
646 return 0 if 'superserials' eq ($flagsrequired->{serials} // 0);
647 return exists($flagsrequired->{serials});
651 t::lib::Mocks::mock_preference('IndependentBranches', 0);
652 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
654 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
656 'ordinary user can see all subscriptions with IndependentBranches off'
659 t::lib::Mocks::mock_preference('IndependentBranches', 1);
660 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
662 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
664 'ordinary user can see only their library\'s subscriptions with IndependentBranches on'
667 # don the cape and turn into Superlibrarian!
668 C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 1, '', '');
669 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
671 scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
673 'superlibrarian can see all subscriptions with IndependentBranches on (bug 12048)'
676 #Test contact editing
677 my $sample_supplier = {
678 name => "my vendor",
679 address1 => "bookseller's address",
680 phone => "0123456",
681 active => 1
683 my $supplier = Koha::Acquisition::Bookseller->new($sample_supplier)->store;
684 my $booksellerid = $supplier->id;
685 my $contact1 = Koha::Acquisition::Bookseller::Contact->new({
686 name => 'John Smith',
687 phone => '0123456x1',
688 booksellerid => $booksellerid,
689 })->store;
690 my $contact2 = Koha::Acquisition::Bookseller::Contact->new({
691 name => 'Leo Tolstoy',
692 phone => '0123456x2',
693 booksellerid => $booksellerid,
694 })->store;
696 @booksellers = Koha::Acquisition::Booksellers->search({ name => 'my vendor' });
698 ( grep { $_->id == $booksellerid } @booksellers ),
699 'Koha::Acquisition::Booksellers->search returns correct record when passed a name'
702 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
703 is( $bookseller->id, $booksellerid, 'Retrieved desired record' );
704 is( $bookseller->phone, '0123456', 'New bookseller has expected phone' );
705 my $contacts = $bookseller->contacts;
706 is( $contacts->count,
707 2, 'bookseller should have 2 contacts' );
708 my $first_contact = $contacts->next;
710 ref $first_contact,
711 'Koha::Acquisition::Bookseller::Contact',
712 'First contact is a AqContact'
714 is( $first_contact->phone,
715 '0123456x1', 'Contact has expected phone number' );
717 my $second_contact = $contacts->next;
718 $second_contact->delete;
719 $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
720 $bookseller->name('your vendor')->store;
721 $contacts = $bookseller->contacts;
722 $first_contact = $contacts->next;
723 $first_contact->phone('654321');
724 $first_contact->store;
726 $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
727 is( $bookseller->name, 'your vendor',
728 'Successfully changed name of vendor' );
729 $contacts = $bookseller->contacts;
730 is( $contacts->count,
731 1, 'Only one contact after modification' );
732 $first_contact = $contacts->next;
733 is( $first_contact->phone,
734 '654321',
735 'Successfully changed contact phone number by modifying bookseller hash' );
737 $first_contact->name( 'John Jacob Jingleheimer Schmidt' );
738 $first_contact->phone(undef);
739 $first_contact->store;
741 $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
742 $contacts = $bookseller->contacts;
743 $first_contact = $contacts->next;
745 $first_contact->name,
746 'John Jacob Jingleheimer Schmidt',
747 'Changed name of contact'
749 is( $first_contact->phone,
750 undef, 'Removed phone number from contact' );
751 is( $contacts->count,
752 1, 'Only one contact after modification' );
754 #End transaction
755 $schema->storage->txn_rollback();
757 #field_filter filters the useless fields or foreign keys
758 #NOTE: all the fields of aqbookseller arent considered
759 #returns a cleaned structure
760 sub field_filter {
761 my ($struct) = @_;
763 for my $field (
764 'bookselleremail', 'booksellerfax',
765 'booksellerurl', 'othersupplier',
766 'currency', 'invoiceprice',
767 'listprice', 'contacts'
771 if ( grep { /^$field$/ } keys %$struct ) {
772 delete $struct->{$field};
775 return $struct;