+# Create 4 orders in database
+for ( 0 .. 4 ) {
+ my %ocontent;
+ @ocontent{ keys %{ $order_content[$_]->{num} } } =
+ values %{ $order_content[$_]->{num} };
+ @ocontent{ keys %{ $order_content[$_]->{str} } } =
+ values %{ $order_content[$_]->{str} };
+ $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->insert->{ordernumber};
+ $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
+}
+
+# Test UT sub _check_fields_of_order
+
+my (
+ $test_missing_fields, $test_extra_fields,
+ $test_different_fields, $test_nbr_fields
+ )
+ = _check_fields_of_order(
+ [qw /a b c d e/],
+ { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
+ { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
+ );
+ok(
+ (
+ ( $test_nbr_fields == 5 )
+ and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
+ and ( join( " ", sort @$test_extra_fields ) eq 'f g' )
+ and ( join( " ", @$test_different_fields ) eq 'a' )
+ ),
+ "_check_fields_of_order can check an order (test 1)"
+);
+(
+ $test_missing_fields, $test_extra_fields,
+ $test_different_fields, $test_nbr_fields
+ )
+ = _check_fields_of_order(
+ [qw /a b c /],
+ { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
+ { a => "bla", b => "105", c => 15 }
+ );
+ok(
+ (
+ ( $test_nbr_fields == 3 )
+ and ( scalar @$test_missing_fields == 0 )
+ and ( scalar @$test_extra_fields == 0 )
+ and ( scalar @$test_different_fields == 0 )
+ ),
+ "_check_fields_of_order can check an order (test 2)"
+);
+(
+ $test_missing_fields, $test_extra_fields,
+ $test_different_fields, $test_nbr_fields
+ )
+ = _check_fields_of_order(
+ [qw /a b c d e/],
+ { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
+ { a => "blabla", b => "105", c => 15, d => "error" }
+ );
+ok(
+ (
+ ( $test_nbr_fields == 4 )
+ and ( join( " ", sort @$test_missing_fields ) eq 'e' )
+ and ( scalar @$test_extra_fields == 0 )
+ and ( join( " ", @$test_different_fields ) eq 'a c' )
+ ),
+ "_check_fields_of_order can check an order (test 3)"
+);
+
+#
+# test GetOrder
+#
+
+my @expectedfields = qw(
+ order_internalnote
+ order_vendornote
+ ordernumber
+ biblionumber
+ entrydate
+ quantity
+ currency
+ listprice
+ totalamount
+ datereceived
+ invoiceid
+ freight
+ unitprice
+ quantityreceived
+ datecancellationprinted
+ purchaseordernumber
+ basketno
+ timestamp
+ rrp
+ ecost
+ unitpricesupplier
+ unitpricelib
+ gstrate
+ discount
+ budget_id
+ budgetgroup_id
+ budgetdate
+ sort1
+ sort2
+ sort1_authcat
+ sort2_authcat
+ uncertainprice
+ claims_count
+ claimed_date
+ subscriptionid
+ parent_ordernumber
+ orderstatus
+ title
+ author
+ basketname
+ branchcode
+ publicationyear
+ copyrightdate
+ editionstatement
+ isbn
+ ean
+ seriestitle
+ publishercode
+ publisher
+ budget
+ supplier
+ supplierid
+ estimateddeliverydate
+ orderdate
+ quantity_to_receive
+ subtotal
+ latesince
+ cancellationreason
+);
+(
+ $test_missing_fields, $test_extra_fields,
+ $test_different_fields, $test_nbr_fields
+ )
+ = _check_fields_of_order( \@expectedfields, $order_content[0],
+ GetOrder( $ordernumbers[0] ) );
+is(
+ $test_nbr_fields,
+ scalar @expectedfields,
+ "GetOrder gets an order with the right number of fields"
+);
+is( join( " ", @$test_missing_fields ),
+ '', "GetOrder gets an order with no missing fields" );
+is( join( " ", @$test_extra_fields ),
+ '', "GetOrder gets an order with no unexpected fields" );
+is( join( " ", @$test_different_fields ),
+ '', "GetOrder gets an order with the right content in every fields" );
+
+#
+# Test GetOrders
+#
+
+my @base_expectedfields = qw(
+ order_internalnote
+ order_vendornote
+ notes
+ ordernumber
+ ecost
+ uncertainprice
+ marc
+ url
+ isbn
+ copyrightdate
+ serial
+ cn_suffix
+ cn_item
+ marcxml
+ freight
+ cn_class
+ title
+ pages
+ budget_encumb
+ budget_name
+ number
+ itemtype
+ totalissues
+ author
+ budget_permission
+ parent_ordernumber
+ size
+ claims_count
+ currency
+ seriestitle
+ timestamp
+ editionstatement
+ budget_parent_id
+ publishercode
+ unitprice
+ collectionvolume
+ budget_amount
+ budget_owner_id
+ datecreated
+ claimed_date
+ subscriptionid
+ editionresponsibility
+ sort2
+ volumedate
+ budget_id
+ illus
+ ean
+ biblioitemnumber
+ datereceived
+ orderstatus
+ agerestriction
+ budget_branchcode
+ gstrate
+ listprice
+ budget_code
+ budgetdate
+ basketno
+ discount
+ abstract
+ collectionissn
+ publicationyear
+ collectiontitle
+ invoiceid
+ budgetgroup_id
+ place
+ issn
+ quantityreceived
+ entrydate
+ cn_source
+ sort1_authcat
+ budget_notes
+ biblionumber
+ unititle
+ sort2_authcat
+ budget_expend
+ rrp
+ cn_sort
+ totalamount
+ lccn
+ sort1
+ volume
+ purchaseordernumber
+ quantity
+ budget_period_id
+ frameworkcode
+ volumedesc
+ datecancellationprinted
+ cancellationreason
+);
+@expectedfields =
+ ( @base_expectedfields,
+ ( 'transferred_from_timestamp', 'transferred_from' ) );
+is( GetOrders(), undef, "GetOrders with no params returns undef" );
+DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
+my @get_orders = GetOrders($basketno);
+(
+ $test_missing_fields, $test_extra_fields,
+ $test_different_fields, $test_nbr_fields
+ )
+ = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
+is(
+ $$test_nbr_fields[0],
+ scalar @expectedfields,
+ "GetOrders gets orders with the right number of fields"
+);
+is( join( " ", @$test_missing_fields ),
+ '', "GetOrders gets orders with no missing fields" );
+is( join( " ", @$test_extra_fields ),
+ '', "GetOrders gets orders with no unexpected fields" );
+is( join( " ", @$test_different_fields ),
+ '', "GetOrders gets orders with the right content in every fields" );
+ok(
+ (
+ ( scalar @get_orders == 4 )
+ and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
+ ),
+ "GetOrders only gets non-cancelled orders"
+);
+
+#
+# Test GetCancelledOrders
+#
+
+@expectedfields =
+ ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
+is( GetCancelledOrders(), undef,
+ "GetCancelledOrders with no params returns undef" );
+@get_orders = GetCancelledOrders($basketno);
+(
+ $test_missing_fields, $test_extra_fields,
+ $test_different_fields, $test_nbr_fields
+ )
+ = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
+is(
+ $$test_nbr_fields[0],
+ scalar @expectedfields,
+ "GetCancelledOrders gets orders with the right number of fields"
+);
+is( join( " ", @$test_missing_fields ),
+ '', "GetCancelledOrders gets orders with no missing fields" );
+is( join( " ", @$test_extra_fields ),
+ '', "GetCancelledOrders gets orders with no unexpected fields" );
+is( join( " ", @$test_different_fields ),
+ '',
+ "GetCancelledOrders gets orders with the right content in every fields" );
+ok(
+ (
+ ( scalar @get_orders == 1 )
+ and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
+ ),
+ "GetCancelledOrders only gets cancelled orders"
+);
+
+#
+# Test SearchOrders
+#
+
+@expectedfields = qw (
+ order_internalnote
+ order_vendornote
+ notes
+ basketgroupid
+ basketgroupname
+ firstname
+ biblioitemnumber
+ ecost
+ uncertainprice
+ creationdate
+ datereceived
+ orderstatus
+ isbn
+ copyrightdate
+ gstrate
+ serial
+ listprice
+ budgetdate
+ basketno
+ discount
+ surname
+ freight
+ abstract
+ title
+ closedate
+ basketname
+ budgetgroup_id
+ invoiceid
+ author
+ parent_ordernumber
+ claims_count
+ entrydate
+ currency
+ quantityreceived
+ seriestitle
+ sort1_authcat
+ timestamp
+ biblionumber
+ unititle
+ sort2_authcat
+ rrp
+ unitprice
+ totalamount
+ sort1
+ ordernumber
+ datecreated
+ purchaseordernumber
+ quantity
+ claimed_date
+ subscriptionid
+ frameworkcode
+ sort2
+ datecancellationprinted
+ budget_id
+ authorisedby
+ booksellerid
+ cancellationreason
+);
+
+# note that authorisedby was added to the return of SearchOrder by the
+# patch for bug 11777
+
+my $invoiceid = AddInvoice(
+ invoicenumber => 'invoice',
+ booksellerid => $booksellerid,
+ unknown => "unknown"