So, I'm trying to get this to email only once for the jobs that are at 75% of estimated cost, but something must be wrong because it keeps emailing everytime this code is called. Any help is appreciated.
PHP Code:function display() {
global $dbm;
print '<span class="header">Open Jobs Report</span><p>';
// Get the data for this report.
$results = $dbm->select("SELECT j_jobnumber, j_desc, j_datedue, j_clientid, j_graphicest, j_printingest, j_scansest, j_warningest FROM jobs WHERE j_dateclosed IS NULL ORDER BY j_jobnumber");
if ($dbm->num_rows($results) == 0) {
print 'There are no open jobs.';
} else {
print '<table width="'.REPORT_WIDTH.'"><tr><th>Job Number</th><th>Description</th><th>Date Due</th><th>Est. Cost</th><th>Cost to Date</th></tr>';
$color = '';
while ($data = $dbm->fetch_array($results)) {
// Determine the color for this line.
if ($color == '') {
$color = ' class="color"';
} else {
$color = '';
}
// Run subqueries to determine the cost to date.
$costToDate = 0;
$subData = $dbm->fetch_array($dbm->select("SELECT SUM(l_total) AS subtotal FROM labor WHERE l_jobnumber='" . $data['j_jobnumber'] . "'"));
//$laborD = $dbm->fetch_array($dbm->select("SELECT l_jobnumber FROM labor"));
$costToDate += $subData['subtotal'];
$subData = $dbm->fetch_array($dbm->select("SELECT SUM(f_fee) AS subtotal FROM fees WHERE f_jobnumber='" . $data['j_jobnumber'] . "'"));
//$feeD = $dbm->fetch_array($dbm->select("SELECT f_jobnumber FROM fees"));
$costToDate += $subData['subtotal'];
$subData = $dbm->fetch_array($dbm->select("SELECT SUM(m_total) AS subtotal FROM materials WHERE m_jobnumber='" . $data['j_jobnumber'] . "'"));
//$materialD = $dbm->fetch_array($dbm->select("SELECT m_jobnumber FROM materials"));
$costToDate += $subData['subtotal'];
$estCost = "$" . number_format(($data['j_graphicest'] + $data['j_printingest'] + $data['j_scansest']), 2);
// Email designer at 75% estimate used
$to = 'pg.test@hotmail.com';
$subject = "Job number " . $data['j_jobnumber'] . " has reached 75% of its estimate!";
$message = "Job number " . $data['j_jobnumber'] . " has reached 75% of its estimate!\n
Job Description: " . $data['j_desc'] . "\n
Cost to date: " . "$" . number_format($costToDate, 2) . "\n
Estimated Cost: " . $estCost . "\n
Client ID Number: " . $data['j_clientid'] . "\n";
$headers = 'From: pg.test@hotmail.com' . "\r\n" . 'Reply-To: pg.test@hotmail.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
// 75% of estimate used
$nearingEstimate = 0;
$tempestCost = $data['j_graphicest'] + $data['j_printingest'] + $data['j_scansest'];
$tempcostToDate = $costToDate;
if($tempestCost > 0) {
$nearingEstimate = $tempcostToDate / $tempestCost;
}
if($nearingEstimate >= 0.75 && !$data['j_warningest']) {
//send email to designer
mail($to,$subject,$message,$headers);
//update 0 to 1
$updateSQL = ("UPDATE jobs SET(" . $data['j_warningest'] . "=1) WHERE l_jobnumber='" . $data['j_jobnumber'] . "' OR f_jobnumber='" . $data['j_jobnumber'] . "' OR m_jobnumber='" . $data['j_jobnumber'] . "'");
mysql_query($updateSQL);
}
// Display the info on this job.
print '<tr'.$color.'>';
print '<td>'.$data['j_jobnumber'].'</td>';
print '<td>'.$data['j_desc'].'</td>';
print '<td>'.$data['j_datedue'].'</td>';
print '<td align="right">'.$estCost.'</td>';
print '<td align="right">$'.number_format($costToDate, 2).'</td>';
print '</tr>';
}
print '</table>';
}
if (!$this->printVersion) {
printVersionButton();
}
} // display




Bookmarks