Files
Global-Jain/app/Console/Commands/ReplaceVihaarEndLocation.php
2025-11-05 10:37:10 +05:30

127 lines
5.6 KiB
PHP

<?php
namespace App\Console\Commands;
use Exception;
use App\Models\Sant;
use App\Constant\Constant;
use App\Models\ThanaMember;
use App\Models\SantLocation;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class ReplaceVihaarEndLocation extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'check:vihaar';
/**
* The console command description.
*
* @var string
*/
protected $description = 'It checks if vihaar is ended and replace sant\'s location';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
try {
$sants = Sant::with([
'location.createdBy:id,name,avatar',
'location.updatedBy:id,name,avatar',
])->get()->toArray();
foreach ($sants as $sant) {
$thanaID = Constant::NULL;
$thanaSantID = [];
$thanaID = ThanaMember::where('sant_id', $sant['id'])->value('thana_id');
$thanaSantID = ThanaMember::where('thana_id', $thanaID)->pluck('sant_id');
if (!empty($sant['location'])) {
// $santLocation = SantLocation::find($sant['location']['sant_id']);
$santLocation = SantLocation::where('sant_id', $sant['location']['sant_id'])->first();
if (empty($santLocation)) {
if (!empty($thanaSantID) && count($thanaSantID) > 0) {
foreach ($thanaSantID as $santID) {
SantLocation::create([
'sant_id' => $santID,
'sangh_id' => Constant::NULL,
'type' => isset($sant['location']['to_sangh_id']) ? Constant::STATUS_TWO : Constant::STATUS_ONE,
'location' => $sant['location']['to'],
'latitude' => $sant['location']['to_latitude'],
'longitude' => $sant['location']['to_longitude'],
'created_by' => $sant['location']['created_by']['id'] ?? Constant::NULL,
'updated_by' => Constant::NULL,
]);
}
} else {
SantLocation::create([
'sant_id' => $sant['id'] ?? Constant::NULL,
'sangh_id' => Constant::NULL,
'type' => isset($sant['location']['to_sangh_id']) ? Constant::STATUS_TWO : Constant::STATUS_ONE,
'location' => $sant['location']['to'],
'latitude' => $sant['location']['to_latitude'],
'longitude' => $sant['location']['to_longitude'],
'created_by' => $sant['location']['created_by']['id'] ?? Constant::NULL,
'updated_by' => Constant::NULL,
]);
}
} else {
if (!empty($thanaSantID) && count($thanaSantID) > 0) {
foreach ($thanaSantID as $santID) {
if ($santID == $santLocation->sant_id) {
$santLocation->update([
'sant_id' => $santID,
'sangh_id' => Constant::NULL,
'type' => $santLocation->type,
'location' => $sant['location']['to'] ?? $santLocation->location,
'latitude' => $sant['location']['to_latitude'] ?? $santLocation->latitude,
'longitude' => $sant['location']['to_longitude'] ?? $santLocation->longitude,
'created_by' => $santLocation->created_by ?? Constant::NULL,
'updated_by' => $sant['location']['created_by']['id'] ?? Constant::NULL,
]);
}
}
} else {
$santLocation->update([
'sant_id' => $sant['id'] ?? $santLocation->sant_id,
'sangh_id' => Constant::NULL,
'type' => $santLocation->type,
'location' => $sant['location']['to'] ?? $santLocation->location,
'latitude' => $sant['location']['to_latitude'] ?? $santLocation->latitude,
'longitude' => $sant['location']['to_longitude'] ?? $santLocation->longitude,
'created_by' => $santLocation->created_by ?? Constant::NULL,
'updated_by' => $sant['location']['created_by']['id'] ?? Constant::NULL,
]);
}
}
}
}
return $this->info(trans(__('Command executed successfully!')));
} catch(Exception $ex) {
Log::error($ex->getMessage());
}
}
}