$emailresult=$adb->pquery("SELECT email1 from vtiger_users",array());
$emailid=$adb->fetch_array($emailresult);
$emailaddress=$emailid[0];
$mailserveresult=$adb->pquery("SELECT server,server_username,server_password,smtp_auth FROM vtiger_systems where server_type = ?",array('email'));
$mailrow=$adb->fetch_array($mailserveresult);
$mailserver=$mailrow[0];
$mailuname=$mailrow[1];
$mailpwd=$mailrow[2];
$smtp_auth=$mailrow[3];
// End Email Setup
//query the vtiger_notificationscheduler vtiger_table and get data for those notifications which are active
$sql="select active from vtiger_notificationscheduler where schedulednotificationid=1";
$result=$adb->pquery($sql,array());
$activevalue=$adb->fetch_array($result);
if($activevalue[0]==1)
{
//Delayed Tasks Notification
//get all those activities where the status is not completed even after the passing of 24 hours
$today=date("Ymd");
$result=$adb->pquery("select vtiger_activity.status,vtiger_activity.activityid,subject,(vtiger_activity.date_start +1),vtiger_crmentity.smownerid from vtiger_activity inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_activity.activityid where vtiger_crmentity.deleted=0 and vtiger_activity.status <> 'Completed' and activitytype='Task' and ".$today." > (vtiger_activity.date_start+1)",array());
$sql="select active from vtiger_notificationscheduler where schedulednotificationid=2";
$result=$adb->pquery($sql,array());
$activevalue=$adb->fetch_array($result);
if($activevalue[0]==1)
{
$result=$adb->pquery("SELECT sales_stage,amount,potentialid,potentialname FROM vtiger_potential inner join vtiger_crmentity on vtiger_crmentity.crmid=vtiger_potential.potentialid where vtiger_crmentity.deleted=0 and sales_stage='Closed Won' and amount > 10000",array());
$sql="select active from vtiger_notificationscheduler where schedulednotificationid=3";
$result=$adb->pquery($sql,array());
$activevalue=$adb->fetch_array($result);
if($activevalue[0]==1)
{
$result=$adb->pquery("SELECT vtiger_troubletickets.status,ticketid FROM vtiger_troubletickets INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_troubletickets.ticketid WHERE vtiger_crmentity.deleted='0' AND vtiger_troubletickets.status <> 'Completed' AND vtiger_troubletickets.status <> 'Closed'",array());
//Too many tickets related to a particular vtiger_account/company causing concern
$sql="select active from vtiger_notificationscheduler where schedulednotificationid=4";
$result=$adb->pquery($sql,array());
$activevalue=$adb->fetch_array($result);
if($activevalue[0]==1)
{
$result=$adb->pquery("SELECT count(*) as count FROM vtiger_troubletickets INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_troubletickets.ticketid WHERE vtiger_crmentity.deleted='0' AND vtiger_troubletickets.status <> 'Completed' AND vtiger_troubletickets.status <> 'Closed'",array());
$count=$adb->query_result($result,0,'count');
//changes made to get too many tickets notification only when tickets count is greater than or equal to 5
$sql="select active from vtiger_notificationscheduler where schedulednotificationid=5";
$result=$adb->pquery($sql,array());
$activevalue=$adb->fetch_array($result);
if($activevalue[0]==1)
{
$result=$adb->pquery("SELECT vtiger_products.productname FROM vtiger_products inner join vtiger_crmentity on vtiger_products.productid = vtiger_crmentity.crmid where vtiger_crmentity.deleted=0 and start_date like ?",array(date('Y-m-d')."%"));
$sql="select active from vtiger_notificationscheduler where schedulednotificationid=6";
$result=$adb->pquery($sql,array());
$activevalue=$adb->fetch_array($result);
if($activevalue[0]==1)
{
$result=$adb->pquery("SELECT vtiger_products.productname from vtiger_products inner join vtiger_crmentity on vtiger_products.productid = vtiger_crmentity.crmid where vtiger_crmentity.deleted=0 and expiry_date like ?",array(date('Y-m-d')."%"));