File: /home/posscale/www/printmanager/app/Http/Controllers/Tenant/ManagePrinterJob.php
<?php
namespace App\Http\Controllers\Tenant;
use App\Actions\SmtpAction;
use App\Http\Controllers\Controller;
use App\Models\Printer;
use App\Models\PrinterJob;
use Illuminate\Http\Request;
use PhpParser\Node\Expr\Print_;
class ManagePrinterJob extends Controller
{
/**
* Display a listing of the resource.
*/
public function index($id, Request $request)
{
abort_unless(auth()->user()?->can('manage_printers'), 403);
$job = PrinterJob::find($id);
return view('tenant.job.details', ['job' => $job]);
}
/**
* Sync the data from mail
*/
public function sync(Request $request)
{
abort_unless(auth()->user()?->can('monitoring_settings'), 403);
$printers = Printer::where('smtp_status', 1)->get();
foreach ($printers as $printer) {
if ($printer->smtp_status) {
$smtp_details['client'] = [
'host' => $printer->smtp->host,
'port' => $printer->smtp->port,
'encryption' => 'ssl',
'username' => $printer->smtp->user_name,
'password' => $printer->smtp->user_password,
'protocol' => 'imap'
];
$smtp_connection = new SmtpAction($smtp_details);
$connection_info = $smtp_connection->testConnection();
if ($connection_info['status']) {
$folder = $smtp_connection->client->getFolder('INBOX');
$sinceDateTime = date('Y-m-d h:i:s', strtotime($printer->smtp_sync_date));
$query = $folder->query()
->setFetchOrder("asc")
->subject($printer->model_name)
->since($sinceDateTime)
->unseen()
/*->limit(round(100 / count($printers)))*/
->limit(100);
$messages = $query->get();
// $query->markAsRead();
foreach ($messages as $message) {
$message_id = $message->get('message_id');
$job = PrinterJob::where('mail_id', $message_id)->count();
$message->setFlag('Seen');
if($job){
continue;
}
$body_text = $message->getTextBody();
$array_of_job = (explode("\n", $body_text));
$job_details_data = [];
$prefix = '';
foreach ($array_of_job as $job_details) {
$row_data = explode(':', $job_details);
if (trim($row_data[0]) == 'Printed Pages') {
$prefix = 'Printed ';
}
if (trim($row_data[0]) == 'Scanned Pages') {
$prefix = 'Scanned ';
}
if (trim($row_data[0]) == 'Counters by Paper Size') {
$prefix = 'Counters ';
}
if (trim($row_data[0]) == 'Counters by Duplex') {
$prefix = 'Counters D ';
}
if (trim($row_data[0]) == 'Counters by Combine') {
$prefix = 'Counters C ';
}
if (trim($row_data[0]) == 'none') {
$prefix = 'none ';
}
$key = array_search($prefix . trim($row_data[0]), $smtp_connection->field_connections);
if (isset($smtp_connection->ignore_fields[$key])) {
continue;
}
if (isset($row_data[1])) {
if (isset($smtp_connection->printer_fields[$key])) {
// if ($key == 'smtp_sync_date') {
// unset($row_data[0]);
// // $last_sync_date = trim(join(':', $row_data));
// }
} else {
$job_details_data[$key] = trim($row_data[1]);
}
}
}
$job_details_data['printer_id'] = $printer->id;
$job_details_data['mail_date'] = $message->getDate()->toDate()->format('Y-m-d h:i:s');
$job_details_data['mail_id'] = $message_id;
$new_printer_job = new PrinterJob($job_details_data);
$new_printer_job->save();
$printer->smtp_sync_date = $message->getDate()->toDate()->format('Y-m-d h:i:s');
$printer->save();
}
}
}
}
}
}