Bug 19234: Add query parameters handling helpers
[koha.git] / Koha / Edifact / Message.pm
blob96eb680d2e3bca3f5b9d40509f09c6770308336c
1 package Koha::Edifact::Message;
3 # Copyright 2014,2015 PTFS-Europe Ltd
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use strict;
21 use warnings;
22 use utf8;
24 use Koha::Edifact::Line;
26 sub new {
27 my ( $class, $data_array_ref ) = @_;
28 my $header = shift @{$data_array_ref};
29 my $bgm = shift @{$data_array_ref};
30 my $msg_function = $bgm->elem(2);
31 my $dtm = [];
32 while ( $data_array_ref->[0]->tag eq 'DTM' ) {
33 push @{$dtm}, shift @{$data_array_ref};
36 my $self = {
37 function => $msg_function,
38 header => $header,
39 bgm => $bgm,
40 message_reference_number => $header->elem(0),
41 dtm => $dtm,
42 datasegs => $data_array_ref,
45 bless $self, $class;
46 return $self;
49 sub message_refno {
50 my $self = shift;
51 return $self->{message_reference_number};
54 sub function {
55 my $self = shift;
56 my $msg_function = $self->{bgm}->elem(2);
57 if ( $msg_function == 9 ) {
58 return 'original';
60 elsif ( $msg_function == 7 ) {
61 return 'retransmission';
63 return;
66 sub message_reference_number {
67 my $self = shift;
68 return $self->{header}->elem(0);
71 sub message_type {
72 my $self = shift;
73 return $self->{header}->elem( 1, 0 );
76 sub message_code {
77 my $self = shift;
78 return $self->{bgm}->elem( 0, 0 );
81 sub docmsg_number {
82 my $self = shift;
83 return $self->{bgm}->elem(1);
86 sub message_date {
87 my $self = shift;
89 # usually the first if not only dtm
90 foreach my $d ( @{ $self->{dtm} } ) {
91 if ( $d->elem( 0, 0 ) eq '137' ) {
92 return $d->elem( 0, 1 );
95 return; # this should not happen
98 sub tax_point_date {
99 my $self = shift;
100 if ( $self->message_type eq 'INVOIC' ) {
101 foreach my $d ( @{ $self->{dtm} } ) {
102 if ( $d->elem( 0, 0 ) eq '131' ) {
103 return $d->elem( 0, 1 );
107 return;
110 sub expiry_date {
111 my $self = shift;
112 if ( $self->message_type eq 'QUOTES' ) {
113 foreach my $d ( @{ $self->{dtm} } ) {
114 if ( $d->elem( 0, 0 ) eq '36' ) {
115 return $d->elem( 0, 1 );
119 return;
122 sub shipment_charge {
123 my $self = shift;
125 # A large number of different charges can be expressed at invoice and
126 # item level but the only one koha takes cognizance of is shipment
127 # should we wrap all invoice level charges into it??
128 if ( $self->message_type eq 'INVOIC' ) {
129 my $delivery = 0;
130 my $amt = 0;
131 foreach my $s ( @{ $self->{datasegs} } ) {
132 if ( $s->tag eq 'LIN' ) {
133 last;
135 if ( $s->tag eq 'ALC' ) {
136 if ( $s->elem(0) eq 'C' ) { # Its a charge
137 if ( $s->elem( 4, 0 ) eq 'DL' ) { # delivery charge
138 $delivery = 1;
141 next;
143 if ( $s->tag eq 'MOA' ) {
144 $amt += $s->elem( 0, 1 );
147 return $amt;
149 return;
152 # return NAD fields
154 sub buyer_ean {
155 my $self = shift;
156 foreach my $s ( @{ $self->{datasegs} } ) {
157 if ( $s->tag eq 'LIN' ) {
158 last;
160 if ( $s->tag eq 'NAD' ) {
161 my $qualifier = $s->elem(0);
162 if ( $qualifier eq 'BY' ) {
163 return $s->elem( 1, 0 );
167 return;
170 sub supplier_ean {
171 my $self = shift;
172 foreach my $s ( @{ $self->{datasegs} } ) {
173 if ( $s->tag eq 'LIN' ) {
174 last;
176 if ( $s->tag eq 'NAD' ) {
177 my $qualifier = $s->elem(0);
178 if ( $qualifier eq 'SU' ) {
179 return $s->elem( 1, 0 );
183 return;
187 sub lineitems {
188 my $self = shift;
189 if ( $self->{quotation_lines} ) {
190 return $self->{quotation_lines};
192 else {
193 my $items = [];
194 my $item_arr = [];
195 foreach my $seg ( @{ $self->{datasegs} } ) {
196 my $tag = $seg->tag;
197 if ( $tag eq 'LIN' ) {
198 if ( @{$item_arr} ) {
199 push @{$items}, Koha::Edifact::Line->new($item_arr);
201 $item_arr = [$seg];
202 next;
204 elsif ( $tag =~ m/^(UNS|CNT|UNT)$/sxm ) {
205 if ( @{$item_arr} ) {
206 push @{$items}, Koha::Edifact::Line->new($item_arr);
208 last;
210 else {
211 if ( @{$item_arr} ) {
212 push @{$item_arr}, $seg;
216 $self->{quotation_lines} = $items;
217 return $items;
222 __END__
224 =head1 NAME
226 Koha::Edifact::Message
228 =head1 DESCRIPTION
230 Class modelling an Edifact Message for parsing
232 =head1 METHODS
234 =head2 new
236 Passed an array of segments extracts message level info
237 and parses lineitems as Line objects
239 =head1 AUTHOR
241 Colin Campbell <colin.campbell@ptfs-europe.com>
243 =head1 COPYRIGHT
245 Copyright 2014,2015 PTFS-Europe Ltd
246 This program is free software, You may redistribute it under
247 under the terms of the GNU General Public License
249 =cut