Files
project/app/Http/Controllers/Api/SupplierController.php
2025-02-13 17:13:20 +01:00

102 lines
2.8 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Models\Supplier;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class SupplierController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(): JsonResponse
{
$suppliers = Supplier::orderBy('name')->with(['invoices'])->get();
return response()->json($suppliers);
}
/**
* Display a listing of the resource.
*/
public function withTrashed(): JsonResponse
{
return response()->json(Supplier::with(['invoices'])->withTrashed()->orderBy('name')->get());
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): JsonResponse
{
$supplierData = $request->validate([
'name' => 'required|string',
'registration_name' => 'nullable|string',
'email' => 'nullable|email',
'address' => 'nullable|string',
'city' => 'nullable|string',
'zip' => 'nullable|string',
'country_code' => 'nullable|string',
'tax_fc' => 'nullable|string',
'tax_vat' => 'nullable|string',
'contact_name' => 'nullable|string',
'contact_phone' => 'nullable|string',
'contact_email' => 'nullable|string',
]);
$supplier = new Supplier($supplierData);
$supplier->save();
return response()->json($supplier);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Supplier $supplier): JsonResponse
{
$supplierData = $request->validate([
'name' => 'required|string',
'registration_name' => 'nullable|string',
'email' => 'nullable|email',
'address' => 'nullable|string',
'city' => 'nullable|string',
'zip' => 'nullable|string',
'country_code' => 'nullable|string',
'tax_fc' => 'nullable|string',
'tax_vat' => 'nullable|string',
'contact_name' => 'nullable|string',
'contact_phone' => 'nullable|string',
'contact_email' => 'nullable|string',
]);
$supplier->update($supplierData);
return response()->json($supplier);
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Supplier $supplier): JsonResponse
{
$supplier->delete();
return response()->json(null, 204);
}
/**
* Restore the specified resource.
*/
public function restore(int $id): JsonResponse
{
$supplier = Supplier::withTrashed()->findOrFail($id)->restore();
return response()->json($supplier);
}
}