From 18300c23d210407584f9e194d45da249e1d08bef Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 7 Sep 2023 16:53:00 +0100 Subject: [PATCH] Bug 34737: Add ability to match on combinations of item fields This patch updates the sort bin selection logic to allow for multiple item field comparisons in a single config rule line. Test plan 1) Run the unit tests before and after applying this patch and confirm they pass in both cases (prove -v t/db_debendant/SIP/Transaction.t 2) Check out an item and return it via SIP2: $ telnet localhost 8023 9300CNterm1|COterm1|CPCPL| 09N20200422 12430020200422 124300APCPL|AOCPL|AB001|AC| (Where 001 in |AB001| is the barcode of the item in question) 4) Check there is no CL field in the last response 5) Apply the patch, fill in SIP2SortBinMapping with e.g.: CPL:itype:eq:BK:ccode:eq:TEEN:3 6) Repeat the first step 7) Check the response contains a CL field with a value of 3 (or what you put in the config). The field should look like |CL3| Signed-off-by: Toni Ford Signed-off-by: Kyle M Hall --- C4/SIP/ILS/Transaction/Checkin.pm | 78 ++++++++++++------- .../admin/preferences/circulation.pref | 6 +- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/C4/SIP/ILS/Transaction/Checkin.pm b/C4/SIP/ILS/Transaction/Checkin.pm index e512350be88..63e017018fd 100644 --- a/C4/SIP/ILS/Transaction/Checkin.pm +++ b/C4/SIP/ILS/Transaction/Checkin.pm @@ -216,11 +216,16 @@ The mapping should be: :::: +The field comparison triplet is repeatable, so you may include multiple sections + + :::: + For example: CPL:itype:eq:BOOK:1 CPL:location:eq:OFFICE:2 CPL:classmark:<:339.6:3 + CPL:itype:eq:BOOK:ccode:eq:TEEN:4 This will give: @@ -232,6 +237,8 @@ This will give: =item * sort_bin = "3" for items at the CPL branch with a classmark less than 339.6 +=item * sort_bin = "4" for items at the CPL branch with an itype of BOOK and a ccode of TEEN + =back Returns the ID of the appropriate sort_bin, if there is one, or undef. @@ -245,11 +252,12 @@ sub _get_sort_bin { return unless $item; my @lines; + # Mapping in SIP config takes precedence over syspref if ( my $mapping = $account->{sort_bin_mapping} ) { @lines = map { $_->{mapping} } @$mapping; - } - else { + } else { + # Get the mapping and split on newlines my $raw_map = C4::Context->preference('SIP2SortBinMapping'); return unless $raw_map; @@ -258,36 +266,54 @@ sub _get_sort_bin { # Iterate over the mapping. The first hit wins. my $rule = 0; - foreach my $line (@lines) { + RULE: foreach my $line (@lines) { + + my $match = 0; # Split the line into fields - my ( $branchcode, $item_property, $comparator, $value, $sort_bin ) = - split /:/, $line; - if ( $value =~ s/^\$// ) { - $value = $item->$value; - } - # Check the fields against values in the item - if ( $branch eq $branchcode ) { + my @fields = split /:/, $line; + + # Capture branchcode from first field + my $branchcode = shift @fields; + next RULE unless ( $branch eq $branchcode ); + + # Capture sort_bin from last field + my $sort_bin = pop @fields; + + # Capture rule sets + while ( my ( $item_property, $comparator, $value ) = splice( @fields, 0, 3 ) ) { + + # Skip badly formed rules + next RULE if ( !defined($item_property) || !defined($comparator) || !defined($value) ); + + if ( $value =~ s/^\$// ) { + $value = $item->$value; + } + + # Check the fields against values in the item my $property = $item->$item_property; if ( ( $comparator eq 'eq' || $comparator eq '=' ) && ( $property eq $value ) ) { - return $sort_bin; - } - if ( ( $comparator eq 'ne' || $comparator eq '!=' ) && ( $property ne $value ) ) { - return $sort_bin; - } - if ( ( $comparator eq '<' ) && ( $property < $value ) ) { - return $sort_bin; - } - if ( ( $comparator eq '>' ) && ( $property > $value ) ) { - return $sort_bin; - } - if ( ( $comparator eq '<=' ) && ( $property <= $value ) ) { - return $sort_bin; - } - if ( ( $comparator eq '>=' ) && ( $property >= $value ) ) { - return $sort_bin; + $match = 1; + } elsif ( ( $comparator eq 'ne' || $comparator eq '!=' ) && ( $property ne $value ) ) { + $match = 1; + } elsif ( ( $comparator eq '<' ) && ( $property < $value ) ) { + $match = 1; + } elsif ( ( $comparator eq '>' ) && ( $property > $value ) ) { + $match = 1; + } elsif ( ( $comparator eq '<=' ) && ( $property <= $value ) ) { + $match = 1; + } elsif ( ( $comparator eq '>=' ) && ( $property >= $value ) ) { + $match = 1; + } else { + + # No match, skip to next rule + next RULE; } } + + # Return sort bin if match + return $sort_bin if $match; + } # Return undef if no hits were found diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref index ba28859e031..c9e3b51880a 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref @@ -1333,12 +1333,14 @@ Circulation: - "- 'comparator' is the type of comparison, possible values are: eq,<,<=,>,>=,ne
" - "- 'item field value' is the value to compare against the value in the specified 'item field'
" - "- 'sort bin number' is the expected return value in the CL field of the SIP response for an item matching a rule

" + - "The field comparison triplet, 'item field:comparator:item field value', is repeatable within a single line to allow for more complex cases" - "NOTE: Specifying 'item_field_value' with a leading '\\$' and an item field name will use the value of that field in the item for comparison:
" - "i.e. \\$holdingbranch

" - "Examples:
" - "CPL:itype:eq:BOOK:1 - Will return sort bin 1 for an item of itemtype 'BOOK' returned to CPL.
" - - "CPL:itemcallnumber:<:339.6:3 - Will return sort bin 3 for an item with a callnumber less than 339.6 returned to CPL .
" - - "CPL:homebranch:ne:\\$holdingbranch:X - Will return sort bin 'X' for an item returned to CPL where the holdingbranch is not equal to the homebranch (i.e. any item belonging to a different branch than CPL).

" + - "CPL:itemcallnumber:<:339.6:3 - Will return sort bin 3 for an item with a callnumber less than 339.6 returned to CPL.
" + - "CPL:homebranch:ne:\\$holdingbranch:X - Will return sort bin 'X' for an item returned to CPL where the holdingbranch is not equal to the homebranch (i.e. any item belonging to a different branch than CPL).
" + - "CPL:itype:eq:BOOK:ccode:eq:TEEN:5 - Will return sort bin 5 for an item of itemtype 'BOOK' and a ccode 'TEEN' returned to CPL.

" - pref: SIP2SortBinMapping type: textarea class: long -- 2.41.0