Creating Custom 30 days product sales report CSV file and send to admin via email cron in D6
Categories
Component ID
2183511
Component name
Creating Custom 30 days product sales report CSV file and send to admin via email cron in D6
Component type
module
Maintenance status
Development status
Component security advisory coverage
not-covered
Component created
Component changed
Component body
$site_email = variable_get('site_mail', '');
$today_date = mktime(0,0,0,date("m"),date("d"),date("Y"));
"Tomorrow is ".date("Y/m/d", $today_date);
$tomorrow = mktime(0,0,0,date("m"),date("d")-600,date("Y"));
"Tomorrow is ".date("Y/m/d", $tomorrow);
$sql = "SELECT uc_order_products.order_product_id AS order_product_id, uc_orders.order_id AS uc_orders_order_id, uc_orders.uid AS uc_orders_uid, node.title AS node_title, uc_orders.order_status AS uc_orders_order_status, uc_orders.primary_email AS uc_orders_primary_email, uc_order_products.qty AS uc_order_products_qty, uc_products.sell_price AS uc_products_sell_price, uc_orders.created AS uc_orders_created FROM uc_order_products uc_order_products LEFT JOIN node node ON uc_order_products.nid = node.nid LEFT JOIN uc_orders uc_orders ON uc_order_products.order_id = uc_orders.order_id LEFT JOIN uc_products uc_products ON uc_order_products.nid = uc_products.nid WHERE (node.type in ('product')) AND (node.status <> 0) ORDER BY uc_orders_created DESC";
$results = db_query($sql); // Run the query
$file_save_path = base_path() . 'sites/default/files/productsale_report_csvs/';
$output = "";
$output.= 'User id,User Name, User Email, User Created Date ';
$output .="
";
$headers = array('Order product ID','Order ID','User Id','Product Title','Order Status','User Email','Quantity','Price($)','Order Created Date');
$filename = 'product_sale_records_'.date('d_m_y_h_i_s').'.csv'; // exit;
$folder_path = $_SERVER['DOCUMENT_ROOT'] . $file_save_path;
$path = $_SERVER['DOCUMENT_ROOT'] . $file_save_path . $filename;
$fp = fopen($path, 'w+');
if ($fp && $results) {
/*header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename=' . $filename);
header('pragma: no-cache');
header('Expires : 0');*/
fputcsv($fp, array_values($headers));
while ($row = db_fetch_array($results)) { // Get the next result, which will/must always be a single field
$row['uc_orders_created'] = date('M d, Y', $row['uc_orders_created']);
fputcsv($fp, array_values($row));
}
fclose($fp);
$mailto = 'xyz@example.com';
$from_mail = 'example_demo@demo.com';
$replyto = '';
$subject = "product sale reports ";
$message = "Dear Admin,";
$message .="
";
$message .= "Please find the last 30 day product sales reports.";
mail_attachment($filename, $folder_path, $mailto, $from_mail, $from_name, $replyto, $subject, $message);
exit;
}
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">
";
$header .= "Reply-To: ".$replyto."
";
$header .= "MIME-Version: 1.0
";
$header .= "Content-Type: multipart/mixed; boundary="".$uid.""
";
$header .= "This is a multi-part message in MIME format.
";
$header .= "--".$uid."
";
$header .= "Content-type:text/plain; charset=iso-8859-1
";
$header .= "Content-Transfer-Encoding: 7bit
";
$header .= $message."
";
$header .= "--".$uid."
";
$header .= "Content-Type: application/octet-stream; name="".$filename.""
"; // use different content types here
$header .= "Content-Transfer-Encoding: base64
";
$header .= "Content-Disposition: attachment; filename="".$filename.""
";
$header .= $content."
";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
//echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
