100 lines
2.7 KiB
PHP
100 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\V3;
|
|
|
|
use App\Exports\V3\Damage\DamageCartableReport;
|
|
use App\Facades\DataTable\DataTableFacade;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\V3\Damage\StoreRequest;
|
|
use App\Http\Requests\V3\Damage\UpdateRequest;
|
|
use App\Http\Traits\ApiResponse;
|
|
use App\Models\Damage;
|
|
use App\Services\Cartables\Damage\DamageService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Maatwebsite\Excel\Excel;
|
|
use PhpOffice\PhpSpreadsheet\Exception;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
class DamageManagementController extends Controller
|
|
{
|
|
use ApiResponse;
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request ,DamageService $damageService): JsonResponse
|
|
{
|
|
$data = $damageService->dataTable($request);
|
|
return response()->json($data);
|
|
}
|
|
|
|
public function list(): JsonResponse
|
|
{
|
|
$damages = Damage::query()->where('status', '=', 1)->get();
|
|
return $this->successResponse($damages);
|
|
}
|
|
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(StoreRequest $request): JsonResponse
|
|
{
|
|
Damage::query()->create([
|
|
'title' => $request->title,
|
|
'unit' => $request->unit,
|
|
'base_price' => $request->base_price,
|
|
'status' => 1,
|
|
'update_time' => now()
|
|
]);
|
|
|
|
return $this->successResponse();
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Damage $damage): JsonResponse
|
|
|
|
{
|
|
return $this->successResponse($damage);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
|
|
public function update(UpdateRequest $request, Damage $damage): JsonResponse
|
|
{
|
|
$damage->update([
|
|
'title' => $request->title,
|
|
'unit' => $request->unit,
|
|
'base_price' => $request->base_price,
|
|
'update_time' => now()
|
|
]);
|
|
|
|
return $this->successResponse();
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function activate(Damage $damage): JsonResponse
|
|
{
|
|
$damage->status == 1 ? $damage->update(['status' => 0]) : $damage->update(['status' => 1]);
|
|
|
|
return $this->successResponse();
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
|
|
*/
|
|
public function excel(Request $request, DamageService $damageService): BinaryFileResponse
|
|
{
|
|
$name = 'آیتم خسارات وارده بر ابنیه فنی' . verta()->now()->format('Y-m-d H-i') . '.xlsx';
|
|
$data = $damageService->datatable($request);
|
|
return Excel::download(new DamageCartableReport($data['data']), $name);
|
|
}
|
|
}
|