File: /mnt/data/ghayatcom/ghayatcom-api/app/Library/DependantLibrary.php
<?php
namespace App\Library;
use App\DependantAccountHolder;
use App\User;
use App\UserAddress;
use DB;
class DependantLibrary
{
public function list($user_id)
{
$query = User::with(['dependantAddress', 'dependantRelation.relationShipName'])->find($user_id);
return $query;
}
/*public function list($user_id,$authID=NULL) {
$query = User::with(['dependantAddress','dependantRelation.relationShipName']);
if($authID != NULL) {
$query->whereHas('dependantRelation', function($q) use ($authID) {
$q->where('user_id', $authID);
});
}
$query->find($user_id);
return $query;
}*/
public function updatePreConditions($update_id,$authID,$dob,$id_card_or_passport_no) {
$userModel = new User();
$dob = date('Y-m-d',strtotime(str_replace('/', '-', $dob)));
$checkAccountHolder = $userModel->role('dependant')->with(['dependantAccountHolder'])->where('id', $update_id)->whereHas('dependantAccountHolder', function($q) use ($authID) {$q->where('user_id', $authID);})->first();
if(empty($checkAccountHolder)) {
return ['is_error' => true,'code' => 401, 'message' => __('digimed_validation.error_response.not_authorized')];
}
/*if(!empty($checkAccountHolder)) {
$awaitingAdminApproval = "";
$loopStatus = $checkAccountHolder->dependantAccountHolder;
foreach($loopStatus AS $val) {
if($val['user_id'] == $authID) {
if($val['status'] == 0) {
$awaitingAdminApproval = 1;
}
}
}
if(!empty($awaitingAdminApproval)) {
return ['is_error' => true,'code' => 500, 'message' => __('digimed_validation.error_response.awaiting_admin_approval_dependant')];
}
}*/
//->where('dob', $dob)
$checkPassportID = $userModel->role('dependant')->where('id', '!=', $update_id)->where('passport_number', $id_card_or_passport_no)->get();
if(count($checkPassportID) > 0) {
return ['is_error' => true,'code' => 409, 'message' => __('digimed_validation.error_response.passport_id_conflict')];
}
return ['is_error' => false];
}
public function addPreConditions($authID, $dob, $id_card_or_passport_no)
{
//->where('dob', $dob)
$roleDependant = User::role('dependant')->with(['dependantAccountHolder'])->where('passport_number', $id_card_or_passport_no)->first();
if (! empty($roleDependant)) {
$createdBySelf = '';
$arr = [];
$holderLoop = $roleDependant->dependantAccountHolder;
foreach ($holderLoop as $val) {
if ($val->user_id == $authID) {
$createdBySelf = 'Yes';
$arr['dependent_relationship_proof'] = $val->dependent_relationship_proof;
$arr['is_proof_verified'] = $val->is_proof_verified;
}
}
if (! empty($createdBySelf)) {
if (! empty($arr)) {
if ($arr['dependent_relationship_proof'] == '') {
DB::beginTransaction();
$holders = DependantAccountHolder::where('dependant_id', $roleDependant->id)->count();
UserAddress::where('user_id', $roleDependant->id)->forceDelete();
DependantAccountHolder::where('dependant_id', $roleDependant->id)->where('user_id', $authID)->forceDelete();
if ($holders == 1) {
$roleDependant->roles()->detach();
$roleDependant->save();
$roleDependant->forceDelete();
}
DB::commit();
return ['is_error' => false];
// return ['is_error' => true,'code' => 500, 'message' => __('digimed_validation.error_response.created_by_myself_dependant'), 'data' => []];
} else {
return ['is_error' => true, 'code' => 200, 'message' => '', 'data' => [], 'legacy_data' => $roleDependant];
}
}
} else {
$awaitingAdminApproval = [];
$loopStatus = $roleDependant->dependantAccountHolder;
foreach ($loopStatus as $val) {
// if($val['user_id'] == $authID) {}
if ($val['status'] == 0) {
array_push($awaitingAdminApproval, 1);
}
}
$overAllCount = count($loopStatus);
$awaitingCount = count($awaitingAdminApproval);
/*if($overAllCount == $awaitingCount) {
return ['is_error' => true,'code' => 500, 'message' => __('digimed_validation.error_response.add_awaiting_admin_approval_dependant')];
}*/
return ['is_error' => true, 'code' => 200, 'message' => __('digimed_validation.error_response.created_by_myself_dependant'), 'data' => $roleDependant];
}
} else {
return ['is_error' => false];
}
}
}