I have build a contract management database with appgini and i have an issue on receiving automatic email notifications for contacts that are near expiration date. What i have done:
So I have created a calculated field that is called alert and it is calculated with an if statement depending of the result of the diff between the finish date and the Today date. Than i have used hooks before and after update to send email notification when the field alert is changed.
Code: Select all
function Kontratat_before_update(&$data, $memberInfo, &$args) {
$id_kontrata = makeSafe($data['selectedID']);
$res = sql("select * from kontratat where id_kontrata='{$id_kontrata}'", $eo);
$GLOBALS['old_data'] = db_fetch_assoc($res);
return TRUE;
}
function Kontratat_after_update($data, $memberInfo, &$args) {
$id_kontrata = makeSafe($data['selectedID']);
$data['alert'] = sqlValue("SELECT IF(status!='Ongoing','',
IF(DATEDIFF(finish_date,NOW()) > 60,'',
IF(DATEDIFF(finish_date, NOW()) > 30,'2 months',
IF(DATEDIFF(finish_date, NOW()) > 0,'1 month','Expired'))))
FROM Kontratat WHERE id_kontrata = '{$id_kontrata}'");
if($data['alert'] != $GLOBALS['old_data']['alert'])
{
sendmail(array(
'to' => '[email protected]',
'name' => 'Contract Alert',
'subject' => 'Alert Kontrata nr. ' . $data['id_kontrata'].' - '.$data['objekti_kontrata'],
'message' =>'old data'. $GLOBALS['old_data']['alert'].'new data'.$data['alert']
));
}
return TRUE;
}
But it doesn't work automatically. When the date is about to expire i see that the field alert is updated on the background but i dont receive email notification. Please any tip what should i do?