Files
email_reports/index.php
franknstayn 3c58240206 update
2020-02-29 06:48:02 -06:00

104 lines
3.7 KiB
PHP
Executable File

<?php
include 'dbconfig.php';
require("phpmailer/class.phpmailer.php");
date_default_timezone_set('America/Chicago');
$dateToday = date('Y-m-d');
//$dateToday = "2019-12-12";
$dateTimeToday = date("Y-m-d H:i:s");
//grab all rows from the table where state is "new" and put into array
$q = $conn->prepare("SELECT t.StoreName, o.CartKey, o.ProductName, o.NAME, o.NUMBER, o.Size, o.JerseySize, o.ShortsSize, o.Price, o.Quantity, (o.Price * o.Quantity) AS TotalPrice, ((o.Price * o.Quantity) * 0.10) AS Tax,
o.DateCreated, pd.InvoiceNumber, pd.Payer_Email FROM orders AS o
INNER JOIN payment_details AS pd ON pd.CartKey = o.CartKey
INNER JOIN teamstores AS t ON t.Id = o.StoreId
WHERE o.DateCreated BETWEEN '$dateToday 00:00:00' AND '$dateToday 23:59:00'
ORDER BY o.DateCreated");
$q->execute();
$result = $q->rowCount();
if($result > 0){
$invoice_array = array();
$filename = 'daily_order_report_'.$dateToday.'.csv';
$headers = array('StoreName', 'ProductName', 'NAME', 'NUMBER', 'Size', 'JerseySize', 'ShortsSize', 'Price', 'Quantity', 'TotalPrice', 'Tax', 'DateCreated', 'InvoiceNumber', 'Payer_Email');
$fp = fopen('daily_order_reports/'.$filename, 'w');
fputcsv($fp, $headers);
while ($row = $q->fetch()) {
$value = $row['CartKey'] . "|" . $row['InvoiceNumber'];
if(!in_array($value, $invoice_array, true)){
array_push($invoice_array, $value);
}
$lineData = array($row['StoreName'], $row['ProductName'], $row['NAME'], $row['NUMBER'], $row['Size'], $row['JerseySize'], $row['ShortsSize'], $row['Price'], $row['Quantity'], $row['TotalPrice'], $row['Tax'], $row['DateCreated'], $row['InvoiceNumber'], $row['Payer_Email']);
fputcsv($fp, $lineData);
}
$report_link = "https://www.crewsportswear.com/email_reports/daily_reports_with_image.php?d=". $dateToday;
$body = "<html>";
$body .= "<body>";
$body .= "<p> Please download the attached file for today's order report.</p>";
$body .= " <br><br>";
$body .= "<p>For the order report with image please <a href='".$report_link ."'>click here</a>.</p>";
$body .= "<p>####################################</p>";
$body .= "<p>Order Details per Invoice</p>";
foreach($invoice_array as $invoice){
$v = explode("|", $invoice);
$body .= "<div>Invoice Number: <a href='http://admin.crewsportswear.com:23000/admin/orders/view/" . $v['0']."/print'>" . $v['1'] . "</a></div>";
}
$body .= "</body>";
$body .= "</html>";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = true;
// $mail->SMTPDebug = 3; // uncomment for debug mode.
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = "587";
//gmail account
$mail->Username = 'no-reply@crewsportswear.com';
$mail->Password = '20HustleHard19!';
$mail->SetFrom('orders@crewsportswear.com', 'CREW Daily Order Report');
$mail->addAddress('graphics@crewsportswear.com');
$mail->addBCC("webmaster@crewsportswear.com");
$mail->addBCC("angelo@crewsportswear.com");
$mail->addAttachment('daily_order_reports/'.$filename);
$mail->Subject = 'Daily Report ' . $dateToday;
$mail->Body = $body;
$mail->ErrorInfo;
$mail->IsHTML(true);
// return $mail->Send();
if(!$mail->Send()){
$msg = $dateTimeToday . "\t\t\tMailer Error: " . $mail->ErrorInfo .'\n';
file_put_contents('email.log', print_r($msg, true), FILE_APPEND);
}else {
$msg = $dateTimeToday . "\t\t\tsuccessfully sent\n";
file_put_contents('email.log', print_r($msg, true), FILE_APPEND);
}
}else{
$msg = $dateTimeToday . "\t\t\tNo order for today\n";
file_put_contents('email.log', print_r($msg, true), FILE_APPEND);
}
?>