197 lines
5.9 KiB
PHP
197 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\ClientCase;
|
|
use App\Models\Contract;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
|
|
class ClientCaseContoller extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(ClientCase $clientCase, Request $request)
|
|
{
|
|
return Inertia::render('Cases/Index', [
|
|
'client_cases' => $clientCase::with(['person'])
|
|
->when($request->input('search'), fn($que, $search) =>
|
|
$que->whereHas(
|
|
'person',
|
|
fn($q) => $q->where('full_name', 'ilike', '%' . $search . '%')
|
|
)
|
|
)
|
|
->where('active', 1)
|
|
->orderByDesc('created_at')
|
|
->paginate(15, ['*'], 'client-cases-page')
|
|
->withQueryString(),
|
|
'filters' => $request->only(['search'])
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
//
|
|
$cuuid = $request->input('client_uuid');
|
|
|
|
$client = \App\Models\Client::where('uuid', $cuuid)->firstOrFail();
|
|
|
|
if( isset($client->id) ){
|
|
|
|
\DB::transaction(function() use ($request, $client){
|
|
$pq = $request->input('person');
|
|
|
|
$person = $client->person()->create([
|
|
'nu' => rand(100000,200000),
|
|
'first_name' => $pq['first_name'],
|
|
'last_name' => $pq['last_name'],
|
|
'full_name' => $pq['full_name'],
|
|
'gender' => null,
|
|
'birthday' => null,
|
|
'tax_number' => $pq['tax_number'],
|
|
'social_security_number' => $pq['social_security_number'],
|
|
'description' => $pq['description'],
|
|
'group_id' => 2,
|
|
'type_id' => 1
|
|
]);
|
|
|
|
$person->addresses()->create([
|
|
'address' => $pq['address']['address'],
|
|
'country' => $pq['address']['country'],
|
|
'type_id' => $pq['address']['type_id']
|
|
]);
|
|
|
|
$person->phones()->create([
|
|
'nu' => $pq['phone']['nu'],
|
|
'country_code' => $pq['phone']['country_code'],
|
|
'type_id' => $pq['phone']['type_id']
|
|
]);
|
|
|
|
$person->clientCase()->create([
|
|
'client_id' => $client->id
|
|
]);
|
|
});
|
|
}
|
|
|
|
return to_route('client.show', $client);
|
|
}
|
|
|
|
public function storeContract(ClientCase $clientCase, Request $request)
|
|
{
|
|
|
|
\DB::transaction(function() use ($request, $clientCase){
|
|
|
|
//Create contract
|
|
$clientCase->contracts()->create([
|
|
'reference' => $request->input('reference'),
|
|
'start_date' => date('Y-m-d', strtotime($request->input('start_date'))),
|
|
'type_id' => $request->input('type_id')
|
|
]);
|
|
|
|
});
|
|
|
|
return to_route('clientCase.show', $clientCase);
|
|
}
|
|
|
|
public function updateContract(ClientCase $clientCase, String $uuid, Request $request)
|
|
{
|
|
$contract = Contract::where('uuid', $uuid)->firstOrFail();
|
|
|
|
\DB::transaction(function() use ($request, $contract){
|
|
$contract->update([
|
|
'reference' => $request->input('reference'),
|
|
'type_id' => $request->input('type_id')
|
|
]);
|
|
|
|
});
|
|
|
|
return to_route('clientCase.show', $clientCase);
|
|
}
|
|
|
|
public function storeActivity(ClientCase $clientCase, Request $request) {
|
|
|
|
$attributes = $request->validate([
|
|
'due_date' => 'nullable|date',
|
|
'amount' => 'nullable|decimal:0,4',
|
|
'note' => 'string',
|
|
'action_id' => 'exists:\App\Models\Action,id',
|
|
'decision_id' => 'exists:\App\Models\Decision,id'
|
|
]);
|
|
|
|
//Create activity
|
|
$clientCase->activities()->create($attributes);
|
|
|
|
return to_route('clientCase.show', $clientCase);
|
|
|
|
}
|
|
|
|
public function deleteContract(ClientCase $clientCase, String $uuid, Request $request) {
|
|
$contract = Contract::where('uuid', $uuid)->firstOrFail();
|
|
|
|
\DB::transaction(function() use ($request, $contract){
|
|
$contract->delete();
|
|
});
|
|
|
|
return to_route('clientCase.show', $clientCase);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(ClientCase $clientCase)
|
|
{
|
|
$case = $clientCase::with([
|
|
'person' => fn($que) => $que->with(['addresses', 'phones'])
|
|
])->where('active', 1)->findOrFail($clientCase->id);
|
|
|
|
return Inertia::render('Cases/Show', [
|
|
'client' => $case->client()->with('person', fn($q) => $q->with(['addresses', 'phones']))->firstOrFail(),
|
|
'client_case' => $case,
|
|
'contracts' => $case->contracts()
|
|
->with(['type'])
|
|
->orderByDesc('created_at')->get(),
|
|
'activities' => $case->activities()->with(['action', 'decision'])
|
|
->orderByDesc('created_at')
|
|
->paginate(20, ['*'], 'activities'),
|
|
'contract_types' => \App\Models\ContractType::whereNull('deleted_at')->get(),
|
|
'actions' => \App\Models\Action::with('decisions')->get()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
//
|
|
}
|
|
}
|