leadsreport.blade.php 18.7 KB
<?php
use App\Models\CRMCall;
use App\Models\Record;
use App\Models\User;
use App\Jobs\KHRMSLib;

$wakka = new KHRMSLib();
$dashboarduser=Auth::user();
$campaign=$options='';

$client_list = $wakka->clientsOwnerRWAccess();
$user_list = User::get()->pluck('fullname', 'id');

if(isset($_GET['logdate']))$logdate=strtotime($_GET['logdate']." 00:00:00");
else $logdate=strtotime(date("Y-m-d")." 00:00:00");

if(isset($_GET['logdateto']))$logdateto=strtotime($_GET['logdateto']." 23:59:59");
else $logdateto=strtotime(date("Y-m-d")." 23:59:59");

//if(isset($_GET['campaign']))$campaign=$_GET['campaign'];
$campaign   =(isset($_GET['campaign']))  ? $_GET['campaign']  : "All";

$alist=DB::table('records')
->where('modified','>=',date("Y-m-d H:i:s",$logdate))->where('modified','<=',date("Y-m-d H:i:s",$logdateto));

if($campaign != 'All') 
{
	$alist->where('client','=',$campaign);
}
else
{
	$alist->whereIn('client', $client_list);
}

$options .= '<option value="All">All</option>';

foreach ($client_list AS $client) {
	if($client == $campaign) {
		$options .= '<option value="'.$client.'" selected="selected">'.$client.'</option>';
	}
	else
		$options .= '<option value="'.$client.'">'.$client.'</option>';
}

if($dashboarduser->usertype != 'Admin')
{
    $uidlist=array($dashboarduser->id);
    $allusers=User::where(function ($query)  use($dashboarduser) {
            $query->where('supervisor','=',$dashboarduser->username)
            ->orWhere('lteam2','=',$dashboarduser->username)
            ->orWhere('lteam','=',$dashboarduser->username);
    })->get();
    
    foreach($allusers as $tuser)$uidlist[]=$tuser->id;

    $roclientstr=array();$didlinesstr=array();
    if($dashboarduser->exten!="")$didlinesstr[]=$dashboarduser->exten;
    $oclientlst=$wakka->clientsReadAccess();
    if(!empty($oclientlst))foreach($oclientlst as $tclnt)if($tclnt!="")
    {
        $roclientstr[]="$tclnt";
        
        $mastersdata=$wakka->getCompanyMaster($tclnt);
        if(!empty($mastersdata["DialerDID"]))$didlinesstr[]=$mastersdata["DialerDID"];
    }
    
    $alist=$alist->where(function ($query) use($roclientstr,$uidlist,$didlinesstr){
        $query->orWhereIn('client',$roclientstr);
    }); 
}

$alist=$alist->where('dialer_status', '=', 'Interested');

$alist=$alist->get();

$userarr=array();
$users = DB::table('users as u1')->
    leftjoin('users AS u2', 'u2.username', '=', 'u1.supervisor')->
    select('u1.id','u1.fullname AS telecaller', 'u2.fullname AS sepervisor')->
    get();
foreach($users as $tuser)
{
    $userarr[$tuser->id] = $tuser;
}    

$i=1;
$reporthead=array("#","DATA BASE", "BranchCode", "ReAllocatedBranchCode", "City", "State", "Cluster", "Tier", "LoanAccountNumber", "CustId", "Title", "FirstName", "MiddleName", "LastName", "DisbursalDate", "Loanamount", "InterestRate", "Tenor", "EMIamount", "NumberofEMIspaid", "POS","Incremental","Age","Occupation", "SavedDateTime", "Agent Name", "Date of lead generation", "Month", "offered loan amount", "Lead ID", "Camp name", "Risk Status", "Gross Salary", "Remarks", "RO name", "RO Code", "Sources");
$reportarray=array();
foreach($alist as $aline)
{
    $tuser=$userarr[$aline->user_id];
    $modified_date = $aline->modified;
    $agentname=$user_list[$aline->usr_id];
    $people_data = unserialize($aline->peopledata);

     $substring = "EBPL";
     $pos = strpos($campaign, $substring);
     
    if($pos !== true)
      {
           $campaign = $substring;
      }


    switch($campaign)
    {
        case 'EBPL':        
        $LoanAmt                = (array_key_exists('Eligible_TopUp_Amount', $people_data))              ? $people_data['Eligible_TopUp_Amount']              : '';
        $ReAllocatedBranchCode  = (array_key_exists('ReallocatedBrCode', $people_data)) ? $people_data['ReallocatedBrCode'] : '';
        
        $Gross_EMI              = (array_key_exists('CurrentEmi', $people_data))             ? $people_data['CurrentEmi']             : '';
        $IncrementalTag         = (array_key_exists('Incremental_Tag', $people_data))       ? $people_data['Incremental_Tag']       : '';    
        $LeadDate               =  ($modified_date)       ? date("Y-m-d",strtotime($modified_date))       : '';    ;
        $month                  = '';
        $off_loan_amt           = '';
        $LeadID                 = (array_key_exists('leadid', $people_data))               ? $people_data['leadid']               : '';
        $camp                   = (array_key_exists('campname', $people_data))                  ? $people_data['campname']                  : '';
        $RiskStatus             = (array_key_exists('risk', $people_data))           ? $people_data['risk']           : '';
        $gross_sal              = (array_key_exists('c_gross_sal', $people_data))               ? $people_data['c_gross_sal']               : '';
        $ROCODE                 = (array_key_exists('rocode', $people_data))               ? $people_data['rocode']               : '';
        $roname                 = (array_key_exists('roname', $people_data))               ? $people_data['roname']               : '';
        $Source                 = (array_key_exists('source', $people_data))               ? $people_data['source']               : '';
        $DateofDisbursal        = (array_key_exists('Date_of_Disbursal', $people_data))               ? $people_data['Date_of_Disbursal']               : '';
        break;

        case 'Web_and_online':        
        $LoanAmt                = (array_key_exists('Eligible_TopUp_Amount', $people_data))              ? $people_data['Eligible_TopUp_Amount']              : '';
        $ReAllocatedBranchCode  = (array_key_exists('ReallocatedBrCode', $people_data)) ? $people_data['ReallocatedBrCode'] : '';
        
        $Gross_EMI              = (array_key_exists('CurrentEmi', $people_data))             ? $people_data['CurrentEmi']             : '';
        $IncrementalTag         = (array_key_exists('Incremental_Tag', $people_data))       ? $people_data['Incremental_Tag']       : '';    
        $LeadDate               = '';
        $month                  = '';
        $off_loan_amt           = '';
        $LeadID                 = (array_key_exists('leadid', $people_data))               ? $people_data['leadid']               : '';
        $camp                   = (array_key_exists('campname', $people_data))                  ? $people_data['campname']                  : '';
        $RiskStatus             = (array_key_exists('risk', $people_data))           ? $people_data['risk']           : '';
        $gross_sal              = (array_key_exists('c_gross_sal', $people_data))               ? $people_data['c_gross_sal']               : '';
        $ROCODE                 = (array_key_exists('rocode', $people_data))               ? $people_data['rocode']               : '';
        $roname                 = (array_key_exists('roname', $people_data))               ? $people_data['roname']               : '';
        $Source                 = '';
        $DateofDisbursal        = (array_key_exists('Date_of_Disbursal', $people_data))               ? $people_data['Date_of_Disbursal']               : '';
        break;

        case 'Cibil_Watch':        
        $LoanAmt                = (array_key_exists('Eligible_TopUp_Amount', $people_data))              ? $people_data['Eligible_TopUp_Amount']              : '';
        $ReAllocatedBranchCode  = (array_key_exists('ReallocatedBrCode', $people_data)) ? $people_data['ReallocatedBrCode'] : '';
        
        $Gross_EMI              = (array_key_exists('CurrentEmi', $people_data))             ? $people_data['CurrentEmi']             : '';
        $IncrementalTag         = (array_key_exists('Incremental_Tag', $people_data))       ? $people_data['Incremental_Tag']       : '';    
        $LeadDate               = '';
        $month                  = '';
        $off_loan_amt           = '';
        $LeadID                 = (array_key_exists('leadid', $people_data))               ? $people_data['leadid']               : '';
        $camp                   = (array_key_exists('campname', $people_data))                  ? $people_data['campname']                  : '';
        $RiskStatus             = (array_key_exists('risk', $people_data))           ? $people_data['risk']           : '';
        $gross_sal              = (array_key_exists('c_gross_sal', $people_data))               ? $people_data['c_gross_sal']               : '';
        $ROCODE                 = (array_key_exists('rocode', $people_data))               ? $people_data['rocode']               : '';
        $roname                 = (array_key_exists('roname', $people_data))               ? $people_data['roname']               : '';
        $Source                 = '';
        $DateofDisbursal        = (array_key_exists('Date_of_Disbursal', $people_data))               ? $people_data['Date_of_Disbursal']               : '';
        break;

        default:
        $LoanAmt                = (array_key_exists('Loan Amt', $people_data))              ? $people_data['Loan Amt']              : '';
        $Gross_EMI              = (array_key_exists('Gross_EMI', $people_data))             ? $people_data['Gross_EMI']             : '';
        $DateofDisbursal        = (array_key_exists('Date_of_Disbursal', $people_data))         ? $people_data['Date_of_Disbursal']         : '';
        $IncrementalTag         = (array_key_exists('Incremental_Tag', $people_data))       ? $people_data['Incremental_Tag']       : '';
        $LeadDate               = (array_key_exists('Lead Date', $people_data))             ? $people_data['Lead Date']             : '';
        $month                  = (array_key_exists('month', $people_data))                 ? $people_data['month']                 : '';
        $off_loan_amt           = '';
        $LeadID                 = (array_key_exists('Lead ID', $people_data))               ? $people_data['Lead ID']               : '';
        $camp                   = (array_key_exists('camp', $people_data))                  ? $people_data['camp']                  : '';
        $RiskStatus             = (array_key_exists('Risk Status', $people_data))           ? $people_data['Risk Status']           : '';
        $gross_sal              = '';
        $ROCODE                 = (array_key_exists('RO CODE', $people_data))               ? $people_data['RO CODE']               : '';
        $Source                 = (array_key_exists('Source', $people_data))                ? $people_data['Source']                : '';
        $roname                 = '';
        break;
    }

    $saved_date_time        = date("Y-m-d H:i:s",strtotime($modified_date)+(332*60));
    $dialer_remarks         = (array_key_exists('dialer_remarks', $people_data))               ? $people_data['dialer_remarks']               : '';

    $BranchCode 			= (array_key_exists('Branch_Code', $people_data)) 			? $people_data['Branch_Code'] 			: '';
    
    $city 					= (array_key_exists('city', $people_data)) 					? $people_data['city'] 					: '';
    $State 					= (array_key_exists('State', $people_data)) 				? $people_data['State'] 				: '';
    $Cluster 				= (array_key_exists('Cluster', $people_data)) 				? $people_data['Cluster'] 				: '';
    $Tier 					= (array_key_exists('Tier', $people_data)) 					? $people_data['Tier'] 					: '';
    $LoanAccountNumber 		= (array_key_exists('lan', $people_data)) 					? $people_data['lan'] 					: '';
    $CustId 				= (array_key_exists('OMNI Customer ID', $people_data)) 				? $people_data['OMNI Customer ID'] 				: '';
    $CustomerTitle 			= (array_key_exists('Customer_Title', $people_data)) 		? $people_data['Customer_Title'] 		: '';    
    $FirstName 				= (array_key_exists('firstname', $people_data)) 			? $people_data['firstname'] 			: '';
    $MiddleName 			= (array_key_exists('middle_name', $people_data)) 			? $people_data['middle_name'] 			: '';
    $LastName 				= (array_key_exists('last_name', $people_data)) 				? $people_data['last_name'] 				: '';
    $Tenor 					= (array_key_exists('Tenor', $people_data)) 				? $people_data['Tenor'] 				: '';    
    $POS 					= (array_key_exists('POS', $people_data)) 					? $people_data['POS'] 					: '';    
    $age 					= (array_key_exists('age', $people_data)) 					? $people_data['age'] 					: '';
    $IR                                        = (array_key_exists('IRR', $people_data))                                       ? $people_data['IRR']                                   : '';

    $Product                                        = (array_key_exists('Product', $people_data))                                       ? $people_data['Product']                                   : '';
    
    $reportarray[]=array_merge(array("#"=>$i++,"DATA BASE"=>"", "BranchCode"=>$BranchCode, "ReAllocatedBranchCode"=>$ReAllocatedBranchCode, "City"=>$city, "State"=>$State, "Cluster"=>$Cluster, "Tier"=>$Tier, "LoanAccountNumber"=>$LoanAccountNumber, "CustId"=>$CustId, "Title"=>$CustomerTitle, "FirstName"=>$FirstName, "MiddleName"=>$MiddleName, "LastName"=>$LastName, "DisbursalDate"=>$DateofDisbursal, "Loanamount"=>$LoanAmt, "InterestRate"=>$IR, "Tenor"=>$Tenor, "EMIamount"=>$Gross_EMI, "NumberofEMIspaid"=>"", "POS"=>$POS,"Incremental"=>$IncrementalTag,"Age"=>$age,"Occupation"=>$Product, "SavedDateTime"=>$saved_date_time, "Agent Name"=> $agentname, "Date of lead generation"=>$LeadDate, "Month"=>$month, "offered loan amount"=>$off_loan_amt, "Lead ID"=>$LeadID, "Camp name"=>$camp, "Risk Status"=>$RiskStatus, "Gross Salary"=>$gross_sal, "Remarks"=>$dialer_remarks, "RO name"=>$roname, "RO Code"=>$ROCODE, "Sources"=>$Source));
    
    /* if(!isset($reportarray[$aline->user_id]))
    	$reportarray[]=array_merge(array("#"=>$i++,"DATA BASE"=>"", "BranchCode"=>$BranchCode, "ReAllocatedBranchCode"=>$ReAllocatedBranchCode, "City"=>$city, "State"=>$State, "Cluster"=>"", "Tier"=>$Tier, "LoanAccountNumber"=>$LoanAccountNumber, "CustId"=>$CustId, "Title"=>$CustomerTitle, "FirstName"=>"", "MiddleName"=>"", "LastName"=>"", "DisbursalDate"=>"", "Loanamount"=>"", "InterestRate"=>"", "Tenor"=>"", "EMIamount"=>"", "NumberofEMIspaid"=>"", "POS"=>"","Incremental"=>"","Age"=>"","Occupation"=>"", "SavedDateTime"=>"", "Agent Name"=>"", "Date of lead generation"=>"", "Month"=>"", "offered loan amount"=>"", "Agent Name"=>"", "Lead ID"=>"", "Camp name"=>"", "Risk Status"=>"", "Gross Salary"=>"", "Remarks"=>"", "RO name"=>"", "RO Code"=>"", "Sources"=>"")); */
}

if(Input::has("dllogxls"))
{
    include_once(app_path().'/lib/phpexcel/PHPExcel.php');
    
    $inputFileType = "Excel5";
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load("assets/extras/blank.xls");
    $baseRow = 2;


    $highestColumn = sizeof($reporthead);
    for ($head = 0; $head < $highestColumn; $head++){
        $colstr=PHPExcel_Cell::stringFromColumnIndex($head);
        $objPHPExcel->getActiveSheet()->setCellValue($colstr."1", $reporthead[$head]);
    }

    foreach($reportarray as $uid=>$uarr)
    {
            $row = $baseRow++;
            $col = 0;
            
            for ($head = 0; $head < $highestColumn; $head++){
                $colstr=PHPExcel_Cell::stringFromColumnIndex($head);
                $objPHPExcel->getActiveSheet()->setCellValue($colstr.$row, $uarr[$reporthead[$head]]);
            }
    }


    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="leads.xls"');
    header('Cache-Control: max-age=0');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $inputFileType);
    $objWriter->save('php://output');
    
    return;
}

$highestColumn = sizeof($reporthead);
$outhead="<tr>";$outstr="";
for ($head = 0; $head < $highestColumn; $head++){
	$outhead.="<td>".$reporthead[$head]."</td>";
}
$outhead.="</tr>";

foreach($reportarray as $uid=>$uarr)
{
	$outstr.="<tr>";
	for ($head = 0; $head < $highestColumn; $head++){
		$data_str = $uarr[$reporthead[$head]];
		$outstr.="<td>".$data_str."</td>";
	}
	$outstr.="</tr>";
}
$outstr.="<tr>";
?>


            <div class=innerAll>


<style>#logtable.td{vertical-align:top;}#logtable.tr{height:28px;overflow-y:hidden;}</style>
<script>
$(document).ready(function()
{
    if(!$('#dialoglog').hasClass('ui-dialog-content'))
    {
  $('#dialoglog').dialog({
    autoOpen: false,
    width: '70%',
    buttons: {
      "Ok": function() { 
    $(this).dialog("close"); 
      }, 
      "Cancel": function() { 
    $(this).dialog("close"); 
      } 
    }
  });
    }

  waitKeyUpRun("filter",function() { filter2(document.getElementById('filter'), 'logtable') },"2000");
  $('#modfrom').datepicker({dateFormat: 'yy-mm-dd',inline: true,changeYear: true,selectOtherMonths: true,yearRange: '<?php echo (date('Y')-70).":".(date('Y')+5); ?>'});
  $('#modto').datepicker({dateFormat: 'yy-mm-dd',inline: true,changeYear: true,selectOtherMonths: true,yearRange: '<?php echo (date('Y')-70).":".(date('Y')+5); ?>'});
});
</script>

<h4>Leads Report</h4>
<hr style="margin-bottom: 10px;">
<div style="float:left;width:80%">

Search <input id=filter name="filter" type="text" style="border:1px solid #efefef; padding: 2px 10px;"> 
Date : From<input size=10 id='modfrom' name='modfrom' type='text' value='<?php echo date("Y-m-d",$logdate); ?>' onchange='statusLogReloadFun("");' /> To <input size=10 id='modto' name='modto' type='text' value='<?php echo date("Y-m-d",$logdateto); ?>' onchange='statusLogReloadFun("");' />  Campaign <select id='modcamp' name='modcamp' onchange='statusLogReloadFun("");' style="width:20%"> <?php echo $options; ?>	</select>
</div>
<div style="float:left;width:20%">


    <button class="pull-right btn btn-sm btn-default" onclick='dlAgentlogXls();return false;' title='Download'><i class='fa fa-download'></i> Download</button>

</div>
<div style="clear:both"></div>
<hr style="margin:5px;">

<script>
function statusLogReloadFun(sortby)
{
    var sortstr='';if(sortby!="")sortstr="&sort="+sortby
    
    doAjax('dialer/leadsreport?logdate='+$("#modfrom").val()+'&logdateto='+$("#modto").val()+'&campaign='+$("#modcamp").val()+sortstr,'','rightmainreportdiv','ajax_dialer_reports','singlethis','GET');
}
function dlAgentlogXls()
{
	window.open('dialer/leadsreport?dllogxls=1&logdate='+$("#modfrom").val()+'&logdateto='+$("#modto").val()+'&campaign='+$("#modcamp").val());return false;
}
</script>

</div>

<script>
function displayMode(mode) {
    if(mode == 'percent') {
        $(".summation").hide();
        $(".percent").show();
    }
    else {
        $(".percent").hide();
        $(".summation").show();
    }
}
</script>

</div>



<div style='clear:both'></div>

<div style="overflow: auto; margin-top: 10px;">
<table id=logtable class='footable table table-striped table-bordered table-white table-primary footable-loaded' style='font-size:12px; margin:0; border:1px solid #BBB;'>
<thead>
    <?php echo $outhead; ?>
</thead>
<?php echo $outstr; ?>
</table>
</div>

<div id=dialoglog></div>

                
                
                
                
            </div>