CronJob will einfach nicht
Hey Leute, ich bins mal wieder^^
Ziel: Ein CronJob, der täglich prüft, ob eine "wiederkehrende Rechnung" fällig ist und wenn ja, eine Erinnerungs-Email verschickt.
Die betreffenden DB-Felder sehen so aus:
tl_yw_crm_invoices
Code:
CREATE TABLE `tl_yw_crm_invoices` (
`id` int(10) unsigned NOT NULL auto_increment,
`tstamp` int(10) unsigned NOT NULL default '0',
`title` varchar(64) NOT NULL default '',
`status` varchar(64) NOT NULL default '',
`date` int(10) unsigned NOT NULL default '0',
`recurring` char(1) NOT NULL default '',
`sendmaildate` int(10) unsigned NOT NULL default '0',
`recmailtoaddress` varchar(64) NOT NULL default '',
`recmailsent` char(1) NOT NULL default '0',
`customer` int(10) unsigned NOT NULL default '0',
`products` blob NULL,
`discount` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
tl_yw_crm_customers
Code:
CREATE TABLE `tl_yw_crm_customers` (
`id` int(10) unsigned NOT NULL auto_increment,
`tstamp` int(10) unsigned NOT NULL default '0',
`name` varchar(64) NOT NULL default '',
`address` varchar(64) NOT NULL default '',
`plz` int(4) NOT NULL default '0',
`location` varchar(64) NOT NULL default '',
`ctperson` varchar(64) NOT NULL default '',
`phone` varchar(64) NOT NULL default '',
`mobile` varchar(64) NOT NULL default '',
`email` varchar(64) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
tl_yw_crm_settings
Code:
CREATE TABLE `tl_yw_crm_settings` (
`id` int(10) unsigned NOT NULL auto_increment,
`tstamp` int(10) unsigned NOT NULL default '0',
`recmailsenderaddress` varchar(64) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
In der config.php steht folgendes für den CronJob:
PHP-Code:
// CronJobs
$GLOBALS['TL_CRON']['daily'][] = array('yw_crm_cronjob', 'checkrecurringinvoice');
Und in einer eigenstehenden Datei (system/modules/yw_crm/yw_crm_cronjob.php) steht mein Code:
PHP-Code:
class yw_crm_cronjob extends Backend
{
/**
* Send an email to the defined address if a recurring invoice is due
*/
public function checkrecurringinvoice()
{
// get current timestamp
$time = time();
// get all recurring invoices which haven't been sent yet (default = 0, sent = 1)
$objDate = $this->Database->prepare("SELECT id, recurring, sendmaildate, recmailsent FROM tl_yw_crm_invoices WHERE recurring=? AND recmailsent=?")
->execute(1,0);
if($objDate->NumRows < 1)
{
return;
}
while($objDate->next())
{
// check whether the current timestamp is lower than the sendmaildate (if so return because there's no mail due)
if($time < $objDate->sendmaildate)
{
return;
}
// send reminder mail
$objEmail = new Email();
// get sender address from database
$objSender = $this->Database->execute("SELECT recmailsenderaddress FROM tl_yw_crm_settings");
$objEmail->from = $objSender->recmailsenderaddress;
$objEmail->subject = 'Reminder for a recurring invoice';
// get data for the reminder text
$objText = $this->Database->execute("SELECT i.date,i.recmailtoaddress,c.name FROM tl_yw_crm_invoices i LEFT JOIN tl_yw_crm_customers c ON i.customer=c.id");
$objEmail->text = sprintf($GLOBALS['TL_LANG']['yw_crm_cronjob']['mailtext'],$objText->name,date("d.m.Y",$objText->date));
$objEmail->sendTo = $objText->recmailtoaddress;
// set mail status to 'sent'
$this->Database->prepare("UPDATE tl_yw_crm_invoices SET recmailsent=? WHERE id=?")
->execute(1,$objDate->id);
$this->log('Sent email reminder of a recurring invoice to: '.$objText->recmailtoaddress.' - yw_crm_cronjob checkrecurringinvoice()', TL_CRON);
}
}
}
Ich weiss, sieht nach viel Code aus, ist es aber nicht und ich bin kurz vorm verzweifeln :D
Wäre wirklich enorm froh, wenn sich das ein Coder mal anschauen könnte :D
Danke für eure Hilfe!!!