Bug 28210: Unit test
[koha.git] / t / EdiInvoice.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use FindBin qw( $Bin );
5
6 use Test::More tests => 26;
7 use Koha::EDI;
8
9 BEGIN { use_ok('Koha::Edifact') }
10
11 my $invoice_file = "$Bin/edi_testfiles/BLSINV337023.CEI";
12
13 my $invoice = Koha::Edifact->new( { filename => $invoice_file, } );
14
15 isa_ok( $invoice, 'Koha::Edifact' );
16 my $x                 = $invoice->interchange_header('sender');
17 my $control_reference = '337023';
18 is( $x, '5013546025078', "sender returned" );
19
20 $x = $invoice->interchange_header('recipient');
21 is( $x, '5013546121974', "recipient returned" );
22 $x = $invoice->interchange_header('datetime');
23 is( $x->[0], '140729', "datetime returned" );
24 $x = $invoice->interchange_header('interchange_control_reference');
25 is( $x, $control_reference, "interchange_control_reference returned" );
26
27 $x = $invoice->interchange_header('application_reference');
28 is( $x, 'INVOIC', "application_reference returned" );
29 $x = $invoice->interchange_trailer('interchange_control_count');
30 is( $x, 6, "interchange_control_count returned" );
31
32 my $messages = $invoice->message_array();
33
34 # check inv number from BGM
35
36 my $msg_count = @{$messages};
37 is( $msg_count, 6, 'correct message count returned' );
38
39 is( $messages->[0]->message_type, 'INVOIC', 'Message shows correct type' );
40
41 my $expected_date = '20140729';
42 is( $messages->[0]->message_date,
43     $expected_date, 'Message date correctly returned' );
44 is( $messages->[0]->tax_point_date,
45     $expected_date, 'Tax point date correctly returned' );
46
47 my $expected_invoicenumber = '01975490';
48
49 my $invoicenumber = $messages->[1]->docmsg_number();
50
51 is( $messages->[0]->buyer_ean,    '5013546121974', 'Buyer ean correct' );
52 is( $messages->[0]->supplier_ean, '5013546025078', 'Supplier ean correct' );
53
54 is( $invoicenumber, $expected_invoicenumber,
55     'correct invoicenumber extracted' );
56
57 my $lines = $messages->[1]->lineitems();
58
59 my $num_lines = @{$lines};
60
61 is( $num_lines, 8, "Correct number of lineitems returned" );
62
63 # sample invoice was from an early version where order was formatted basketno/ordernumber
64 my $expected_ordernumber = '2818/74593';
65
66 my $ordernumber = $lines->[7]->ordernumber;
67
68 is( $ordernumber, $expected_ordernumber, 'correct ordernumber returned' );
69
70 my $lineprice = $lines->[7]->price_net;
71
72 is( $lineprice, 4.55, 'correct net line price returned' );
73
74 $lineprice = $lines->[7]->price_gross;
75
76 is( $lineprice, 7.99, 'correct gross line price returned' );
77
78 my $tax = $lines->[7]->tax;
79
80 is( $tax, 0, 'correct tax amount returned' );
81
82 my $tax_rate = $lines->[7]->tax_rate;
83
84 is( $tax_rate->{rate}, 0.0, 'correct tax rate returned' );
85
86 my $tax_on_charge = $lines->[7]->amt_taxoncharge;
87
88 is( $tax_on_charge, 0, 'correct tax on charge value returned' );
89
90 my $qty_invoiced = $lines->[7]->quantity_invoiced;
91
92 is( $qty_invoiced, 1, 'quantity_invoiced returns correct value' );
93
94 my ($lt, $excl) = Koha::EDI::_get_invoiced_price($lines->[7], 1);
95 is( $lt, 4.55, 'invoiced price calculated');
96 is($excl, 4.55, 'Price excluding tax returned correctly');
97
98 ($lt, $excl) = Koha::EDI::_get_invoiced_price($lines->[7], 2);
99 is( $lt, 4.55 / 2, 'invoiced pricei calculated for copies > 1');