Skip to content

Commit ba4ab81

Browse files
committed
Add match_field and match_value to allow multiple accounts in the same file directory
1 parent db8cb9c commit ba4ab81

File tree

7 files changed

+104
-16
lines changed

7 files changed

+104
-16
lines changed

Koha/MarcOrder.pm

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,4 +852,50 @@ sub create_order_lines {
852852
return;
853853
}
854854

855+
856+
=head3 match_file_to_account
857+
858+
my $file_match = Koha::MarcOrder->match_file_to_account({
859+
filename => $filename,
860+
filepath => $filepath,
861+
profile => $profile
862+
});
863+
864+
Used by the cronjob to detect whether a file matches the account and should be processed
865+
866+
=cut
867+
868+
869+
sub match_file_to_account {
870+
my ($self, $args) = @_;
871+
872+
my $match = 0;
873+
my $filename = $args->{filename};
874+
my $filepath = $args->{filepath};
875+
my $profile = $args->{profile};
876+
my $format = index($filename, '.mrc') != -1 ? 'ISO2709' : 'MARCXML';
877+
878+
my ( $errors, $marcrecords );
879+
if ( $format eq 'MARCXML' ) {
880+
( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $profile->encoding );
881+
} elsif ( $format eq 'ISO2709' ) {
882+
( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File(
883+
$filepath, $profile->record_type,
884+
$profile->encoding
885+
);
886+
}
887+
888+
my $match_record = @{ $marcrecords }[0];
889+
my ( $field, $subfield ) = split /\$/, $profile->match_field;
890+
891+
my $field_value = $match_record->subfield( $field, $subfield );
892+
my $match_value = $profile->match_value;
893+
894+
if($field_value eq $match_value) {
895+
$match = 1;
896+
}
897+
898+
return $match;
899+
}
900+
855901
1;

Koha/Schema/Result/MarcOrderAccount.pm

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ type of record in the file
116116
117117
file encoding
118118
119+
=head2 match_field
120+
121+
data_type: 'varchar'
122+
is_nullable: 1
123+
size: 10
124+
125+
the field that a vendor account has been mapped to in a marc record
126+
127+
=head2 match_value
128+
129+
data_type: 'varchar'
130+
is_nullable: 1
131+
size: 50
132+
133+
the value to be matched against the marc record
134+
119135
=cut
120136

121137
__PACKAGE__->add_columns(
@@ -143,6 +159,10 @@ __PACKAGE__->add_columns(
143159
{ data_type => "varchar", is_nullable => 1, size => 50 },
144160
"encoding",
145161
{ data_type => "varchar", is_nullable => 1, size => 50 },
162+
"match_field",
163+
{ data_type => "varchar", is_nullable => 1, size => 10 },
164+
"match_value",
165+
{ data_type => "varchar", is_nullable => 1, size => 50 },
146166
);
147167

148168
=head1 PRIMARY KEY
@@ -200,8 +220,8 @@ __PACKAGE__->belongs_to(
200220
);
201221

202222

203-
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2023-07-18 16:31:16
204-
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:vMLrmisXQnn2e60qW7ppnA
223+
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2023-09-07 08:55:26
224+
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:6pN1TLuqkf9rQaeQ7Wf26A
205225

206226

207227
# You can replace this text with custom code or comments, and it will be preserved on regeneration

admin/marc_order_accounts.pl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@
8989
item_action => scalar $input->param('item_action'),
9090
record_type => scalar $input->param('record_type'),
9191
encoding => scalar $input->param('encoding') || 'UTF-8',
92+
match_field => scalar $input->param('match_field'),
93+
match_value => scalar $input->param('match_value'),
9294
};
9395

9496
if(scalar $input->param('id')) {

installer/data/mysql/atomicupdate/bug_34355-add_marc_order_accounts.pl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use Modern::Perl;
22

33
return {
4-
bug_number => "34355",
4+
bug_number => "34355",
55
description => "Add a table to allow creation of MARC order accounts and a syspref to activate it.",
6-
up => sub {
6+
up => sub {
77
my ($args) = @_;
8-
my ($dbh, $out) = @$args{qw(dbh out)};
8+
my ( $dbh, $out ) = @$args{qw(dbh out)};
99

10-
unless( TableExists('marc_order_accounts') ) {
11-
$dbh->do(q{
10+
unless ( TableExists('marc_order_accounts') ) {
11+
$dbh->do(
12+
q{
1213
CREATE TABLE `marc_order_accounts` (
1314
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'unique identifier and primary key',
1415
`description` varchar(250) NOT NULL COMMENT 'description of this account',
@@ -22,11 +23,14 @@
2223
`parse_items` tinyint(1) DEFAULT NULL COMMENT 'should items be parsed',
2324
`record_type` varchar(50) DEFAULT NULL COMMENT 'type of record in the file',
2425
`encoding` varchar(50) DEFAULT NULL COMMENT 'file encoding',
26+
`match_field` varchar(10) DEFAULT NULL COMMENT 'the field that a vendor account has been mapped to in a marc record',
27+
`match_value` varchar(50) DEFAULT NULL COMMENT 'the value to be matched against the marc record',
2528
PRIMARY KEY (`id`),
2629
CONSTRAINT `marc_ordering_account_ibfk_1` FOREIGN KEY (`vendor_id`) REFERENCES `aqbooksellers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
2730
CONSTRAINT `marc_ordering_account_ibfk_2` FOREIGN KEY (`budget_id`) REFERENCES `aqbudgets` (`budget_id`) ON DELETE CASCADE ON UPDATE CASCADE
2831
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
29-
});
32+
}
33+
);
3034

3135
say $out "Added new table 'marc_order_accounts'";
3236
} else {

installer/data/mysql/kohastructure.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4111,6 +4111,8 @@ CREATE TABLE `marc_order_accounts` (
41114111
`parse_items` tinyint(1) DEFAULT NULL COMMENT 'should items be parsed',
41124112
`record_type` varchar(50) DEFAULT NULL COMMENT 'type of record in the file',
41134113
`encoding` varchar(50) DEFAULT NULL COMMENT 'file encoding',
4114+
`match_field` varchar(10) DEFAULT NULL COMMENT 'the field that a vendor account has been mapped to in a marc record',
4115+
`match_value` varchar(50) DEFAULT NULL COMMENT 'the value to be matched against the marc record',
41144116
PRIMARY KEY (`id`),
41154117
CONSTRAINT `marc_ordering_account_ibfk_1` FOREIGN KEY (`vendor_id`) REFERENCES `aqbooksellers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
41164118
CONSTRAINT `marc_ordering_account_ibfk_2` FOREIGN KEY (`budget_id`) REFERENCES `aqbudgets` (`budget_id`) ON DELETE CASCADE ON UPDATE CASCADE

koha-tmpl/intranet-tmpl/prog/en/modules/admin/marc_order_accounts.tt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ MARC Order Accounts
126126
<input type="text" name="download_directory" id="download_directory" size="20" value="[% account.download_directory | html %]" />
127127
<div class="hint">The download directory specifies the directory in your koha installation that should be searched for new files.</div>
128128
</li>
129+
<li>
130+
<label for="match_field">Match field: </label>
131+
<input type="text" name="match_field" id="match_field" size="20" value="[% account.match_field | html %]" />
132+
<div class="hint">(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.</div>
133+
</li>
134+
<li>
135+
<label for="match_value">Match value: </label>
136+
<input type="text" name="match_value" id="match_value" size="20" value="[% account.match_value | html %]" />
137+
<div class="hint">(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.</div>
138+
</li>
129139
</ol>
130140
</fieldset>
131141
<fieldset class="rows">

misc/cronjobs/marc_ordering_process.pl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,18 @@ =head1 OPTIONS
106106

107107
foreach my $filename ( @files ) {
108108
say sprintf "Creating order lines from file %s", $filename if $verbose;
109+
my $full_path = "$working_dir/$filename";
110+
my $args = {
111+
filename => $filename,
112+
filepath => $full_path,
113+
profile => $acct,
114+
agent => 'cron'
115+
};
116+
if($acct->match_field && $acct->match_value) {
117+
my $file_match = Koha::MarcOrder->match_file_to_account($args);
118+
next if !$file_match;
119+
}
109120
if($confirm) {
110-
my $full_path = "$working_dir/$filename";
111-
my $args = {
112-
filename => $filename,
113-
filepath => $full_path,
114-
profile => $acct,
115-
agent => 'cron'
116-
};
117121
my $result = Koha::MarcOrder->create_order_lines_from_file($args);
118122
if($result->{success}) {
119123
$files_processed++;
@@ -125,7 +129,7 @@ =head1 OPTIONS
125129
};
126130
}
127131
}
128-
say sprintf "%s files processed", $files_processed unless $files_processed == 0;
132+
say sprintf "%s file(s) processed", $files_processed unless $files_processed == 0;
129133
print "Moving to next account\n\n";
130134
}
131135
print "Process complete\n";

0 commit comments

Comments
 (0)