Route calls based on caller id

I am currently using Freepbx 2.7.05
i have a need to route certain calls based on the caller ID to different groups.

This is what I currently have setup.

I’m using a callerid.php to lookup the callerid from a local astridex table

<?php $thenumber=$_REQUEST['thenumber']; $link = mysql_connect("localhost", "root", "passw0rd") or die("AsteriDex connection failed"); mysql_select_db("asteridex") or die("AsteriDex database open failed"); $query = "SELECT * FROM `user1` where `out` LIKE '$thenumber'"; $result = mysql_query($query) or die("AsteriDex query failed"); if (mysql_num_rows($result)>0) : $row = mysql_fetch_array($result); $value=$row["name"]; endif ; mysql_close($link); if (strlen($value)>0) : echo $value; exit ; endif ; $value=$thenumber; echo $value; exit ; ?>

my inbound route is going to a custom extention which is calling the following script

exten => s,1,Answer exten => s,n,SetMusicOnHold(default) exten => s,n,GotoIf($["${CALLERID(name):0:2}" = "H:"]?ext-queues,801,1) exten => s,n,Dial(Local/[email protected]) exten => s,n,Hangup

This works fine if the caller id from the DB begins H: then to goes to queue 801 otherwise it goes to extn 800

The issue I have is I also use this database for a lookup on my phone and doing it this way means my entries are going to all start H: not alphabetically.

I have looked at adding another column to the astridex table containing the department which the caller id belongs to and then somehow setting this as part of the callerid lookup php page to create a new asterisk variable which I can filter the call based on.

Does anyone know if this is possible ?

My company has about 7 different departments that get regular calls from about 50 different locations, so doing this through matching the caller id in the inbound route will not work, also the call flow runs through a few time conditions and day and night controls before this check so that too rules out the inbound route.

I look forward to your thoughts.