bebeboutik/modules/loyalty/bin/reminder.php
2018-01-30 17:47:03 +01:00

119 lines
3.2 KiB
PHP

<?php
$_SERVER['HTTP_HOST'] = 'www.bebeboutik.com';
$_SERVER['SERVER_NAME'] = 'www.bebeboutik.com';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['HTTP_PORT'] = 443;
$_SERVER['HTTP_X_FORWARDED_PORT'] = 443;
$_SERVER['SERVER_PORT'] = 443;
include __DIR__ . '/../../../config/settings.inc.php';
include __DIR__ . '/../../../config/config.inc.php';
if(!Tools::isCli()){
exit();
}
$longopts = array(
'verbose',
'dry-run',
'date:',
'cron',
);
$shortopts = "";
$options = getopt($shortopts, $longopts);
// Options
$optVerbose = false;
if (isset($options['verbose'])) {
$optVerbose = true;
}
$optTest = false;
if (isset($options['dry-run'])) {
$optTest = true;
}
$optCron = false;
if (isset($options['cron'])) {
$optCron = true;
}
// Date
$delay = new DateInterval('P1M');
$dateSelect = new DateTime();
if (isset($options['date'])) {
$dateSelect->createFromFormat('Ymd', $options['date']);
} else {
$dateSelect->add($delay)->sub(new DateInterval('P1D'));
}
// Start
echo date('Y-m-d H:i:s')." - START ".$dateSelect->format('Y-m-d')." \n";
$dateSqlStart = $dateSelect->format('Y-m-d').' 00:00:00';
$dateSqlEnd = $dateSelect->format('Y-m-d').' 23:59:59';
if ($optVerbose) {
echo date('Y-m-d H:i:s')." - START DATE ".$dateSqlStart." to ".$dateSqlEnd." \n";
}
$sql = "
SELECT
d.id_discount,
d.id_customer,
d.date_from,
d.date_to,
l.id_order
FROM `"._DB_PREFIX_."discount` d, `"._DB_PREFIX_."loyalty` l
LEFT OUTER JOIN `"._DB_PREFIX_."order_discount` od ON (od.id_discount IS NULL)
WHERE d.active=1 AND d.`quantity` != 0 AND d.id_discount=l.id_discount
AND d.date_to > '".$dateSqlStart."' AND d.date_to < '".$dateSqlEnd."'";
$result = Db::getInstance()->ExecuteS($sql);
if ($result === false) {
echo $sql;
echo "\n";
} else {
$nb = count($result);
if ($optVerbose) { echo date('YmdHis')." - $nb mails to send \n"; }
if ($nb > 0) {
foreach($result as $l) {
// Customer infos
$customerModel = new Customer($l['id_customer']);
// Order infos
$orderModel = new Order($l['id_order']);
$id_lang = $orderModel->id_lang;
$templateVars = array(
'{firstname}' => $customerModel->firstname,
'{lastname}' => $customerModel->lastname,
'{commandenum}' => $orderModel->id,
);
echo date('Y-m-d H:i:s')." - Rappel credit pour le client ".$customerModel->email;
$to = $customerModel->email;
// Only Display values
if ($optTest) {
print_r($templateVars);
echo "\n";
}
// Send mail
else {
$isSent = Mail::Send($id_lang, 'loyalty_discount_reminder',
Mail::l('Your loyalty credits', $id_lang), $templateVars, $to,
$customerModel->firstname.' '.$customerModel->lastname);
if ($isSent) {
echo " - Envoyé";
}
else {
echo " - ERROR";
}
}
echo "\n";
}
}
}
echo date('Y-m-d H:i:s')." - END\n";