<?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_SALES_REPORT_TEXT_TITLE', 'Service: Monthly Sales Report');
define('MAILBEEZ_SALES_REPORT_TEXT_DESCRIPTION', 'This MailBeez Module sends you an email with monthly sales result<br>');
class sales_report extends mailbeez {
// class constructor
function sales_report() {
// call constructor
mailbeez::mailbeez();
// set some stuff:
$this->code = 'sales_report';
$this->module = 'sales_report';
$this->version = '1.0'; // float value
$this->required_mb_version = 1.1; // required mailbeez version
$this->iteration = $this->_getCheckInterval();
$this->title = MAILBEEZ_SALES_REPORT_TEXT_TITLE;
$this->description = MAILBEEZ_SALES_REPORT_TEXT_DESCRIPTION;
$this->sort_order = 900;
$this->enabled = ((MAILBEEZ_SALES_REPORT_STATUS == 'True') ? true : false);
$this->sender = MAILBEEZ_SALES_REPORT_SENDER;
$this->sender_name = MAILBEEZ_SALES_REPORT_SENDER_NAME;
$this->status_key = 'MAILBEEZ_SALES_REPORT_STATUS';
$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->htmlResultListTemplateResource = 'optlist_html.tpl'; // located in folder of this module
$this->txtResultListResource = 'optlist_txt.tpl'; // located in folder of this module
$this->audience = array();
$this->additionalFields = array('iteration' => $this->iteration); // list of additional fields to show in listing with testvalues
// list of additional fields to show in listing with testvalues used for Test-Mail
}
// class methods
function getAudience() {
$id = -5549; // the answer... ;-)
$day = date('j');
// early check to avoid processing when email was already sent
$mb_chk = new mailbeez_mailer($this);
$chk_result = $mb_chk->check($this->module, $this->iteration, $id);
//$chk_result = true; //no email
if ((MAILBEEZ_SALES_REPORT_RESEND == 'True') && defined('MAILBEEZ_SALES_REPORT_RESEND')) {$chk_result = false;} //overwite chk_result
if (($chk_result != false) ) {
// this iteration was already sent
return false;
}
//exit if store is closed
$current_time = time();
$day_of_week = strtoupper(date(l,$current_time));
//$isopen = constant('MODULE_SHIPPING_STOREHOURS_OPCL_' . $day_of_week);
//if ( $isopen == 0 ) { exit;}
$htmlListOut = '';
$txtListOut = '';
// load subtemplates
$htmlResultListTemplate = $this->loadResource($this->pathToMailbeez . $this->module. '/email/' . $this->htmlResultListTemplateResource);
$txtResultListTemplate = $this->loadResource($this->pathToMailbeez . $this->module. '/email/' . $this->txtResultListResource);
$month = date('m');
$last_month = $month -1;
$year = date('Y');
if ($last_month == 0 ) {
$last_month = 12;
$year = $year -1;
}
//give a month and year
if ((MAILBEEZ_SALES_REPORT_MONTH !=='') && defined('MAILBEEZ_SALES_REPORT_MONTH')) {
$pieces = explode("/", MAILBEEZ_SALES_REPORT_MONTH);
if ($pieces[0] !='') { $last_month = $pieces[0];}
if ($pieces[1] !='') { $year = $pieces[1];}
}
$query_raw = "SELECT MONTHNAME( ".TABLE_ORDERS.".date_purchased ) AS month,
YEAR( ".TABLE_ORDERS.".date_purchased ) AS year,
sum( ".TABLE_ORDERS_TOTAL.".value ) AS subtotal, COUNT( * ) AS num_orders, sum( " .TABLE_ORDERS." .order_total) AS total
FROM ".TABLE_ORDERS." LEFT JOIN ".TABLE_ORDERS_TOTAL." ON ".TABLE_ORDERS.".orders_id = ".TABLE_ORDERS_TOTAL.".orders_id
WHERE MONTH( ".TABLE_ORDERS.".date_purchased ) = '".$last_month."'
AND YEAR( ".TABLE_ORDERS.".date_purchased ) = '".$year."'
AND ".TABLE_ORDERS_TOTAL.".class = 'ot_subtotal'";
$results = mh_db_query($query_raw);
$subtotal = 0;
$total = 0;
$num_orders = 0;
while ($opt = mh_db_fetch_array($results)) {
$subtotal = round($opt['subtotal'], 2);
$total = round($opt['total'],2);
$num_orders = $opt['num_orders'];
$subTemplateVars = array('table' => $opt['month'],
'operation' => $opt['year'],
'type' => round ($opt['total'], 2),
'msg' => $opt['num_orders']
);
$htmlListOut .= $this->replace_variables($htmlResultListTemplate, $subTemplateVars);
$txtListOut .= $this->replace_variables($txtResultListTemplate, $subTemplateVars);
$monthname = $opt['month'];
$yearname= $opt['year'];
}
$orders_query_raw = "select o.orders_id, o.customers_id, o.customers_name, o.delivery_street_address,o.order_total,
o.payment_method, o.shipping_method, DATE_FORMAT(o.date_purchased, '%m/%d %h:%i' ) as date_purchased, ot.text as order_subtotal
from " . TABLE_ORDERS . " o
left join " . TABLE_ORDERS_TOTAL . " ot on (o.orders_id = ot.orders_id)
where ot.class = 'ot_subtotal'
AND MONTH( o.date_purchased ) = '".$last_month."'
AND YEAR( o.date_purchased ) = '".$year."'
order by o.orders_id DESC";
$order_results = mh_db_query($orders_query_raw);
//construct sale report
$fax_info = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
<header><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
table {
width: 100%;
font-size: 0.6em;
font-family: Arial, Helvetica, sans-serif;
margin-left:auto;
margin-right:auto;
border-collapse: collapse;
}
.dataTableContent {
font-size: 95%;
}
</style>
</header>
<BODY STYLE="font-family: Lucida Sans, sans-serif">
<u>
<h3> RED PASSION '. $monthname .' '. $year . ' Order Report: ' . preg_replace('/\(.*?\)/', '', STORE_NAME) . "</a></h3></u>" .
'<br><br><table border="1" width="100%" cellspacing="0" cellpadding="0">
<tr class="dataTableHeadingRow">
<th class="dataTableHeadingContent" align="left">ID</th>
<th class="dataTableHeadingContent" align="left">Name</th>
<th class="dataTableHeadingContent" align="left">Address</th>
<th class="dataTableHeadingContent" align="left">Total</th>
';
if ((MAILBEEZ_SALES_REPORT_TOTAL == 'False') && defined('MAILBEEZ_SALES_REPORT_TOTAL')) {
$fax_info .= '<th class="dataTableHeadingContent" align="right">SubT</th>';
}
$fax_info .= '
<th class="dataTableHeadingContent" align="left">Date</th>
<th class="dataTableHeadingContent" align="left">Type</th>
<th class="dataTableHeadingContent" align="left">Paid</th>
</tr>
';
while ($order_rows = mh_db_fetch_array($order_results)) {
$shipping = explode (' ', $order_rows['shipping_method']);
$payment = explode (' ', $order_rows['payment_method']);
$fax_info .= '
<tr class="dataTableRow" >
<td class="dataTableContent" align="left">' . $order_rows['orders_id'] . '</td>
<td class="dataTableContent" align="left">' . $order_rows['customers_name'] . '</td>
<td class="dataTableContent" align="left">' . $order_rows['delivery_street_address'] . '</td>
<td class="dataTableContent" align="left">' . $order_rows['order_total'] . '</td>
';
if ((MAILBEEZ_SALES_REPORT_TOTAL == 'False') && defined('MAILBEEZ_SALES_REPORT_TOTAL')) {
$fax_info .= '
<td class="dataTableContent" align="right">' . $order_rows['order_subtotal'] . '</td>
';
}
$fax_info .= '
<td class="dataTableContent" align="left">' . $order_rows['date_purchased'] . '</td>
<td class="dataTableContent" align="left">' . $shipping[0] . '</td>
<td class="dataTableContent" align="left">' . $payment[0] . '</td>
</tr>
';
}
$fax_info .= '
<tr>
<td class="dataTableContent" align="left" colspan="3" >Order Counts: ' . $num_orders . '</td>
';
if ((MAILBEEZ_SALES_REPORT_TOTAL == 'False') && defined('MAILBEEZ_SALES_REPORT_TOTAL')) {
$fax_info .= '
<td class="dataTableContent" align="left" colspan="3" > Subtotal: $' . $subtotal . '</td>
';
}
$fax_info .= '
<td class="dataTableContent" align="Right" colspan="3" >Total: $' . $total . '</td>
</tr>
';
$fax_info .= '</table></body></html>';
$JattachArray = array();
$attachFileTextContent = $fax_info;
//$JattachArray = array("raw_data" => $attachFileTextContent, "order_number" => $zf_insert_id );
$JattachArray['raw_data'] = $attachFileTextContent;
$JattachArray['mime_type'] = 'text/html';
$JattachArray['name'] = STORE_NAME . '.html'; // filename that the $attachFileTextContent attachment will have inside the email
// $html_msg = array('EMAIL_SUBJECT' => MODULE_SHIPPING_STORE_FAX_NUMBER . SEND_EXTRA_ORDER_EMAILS_TO,
// 'EMAIL_MESSAGE_HTML' => $fax_info); // currently complete HTML mail
// zen_mail('', 'chinagarden826@gmail.com', MODULE_SHIPPING_STORE_FAX_NUMBER . SEND_EXTRA_ORDER_EMAILS_TO,
// 'Please see the this month sales report on attachment\r\n\r\n', STORE_NAME, EMAIL_FROM, 'none', 'checkout_extra', $JattachArray);
/**
$pieces = explode(",", SEND_EXTRA_ORDER_EMAILS_TO);
if (strpos($pieces[0], 'sales@') === false) {
zen_mail('', $pieces[0], MODULE_SHIPPING_STORE_FAX_NUMBER . SEND_EXTRA_ORDER_EMAILS_TO,
'Please see the this month sales report on attachment\r\n\r\n', STORE_NAME, EMAIL_FROM, 'none', 'checkout_extra', $JattachArray);
}
***/
if ((MAILBEEZ_SALES_REPORT_TAX == 'True') && defined('MAILBEEZ_SALES_REPORT_TAX')) {
//upload to rcfax
$myurl = 'https://service.ringcentral.com/faxapi.asp'; // RingCentral FaxOut API URL
$RCUsername = "18883873312"; // RingCentral Fax Username (10-digit RC phone number)
$RCPassword = "Alena1129"; // RingCentral password (5-10 numeric digits)
$recipientFax = MODULE_SHIPPING_STORE_FAX_NUMBER; // Recipient 10-digit Fax number
//set data (in this example from post)
$mypostdata = array(
'Username' => $RCUsername,
'Password' => $RCPassword,
'Recipient' => $recipientFax,
'coverpage' => '0', //None =0
'Resolution' => '2' //Low =2
);
//sample image
$files['Attachment'] = $JattachArray['name'] ;
//ringcentral api
$file_name= STORE_NAME . '.html';
//$api_result = do_post_request($myurl, $mypostdata, $fax_info, $files);
$api_result = send_rcfax($file_name, $fax_info);
// zen_mail('', '8883873312@rcfax.com', '',
// 'Please see the this month sales report on attachment\r\n\r\n', STORE_NAME, 'spanc07@gmail.com', 'none', 'checkout_extra', $JattachArray);
// zen_mail('', MODULE_SHIPPING_STORE_FAX_NUMBER . '@rcfax.com', '',
// 'Please see the this month sales report on attachment\r\n\r\n', STORE_NAME, 'spanc07@gmail.com', 'none', 'checkout_extra', $JattachArray);
}
//return false;
$this->audience[$id] = array('firstname' => MAILBEEZ_SALES_REPORT_SENDER_NAME,
'lastname' => ' - Monthly Sales Report',
'email_address' => MAILBEEZ_SALES_REPORT_SENDER,
'customers_id' => $id,
'iteration' => $this->iteration,
'table' => $monthname,
'operation' => $yearname,
'opt_result_html' => $htmlListOut,
'opt_result_txt' => $txtListOut
);
return $this->audience;
}
function _getCheckInterval() {
switch ( MAILBEEZ_SALES_REPORT_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_SALES_REPORT_STATUS', 'MAILBEEZ_SALES_REPORT_SENDER', 'MAILBEEZ_SALES_REPORT_TOTAL',
'MAILBEEZ_SALES_REPORT_TAX', 'MAILBEEZ_SALES_REPORT_RESEND', 'MAILBEEZ_SALES_REPORT_MONTH', 'MAILBEEZ_SALES_REPORT_SENDER_NAME', 'MAILBEEZ_SALES_REPORT_INTERVAL');
}
function install() {
mh_insert_config_value(array('configuration_title' => 'Run a Sale Report',
'configuration_key' => 'MAILBEEZ_SALES_REPORT_STATUS',
'configuration_value' => 'True',
'configuration_description' => 'Do you want to run a monthly sales report?',
'set_function' => 'mh_cfg_select_option(array(\'True\', \'False\'), '
));
mh_insert_config_value(array('configuration_title' => 'Total (True) or Subtotal (False)',
'configuration_key' => 'MAILBEEZ_SALES_REPORT_TOTAL',
'configuration_value' => 'True',
'configuration_description' => 'Do you want Total (True) or Subtotal (False) sales report?',
'set_function' => 'mh_cfg_select_option(array(\'True\', \'False\'), '
));
mh_insert_config_value(array('configuration_title' => 'Send a Fax',
'configuration_key' => 'MAILBEEZ_SALES_REPORT_TAX',
'configuration_value' => 'False',
'configuration_description' => 'Do you want to fax sales report?',
'set_function' => 'mh_cfg_select_option(array(\'True\', \'False\'), '
));
mh_insert_config_value(array('configuration_title' => 'Resend Sale Report',
'configuration_key' => 'MAILBEEZ_SALES_REPORT_RESEND',
'configuration_value' => 'False',
'configuration_description' => 'Do you want to resend sales report?',
'set_function' => 'mh_cfg_select_option(array(\'True\', \'False\'), '
));
mh_insert_config_value(array('configuration_title' => 'Resend Sale Report on Month',
'configuration_key' => 'MAILBEEZ_SALES_REPORT_MONTH',
'configuration_value' => '',
'configuration_description' => 'Resend sales report of MM/YYYY',
'set_function' => ''
));
mh_insert_config_value(array('configuration_title' => 'Interval',
'configuration_key' => 'MAILBEEZ_SALES_REPORT_INTERVAL',
'configuration_value' => 'every month',
'configuration_description' => 'How often do yo want to run monthly sales report?',
'set_function' => ''
));
mh_insert_config_value(array('configuration_title' => 'sender and receiver email',
'configuration_key' => 'MAILBEEZ_SALES_REPORT_SENDER',
'configuration_value' => STORE_OWNER_EMAIL_ADDRESS,
'configuration_description' => 'sender email',
'set_function' => ''
));
mh_insert_config_value(array('configuration_title' => 'sender and receiver name',
'configuration_key' => 'MAILBEEZ_SALES_REPORT_SENDER_NAME',
'configuration_value' => STORE_NAME,
'configuration_description' => 'sender email',
'set_function' => ''
));
}
}
?>