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

(-)a/Koha/MarcOrder.pm (-23 / +100 lines)
Lines 325-330 sub _stage_file { Link Here
325
    };
325
    };
326
}
326
}
327
327
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
328
=head3 _get_MarcItemFieldsToOrder_syspref_data
363
=head3 _get_MarcItemFieldsToOrder_syspref_data
329
364
330
    my $marc_item_fields_to_order = _get_MarcItemFieldsToOrder_syspref_data('MarcItemFieldsToOrder', $marcrecord, $fields);
365
    my $marc_item_fields_to_order = _get_MarcItemFieldsToOrder_syspref_data('MarcItemFieldsToOrder', $marcrecord, $fields);
Lines 359-366 sub _get_MarcItemFieldsToOrder_syspref_data { Link Here
359
    }
394
    }
360
    @tags_list = List::MoreUtils::uniq(@tags_list);
395
    @tags_list = List::MoreUtils::uniq(@tags_list);
361
396
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
364
    my $tags_count = _verify_number_of_fields(\@tags_list, $record);
397
    my $tags_count = _verify_number_of_fields(\@tags_list, $record);
365
    # Return if the number of these fields in the record is not the same.
398
    # Return if the number of these fields in the record is not the same.
366
    die "Invalid number of fields detected on field $tags_count->{key}, please check this file" if $tags_count->{error};
399
    die "Invalid number of fields detected on field $tags_count->{key}, please check this file" if $tags_count->{error};
Lines 375-395 sub _get_MarcItemFieldsToOrder_syspref_data { Link Here
375
        $fields_hash->{$tag} = \@tmp_fields;
408
        $fields_hash->{$tag} = \@tmp_fields;
376
    }
409
    }
377
410
378
    for (my $i = 0; $i < $tags_count->{count}; $i++) {
411
    if($tags_count->{count}){
379
        my $r;
412
        for (my $i = 0; $i < $tags_count->{count}; $i++) {
380
        for my $field_name ( @$field_list ) {
413
            my $r;
381
            next unless exists $yaml->{$field_name};
414
            for my $field_name ( @$field_list ) {
382
            my @fields = split /\|/, $yaml->{$field_name};
415
                next unless exists $yaml->{$field_name};
383
            for my $field ( @fields ) {
416
                my @fields = split /\|/, $yaml->{$field_name};
384
                my ( $f, $sf ) = split /\$/, $field;
417
                for my $field ( @fields ) {
385
                next unless $f and $sf;
418
                    my ( $f, $sf ) = split /\$/, $field;
386
                my $v = $fields_hash->{$f}[$i] ? $fields_hash->{$f}[$i]->subfield( $sf ) : undef;
419
                    next unless $f and $sf;
387
                $r->{$field_name} = $v if (defined $v);
420
                    my $v = $fields_hash->{$f}[$i] ? $fields_hash->{$f}[$i]->subfield( $sf ) : undef;
388
                last if $yaml->{$field};
421
                    $r->{$field_name} = $v if (defined $v);
422
                    last if $yaml->{$field};
423
                }
389
            }
424
            }
425
            push @result, $r;
390
        }
426
        }
391
        push @result, $r;
392
    }
427
    }
428
393
    return $result[0];
429
    return $result[0];
394
}
430
}
395
431
Lines 416-422 sub _verify_number_of_fields { Link Here
416
            return { error => 1, key => $key } if $tag_fields_count->{$key} != $tags_count; # All counts of various fields should be equal if they exist
452
            return { error => 1, key => $key } if $tag_fields_count->{$key} != $tags_count; # All counts of various fields should be equal if they exist
417
        }
453
        }
418
    }
454
    }
419
420
    return { error => 0, count => $tags_count };
455
    return { error => 0, count => $tags_count };
421
}
456
}
422
457
Lines 539-544 sub add_items_from_import_record { Link Here
539
    my @order_line_details;
574
    my @order_line_details;
540
575
541
    if($agent eq 'cron') {
576
    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
542
        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']);
594
        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']);
543
        my $item_homebranch           = $marc_item_fields_to_order->{homebranch};
595
        my $item_homebranch           = $marc_item_fields_to_order->{homebranch};
544
        my $item_holdingbranch        = $marc_item_fields_to_order->{holdingbranch};
596
        my $item_holdingbranch        = $marc_item_fields_to_order->{holdingbranch};
Lines 550-556 sub add_items_from_import_record { Link Here
550
        my $item_notforloan           = $marc_item_fields_to_order->{notforloan};
602
        my $item_notforloan           = $marc_item_fields_to_order->{notforloan};
551
        my $item_uri                  = $marc_item_fields_to_order->{uri};
603
        my $item_uri                  = $marc_item_fields_to_order->{uri};
552
        my $item_copyno               = $marc_item_fields_to_order->{copyno};
604
        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;
554
        my $item_budget_code          = $marc_item_fields_to_order->{budget_code};
606
        my $item_budget_code          = $marc_item_fields_to_order->{budget_code};
555
        my $item_budget_id;
607
        my $item_budget_id;
556
        if ( $marc_item_fields_to_order->{budget_code} ) {
608
        if ( $marc_item_fields_to_order->{budget_code} ) {
Lines 566-578 sub add_items_from_import_record { Link Here
566
        my $item_price             = $marc_item_fields_to_order->{price};
618
        my $item_price             = $marc_item_fields_to_order->{price};
567
        my $item_replacement_price = $marc_item_fields_to_order->{replacementprice};
619
        my $item_replacement_price = $marc_item_fields_to_order->{replacementprice};
568
        my $item_callnumber        = $marc_item_fields_to_order->{itemcallnumber};
620
        my $item_callnumber        = $marc_item_fields_to_order->{itemcallnumber};
569
621
        my $itemcreation           = 0;
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
        }
574
622
575
        for (my $i = 0; $i < $item_quantity; $i++) {
623
        for (my $i = 0; $i < $item_quantity; $i++) {
624
            $itemcreation = 1;
576
            my $item = Koha::Item->new({
625
            my $item = Koha::Item->new({
577
                biblionumber          => $biblionumber,
626
                biblionumber          => $biblionumber,
578
                homebranch            => $item_homebranch,
627
                homebranch            => $item_homebranch,
Lines 592-599 sub add_items_from_import_record { Link Here
592
641
593
            my %order_detail_hash = (
642
            my %order_detail_hash = (
594
                biblionumber => $biblionumber,
643
                biblionumber => $biblionumber,
595
                itemnumbers  => ($item->itemnumber),
596
                basketno     => $basket_id,
644
                basketno     => $basket_id,
645
                itemnumbers   => ($item->itemnumber),
597
                quantity     => 1,
646
                quantity     => 1,
598
                budget_id    => $item_budget_id,
647
                budget_id    => $item_budget_id,
599
                currency     => $vendor->listprice,
648
                currency     => $vendor->listprice,
Lines 615-620 sub add_items_from_import_record { Link Here
615
664
616
            push @order_line_details, \%order_detail_hash;
665
            push @order_line_details, \%order_detail_hash;
617
        }
666
        }
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
        }
618
    }
695
    }
619
696
620
    if($agent eq 'client') {
697
    if($agent eq 'client') {
Lines 750-756 sub create_order_lines { Link Here
750
    my $order_line_details = $args->{order_line_details};
827
    my $order_line_details = $args->{order_line_details};
751
828
752
    foreach  my $order_detail ( @{ $order_line_details } ) {
829
    foreach  my $order_detail ( @{ $order_line_details } ) {
753
        my @itemnumbers = $order_detail->{itemnumbers};
830
        my @itemnumbers = $order_detail->{itemnumbers} || ();
754
        delete($order_detail->{itemnumber});
831
        delete($order_detail->{itemnumber});
755
        my $order = Koha::Acquisition::Order->new( \%{ $order_detail } );
832
        my $order = Koha::Acquisition::Order->new( \%{ $order_detail } );
756
        $order->populate_with_prices_for_ordering();
833
        $order->populate_with_prices_for_ordering();
(-)a/misc/cronjobs/marc_ordering_process.pl (-2 / +5 lines)
Lines 100-105 foreach my $acct ( @accounts ) { Link Here
100
    opendir my $dir, $working_dir or die "Can't open filepath";
100
    opendir my $dir, $working_dir or die "Can't open filepath";
101
    my @files = grep { /\.(mrc|marcxml|mrk)/i } readdir $dir;
101
    my @files = grep { /\.(mrc|marcxml|mrk)/i } readdir $dir;
102
    closedir $dir;
102
    closedir $dir;
103
    print "No new files found\n" if scalar(@files) == 0;
104
105
    my $files_processed = 0;
103
106
104
    foreach my $filename ( @files ) {
107
    foreach my $filename ( @files ) {
105
        say sprintf "Creating order lines from file %s", $filename if $verbose;
108
        say sprintf "Creating order lines from file %s", $filename if $verbose;
Lines 113-118 foreach my $acct ( @accounts ) { Link Here
113
            };
116
            };
114
            my $result = Koha::MarcOrder->create_order_lines_from_file($args);
117
            my $result = Koha::MarcOrder->create_order_lines_from_file($args);
115
            if($result->{success}) {
118
            if($result->{success}) {
119
                $files_processed++;
116
                say sprintf "Successfully processed file: %s", $filename if $verbose;
120
                say sprintf "Successfully processed file: %s", $filename if $verbose;
117
                unlink $full_path;
121
                unlink $full_path;
118
            } else {
122
            } else {
Lines 121-127 foreach my $acct ( @accounts ) { Link Here
121
            };
125
            };
122
        }
126
        }
123
    }
127
    }
124
    print "All files completed\n";
128
    say sprintf "%s files processed", $files_processed unless $files_processed == 0;
125
    print "Moving to next account\n\n";
129
    print "Moving to next account\n\n";
126
}
130
}
127
print "Process complete\n";
131
print "Process complete\n";
128
- 

Return to bug 34355