Files
email_reports/index.php
Frank John Begornia d0d82aa8e1 Unified email reports
2026-01-02 01:19:07 +08:00

110 lines
4.1 KiB
PHP

<?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 = (getenv('APP_URL') ?: '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);
$admin_url = getenv('ADMIN_URL') ?: 'https://admin.crewsportswear.app';
$body .= "<div>Invoice Number: <a href='" . $admin_url . "/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 = getenv('SMTP_HOST') ?: 'smtp.gmail.com';
$mail->Port = getenv('SMTP_PORT') ?: '587';
//gmail account credentials from environment
$mail->Username = getenv('SMTP_USER') ?: 'mail@crewsportswear.com';
$mail->Password = getenv('SMTP_PASS') ?: 'tjpwykpttvkjilxh';
$mail->SetFrom('orders@crewsportswear.com', 'CREW Daily Order Report');
// Recipients from environment or defaults
$email_to = getenv('EMAIL_TO') ?: 'graphics@crewsportswear.com';
$mail->addAddress($email_to);
$email_bcc = getenv('EMAIL_BCC') ?: 'webmaster@crewsportswear.com,angelo@crewsportswear.com,production@crewsportswear.com';
foreach (explode(',', $email_bcc) as $bcc_email) {
$mail->addBCC(trim($bcc_email));
}
$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);
}
?>