From b91cb01f0bf513e8ef61a047271b26b4fb8043d5 Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Thu, 7 Sep 2023 10:47:15 +0000 Subject: [PATCH] Bug 34355: Add match_field and match_value to allow multiple accounts in the same file directory Signed-off-by: Andrew Fuerste Henry Signed-off-by: Nick Clemens --- Koha/MarcOrder.pm | 65 ++++++++++++++++--- Koha/Schema/Result/MarcOrderAccount.pm | 24 ++++++- admin/marc_order_accounts.pl | 2 + .../bug_34355-add_marc_order_accounts.pl | 16 +++-- installer/data/mysql/kohastructure.sql | 2 + .../en/modules/admin/marc_order_accounts.tt | 10 +++ misc/cronjobs/marc_ordering_process.pl | 2 +- 7 files changed, 103 insertions(+), 18 deletions(-) mode change 100644 => 100755 admin/marc_order_accounts.pl diff --git a/Koha/MarcOrder.pm b/Koha/MarcOrder.pm index 2ec28cff59e..be8f9073196 100644 --- a/Koha/MarcOrder.pm +++ b/Koha/MarcOrder.pm @@ -126,15 +126,18 @@ sub create_order_lines_from_file { } ); - while( my $import_record = $import_records->next ){ - my $result = add_biblio_from_import_record({ - import_batch_id => $import_batch_id, - import_record => $import_record, - matcher_id => $params->{matcher_id}, - overlay_action => $params->{overlay_action}, - agent => $agent, - }); - warn "Duplicates found in $result->{duplicates_in_batch}, record was skipped." if $result->{duplicates_in_batch}; + while ( my $import_record = $import_records->next ) { + my $result = add_biblio_from_import_record( + { + import_batch_id => $import_batch_id, + import_record => $import_record, + matcher_id => $params->{matcher_id}, + overlay_action => $params->{overlay_action}, + agent => $agent, + } + ); + warn "Duplicates found in $result->{duplicates_in_batch}, record was skipped." + if $result->{duplicates_in_batch}; next if $result->{skip}; my $order_line_details = add_items_from_import_record( @@ -595,6 +598,50 @@ sub add_items_from_import_record { } +=head3 match_file_to_account + + my $file_match = Koha::MarcOrder->match_file_to_account({ + filename => $filename, + filepath => $filepath, + profile => $profile + }); + + Used by the cronjob to detect whether a file matches the account and should be processed + +=cut + +sub match_file_to_account { + my ( $self, $args ) = @_; + + my $match = 0; + my $filename = $args->{filename}; + my $filepath = $args->{filepath}; + my $profile = $args->{profile}; + my $format = index( $filename, '.mrc' ) != -1 ? 'ISO2709' : 'MARCXML'; + + my ( $errors, $marcrecords ); + if ( $format eq 'MARCXML' ) { + ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $profile->encoding ); + } elsif ( $format eq 'ISO2709' ) { + ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( + $filepath, $profile->record_type, + $profile->encoding + ); + } + + my $match_record = @{$marcrecords}[0]; + my ( $field, $subfield ) = split /\$/, $profile->match_field; + + my $field_value = $match_record->subfield( $field, $subfield ) || ''; + my $match_value = $profile->match_value || ''; + + if ( $field_value eq $match_value ) { + $match = 1; + } + + return $match; +} + =head3 import_batches_list Fetches import batches matching the batch to be added to the basket and returns these to the template diff --git a/Koha/Schema/Result/MarcOrderAccount.pm b/Koha/Schema/Result/MarcOrderAccount.pm index faaf9054148..2132ab3fbb2 100644 --- a/Koha/Schema/Result/MarcOrderAccount.pm +++ b/Koha/Schema/Result/MarcOrderAccount.pm @@ -116,6 +116,22 @@ type of record in the file file encoding +=head2 match_field + + data_type: 'varchar' + is_nullable: 1 + size: 10 + +the field that a vendor account has been mapped to in a marc record + +=head2 match_value + + data_type: 'varchar' + is_nullable: 1 + size: 50 + +the value to be matched against the marc record + =cut __PACKAGE__->add_columns( @@ -143,6 +159,10 @@ __PACKAGE__->add_columns( { data_type => "varchar", is_nullable => 1, size => 50 }, "encoding", { data_type => "varchar", is_nullable => 1, size => 50 }, + "match_field", + { data_type => "varchar", is_nullable => 1, size => 10 }, + "match_value", + { data_type => "varchar", is_nullable => 1, size => 50 }, ); =head1 PRIMARY KEY @@ -200,8 +220,8 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07051 @ 2024-07-31 13:01:54 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:Fz6iYOJIQLrlYvAnmW6C4A +# Created by DBIx::Class::Schema::Loader v0.07051 @ 2024-10-21 14:34:15 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:6WEUWsxB/YuNiYoIg8TjMQ __PACKAGE__->add_columns( '+parse_items' => { is_boolean => 1 }, diff --git a/admin/marc_order_accounts.pl b/admin/marc_order_accounts.pl old mode 100644 new mode 100755 index c0e1bf25cb1..acd969e997c --- a/admin/marc_order_accounts.pl +++ b/admin/marc_order_accounts.pl @@ -88,6 +88,8 @@ if ( $op eq 'acct_form' ) { item_action => scalar $input->param('item_action'), record_type => scalar $input->param('record_type'), encoding => scalar $input->param('encoding') || 'UTF-8', + match_field => scalar $input->param('match_field'), + match_value => scalar $input->param('match_value'), }; if ( scalar $input->param('id') ) { diff --git a/installer/data/mysql/atomicupdate/bug_34355-add_marc_order_accounts.pl b/installer/data/mysql/atomicupdate/bug_34355-add_marc_order_accounts.pl index f0a5f2ab216..149afbeee96 100755 --- a/installer/data/mysql/atomicupdate/bug_34355-add_marc_order_accounts.pl +++ b/installer/data/mysql/atomicupdate/bug_34355-add_marc_order_accounts.pl @@ -1,14 +1,15 @@ use Modern::Perl; return { - bug_number => "34355", + bug_number => "34355", description => "Add a table to allow creation of MARC order accounts and a syspref to activate it.", - up => sub { + up => sub { my ($args) = @_; - my ($dbh, $out) = @$args{qw(dbh out)}; + my ( $dbh, $out ) = @$args{qw(dbh out)}; - unless( TableExists('marc_order_accounts') ) { - $dbh->do(q{ + unless ( TableExists('marc_order_accounts') ) { + $dbh->do( + q{ CREATE TABLE `marc_order_accounts` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'unique identifier and primary key', `description` varchar(250) NOT NULL COMMENT 'description of this account', @@ -22,11 +23,14 @@ return { `parse_items` tinyint(1) DEFAULT NULL COMMENT 'should items be parsed', `record_type` varchar(50) DEFAULT NULL COMMENT 'type of record in the file', `encoding` varchar(50) DEFAULT NULL COMMENT 'file encoding', + `match_field` varchar(10) DEFAULT NULL COMMENT 'the field that a vendor account has been mapped to in a marc record', + `match_value` varchar(50) DEFAULT NULL COMMENT 'the value to be matched against the marc record', PRIMARY KEY (`id`), CONSTRAINT `marc_ordering_account_ibfk_1` FOREIGN KEY (`vendor_id`) REFERENCES `aqbooksellers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `marc_ordering_account_ibfk_2` FOREIGN KEY (`budget_id`) REFERENCES `aqbudgets` (`budget_id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - }); + } + ); say $out "Added new table 'marc_order_accounts'"; } else { diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index a1b2957e2e9..3afcfa57663 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -4490,6 +4490,8 @@ CREATE TABLE `marc_order_accounts` ( `parse_items` tinyint(1) DEFAULT NULL COMMENT 'should items be parsed', `record_type` varchar(50) DEFAULT NULL COMMENT 'type of record in the file', `encoding` varchar(50) DEFAULT NULL COMMENT 'file encoding', + `match_field` varchar(10) DEFAULT NULL COMMENT 'the field that a vendor account has been mapped to in a marc record', + `match_value` varchar(50) DEFAULT NULL COMMENT 'the value to be matched against the marc record', PRIMARY KEY (`id`), CONSTRAINT `marc_ordering_account_ibfk_1` FOREIGN KEY (`vendor_id`) REFERENCES `aqbooksellers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `marc_ordering_account_ibfk_2` FOREIGN KEY (`budget_id`) REFERENCES `aqbudgets` (`budget_id`) ON DELETE CASCADE ON UPDATE CASCADE diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/marc_order_accounts.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/marc_order_accounts.tt index 936247ea87d..eda9b144475 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/marc_order_accounts.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/marc_order_accounts.tt @@ -127,6 +127,16 @@ MARC Order Accounts
The download directory specifies the directory in your koha installation that should be searched for new files.
+
  • + + +
    (Optional): If you have files from multiple vendors in the same file directory, the match field is the field in the marc record that will be checked to see if the file should be processed by this account.
    +
  • +
  • + + +
    (Optional): This is the value that will be checked against the match field to see if the file matches this account. If it does it will be processed by this account, if not it will be skipped.
    +
  • diff --git a/misc/cronjobs/marc_ordering_process.pl b/misc/cronjobs/marc_ordering_process.pl index 301579dcd19..113c4d8876f 100755 --- a/misc/cronjobs/marc_ordering_process.pl +++ b/misc/cronjobs/marc_ordering_process.pl @@ -127,7 +127,7 @@ foreach my $acct (@accounts) { } } } - say sprintf "%s files processed", $files_processed unless $files_processed == 0; + say sprintf "%s file(s) processed", $files_processed unless $files_processed == 0; print "Moving to next account\n\n"; } print "Process complete\n"; -- 2.39.5