diff --git a/app/Console/Commands/DailyMovingMachineCommand.php b/app/Console/Commands/DailyMovingMachineCommand.php index e315e194..96bdf9d6 100644 --- a/app/Console/Commands/DailyMovingMachineCommand.php +++ b/app/Console/Commands/DailyMovingMachineCommand.php @@ -4,7 +4,7 @@ namespace App\Console\Commands; use App\Enums\ActivityMachineType; use App\Models\Mission; -use App\Models\DailyMoveMachine; +use App\Models\AssignmentFails; use App\Services\CMMS\DailyMovingMachinesService; use Exception; use Illuminate\Console\Command; @@ -31,7 +31,8 @@ class DailyMovingMachineCommand extends Command */ public function handle(DailyMovingMachinesService $dailyMovingMachines): void { - $response = $dailyMovingMachines->run(); + $vehiclesInfo = $dailyMovingMachines->run(); + $response =$vehiclesInfo->get('vehiclesInfo', collect()); $date = now()->toDateString(); @@ -39,55 +40,38 @@ class DailyMovingMachineCommand extends Command ->pluck('machineCode'); $mission = Mission::query() - ->whereDate('start_time',today()) + ->whereDate('start_time',$date) ->pluck('machine_code'); - $cmmsOnly = $cmms->diff($mission); $missionOnly = $mission->diff($cmms); - $matching = $cmms->intersect($mission); - - foreach ($matching as $code) { - $cmm = (object) $response->firstWhere('machineCode', $code); - $missionRecord = Mission::where('machine_code', $code) - ->whereDate('start_time', today()) - ->first(); - - DailyMoveMachine::query()->create([ - 'machine_code' => $code, - 'mission_id' => $missionRecord->id , - 'type' => ActivityMachineType::MOTABEGAT->label(), - 'request_date' => $date, - 'mileage' => $cmm->mileage, - 'exit_time' => $cmm->exit_time, - 'enter_time' => $cmm->enter_time, - ]); - } foreach ($cmmsOnly as $code) { - $cmm = (object) $response->firstWhere('machineCode', $code); + $cmm = $response->firstWhere('machineCode', $code); - DailyMoveMachine::query()->create([ + AssignmentFails::query()->create([ 'machine_code' => $code, - 'type' => ActivityMachineType::TEDADE_MACHINES_BISHTAR_AS_MISSION->label(), + 'type' => ActivityMachineType::KHOROJ_BEDONE_MOGAVEZ->label(), 'request_date' => $date, - 'mileage' => $cmm->mileage, - 'exit_time' => $cmm->exit_time, - 'enter_time' => $cmm->enter_time, + 'mileage' => $cmm['mileageKM'], + 'exit_time' => $cmm['exit_time'], + 'enter_time' => $cmm['enter_time'], ]); } foreach ($missionOnly as $code) { - DailyMoveMachine::create([ + $missionRecord = Mission::query()->where('machine_code', $code) + ->whereDate('start_time', today()) + ->get(); + AssignmentFails::query()->create([ 'machine_code' => $code, - 'mission_id' => $mission->id, - 'type' => ActivityMachineType::TEDADE_MISSIONS_BISHTAR_AS_MACHINES->label(), + 'mission_id' => $missionRecord->id, + 'type' => ActivityMachineType::ADAM_TAHAROK->label(), 'request_date' => $date, ]); } - - $this->info('Diffs saved.'); + $this->info("ماشین‌های متحرک بدون مأموریت: " . $cmmsOnly->count()); + $this->info("مأموریت‌های بدون حرکت در CMMS: " . $missionOnly->count()); } - } diff --git a/app/Enums/ActivityMachineType.php b/app/Enums/ActivityMachineType.php index 23cd3879..4a4d2595 100644 --- a/app/Enums/ActivityMachineType.php +++ b/app/Enums/ActivityMachineType.php @@ -4,17 +4,15 @@ namespace App\Enums; enum ActivityMachineType: int { - case MOTABEGAT = 1; - case TEDADE_MACHINES_BISHTAR_AS_MISSION = 2; - case TEDADE_MISSIONS_BISHTAR_AS_MACHINES = 3; + case KHOROJ_BEDONE_MOGAVEZ = 1; + case ADAM_TAHAROK = 2; public function label(): string { return match ($this) { - self::MOTABEGAT => "مطابقت ", - self::TEDADE_MACHINES_BISHTAR_AS_MISSION => " تعداد ماشین ها بیشتر از ماموریت ها", - self::TEDADE_MISSIONS_BISHTAR_AS_MACHINES => "تغداد ماموریت ها بیشتر از ماشین ها ", + self::KHOROJ_BEDONE_MOGAVEZ => "خروج بدون مجوز ", + self::ADAM_TAHAROK => "عدم تحرک", }; } diff --git a/app/Models/DailyMoveMachine.php b/app/Models/AssignmentFails.php similarity index 87% rename from app/Models/DailyMoveMachine.php rename to app/Models/AssignmentFails.php index 033f8681..ea33b98c 100644 --- a/app/Models/DailyMoveMachine.php +++ b/app/Models/AssignmentFails.php @@ -5,7 +5,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; -class DailyMoveMachine extends Model +class AssignmentFails extends Model { use HasFactory; protected $guarded =[]; diff --git a/app/Services/CMMS/DailyMovingMachinesService.php b/app/Services/CMMS/DailyMovingMachinesService.php index 828a83fd..6383eb10 100644 --- a/app/Services/CMMS/DailyMovingMachinesService.php +++ b/app/Services/CMMS/DailyMovingMachinesService.php @@ -3,6 +3,7 @@ namespace App\Services\CMMS; use Exception; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; @@ -13,8 +14,6 @@ class DailyMovingMachinesService protected string $password; protected string $channelName; - - public function __construct() { $this->url = config('cmms_web_services.CHECK_ACTIVITY.url'); @@ -26,12 +25,11 @@ class DailyMovingMachinesService /** * @throws Exception */ - public function run(): array + public function run(): Collection { try { - return $this->sendRequest(); - } - catch (Exception $e) { + return collect( $this->sendRequest()); + } catch (Exception $e) { Log::channel($this->channelName) ->error(get_class($this), [ @@ -56,13 +54,11 @@ class DailyMovingMachinesService private function makeInputParameters(): string { - $inputData = array( - "username" => $this->username, - "password" => $this->password, - "day" => today(), - "minMileage" => 10, - ); - - return json_encode($inputData); + return json_encode([ + "username" => $this->username, + "password" => $this->password, + "day" => today(), + "minMileage" => 10, + ]); } } diff --git a/app/Services/Cartables/Mission/DailyMoveMachineService.php b/app/Services/Cartables/Mission/DailyMoveMachineService.php index 1a63ed8a..b3d08ac1 100644 --- a/app/Services/Cartables/Mission/DailyMoveMachineService.php +++ b/app/Services/Cartables/Mission/DailyMoveMachineService.php @@ -2,20 +2,16 @@ namespace App\Services\Cartables\Mission; -use App\Exceptions\ProhibitedAction; use App\Facades\DataTable\DataTableFacade; -use App\Models\DailyMoveMachine; -use App\Models\Damage; -use App\Models\SafetyAndPrivacy; +use App\Models\AssignmentFails; use Illuminate\Http\Request; -use Illuminate\Support\Facades\DB; class DailyMoveMachineService { public function dataTable(Request $request) { return DataTableFacade::run( - DailyMoveMachine::query(), + AssignmentFails::query(), $request, allowedFilters: ['*'], allowedSortings: ['*'], diff --git a/config/cmms_web_services.php b/config/cmms_web_services.php index 25be6244..2541aa46 100644 --- a/config/cmms_web_services.php +++ b/config/cmms_web_services.php @@ -8,7 +8,7 @@ return [ ], 'CHECK_ACTIVITY' => [ - 'url' => env('CHECKE_LIST_MACHINES_ACTIVITY_URL'), + 'url' => env('CHECKE_LIST_MACHINES_ACTIVITY_URL'), 'password' => env('CHECKE_LIST_MACHINES_ACTIVITY_PASSWORD'), 'username' => env('CHECKE_LIST_MACHINES_ACTIVITY_USERNAME'), ], diff --git a/database/factories/AssignmentFailsFactory.php b/database/factories/AssignmentFailsFactory.php new file mode 100644 index 00000000..1f5144f5 --- /dev/null +++ b/database/factories/AssignmentFailsFactory.php @@ -0,0 +1,18 @@ +id(); - $table->unsignedBigInteger('mission_id')->index()->nullable(); - $table->string('machine_code')->index(); - $table->date('request_date')->index(); + $table->foreignId('mission_id')->constrained('missions')->nullable(); + $table->string('machine_code')->nullable(); + $table->date('request_date')->nullable(); $table->string('type')->nullable(); - $table->unsignedInteger('mileage')->nullable(); + $table->bigInteger('mileage')->nullable(); $table->dateTime('exit_time')->nullable(); $table->dateTime('enter_time')->nullable(); $table->timestamps(); @@ -30,6 +30,6 @@ return new class extends Migration */ public function down(): void { - Schema::dropIfExists('report_mission_and_machines'); + Schema::dropIfExists('assignment_fails'); } };