Bug 14778: Make 3 tests pass
[koha.git] / t / db_dependent / Acquisition.t
blob83486d256c11157cd48d8db190e690184c238680
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use POSIX qw(strftime);
22 use Test::More tests => 87;
23 use Koha::Database;
25 BEGIN {
26 use_ok('C4::Acquisition');
27 use_ok('C4::Bookseller');
28 use_ok('C4::Biblio');
29 use_ok('C4::Budgets');
30 use_ok('C4::Bookseller');
31 use_ok('Koha::Acquisition::Order');
32 use_ok('Koha::Acquisition::Bookseller');
35 # Sub used for testing C4::Acquisition subs returning order(s):
36 # GetOrdersByStatus, GetOrders, GetDeletedOrders, GetOrder etc.
37 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,$test_nbr_fields) =
38 # _check_fields_of_order ($exp_fields, $original_order_content, $order_to_check);
39 # params :
40 # $exp_fields : arrayref whose elements are the keys we expect to find
41 # $original_order_content : hashref whose 2 keys str and num contains hashrefs
42 # containing content fields of the order created with Koha::Acquisition::Order
43 # $order_to_check : hashref whose keys/values are the content of an order
44 # returned by the C4::Acquisition sub we are testing
45 # returns :
46 # \@test_missing_fields : arrayref void if ok ; otherwise contains the list of
47 # fields missing in $order_to_check
48 # \@test_extra_fields : arrayref void if ok ; otherwise contains the list of
49 # fields unexpected in $order_to_check
50 # \@test_different_fields : arrayref void if ok ; otherwise contains the list of
51 # fields which value is not the same in between $order_to_check and
52 # $test_nbr_fields : contains the number of fields of $order_to_check
54 sub _check_fields_of_order {
55 my ( $exp_fields, $original_order_content, $order_to_check ) = @_;
56 my @test_missing_fields = ();
57 my @test_extra_fields = ();
58 my @test_different_fields = ();
59 my $test_nbr_fields = scalar( keys %$order_to_check );
60 foreach my $field (@$exp_fields) {
61 push @test_missing_fields, $field
62 unless exists( $order_to_check->{$field} );
64 foreach my $field ( keys %$order_to_check ) {
65 push @test_extra_fields, $field
66 unless grep ( /^$field$/, @$exp_fields );
68 foreach my $field ( keys %{ $original_order_content->{str} } ) {
69 push @test_different_fields, $field
70 unless ( !exists $order_to_check->{$field} )
71 or ( $original_order_content->{str}->{$field} eq
72 $order_to_check->{$field} );
74 foreach my $field ( keys %{ $original_order_content->{num} } ) {
75 push @test_different_fields, $field
76 unless ( !exists $order_to_check->{$field} )
77 or ( $original_order_content->{num}->{$field} ==
78 $order_to_check->{$field} );
80 return (
81 \@test_missing_fields, \@test_extra_fields,
82 \@test_different_fields, $test_nbr_fields
86 # Sub used for testing C4::Acquisition subs returning several orders
87 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,\@test_nbr_fields) =
88 # _check_fields_of_orders ($exp_fields, $original_orders_content, $orders_to_check)
89 sub _check_fields_of_orders {
90 my ( $exp_fields, $original_orders_content, $orders_to_check ) = @_;
91 my @test_missing_fields = ();
92 my @test_extra_fields = ();
93 my @test_different_fields = ();
94 my @test_nbr_fields = ();
95 foreach my $order_to_check (@$orders_to_check) {
96 my $original_order_content =
97 ( grep { $_->{str}->{ordernumber} eq $order_to_check->{ordernumber} }
98 @$original_orders_content )[0];
99 my (
100 $t_missing_fields, $t_extra_fields,
101 $t_different_fields, $t_nbr_fields
103 = _check_fields_of_order( $exp_fields, $original_order_content,
104 $order_to_check );
105 push @test_missing_fields, @$t_missing_fields;
106 push @test_extra_fields, @$t_extra_fields;
107 push @test_different_fields, @$t_different_fields;
108 push @test_nbr_fields, $t_nbr_fields;
110 @test_missing_fields = keys %{ { map { $_ => 1 } @test_missing_fields } };
111 @test_extra_fields = keys %{ { map { $_ => 1 } @test_extra_fields } };
112 @test_different_fields =
113 keys %{ { map { $_ => 1 } @test_different_fields } };
114 return (
115 \@test_missing_fields, \@test_extra_fields,
116 \@test_different_fields, \@test_nbr_fields
121 my $schema = Koha::Database->new()->schema();
122 $schema->storage->txn_begin();
124 my $dbh = C4::Context->dbh;
125 $dbh->{RaiseError} = 1;
127 # Creating some orders
128 my $booksellerid = C4::Bookseller::AddBookseller(
130 name => "my vendor",
131 address1 => "bookseller's address",
132 phone => "0123456",
133 active => 1,
134 deliverytime => 5,
138 my $booksellerinfo = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
140 is( $booksellerinfo->{deliverytime},
141 5, 'set deliverytime when creating vendor (Bug 10556)' );
143 my ( $basket, $basketno );
145 $basketno = NewBasket( $booksellerid, 1 ),
146 "NewBasket( $booksellerid , 1 ) returns $basketno"
148 ok( $basket = GetBasket($basketno), "GetBasket($basketno) returns $basket" );
150 my $budgetid = C4::Budgets::AddBudget(
152 budget_code => "budget_code_test_getordersbybib",
153 budget_name => "budget_name_test_getordersbybib",
156 my $budget = C4::Budgets::GetBudget($budgetid);
158 my @ordernumbers;
159 my ( $biblionumber1, $biblioitemnumber1 ) = AddBiblio( MARC::Record->new, '' );
160 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( MARC::Record->new, '' );
161 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio( MARC::Record->new, '' );
162 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio( MARC::Record->new, '' );
164 # Prepare 5 orders, and make distinction beween fields to be tested with eq and with ==
165 # Ex : a price of 50.1 will be stored internally as 5.100000
167 my @order_content = (
169 str => {
170 basketno => $basketno,
171 biblionumber => $biblionumber1,
172 budget_id => $budget->{budget_id},
173 uncertainprice => 0,
174 order_internalnote => "internal note",
175 order_vendornote => "vendor note",
176 ordernumber => '',
178 num => {
179 quantity => 24,
180 listprice => 50.121111,
181 ecost => 38.15,
182 rrp => 40.15,
183 discount => 5.1111,
184 gstrate => 0.0515
188 str => {
189 basketno => $basketno,
190 biblionumber => $biblionumber2,
191 budget_id => $budget->{budget_id}
193 num => { quantity => 42 }
196 str => {
197 basketno => $basketno,
198 biblionumber => $biblionumber2,
199 budget_id => $budget->{budget_id},
200 uncertainprice => 0,
201 order_internalnote => "internal note",
202 order_vendornote => "vendor note"
204 num => {
205 quantity => 4,
206 ecost => 42.1,
207 rrp => 42.1,
208 listprice => 10.1,
209 ecost => 38.1,
210 rrp => 11.0,
211 discount => 5.1,
212 gstrate => 0.1
216 str => {
217 basketno => $basketno,
218 biblionumber => $biblionumber3,
219 budget_id => $budget->{budget_id},
220 order_internalnote => "internal note",
221 order_vendornote => "vendor note"
223 num => {
224 quantity => 4,
225 ecost => 40,
226 rrp => 42,
227 listprice => 10,
228 ecost => 38.15,
229 rrp => 11.00,
230 discount => 0,
231 uncertainprice => 0,
232 gstrate => 0
236 str => {
237 basketno => $basketno,
238 biblionumber => $biblionumber4,
239 budget_id => $budget->{budget_id},
240 order_internalnote => "internal note",
241 order_vendornote => "vendor note"
243 num => {
244 quantity => 1,
245 ecost => 10,
246 rrp => 10,
247 listprice => 10,
248 ecost => 10,
249 rrp => 10,
250 discount => 0,
251 uncertainprice => 0,
252 gstrate => 0
257 # Create 4 orders in database
258 for ( 0 .. 4 ) {
259 my %ocontent;
260 @ocontent{ keys %{ $order_content[$_]->{num} } } =
261 values %{ $order_content[$_]->{num} };
262 @ocontent{ keys %{ $order_content[$_]->{str} } } =
263 values %{ $order_content[$_]->{str} };
264 $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->insert->{ordernumber};
265 $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
268 # Test UT sub _check_fields_of_order
270 my (
271 $test_missing_fields, $test_extra_fields,
272 $test_different_fields, $test_nbr_fields
274 = _check_fields_of_order(
275 [qw /a b c d e/],
276 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
277 { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
281 ( $test_nbr_fields == 5 )
282 and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
283 and ( join( " ", sort @$test_extra_fields ) eq 'f g' )
284 and ( join( " ", @$test_different_fields ) eq 'a' )
286 "_check_fields_of_order can check an order (test 1)"
289 $test_missing_fields, $test_extra_fields,
290 $test_different_fields, $test_nbr_fields
292 = _check_fields_of_order(
293 [qw /a b c /],
294 { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
295 { a => "bla", b => "105", c => 15 }
299 ( $test_nbr_fields == 3 )
300 and ( scalar @$test_missing_fields == 0 )
301 and ( scalar @$test_extra_fields == 0 )
302 and ( scalar @$test_different_fields == 0 )
304 "_check_fields_of_order can check an order (test 2)"
307 $test_missing_fields, $test_extra_fields,
308 $test_different_fields, $test_nbr_fields
310 = _check_fields_of_order(
311 [qw /a b c d e/],
312 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
313 { a => "blabla", b => "105", c => 15, d => "error" }
317 ( $test_nbr_fields == 4 )
318 and ( join( " ", sort @$test_missing_fields ) eq 'e' )
319 and ( scalar @$test_extra_fields == 0 )
320 and ( join( " ", @$test_different_fields ) eq 'a c' )
322 "_check_fields_of_order can check an order (test 3)"
326 # test GetOrder
329 my @expectedfields = qw(
330 order_internalnote
331 order_vendornote
332 ordernumber
333 biblionumber
334 entrydate
335 quantity
336 currency
337 listprice
338 datereceived
339 invoiceid
340 freight
341 unitprice
342 quantityreceived
343 datecancellationprinted
344 purchaseordernumber
345 basketno
346 timestamp
348 ecost
349 unitpricesupplier
350 unitpricelib
351 gstrate
352 discount
353 budget_id
354 budgetgroup_id
355 budgetdate
356 sort1
357 sort2
358 sort1_authcat
359 sort2_authcat
360 uncertainprice
361 claims_count
362 claimed_date
363 subscriptionid
364 parent_ordernumber
365 orderstatus
366 title
367 author
368 basketname
369 branchcode
370 publicationyear
371 copyrightdate
372 editionstatement
373 isbn
375 seriestitle
376 publishercode
377 publisher
378 budget
379 supplier
380 supplierid
381 estimateddeliverydate
382 orderdate
383 quantity_to_receive
384 subtotal
385 latesince
386 cancellationreason
389 $test_missing_fields, $test_extra_fields,
390 $test_different_fields, $test_nbr_fields
392 = _check_fields_of_order( \@expectedfields, $order_content[0],
393 GetOrder( $ordernumbers[0] ) );
395 $test_nbr_fields,
396 scalar @expectedfields,
397 "GetOrder gets an order with the right number of fields"
399 is( join( " ", @$test_missing_fields ),
400 '', "GetOrder gets an order with no missing fields" );
401 is( join( " ", @$test_extra_fields ),
402 '', "GetOrder gets an order with no unexpected fields" );
403 is( join( " ", @$test_different_fields ),
404 '', "GetOrder gets an order with the right content in every fields" );
407 # Test GetOrders
410 my @base_expectedfields = qw(
411 order_internalnote
412 order_vendornote
413 notes
414 ordernumber
415 ecost
416 uncertainprice
417 marc
419 isbn
420 copyrightdate
421 serial
422 cn_suffix
423 cn_item
424 marcxml
425 freight
426 cn_class
427 title
428 pages
429 budget_encumb
430 budget_name
431 number
432 itemtype
433 totalissues
434 author
435 budget_permission
436 parent_ordernumber
437 size
438 claims_count
439 currency
440 seriestitle
441 timestamp
442 editionstatement
443 budget_parent_id
444 publishercode
445 unitprice
446 collectionvolume
447 budget_amount
448 budget_owner_id
449 datecreated
450 claimed_date
451 subscriptionid
452 editionresponsibility
453 sort2
454 volumedate
455 budget_id
456 illus
458 biblioitemnumber
459 datereceived
460 orderstatus
461 agerestriction
462 budget_branchcode
463 gstrate
464 listprice
465 budget_code
466 budgetdate
467 basketno
468 discount
469 abstract
470 collectionissn
471 publicationyear
472 collectiontitle
473 invoiceid
474 budgetgroup_id
475 place
476 issn
477 quantityreceived
478 entrydate
479 cn_source
480 sort1_authcat
481 budget_notes
482 biblionumber
483 unititle
484 sort2_authcat
485 budget_expend
487 cn_sort
488 lccn
489 sort1
490 volume
491 purchaseordernumber
492 quantity
493 budget_period_id
494 frameworkcode
495 volumedesc
496 datecancellationprinted
497 cancellationreason
499 @expectedfields =
500 ( @base_expectedfields,
501 ( 'transferred_from_timestamp', 'transferred_from' ) );
502 is( GetOrders(), undef, "GetOrders with no params returns undef" );
503 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
504 my @get_orders = GetOrders($basketno);
506 $test_missing_fields, $test_extra_fields,
507 $test_different_fields, $test_nbr_fields
509 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
511 $$test_nbr_fields[0],
512 scalar @expectedfields,
513 "GetOrders gets orders with the right number of fields"
515 is( join( " ", @$test_missing_fields ),
516 '', "GetOrders gets orders with no missing fields" );
517 is( join( " ", @$test_extra_fields ),
518 '', "GetOrders gets orders with no unexpected fields" );
519 is( join( " ", @$test_different_fields ),
520 '', "GetOrders gets orders with the right content in every fields" );
523 ( scalar @get_orders == 4 )
524 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
526 "GetOrders only gets non-cancelled orders"
530 # Test GetOrders { cancelled => 1 }
533 @expectedfields =
534 ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
535 @get_orders = GetOrders($basketno, { cancelled => 1 });
537 $test_missing_fields, $test_extra_fields,
538 $test_different_fields, $test_nbr_fields
540 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
542 $$test_nbr_fields[0],
543 scalar @expectedfields,
544 "GetOrders { cancelled => 1 } gets orders with the right number of fields"
546 is( join( " ", @$test_missing_fields ),
547 '', "GetOrders { cancelled => 1 } gets orders with no missing fields" );
548 is( join( " ", @$test_extra_fields ),
549 '', "GetOrders { cancelled => 1 } gets orders with no unexpected fields" );
550 is( join( " ", @$test_different_fields ),
552 "GetOrders { cancelled => 1 } gets orders with the right content in every fields" );
555 ( scalar @get_orders == 1 )
556 and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
558 "GetOrders { cancelled => 1 } only gets cancelled orders"
562 # Test SearchOrders
565 @expectedfields = qw (
566 order_internalnote
567 order_vendornote
568 notes
569 basketgroupid
570 basketgroupname
571 firstname
572 biblioitemnumber
573 ecost
574 uncertainprice
575 creationdate
576 datereceived
577 orderstatus
578 isbn
579 copyrightdate
580 gstrate
581 serial
582 listprice
583 budgetdate
584 basketno
585 discount
586 surname
587 freight
588 abstract
589 title
590 closedate
591 basketname
592 budgetgroup_id
593 invoiceid
594 author
595 parent_ordernumber
596 claims_count
597 entrydate
598 currency
599 quantityreceived
600 seriestitle
601 sort1_authcat
602 timestamp
603 biblionumber
604 unititle
605 sort2_authcat
607 unitprice
608 sort1
609 ordernumber
610 datecreated
611 purchaseordernumber
612 quantity
613 claimed_date
614 subscriptionid
615 frameworkcode
616 sort2
617 datecancellationprinted
618 budget_id
619 authorisedby
620 booksellerid
621 cancellationreason
624 # note that authorisedby was added to the return of SearchOrder by the
625 # patch for bug 11777
627 my $invoiceid = AddInvoice(
628 invoicenumber => 'invoice',
629 booksellerid => $booksellerid,
630 unknown => "unknown"
633 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
635 biblionumber => $biblionumber4,
636 ordernumber => $ordernumbers[4],
637 quantityreceived => 1,
638 cost => 10,
639 ecost => 10,
640 invoiceid => $invoiceid,
641 rrp => 10,
642 budget_id => $order_content[4]->{str}->{budget_id},
646 my $search_orders = SearchOrders({
647 booksellerid => $booksellerid,
648 basketno => $basketno
650 isa_ok( $search_orders, 'ARRAY' );
652 $test_missing_fields, $test_extra_fields,
653 $test_different_fields, $test_nbr_fields
655 = _check_fields_of_orders( \@expectedfields, \@order_content,
656 $search_orders );
658 $$test_nbr_fields[0],
659 scalar @expectedfields,
660 "SearchOrders gets orders with the right number of fields"
662 is( join( " ", @$test_missing_fields ),
663 '', "SearchOrders gets orders with no missing fields" );
664 is( join( " ", @$test_extra_fields ),
665 '', "SearchOrders gets orders with no unexpected fields" );
666 is( join( " ", @$test_different_fields ),
667 '', "SearchOrders gets orders with the right content in every fields" );
670 ( scalar @$search_orders == 4 )
671 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
673 "SearchOrders only gets non-cancelled orders"
676 $search_orders = SearchOrders({
677 booksellerid => $booksellerid,
678 basketno => $basketno,
679 pending => 1
683 ( scalar @$search_orders == 3 ) and !grep ( (
684 ( $_->{ordernumber} eq $ordernumbers[3] )
685 or ( $_->{ordernumber} eq $ordernumbers[4] )
687 @$search_orders )
689 "SearchOrders with pending params gets only pending orders (bug 10723)"
692 $search_orders = SearchOrders({
693 booksellerid => $booksellerid,
694 basketno => $basketno,
695 pending => 1,
696 ordered => 1,
698 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
700 $search_orders = SearchOrders({
701 ordernumber => $ordernumbers[4]
703 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
705 $search_orders = SearchOrders({
706 biblionumber => $biblionumber4
708 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
710 $search_orders = SearchOrders({
711 biblionumber => $biblionumber4,
712 pending => 1
714 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
717 # Test GetBudgetByOrderNumber
719 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
720 "GetBudgetByOrderNumber returns expected budget" );
723 # Test GetLateOrders
726 @expectedfields = qw (
727 orderdate
728 author
729 budget
730 supplierid
731 claims_count
732 supplier
733 publisher
734 ordernumber
735 quantity
736 basketno
737 claimed_date
738 branch
739 estimateddeliverydate
740 title
741 publicationyear
742 unitpricelib
743 unitpricesupplier
744 subtotal
745 latesince
746 basketname
747 basketgroupid
748 basketgroupname
750 my @lateorders = GetLateOrders(0);
751 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
752 0, "GetLateOrders does not get orders from opened baskets" );
753 C4::Acquisition::CloseBasket($basketno);
754 @lateorders = GetLateOrders(0);
755 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
756 0, "GetLateOrders gets orders from closed baskets" );
757 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
758 "GetLateOrders does not gets cancelled orders" );
759 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
760 "GetLateOrders does not gets reveived orders" );
762 $test_missing_fields, $test_extra_fields,
763 $test_different_fields, $test_nbr_fields
765 = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
767 $$test_nbr_fields[0],
768 scalar @expectedfields,
769 "GetLateOrders gets orders with the right number of fields"
771 is( join( " ", @$test_missing_fields ),
772 '', "GetLateOrders gets orders with no missing fields" );
773 is( join( " ", @$test_extra_fields ),
774 '', "GetLateOrders gets orders with no unexpected fields" );
775 is( join( " ", @$test_different_fields ),
776 '', "GetLateOrders gets orders with the right content in every fields" );
778 $search_orders = SearchOrders({
779 booksellerid => $booksellerid,
780 basketno => $basketno,
781 pending => 1,
782 ordered => 1,
784 is( scalar (@$search_orders), 3, "SearchOrders with pending and ordered params gets only pending ordered orders. After closing the basket, orders are marked as 'ordered' (bug 11170)" );
787 # Test AddClaim
790 my $order = $lateorders[0];
791 AddClaim( $order->{ordernumber} );
792 my $neworder = GetOrder( $order->{ordernumber} );
794 $neworder->{claimed_date},
795 strftime( "%Y-%m-%d", localtime(time) ),
796 "AddClaim : Check claimed_date"
799 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
801 biblionumber => $biblionumber2,
802 ordernumber => $ordernumbers[1],
803 quantityreceived => 2,
804 cost => 12,
805 ecost => 12,
806 invoiceid => $invoiceid,
807 rrp => 42,
808 order_internalnote => "my notes",
809 order_vendornote => "my vendor notes",
812 my $order2 = GetOrder( $ordernumbers[1] );
813 is( $order2->{'quantityreceived'},
814 0, 'Splitting up order did not receive any on original order' );
815 is( $order2->{'quantity'}, 40, '40 items on original order' );
816 is( $order2->{'budget_id'}, $budgetid,
817 'Budget on original order is unchanged' );
818 is( $order2->{order_internalnote}, "my notes",
819 'ModReceiveOrder and GetOrder deal with internal notes' );
820 is( $order2->{order_vendornote}, "my vendor notes",
821 'ModReceiveOrder and GetOrder deal with vendor notes' );
823 $neworder = GetOrder($new_ordernumber);
824 is( $neworder->{'quantity'}, 2, '2 items on new order' );
825 is( $neworder->{'quantityreceived'},
826 2, 'Splitting up order received items on new order' );
827 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
829 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
830 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
832 my $orders = GetHistory( ordernumber => $ordernumbers[1] );
833 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
834 $orders = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
835 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
837 my $budgetid2 = C4::Budgets::AddBudget(
839 budget_code => "budget_code_test_modrecv",
840 budget_name => "budget_name_test_modrecv",
844 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
846 biblionumber => $biblionumber2,
847 ordernumber => $ordernumbers[2],
848 quantityreceived => 2,
849 cost => 12,
850 ecost => 12,
851 invoiceid => $invoiceid,
852 rrp => 42,
853 budget_id => $budgetid2,
854 order_internalnote => "my other notes",
858 my $order3 = GetOrder( $ordernumbers[2] );
859 is( $order3->{'quantityreceived'},
860 0, 'Splitting up order did not receive any on original order' );
861 is( $order3->{'quantity'}, 2, '2 items on original order' );
862 is( $order3->{'budget_id'}, $budgetid,
863 'Budget on original order is unchanged' );
864 is( $order3->{order_internalnote}, "my other notes",
865 'ModReceiveOrder and GetOrder deal with notes' );
867 $neworder = GetOrder($new_ordernumber);
868 is( $neworder->{'quantity'}, 2, '2 items on new order' );
869 is( $neworder->{'quantityreceived'},
870 2, 'Splitting up order received items on new order' );
871 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
873 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
875 biblionumber => $biblionumber2,
876 ordernumber => $ordernumbers[2],
877 quantityreceived => 2,
878 cost => 12,
879 ecost => 12,
880 invoiceid => $invoiceid,
881 rrp => 42,
882 budget_id => $budgetid2,
883 order_internalnote => "my third notes",
887 $order3 = GetOrder( $ordernumbers[2] );
888 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
889 is( $order3->{'quantity'}, 2, '2 items on order' );
890 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
891 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
893 my $nonexistent_order = GetOrder();
894 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
895 $nonexistent_order = GetOrder( 424242424242 );
896 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
898 # Tests for DelOrder
899 my $order1 = GetOrder($ordernumbers[0]);
900 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
901 ok((not defined $error), "DelOrder does not fail");
902 $order1 = GetOrder($order1->{ordernumber});
903 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
904 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
905 ok((defined GetBiblio($order1->{biblionumber})), "biblio still exists");
907 $order2 = GetOrder($ordernumbers[1]);
908 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
909 ok((not defined $error), "DelOrder does not fail");
910 $order2 = GetOrder($order2->{ordernumber});
911 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
912 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
913 ok((not defined GetBiblio($order2->{biblionumber})), "biblio does not exist anymore");
915 my $order4 = GetOrder($ordernumbers[3]);
916 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
917 ok((not defined $error), "DelOrder does not fail");
918 $order4 = GetOrder($order4->{ordernumber});
919 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
920 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
921 ok((not defined GetBiblio($order4->{biblionumber})), "biblio does not exist anymore");
922 # End of tests for DelOrder
924 $schema->storage->txn_rollback();