ifnex/04_Laravel/app/Http/Controllers/Api/CommitmentFormController.php
Kazem Alghasi 7a8ff49baa feat(api): implement shipment commitment forms and document downloads
Introduce a complete workflow for managing shipment-related documents
and mandatory commitment forms between the Laravel backend and
WordPress frontend.

- Laravel:
  - Add `ShipmentCommitmentForm` model and migration to track signed
    forms per shipment.
  - Implement `CommitmentFormController` to handle fetching available
    forms and uploading signed documents.
  - Add PDF download endpoints for AWB, labels, and invoices via
    `ShipmentPdfController`.
  - Extend `User` model with notification management methods.
  - Add `FinanceOverviewWidget` for Filament dashboard.

- WordPress:
  - Implement AJAX handlers in `user-bridge.php` for PDF downloads and
    commitment form management.
  - Update `shortcodes.php` to display document download grid and
    dynamic commitment form upload interface in order details.
  - Add styling for document buttons and form status indicators in
    `ifnex-orders.css`.
2026-09-09 13:08:22 +03:30

181 lines
6.3 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\CommitmentForm;
use App\Models\Shipment;
use App\Models\ShipmentCommitmentForm;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class CommitmentFormController extends Controller
{
/**
* دریافت لیست فایل‌های تعهدنامه فعال
* GET /api/v1/commitment-forms
*/
public function index(): JsonResponse
{
$forms = CommitmentForm::query()
->where('is_active', true)
->orderBy('sort_order')
->orderBy('created_at', 'desc')
->get()
->map(fn ($form) => [
'id' => $form->id,
'title' => $form->title,
'description' => $form->description,
'file_url' => $form->file_url,
'file_type' => strtoupper(pathinfo($form->file_path, PATHINFO_EXTENSION)),
'direction' => $form->direction,
]);
return response()->json([
'success' => true,
'data' => $forms,
]);
}
/**
* دریافت تعهدنامه‌ها بر اساس جهت ارسال
* GET /api/v1/commitment-forms/{direction}
*/
public function byDirection(string $direction): JsonResponse
{
$forms = CommitmentForm::query()
->where('is_active', true)
->where(function ($query) use ($direction) {
$query->where('direction', 'both')
->orWhere('direction', $direction);
})
->orderBy('sort_order')
->orderBy('created_at', 'desc')
->get()
->map(fn ($form) => [
'id' => $form->id,
'title' => $form->title,
'description' => $form->description,
'file_url' => $form->file_url,
'file_type' => strtoupper(pathinfo($form->file_path, PATHINFO_EXTENSION)),
'direction' => $form->direction,
]);
return response()->json([
'success' => true,
'data' => $forms,
]);
}
/**
* دریافت لیست تعهدنامه‌های مربوط به سفارش خاص
* GET /api/v1/customer/orders/{shipment}/commitment-forms
*/
public function shipmentForms(Shipment $shipment): JsonResponse
{
$user = auth()->user();
// بررسی مالکیت
if ($shipment->user_id !== $user->id) {
return response()->json([
'success' => false,
'message' => 'شما به این سفارش دسترسی ندارید.',
], 403);
}
$forms = CommitmentForm::query()
->where('is_active', true)
->where(function ($query) use ($shipment) {
$query->where('direction', 'both')
->orWhere('direction', $shipment->direction);
})
->orderBy('sort_order')
->orderBy('created_at', 'desc')
->get()
->map(function ($form) use ($shipment) {
$upload = ShipmentCommitmentForm::where('shipment_id', $shipment->id)
->where('commitment_form_id', $form->id)
->first();
return [
'id' => $form->id,
'title' => $form->title,
'description' => $form->description,
'file_url' => $form->file_url,
'file_type' => strtoupper(pathinfo($form->file_path, PATHINFO_EXTENSION)),
'direction' => $form->direction,
'upload_status' => $upload ? $upload->status : 'pending',
'uploaded_file_url' => $upload && $upload->uploaded_file_path ? asset('storage/' . $upload->uploaded_file_path) : null,
'uploaded_at' => $upload ? $upload->created_at : null,
'notes' => $upload ? $upload->notes : null,
];
});
return response()->json([
'success' => true,
'data' => $forms,
]);
}
/**
* آپلود فرم تعهدنامه امضاشده
* POST /api/v1/customer/orders/{shipment}/commitment-forms/{form}/upload
*/
public function uploadSigned(Request $request, Shipment $shipment, CommitmentForm $form): JsonResponse
{
$user = auth()->user();
// بررسی مالکیت
if ($shipment->user_id !== $user->id) {
return response()->json([
'success' => false,
'message' => 'شما به این سفارش دسترسی ندارید.',
], 403);
}
$request->validate([
'file' => 'required|file|mimes:pdf,jpg,jpeg,png|max:5120', // حداکثر 5MB
'notes' => 'nullable|string|max:1000',
]);
try {
$file = $request->file('file');
$path = $file->store("commitment-forms/{$shipment->id}", 'public');
$upload = ShipmentCommitmentForm::updateOrCreate(
[
'shipment_id' => $shipment->id,
'commitment_form_id' => $form->id,
],
[
'uploaded_file_path' => $path,
'uploaded_file_type' => $file->getClientOriginalExtension(),
'uploaded_file_size' => $file->getSize(),
'status' => 'uploaded',
'notes' => $request->input('notes'),
'uploaded_by' => $user->id,
]
);
return response()->json([
'success' => true,
'message' => 'فایل با موفقیت آپلود شد.',
'data' => [
'id' => $upload->id,
'file_url' => asset('storage/' . $path),
'file_type' => $upload->uploaded_file_type,
'status' => $upload->status,
'uploaded_at' => $upload->created_at,
],
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'message' => 'خطا در آپلود فایل: ' . $e->getMessage(),
], 500);
}
}
}