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 <Toni.Ford@newcastle.gov.uk>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Martin Renvoize 2023-09-07 16:53:00 +01:00 committed by Tomas Cohen Arazi
parent 6296658e86
commit 1a633d68da
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
2 changed files with 56 additions and 28 deletions

View File

@ -216,11 +216,16 @@ The mapping should be:
<branchcode>:<item field>:<comparator>:<item field value>:<sort bin number>
The field comparison triplet is repeatable, so you may include multiple sections
:<item field>:<comparator>:<item field value>:
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

View File

@ -1336,12 +1336,14 @@ Circulation:
- "- 'comparator' is the type of comparison, possible values are: eq,<,<=,>,>=,ne<br/>"
- "- 'item field value' is the value to compare against the value in the specified 'item field'<br/>"
- "- 'sort bin number' is the expected return value in the CL field of the SIP response for an item matching a rule<br/><br/>"
- "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:<br/>"
- "i.e. \\$holdingbranch<br/></br>"
- "Examples:<br/>"
- "CPL:itype:eq:BOOK:1 - Will return sort bin 1 for an item of itemtype 'BOOK' returned to CPL.<br/>"
- "CPL:itemcallnumber:<:339.6:3 - Will return sort bin 3 for an item with a callnumber less than 339.6 returned to CPL .<br/>"
- "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).<br/><br/>"
- "CPL:itemcallnumber:<:339.6:3 - Will return sort bin 3 for an item with a callnumber less than 339.6 returned to CPL.<br/>"
- "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).<br/>"
- "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.<br/><br/>"
- pref: SIP2SortBinMapping
type: textarea
class: long