ifnex/04_Laravel/app/Http/Controllers/OrderController.php
Kazem Alghasi 24fc56feaf feat(order): implement order creation flow and pricing API
Introduce order management functionality including web routes for creating orders and a new API endpoint for price calculations. This change also cleans up the codebase by removing various debug scripts used during the development of the shipping rate logic.

- Add `OrderController` to handle order creation and success redirection.
- Add `POST /v1/calculate` endpoint for pricing calculations.
- Define web routes for order creation flow (`/order`).
- Remove multiple debug PHP scripts from the `04_Laravel` directory.
- Add base layout and order view directories.
2026-08-04 07:59:23 +03:30

110 lines
4.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Enums\ShipmentDirection;
use App\Enums\ShipmentType;
use App\Models\Country;
use App\Models\Shipment;
use App\Models\ShipmentItem;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class OrderController extends Controller
{
public function create()
{
$countries = Country::orderBy('name')->get();
$directions = ShipmentDirection::cases();
$types = ShipmentType::cases();
return view('orders.create', compact('countries', 'directions', 'types'));
}
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'direction' => ['required', 'in:export,import'],
'type' => ['required', 'in:DOC_NORMAL,DOC_ECONOMY,PARCEL'],
'from_country_id' => ['required', 'exists:countries,id'],
'to_country_id' => ['required', 'exists:countries,id'],
'forwarder' => ['nullable', 'string', 'max:255'],
'weight' => ['required', 'numeric', 'min:0.1'],
'volumetric_weight' => ['nullable', 'numeric', 'min:0'],
'dimensions' => ['nullable', 'string', 'max:255'],
'sender_name' => ['required', 'string', 'max:255'],
'sender_company' => ['nullable', 'string', 'max:255'],
'sender_phone' => ['required', 'string', 'max:50'],
'sender_email' => ['nullable', 'email', 'max:255'],
'sender_address' => ['required', 'string', 'max:500'],
'sender_city' => ['required', 'string', 'max:255'],
'sender_zip' => ['nullable', 'string', 'max:20'],
'sender_id_number' => ['nullable', 'string', 'max:50'],
'receiver_name' => ['required', 'string', 'max:255'],
'receiver_company' => ['nullable', 'string', 'max:255'],
'receiver_phone' => ['required', 'string', 'max:50'],
'receiver_email' => ['nullable', 'email', 'max:255'],
'receiver_address' => ['required', 'string', 'max:500'],
'receiver_city' => ['required', 'string', 'max:255'],
'receiver_zip' => ['nullable', 'string', 'max:20'],
'receiver_id_number' => ['nullable', 'string', 'max:50'],
'items' => ['required', 'array', 'min:1', 'max:9'],
'items.*.description' => ['required', 'string', 'max:500'],
'items.*.hs_code' => ['required', 'string', 'max:50'],
'items.*.quantity' => ['required', 'numeric', 'min:1'],
'items.*.unit_price' => ['required', 'numeric', 'min:0'],
'items.*.total_usd' => ['required', 'numeric', 'min:0'],
]);
$data = $validator->validated();
$shipment = Shipment::create([
'awb_no' => 'IFNEX-' . strtoupper(uniqid()),
'direction' => $data['direction'],
'type' => $data['type'],
'from_country_id' => $data['from_country_id'],
'to_country_id' => $data['to_country_id'],
'forwarder' => $data['forwarder'] ?? null,
'weight' => $data['weight'],
'volumetric_weight' => $data['volumetric_weight'] ?? $data['weight'],
'chargeable_weight' => max($data['weight'], $data['volumetric_weight'] ?? $data['weight']),
'dimensions' => $data['dimensions'] ?? null,
'sender_name' => $data['sender_name'],
'sender_company' => $data['sender_company'] ?? null,
'sender_phone' => $data['sender_phone'],
'sender_email' => $data['sender_email'] ?? null,
'sender_address' => $data['sender_address'],
'sender_city' => $data['sender_city'],
'sender_zip' => $data['sender_zip'] ?? null,
'sender_id_number' => $data['sender_id_number'] ?? null,
'receiver_name' => $data['receiver_name'],
'receiver_company' => $data['receiver_company'] ?? null,
'receiver_phone' => $data['receiver_phone'],
'receiver_email' => $data['receiver_email'] ?? null,
'receiver_address' => $data['receiver_address'],
'receiver_city' => $data['receiver_city'],
'receiver_zip' => $data['receiver_zip'] ?? null,
'receiver_id_number' => $data['receiver_id_number'] ?? null,
'status' => 'processed',
]);
foreach ($data['items'] as $index => $item) {
$shipment->items()->create([
'row_number' => $index + 1,
'description' => $item['description'],
'hs_code' => $item['hs_code'],
'quantity' => $item['quantity'],
'unit_price' => $item['unit_price'],
'total_usd' => $item['total_usd'],
]);
}
return redirect()->route('orders.success', $shipment)->with('success', 'سفارش شما با موفقیت ثبت شد.');
}
public function success(Shipment $shipment)
{
return view('orders.success', compact('shipment'));
}
}