Fixed 222 style issues across 399 files: - class_definition, braces_position formatting - concat_space, trailing_comma_in_multiline - ordered_imports, no_unused_imports - not_operator_with_successor_space - no_whitespace_in_blank_line - single_blank_line_at_eof - And many more PSR-12/Laravel conventions All files now pass Laravel Pint code style checks.
180 lines
6.1 KiB
PHP
180 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Manifest;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class DeliveryController extends Controller
|
|
{
|
|
/**
|
|
* Show driver verification screen.
|
|
*/
|
|
public function verify(string $token): View|RedirectResponse
|
|
{
|
|
$manifest = Manifest::where('delivery_token', $token)->first();
|
|
|
|
// Check if token is valid
|
|
if (! $manifest) {
|
|
abort(404, 'Invalid delivery link. Please contact your dispatcher.');
|
|
}
|
|
|
|
// Check if token has expired
|
|
if (! $manifest->isDeliveryTokenValid()) {
|
|
return view('seller.delivery.expired', compact('manifest'));
|
|
}
|
|
|
|
// Check if already verified in session
|
|
if (session("delivery_verified_{$token}")) {
|
|
return redirect()->route('seller.delivery.dashboard', ['token' => $token]);
|
|
}
|
|
|
|
// Show verification form
|
|
return view('seller.delivery.verify', compact('manifest', 'token'));
|
|
}
|
|
|
|
/**
|
|
* Authenticate driver using first name and last initial.
|
|
*/
|
|
public function authenticate(string $token, Request $request): RedirectResponse
|
|
{
|
|
$manifest = Manifest::where('delivery_token', $token)->firstOrFail();
|
|
|
|
// Check if token has expired
|
|
if (! $manifest->isDeliveryTokenValid()) {
|
|
return back()->with('error', 'This delivery link has expired.');
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'first_name' => 'required|string|max:255',
|
|
'last_initial' => 'required|string|size:1',
|
|
]);
|
|
|
|
// Verify driver identity
|
|
if (! $manifest->verifyDriver($validated['first_name'], $validated['last_initial'])) {
|
|
return back()
|
|
->withInput()
|
|
->withErrors(['verification' => 'Driver verification failed. Please check your name and try again.']);
|
|
}
|
|
|
|
// Mark driver as verified
|
|
$manifest->markDriverVerified();
|
|
|
|
// Store verification in session
|
|
session(["delivery_verified_{$token}" => true]);
|
|
|
|
return redirect()->route('seller.delivery.dashboard', ['token' => $token])
|
|
->with('success', 'Driver verified successfully!');
|
|
}
|
|
|
|
/**
|
|
* Show delivery dashboard with today's deliveries.
|
|
*/
|
|
public function dashboard(string $token): View|RedirectResponse
|
|
{
|
|
$manifest = Manifest::where('delivery_token', $token)->firstOrFail();
|
|
|
|
// Check if token has expired
|
|
if (! $manifest->isDeliveryTokenValid()) {
|
|
return view('seller.delivery.expired', compact('manifest'));
|
|
}
|
|
|
|
// Check if driver is verified
|
|
if (! session("delivery_verified_{$token}")) {
|
|
return redirect()->route('seller.delivery.verify', ['token' => $token]);
|
|
}
|
|
|
|
// Load relationships
|
|
$manifest->load([
|
|
'order.items.product.brand',
|
|
'order.items.product.labs',
|
|
'sellerCompany',
|
|
'buyerCompany',
|
|
'deliveryLocation',
|
|
]);
|
|
|
|
// Get all deliveries for this date (same driver)
|
|
$deliveries = Manifest::where('scheduled_delivery_date', $manifest->scheduled_delivery_date)
|
|
->where('driver_first_name', $manifest->driver_first_name)
|
|
->where('driver_last_initial', $manifest->driver_last_initial)
|
|
->where('status', 'scheduled')
|
|
->with([
|
|
'order.items.product.brand',
|
|
'buyerCompany',
|
|
])
|
|
->orderBy('delivery_time_window')
|
|
->get();
|
|
|
|
return view('seller.delivery.dashboard', compact('manifest', 'deliveries', 'token'));
|
|
}
|
|
|
|
/**
|
|
* Mark delivery as completed.
|
|
*/
|
|
public function complete(string $token, Request $request): RedirectResponse
|
|
{
|
|
$manifest = Manifest::where('delivery_token', $token)->firstOrFail();
|
|
|
|
// Check if token has expired
|
|
if (! $manifest->isDeliveryTokenValid()) {
|
|
return back()->with('error', 'This delivery link has expired.');
|
|
}
|
|
|
|
// Check if driver is verified
|
|
if (! session("delivery_verified_{$token}")) {
|
|
return redirect()->route('seller.delivery.verify', ['token' => $token]);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'manifest_id' => 'required|exists:manifests,id',
|
|
'odometer' => 'nullable|integer|min:0',
|
|
'notes' => 'nullable|string|max:1000',
|
|
]);
|
|
|
|
$deliveryManifest = Manifest::findOrFail($validated['manifest_id']);
|
|
|
|
// Complete the delivery
|
|
$deliveryManifest->completeDelivery(
|
|
odometer: $validated['odometer'] ?? null,
|
|
notes: $validated['notes'] ?? null
|
|
);
|
|
|
|
return back()->with('success', "Delivery {$deliveryManifest->manifest_number} marked as completed!");
|
|
}
|
|
|
|
/**
|
|
* Display printable route sheet for the day's deliveries.
|
|
*/
|
|
public function routeSheet(string $token): View|RedirectResponse
|
|
{
|
|
$manifest = Manifest::where('delivery_token', $token)->firstOrFail();
|
|
|
|
// Check if token has expired
|
|
if (! $manifest->isDeliveryTokenValid()) {
|
|
return view('seller.delivery.expired', compact('manifest'));
|
|
}
|
|
|
|
// Check if driver is verified
|
|
if (! session("delivery_verified_{$token}")) {
|
|
return redirect()->route('seller.delivery.verify', ['token' => $token]);
|
|
}
|
|
|
|
// Get all deliveries for this date (same driver)
|
|
$deliveries = Manifest::where('scheduled_delivery_date', $manifest->scheduled_delivery_date)
|
|
->where('driver_first_name', $manifest->driver_first_name)
|
|
->where('driver_last_initial', $manifest->driver_last_initial)
|
|
->whereIn('status', ['scheduled', 'delivered'])
|
|
->with([
|
|
'order.items.product.brand',
|
|
'buyerCompany',
|
|
'sellerCompany',
|
|
])
|
|
->orderBy('delivery_time_window')
|
|
->get();
|
|
|
|
return view('seller.delivery.route-sheet', compact('manifest', 'deliveries'));
|
|
}
|
|
}
|