Merge pull request #94 from witelgroup/feature/CheckingMoveMachinceForMission

Create prosess for check moving machine in mission
This commit is contained in:
Amir Ghasempoor
2025-11-16 11:24:32 +03:30
committed by GitHub
13 changed files with 352 additions and 1 deletions

View File

@@ -98,3 +98,7 @@ BACKUP_PASSWORD=
HARIM_ENCRYPTION_KEY="TestKey"
HARIM_ENCRYPTION_IV="1111000011110101"
CHECKE_LIST_MACHINES_ACTIVITY_URL='fms.141.ir:7030/api/RMS/GetMovingVehicles',
CHECKE_LIST_MACHINES_ACTIVITY_PASSWORD=
CHECKE_LIST_MACHINES_ACTIVITY_USERNAME=

View File

@@ -0,0 +1,78 @@
<?php
namespace App\Console\Commands;
use App\Enums\ActivityMachineType;
use App\Models\Mission;
use App\Models\MissionViolation;
use App\Services\CMMS\DailyMovingMachinesService;
use Exception;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
class DailyMovingMachineCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'daily_moving_machine';
/**
* The console command description.
*
* @var string
*/
protected $description = 'receive cmms cars information and compare my information';
/**
* Execute the console command.
* @throws Exception
*/
public function handle(DailyMovingMachinesService $dailyMovingMachines): void
{
$vehiclesInfo = $dailyMovingMachines->run();
$response = collect($vehiclesInfo['vehiclesInfo']);
$date = Carbon::yesterday()->toDateString();
$cmms = $response->pluck('machineCode');
$mission = Mission::query()
->whereDate('start_time',$date)
->pluck('machine_code');
$cmmsOnly = $cmms->diff($mission);
$missionOnly = $mission->diff($cmms);
foreach ($cmmsOnly as $code) {
$cmm = $response->firstWhere('machineCode', '=', $code);
MissionViolation::query()->create([
'machine_code' => $code,
'type' => ActivityMachineType::KHOROJ_BEDONE_MOGAVEZ->value,
'request_date' => $date,
'mileage' => $cmm['mileageKM'],
// 'exit_time' => $cmm['exit_time'],
// 'enter_time' => $cmm['enter_time'],
]);
}
foreach ($missionOnly as $code) {
$missionId = Mission::query()->where('machine_code', $code)
->whereDate('start_time', $date)
->value('id');
MissionViolation::query()->create([
'machine_code' => $code,
'mission_id' => $missionId,
'type' => ActivityMachineType::ADAM_TAHAROK->value,
'request_date' => $date,
]);
}
$this->info("ماشین‌های متحرک بدون مأموریت: " . $cmmsOnly->count());
$this->info("مأموریت‌های بدون حرکت در CMMS: " . $missionOnly->count());
}
}

View File

@@ -16,6 +16,7 @@ class Kernel extends ConsoleKernel
protected $commands = [
'App\Console\Commands\RoadObservationProblems', //// road observed webservice from 141 sawaneh
'App\Console\Commands\SendContractSmsNotification', //// road observed webservice from 141 sawaneh
'App\Console\Commands\DailyMovingMachineCommand',
];
/**
@@ -48,6 +49,9 @@ class Kernel extends ConsoleKernel
->dailyAt('04:00')
->appendOutputTo(storage_path('logs/telescope.log'));
$schedule->command('daily_moving_machine')
->dailyAt('02:00')->appendOutputTo(storage_path('logs/dailymovingmachine.log'));
}
/**

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Enums;
enum ActivityMachineType: int
{
case KHOROJ_BEDONE_MOGAVEZ = 1;
case ADAM_TAHAROK = 2;
public function label(): string
{
return match ($this) {
self::KHOROJ_BEDONE_MOGAVEZ => "خروج بدون مجوز ",
self::ADAM_TAHAROK => "عدم تحرک",
};
}
}

View File

@@ -2,7 +2,6 @@
namespace App\Events\V3\Dashboard\Mission;
use App\Models\Harim;
use App\Models\Mission;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;

View File

@@ -0,0 +1,79 @@
<?php
namespace App\Http\Controllers\V3\Dashboard\Mission;
use App\Enums\MissionCategory;
use App\Enums\MissionStates;
use App\Enums\MissionTypes;
use App\Enums\MissionZones;
use App\Http\Controllers\Controller;
use App\Http\Controllers\V3\Dashboard\RoadPatrol\OperatorController;
use App\Http\Requests\V3\Mission\RequestPortal\NoProcessRequest;
use App\Http\Traits\ApiResponse;
use App\Models\Mission;
use App\Services\Cartables\Mission\DailyMoveMachineService;
use Carbon\Carbon;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ViolationManagementController extends Controller
{
use ApiResponse;
public function index(Request $request, DailyMoveMachineService $dailyMoveMachineService): JsonResponse
{
$data = $dailyMoveMachineService->dataTable($request);
return response()->json($data);
}
public function noProcess(NoProcessRequest $request): JsonResponse
{
DB::transaction(function () use ($request) {
$start = Carbon::parse($request->start_date);
$end = Carbon::parse($request->end_date);
$type = $start->diffInMinutes($end) > 480 ? MissionTypes::NO_PROCESS_ROZANE->value : MissionTypes::NO_PROCESS_SAATY->value;
$user = auth()->user();
$zone = $request->zone;
$state = MissionStates::END_MISSION->value;
$mission = Mission::query()->create([
'user_id' => $user->id,
'username' => $user->username,
'province_id' => $user->province_id,
'province_name' => $user->province_fa,
'edare_shahri_id' => $user->edarate_shahri_id ?? null,
'edare_shahri_name' => $user->edarate_shahri_name ?? null,
'zone' => $zone,
'zone_fa' => MissionZones::name($zone),
'type' => $type,
'type_fa' => MissionTypes::name($type),
'start_date' => $request->start_date,
'end_date' => $request->end_date,
'end_point' => $request->end_point,
'area' => json_encode($request->area),
'category_id' => $request->category_id,
'category_name' => MissionCategory::name($request->category_id),
'start_time' => $request->start_date,
'finish_time' => $request->end_date,
'state_id' => $state,
'state_name' => MissionStates::name($state),
'explanation' => $request->explanation,
'request_date' => now(),
]);
$mission->rahdaran()->sync($request->rahdaran);
$mission->machines()->attach($request->machines);
$mission->rahdaran()->attach($request->driver, ['is_driver' => true]);
if ($mission->category_id == 2) {
$rpc = new OperatorController;
$rpc->store($mission);
}
});
return $this->successResponse();
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class MissionViolation extends Model
{
use HasFactory;
protected $guarded =[];
public function mission()
{
return $this->belongsTo(Mission::class);
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Services\CMMS;
use Exception;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class DailyMovingMachinesService
{
protected string $url;
protected string $username;
protected string $password;
protected string $channelName;
public function __construct()
{
$this->url = config('cmms_web_services.CHECK_ACTIVITY.url');
$this->password = config('cmms_web_services.CHECK_ACTIVITY.password');
$this->username = config('cmms_web_services.CHECK_ACTIVITY.username');
$this->channelName = 'daily_moving_machine';
}
/**
* @throws Exception
*/
public function run(): array
{
try {
return $this->sendRequest();
} catch (Exception $e) {
Log::channel($this->channelName)
->error(get_class($this),
[
'message' => $e->getMessage(),
]
);
throw $e;
}
}
public function sendRequest(): array
{
$inputData = $this->makeInputParameters();
return Http::withBody($inputData)
->throw()
->withoutVerifying()
->post($this->url)
->json();
}
private function makeInputParameters(): string
{
$day = Carbon::yesterday()->toDateString();
return json_encode([
"username" => $this->username,
"password" => $this->password,
"day" => $day,
"minMileage" => 10,
]);
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Services\Cartables\Mission;
use App\Facades\DataTable\DataTableFacade;
use App\Models\MissionViolation;
use Illuminate\Http\Request;
class DailyMoveMachineService
{
public function dataTable(Request $request)
{
return DataTableFacade::run(
MissionViolation::query(),
$request,
allowedFilters: ['*'],
allowedSortings: ['*'],
allowedSelects: ['id', 'mission_id', 'machine_code', 'request_date', 'type', 'exit_time', 'enter_time']
);
}
}

View File

@@ -6,4 +6,10 @@ return [
'password' => env('CMMS_MACHINE_INFO_PASSWORD'),
'ViewName' => env('CMMS_MACHINE_INFO_VIEW_NAME'),
],
'CHECK_ACTIVITY' => [
'url' => env('CHECKE_LIST_MACHINES_ACTIVITY_URL'),
'password' => env('CHECKE_LIST_MACHINES_ACTIVITY_PASSWORD'),
'username' => env('CHECKE_LIST_MACHINES_ACTIVITY_USERNAME'),
],
];

View File

@@ -147,5 +147,10 @@ return [
'path' => storage_path('logs/telescope.log'),
'level' => 'info',
],
'daily_moving_machine' => [
'driver' => 'single',
'path' => storage_path('logs/cmms/daily_moving_machine_service.log'),
'level' => 'info',
],
],
];

View File

@@ -0,0 +1,18 @@
<?php
namespace Database\Factories;
use App\Models\MissionViolation;
use Illuminate\Database\Eloquent\Factories\Factory;
class AssignmentFailsFactory extends Factory
{
protected $model = MissionViolation::class;
public function definition(): array
{
return [
];
}
}

View File

@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('mission_violations', function (Blueprint $table) {
$table->id();
$table->foreignId('mission_id')->nullable()->constrained('missions');
$table->string('machine_code')->nullable();
$table->date('request_date')->nullable();
$table->tinyInteger('type')->nullable();
$table->bigInteger('mileage')->nullable();
$table->dateTime('exit_time')->nullable();
$table->dateTime('enter_time')->nullable();
$table->timestamps();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('mission_violations');
}
};