PredictiveCallHangUp.php 1.85 KB
<?php namespace App\Console\Commands;

use Illuminate\Console\Command;
//use Mail;
use DB;
use Config;

use App\Models\User;
use App\Models\Accesslog;

use App\Models\CRMCall;
use Schema;
use PDO;
use App\Models\Notification;
use App\Jobs\KHRMSLib;

use Input;
use App\Models\Sipid;
use App\Models\Kqueue;
use App\Models\Dialline;
use App\Models\Session;
use App\Models\Cutoff;

use Log;
use Illuminate\Database\Schema\Blueprint;

class PredictiveCallHangUp extends Command {

	/**
	 * The console command name.
	 *
	 * @var string
	 */
	protected $signature = 'PredictiveCallHangUp';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Hang Up Extra Calls If Agents Are Not Free';

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle()
	{
		while(true)
		{
			usleep(1000000);
			$this->runHangUp();
		}
	}

	public function runHangUp()
	{
		try {		
		$breathingTime = 5;
		$avgringsec = 30;

		$avgringsec = Cutoff::select(DB::Raw('avg(avg_ring) as avgringsec'))->first();
		if($avgringsec)$avgringsec= intval($avgringsec->avgringsec);

		$diallines = Dialline::whereIn("status", ["Auto","AutoCall"])->where("conf","=","")->select('src_channel','status','updated_at','channel','server')->get();

		foreach ($diallines as $dialline) {

			$newqueue=new Kqueue();
			$lastUpdatedTime = strtotime(date("Y-m-d H:i:s")) - strtotime($dialline->updated_at);

			if ($dialline->status == 'Auto' && $lastUpdatedTime > $breathingTime) {
				$newqueue->hangupChannelS($dialline->channel,$dialline->server);
			}
			elseif($dialline->status == 'AutoCall' && $lastUpdatedTime > ($avgringsec+$breathingTime)) {
				$newqueue->hangupChannelS($dialline->src_channel,$dialline->server);
			}
		}
		} catch (Exception $e) {
			Log::useFiles(storage_path()."/logs/predictive_".date("Y_m_d").".log");
			Log::error($e);
		}
	}
}