this is the dailplan,
[default]
exten => _XXXX,1,AGI(agi.php)
exten => h,1,AGI(agi.php,CDR)
then this is the cdr function in agi.php after argv[1] ==‘CDR’ in case of hangup…
elseif ($argv[1] == 'CDR') {
cdr($agi, $TP);
function cdr($agi, $TP) {
$new_dst = $TP->extension;
$callerid = $TP->callerid;
$dcontext = $agi->get_variable("CDR(dcontext)");
$dcontext = $dcontext['data'];
$channel = $agi->get_variable("CDR(channel)");
$tr_src_channel = $channel = $channel['data'];
$customer_id = $agi->get_variable("CUSTOMERID");
$custid = $customer_id = $customer_id['data'];
$slevel = $agi->get_variable('SLEVEL');
$slevel = $slevel['data'];
$slevel_value = $agi->get_variable('SLEVELVALUE');
$slevel_value = $slevel_value['data'];
if($slevel == 'SLEVEL' AND $slevel_value != ''){
$query = "UPDATE tp_servicelevel SET tp_servicelevel.service_level = $slevel_value, tp_servicelevel.cdr_callid = (select tp_cdr.cdr_id from tp_cdr where tp_cdr.uniqueid = tp_servicelevel.callstatus_callid and tp_cdr.call_type = 'INBOUND' order by tp_cdr.cdr_id desc limit 1) WHERE tp_servicelevel.customer_id = '$custid' AND tp_servicelevel.channel = '$tr_src_channel'";
$TP->sqlQuery($query, $agi, 'queue_agentunpause');
$agi->hangup;
}
$dstchannel = $agi->get_variable("CDR(dstchannel)");
$dstchannel = $dstchannel['data'];
$clid = $agi->get_variable("CDR(clid)");
$clid = $clid['data'];
$src = $agi->get_variable("CDR(src)");
$tr_src = $src = $src['data'];
$dst = $agi->get_variable("CDR(dst)");
$dst = $dst['data'];
$srcchannel = $agi->get_variable("DIALEDPEERNAME");
print_r($srcchannel);
$srcchannel = $srcchannel['data'];
echo "line 2747:\n";
print_r($srcchannel);
$hangup_chanel = $TP->channel;
echo "line 2748: \n";
echo $hangup_chanel;
$new_channel = str_replace('<ZOMBIE>', '', $hangup_chanel);
$hangup_type = $TP->type;
$forward_account_id = $agi->get_variable("FORWARDINGACCOUNT");
$forward_account_id = $forward_account_id['data'];
echo "below forwarding \n";
echo $forward_account_id;
echo "----\n";
echo $hangup_type;
if ($hangup_type == 'Kill' AND $new_channel != $hangup_chanel) {
$dest_number = $TP->dnid;
$TP->livecall($agi, $custid, $new_dst, 'ENDTRANSFER', $request_type = NULL, $uniqueid_answer = '', $dest_number, $new_channel, $tr_src_channel, $tr_src, $srcchannel, $dst);
} else {
echo "hangup type not kill\n";
$TP->test();
echo "custid: ";var_dump($custid);echo "\n";
echo "tr_src: ";var_dump($tr_src);echo "\n";
echo "srcchannel: ";var_dump($srcchannel);echo "\n";
echo "dst: ";var_dump($dst);echo "\n";
echo "dstchannel: ";var_dump($dstchannel);echo "\n";
$TP->livecall($agi, $custid, $new_dst, 'END', $request_type = NULL, $uniqueid_answer = '', $dest_number, $new_channel, $tr_src_channel, $tr_src, $srcchannel, $dst, $channel, $dstchannel);
echo "below hangup type not kill\n";
}
$recordingfile = $agi->get_variable("MONITORFILE");
$recordingfile = $recordingfile['data'];
if ($recordingfile == '')
$recordingfile = $on_demond_recordingfile = $TP->monitorfile;
$TP->check_auth_account($agi, $forward_account_id);
$calltypestart = $agi->get_variable("CALLTYPESTART");
$calltypestart = $calltypestart['data'];
$calltype = $agi->get_variable("CALLTYPE");
$calltype = $calltype['data'];
$dialstatus = $agi->get_variable("DIALSTATUS");
$dialstatus = $dialstatus['data'];
$clid = $agi->get_variable("CDR(clid)");
$clid = $clid['data'];
$auto_recording = $agi->get_variable("TOUCH_MIXMONITOR_OUTPUT");
$auto_recording = $auto_recording['data'];
if ($auto_recording != '')
$recordingfile = $auto_recording;
$newdst = $agi->get_variable("NEWDST");
$newdst = $newdst['data'];
$dialednumber = $agi->get_variable("DIALEDPEERNUMBER");
$cn = $dialednumber['data'];
$xy = substr($cn, 1, 1);
if ($xy == '*') {
$cn = explode('*', $cn);
$dialednumber = $cn[1];
} else {
$dialednumber = $dialednumber['data'];
}
$accountid = $agi->get_variable("ACCOUNTCODEID");
$accountid = $accountid['data'];
if ($accountid == '')
$accountid = $forward_account_id;
$trunkid = $agi->get_variable("TRUNKID");
$trunkid = $trunkid['data'];
$rateid = $agi->get_variable("RATEID");
$rateid = $rateid['data'];
$rateplanid = $agi->get_variable("RATEPLANID");
$rateplanid = $rateplanid['data'];
$callplanid = $agi->get_variable("CALLPLANID");
$callplanid = $callplanid['data'];
$trunkgroupid = $agi->get_variable("TRUNKGROUPID");
$trunkgroupid = $trunkgroupid['data'];
$trunkprovider_id = $agi->get_variable("TRUNKPROVIDERID");
$trunkprovider_id = $trunkprovider_id['data'];
$lastdst = $agi->get_variable("LASTDST");
$lastdst = $lastdst['data'];
$uniqueid = NULL;
$salerate = NULL;
$salecost = NULL;
$buyrate = NULL;
$buycost = NULL;
$sipiax = NULL;
$lastapp = $agi->get_variable("CDR(lastapp)");
$lastapp = $lastapp['data'];
$lastappdata = $agi->get_variable("LASTAPPDATA");
$lastappdata = $lastappdata['data'];
if ($lastappdata == 'QUEUE' AND $lastapp == 'AGI')
$lastapp = 'Queue';
elseif ($lastappdata == 'MEETME' AND $lastapp == 'AGI')
$lastapp = 'MeetMe';
if ($calltype == 'DIDTOEXTEN' AND $lastapp == 'AGI') {
$lastapp = "Exten";
}if ($calltype == 'DIDTORINGGROUP' AND $lastapp == 'Dial') {
$lastapp = "RingGroup";
} elseif ($calltype == 'DIDTOIVR' AND $lastapp == 'AGI') {
$lastapp = "IVR";
} elseif ($calltype == 'DIDTOANNOUNCEMENT' AND $lastapp == 'AGI') {
$lastapp = "Announcement";
} elseif ($lastapp == 'Dial') {
$lastapp = "Exten";
}
$lastdata = $agi->get_variable("CDR(lastdata)");
$lastdata = $lastdata['data'];
$starttime = $agi->get_variable("CDR(start)");
$starttime = $starttime['data'];
$answertime = $agi->get_variable("CDR(answer)");
$answertime = $answertime['data'];
$stoptime = $agi->get_variable("CDR(end)");
$stoptime = $stoptime['data'];
$callduration = $agi->get_variable("CDR(duration)");
$callduration = $callduration['data'];
$billsec = $agi->get_variable("CDR(billsec)");
$billsec = $billsec['data'];
$answeredtime = $agi->get_variable("ANSWEREDTIME");
$answeredtime = (isset($answeredtime['data']) && $answeredtime['data'] != '') ? $answeredtime['data'] : 0;
if ($answeredtime > $billsec)
$answeredtime = $billsec;
$disposition = $agi->get_variable("CDR(disposition)");
$disposition = $disposition['data'];
if ($dialstatus == "")
$dialstatus = $disposition;
$accountcode = $agi->get_variable("CDR(accountcode)");
$accountcode = $accountcode['data'];
$uniqueid = $agi->get_variable("CDR(uniqueid)");
$uniqueid = $uniqueid['data'];
$ringduration = $callduration - $billsec;
if ($lastapp == 'MeetMe' and $calltypestart != 'SERVICE') {
$answeredtime = $billsec;
if ($answeredtime != 0) {
//$cmd = "/usr/bin/lame -v -o -m m -B 8 -s 44.2 ".$recordingfile.".wav ".$recordingfile.".mp3 && /bin/rm -rf ".$recordingfile.".wav";
$cmd = "/usr/bin/lame " . $recordingfile . ".wav " . $recordingfile . ".mp3 && /bin/rm -rf " . $recordingfile . ".wav";
$output = system("$cmd");
$TP->debug(INFO, $agi, __FILE__, __LINE__, 'CMD ' . $cmd);
}
}
if ($auto_recording != '') {
/*
if (substr($TP->recording_path, -1) != '/') {
$TP->recording_path .= '/';
}else{
$TP->recording_path = "/var/spool/asterisk/monitor/";
}
$recordingfile1 = $TP->recording_path.$recordingfile;
$cmd = "/usr/bin/lame /var/spool/asterisk/monitor/" . $recordingfile . ".wav " . $recordingfile1 . ".mp3 && /bin/rm -rf /var/spool/asterisk/monitor/" . $recordingfile . ".wav";
$output = system("$cmd");
$TP->debug(INFO, $agi, __FILE__, __LINE__, 'CMD ' . $cmd);
*/
}
if ($on_demond_recordingfile != '') {
$cmd = "/usr/bin/lame " . $on_demond_recordingfile . ".wav " . $on_demond_recordingfile . ".mp3 && /bin/rm -rf " . $on_demond_recordingfile . ".wav";
$output = system("$cmd");
$TP->debug(INFO, $agi, __FILE__, __LINE__, 'CMD ' . $cmd);
}
if ($answeredtime == 0)
$recordingfile = '';
if ($answeredtime == 0)
$dialednumber = $newdst;
if ($answeredtime == 0 AND $lastdst != '')
$dialednumber = $lastdst;
if ($answeredtime == 0 AND $dialednumber == '')
$dialednumber = '';
$vads = $agi->get_variable("VADS");
$vads = $vads['data'];
if ($vads != '') {
$query = "UPDATE `tp_agent_status` set `vads` = '$vads', agentName = '" . $TP->name . "' where `customer_id` = $customer_id and agent_exten =" . $TP->extennumber;
$TP->sqlQuery($query, $agi, 'VADS');
}
if(DEBUG)
$TP->debug(INFO, $agi, __FILE__, __LINE__, 'CDR clid ' . $clid . ' src ' . $src . ' dst ' . $dst . ' dcontext ' . $dcontext . ' channel ' . $channel . ' dstchannel ' . $dstchannel . ' lastapp ' . $lastapp . ' lastdata ' . $lastdata . ' starttime ' . $starttime . ' answertime ' . $answertime . ' endtime ' . $endtime . ' duration ' . $duration . ' billduration ' . $billduration . ' disposition ' . $disposition . ' Stotime ' . $stoptime);
if(DEBUGF)
$TP->debug_logfile(INFO, $agi, __FILE__, __LINE__, 'CDR clid ' . $clid . ' src ' . $src . ' dst ' . $dst . ' dcontext ' . $dcontext . ' channel ' . $channel . ' dstchannel ' . $dstchannel . ' lastapp ' . $lastapp . ' lastdata ' . $lastdata . ' starttime ' . $starttime . ' answertime ' . $answertime . ' endtime ' . $endtime . ' duration ' . $duration . ' billduration ' . $billduration . ' disposition ' . $disposition);
$arr_dst = explode('/', $dialednumber);
if (isset($arr_dst[1])) {
$query = "SELECT trunk_id from tp_trunk WHERE trunkname = '" . $arr_dst[0] . "'";
if(DEBUG)
$TP->debug(INFO, $agi, __FILE__, __LINE__, 'cdr trunk ' . $query);
$result = $TP->sqlQuery($query, $agi, 'cdr');
if ($row = mysqli_fetch_assoc($result)) {
if ($calltypestart == 'INTERNAL') {
$calltypestart = 'OUTBOUND';
} else {
$calltypestart = 'OUTBOUND';
}
$dialednumber = $arr_dst[1];
if(DEBUG)
$TP->debug(INFO, $agi, __FILE__, __LINE__, 'cdR NEW dialednumber ' . $dialednumber);
}
}
if ($calltypestart == 'OUTBOUND' OR $calltypestart == 'OUTBOUND' or $calltypestart == 'OUTBOUND') {
$call_duration = $answeredtime;
$new_dstatus = $agi->get_variable("NEWDSTARTUS");
$new_dstatus = $new_dstatus['data'];
/* Generate Billing & CDR if rates and trunk exist */
if ($new_dstatus == 'RUNDIAL') {
$rate_result = $TP->get_rates($agi, $callplanid, $newdst, $custid, $rateid);
if ($rates_row = mysqli_fetch_assoc($rate_result)) {
$dialprefix = $rates_row['dialprefix'];
$destination = $rates_row['destination'];
$salerate = $rates_row['sale_rate'];
$buyrate = $rates_row['buy_rate'];
$destination_name = $rates_row['destination'];
$TP->pbx2pbx = $rates_row['pbx2pbx'];
if ($dialstatus != 'ANSWER') {
$new_call_duration = 0;
} else {
$new_call_duration = $TP->get_minutes($agi, $rates_row, $amount = 0, $call_duration, 'bill');
}
$TP->get_billcost($agi, $rates_row, $new_call_duration);
}
$salecost = $TP->sale_cost;
$buycost = $TP->buy_cost;
if ($TP->billing_type == 'common' AND $TP->credit < 0.5) {
$bill_level = 1;
$amount = $TP->customer_commoncredit;
$query = "UPDATE `tp_customer` set `commoncredit` = `commoncredit` - $salecost WHERE `customer_id` = '$customer_id'";
} else {
$bill_level = 0;
$amount = $TP->credit;
$query = "UPDATE `tp_account` set `credit` = `credit` - $salecost WHERE `account_id` = '$accountid' AND `customer_id` = '$customer_id'";
}
$TP->sqlQuery($query, $agi, 'credit update');
if ($TP->agentexten_type == '1') {
$agent_interface = '';
if ($TP->sipuser == '1')
$agent_interface = "SIP/";
if ($TP->iaxuser == '1')
$agent_interface = "IAX2/";
$agent_interface .=$TP->customer_id . "*" . $TP->extennumber;
$queuelogstring = "NONE,NONE," . $agent_interface . ",OUTBOUND," . $answeredtime;
$agi->exec("QueueLog $queuelogstring");
}
if ($forward_account_id == '')
$forward_account_id = $accountid;
$query = "INSERT INTO `tp_cdr` (`customer_id`, `account_id`, `trunk_id`, `rate_id`, `rateplan_id`, `callplan_id`, `trunkgroup_id`, `uniqueid`, `starttime`, `stoptime`, `answertime`, `ringduration`, `billsec`, `callduration`, `destination`, `src`, `terminatecause`, `sale_rate`, `sale_cost`, `buy_rate`, `buy_cost`, `sipiax`, `recording_file`, `call_type`,`app_billsec`,`last_app`, `dst`,`last_credit`,`bill_level`,`provider_id`, `destination_name`,bill_account_id) VALUES ('$customer_id', '$accountid', '$trunkid', '$rateid', '$rateplanid', '$callplanid', '$trunkgroupid', '$uniqueid', '$starttime', '$stoptime', '$answertime', '$ringduration', '$answeredtime', '$callduration','$dst', '$src', '$dialstatus', '$salerate', '$salecost', '$buyrate', '$buycost', '$sipiax', '$recordingfile', '$calltypestart','$billsec','$lastapp','$newdst','$amount','$bill_level','$trunkprovider_id','$destination_name','$forward_account_id')";
if(DEBUG)
$TP->debug(INFO, $agi, __FILE__, __LINE__, 'CDR Query ' . $query);
$TP->sqlQuery($query, $agi, 'cdr');
}
}else {
if ($lastapp != 'Queue') {
if ($TP->agentexten_type == '1') {
$agent_interface = '';
if ($TP->sipuser == '1')
$agent_interface = "SIP/";
if ($TP->iaxuser == '1')
$agent_interface = "IAX2/";
$agent_interface .=$TP->customer_id . "*" . $TP->extennumber;
$queuelogstring = "NONE,NONE," . $agent_interface . ",INTERNAL," . $answeredtime;
$agi->exec("QueueLog $queuelogstring");
}
$query = "INSERT INTO `tp_cdr` (`customer_id`, `account_id`, `trunk_id`, `rate_id`, `rateplan_id`, `callplan_id`, `trunkgroup_id`, `uniqueid`, `starttime`, `stoptime`, `answertime`, `ringduration`, `billsec`, `callduration`, `destination`, `src`, `terminatecause`, `sale_rate`, `sale_cost`, `buy_rate`, `buy_cost`, `sipiax`, `recording_file`, `call_type`,`app_billsec`,`last_app`, `dst`) VALUES ('$customer_id', '$accountid', '$trunkid', '$rateid', '$rateplanid', '$callplanid', '$trunkgroupid', '$uniqueid', '$starttime', '$stoptime', '$answertime', '$ringduration', '$answeredtime', '$callduration','$dst', '$src', '$dialstatus', '$salerate', '$salecost', '$buyrate', '$buycost', '$sipiax', '$recordingfile', '$calltypestart','$billsec','$lastapp','$dialednumber')";
$TP->sqlQuery($query, $agi, 'cdr');
if(DEBUG)
$TP->debug(INFO, $agi, __FILE__, __LINE__, 'CDR Query ' . $query);
}
$query = "delete from `tp_call_status` where `dead_call` = '1' AND `callId` = '$uniqueid' AND `status` = 'NEW' AND `callerId` = ''";
$TP->sqlQuery($query, $agi, 'cdr');
/* New changes for BLF Pannel */
if ($dialstatus != 'ANSWERED' AND $dialstatus != 'ANSWER') {
$query = "UPDATE blf_livestatus SET missed_call = ifnull(missed_call,0) + 1, exten_status_update = now() where customer_id = '$customer_id' AND extension IN ('$dialednumber' , '$dst') AND account_id<> '$accountid' ";
$TP->sqlQuery($query, $agi, 'missedcall_count');
if(DEBUG)
$TP->debug(INFO, $agi, __FILE__, __LINE__, 'CDR Query ' . $query);
}
}
if ($lastapp == 'Queue') {
$agent = reset(explode('-', end(explode('*', $dstchannel))));
if(DEBUG)
$TP->debug(INFO, $agi, __FILE__, __LINE__, 'Set Queue Agent status Pause' . $agent);
$agent_status = $TP->queue_agentpause($agi, $agent, 'CDR', 'AFTERCALL');
if(DEBUG)
$TP->debug(INFO, $agi, __FILE__, __LINE__, 'Set Queue Agent status Pause' . $string);
}
$star = strpos($dstchannel, "*");
if($star == true) {
$arr = explode("/",$dstchannel);
$arr2 = explode("-",$arr[1]);
$user = $arr2[0];
$cnu = explode("*",$user);
$cid = $cnu[0];
$username = $cnu[1];
$localdst = 1;
}
$sql = "SELECT email from as_voicemail where mailbox = '$user'";
if($res = $TP->sqlQuery($sql, $agi, 'cdr')){
$result = mysqli_fetch_assoc($res);
$email = $result['email'];
if($email != "" AND $localdst == 1 AND $dialstatus != 'ANSWERED' AND $dialstatus != 'ANSWER' AND $dialstatus != 'BUSY') {
send_missedcall($agi,$TP, $dst, $src, $email);
}
}
}
where TP is $TP = new PBX($agi);