Actually, this script only handles the rule creation part of it, not the rule usage. However, I did have an idea (not yet implemented, but I will soon) for fixing those as well. The rules are documented as follows:
* same branch and same borrower category, itemtype * * same branch and same itemtype, borrower category * * same itemtype and borrower category, branch * * everywhere * If nothing is set, default is 21,5 (hardcoded)0
However, GetLoanLength is the only part of Koha that actually implements these, and clumsily at that. We can reduce those 9 selects to a single one, using a stored function:
CREATE FUNCTION specificity(categorycode CHAR(2), itemtype VARCHAR(6), branchcode VARCHAR(4)) RETURNS INTEGER NO SQL RETURN (IF(categorycode != '*', 7, 0) + IF(itemtype != '*', 5, 0) + IF(branchcode != '*', 9, 0));
This calculates how specific a given issuing rule is, given what it specifies. For instance, a rule that defines a branch and category has a specificity of 16, while a rule that defines a branch and itemtype has a specificity of 14. The numbers are somewhat arbitrary, and are mainly used because they get what the documentation says should be gotten. Thus, GetLoanLength's SQL can become:
SELECT issuelength FROM issuingrules WHERE categorycode IN (?, '*') AND itemtype IN (?, '*') AND branchcode IN (?, '*') ORDER BY specificity(categorycode, itemtype, branchcode) DESC LIMIT 1;
This can be adapted for GetIssuingRules, CanBookBeIssued and TooMany merely by changing the returned columns. I know that there isn't any precedent for stored functions in Koha, but this solves the problem with a minimum amount of fuss. It is also entirely separate from smart-rules.pl, though specificity() could be useful for sorting the list of rules. -- Jesse