<?php
/*
MailBeez Automatic Trigger Email Campaigns
http://www.mailbeez.com
Copyright (c) 2010 MailBeez
inspired and in parts based on
Copyright (c) 2003 osCommerce
Released under the GNU General Public License
*/
// make path work from admin
require_once(DIR_FS_CATALOG. 'mailhive/common/classes/mailbeez.php');
// could be in language-file
// just easier to define it in the mailbee
define('MAILBEEZ_LOYALTY_TEXT_TITLE', 'Contact Customers with loyality discount.');
define('MAILBEEZ_LOYALTY_TEXT_DESCRIPTION', 'Contact customers with loyality discount. Need to enable Loyalty Discount First.<br>
<br>');
class loyalty extends mailbeez {
// class constructor
function loyalty() {
// call constructor
mailbeez::mailbeez();
// set some stuff:
$this->code = 'loyalty';
$this->module = 'loyalty'; // same as folder name
$this->version = '1.1'; // float value
//$this->iteration = 1;
$this->iteration = $this->_getCheckInterval();
$this->title = MAILBEEZ_LOYALTY_TEXT_TITLE;
$this->description = MAILBEEZ_LOYALTY_TEXT_DESCRIPTION;
$this->sort_order = MAILBEEZ_LOYALTY_SORT_ORDER;
$this->enabled = ((MAILBEEZ_LOYALTY_STATUS == 'True') ? true : false);
$this->sender = MAILBEEZ_LOYALTY_SENDER;
$this->sender_name = MAILBEEZ_LOYALTY_SENDER_NAME;
$this->status_key = 'MAILBEEZ_LOYALTY_STATUS';
//$this->icon = 'icon.png';
$this->documentation_key = $this->module; // leave empty if no documentation available
// $this->documentation_root = 'http:://yoursite.com/' // modify documentation root if necessary
$this->htmlBodyTemplateResource = 'body_html.tpl'; // located in folder of this module
$this->txtBodyTemplateResource = 'body_txt.tpl'; // located in folder of this module
$this->subjectTemplateResource = 'subject.tpl'; // located in folder of this module
$this->audience = array();
$this->additionalFields = array('customers_id' => '007', 'customers_email_address' => 'customer@mail.com');
// list of additional fields to show in listing with testvalues used for Test-Mail
}
// class methods
function getAudience() {
global $languages_id;
//get cutoff days
$status = MAILBEEZ_LOYALTY_ORDER_STATUS_ID;
$min_amount = get_min_amount();
$cutoff_date = get_cutoffdate() ;
$date_passed = $this->dbdate(MAILBEEZ_LOYALTY_PASSED_DAYS);
/**
$query_raw = "select c.customers_firstname, c.customers_lastname,
c.customers_id, c.customers_email_address
from " . TABLE_CUSTOMERS. " c, " . TABLE_CUSTOMERS_INFO . " ci
left join " . TABLE_ORDERS . " o
on (o.customers_id = ci.customers_info_id) " .
( (MAILBEEZ_LOYALTY_CB == 'True') ?
" left join " . TABLE_CUSTOMERS_BASKET . " cb
on (cb.customers_id = ci.customers_info_id) " : '' ) . "
where o.customers_id is NULL " .
( (MAILBEEZ_LOYALTY_CB == 'True') ?
" and cb.customers_id is NULL " : '' ) . "
and ci.customers_info_id = c.customers_id
and ci.customers_info_date_account_created <= '" . $date_passed . "'
and ci.customers_info_date_account_created > '" . $date_skip . "' ";
**/
$query_raw = "select c.customers_firstname, c.customers_lastname,
c.customers_id, c.customers_email_address, sum(ot.value) as order_total, max(o.date_purchased) as last_purchased
from " . TABLE_CUSTOMERS. " c
left join " . TABLE_ORDERS . " o on (c.customers_id = o.customers_id)
left join " . TABLE_ORDERS_TOTAL . " ot on (o.orders_id = ot.orders_id)
where ot.class = 'ot_total'
and o.date_purchased >= date_sub(curdate(), interval ". $cutoff_date ." day)
group by o.customers_name
having order_total >= '" . $min_amount . "'
and last_purchased <= date_sub(curdate(), interval " . MAILBEEZ_LOYALTY_PASSED_DAYS . " day)
order by order_total DESC";
//echo $query_raw;
$query = mh_db_query($query_raw);
// for early check
$mb_chk = new mailbeez_mailer($this);
while ($item = mh_db_fetch_array($query)) {
// mandatory fields:
// - firstname
// - lastname
// - email_address
// - customers-id -> block
// other keys are replaced while sending: $<key>
// early check to avoid processing when email was already sent
$chk_result = $mb_chk->check($this->module, $this->iteration, $item['customers_id']);
if ($chk_result != false) {
// this iteration for this customer was already sent -> skip
continue;
}
$amount_cum_order = $item['order_total'];
$od_pc = get_discount_pc($amount_cum_order);
$this->audience[$item['customers_id']] = array('firstname' => $item['customers_firstname'],
'lastname' => $item['customers_lastname'],
'email_address' => $item['customers_email_address'],
'customers_id' => $item['customers_id'],
'cutoff_date' => $cutoff_date,
//'cutoff_date' => $query_raw,
'cum_total_order' => round($item['order_total'], 2),
'discount_pc' => $od_pc
);
}
return $this->audience;
}
function _getCheckInterval() {
switch ( MAILBEEZ_LOYALTY_INTERVAL ) {
case 'every day':
return date('Yz');
break;
case 'every week':
return date('YW');
break;
case 'every month':
return date('Ym');
break;
}
}
// installation methods
function keys() {
return array('MAILBEEZ_LOYALTY_STATUS', 'MAILBEEZ_LOYALTY_PASSED_DAYS', 'MAILBEEZ_LOYALTY_INTERVAL', 'MAILBEEZ_LOYALTY_SENDER', 'MAILBEEZ_LOYALTY_SENDER_NAME', 'MAILBEEZ_LOYALTY_SORT_ORDER');
}
function install() {
mh_insert_config_value(array('configuration_title' => 'Send loyalty discount email',
'configuration_key' => 'MAILBEEZ_LOYALTY_STATUS',
'configuration_value' => 'False',
'configuration_description' => 'Do you want to send loyalty discount emails?',
'set_function' => 'mh_cfg_select_option(array(\'True\', \'False\'), '
));
mh_insert_config_value(array('configuration_title' => 'Set days passed since last purchased',
'configuration_key' => 'MAILBEEZ_LOYALTY_PASSED_DAYS',
'configuration_value' => '3',
'configuration_description' => 'number of days since customer last purchased',
'set_function' => ''
));
mh_insert_config_value(array('configuration_title' => 'Interval',
'configuration_key' => 'MAILBEEZ_LOYALTY_INTERVAL',
'configuration_value' => 'every week',
'configuration_description' => 'How often do yo want to send out loyalty discount mail?',
'set_function' => 'mh_cfg_select_option(array(\'every week\', \'every month\'), '
));
mh_insert_config_value(array('configuration_title' => 'sender email',
'configuration_key' => 'MAILBEEZ_LOYALTY_SENDER',
'configuration_value' => STORE_OWNER_EMAIL_ADDRESS,
'configuration_description' => 'sender email',
'set_function' => ''
));
mh_insert_config_value(array('configuration_title' => 'sender name',
'configuration_key' => 'MAILBEEZ_LOYALTY_SENDER_NAME',
'configuration_value' => STORE_NAME,
'configuration_description' => 'sender email',
'set_function' => ''
));
mh_insert_config_value(array('configuration_title' => 'Sort order of display.',
'configuration_key' => 'MAILBEEZ_LOYALTY_SORT_ORDER',
'configuration_value' => '60',
'configuration_description' => 'Sort order of display. Lowest is displayed first.',
'set_function' => ''
));
}
}
?>