Current LCR implementation doesn't select the lowest cost trunk correctly if rate cards for different trunks have different dial prefix length, current algorithm always selects longest dial prefix. Here is an example:
trunk a:
7,Russia,0.03
79,Russia mobile,0.05
trunk b:
7,Russia,0.04
7905,Russia-mobile,0.06
If the number dialed begins with 7905, current LCR algorithm will select trunk b (longer preffix match), but trunk a offers lower rate.
The patch attached fixes the problem. The idea of the patch is the following:
1. Remove "1) REMOVE THOSE THAT HAVE A SMALLER DIALPREFIX". Step 3 will do the work
2. Move step "3) REMOVE THOSE THAT USE THE SAME TRUNK - MAKE A DISTINCT" before results sort.
How it works:
1. "Superquiry" returns all matched prefixes ordered by prefix lenght in reverse order (longest prefix first).
2. Step 3 ("MAKE A DISTINCT") for each trunk will remove short prefixes, only the longest dial prefix will remain. After this step we have right matched dialprefix for each trunk.
3. Now just sort the remaining array. You got the right trunk with the lowest price.
For my example above trunk a will be selected, which is right.