From 74e34cc8413d0f2b6646ebf8a1c4d57ca1e14c2e Mon Sep 17 00:00:00 2001 From: Matt Blenkinsop Date: Tue, 8 Aug 2023 10:39:10 +0000 Subject: [PATCH] Bug 34355: Make item addition dependent on MarcItemFieldsToOrder mappings and introduce MarcFieldsToOrder to cronjob --- Koha/MarcOrder.pm | 96 +++++++++++--------------- misc/cronjobs/marc_ordering_process.pl | 6 +- 2 files changed, 46 insertions(+), 56 deletions(-) diff --git a/Koha/MarcOrder.pm b/Koha/MarcOrder.pm index e9e5ec4420..44680e088a 100644 --- a/Koha/MarcOrder.pm +++ b/Koha/MarcOrder.pm @@ -122,7 +122,7 @@ sub create_order_lines_from_file { }); while( my $import_record = $import_records->next ){ - my $result = add_biblios_from_import_record({ + my $result = add_biblio_from_import_record({ import_batch_id => $import_batch_id, import_record => $import_record, matcher_id => $params->{matcher_id}, @@ -333,9 +333,44 @@ sub _stage_file { }; } +=head3 _get_MarcFieldsToOrder_syspref_data + + my $marc_fields_to_order = _get_MarcFieldsToOrder_syspref_data( $marcrecord ); + + Fetches data from a marc record based on the mappings in the syspref MarcFieldsToOrder using the fields selected in $fields (array). + +=cut + +sub _get_MarcFieldsToOrder_syspref_data { + my ( $record ) = @_; + my $syspref = C4::Context->preference('MarcFieldsToOrder'); + $syspref = "$syspref\n\n"; + my $yaml = eval { + YAML::XS::Load(Encode::encode_utf8($syspref)); + }; + if ( $@ ) { + warn "Unable to parse $syspref syspref : $@"; + return (); + } + my $r; + for my $field_name ( keys %$yaml ) { + next unless exists $yaml->{$field_name}; + my @fields = split /\|/, $yaml->{$field_name}; + for my $field ( @fields ) { + my ( $f, $sf ) = split /\$/, $field; + next unless $f and $sf; + if ( my $v = $record->subfield( $f, $sf ) ) { + $r->{$field_name} = $v; + } + last if $yaml->{$field}; + } + } + return $r; +} + =head3 _get_MarcItemFieldsToOrder_syspref_data - my $marc_item_fields_to_order = _get_MarcItemFieldsToOrder_syspref_data('MarcItemFieldsToOrder', $marcrecord, $fields); + my $marc_item_fields_to_order = _get_MarcItemFieldsToOrder_syspref_data($marcrecord); Fetches data from a marc record based on the mappings in the syspref MarcItemFieldsToOrder using the fields selected in $fields (array). @@ -358,8 +393,6 @@ sub _get_MarcItemFieldsToOrder_syspref_data { } @tags_list = List::MoreUtils::uniq(@tags_list); - die "System preference MarcItemFieldsToOrder has not been filled in. Please set the mapping values to use this cron script." if scalar(@tags_list == 0); - my $tags_count = _verify_number_of_fields(\@tags_list, $record); # Return if the number of these fields in the record is not the same. die "Invalid number of fields detected on field $tags_count->{key}, please check this file" if $tags_count->{error}; @@ -417,7 +450,6 @@ sub _verify_number_of_fields { return { error => 1, key => $key } if $tag_fields_count->{$key} != $tags_count; # All counts of various fields should be equal if they exist } } - return { error => 0, count => $tags_count }; } @@ -539,11 +571,7 @@ sub add_items_from_import_record { my $marcrecord = $record_result->{marcrecord}; if ( $agent eq 'cron' ) { - my $marc_fields_to_order = _get_MarcFieldsToOrder_syspref_data( - 'MarcFieldsToOrder', $marcrecord, - [ 'price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2' ] - ); - + my $marc_fields_to_order = _get_MarcFieldsToOrder_syspref_data($marcrecord); my $marc_item_fields_to_order = _get_MarcItemFieldsToOrder_syspref_data($marcrecord); my $item_fields = _create_item_fields_from_syspref( @@ -556,7 +584,6 @@ sub add_items_from_import_record { my $order_line_fields = parse_input_into_order_line_fields( { - agent => $agent, biblionumber => $biblionumber, budget_id => $budget_id, basket_id => $basket_id, @@ -579,7 +606,6 @@ sub add_items_from_import_record { if ( $agent eq 'client' ) { my $order_line_fields = parse_input_into_order_line_fields( { - agent => $agent, biblionumber => $biblionumber, budget_id => $budget_id, basket_id => $basket_id, @@ -618,7 +644,7 @@ sub create_order_lines { my $order_line_details = $args->{order_line_details}; foreach my $order_detail ( @{ $order_line_details } ) { - my @itemnumbers = $order_detail->{itemnumbers}; + my @itemnumbers = $order_detail->{itemnumbers} || (); delete($order_detail->{itemnumber}); my $order = Koha::Acquisition::Order->new( \%{ $order_detail } ); $order->populate_with_prices_for_ordering(); @@ -816,7 +842,7 @@ sub import_biblios_list { push @list, \%cellrecord; # If MarcItemFieldsToOrder is not set, we use MarcFieldsToOrder to populate the order form. - if ( $alliteminfos || %$alliteminfos == -1 ) { + if ( $alliteminfos == -1 || scalar(@$alliteminfos) == 0 ) { $cellrecord{price} = $price || ''; $cellrecord{replacementprice} = $replacementprice || ''; $cellrecord{quantity} = $quantity || ''; @@ -887,7 +913,6 @@ This function takes inputs from either the cronjob or UI and then parses that in my $order_line_fields = parse_input_into_order_line_fields( { - agent => $agent, biblionumber => $biblionumber, budget_id => $budget_id, basket_id => $basket_id, @@ -900,51 +925,12 @@ my $order_line_fields = parse_input_into_order_line_fields( sub parse_input_into_order_line_fields { my ($args) = @_; - my $agent = $args->{agent}; - my $client = $agent eq 'client' ? 1 : 0; my $biblionumber = $args->{biblionumber}; my $budget_id = $args->{budget_id}; my $basket_id = $args->{basket_id}; my $fields = $args->{fields}; my $quantity = $fields->{quantity} || 1; - # my @homebranches = $client ? @{ $fields->{homebranches} } : ( ( $fields->{homebranch} ) x $quantity ); - # my @holdingbranches = $client ? @{ $fields->{holdingbranches} } : ( ( $fields->{holdingbranch} ) x $quantity ); - # my @itypes = $client ? @{ $fields->{itypes} } : ( ( $fields->{itype} ) x $quantity ); - # my @nonpublic_notes = $client ? @{ $fields->{nonpublic_notes} } : ( ( $fields->{nonpublic_note} ) x $quantity ); - # my @public_notes = $client ? @{ $fields->{public_notes} } : ( ( $fields->{public_note} ) x $quantity ); - # my @locs = $client ? @{ $fields->{locs} } : ( ( $fields->{loc} ) x $quantity ); - # my @ccodes = $client ? @{ $fields->{ccodes} } : ( ( $fields->{ccode} ) x $quantity ); - # my @notforloans = $client ? @{ $fields->{notforloans} } : ( ( $fields->{notforloan} ) x $quantity ); - # my @uris = $client ? @{ $fields->{uris} } : ( ( $fields->{uri} ) x $quantity ); - # my @copynos = $client ? @{ $fields->{copynos} } : ( ( $fields->{copyno} ) x $quantity ); - # my @itemprices = $client ? @{ $fields->{itemprices} } : ( ( $fields->{price} ) x $quantity ); - # my @replacementprices = - # $client ? @{ $fields->{replacementprices} } : ( ( $fields->{replacementprice} ) x $quantity ); - # my @itemcallnumbers = $client ? @{ $fields->{itemcallnumbers} } : ( ( $fields->{itemcallnumber} ) x $quantity ); - # my $c_quantity = $client ? $fields->{c_quantity} : $fields->{c_quantity}; - # my $c_budget_id = $client ? $fields->{c_budget_id} : $fields->{c_budget_id}; - # my $c_discount = $client ? $fields->{c_discount} : $fields->{c_discount}; - # my $c_sort1 = $client ? $fields->{c_sort1} : $fields->{c_sort1}; - # my $c_sort2 = $client ? $fields->{c_sort2} : $fields->{c_sort2}; - # my $c_replacement_price = $client ? $fields->{c_replacement_price} : $fields->{c_replacement_price}; - # my $c_price = $client ? $fields->{c_price} : $fields->{c_price}; - - # If using the cronjob, we want to default to the account budget if not mapped on the record - # my $item_budget_id; - # if ( !$client && ( $fields->{budget_code} || $fields->{c_budget_code} ) ) { - # my $budget_code = $fields->{budget_code} || $fields->{c_budget_code}; - # my $item_budget = GetBudgetByCode($budget_code); - # if ($item_budget) { - # $item_budget_id = $item_budget->{budget_id}; - # } else { - # $item_budget_id = $budget_id; - # } - # } else { - # $item_budget_id = $budget_id; - # } - # my @budget_codes = $client ? @{ $fields->{budget_codes} } : ($item_budget_id); - # my $loop_limit = $client ? scalar(@homebranches) : $quantity; my $order_line_fields = { biblionumber => $biblionumber, @@ -1124,7 +1110,7 @@ sub _create_item_fields_from_syspref { push @uris, $infoset->{uri}; push @copynos, $infoset->{copyno}; push @budget_codes, $item_budget_id; - push @itemprices, $infoset->{itemprice}; + push @itemprices, $infoset->{price}; push @replacementprices, $infoset->{replacementprice}; push @itemcallnumbers, $infoset->{itemcallnumber}; } diff --git a/misc/cronjobs/marc_ordering_process.pl b/misc/cronjobs/marc_ordering_process.pl index 498bf01701..e53ffb6765 100644 --- a/misc/cronjobs/marc_ordering_process.pl +++ b/misc/cronjobs/marc_ordering_process.pl @@ -100,6 +100,9 @@ foreach my $acct ( @accounts ) { opendir my $dir, $working_dir or die "Can't open filepath"; my @files = grep { /\.(mrc|marcxml|mrk)/i } readdir $dir; closedir $dir; + print "No new files found\n" if scalar(@files) == 0; + + my $files_processed = 0; foreach my $filename ( @files ) { say sprintf "Creating order lines from file %s", $filename if $verbose; @@ -113,6 +116,7 @@ foreach my $acct ( @accounts ) { }; my $result = Koha::MarcOrder->create_order_lines_from_file($args); if($result->{success}) { + $files_processed++; say sprintf "Successfully processed file: %s", $filename if $verbose; unlink $full_path; } else { @@ -121,7 +125,7 @@ foreach my $acct ( @accounts ) { }; } } - print "All files completed\n"; + say sprintf "%s files processed", $files_processed unless $files_processed == 0; print "Moving to next account\n\n"; } print "Process complete\n"; -- 2.37.1 (Apple Git-137.1)