Newer
Older
ubFramework / Portal / cron / dialer / predictive_dialer_003_cron.php
@Christopher W. Olsen Christopher W. Olsen on 10 Dec 2017 8 KB Cleaning Up Making It A Sub Module
#!/usr/local/bin/php

<?php

/*
 * (C) 2014, 2015 Christopher Olsen <cwolsen@uBixTechnologies.com>
 * 
 * $Id: predictive_dialer_003_cron.php 2230 2015-06-17 19:03:56Z reddawg $
 * 
 */

if (file_exists("/tmp/predictive_dialer_003_cron.lock"))
  exit(0);

$working_dir =  dirname(__FILE__);

$log_file = fopen($working_dir . "/../../log/cron/predictive_dialer_003_cron.log", "a");

fwrite($log_file, "Start: " . date("Y-m-d H:i:s") . "\n");

system("/usr/bin/touch /tmp/predictive_dialer_003_cron.lock");

/* Connect To The Required Databases */
$data['ubix_db']   = new mysqli("10.0.10.59", "ubixcube", "5558585", "ubixcube");
$data['dialer_db'] = new mysqli("10.0.10.54", "dialer",   "5558585", "as_dialer_003");
/* End of Database Connections */

/* Get The Last Time This Script Ran */
$query = "SELECT value FROM system_var WHERE name = 'predictive_dialer_003_cron_last_run'";

if (($result = $data['ubix_db']->query($query)) === false) {
  mail("cwolsen@ubixos.com", "Dialer 003 Cron Failed!", "MySQL Error: Line " . __LINE__ . "\n");
  fwrite($log_file, "MySQL Error: Line " . __LINE__ . "\n");
  fclose($log_file);
  exit(1);
}

if ($result->num_rows == 0) {
  $query = "INSERT INTO system_var (name,value) VALUES('predictive_dialer_003_cron_last_run',0)";
  $data['ubix_db']->query($query);
  $last_run = 0;
}
else {
  $qData = $result->fetch_row();
  $last_run = intval($qData[0]);
  $result->free();
}

$bTime = time();
  
$sdate = date("Y-m-d", $last_run);
$edate = date("Y-m-d", $bTime);

fwrite($log_file, "Info - sdate: " . $sdate . ", edate: " . $edate . ", last_run: " . $last_run . ", bTime: " . $bTime . "\n");

/*
 * 
 * Align Broadcast Campaign Calls To 6 Seconds
 * 
 */

$query = "UPDATE vicidial_log set length_in_sec = CEIL(length_in_sec/6)*6 WHERE length_in_sec > 0 AND call_date >= '$sdate 00:00:00' and call_date <= '$edate 23:59:59' AND end_epoch IS NOT NULL AND MOD(length_in_sec,6) != 0";
if ($data['dialer_db']->query($query) === true)
	fwrite($log_file, "Broadcast Campaign Calls 6 Second Aligned: " . $data['dialer_db']->affected_rows . " Affected Rows\n");
else
	fwrite($log_file, "MySQL Error: Line " . __LINE__ . "\n");

/*
 *
 * Calculate Daily Spending
 *
 * Update Each Campaign
 * Update Account Record
 *
 */

$query = "SELECT aid, first_name, email, dialer_group, dialer_prepay, dialer_cpm, company FROM accounts WHERE dialer_group != 'NA' AND predictive_server = 'as-dialer-003'";

if (($aResult = $data['ubix_db']->query($query)) === false) {
  fwrite($log_file, "MySQL Error: Line - " . __LINE__ . ", Query - [$query]\n");
  exit(1);
}

while ($aData = $aResult->fetch_assoc()) {
  $query = "SELECT campaign_id, campaign_name, daily_limit, daily_total, daily_notification, active FROM vicidial_campaigns WHERE user_group = '" . $aData['dialer_group'] . "'";

  if (($vcResult = $data['dialer_db']->query($query)) === false) {
    fwrite($log_file, "MySQL Error: Line - " . __LINE__ . ", Query - [$query]\n");
	exit(1);
  }

  /* Spending For All Campaigns On Account */
  $predictive_daily_total = 0.0000;

  while ($vcData = $vcResult->fetch_assoc()) {
    $query = "SELECT SUM(length_in_sec) as total_time, COUNT(*) as total_records FROM vicidial_log WHERE campaign_id = " . $vcData['campaign_id'] . " AND call_date >= '$sdate 00:00:00' and call_date <= '$edate 23:59:59'";

    $vlResult = $data['dialer_db']->query($query);
    
    if ($vlResult === true && $vlResult->num_rows > 0) {
      $vlData = $vlResult->fetch_assoc();

      if ($vlData['total_time'] > 0) {
        $daily_total = ($vlData['total_time']/60) * $aData['dialer_cpm'];
	    $predictive_daily_total += $daily_total;

        $aData['dialer_prepay'] -= $daily_total;

	    fwrite($log_file, "TT: [" . $vlData['total_time'] . ":" . $aData['dialer_cpm'] . "][" . $vcData['daily_total'] . ":" . $daily_total . "][" . $vlData['total_records'] . "]\n");

	    if ($daily_total > $vcData['daily_limit']) {
	      $query = "UPDATE vicidial_campaigns SET daily_total = " . $daily_total . ", active = 'N' WHERE campaign_id = '" . $vcData['campaign_id'] . "'";
		  if ($vcData['active'] == "Y")
		    sendNotification($aData['email'], $aData['first_name'], $vcData['campaign_name'], $vcData['campaign_id'], $vcData['daily_limit'], $daily_total, 100);
	    }
	    else {
	      $lP = floor(($daily_total / $vcData['daily_limit']) / .25) * 25;
		  $query = "UPDATE vicidial_campaigns SET daily_total = " . $daily_total . ", daily_notification = " . $lP . " WHERE campaign_id = '" . $vcData['campaign_id'] . "'";

		  if ($vcData['daily_notification'] < $lP)
		    sendNotification($aData['email'], $aData['first_name'], $vcData['campaign_name'], $vcData['campaign_id'], $vcData['daily_limit'], $daily_total, $lP);
	    }

	  if ($data['dialer_db']->query($query) === false)
		fwrite($log_file, "MySQL Error - [$query] on line " . __LINE__ . "\n");
	  }
      $vlResult->free();
    }
  }

  $query = "UPDATE accounts SET predictive_daily_total = " . $predictive_daily_total . " WHERE aid = " . $aData['aid'];

  if ($data['ubix_db']->query($query) === false)
    fwrite($log_file, "MySQL Error - [$query] on line " . __LINE__ . "\n");
	 
  fwrite($log_file, "Adjusted Predictive Daily Total to " . $predictive_daily_total . " for " . $aData['company'] . " with bTime - $bTime\n");

  $vcResult->free();
}

$aResult->free();

$query = "UPDATE system_var SET value = '" . $bTime . "' WHERE name = 'predictive_dialer_003_cron_last_run'";
$data['ubix_db']->query($query);

fwrite($log_file, "Adjust System Var: [$query]\n");

fwrite($log_file, "Finish: " . date("Y-m-d H:i:s") . "\n");

fclose($log_file);

system("/bin/rm /tmp/predictive_dialer_003_cron.lock");

function sendNotification($recipient,$first_name,$campaign_name,$campaign_id,$limit,$total,$lP=100) {
  $random_hash = md5(date('r', time()));

  $message_body = "--PHP-alt-" . $random_hash . "\nContent-Type: text/plain; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: 7bit\n\n";

  if ($lP == 100) {
    $subject = "uBix Dialer Notification - Daily Spending Limit Has Been Reached";

    $message_body .= "Dear $first_name,\n\nWe wanted to let you know that daily spending on " . $campaign_name . " has reached $" . number_format($total,2) . "\n\n"; 
    $message_body .= "Your daily limit for this campaign is $" . number_format($limit,2) . "\n\nYour campaign has been automatically disabled.\n\nYou may want to consider adjusting your daily spending limit to reactivate your campaign.\n\nSincerely,\n\nuBix Technologies Support\n516-986-UBIX (8249)\n\n";
      
    $message_body .= "--PHP-alt-" . $random_hash . "\nContent-Type: text/html; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: 7bit\n\n";

    $message_body .= "Dear $first_name,<br><br>\n\nWe wanted to let you know that daily spending on " . $campaign_name . " has reached $" . number_format($total,2) . "<br>\n<br>\n"; 
    $message_body .= "Your daily limit for this campaign is $" . number_format($limit,2) . "<br>\n<br>\nYour campaign has been automatically disabled.<br>\n<br>\nYou may want to consider adjusting your <a href=\"https://manage.ubixtechnologies.net/dialer/edit.php?data[cid]=" . $campaign_id . "\">daily spending limit</a> to reactivate your campaign.<br>\n<br>\nSincerely,<br>\n<br>\nuBix Technologies Support<br>\n516-986-UBIX (8249)<br>\n\n";
    } 
  else {
    $subject = "uBix Dialer Notification - Daily Spending Has Reached " . $lP . "%";

    $message_body .= "Dear $first_name,\n\nWe wanted to let you know that daily spending on " . $campaign_name . " has reached $" . number_format($total,2) . "\n\n"; 
    $message_body .= "Your daily limit for this campaign is $" . number_format($limit,2) . "\n\nYou may want to consider adjusting your dial speed if you feel this has been reached to fast or slow.\n\nSincerely,\n\nuBix Technologies Support\n516-986-UBIX (8249)\n\n";
      
    $message_body .= "--PHP-alt-" . $random_hash . "\nContent-Type: text/html; charset=\"iso-8859-1\"\nContent-Transfer-Encoding: 7bit\n\n";

    $message_body .= "Dear $first_name,<br><br>\n\nWe wanted to let you know that daily spending on " . $campaign_name . " has reached $" . number_format($total,2) . "<br>\n<br>\n"; 
    $message_body .= "Your daily limit for this campaign is $" . number_format($limit,2) . "<br>\n<br>\nYou may want to consider adjusting your <a href=\"https://manage.ubixtechnologies.net/dialer/edit.php?data[cid]=" . $campaign_id . "\">dial speed</a> if you feel this has been reached to fast or slow.<br>\n<br>\nSincerely,<br>\n<br>\nuBix Technologies Support<br>\n516-986-UBIX (8249)<br>\n\n";
    }

  $message_body .= "--PHP-alt-" . $random_hash . "--\n";

  $header = "From: uBix Technologies <support@ubixtechnologies.com>\r\n";
  $header .= "MIME-Version: 1.0\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"\n";

  mail($recipient, $subject, $message_body, $header);
  mail("cwolsen@ubixos.com", $subject, $message_body, $header);
}

?>