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

(-)a/C4/Items.pm (-5 / +8 lines)
Lines 1262-1268 sub _find_value { Link Here
1262
1262
1263
=head2 PrepareItemrecordDisplay
1263
=head2 PrepareItemrecordDisplay
1264
1264
1265
  PrepareItemrecordDisplay($bibnum,$itemumber,$defaultvalues,$frameworkcode);
1265
  PrepareItemrecordDisplay($bibnum,$itemumber,$defaultvalues,$frameworkcode, $use_defaults);
1266
1266
1267
Returns a hash with all the fields for Display a given item data in a template
1267
Returns a hash with all the fields for Display a given item data in a template
1268
1268
Lines 1270-1280 $defaultvalues should either contain a hashref of values for the new item, or be Link Here
1270
1270
1271
The $frameworkcode returns the item for the given frameworkcode, ONLY if bibnum is not provided
1271
The $frameworkcode returns the item for the given frameworkcode, ONLY if bibnum is not provided
1272
1272
1273
If $use_defaults is true, the returned item values will be generated from the default values only.
1274
1273
=cut
1275
=cut
1274
1276
1275
sub PrepareItemrecordDisplay {
1277
sub PrepareItemrecordDisplay {
1276
1278
1277
    my ( $bibnum, $itemnum, $defaultvalues, $frameworkcode ) = @_;
1279
    my ( $bibnum, $itemnum, $defaultvalues, $frameworkcode, $use_defaults ) = @_;
1278
1280
1279
    my $dbh = C4::Context->dbh;
1281
    my $dbh = C4::Context->dbh;
1280
    $frameworkcode = C4::Biblio::GetFrameworkCode($bibnum) if $bibnum;
1282
    $frameworkcode = C4::Biblio::GetFrameworkCode($bibnum) if $bibnum;
Lines 1294-1304 sub PrepareItemrecordDisplay { Link Here
1294
    # return nothing if we don't have found an existing framework.
1296
    # return nothing if we don't have found an existing framework.
1295
    return q{} unless $tagslib;
1297
    return q{} unless $tagslib;
1296
    my $itemrecord;
1298
    my $itemrecord;
1297
    if ($itemnum) {
1299
    if ($use_defaults) {
1298
        $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum );
1299
    }elsif ($defaultvalues && $defaultvalues->{'itemrecord'} ) {
1300
        $itemrecord = $defaultvalues->{'itemrecord'};
1300
        $itemrecord = $defaultvalues->{'itemrecord'};
1301
    }
1301
    }
1302
    elsif ($itemnum) {
1303
        $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum );
1304
    }
1302
    my @loop_data;
1305
    my @loop_data;
1303
1306
1304
    my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
1307
    my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
(-)a/Koha/Acquisition/Utils.pm (+176 lines)
Line 0 Link Here
1
package Koha::Acquisition::Utils;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use YAML::XS;
21
22
use C4::Context;
23
24
=head1 NAME
25
26
Koha::Acquisition::Utils - Additional Koha functions for dealing with orders and acquisitions
27
28
=head2 SUBROUTINES
29
30
=head3 GetMarcFieldsToOrderValues($syspref_name, $record, $field_list)
31
32
my $data = Koha::Acquisition::Utils::GetMarcFieldsToOrderValues('MarcFieldsToOrder', $marcrecord, ['price', 'quantity', 'budget_code', etc.]);
33
34
The return value is a hashref of key value pairs, where the keys are the field list parameters,
35
and the values are extracted from the MARC record based on the key to MARC field mapping from the
36
system preference MarcFieldsToOrder.
37
38
=cut
39
40
sub GetMarcFieldsToOrderValues {
41
    my ($record, $field_list) = @_;
42
    my $syspref = C4::Context->preference('MarcFieldsToOrder');
43
    $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
44
    my $yaml = eval {
45
        YAML::XS::Load($syspref);
46
    };
47
    if ( $@ ) {
48
        warn "Unable to parse $syspref syspref : $@";
49
        return ();
50
    }
51
    my $r;
52
    for my $field_name ( @$field_list ) {
53
        next unless exists $yaml->{$field_name};
54
        my @fields = split /\|/, $yaml->{$field_name};
55
        for my $field ( @fields ) {
56
            my ( $f, $sf ) = split /\$/, $field;
57
            next unless $f and $sf;
58
            if ( my $v = $record->subfield( $f, $sf ) ) {
59
                $r->{$field_name} = $v;
60
            }
61
            last if $yaml->{$field};
62
        }
63
    }
64
    return $r;
65
}
66
67
=head3 GetMarcItemFieldsToOrderValues($syspref_name, $record, $field_list)
68
69
my $data = GetMarcItemFieldsToOrderValues('MarcItemFieldsToOrder', $marcrecord, ['homebranch', 'holdingbranch', 'itype', 'nonpublic_note', 'public_note', 'loc', 'ccode', 'notforloan', 'uri', 'copyno', 'price', 'replacementprice', 'itemcallnumber', 'quantity', 'budget_code']);
70
71
The return value is a hashref of key value pairs, where the keys are the field list parameters,
72
and the values are extracted from the MARC record based on the key to MARC field mapping from the
73
system preference MarcFieldsToOrder.
74
75
The largest difference between GetMarcFieldsToOrderValues and GetMarcItemFieldsToOrderValues is that the former deals
76
with singular marc fields, while the latter works on multiple matching marc fields and returns -1 if it cannot
77
find a matching number of all fields to be looked up.
78
79
=cut
80
81
sub GetMarcItemFieldsToOrderValues {
82
    my ($record, $field_list) = @_;
83
    my $syspref = C4::Context->preference('MarcItemFieldsToOrder');
84
    $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
85
    my $yaml = eval {
86
        YAML::XS::Load($syspref);
87
    };
88
    if ( $@ ) {
89
        warn "Unable to parse $syspref syspref : $@";
90
        return ();
91
    }
92
    my @result;
93
    my @tags_list;
94
95
    # Check tags in syspref definition
96
    for my $field_name ( @$field_list ) {
97
        next unless exists $yaml->{$field_name};
98
        my @fields = split /\|/, $yaml->{$field_name};
99
        for my $field ( @fields ) {
100
            my ( $f, $sf ) = split /\$/, $field;
101
            next unless $f and $sf;
102
            push @tags_list, $f;
103
        }
104
    }
105
    @tags_list = List::MoreUtils::uniq(@tags_list);
106
107
    my $tags_count = equal_number_of_fields(\@tags_list, $record);
108
    # Return if the number of these fields in the record is not the same.
109
    return -1 if $tags_count == -1;
110
111
    # Gather the fields
112
    my $fields_hash;
113
    foreach my $tag (@tags_list) {
114
        my @tmp_fields;
115
        foreach my $field ($record->field($tag)) {
116
            push @tmp_fields, $field;
117
        }
118
        $fields_hash->{$tag} = \@tmp_fields;
119
    }
120
121
    for (my $i = 0; $i < $tags_count; $i++) {
122
        my $r;
123
        for my $field_name ( @$field_list ) {
124
            next unless exists $yaml->{$field_name};
125
            my @fields = split /\|/, $yaml->{$field_name};
126
            for my $field ( @fields ) {
127
                my ( $f, $sf ) = split /\$/, $field;
128
                next unless $f and $sf;
129
                my $v = $fields_hash->{$f}[$i] ? $fields_hash->{$f}[$i]->subfield( $sf ) : undef;
130
                $r->{$field_name} = $v if (defined $v);
131
                last if $yaml->{$field};
132
            }
133
        }
134
        push @result, $r;
135
    }
136
    return \@result;
137
}
138
139
=head3 equal_number_of_fields($tags_list, $record)
140
141
$value = equal_number_of_fields(\@tags_list, $record);
142
143
Returns -1 if the number of instances of the given tags are not equal.
144
145
For example, if you need to verify there are equal 975$i's and 975$a's,
146
this will let you know.
147
148
=cut
149
150
sub equal_number_of_fields {
151
    my ($tags_list, $record) = @_;
152
    my $tag_fields_count;
153
    for my $tag (@$tags_list) {
154
        my @fields = $record->field($tag);
155
        $tag_fields_count->{$tag} = scalar @fields;
156
    }
157
158
    my $tags_count;
159
    foreach my $key ( keys %$tag_fields_count ) {
160
        if ( $tag_fields_count->{$key} > 0 ) { # Having 0 of a field is ok
161
            $tags_count //= $tag_fields_count->{$key}; # Start with the count from the first occurrence
162
            return -1 if $tag_fields_count->{$key} != $tags_count; # All counts of various fields should be equal if they exist
163
        }
164
    }
165
166
    return $tags_count;
167
}
168
169
1;
170
__END__
171
172
=head1 AUTHOR
173
174
Koha Development Team <http://koha-community.org/>
175
176
=cut
(-)a/acqui/addorderiso2709.pl (-110 / +9 lines)
Lines 44-57 use C4::Items qw( PrepareItemrecordDisplay AddItemFromMarc ); Link Here
44
use C4::Budgets qw( GetBudget GetBudgets GetBudgetHierarchy CanUserUseBudget GetBudgetByCode );
44
use C4::Budgets qw( GetBudget GetBudgets GetBudgetHierarchy CanUserUseBudget GetBudgetByCode );
45
use C4::Suggestions;    # GetSuggestion
45
use C4::Suggestions;    # GetSuggestion
46
use C4::Members;
46
use C4::Members;
47
47
use C4::Output;
48
use Koha::Number::Price;
48
use C4::Search qw/FindDuplicate/;
49
use Koha::Libraries;
49
use C4::Suggestions;    # GetSuggestion
50
use Koha::Acquisition::Baskets;
50
use Koha::Acquisition::Baskets;
51
use Koha::Acquisition::Booksellers;
51
use Koha::Acquisition::Currencies;
52
use Koha::Acquisition::Currencies;
52
use Koha::Acquisition::Orders;
53
use Koha::Acquisition::Orders;
53
use Koha::Acquisition::Booksellers;
54
use Koha::Acquisition::Utils;
54
use Koha::Import::Records;
55
use Koha::Import::Records;
56
use Koha::Libraries;
57
use Koha::Number::Price;
55
use Koha::Patrons;
58
use Koha::Patrons;
56
59
57
my $input = CGI->new;
60
my $input = CGI->new;
Lines 489-495 sub import_biblios_list { Link Here
489
        );
492
        );
490
        my $marcrecord = $import_record->get_marc_record || die "couldn't translate marc information";
493
        my $marcrecord = $import_record->get_marc_record || die "couldn't translate marc information";
491
494
492
        my $infos = get_infos_syspref('MarcFieldsToOrder', $marcrecord, ['price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2','replacementprice']);
495
        my $infos = Koha::Acquisition::Utils::GetMarcFieldsToOrderValues( $marcrecord, [ 'price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2', 'replacementprice' ] );
493
        my $price = $infos->{price};
496
        my $price = $infos->{price};
494
        my $replacementprice = $infos->{replacementprice};
497
        my $replacementprice = $infos->{replacementprice};
495
        my $quantity = $infos->{quantity};
498
        my $quantity = $infos->{quantity};
Lines 508-514 sub import_biblios_list { Link Here
508
        # Items
511
        # Items
509
        my @itemlist = ();
512
        my @itemlist = ();
510
        my $all_items_quantity = 0;
513
        my $all_items_quantity = 0;
511
        my $alliteminfos = get_infos_syspref_on_item('MarcItemFieldsToOrder', $marcrecord, ['homebranch', 'holdingbranch', 'itype', 'nonpublic_note', 'public_note', 'loc', 'ccode', 'notforloan', 'uri', 'copyno', 'price', 'replacementprice', 'itemcallnumber', 'quantity', 'budget_code']);
514
        my $alliteminfos = Koha::Acquisition::Utils::GetMarcItemFieldsToOrderValues( $marcrecord, [ 'homebranch', 'holdingbranch', 'itype', 'nonpublic_note', 'public_note', 'loc', 'ccode', 'notforloan', 'uri', 'copyno', 'price', 'replacementprice', 'itemcallnumber', 'quantity', 'budget_code' ] );
512
        if ($alliteminfos != -1) {
515
        if ($alliteminfos != -1) {
513
            foreach my $iteminfos (@$alliteminfos) {
516
            foreach my $iteminfos (@$alliteminfos) {
514
                my $item_homebranch = $iteminfos->{homebranch};
517
                my $item_homebranch = $iteminfos->{homebranch};
Lines 643-749 sub add_matcher_list { Link Here
643
    }
646
    }
644
    $template->param(available_matchers => \@matchers);
647
    $template->param(available_matchers => \@matchers);
645
}
648
}
646
647
sub get_infos_syspref {
648
    my ($syspref_name, $record, $field_list) = @_;
649
    my $syspref = C4::Context->preference($syspref_name);
650
    $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
651
    my $yaml = eval {
652
        YAML::XS::Load(Encode::encode_utf8($syspref));
653
    };
654
    if ( $@ ) {
655
        warn "Unable to parse $syspref syspref : $@";
656
        return ();
657
    }
658
    my $r;
659
    for my $field_name ( @$field_list ) {
660
        next unless exists $yaml->{$field_name};
661
        my @fields = split /\|/, $yaml->{$field_name};
662
        for my $field ( @fields ) {
663
            my ( $f, $sf ) = split /\$/, $field;
664
            next unless $f and $sf;
665
            if ( my $v = $record->subfield( $f, $sf ) ) {
666
                $r->{$field_name} = $v;
667
            }
668
            last if $yaml->{$field};
669
        }
670
    }
671
    return $r;
672
}
673
674
sub equal_number_of_fields {
675
    my ($tags_list, $record) = @_;
676
    my $tag_fields_count;
677
    for my $tag (@$tags_list) {
678
        my @fields = $record->field($tag);
679
        $tag_fields_count->{$tag} = scalar @fields;
680
    }
681
682
    my $tags_count;
683
    foreach my $key ( keys %$tag_fields_count ) {
684
        if ( $tag_fields_count->{$key} > 0 ) { # Having 0 of a field is ok
685
            $tags_count //= $tag_fields_count->{$key}; # Start with the count from the first occurrence
686
            return -1 if $tag_fields_count->{$key} != $tags_count; # All counts of various fields should be equal if they exist
687
        }
688
    }
689
690
    return $tags_count;
691
}
692
693
sub get_infos_syspref_on_item {
694
    my ($syspref_name, $record, $field_list) = @_;
695
    my $syspref = C4::Context->preference($syspref_name);
696
    $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
697
    my $yaml = eval {
698
        YAML::XS::Load(Encode::encode_utf8($syspref));
699
    };
700
    if ( $@ ) {
701
        warn "Unable to parse $syspref syspref : $@";
702
        return ();
703
    }
704
    my @result;
705
    my @tags_list;
706
707
    # Check tags in syspref definition
708
    for my $field_name ( @$field_list ) {
709
        next unless exists $yaml->{$field_name};
710
        my @fields = split /\|/, $yaml->{$field_name};
711
        for my $field ( @fields ) {
712
            my ( $f, $sf ) = split /\$/, $field;
713
            next unless $f and $sf;
714
            push @tags_list, $f;
715
        }
716
    }
717
    @tags_list = List::MoreUtils::uniq(@tags_list);
718
719
    my $tags_count = equal_number_of_fields(\@tags_list, $record);
720
    # Return if the number of these fields in the record is not the same.
721
    return -1 if $tags_count == -1;
722
723
    # Gather the fields
724
    my $fields_hash;
725
    foreach my $tag (@tags_list) {
726
        my @tmp_fields;
727
        foreach my $field ($record->field($tag)) {
728
            push @tmp_fields, $field;
729
        }
730
        $fields_hash->{$tag} = \@tmp_fields;
731
    }
732
733
    for (my $i = 0; $i < $tags_count; $i++) {
734
        my $r;
735
        for my $field_name ( @$field_list ) {
736
            next unless exists $yaml->{$field_name};
737
            my @fields = split /\|/, $yaml->{$field_name};
738
            for my $field ( @fields ) {
739
                my ( $f, $sf ) = split /\$/, $field;
740
                next unless $f and $sf;
741
                my $v = $fields_hash->{$f}[$i] ? $fields_hash->{$f}[$i]->subfield( $sf ) : undef;
742
                $r->{$field_name} = $v if (defined $v);
743
                last if $yaml->{$field};
744
            }
745
        }
746
        push @result, $r;
747
    }
748
    return \@result;
749
}
(-)a/acqui/neworderempty.pl (-112 / +10 lines)
Lines 66-72 the item's id in the breeding reservoir Link Here
66
66
67
use Modern::Perl;
67
use Modern::Perl;
68
use CGI qw ( -utf8 );
68
use CGI qw ( -utf8 );
69
use C4::Context;
70
69
71
use C4::Auth qw( get_template_and_user );
70
use C4::Auth qw( get_template_and_user );
72
use C4::Budgets qw( GetBudget GetBudgetHierarchy CanUserUseBudget );
71
use C4::Budgets qw( GetBudget GetBudgetHierarchy CanUserUseBudget );
Lines 85-90 use C4::Biblio qw( Link Here
85
use C4::Output qw( output_and_exit output_html_with_http_headers );
84
use C4::Output qw( output_and_exit output_html_with_http_headers );
86
use C4::Members;
85
use C4::Members;
87
use C4::Search qw( FindDuplicate );
86
use C4::Search qw( FindDuplicate );
87
use C4::Items qw( PrepareItemrecordDisplay );
88
88
89
#needed for z3950 import:
89
#needed for z3950 import:
90
use C4::ImportBatch qw( SetImportRecordStatus SetMatchedBiblionumber GetImportRecordMarc );
90
use C4::ImportBatch qw( SetImportRecordStatus SetMatchedBiblionumber GetImportRecordMarc );
Lines 92-101 use C4::ImportBatch qw( SetImportRecordStatus SetMatchedBiblionumber GetImportRe Link Here
92
use Koha::Acquisition::Booksellers;
92
use Koha::Acquisition::Booksellers;
93
use Koha::Acquisition::Currencies qw( get_active );
93
use Koha::Acquisition::Currencies qw( get_active );
94
use Koha::Biblios;
94
use Koha::Biblios;
95
use Koha::Acquisition::Utils;
95
use Koha::BiblioFrameworks;
96
use Koha::BiblioFrameworks;
96
use Koha::DateUtils qw( dt_from_string );
97
use Koha::DateUtils qw( dt_from_string );
97
use Koha::MarcSubfieldStructures;
98
use Koha::ItemTypes;
98
use Koha::ItemTypes;
99
use Koha::MarcSubfieldStructures;
99
use Koha::Patrons;
100
use Koha::Patrons;
100
use Koha::RecordProcessor;
101
use Koha::RecordProcessor;
101
use Koha::Subscriptions;
102
use Koha::Subscriptions;
Lines 629-635 sub staged_items_field { Link Here
629
    my ($marcrecord, $encoding) = MARCfindbreeding($breedingid);
630
    my ($marcrecord, $encoding) = MARCfindbreeding($breedingid);
630
    die("Could not find the selected record in the reservoir, bailing") unless $marcrecord;
631
    die("Could not find the selected record in the reservoir, bailing") unless $marcrecord;
631
632
632
    my $infos = get_infos_syspref('MarcFieldsToOrder', $marcrecord, ['price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2', 'replacementprice']);
633
    my $infos = Koha::Acquisition::Utils::GetMarcFieldsToOrderValues( $marcrecord, [ 'price', 'quantity', 'budget_code', 'discount', 'sort1', 'sort2', 'replacementprice' ] );
633
    my $price = $infos->{price};
634
    my $price = $infos->{price};
634
    my $replacementprice = $infos->{replacementprice};
635
    my $replacementprice = $infos->{replacementprice};
635
    my $quantity = $infos->{quantity};
636
    my $quantity = $infos->{quantity};
Lines 647-653 sub staged_items_field { Link Here
647
    # Items
648
    # Items
648
    my @itemlist = ();
649
    my @itemlist = ();
649
    my $all_items_quantity = 0;
650
    my $all_items_quantity = 0;
650
    my $alliteminfos = get_infos_syspref_on_item('MarcItemFieldsToOrder', $marcrecord, ['homebranch', 'holdingbranch', 'itype', 'nonpublic_note', 'public_note', 'loc', 'ccode', 'notforloan', 'uri', 'copyno', 'price', 'replacementprice', 'itemcallnumber', 'quantity', 'budget_code']);
651
    my $alliteminfos = Koha::Acquisition::Utils::GetMarcItemFieldsToOrderValues( $marcrecord, [ 'homebranch', 'holdingbranch', 'itype', 'nonpublic_note', 'public_note', 'loc', 'ccode', 'notforloan', 'uri', 'copyno', 'price', 'replacementprice', 'itemcallnumber', 'quantity', 'budget_code' ] );
651
    if ($alliteminfos != -1) {
652
    if ($alliteminfos != -1) {
652
        foreach my $iteminfos (@$alliteminfos) {
653
        foreach my $iteminfos (@$alliteminfos) {
653
            my %itemrecord=(
654
            my %itemrecord=(
Lines 674-680 sub staged_items_field { Link Here
674
                    'branchcode'=>_trim($iteminfos->{homebranch})
675
                    'branchcode'=>_trim($iteminfos->{homebranch})
675
                );
676
                );
676
                $all_items_quantity++;
677
                $all_items_quantity++;
677
                my $itemprocessed = PrepareItemrecordDisplay('', '', \%defaultvalues , 'ACQ');
678
                my $itemprocessed = PrepareItemrecordDisplay('', '', \%defaultvalues , 'ACQ', 1);
678
                push @itemlist, $itemprocessed->{'iteminformation'};
679
                push @itemlist, $itemprocessed->{'iteminformation'};
679
            }
680
            }
680
        }
681
        }
Lines 696-808 sub staged_items_field { Link Here
696
    return (\%cellrecord);
697
    return (\%cellrecord);
697
}
698
}
698
699
699
sub get_infos_syspref {
700
    my ($syspref_name, $record, $field_list) = @_;
701
    my $syspref = C4::Context->preference($syspref_name);
702
    $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
703
    my $yaml = eval {
704
        YAML::Load($syspref);
705
    };
706
    if ( $@ ) {
707
        warn "Unable to parse $syspref syspref : $@";
708
        return ();
709
    }
710
    my $r;
711
    for my $field_name ( @$field_list ) {
712
        next unless exists $yaml->{$field_name};
713
        my @fields = split /\|/, $yaml->{$field_name};
714
        for my $field ( @fields ) {
715
            my ( $f, $sf ) = split /\$/, $field;
716
            next unless $f and $sf;
717
            if ( my $v = $record->subfield( $f, $sf ) ) {
718
                $r->{$field_name} = $v;
719
            }
720
            last if $yaml->{$field};
721
        }
722
    }
723
    return $r;
724
}
725
726
sub equal_number_of_fields {
727
    my ($tags_list, $record) = @_;
728
    my $tag_fields_count;
729
    for my $tag (@$tags_list) {
730
        my @fields = $record->field($tag);
731
        $tag_fields_count->{$tag} = scalar @fields;
732
    }
733
734
    my $tags_count;
735
    foreach my $key ( keys %$tag_fields_count ) {
736
        if ( $tag_fields_count->{$key} > 0 ) { # Having 0 of a field is ok
737
            $tags_count //= $tag_fields_count->{$key}; # Start with the count from the first occurrence
738
            return -1 if $tag_fields_count->{$key} != $tags_count; # All counts of various fields should be equal if they exist
739
        }
740
    }
741
742
    return $tags_count;
743
}
744
745
sub get_infos_syspref_on_item {
746
    my ($syspref_name, $record, $field_list) = @_;
747
    my $syspref = C4::Context->preference($syspref_name);
748
    $syspref = "$syspref\n\n"; # YAML is anal on ending \n. Surplus does not hurt
749
    my $yaml = eval {
750
        YAML::Load($syspref);
751
    };
752
    if ( $@ ) {
753
        warn "Unable to parse $syspref syspref : $@";
754
        return ();
755
    }
756
    my @result;
757
    my @tags_list;
758
759
    # Check tags in syspref definition
760
    for my $field_name ( @$field_list ) {
761
        next unless exists $yaml->{$field_name};
762
        my @fields = split /\|/, $yaml->{$field_name};
763
        for my $field ( @fields ) {
764
            my ( $f, $sf ) = split /\$/, $field;
765
            next unless $f and $sf;
766
            push @tags_list, $f;
767
        }
768
    }
769
    @tags_list = List::MoreUtils::uniq(@tags_list);
770
771
    my $tags_count = equal_number_of_fields(\@tags_list, $record);
772
    # Return if the number of these fields in the record is not the same.
773
    return -1 if $tags_count == -1;
774
775
    # Gather the fields
776
    my $fields_hash;
777
    foreach my $tag (@tags_list) {
778
        my @tmp_fields;
779
        foreach my $field ($record->field($tag)) {
780
            push @tmp_fields, $field;
781
        }
782
        $fields_hash->{$tag} = \@tmp_fields;
783
    }
784
785
    for (my $i = 0; $i < $tags_count; $i++) {
786
        my $r;
787
        for my $field_name ( @$field_list ) {
788
            next unless exists $yaml->{$field_name};
789
            my @fields = split /\|/, $yaml->{$field_name};
790
            for my $field ( @fields ) {
791
                my ( $f, $sf ) = split /\$/, $field;
792
                next unless $f and $sf;
793
                my $v = $fields_hash->{$f}[$i] ? $fields_hash->{$f}[$i]->subfield( $sf ) : undef;
794
                $r->{$field_name} = $v if (defined $v);
795
                last if $yaml->{$field};
796
            }
797
        }
798
        push @result, $r;
799
    }
800
    return \@result;
801
}
802
700
803
sub _trim {
701
sub _trim {
804
    return $_[0] unless $_[0];
702
    my $string = shift;
805
    $_[0] =~ s/^\s+//;
703
    return unless $string;
806
    $_[0] =~ s/\s+$//;
704
    $string =~ s/^\s+|\s+$//g;
807
    $_[0];
705
    return $string // q{};
808
}
706
}
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/neworderempty.tt (-1 / +1 lines)
Lines 372-378 Link Here
372
                        <input class="addItemControl cancel" name="buttonClear" style="cursor:pointer;" onclick="clearItemBlock(this)" value="Clear" type="button">
372
                        <input class="addItemControl cancel" name="buttonClear" style="cursor:pointer;" onclick="clearItemBlock(this)" value="Clear" type="button">
373
                        <input class="addItemControl" name="buttonPlusMulti" onclick="javascript:this.nextElementSibling.style.display='inline'; return false;" style="cursor:pointer; margin:0 1em;" value="Add multiple items" type="button">
373
                        <input class="addItemControl" name="buttonPlusMulti" onclick="javascript:this.nextElementSibling.style.display='inline'; return false;" style="cursor:pointer; margin:0 1em;" value="Add multiple items" type="button">
374
                        <span id="add_multiple_copies" style="display:none">
374
                        <span id="add_multiple_copies" style="display:none">
375
                            <input class="addItemControl" id="multiValue" name="multiValue" placeholder="Number of items to add" type="number">
375
                            <input class="addItemControl" id="multiValue" name="multiValue" placeholder="Number of items to add" type="text" inputmode="numeric" pattern="[0-9]*">
376
                            <input class="addItemControl" name="buttonAddMulti&quot;" style="cursor:pointer; margin:0 1em;" onclick="checkCount( this ,'[% UniqueItemFields | html %]')" value="Add" type="button">
376
                            <input class="addItemControl" name="buttonAddMulti&quot;" style="cursor:pointer; margin:0 1em;" onclick="checkCount( this ,'[% UniqueItemFields | html %]')" value="Add" type="button">
377
                            <div class="dialog message">NOTE: Fields listed in the 'UniqueItemsFields' system preference will not be copied</div>
377
                            <div class="dialog message">NOTE: Fields listed in the 'UniqueItemsFields' system preference will not be copied</div>
378
                        </span>
378
                        </span>
(-)a/t/Acquisition/Utils.t (-1 / +158 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
use utf8;
20
21
use Test::More tests => 4;
22
use Test::MockModule;
23
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
26
27
use MARC::Record;
28
29
use_ok('Koha::Acquisition::Utils');
30
31
my $schema = Koha::Database->schema;
32
$schema->storage->txn_begin;
33
my $builder = t::lib::TestBuilder->new;
34
my $dbh = C4::Context->dbh;
35
36
subtest "GetMarcFieldsToOrderValues" => sub {
37
    plan tests => 4;
38
39
    my $record = MARC::Record->new;
40
    $record->append_fields(
41
        MARC::Field->new( '500', '', '', a => 'Test 1' ),
42
        MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ),
43
        MARC::Field->new( '520', '', '', a => 'Test 3' ),
44
        MARC::Field->new( '541', '', '', a => 'Test 4' ),
45
    );
46
47
    my $MarcFieldsToOrder = q{
48
test1: 500$a
49
test2: 505$a
50
test3: 520$a
51
test4: 541$a
52
    };
53
    t::lib::Mocks::mock_preference('MarcFieldsToOrder', $MarcFieldsToOrder);
54
    my $data = Koha::Acquisition::Utils::GetMarcFieldsToOrderValues(
55
        $record,
56
        [ 'test1', 'test2', 'test3', 'test4' ]
57
    );
58
59
    is( $data->{test1}, "Test 1", "Got test 1 correctly" );
60
    is( $data->{test2}, "Test 2", "Got test 2 correctly" );
61
    is( $data->{test3}, "Test 3", "Got test 3 correctly" );
62
    is( $data->{test4}, "Test 4", "Got test 4 correctly" );
63
};
64
65
subtest "GetMarcItemFieldsToOrderValues" => sub {
66
    plan tests => 13;
67
68
    my $record = MARC::Record->new;
69
    $record->append_fields(
70
        MARC::Field->new( '500', '', '', a => 'Test 1' ),
71
        MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ),
72
        MARC::Field->new( '975', '', '', a => 'Test 3', b => "Test 4" ),
73
        MARC::Field->new( '975', '', '', a => 'Test 5', b => "Test 6" ),
74
        MARC::Field->new( '975', '', '', a => 'Test 7', b => "Test 8" ),
75
        MARC::Field->new( '976', '', '', a => 'Test 9', b => "Test 10" ),
76
        MARC::Field->new( '976', '', '', a => 'Test 11', b => "Test 12" ),
77
        MARC::Field->new( '976', '', '', a => 'Test 13', b => "Test 14" ),
78
    );
79
80
    my $MarcItemFieldsToOrder = q{
81
testA: 975$a
82
testB: 975$b
83
testC: 976$a
84
testD: 976$b
85
    };
86
    t::lib::Mocks::mock_preference('MarcItemFieldsToOrder', $MarcItemFieldsToOrder);
87
    my $data = Koha::Acquisition::Utils::GetMarcItemFieldsToOrderValues(
88
        $record,
89
        [ 'testA', 'testB', 'testC', 'testD' ]
90
    );
91
92
    is( $data->[0]->{testA}, "Test 3", 'Got first 975$a correctly' );
93
    is( $data->[0]->{testB}, "Test 4", 'Got first 975$b correctly' );
94
    is( $data->[1]->{testA}, "Test 5", 'Got second 975$a correctly' );
95
    is( $data->[1]->{testB}, "Test 6", 'Got second 975$b correctly' );
96
    is( $data->[2]->{testA}, "Test 7", 'Got third 975$a correctly' );
97
    is( $data->[2]->{testB}, "Test 8", 'Got third 975$b correctly' );
98
99
    is( $data->[0]->{testC}, "Test 9", 'Got first 976$a correctly' );
100
    is( $data->[0]->{testD}, "Test 10", 'Got first 976$b correctly' );
101
    is( $data->[1]->{testC}, "Test 11", 'Got second 976$a correctly' );
102
    is( $data->[1]->{testD}, "Test 12", 'Got second 976$b correctly' );
103
    is( $data->[2]->{testC}, "Test 13", 'Got third 976$a correctly' );
104
    is( $data->[2]->{testD}, "Test 14", 'Got third 976$b correctly' );
105
106
    # Test with bad record where fields are not one-to-one
107
    $record->append_fields(
108
        MARC::Field->new( '500', '', '', a => 'Test 1' ),
109
        MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ),
110
        MARC::Field->new( '975', '', '', a => 'Test 3', b => "Test 4" ),
111
        MARC::Field->new( '975', '', '', b => "Test 6" ),
112
        MARC::Field->new( '975', '', '', b => 'Test 7' ),
113
        MARC::Field->new( '976', '', '', a => 'Test 9', b => "Test 10" ),
114
        MARC::Field->new( '976', '', '', a => 'Test 11', b => "Test 12" ),
115
    );
116
117
    $data = Koha::Acquisition::Utils::GetMarcItemFieldsToOrderValues(
118
        $record,
119
        [ 'testA', 'testB', 'testC', 'testD' ]
120
    );
121
    is( $data, -1, "Got -1 if record fields are not one-to-one");
122
};
123
124
subtest "equal_number_of_fields" => sub {
125
    plan tests => 2;
126
127
    my $record = MARC::Record->new;
128
    $record->append_fields(
129
        MARC::Field->new( '500', '', '', a => 'Test 1' ),
130
        MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ),
131
        MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ),
132
        MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ),
133
        MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ),
134
        MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ),
135
        MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ),
136
        MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ),
137
    );
138
139
    my $data = Koha::Acquisition::Utils::equal_number_of_fields( [ '975', '976' ], $record );
140
    is( $data, '3', "Got correct number of fields in return value" );
141
142
    # Test with non-matching field sets
143
    $record->append_fields(
144
        MARC::Field->new( '500', '', '', a => 'Test 1' ),
145
        MARC::Field->new( '505', '', '', a => 'Test 2', u => 'http://example.com' ),
146
        MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ),
147
        MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ),
148
        MARC::Field->new( '975', '', '', a => 'Test a', b => "Test b" ),
149
        MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ),
150
        MARC::Field->new( '976', '', '', a => 'Test a', b => "Test b" ),
151
    );
152
153
    $data = Koha::Acquisition::Utils::equal_number_of_fields( [ '975', '976' ], $record );
154
    is( $data, '-1', "Got -1 in return value" );
155
};
156
157
$schema->storage->txn_rollback;
158
C4::Context->clear_syspref_cache();

Return to bug 20817