belongsTo(ConsumerApplication::class, 'application_id'); } /** * Relationship with reason/agreement type */ public function agreementType() { return $this->belongsTo(AgreementType::class, 'agreement_type_id'); } /** * Maker user relationship */ public function maker() { return $this->belongsTo(User::class, 'maker_id'); } /** * Checker user relationship */ public function checker() { return $this->belongsTo(User::class, 'checker_user_id'); } /** * Check if a pending request exists for the application */ public static function hasPendingRequest($applicationId) { return self::where('application_id', $applicationId) ->where('status', self::STATUS_PENDING) ->exists(); } /** * Get count of approved deletion requests for this application */ public static function getApprovedCount($applicationId) { return self::where('application_id', $applicationId) ->where('status', self::STATUS_APPROVED) ->count(); } /** * Check if the application's current state allows raising a deletion request */ public static function isStateEligible($state) { return in_array(strtolower(trim((string) $state)), self::ALLOWED_APPLICATION_STATES, true); } /** * Check if a new deletion request can be raised for an application * * @param int $applicationId * @return array ['can_raise' => bool, 'message' => string] */ public static function canRaiseRequest($applicationId) { $application = ConsumerApplication::find($applicationId, ['id', 'state']); if (!$application) { return [ 'can_raise' => false, 'message' => 'Application not found.' ]; } if (!self::isStateEligible($application->state)) { return [ 'can_raise' => false, 'message' => 'Deletion requests can only be raised for applications in Approved, DCD, Query, or RFD state.' ]; } if (self::hasPendingRequest($applicationId)) { return [ 'can_raise' => false, 'message' => 'A deletion request is already pending for this application.' ]; } if (self::getApprovedCount($applicationId) >= self::MAX_APPROVED_REQUESTS) { return [ 'can_raise' => false, 'message' => 'Maximum ' . self::MAX_APPROVED_REQUESTS . ' agreement deletion requests have already been approved for this application.' ]; } return [ 'can_raise' => true, 'message' => 'Request can be raised.' ]; } /** * Check if current session has Maker permission to raise LA deletion request */ public static function hasMakerPermission() { return Session::has('Raise_LA_Deletion_Request|-4-0'); } /** * Check if current session has Checker permission to delete/approve LA deletion request */ public static function hasCheckerPermission() { return Session::has('Delete_LA_Request|-4-1'); } }