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

(-)a/t/db_dependent/Koha/Edifact/Order.t (-7 / +172 lines)
Lines 20-28 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::NoWarnings;
22
use Test::NoWarnings;
23
use Test::More tests => 6;
23
use Test::More tests => 7;
24
24
25
use Koha::Edifact::Order;
25
use Koha::Edifact::Order;
26
use JSON qw( encode_json );
26
27
27
use t::lib::Mocks;
28
use t::lib::Mocks;
28
use t::lib::TestBuilder;
29
use t::lib::TestBuilder;
Lines 106-116 subtest 'order_line() tests' => sub { Link Here
106
        {
107
        {
107
            class => 'Koha::Acquisition::Orders',
108
            class => 'Koha::Acquisition::Orders',
108
            value => {
109
            value => {
109
                biblionumber     => $biblio->biblionumber,
110
                biblionumber          => $biblio->biblionumber,
110
                quantity         => 2,
111
                quantity              => 2,
111
                line_item_id     => 'EDILINEID1',
112
                line_item_id          => 'EDILINEID1',
112
                order_vendornote => 'A not so pretty note',
113
                order_vendornote      => 'A not so pretty note',
113
                listprice        => '1.50'
114
                listprice             => '1.50',
115
                servicing_instruction => undef
114
            }
116
            }
115
        }
117
        }
116
    );
118
    );
Lines 560-562 subtest 'gir_segments() with LSL and LSQ preferences' => sub { Link Here
560
562
561
    $schema->storage->txn_rollback;
563
    $schema->storage->txn_rollback;
562
};
564
};
563
- 
565
566
subtest 'gir_segments() with servicing instructions' => sub {
567
    plan tests => 18;
568
569
    $schema->storage->txn_begin;
570
571
    # Test 1: Single LVC instruction
572
    my $params_lvc = {
573
        ol_fields => {
574
            budget_code           => 'FUND123',
575
            servicing_instruction => encode_json( [ [ { type => 'LVC', value => 'BB' } ] ] )
576
        },
577
        items => [
578
            {
579
                branchcode     => 'BRANCH1',
580
                itype          => 'BOOK',
581
                itemcallnumber => 'CALL1',
582
            }
583
        ]
584
    };
585
586
    my @segments = Koha::Edifact::Order::gir_segments($params_lvc);
587
    ok( scalar @segments >= 1, 'Segment created with LVC servicing instruction' );
588
    like( $segments[0], qr/BB:LVC/, 'LVC servicing instruction included in GIR segment' );
589
590
    # Test 2: Single LVT instruction
591
    my $params_lvt = {
592
        ol_fields => {
593
            budget_code           => 'FUND123',
594
            servicing_instruction => encode_json( [ [ { type => 'LVT', value => 'Special handling required' } ] ] )
595
        },
596
        items => [
597
            {
598
                branchcode     => 'BRANCH1',
599
                itype          => 'BOOK',
600
                itemcallnumber => 'CALL1',
601
            }
602
        ]
603
    };
604
605
    @segments = Koha::Edifact::Order::gir_segments($params_lvt);
606
    ok( scalar @segments >= 1, 'Segment created with LVT servicing instruction' );
607
    like(
608
        $segments[0], qr/Special handling required:LVT/,
609
        'LVT servicing instruction included in GIR segment'
610
    );
611
612
    # Test 3: Multiple LVC instructions in one group
613
    my $params_multi_lvc = {
614
        ol_fields => {
615
            budget_code           => 'FUND123',
616
            servicing_instruction =>
617
                encode_json( [ [ { type => 'LVC', value => 'BB' }, { type => 'LVC', value => 'BI' } ] ] )
618
        },
619
        items => [
620
            {
621
                branchcode     => 'BRANCH1',
622
                itype          => 'BOOK',
623
                itemcallnumber => 'CALL1',
624
            }
625
        ]
626
    };
627
628
    @segments = Koha::Edifact::Order::gir_segments($params_multi_lvc);
629
    ok( scalar @segments >= 2, 'Multiple segments created when exceeding 5 elements' );
630
    like( $segments[0], qr/BB:LVC/, 'First LVC instruction in first segment' );
631
    like( $segments[1], qr/BI:LVC/, 'Second LVC instruction in second segment (overflow)' );
632
633
    # Test 4: Mixed LVC and LVT in one group
634
    my $params_mixed = {
635
        ol_fields => {
636
            budget_code           => 'FUND123',
637
            servicing_instruction => encode_json(
638
                [
639
                    [
640
                        { type => 'LVC', value => 'BB' },
641
                        { type => 'LVT', value => 'Apply on spine only' }
642
                    ]
643
                ]
644
            )
645
        },
646
        items => [
647
            {
648
                branchcode     => 'BRANCH1',
649
                itype          => 'BOOK',
650
                itemcallnumber => 'CALL1',
651
            }
652
        ]
653
    };
654
655
    @segments = Koha::Edifact::Order::gir_segments($params_mixed);
656
    ok( scalar @segments >= 2, 'Multiple segments created with mixed LVC/LVT instructions' );
657
    like( $segments[0], qr/BB:LVC/,                  'LVC instruction in first segment' );
658
    like( $segments[1], qr/Apply on spine only:LVT/, 'LVT instruction in second segment (overflow)' );
659
660
    # Test 5: Multiple groups
661
    my $params_multi_groups = {
662
        ol_fields => {
663
            budget_code           => 'FUND123',
664
            servicing_instruction => encode_json(
665
                [
666
                    [ { type => 'LVC', value => 'BB' }, { type => 'LVT', value => 'Spine label' } ],
667
                    [ { type => 'LVC', value => 'BI' } ],
668
                ]
669
            )
670
        },
671
        items => [
672
            {
673
                branchcode     => 'BRANCH1',
674
                itype          => 'BOOK',
675
                itemcallnumber => 'CALL1',
676
            }
677
        ]
678
    };
679
680
    @segments = Koha::Edifact::Order::gir_segments($params_multi_groups);
681
    ok( scalar @segments >= 2, 'Multiple segments created with multiple servicing groups' );
682
683
    # With 4 item fields + 3 servicing instructions = 7 elements, we'll have overflow
684
    like( $segments[0], qr/BB:LVC/,          'First group LVC in first segment' );
685
    like( $segments[1], qr/Spine label:LVT/, 'First group LVT in second segment (overflow)' );
686
    like( $segments[1], qr/BI:LVC/,          'Second group LVC in second segment' );
687
688
    # Test 6: Empty servicing instruction
689
    my $params_empty = {
690
        ol_fields => {
691
            budget_code           => 'FUND123',
692
            servicing_instruction => undef
693
        },
694
        items => [
695
            {
696
                branchcode     => 'BRANCH1',
697
                itype          => 'BOOK',
698
                itemcallnumber => 'CALL1',
699
            }
700
        ]
701
    };
702
703
    @segments = Koha::Edifact::Order::gir_segments($params_empty);
704
    ok( scalar @segments >= 1, 'Segment created without servicing instruction' );
705
    unlike( $segments[0], qr/:LVC/, 'No LVC instruction when servicing_instruction is empty' );
706
    unlike( $segments[0], qr/:LVT/, 'No LVT instruction when servicing_instruction is empty' );
707
708
    # Test 7: Invalid JSON (should log error but not crash)
709
    my $params_invalid = {
710
        ol_fields => {
711
            budget_code           => 'FUND123',
712
            servicing_instruction => 'not valid json'
713
        },
714
        items => [
715
            {
716
                branchcode     => 'BRANCH1',
717
                itype          => 'BOOK',
718
                itemcallnumber => 'CALL1',
719
            }
720
        ]
721
    };
722
723
    # Invalid JSON is logged to EDI log but doesn't crash
724
    @segments = Koha::Edifact::Order::gir_segments($params_invalid);
725
    ok( scalar @segments >= 1, 'Segment created even with invalid JSON (degrades gracefully)' );
726
727
    $schema->storage->txn_rollback;
728
};

Return to bug 30144