From 80d7258ae008158bca2769c5471b9861f4efb74f Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Mon, 11 May 2026 10:25:47 +0530 Subject: [PATCH] Added automatic rejection logic and added template quality characteristics --- .../Commands/TriggerPendingApprovalMails.php | 141 +++++++++++++++++- 1 file changed, 139 insertions(+), 2 deletions(-) diff --git a/app/Console/Commands/TriggerPendingApprovalMails.php b/app/Console/Commands/TriggerPendingApprovalMails.php index 3673398..c914972 100644 --- a/app/Console/Commands/TriggerPendingApprovalMails.php +++ b/app/Console/Commands/TriggerPendingApprovalMails.php @@ -5,9 +5,12 @@ namespace App\Console\Commands; use App\Mail\CharacteristicApprovalMail; use App\Models\CharacteristicApproverMaster; use App\Models\RequestCharacteristic; +use App\Models\TempClassCharacteristic; +// use App\Models\TempClassCharacteristic; use Illuminate\Console\Command; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\Schema; class TriggerPendingApprovalMails extends Command { @@ -40,6 +43,8 @@ class TriggerPendingApprovalMails extends Command public $subjectLine; + // public $tempCharacteristics = []; + public $wfId; public function handle() @@ -195,6 +200,38 @@ class TriggerPendingApprovalMails extends Command $updateData['mail_status'] = 'Sent-Mail3'; } + $totalMinutes = + $this->convertToMinutes($approver->duration1 ?? 0) + + $this->convertToMinutes($approver->duration2 ?? 0) + + $this->convertToMinutes($approver->duration3 ?? 0); + + $expiryTime = Carbon::parse($first->created_at) + ->copy() + ->addMinutes($totalMinutes); + + $ids = $groupRecords->pluck('id'); + + $this->info("Expiry Time for ID {$first->id}: {$expiryTime}, Now: {$now}"); + + // --- AUTO REJECT --- + if ( + $first->mail_status == 'Sent-Mail3' && + (is_null($first->approver_status1) || $first->approver_status1 == 'Hold') && + (is_null($first->approver_status2) || $first->approver_status2 == 'Hold') && + (is_null($first->approver_status3) || $first->approver_status3 == 'Hold') && + $now->gte($expiryTime) + ) { + RequestCharacteristic::whereIn('id', $ids) + ->update([ + 'approver_status3' => 'Rejected', + 'approver_remark3' => 'Time Limit Reached', + 'approved3_at' => now(), + ]); + + $this->info("Auto Rejected ID: {$first->id}"); + continue; + } + if (!$level || !$mail) { continue; } @@ -203,6 +240,8 @@ class TriggerPendingApprovalMails extends Command $subjectLine = 'Characteristic Approval Mail'; + // $emails = array_map('trim', explode(',', $mail)); + Mail::to($mail)->send( new CharacteristicApprovalMail( $first, @@ -251,6 +290,23 @@ class TriggerPendingApprovalMails extends Command return $approver && $approver->approver_type == 'Quality'; }); + // $approvers = CharacteristicApproverMaster::where('approver_type', 'Quality') + // ->get() + // ->keyBy('id'); + + // $qualityRecords = RequestCharacteristic::where(function ($q) { + // $q->whereNull('approver_status1')->orWhere('approver_status1', 'Hold'); + // }) + // ->where(function ($q) { + // $q->whereNull('approver_status2')->orWhere('approver_status2', 'Hold'); + // }) + // ->where(function ($q) { + // $q->whereNull('approver_status3')->orWhere('approver_status3', 'Hold'); + // }) + // ->whereIn('characteristic_approver_master_id', $approvers->keys()) + // ->get(); + + if ($qualityRecords->isEmpty()) { $this->info('No quality pending approvals'); return; @@ -283,6 +339,43 @@ class TriggerPendingApprovalMails extends Command continue; } + $columns = Schema::getColumnListing('temp_class_characteristics'); + + $exclude = ['id', 'plant_id', 'machine_id', 'item_id', 'aufnr', 'class', 'arbid', 'gamng', 'lmnga', 'zz1_cn_bill_ord', 'zmm_heading', 'created_at', 'updated_at', 'deleted_at', 'has_work_flow_id', 'model_type', 'created_by', 'updated_by' ]; + + $filteredColumns = array_diff($columns, $exclude); + + $row1 = TempClassCharacteristic::where('plant_id', $first->plant_id) + ->where('machine_id', $first->machine_id) + ->where('aufnr', $first->aufnr) + ->where('model_type', $first->model_type) + ->latest() + ->first(); + + $this->info(json_encode([ + 'id' => $row1->id, + 'plant_id' => $row1->plant_id, + 'machine_id' => $row1->machine_id, + 'aufnr' => $row1->aufnr, + 'motor_speed' => $row1->zmm_motor_speed, + 'all_data' => $row1->toArray(), + ])); + + $data = []; + + if ($row1) { + foreach ($filteredColumns as $column) { + + $value = $row1->getAttribute($column); + + if ($value != null && $value != '') { + $data[$column] = $value; + } + } + } + + $characteristics = $data; + $level = null; $mail = null; $name = null; @@ -359,6 +452,38 @@ class TriggerPendingApprovalMails extends Command $updateData['mail_status'] = 'Sent-Mail3'; } + $totalMinutes = + $this->convertToMinutes($approver->duration1 ?? 0) + + $this->convertToMinutes($approver->duration2 ?? 0) + + $this->convertToMinutes($approver->duration3 ?? 0); + + $expiryTime = Carbon::parse($first->created_at) + ->copy() + ->addMinutes($totalMinutes); + + $ids = $groupRecords->pluck('id'); + + $this->info("Expiry Time for ID {$first->id}: {$expiryTime}, Now: {$now}"); + + // --- AUTO REJECT --- + if ( + $first->mail_status == 'Sent-Mail3' && + (is_null($first->approver_status1) || $first->approver_status1 == 'Hold') && + (is_null($first->approver_status2) || $first->approver_status2 == 'Hold') && + (is_null($first->approver_status3) || $first->approver_status3 == 'Hold') && + $now->gte($expiryTime) + ) { + RequestCharacteristic::whereIn('id', $ids) + ->update([ + 'approver_status3' => 'Rejected', + 'approver_remark3' => 'Time Limit Reached', + 'approved3_at' => now(), + ]); + + $this->info("Auto Rejected ID: {$first->id}"); + continue; + } + if (!$level || !$mail) { continue; } @@ -381,8 +506,8 @@ class TriggerPendingApprovalMails extends Command $pdfPath, $pendingApprovers, $approverNameFromMaster, - $subjectLine - // $characteristics + $subjectLine, + $characteristics ) ); @@ -406,4 +531,16 @@ class TriggerPendingApprovalMails extends Command $this->info('Approval mail job completed'); } + + public function convertToMinutes($duration) + { + if (!$duration) return 0; + + $parts = explode('.', (string)$duration); + + $hours = (int)($parts[0] ?? 0); + $minutes = (int)($parts[1] ?? 0); + + return ($hours * 60) + $minutes; + } }