diff --git a/app/Http/Controllers/V3/Dashboard/Mission/Report/MachineMissionController.php b/app/Http/Controllers/V3/Dashboard/Mission/Report/MachineMissionController.php new file mode 100644 index 00000000..be42e713 --- /dev/null +++ b/app/Http/Controllers/V3/Dashboard/Mission/Report/MachineMissionController.php @@ -0,0 +1,44 @@ +successResponse([ + 'activities' => $reportMissionService->countryActivity($request), + 'provinces' => Province::all(['id', 'name_fa']), + ]); + } + + /** + * @throws Exception + * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception + */ + + public function countryExcelActivity(Request $request, ReportTradodService $reportMissionService): BinaryFileResponse + { + $data = $reportMissionService->countryActivity($request); + $name = 'گزارش کل تردد '.verta()->now()->format('Y-m-d H-i').'.xlsx'; + + return Excel::download(new CountryReport($data), $name); + } + + +} diff --git a/app/Listeners/V3/Dashboard/Harim/RejectRequestListener.php b/app/Listeners/V3/Dashboard/Harim/RejectRequestListener.php index 6c1a148a..343a844c 100644 --- a/app/Listeners/V3/Dashboard/Harim/RejectRequestListener.php +++ b/app/Listeners/V3/Dashboard/Harim/RejectRequestListener.php @@ -2,10 +2,14 @@ namespace App\Listeners\V3\Dashboard\Harim; +use App\Enums\HarimStates; use App\Events\V3\Dashboard\Harim\RejectRequestEvent; use App\Services\PanjarehVahed\SendAccessRoadStatusService; use Exception; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Support\Facades\DB; +use Throwable; + class RejectRequestListener implements ShouldQueue { @@ -15,20 +19,28 @@ class RejectRequestListener implements ShouldQueue * @var string|null */ public ?string $queue = 'harim'; + public int $tries = 2; + public function backoff(): array + { + return [10, 30]; + } - public function __construct(){} + public function __construct( + + private SendAccessRoadStatusService $sendAccessRoadStatusService + ){} /** * @throws Exception */ - public function handle(RejectRequestEvent $event, SendAccessRoadStatusService $sendAccessRoadStatusService): void + public function handle(RejectRequestEvent $event): void { $arrayData = json_decode($event->harim->data, true); - $sendAccessRoadStatusService->makeInputParameters([ + $this->sendAccessRoadStatusService->makeInputParameters([ 'data' => [ - 'actionId' => $arrayData['actionId'], + 'actionId' => 470, 'ruleId' => $arrayData['ruleId'], 'activityId' => $arrayData['activityId'], 'requestId' => $arrayData['requestId'], @@ -41,6 +53,119 @@ class RejectRequestListener implements ShouldQueue 'token' => $arrayData['token'], ]); - $sendAccessRoadStatusService->run(); + $this->sendAccessRoadStatusService->run(); + } + + public function failed(RejectRequestEvent $event, Throwable $e): void + { + $harim = $event->harim->fresh(); + + DB::transaction(function () use ($harim, $e) { + if ((int)$harim->state_id === HarimStates::EMKAN_PAZIR_NEMEBASHAD->value) { + $harim->update([ + 'state_id' => HarimStates::BARESI_TAVASOT_MODIR_KOL->value, // همون 4 + 'state_name' => HarimStates::BARESI_TAVASOT_MODIR_KOL->label(), + ]); + } + + }); } } + +// +//namespace App\Listeners\V3\Dashboard\Harim; +// +//use App\Enums\HarimStates; +//use App\Events\V3\Dashboard\Harim\RejectRequestEvent; +//use App\Services\PanjarehVahed\SendAccessRoadStatusService; +//use Illuminate\Contracts\Queue\ShouldQueue; +//use Illuminate\Support\Facades\DB; +//use Illuminate\Support\Facades\Log; +// +//class RejectRequestListener implements ShouldQueue +//{ +// public ?string $queue = 'harim'; +// +// public function __construct( +// private readonly SendAccessRoadStatusService $sendAccessRoadStatusService +// ) +// { +// } +// +// public function handle(RejectRequestEvent $event): void +// { +// $harim = $event->harim; +// +// $raw = $harim->data; +// +// // ✅ اگر data خالی/Null یا غیر string بود => 500 نده، فقط revert و خارج شو +// if (!is_string($raw) || trim($raw) === '') { +// $this->revertHarim($harim->id, 'harim.data is empty or not string'); +// return; +// } +// +// $arrayData = json_decode($raw, true); +// +// // ✅ اگر json خراب بود => 500 نده، فقط revert و خارج شو +// if (!is_array($arrayData) || json_last_error() !== JSON_ERROR_NONE) { +// $this->revertHarim($harim->id, 'Invalid harim.data JSON: ' . json_last_error_msg()); +// return; +// } +// +// $payload = [ +// 'data' => [ +// 'actionId' => $arrayData['actionId'] ?? 470, +// 'ruleId' => $arrayData['ruleId'] ?? null, +// 'activityId' => $arrayData['activityId'] ?? null, +// 'requestId' => $arrayData['requestId'] ?? null, +// 'taskId' => $arrayData['taskId'] ?? null, +// 'code' => $harim->id, +// 'resultStatus' => 3, +// 'Description' => $harim->last_description, +// ], +// 'token' => $arrayData['token'] ?? null, +// ]; +// +// // ✅ اگر فیلد حیاتی نبود => revert و خارج شو +// if (empty($payload['token']) || empty($payload['data']['requestId'])) { +// $this->revertHarim($harim->id, 'Missing token/requestId for external service'); +// return; +// } +// +// // ✅ اگر سرویس بیرونی fail شد => 500 نده، revert کن و خارج شو +// try { +// $this->sendAccessRoadStatusService->makeInputParameters($payload); +// $this->sendAccessRoadStatusService->run(); +// +// Log::info('RejectRequest external call succeeded', ['harim_id' => $harim->id]); +// +// } catch (\Throwable $e) { +// $this->revertHarim($harim->id, 'External call failed: ' . $e->getMessage()); +// return; +// } +// } +// +// private function revertHarim(int $harimId): void +// { +// DB::transaction(function () use ($harimId) { +// $harim = \App\Models\Harim::query()->lockForUpdate()->find($harimId); +// +// if (!$harim) { +// Log::error('Revert failed: harim not found', ['harim_id' => $harimId]); +// return; +// } +// +// if ((int)$harim->state_id === HarimStates::EMKAN_PAZIR_NEMEBASHAD->value) { +// $harim->update([ +// 'state_id' => HarimStates::BARESI_TAVASOT_MODIR_KOL->value, +// 'state_name' => HarimStates::BARESI_TAVASOT_MODIR_KOL->label(), +// ]); +// } +// +// Log::warning('RejectRequest reverted harim state (sync mode)', [ +// 'harim_id' => $harimId, +// ]); +// }); +// } +//} + diff --git a/app/Services/Cartables/Mission/Report/MachinMissionService.php b/app/Services/Cartables/Mission/Report/MachinMissionService.php new file mode 100644 index 00000000..e4723298 --- /dev/null +++ b/app/Services/Cartables/Mission/Report/MachinMissionService.php @@ -0,0 +1,59 @@ +province_id ?? auth()->user()->province_id; + $from = $request->from_date ?? today()->startOfDay(); + $to = $request->to_date ? Carbon::parse($request->to_date)->endOfDay() : today()->endOfDay(); + + $sql = " + SELECT + cm.machine_code, + cm.car_name, + COALESCE(c.name_fa, '---') AS city_name, + COUNT(m.id) AS missions_count, + COALESCE(SUM(m.in_area_duration), 0) AS gps, + COALESCE(SUM(m.mission_duration), 0) AS durations + + FROM cmms_machines cm + + LEFT JOIN missions m + ON m.machine_code = cm.machine_code + AND m.created_at >= :from + AND m.created_at <= :to + + LEFT JOIN cities c + ON c.id = cm.city_id + + WHERE (:car_group IS NULL OR cm.car_group = :car_group) + AND (:province_id IS NULL OR cm.province_id = :province_id) + + GROUP BY + cm.machine_code, + cm.car_name, + c.name_fa + + ORDER BY + missions_count DESC, + durations DESC, + cm.machine_code; + + "; + + return DB::select($sql, [ + 'province_id' => $provinceId, + 'from' => $from, + 'to' => $to, + ]); + } +} diff --git a/app/Services/PanjarehVahed/SendAccessRoadStatusService.php b/app/Services/PanjarehVahed/SendAccessRoadStatusService.php index 0cdd351e..7e62c663 100644 --- a/app/Services/PanjarehVahed/SendAccessRoadStatusService.php +++ b/app/Services/PanjarehVahed/SendAccessRoadStatusService.php @@ -15,7 +15,7 @@ class SendAccessRoadStatusService public function __construct() { $this->url = config('harim_web_services.Reject.url'); - $this->channelName = ''; + $this->channelName = 'send_access_road'; } /** diff --git a/app/Services/PanjarehVahed/SendPaymentInfoService.php b/app/Services/PanjarehVahed/SendPaymentInfoService.php index 2a591e2c..90b859ae 100644 --- a/app/Services/PanjarehVahed/SendPaymentInfoService.php +++ b/app/Services/PanjarehVahed/SendPaymentInfoService.php @@ -15,7 +15,7 @@ class SendPaymentInfoService public function __construct() { $this->url = config('harim_web_services.Reject.url'); - $this->channelName = ''; + $this->channelName = 'send_payment_info'; } /** diff --git a/config/logging.php b/config/logging.php index 14edc387..bc7a1353 100644 --- a/config/logging.php +++ b/config/logging.php @@ -152,5 +152,15 @@ return [ 'path' => storage_path('logs/cmms/daily_moving_machine_service.log'), 'level' => 'info', ], + 'send_access_road' => [ + 'driver' => 'single', + 'path' => storage_path('logs/harim/send_access_road_service.log'), + 'level' => 'info', + ], + 'send_payment_info' => [ + 'driver' => 'single', + 'path' => storage_path('logs/harim/send_payment_info_service.log'), + 'level' => 'info', + ], ], ];