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

(-)a/Koha/MarcOrder.pm (-16 / +63 lines)
Lines 236-243 sub _create_basket_for_file { Link Here
236
    my $filename  = $args->{filename};
236
    my $filename  = $args->{filename};
237
    my $vendor_id = $args->{vendor_id};
237
    my $vendor_id = $args->{vendor_id};
238
238
239
    my $basket_name = _check_file_for_basket_name($args);
240
239
    # aqbasketname.basketname has a max length of 50 characters so long file names will need to be truncated
241
    # aqbasketname.basketname has a max length of 50 characters so long file names will need to be truncated
240
    my $basketname = length($filename) > 50 ? substr( $filename, 0, 50 ) : $filename;
242
    my $basketname = length($basket_name) > 50 ? substr( $basket_name, 0, 50 ) : $basket_name;
241
243
242
    my $basketno = NewBasket(
244
    my $basketno = NewBasket(
243
        $vendor_id, 0, $basketname, q{},
245
        $vendor_id, 0, $basketname, q{},
Lines 543-565 sub add_items_from_import_record { Link Here
543
sub match_file_to_account {
545
sub match_file_to_account {
544
    my ( $self, $args ) = @_;
546
    my ( $self, $args ) = @_;
545
547
546
    my $match    = 0;
548
    my $profile = $args->{profile};
547
    my $filename = $args->{filename};
548
    my $filepath = $args->{filepath};
549
    my $profile  = $args->{profile};
550
    my $format   = index( $filename, '.mrc' ) != -1 ? 'ISO2709' : 'MARCXML';
551
549
552
    my ( $errors, $marcrecords );
550
    my $match        = 0;
553
    if ( $format eq 'MARCXML' ) {
551
    my $match_record = _retrieve_first_record_from_batch($args);
554
        ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $profile->encoding );
555
    } elsif ( $format eq 'ISO2709' ) {
556
        ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File(
557
            $filepath, $profile->record_type,
558
            $profile->encoding
559
        );
560
    }
561
552
562
    my $match_record = @{$marcrecords}[0];
563
    my ( $field, $subfield ) = split /\$/, $profile->match_field;
553
    my ( $field, $subfield ) = split /\$/, $profile->match_field;
564
554
565
    my $field_value = $match_record->subfield( $field, $subfield ) || '';
555
    my $field_value = $match_record->subfield( $field, $subfield ) || '';
Lines 572-577 sub match_file_to_account { Link Here
572
    return $match;
562
    return $match;
573
}
563
}
574
564
565
=head3 _check_file_for_basket_name
566
567
    my $basket_name = _check_file_for_basket_name({
568
        filename => $filename,
569
        filepath => $filepath,
570
        profile  => $profile
571
    });
572
573
    Checks to see if the account has a basket name field assigned.
574
    If yes, it retrieves this value to use as the name.
575
    If not, to uses the filename.
576
577
=cut
578
579
sub _check_file_for_basket_name {
580
    my ( $self, $args ) = @_;
581
582
    my $profile  = $args->{profile};
583
    my $filename = $args->{filename};
584
    return $filename if !$profile->basket_name_field;
585
586
    my $first_record = _retrieve_first_record_from_batch($args);
587
588
    my ( $field, $subfield ) = split /\$/, $profile->basket_name_field;
589
590
    my $field_value = $first_record->subfield( $field, $subfield ) || '';
591
592
    return $field_value ? $field_value : $filename;
593
}
594
575
=head3 import_batches_list
595
=head3 import_batches_list
576
596
577
Fetches import batches matching the batch to be added to the basket and returns these to the template
597
Fetches import batches matching the batch to be added to the basket and returns these to the template
Lines 1251-1254 sub _create_item_fields_from_syspref { Link Here
1251
    return $item_fields;
1271
    return $item_fields;
1252
}
1272
}
1253
1273
1274
=head3 _retrieve_first_record_from_batch
1275
1276
=cut
1277
1278
sub _retrieve_first_record_from_batch {
1279
    my ($args) = @_;
1280
1281
    my $filename = $args->{filename};
1282
    my $filepath = $args->{filepath};
1283
    my $profile  = $args->{profile};
1284
    my $format   = index( $filename, '.mrc' ) != -1 ? 'ISO2709' : 'MARCXML';
1285
1286
    my ( $errors, $marcrecords );
1287
    if ( $format eq 'MARCXML' ) {
1288
        ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $profile->encoding );
1289
    } elsif ( $format eq 'ISO2709' ) {
1290
        ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File(
1291
            $filepath, $profile->record_type,
1292
            $profile->encoding
1293
        );
1294
    }
1295
1296
    my $first_record = @{$marcrecords}[0];
1297
    return $first_record;
1298
1299
}
1300
1254
1;
1301
1;
(-)a/t/db_dependent/Koha/MarcOrder.t (-2 / +56 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 5;
20
use Test::More tests => 6;
21
21
22
use Koha::MarcOrder;
22
use Koha::MarcOrder;
23
use Koha::MarcOrderAccount;
23
use Koha::MarcOrderAccount;
Lines 627-629 subtest 'match_file_to_account' => sub { Link Here
627
627
628
    $schema->storage->txn_rollback;
628
    $schema->storage->txn_rollback;
629
};
629
};
630
- 
630
631
subtest '_check_file_for_basket_name' => sub {
632
    plan tests => 2;
633
634
    $schema->storage->txn_begin;
635
636
    my ( $fh, $name ) = tempfile( SUFFIX => '.marcxml' );
637
638
    my $rec = MARC::Record->new;
639
    my $fld = MARC::Field->new( '975', '', '', 'p', 'This is a basket' );
640
    $rec->append_fields($fld);
641
    my $str = $rec->as_xml;
642
643
    print $fh $str;
644
645
    close $fh;
646
647
    my $account1 = Koha::MarcOrderAccount->new(
648
        {
649
            basket_name_field => '975$p',
650
            encoding          => 'UTF-8',
651
            description       => 'test',
652
        }
653
    )->store;
654
655
    my $basket_name = Koha::MarcOrder->_check_file_for_basket_name(
656
        {
657
            filename => $name,
658
            filepath => $name,
659
            profile  => $account1,
660
        }
661
    );
662
663
    is( $basket_name, "This is a basket", 'Basket name identified correctly' );
664
665
    my $account2 = Koha::MarcOrderAccount->new(
666
        {
667
            basket_name_field => '975$z',
668
            encoding          => 'UTF-8',
669
            description       => 'test',
670
        }
671
    )->store;
672
673
    my $basket_name2 = Koha::MarcOrder->_check_file_for_basket_name(
674
        {
675
            filename => $name,
676
            filepath => $name,
677
            profile  => $account2,
678
        }
679
    );
680
681
    is( $basket_name2, $name, 'No filename provided in the file' );
682
683
    $schema->storage->txn_rollback;
684
};

Return to bug 39518