api code global jain
This commit is contained in:
88
app/Rules/ViharCanNotAfterTwoHours.php
Normal file
88
app/Rules/ViharCanNotAfterTwoHours.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use App\Models\Vihar;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class ViharCanNotAfterTwoHours implements Rule
|
||||
{
|
||||
public $sant_id;
|
||||
public $start_date;
|
||||
public $start_time;
|
||||
public $vihar_id;
|
||||
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($data, $vihar_id = null)
|
||||
{
|
||||
$this->sant_id = $data['sant_id'];
|
||||
$this->start_date = $data['start_date'];
|
||||
$this->start_time = $data['start_time'];
|
||||
$this->vihar_id = $vihar_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
$santID = $this->sant_id;
|
||||
$numberOfPosts = Vihar::where('sant_id', $this->sant_id)
|
||||
// ->orWhere( function ($query) use ($santID) {
|
||||
// return $query->whereHas('viharThana', function ($subQuery) use ($santID) {
|
||||
// $subQuery->where('vihar_thanas.sant_id', $santID);
|
||||
// })->orWhere('vihars.sant_id', $santID);
|
||||
// })
|
||||
->where('id', '<>', $this->vihar_id)
|
||||
->where('start_date', $this->start_date)
|
||||
->whereDate('created_at', Carbon::today())->count();
|
||||
if ($numberOfPosts >= 1) {
|
||||
$previousVihar = Vihar::where('sant_id', $this->sant_id)
|
||||
->where('id', '<>', $this->vihar_id)
|
||||
->where('start_date', $this->start_date)
|
||||
->whereDate('created_at', Carbon::today())->first();
|
||||
$previousTime = Carbon::createFromFormat('H:i', $previousVihar->start_time);
|
||||
$nextViharTimeShoudBe = Carbon::createFromFormat('H:i', $previousVihar->start_time)->addHours(2);
|
||||
$nextViharTimeActual = Carbon::createFromFormat('H:i', $this->start_time);
|
||||
// Update time
|
||||
if ($this->vihar_id) {
|
||||
if ($previousVihar->id < $this->vihar_id) {
|
||||
$result = $nextViharTimeActual->gt($nextViharTimeShoudBe);
|
||||
if ($result) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$result = $nextViharTimeActual->gt($nextViharTimeShoudBe);
|
||||
if ($result) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return 'Another Vihaar record can be entered only after 2 hours of current Vihaar.';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user