$item['call_type'] = "ASTERISKLBL_GOING_OUT";
     $item['direction'] = "Outbound";
     #$item['phone_number'] = $row['callerID'];
     #$item['asterisk_name'] = $row['callerName'];
 }
 // Remove prepending dialout prefix if present
 $tmpCallerID = trim($row['callerID']);
 if (strlen($calloutPrefix) > 0 && strpos($tmpCallerID, $calloutPrefix) === 0) {
     $tmpCallerID = substr($tmpCallerID, strlen($calloutPrefix));
 }
 $item['phone_number'] = $tmpCallerID;
 #$item['phone_number'] = $row['callerID'];
 $item['asterisk_name'] = $row['callerName'];
 $item['asterisk_id'] = $row['asterisk_id'];
 // prepare phone number passed in
 $phoneToFind = regexify($item['phone_number']);
 $found = array();
 if (strlen($phoneToFind) > 0) {
     $sqlReplace = "\r\n\t\t\t    replace(\r\n\t\t\t    replace(\r\n\t\t\t    replace(\r\n\t\t\t    replace(\r\n\t\t\t    replace(\r\n\t\t\t    replace(\r\n\t\t\t    replace(\r\n\t\t\t    replace(\r\n\t\t\t      %s,\r\n\t\t\t        ' ', ''),\r\n\t\t\t        '+', ''),\r\n\t\t\t        '/', ''),\r\n\t\t\t        '(', ''),\r\n\t\t\t        ')', ''),\r\n\t\t\t        '[', ''),\r\n\t\t\t        ']', ''),\r\n\t\t\t        '-', '')\r\n\t\t\t        LIKE '%s'\r\n\t\t\t";
     // First attempt to find a company Account in case it's a shared number
     $queryAccount = "SELECT a.name as account_name, account_id, phone_office, phone_alternate\r\n                   FROM accounts a WHERE ";
     $queryAccount .= sprintf($sqlReplace, "phone_office", $phoneToFind) . " OR ";
     $queryAccount .= sprintf($sqlReplace, "phone_alternate", $phoneToFind);
     $queryAccount .= " LIMIT 1";
     $innerResultSet = $cUser->db->query($queryAccount, false);
     if ($accountRow = $cUser->db->fetchByAssoc($innerResultSet)) {
         $found['$company'] = $contactRow['account_name'];
         $found['$companyId'] = $contactRow['account_id'];
     } else {
         // It is probably an individual number, we should search in Contacts
         $queryContact = "SELECT c.id as contact_id, first_name,\tlast_name,\r\n\t\t\t\t\t\tphone_work, phone_home, phone_mobile, phone_other,\r\n\t\t\t\t\t\ta.name as account_name, account_id\r\n\t\t\t\tFROM contacts c\r\n\t\t\t\t\tleft join accounts_contacts ac on (c.id=ac.contact_id and ac.deleted=0)\r\n\t\t\t\t\tleft join accounts a on (ac.account_id=a.id)\r\n\t\t\t\tWHERE ";
function findSugarObjectByPhoneNumber($aPhoneNumber)
{
    global $soapClient, $soapSessionId;
    print "# +++ findSugarObjectByPhoneNumber({$aPhoneNumber})\n";
    $searchPattern = regexify($aPhoneNumber);
    //
    // Plan A: Attempt to locate an object in Contacts
    //        $soapResult = $soapClient->call('get_entry' , array('session' => $soapSessionId, 'module_name' => 'Calls', 'id' => $callRecId));
    //
    $soapArgs = array('session' => $soapSessionId, 'module_name' => 'Contacts', 'query' => "((contacts.phone_work LIKE '{$searchPattern}') OR (contacts.phone_mobile LIKE '{$searchPattern}') OR (contacts.phone_home LIKE '{$searchPattern}') OR (contacts.phone_other LIKE '{$searchPattern}'))");
    // print "--- SOAP get_entry_list() ----- ARGS ----------------------------------------\n";
    // var_dump($soapArgs);
    // print "-----------------------------------------------------------------------------\n";
    $soapResult = $soapClient->call('get_entry_list', $soapArgs);
    // print "--- SOAP get_entry_list() ----- RESULT --------------------------------------\n";
    // var_dump($soapResult);
    // print "-----------------------------------------------------------------------------\n";
    if ($soapResult['error']['number'] != 0) {
        echo "! Warning: SOAP error " . $soapResult['error']['number'] . " " . $soapResult['error']['string'] . "\n";
    } else {
        $resultDecoded = decode_name_value_list($soapResult['entry_list'][0]['name_value_list']);
        // print "--- SOAP get_entry_list() ----- RESULT --------------------------------------\n";
        // var_dump($resultDecoded);
        // print "-----------------------------------------------------------------------------\n";
        return array('type' => 'Contacts', 'values' => $resultDecoded);
    }
    // Oops nothing found :-(
    return FALSE;
}
function findAccountByPhoneNumber($aPhoneNumber)
{
    global $soapClient, $soapSessionId;
    print "# +++ findAccountByPhoneNumber({$aPhoneNumber})\n";
    $searchPattern = regexify($aPhoneNumber);
    $soapArgs = array('session' => $soapSessionId, 'module_name' => 'Accounts', 'query' => "((accounts.phone_office LIKE '{$searchPattern}') OR (accounts.phone_alternate LIKE '{$searchPattern}'))");
    // print "--- SOAP get_entry_list() ----- ARGS ----------------------------------------\n";
    // var_dump($soapArgs);
    // print "-----------------------------------------------------------------------------\n";
    $soapResult = $soapClient->call('get_entry_list', $soapArgs);
    //     print "--- SOAP get_entry_list() ----- RESULT --------------------------------------\n";
    //     var_dump($soapResult);
    //     print "-----------------------------------------------------------------------------\n";
    if ($soapResult['error']['number'] != 0) {
        echo "! Warning: SOAP error " . $soapResult['error']['number'] . " " . $soapResult['error']['string'] . "\n";
    } elseif (count($soapResult['entry_list']) > 0) {
        //		print "--- SOAP get_entry_list() ----- RESULT --------------------------------------\n";
        //		var_dump($soapResult['entry_list'][0]);
        //		print "-----------------------------------------------------------------------------\n";
        // Return just Account ID
        return $soapResult['entry_list'][0]['id'];
    }
    // Oops nothing found :-(
    return FALSE;
}