Skip to content

Commit 9d648c9

Browse files
committed
Make item addition dependent on MarcItemFieldsToOrder mappings and introduce MarcFieldsToOrder to cronjob
1 parent 74a1a68 commit 9d648c9

File tree

3 files changed

+106
-25
lines changed

3 files changed

+106
-25
lines changed

Koha/MarcOrder.pm

Lines changed: 100 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,41 @@ sub _stage_file {
325325
};
326326
}
327327

328+
=head3 _get_MarcFieldsToOrder_syspref_data
329+
330+
my $marc_fields_to_order = _get_MarcFieldsToOrder_syspref_data('MarcFieldsToOrder', $marcrecord, $fields);
331+
332+
Fetches data from a marc record based on the mappings in the syspref MarcFieldsToOrder using the fields selected in $fields (array).
333+
334+
=cut
335+
336+
sub _get_MarcFieldsToOrder_syspref_data {
337+
my ($syspref_name, $record, $field_list) = @_;
338+
my $syspref = C4::Context->preference($syspref_name);
339+
$syspref = "$syspref\n\n";
340+
my $yaml = eval {
341+
YAML::XS::Load(Encode::encode_utf8($syspref));
342+
};
343+
if ( $@ ) {
344+
warn "Unable to parse $syspref syspref : $@";
345+
return ();
346+
}
347+
my $r;
348+
for my $field_name ( @$field_list ) {
349+
next unless exists $yaml->{$field_name};
350+
my @fields = split /\|/, $yaml->{$field_name};
351+
for my $field ( @fields ) {
352+
my ( $f, $sf ) = split /\$/, $field;
353+
next unless $f and $sf;
354+
if ( my $v = $record->subfield( $f, $sf ) ) {
355+
$r->{$field_name} = $v;
356+
}
357+
last if $yaml->{$field};
358+
}
359+
}
360+
return $r;
361+
}
362+
328363
=head3 _get_MarcItemFieldsToOrder_syspref_data
329364
330365
my $marc_item_fields_to_order = _get_MarcItemFieldsToOrder_syspref_data('MarcItemFieldsToOrder', $marcrecord, $fields);
@@ -359,8 +394,6 @@ sub _get_MarcItemFieldsToOrder_syspref_data {
359394
}
360395
@tags_list = List::MoreUtils::uniq(@tags_list);
361396

362-
die "System preference MarcItemFieldsToOrder has not been filled in. Please set the mapping values to use this cron script." if scalar(@tags_list == 0);
363-
364397
my $tags_count = _verify_number_of_fields(\@tags_list, $record);
365398
# Return if the number of these fields in the record is not the same.
366399
die "Invalid number of fields detected on field $tags_count->{key}, please check this file" if $tags_count->{error};
@@ -375,21 +408,24 @@ sub _get_MarcItemFieldsToOrder_syspref_data {
375408
$fields_hash->{$tag} = \@tmp_fields;
376409
}
377410

378-
for (my $i = 0; $i < $tags_count->{count}; $i++) {
379-
my $r;
380-
for my $field_name ( @$field_list ) {
381-
next unless exists $yaml->{$field_name};
382-
my @fields = split /\|/, $yaml->{$field_name};
383-
for my $field ( @fields ) {
384-
my ( $f, $sf ) = split /\$/, $field;
385-
next unless $f and $sf;
386-
my $v = $fields_hash->{$f}[$i] ? $fields_hash->{$f}[$i]->subfield( $sf ) : undef;
387-
$r->{$field_name} = $v if (defined $v);
388-
last if $yaml->{$field};
411+
if($tags_count->{count}){
412+
for (my $i = 0; $i < $tags_count->{count}; $i++) {
413+
my $r;
414+
for my $field_name ( @$field_list ) {
415+
next unless exists $yaml->{$field_name};
416+
my @fields = split /\|/, $yaml->{$field_name};
417+
for my $field ( @fields ) {
418+
my ( $f, $sf ) = split /\$/, $field;
419+
next unless $f and $sf;
420+
my $v = $fields_hash->{$f}[$i] ? $fields_hash->{$f}[$i]->subfield( $sf ) : undef;
421+
$r->{$field_name} = $v if (defined $v);
422+
last if $yaml->{$field};
423+
}
389424
}
425+
push @result, $r;
390426
}
391-
push @result, $r;
392427
}
428+
393429
return $result[0];
394430
}
395431

@@ -416,7 +452,6 @@ sub _verify_number_of_fields {
416452
return { error => 1, key => $key } if $tag_fields_count->{$key} != $tags_count; # All counts of various fields should be equal if they exist
417453
}
418454
}
419-
420455
return { error => 0, count => $tags_count };
421456
}
422457

@@ -539,6 +574,23 @@ sub add_items_from_import_record {
539574
my @order_line_details;
540575

541576
if($agent eq 'cron') {
577+
my $marc_fields_to_order = _get_MarcFieldsToOrder_syspref_data('MarcFieldsToOrder', $marcrecord, ['price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2']);
578+
my $quantity = $marc_fields_to_order->{quantity};
579+
my $budget_code = $marc_fields_to_order->{budget_code} || $budget_id; # Use fallback from ordering profile if not mapped
580+
my $price = $marc_fields_to_order->{price};
581+
my $discount = $marc_fields_to_order->{discount};
582+
my $sort1 = $marc_fields_to_order->{sort1};
583+
my $sort2 = $marc_fields_to_order->{sort2};
584+
my $mapped_budget;
585+
if($budget_code) {
586+
my $biblio_budget = GetBudgetByCode($budget_code);
587+
if($biblio_budget) {
588+
$mapped_budget = $biblio_budget->{budget_id};
589+
} else {
590+
$mapped_budget = $budget_id;
591+
}
592+
}
593+
542594
my $marc_item_fields_to_order = _get_MarcItemFieldsToOrder_syspref_data('MarcItemFieldsToOrder', $marcrecord, ['homebranch', 'holdingbranch', 'itype', 'nonpublic_note', 'public_note', 'loc', 'ccode', 'notforloan', 'uri', 'copyno', 'price', 'replacementprice', 'itemcallnumber', 'quantity', 'budget_code']);
543595
my $item_homebranch = $marc_item_fields_to_order->{homebranch};
544596
my $item_holdingbranch = $marc_item_fields_to_order->{holdingbranch};
@@ -550,7 +602,7 @@ sub add_items_from_import_record {
550602
my $item_notforloan = $marc_item_fields_to_order->{notforloan};
551603
my $item_uri = $marc_item_fields_to_order->{uri};
552604
my $item_copyno = $marc_item_fields_to_order->{copyno};
553-
my $item_quantity = $marc_item_fields_to_order->{quantity};
605+
my $item_quantity = $marc_item_fields_to_order->{quantity} || 0;
554606
my $item_budget_code = $marc_item_fields_to_order->{budget_code};
555607
my $item_budget_id;
556608
if ( $marc_item_fields_to_order->{budget_code} ) {
@@ -566,13 +618,10 @@ sub add_items_from_import_record {
566618
my $item_price = $marc_item_fields_to_order->{price};
567619
my $item_replacement_price = $marc_item_fields_to_order->{replacementprice};
568620
my $item_callnumber = $marc_item_fields_to_order->{itemcallnumber};
569-
570-
if(!$item_quantity) {
571-
my $isbn = $marcrecord->subfield( '020', "a" );
572-
warn "No quantity found for record with ISBN: $isbn. No items will be added.";
573-
}
621+
my $itemcreation = 0;
574622

575623
for (my $i = 0; $i < $item_quantity; $i++) {
624+
$itemcreation = 1;
576625
my $item = Koha::Item->new({
577626
biblionumber => $biblionumber,
578627
homebranch => $item_homebranch,
@@ -592,8 +641,8 @@ sub add_items_from_import_record {
592641

593642
my %order_detail_hash = (
594643
biblionumber => $biblionumber,
595-
itemnumbers => ($item->itemnumber),
596644
basketno => $basket_id,
645+
itemnumbers => ($item->itemnumber),
597646
quantity => 1,
598647
budget_id => $item_budget_id,
599648
currency => $vendor->listprice,
@@ -615,6 +664,34 @@ sub add_items_from_import_record {
615664

616665
push @order_line_details, \%order_detail_hash;
617666
}
667+
668+
if(!$itemcreation) {
669+
my %order_detail_hash = (
670+
biblionumber => $biblionumber,
671+
basketno => $basket_id,
672+
quantity => $quantity,
673+
budget_id => $mapped_budget,
674+
uncertainprice => 1,
675+
sort1 => $sort1,
676+
sort2 => $sort2,
677+
);
678+
679+
if ($price){
680+
$order_detail_hash{tax_rate_on_ordering} = $vendor->tax_rate;
681+
$order_detail_hash{tax_rate_on_receiving} = $vendor->tax_rate;
682+
my $order_discount = $discount ? $discount : $vendor->discount;
683+
$order_detail_hash{discount} = $order_discount;
684+
$order_detail_hash{rrp} = $price;
685+
$order_detail_hash{ecost} = $order_discount ? $price * ( 1 - $order_discount / 100 ) : $price;
686+
$order_detail_hash{listprice} = $order_detail_hash{rrp} / $active_currency->rate;
687+
$order_detail_hash{unitprice} = $order_detail_hash{ecost};
688+
} else {
689+
$order_detail_hash{listprice} = 0;
690+
}
691+
692+
$order_detail_hash{uncertainprice} = 0 if $order_detail_hash{listprice};
693+
push @order_line_details, \%order_detail_hash;
694+
}
618695
}
619696

620697
if($agent eq 'client') {
@@ -750,7 +827,7 @@ sub create_order_lines {
750827
my $order_line_details = $args->{order_line_details};
751828

752829
foreach my $order_detail ( @{ $order_line_details } ) {
753-
my @itemnumbers = $order_detail->{itemnumbers};
830+
my @itemnumbers = $order_detail->{itemnumbers} || ();
754831
delete($order_detail->{itemnumber});
755832
my $order = Koha::Acquisition::Order->new( \%{ $order_detail } );
756833
$order->populate_with_prices_for_ordering();

acqui/addorderiso2709.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
matcher_id => $matcher_id,
202202
overlay_action => $overlay_action,
203203
agent => 'client',
204-
import_record_id_selected => @import_record_id_selected,
204+
import_record_id_selected => \@import_record_id_selected,
205205
client_item_fields => $client_item_fields,
206206
basket_id => $cgiparams->{'basketno'},
207207
vendor => $bookseller,

misc/cronjobs/marc_ordering_process.pl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ =head1 OPTIONS
100100
opendir my $dir, $working_dir or die "Can't open filepath";
101101
my @files = grep { /\.(mrc|marcxml|mrk)/i } readdir $dir;
102102
closedir $dir;
103+
print "No new files found\n" if scalar(@files) == 0;
104+
105+
my $files_processed = 0;
103106

104107
foreach my $filename ( @files ) {
105108
say sprintf "Creating order lines from file %s", $filename if $verbose;
@@ -113,6 +116,7 @@ =head1 OPTIONS
113116
};
114117
my $result = Koha::MarcOrder->create_order_lines_from_file($args);
115118
if($result->{success}) {
119+
$files_processed++;
116120
say sprintf "Successfully processed file: %s", $filename if $verbose;
117121
unlink $full_path;
118122
} else {
@@ -121,7 +125,7 @@ =head1 OPTIONS
121125
};
122126
}
123127
}
124-
print "All files completed\n";
128+
say sprintf "%s files processed", $files_processed unless $files_processed == 0;
125129
print "Moving to next account\n\n";
126130
}
127131
print "Process complete\n";

0 commit comments

Comments
 (0)