253 lines
9.8 KiB
PHP
253 lines
9.8 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use App\Http\Requests\LambdaRequest;
|
|
use App\Mail\NeblogisticStatusUpdate;
|
|
use League\Csv\Reader;
|
|
use Log;
|
|
|
|
class NeblogisticsOrderSync extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'neblogisticsorders:sync';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Will send Push notification to IOS and Android';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
// Path to the CSV file (update this path as needed)
|
|
$csvFilePath = storage_path('app/neblogistics_orders.csv');
|
|
|
|
if (!file_exists($csvFilePath)) {
|
|
$this->error("CSV file not found at: $csvFilePath");
|
|
return 1;
|
|
}
|
|
|
|
try {
|
|
// Use League CSV Reader instead of fgetcsv
|
|
$csv = Reader::createFromPath($csvFilePath, 'r');
|
|
$csv->setHeaderOffset(0);
|
|
|
|
$header = $csv->getHeader(); //returns the CSV header record
|
|
$records = $csv->getRecords(); //returns all the CSV records as an Iterator object
|
|
|
|
//Log::info('CSV headers found', ['headers' => $header]);
|
|
|
|
// Debug: Check if we have any records
|
|
$recordArray = iterator_to_array($records);
|
|
Log::info('Total records found in CSV', ['count' => count($recordArray)]);
|
|
|
|
if (empty($recordArray)) {
|
|
$this->warn("No records found in CSV file");
|
|
return 0;
|
|
}
|
|
|
|
$rowCount = 0;
|
|
$updatedCount = 0;
|
|
$notFoundCount = 0;
|
|
|
|
foreach ($recordArray as $row) {
|
|
$rowCount++;
|
|
// Map CSV columns to variables
|
|
$orderNo = trim($row['ORDER NO'] ?? '');
|
|
$invoiceNo = trim($row['INVOICE NO'] ?? '');
|
|
$courierName = trim($row['COURIER NAME'] ?? '');
|
|
$lrNo = trim($row['LR NO'] ?? '');
|
|
$lrDate = trim($row['LR DATE'] ?? '');
|
|
$receivedDate = trim($row['RECEIVED DATE'] ?? '');
|
|
$connectOrderId = trim($row['Connect od Order'] ?? '');
|
|
|
|
if (empty($courierName)) {
|
|
$this->warn("Skipping row with empty courier name.");
|
|
continue;
|
|
}
|
|
|
|
if (empty($connectOrderId)) {
|
|
$this->warn("Skipping row with missing Connect od Order.");
|
|
continue;
|
|
}
|
|
|
|
// Find the record in neblogistic table
|
|
$record = DB::table('neblogistic')->where('id', $connectOrderId)->first();
|
|
|
|
if (!$record) {
|
|
$this->warn("No neblogistic record found for id: $connectOrderId");
|
|
$notFoundCount++;
|
|
continue;
|
|
}
|
|
|
|
// Determine status and notification message
|
|
if (!empty($receivedDate)) {
|
|
$status = 'Delivered';
|
|
} else {
|
|
$status = 'Dispatched';
|
|
}
|
|
|
|
// Prepare update data
|
|
$updateData = [
|
|
'order_no' => $orderNo,
|
|
'invoice_no' => $invoiceNo,
|
|
'courier_name' => $courierName,
|
|
'lr_no' => $lrNo,
|
|
'lr_date' => !empty($lrDate) ? (
|
|
($dt = \DateTime::createFromFormat('d/M/Y', $lrDate)) ? $dt->format('Y-m-d') : null
|
|
) : null,
|
|
'received_date' => !empty($receivedDate) ? (
|
|
($dt2 = \DateTime::createFromFormat('d/M/Y', $receivedDate)) ? $dt2->format('Y-m-d') : null
|
|
) : null,
|
|
'status' => $status,
|
|
'updated_at' => date('Y-m-d', strtotime("now"))
|
|
];
|
|
|
|
$this->info('Processing CSV row for NeblogisticsOrderSync: Row ' . $rowCount . ' - ' . json_encode($updateData));
|
|
|
|
//Uncomment the line below to actually update the database
|
|
// Get old data before update for audit trail
|
|
$oldRecord = DB::table('neblogistic')->where('id', $connectOrderId)->first();
|
|
$oldDataArray = $oldRecord ? json_decode(json_encode($oldRecord), true) : null;
|
|
|
|
DB::table('neblogistic')->where('id', $connectOrderId)->update($updateData);
|
|
|
|
// Log UPDATE operation (console command - no request object)
|
|
\App\Services\NeblogisticAuditService::logUpdate(
|
|
$connectOrderId,
|
|
$oldDataArray,
|
|
$updateData,
|
|
null,
|
|
null // No request object in console command
|
|
);
|
|
|
|
$updatedCount++;
|
|
|
|
// Check if status has changed from previous status
|
|
$previousStatus = $record->status ?? null;
|
|
$currentStatus = $status;
|
|
|
|
// Send email notifications if status is Dispatched or Delivered and it's a new status
|
|
if (($currentStatus == 'Dispatched' || $currentStatus == 'Delivered') &&
|
|
($previousStatus != $currentStatus)) {
|
|
|
|
$this->info("Status changed from '{$previousStatus}' to '{$currentStatus}' - Sending notifications");
|
|
|
|
// Prepare email data
|
|
$emailData = array_merge($updateData, [
|
|
'id' => $connectOrderId,
|
|
'previous_status' => $previousStatus,
|
|
'emp_name' => $record->emp_name ?? null,
|
|
'abm_name' => $record->abm_name ?? null,
|
|
'doctor_name' => $record->doctor_name ?? null,
|
|
'msl_code' => $record->msl_code ?? null,
|
|
]);
|
|
|
|
$tmEmail = $record->emp_email ?? ($record->tm_email ?? null);
|
|
|
|
// Send email to TM if available
|
|
if (!empty($tmEmail)) {
|
|
try {
|
|
Mail::to($tmEmail)->cc('ravigulhane1983@gmail.com')->send(new NeblogisticStatusUpdate($emailData, 'tm'));
|
|
$this->info("Status update notification sent to TM: {$tmEmail}");
|
|
Log::info("Status update email sent to TM", [
|
|
'email' => $tmEmail,
|
|
'orderId' => $connectOrderId,
|
|
'status' => $currentStatus
|
|
]);
|
|
} catch (\Exception $e) {
|
|
$this->error("Failed to send email to TM {$tmEmail}: " . $e->getMessage());
|
|
Log::error("Failed to send TM email", [
|
|
'email' => $tmEmail,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
// Send email to abm_email if available
|
|
if (!empty($record->abm_email)) {
|
|
try {
|
|
Mail::to($record->abm_email)->cc('ravigulhane1983@gmail.com')->send(new NeblogisticStatusUpdate($emailData, 'abm'));
|
|
$this->info("Status update notification sent to ABM: {$record->abm_email}");
|
|
Log::info("Status update email sent to ABM", [
|
|
'email' => $record->abm_email,
|
|
'orderId' => $connectOrderId,
|
|
'status' => $currentStatus
|
|
]);
|
|
} catch (\Exception $e) {
|
|
$this->error("Failed to send email to ABM {$record->abm_email}: " . $e->getMessage());
|
|
Log::error("Failed to send ABM email", [
|
|
'email' => $record->abm_email,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
}
|
|
}
|
|
|
|
// Prevent cron from re-sending the same status email
|
|
DB::table('neblogistic')
|
|
->where('id', $connectOrderId)
|
|
->update(['status_email_sent' => $currentStatus]);
|
|
}
|
|
|
|
|
|
// // Fetch tm_email and abm_email from the record (if present)
|
|
// $tmEmail = $record->tm_email ?? null;
|
|
// $abmEmail = $record->abm_email ?? null;
|
|
|
|
// // Prepare email notification
|
|
// $subject = "Order {$status}: Order No {$orderNo}";
|
|
// $body = "Dear User,\n\nOrder No: {$orderNo}\nInvoice No: {$invoiceNo}\nCourier: {$courierName}\nLR No: {$lrNo}\nLR Date: {$lrDate}\n";
|
|
// if (!empty($receivedDate)) {
|
|
// $body .= "Received Date: {$receivedDate}\n";
|
|
// }
|
|
// $body .= "Status: {$status}\n\nThank you.";
|
|
|
|
// // Send email to tm_email and abm_email if available
|
|
// $emails = [];
|
|
// if (!empty($tmEmail)) $emails[] = $tmEmail;
|
|
// if (!empty($abmEmail)) $emails[] = $abmEmail;
|
|
|
|
// foreach ($emails as $email) {
|
|
// try {
|
|
// // You can replace this with Laravel's Mail::to($email)->send(...) if needed
|
|
// mail($email, $subject, $body);
|
|
// Log::info("Notification sent to $email for order $orderNo (status: $status)");
|
|
// } catch (\Exception $e) {
|
|
// Log::error("Failed to send notification to $email: " . $e->getMessage());
|
|
// }
|
|
// }
|
|
}
|
|
|
|
$this->info("CSV processed. Total rows: $rowCount, Updated: $updatedCount, Not found: $notFoundCount");
|
|
|
|
} catch (\Exception $e) {
|
|
$this->error("Error processing CSV: " . $e->getMessage());
|
|
Log::error("Error in NeblogisticsOrderSync: " . $e->getMessage());
|
|
return 1;
|
|
}
|
|
}
|
|
} |