Refactor script
This commit is contained in:
42
app/Http/Controllers/Api/V1/PlanningController.php
Normal file
42
app/Http/Controllers/Api/V1/PlanningController.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\SalesPlan;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class PlanningController extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /api/v1/plans
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$query = SalesPlan::query();
|
||||
|
||||
if ($request->has('date')) {
|
||||
$query->whereDate('date', $request->date);
|
||||
}
|
||||
|
||||
$plans = $query->get();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $plans
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/v1/plans
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Plan created (Sandbox simulation)',
|
||||
'id' => 'PLAN-' . uniqid()
|
||||
]);
|
||||
}
|
||||
}
|
||||
100
app/Http/Controllers/Api/V1/TrackingController.php
Normal file
100
app/Http/Controllers/Api/V1/TrackingController.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\User;
|
||||
use App\Models\SalesRoute;
|
||||
use App\Models\Waypoint;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class TrackingController extends Controller
|
||||
{
|
||||
/**
|
||||
* GET /api/v1/routes
|
||||
* Get routes for the authenticated agent's scope (currently global)
|
||||
*/
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$query = SalesRoute::with('user:id,employee_id,name,color');
|
||||
|
||||
if ($request->has('date')) {
|
||||
$query->whereDate('date', $request->date);
|
||||
}
|
||||
|
||||
$routes = $query->orderBy('date', 'desc')->get();
|
||||
|
||||
$formattedRoutes = $routes->map(function ($route) {
|
||||
return [
|
||||
'id' => (string) $route->id,
|
||||
'salesId' => $route->user->employee_id ?? 'UNKNOWN',
|
||||
'salesName' => $route->user->name ?? 'Unknown',
|
||||
'date' => $route->date->format('Y-m-d'),
|
||||
'waypoints' => $route->waypoints->map(function ($wp) {
|
||||
return [
|
||||
'lat' => (float) $wp->latitude,
|
||||
'lng' => (float) $wp->longitude,
|
||||
'time' => Carbon::parse($wp->recorded_at)->format('H:i'),
|
||||
'type' => $wp->type,
|
||||
];
|
||||
}),
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $formattedRoutes
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/v1/routes/waypoints
|
||||
* Add waypoint to active route
|
||||
*/
|
||||
public function storeWaypoint(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'lat' => 'required|numeric',
|
||||
'lng' => 'required|numeric',
|
||||
'employee_id' => 'required|string', // Required to identify who is moving
|
||||
'type' => 'nullable|in:gps,visit,checkin,checkout'
|
||||
]);
|
||||
|
||||
// Find user
|
||||
$user = User::where('employee_id', $request->employee_id)->first();
|
||||
if (!$user) {
|
||||
return response()->json(['success' => false, 'error' => 'User (Employee) not found'], 404);
|
||||
}
|
||||
|
||||
$today = Carbon::today();
|
||||
|
||||
// Get or create today's route
|
||||
$route = SalesRoute::firstOrCreate(
|
||||
['user_id' => $user->id, 'date' => $today],
|
||||
['status' => 'active', 'started_at' => now()]
|
||||
);
|
||||
|
||||
// Create waypoint
|
||||
$waypoint = Waypoint::create([
|
||||
'sales_route_id' => $route->id,
|
||||
'type' => $request->type ?? 'gps',
|
||||
'latitude' => $request->lat,
|
||||
'longitude' => $request->lng,
|
||||
'recorded_at' => now(),
|
||||
'location_name' => 'API Integration',
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Waypoint recorded successfully',
|
||||
'data' => [
|
||||
'id' => $waypoint->id,
|
||||
'lat' => $waypoint->latitude,
|
||||
'lng' => $waypoint->longitude,
|
||||
'recorded_at' => $waypoint->recorded_at
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user