Lines 21-26
use strict;
Link Here
|
21 |
use warnings; |
21 |
use warnings; |
22 |
use File::Slurp qw( read_file ); |
22 |
use File::Slurp qw( read_file ); |
23 |
use Carp qw( carp croak ); |
23 |
use Carp qw( carp croak ); |
|
|
24 |
use JSON qw( encode_json ); |
24 |
use Koha::Edifact::Segment; |
25 |
use Koha::Edifact::Segment; |
25 |
use Koha::Edifact::Message; |
26 |
use Koha::Edifact::Message; |
26 |
|
27 |
|
Lines 172-177
sub message_array {
Link Here
|
172 |
return $msg_arr; |
173 |
return $msg_arr; |
173 |
} |
174 |
} |
174 |
|
175 |
|
|
|
176 |
sub to_json { |
177 |
my $self = shift; |
178 |
|
179 |
# Find interchange header (UNB) |
180 |
my $header_seg; |
181 |
my $trailer_seg; |
182 |
my @all_segments = @{ $self->{transmission} }; |
183 |
|
184 |
# Get header and trailer |
185 |
for my $i ( 0 .. $#all_segments ) { |
186 |
if ( $all_segments[$i]->tag eq 'UNB' ) { |
187 |
$header_seg = $all_segments[$i]; |
188 |
} elsif ( $all_segments[$i]->tag eq 'UNZ' ) { |
189 |
$trailer_seg = $all_segments[$i]; |
190 |
} |
191 |
} |
192 |
|
193 |
# Build messages array |
194 |
my @json_messages; |
195 |
my $current_message; |
196 |
my @current_segments; |
197 |
my $current_line_id; |
198 |
|
199 |
for my $segment (@all_segments) { |
200 |
my $tag = $segment->tag; |
201 |
|
202 |
if ( $tag eq 'UNH' ) { |
203 |
|
204 |
# Start new message |
205 |
if ($current_message) { |
206 |
$current_message->{segments} = [@current_segments]; |
207 |
push @json_messages, $current_message; |
208 |
} |
209 |
$current_message = { |
210 |
header => $segment->as_string, |
211 |
segments => [], |
212 |
}; |
213 |
@current_segments = (); |
214 |
$current_line_id = undef; |
215 |
} elsif ( $tag eq 'UNT' ) { |
216 |
|
217 |
# End current message |
218 |
if ($current_message) { |
219 |
$current_message->{trailer} = $segment->as_string; |
220 |
$current_message->{segments} = [@current_segments]; |
221 |
push @json_messages, $current_message; |
222 |
} |
223 |
$current_message = undef; |
224 |
@current_segments = (); |
225 |
$current_line_id = undef; |
226 |
} elsif ( $tag eq 'UNB' || $tag eq 'UNZ' ) { |
227 |
|
228 |
# Skip - these are handled separately |
229 |
next; |
230 |
} elsif ($current_message) { |
231 |
|
232 |
# Process segment within message |
233 |
my $segment_data = { |
234 |
tag => $tag, |
235 |
raw => $segment->as_string, |
236 |
elements => [], |
237 |
}; |
238 |
|
239 |
# Extract elements |
240 |
my $elem_count = 0; |
241 |
while ( defined( my $elem = $segment->elem($elem_count) ) ) { |
242 |
push @{ $segment_data->{elements} }, $elem; |
243 |
$elem_count++; |
244 |
} |
245 |
|
246 |
# Handle line_id assignment |
247 |
if ( $tag eq 'LIN' ) { |
248 |
$current_line_id = $segment->elem(0); |
249 |
$segment_data->{line_id} = $current_line_id if defined $current_line_id; |
250 |
} elsif ( $tag eq 'UNS' ) { |
251 |
|
252 |
# UNS (Section Control) marks transition to message summary |
253 |
# Reset line_id so subsequent segments are treated as message-level |
254 |
$current_line_id = undef; |
255 |
} elsif ( defined $current_line_id && $tag !~ /^(UNH|UNT|UNS|CNT)$/ ) { |
256 |
|
257 |
# Assign line_id to segments that belong to current line item |
258 |
# Exclude message control segments (UNH, UNT, UNS, CNT) |
259 |
# After UNS, current_line_id is undefined, so segments are message-level |
260 |
$segment_data->{line_id} = $current_line_id; |
261 |
} |
262 |
|
263 |
push @current_segments, $segment_data; |
264 |
} |
265 |
} |
266 |
|
267 |
# Handle any remaining message |
268 |
if ($current_message) { |
269 |
$current_message->{segments} = [@current_segments]; |
270 |
push @json_messages, $current_message; |
271 |
} |
272 |
|
273 |
# Build final structure |
274 |
my $json_structure = { |
275 |
header => $header_seg ? $header_seg->as_string : undef, |
276 |
messages => \@json_messages, |
277 |
trailer => $trailer_seg ? $trailer_seg->as_string : undef, |
278 |
}; |
279 |
|
280 |
return encode_json($json_structure); |
281 |
} |
282 |
|
175 |
# |
283 |
# |
176 |
# internal parsing routines used in _init |
284 |
# internal parsing routines used in _init |
177 |
# |
285 |
# |
Lines 291-296
Edifact - Edifact message handler
Link Here
|
291 |
|
399 |
|
292 |
return an array of Message objects contained in the Edifact transmission |
400 |
return an array of Message objects contained in the Edifact transmission |
293 |
|
401 |
|
|
|
402 |
=head2 to_json |
403 |
|
404 |
returns a JSON representation of the complete edifact interchange. |
405 |
The JSON structure includes the interchange header, all messages with their |
406 |
segments (both raw and parsed), and the interchange trailer. |
407 |
Line items are grouped using line_id extracted from LIN segments. |
408 |
|
294 |
=head1 Internal Methods |
409 |
=head1 Internal Methods |
295 |
|
410 |
|
296 |
=head2 _init |
411 |
=head2 _init |