Bug 18267: Refactored Edifact Price Calculations
[koha.git] / t / Edifact.t
blob2e5de55a2242ce9107f8fc11965cd0bd655ac7cb
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use FindBin qw( $Bin );
6 use Test::More tests => 40;
7 use Koha::EDI;
9 BEGIN { use_ok('Koha::Edifact') }
11 my $filename = "$Bin/edi_testfiles/prquotes_73050_20140430.CEQ";
13 my $quote = Koha::Edifact->new( { filename => $filename, } );
15 isa_ok( $quote, 'Koha::Edifact' );
17 my $x = $quote->interchange_header('sender');
18 is( $x, '5013546027856', "sender returned" );
20 $x = $quote->interchange_header('recipient');
21 is( $x, '5030670137480', "recipient returned" );
22 $x = $quote->interchange_header('datetime');
23 is( $x->[0], '140430', "datetime returned" );
24 my $control_reference = 'EDIQ2857763';
25 $x = $quote->interchange_header('interchange_control_reference');
26 is( $x, $control_reference, "interchange_control_reference returned" );
28 $x = $quote->interchange_header('application_reference');
29 is( $x, 'QUOTES', "application_reference returned" );
31 $x = $quote->interchange_trailer('interchange_control_count');
32 is( $x, 1, "interchange_control_count returned" );
34 my $msgs = $quote->message_array();
35 my $msg_count = @{$msgs};
36 is( $msg_count, 1, "correct message count returned" );
37 my $m = $msgs->[0];
39 is( $m->message_type, 'QUOTES', "Message shows correct type" );
40 is( $m->message_reference_number,
41 'MQ09791', "Message reference number returned" );
42 is( $m->docmsg_number, 'Q741588', "Message docmsg number returned" );
43 is( $m->message_date, '20140430', "Message date returned" );
44 is( $m->buyer_ean, '5030670137480', 'buyer ean returned');
46 my $lin = $m->lineitems();
48 my $num_lines = @{$lin};
49 is( $num_lines, 18, 'Correct number of lines in message' );
51 my $test_line = $lin->[-1];
53 is( $test_line->line_item_number, 18, 'correct line number returned' );
54 is( $test_line->item_number_id, '9780273761006', 'correct ean returned' );
55 is( $test_line->quantity, 1, 'quantity returned' );
56 is( $test_line->price_info, 114.97, 'price returned' );
57 is( $test_line->price_info_inclusive, undef, 'discounted price undefined as expected' );
59 my $test_title = 'International business [electronic resource]';
60 my $marcrec = $test_line->marc_record;
61 isa_ok( $marcrec, 'MARC::Record' );
63 my $title = $test_line->title();
65 # also tests components are concatenated
66 is( $title, $test_title, "Title returned" );
68 # problems currently with the record (needs leader ??)
69 #is( $marcrec->title(), $test_title, "Title returned from marc");
70 my $test_author = q{Rugman, Alan M.};
71 is( $test_line->author, $test_author, "Author returned" );
72 is( $test_line->publisher, 'Pearson Education', "Publisher returned" );
73 is( $test_line->publication_date, q{2012.}, "Pub. date returned" );
75 # Test data encoded in GIR
77 my $stock_category = $test_line->girfield('stock_category');
78 is( $stock_category, 'EBOOK', "stock_category returned" );
79 my $branch = $test_line->girfield('branch');
80 is( $branch, 'ELIB', "branch returned" );
81 my $fund_allocation = $test_line->girfield('fund_allocation');
82 is( $fund_allocation, '660BOO_2013', "fund_allocation returned" );
83 my $collection_code = $test_line->girfield('collection_code');
84 is( $collection_code, 'EBOO', "collection_code returned" );
86 #my $shelfmark = $test_line->girfield('shelfmark');
87 #my $classification = $test_line->girfield('classification');
89 ## text the free_text returned from the line
90 my $test_line_2 = $lin->[12];
92 my $ftx_string = 'E*610.72* - additional items';
94 is( $test_line_2->orderline_free_text, $ftx_string, "ftx note retrieved" );
96 my $filename2 = "$Bin/edi_testfiles/QUOTES_413514.CEQ";
98 my $q2 = Koha::Edifact->new( { filename => $filename2, } );
99 my $messages = $q2->message_array();
101 my $orderlines = $messages->[0]->lineitems();
103 my $ol = $orderlines->[0];
105 my $y = $ol->girfield( 'copy_value', 5 );
107 is( $y, undef, 'No extra item generated' );
109 $y = $ol->girfield( 'copy_value', 1 );
110 is( $y, '16.99', 'Copy Price returned' );
112 $y = $ol->girfield( 'classification', 4 );
113 is( $y, '914.1061', 'Copy classification returned' );
115 $y = $ol->girfield( 'fund_allocation', 4 );
116 is( $y, 'REF', 'Copy fund returned' );
118 $y = $ol->girfield( 'branch', 4 );
119 is( $y, 'SOU', 'Copy Branch returned' );
121 $y = $ol->girfield( 'collection_code', 4 );
122 is( $y, 'ANF', 'Collection code returned' );
124 $y = $ol->girfield( 'stock_category', 4 );
125 is( $y, 'RS', 'Copy stock category returned' );
127 # test internal routines for prices
128 my $dp = Koha::EDI::_discounted_price(33.0, 9);
129 is( $dp, 6.03, 'Discount calculated' );
131 $dp = Koha::EDI::_discounted_price(0.0, 9);
132 is( $dp, 9.0, 'Discount calculated with discount = 0' );
134 $dp = Koha::EDI::_discounted_price(0.0, 9, 8.0);
135 is( $dp, 8.0, 'Discount overriden by incoming calculated value');