CRMCall.php 9.95 KB
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;

use Auth,DB;

class CRMCall extends Model{

	protected $table = 'crmcalls';

	//protected $fillable = array('status','data','log','group');

	public function user()
	{
		return $this->belongsTo('App\Models\User','user_id');
	}
	
	
	public function setTs($var,$nowts=0)
	{
		if($nowts==0)$nowts=microtime(true)*1000;
		
		if($var=='ts_Wait')
		{
			$this->ts_Wait=$nowts;
			$this->ts_Call=$nowts;
			$this->ts_Talk=$nowts;
			$this->ts_Recstart=$nowts;
			$this->ts_Recend=$nowts;
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_Call')
		{
			$this->ts_Call=$nowts;
			$this->ts_Talk=$nowts;
			$this->ts_Recstart=$nowts;
			$this->ts_Recend=$nowts;
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_Talk')
		{
			$this->ts_Talk=$nowts;
			$this->ts_Recstart=$nowts;
			$this->ts_Recend=$nowts;
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_Recstart')
		{
			$this->ts_Recstart=$nowts;
			$this->ts_Recend=$nowts;
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_Recend')
		{
			$this->ts_Recend=$nowts;
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_Dispo')
		{
			$this->ts_Dispo=$nowts;
			$this->ts_Close=$nowts;
		}
		if($var=='ts_Close')
		{
			$this->ts_Close=$nowts;
		}
		
		$this->calcTime();
	}
	public function calcTime()
	{
		$this->waitSec=      $this->ts_Call-$this->ts_Wait;
		$this->callSec=      $this->ts_Talk-$this->ts_Call;
		$this->talkSec=      $this->ts_Recstart-$this->ts_Talk;
		$this->recstartSec=  $this->ts_Recend-$this->ts_Recstart;
		$this->recendSec=    $this->ts_Dispo-$this->ts_Recend;
		$this->dispoSec=     $this->ts_Close-$this->ts_Dispo;
	}
	
	public function newRecFilePath()
	{
		$recfile="";
		$data=json_decode($this->data,true);
		if(!isset($data['recFolder']))
		{
			$data['recFolder']='drec/'.$this->client.'/'.date('Y').'/'.date('m').'/'.date('d').'/'.$this->type; //$data['recFolder']='drec/'.date('Y-m');
			$data['recExt']='gsm';
			$recPath=storage_path("app/".$data['recFolder']);
			if(!is_dir($recPath)){$umask=umask(0);mkdir($recPath,0777,true);umask($umask);}
			$recFilePath = $this->type.'_'.date('YmdHis', strtotime($this->created_at)).'_'.$this->client.'_'.$this->number.'_'.$this->id.'_'.$this->user->username.'_'.$this->crm_id; //TYPE_OF_CALL__DATETIME__CAMPAIGNID__NUMBER__UNIQUEID__USER__CRMID.gsm
			$recfile=$recPath."/".$recFilePath.".".$data['recExt'];

			$this->data=json_encode($data);
		}
		return $recfile;
	}
	public function getRecFilePath()
	{
		$file="";
		$data=json_decode($this->data,true);
		if(isset($data['recFolder']))
		{
			$recFilePath = $this->type.'_'.date('YmdHis', strtotime($this->created_at)).'_'.$this->client.'_'.$this->number.'_'.$this->id.'_'.$this->user->username.'_'.$this->crm_id; //TYPE_OF_CALL__DATETIME__CAMPAIGNID__NUMBER__UNIQUEID__USER__CRMID.gsm
			$file=storage_path("app/".$data['recFolder'])."/".$recFilePath.".".$data['recExt'];
		}
		return $file;
	}
	public function getRecFileExt()
	{
		$ext="";
		$data=json_decode($this->data,true);
		if(isset($data['recExt']))
		{
			$ext=$data['recExt'];
		}
		return $ext;
	}
	public function saveRecFileSize()
	{
		$fsize=0;
		$data=json_decode($this->data,true);
		if(isset($data['recFolder']))
		{
			$recFilePath = $this->type.'_'.date('YmdHis', strtotime($this->created_at)).'_'.$this->client.'_'.$this->number.'_'.$this->id.'_'.$this->user->username.'_'.$this->crm_id; //TYPE_OF_CALL__DATETIME__CAMPAIGNID__NUMBER__UNIQUEID__USER__CRMID.gsm

			$fpath=storage_path("app/".$data['recFolder'])."/".$recFilePath.".".$data['recExt'];
			if(file_exists($fpath))$fsize=filesize($fpath);
		}
		$this->recsize=$fsize;
	}
	public function addEventLog($nowts,$eventstr)
	{
		$data=json_decode($this->data,true);
		if(!isset($data['eventslog']))$data['eventslog']=array();
		$data['eventslog'][$nowts]=$eventstr;
		$this->data=json_encode($data);
	}
	
	protected static function boot()
	{
		parent::boot();

		static::addGlobalScope('groupacl', function(\Illuminate\Database\Eloquent\Builder $builder)
		{
			if(Auth::check())
			{
				$builder->whereIn('group',Auth::user()->getAccessList("group",true,false,false));
			}
		});
		
		static::creating(function($model)
		{
			if(Auth::check())
			{
				if($model->group=='')$model->group=Auth::user()->group;
				if($model->group=='')$model->group="Default";
				
				$groupacl=Auth::user()->getAccessList("group",false,true,false);
				if(!in_array($model->group,$groupacl))
				{
					throw new \Exception("No Access to Create [".Auth::user()->id."] : (".implode(",",$groupacl).") in $model->group");
					return false;
				}
			}
			else if($model->group=='')$model->group="Default";
		});
		static::updating(function($model)
		{
			if(Auth::check())
			{
				$original = $model->getOriginal();
				if($original['group']=='')$original['group']='Default';
				
				if($model->group=='')$model->group=Auth::user()->group;
				if($model->group=='')$model->group="Default";
				
				$groupacl=Auth::user()->getAccessList("group",false,true,false);
				if(!in_array($original['group'],$groupacl)||!in_array($model->group,$groupacl))
				{
					throw new \Exception("No Access to Update [".Auth::user()->id."] : (".implode(",",$groupacl).") in $model->group");
					return false;
				}
			}
			else if($model->group=='')$model->group="Default";
		});
	}
	// Code written by Amol G
	public function getCRMCallsData($data = array())
	{
		$reportarray  = array();

		$date = $data['date'];
		$startTime = date('Y-m-d H:i:s', $data['start_date']);
		$endTime = date('Y-m-d H:i:s', $data['end_date']);
		$user_id = $data['user_id'];
		$client = ''; //$data['client'];
		$user_type = $data['user_type'];

		$ts_Wait=0;$ts_Call=0;$ts_Talk=0;$ts_Dispo=0;
        $progTs_Wait=0;$progTs_Call=0;$progTs_Talk=0;$progTs_Dispo=0;
        $manTs_Wait=0;$manTs_Call=0;$manTs_Talk=0;$manTs_Dispo=0;
        $inbTs_Wait=0;$inbTs_Call=0;$inbTs_Talk=0;$inbTs_Dispo=0;
        $tt_prog=0;$tt_man=0;$tt_inb=0;
        $inboundCnt = $outboundCnt = $totalCnt = 0;

          /*   <!-- Change for customization of reports -->*/
		$columns = DB::raw('user_id,type,ts_Wait,ts_Call,ts_Talk,ts_Recstart,ts_Recend,ts_Dispo,ts_Close,usercallback,remarks');
		$crmCalls = DB::table('crmcalls')
	                     ->select($columns)	                     
	                     ->where('user_id', '=', $user_id)
	                     ->where('updated_at', '>=', $startTime)
						 ->where('updated_at', '<=', $endTime);
		          /*   <!-- Change for customization of reports -->*/
		//if($user_type != 'Admin')
		//$crmCalls = $crmCalls->where('client', 'like', '%' . $client . '%');
						
		$crmCalls = $crmCalls->get();

		 if($crmCalls!=null)
		 {
			foreach($crmCalls as $crmCall)
			{
			    $ts_Wait += round(($crmCall->ts_Call - $crmCall->ts_Wait)/1000,2);
			    $ts_Call += round(($crmCall->ts_Talk - $crmCall->ts_Call)/1000,2);
			    $ts_Talk += round(($crmCall->ts_Dispo - $crmCall->ts_Talk)/1000,2);
			    $ts_Dispo += round(($crmCall->ts_Close - $crmCall->ts_Dispo)/1000,2);

			    if($crmCall->type == 'Progressive')
			    {
			            $progTs_Wait += round(($crmCall->ts_Call - $crmCall->ts_Wait)/1000,2);
			            $progTs_Call += round(($crmCall->ts_Talk - $crmCall->ts_Call)/1000,2);
			            $progTs_Talk += round(($crmCall->ts_Dispo - $crmCall->ts_Talk)/1000,2);
			            $progTs_Dispo += round(($crmCall->ts_Close - $crmCall->ts_Dispo)/1000,2);
			    }

			    if($crmCall->type == 'Manual')
			    {
			            $manTs_Wait += round(($crmCall->ts_Call - $crmCall->ts_Wait)/1000,2);
			            $manTs_Call += round(($crmCall->ts_Talk - $crmCall->ts_Call)/1000,2);
			            $manTs_Talk += round(($crmCall->ts_Dispo - $crmCall->ts_Talk)/1000,2);
			            $manTs_Dispo += round(($crmCall->ts_Close - $crmCall->ts_Dispo)/1000,2);
			    }

			if($crmCall->type == 'Inbound')
			    {
			            $inbTs_Wait += round(($crmCall->ts_Call - $crmCall->ts_Wait)/1000,2);
			            $inbTs_Call += round(($crmCall->ts_Talk - $crmCall->ts_Call)/1000,2);
			            $inbTs_Talk += round(($crmCall->ts_Dispo - $crmCall->ts_Talk)/1000,2);
			            $inbTs_Dispo += round(($crmCall->ts_Close - $crmCall->ts_Dispo)/1000,2);
			            $inboundCnt++;
			    }
			    $totalCnt++;
			}
		}

		$outboundCnt = ($totalCnt-$inboundCnt);

		$tt_prog = $progTs_Wait + $progTs_Call + $progTs_Talk + $progTs_Dispo;
        $tt_man = $manTs_Wait + $manTs_Call + $manTs_Talk + $manTs_Dispo;
        $tt_inb = $inbTs_Wait + $inbTs_Call + $inbTs_Talk + $inbTs_Dispo;
        
        $prod_TOS = $ts_Wait + $ts_Call + $ts_Talk + $ts_Dispo;

        $reportarray[$user_id][$date]["Prog-Time"]= $tt_prog;
		$reportarray[$user_id][$date]["Man-Time"]= $tt_man;
		$reportarray[$user_id][$date]["Inc-Time"]= $tt_inb;

		$reportarray[$user_id][$date]["Wait-Time"]= $ts_Wait;
		$reportarray[$user_id][$date]["Call-Time"]= $ts_Call;
		$reportarray[$user_id][$date]["Talk-Time"]= $ts_Talk;
		$reportarray[$user_id][$date]["Disp-Time"]= $ts_Dispo;
		$reportarray[$user_id][$date]["Prog-Wait-Time"]= $progTs_Wait;
		$reportarray[$user_id][$date]["Prog-Call-Time"]= $progTs_Call;
		$reportarray[$user_id][$date]["Prog-Talk-Time"]= $progTs_Talk;
		$reportarray[$user_id][$date]["Prog-Disp-Time"]= $progTs_Dispo;
		$reportarray[$user_id][$date]["Man-Wait-Time"]= $manTs_Wait;
		$reportarray[$user_id][$date]["Man-Call-Time"]= $manTs_Call;
		$reportarray[$user_id][$date]["Man-Talk-Time"]= $manTs_Talk;
		$reportarray[$user_id][$date]["Man-Disp-Time"]= $manTs_Dispo;
		$reportarray[$user_id][$date]["Inc-Wait-Time"]= $inbTs_Wait;
		$reportarray[$user_id][$date]["Inc-Call-Time"]= $inbTs_Call;
		$reportarray[$user_id][$date]["Inc-Talk-Time"]= $inbTs_Talk;
		$reportarray[$user_id][$date]["Inc-Disp-Time"]= $inbTs_Dispo;
		$reportarray[$user_id][$date]["Prod-TOS"]= $prod_TOS;
		$reportarray[$user_id][$date]["inboundCnt"]= $inboundCnt;
		$reportarray[$user_id][$date]["outboundCnt"]= $outboundCnt;
		$reportarray[$user_id][$date]["totalCnt"]= $totalCnt;
						 
		return $reportarray;
	}
}