File: /mnt/data/ghayatcom/ghayatcom-api/app/Library/RandomLibrary.php
<?php
namespace App\Library;
use DB;
use Hash;
use Storage;
use Config;
use Illuminate\Support\Carbon;
use App\Diagnosis;
use App\User;
class RandomLibrary {
/**
* @param object $model
* @param string|null $column
* @param string $forCondition
* @return string|null
*/
public function random($model, $column, $forCondition): ?string {
/** @var integer $count */
$count = 1;
/** @var boolean $condition */
$condition = true;
do {
if($forCondition == 'patient') {
//For patient
$checkCount = $model->where('type','patient')->withTrashed()->count();
} else if($forCondition == 'diagnosis') {
//For diagnosis
$checkCount = $model->where('parent_id', NULL)->withTrashed()->count();
} else if($forCondition == 'prescription') {
//For prescription
$checkCount = $model->withTrashed()->count();
} else {
//For appointment
$checkCount = $model->withTrashed()->count();
}
$invID = $checkCount + $count;
$data_invID = (string)$invID;
$returnData = str_pad($data_invID, 8, '0', STR_PAD_LEFT);
$checkLayer = $model->select('id')->where($column, $returnData)->withTrashed()->get();
if (count($checkLayer) != 0) {
$count++;
$condition = true;
} else {
$condition = false;
}
} while ($condition);
/** @var string|null $returnData */
return $returnData;
}
/**
* @param string|null $id
* @param object $model
* @param string|null $column
* @param string $forCondition
* @return string|null
*/
public function randomPluck($id, $model, $column, $forCondition='appointment'): ?string {
if($id != null) {
$initalQuery = $model->select('id')->where($column, $id)->withTrashed()->count();
if($initalQuery != 0) {
$returnData = $this->random($model, $column, $forCondition);
} else {
$returnData = $id;
}
} else {
$returnData = $this->random($model, $column, $forCondition);
}
/** @var string|null $returnData */
return $returnData;
}
}