View | Details | Raw Unified | Return to bug 40383
Collapse All | Expand All

(-)a/Koha/Edifact.pm (+115 lines)
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
(-)a/acqui/edimsg.pl (-4 / +33 lines)
Lines 20-30 use Modern::Perl; Link Here
20
20
21
use CGI;
21
use CGI;
22
use Koha::Database;
22
use Koha::Database;
23
use Koha::Edifact;
23
use C4::Koha;
24
use C4::Koha;
24
use C4::Auth   qw( get_template_and_user );
25
use C4::Auth   qw( get_template_and_user );
25
use C4::Output qw( output_html_with_http_headers );
26
use C4::Output qw( output_html_with_http_headers output_with_http_headers );
26
27
27
my $q = CGI->new;
28
my $q = CGI->new;
29
30
# Check if JSON output is requested
31
my $format        = $q->param('format')   || '';
32
my $accept_header = $ENV{HTTP_ACCEPT}     || '';
33
my $wants_json    = ( $format eq 'json' ) || ( $accept_header =~ m|application/json| );
34
28
my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
35
my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
29
    {
36
    {
30
        template_name => 'acqui/edimsg.tt',
37
        template_name => 'acqui/edimsg.tt',
Lines 33-49 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user( Link Here
33
        flagsrequired => { acquisition => 'edi_manage' },
40
        flagsrequired => { acquisition => 'edi_manage' },
34
    }
41
    }
35
);
42
);
43
36
my $msg_id = $q->param('id');
44
my $msg_id = $q->param('id');
37
my $schema = Koha::Database->new()->schema();
45
my $schema = Koha::Database->new()->schema();
38
46
39
my $msg = $schema->resultset('EdifactMessage')->find($msg_id);
47
my $msg = $schema->resultset('EdifactMessage')->find($msg_id);
48
40
if ($msg) {
49
if ($msg) {
41
    my $transmission = $msg->raw_msg;
50
    my $transmission = $msg->raw_msg;
42
51
43
    my @segments = segmentize($transmission);
52
    if ($wants_json) {
44
    $template->param( segments => \@segments );
53
54
        # Return JSON representation using Koha::Edifact
55
        my $edifact     = Koha::Edifact->new( { transmission => $transmission } );
56
        my $json_output = $edifact->to_json();
57
58
        output_with_http_headers( $q, $cookie, $json_output, 'json', '200 OK' );
59
        exit;
60
    } else {
61
62
        # Return HTML representation (existing behavior)
63
        my @segments = segmentize($transmission);
64
        $template->param(
65
            segments => \@segments,
66
            id       => $msg_id
67
        );
68
    }
45
} else {
69
} else {
46
    $template->param( no_message => 1 );
70
    if ($wants_json) {
71
        output_with_http_headers( $q, $cookie, '{"error":"Message not found"}', 'json', '404 Not Found' );
72
        exit;
73
    } else {
74
        $template->param( no_message => 1 );
75
    }
47
}
76
}
48
77
49
output_html_with_http_headers( $q, $cookie, $template->output );
78
output_html_with_http_headers( $q, $cookie, $template->output );
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/edimsg.tt (-1 / +7 lines)
Lines 32-37 Link Here
32
        [% IF no_message %]
32
        [% IF no_message %]
33
            <div class="alert alert-info">The requested message cannot be displayed</div>
33
            <div class="alert alert-info">The requested message cannot be displayed</div>
34
        [% ELSE %]
34
        [% ELSE %]
35
            <div class="row">
36
                <div class="col-sm-2">
37
                    <div id="toolbar" class="btn-toolbar">
38
                        <a class="btn btn-default btn-sm" href="[% "/cgi-bin/koha/acqui/edimsg.pl?id=" _ id _ "&format=json" %]" target="_blank"> <i class="fa fa-download"></i> JSON </a>
39
                    </div>
40
                </div>
41
            </div>
35
            <ul>
42
            <ul>
36
                [% FOREACH seg IN segments %]
43
                [% FOREACH seg IN segments %]
37
                    <li>[% seg | html %]</li>
44
                    <li>[% seg | html %]</li>
38
- 

Return to bug 40383