2025-04-25 11:30:44 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace app\controller;
|
|
|
|
|
|
|
|
|
|
use app\model\Location;
|
2025-06-10 14:47:46 +08:00
|
|
|
use app\model\Vehicle;
|
2025-04-30 11:30:08 +08:00
|
|
|
use util\Util;
|
2025-04-25 11:30:44 +08:00
|
|
|
use support\Request;
|
|
|
|
|
|
|
|
|
|
class LocationController
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Get all locations that have vehicles
|
|
|
|
|
*/
|
|
|
|
|
public function index(Request $request)
|
|
|
|
|
{
|
2025-04-25 17:22:48 +08:00
|
|
|
$locations = Location::with('vehicles')
|
2025-04-25 16:54:54 +08:00
|
|
|
->with('fboVehicles')
|
2025-04-25 11:30:44 +08:00
|
|
|
->get()
|
|
|
|
|
->map(function ($location) {
|
|
|
|
|
$location->vehicle_count = $location->vehicles->count();
|
2025-04-25 17:17:06 +08:00
|
|
|
$location->fbo_vehicle_count = $location->fboVehicles->count();
|
2025-05-28 13:31:56 +08:00
|
|
|
$location->count = ($location->vehicle_count > 0) ? $location->vehicle_count : $location->fbo_vehicle_count;
|
2025-04-25 11:30:44 +08:00
|
|
|
return $location;
|
|
|
|
|
});
|
2025-06-10 14:47:46 +08:00
|
|
|
foreach ($locations as &$location) {
|
|
|
|
|
unset($location->vehicles);
|
|
|
|
|
unset($location->fboVehicles);
|
|
|
|
|
}
|
|
|
|
|
return Util::success($locations);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all locations that have vehicles
|
|
|
|
|
*/
|
|
|
|
|
public function production(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$where = [];
|
|
|
|
|
$status = $request->post('status', 4);
|
|
|
|
|
if($status == 1){
|
|
|
|
|
$status = [5,6];
|
|
|
|
|
}elseif ($status == 2){
|
|
|
|
|
$status = [4];
|
|
|
|
|
}elseif ($status == 3){
|
|
|
|
|
$status = [1,2,3,7,8];
|
|
|
|
|
$where[] = ['vin' ,'<>', null];
|
|
|
|
|
}elseif ($status == 4){
|
|
|
|
|
$status = [1,2,3,7,8];
|
|
|
|
|
$where[] = ['vin' ,'=', null];
|
|
|
|
|
}
|
|
|
|
|
$vehicles = Vehicle::select('locations_id')->where($where)->whereNotNull('locations_id');
|
|
|
|
|
if(!empty($status)){
|
|
|
|
|
$vehicles = $vehicles->whereIn('status',$status);
|
|
|
|
|
}
|
|
|
|
|
$locations_id = $vehicles->groupBy('locations_id')->get();
|
|
|
|
|
$locations_id = $locations_id->pluck('locations_id')->toArray();
|
|
|
|
|
|
|
|
|
|
$locations = Location::with('vehicles')
|
|
|
|
|
->with('fboVehicles')
|
|
|
|
|
->whereIn('id',$locations_id)
|
|
|
|
|
->get()
|
|
|
|
|
->map(function ($location) {
|
|
|
|
|
$location->vehicle_count = $location->vehicles->count();
|
|
|
|
|
$location->fbo_vehicle_count = $location->fboVehicles->count();
|
|
|
|
|
$location->count = ($location->vehicle_count > 0) ? $location->vehicle_count : $location->fbo_vehicle_count;
|
|
|
|
|
return $location;
|
|
|
|
|
});
|
|
|
|
|
foreach ($locations as &$location) {
|
|
|
|
|
unset($location->vehicles);
|
|
|
|
|
unset($location->fboVehicles);
|
|
|
|
|
}
|
2025-04-25 11:30:44 +08:00
|
|
|
return Util::success($locations);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a specific location with its vehicles
|
|
|
|
|
*/
|
|
|
|
|
public function show(Request $request, $id)
|
|
|
|
|
{
|
|
|
|
|
$location = Location::hasVehicles()
|
|
|
|
|
->with('vehicles')
|
|
|
|
|
->find($id);
|
|
|
|
|
|
|
|
|
|
if (!$location) {
|
|
|
|
|
return Util::fail(null, 'Location not found or has no vehicles');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Util::success($location);
|
|
|
|
|
}
|
|
|
|
|
}
|