149 lines
5.2 KiB
PHP
149 lines
5.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\DepartaimantCollectionType;
|
|
use App\Enums\DepartaimantStatus;
|
|
use App\Models\DepartaimantsCollection;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class BourseController extends Controller
|
|
{
|
|
private $maps = [
|
|
0 => DepartaimantVintage::class,
|
|
1 => DepartaimantCurrent::class
|
|
];
|
|
|
|
|
|
public function bourse(Request $request)
|
|
{
|
|
/** @var \App\Models\Customers */
|
|
$customer = Auth::guard('customer')->user();
|
|
$departaimants = $customer->departaimantsCollections;
|
|
|
|
$departaimants = json_encode($this->getUserDepartaimantsMaps($departaimants));
|
|
|
|
return view('front.club.bourse', compact('departaimants'));
|
|
}
|
|
|
|
public function bourseSaveState(Request $request, DepartaimantCollectionType $type)
|
|
{
|
|
/** @var \App\Models\Customers */
|
|
$user = Auth::guard('customer')->user();
|
|
$departaimants = $user->departaimantsCollections()->where('type', $type->value)->first();
|
|
|
|
/* TODO : validate */
|
|
$id = $request->id;
|
|
$state = $request->state;
|
|
$action = $request->action;
|
|
|
|
if ($action === 'add') {
|
|
switch ($state) {
|
|
case 'own':
|
|
$departaimants->set(forward_static_call([$this->maps[$type->value], 'make'], $id), DepartaimantStatus::has());
|
|
break;
|
|
case 'change':
|
|
$departaimants->set(forward_static_call([$this->maps[$type->value], 'make'], $id), DepartaimantStatus::gives());
|
|
break;
|
|
case 'search':
|
|
$departaimants->set(forward_static_call([$this->maps[$type->value], 'make'], $id), DepartaimantStatus::wants());
|
|
break;
|
|
}
|
|
} else {
|
|
switch ($state) {
|
|
case 'own':
|
|
$departaimants->set(forward_static_call([$this->maps[$type->value], 'make'], $id), DepartaimantStatus::wants());
|
|
break;
|
|
case 'change':
|
|
case 'search':
|
|
$departaimants->set(forward_static_call([$this->maps[$type->value], 'make'], $id), DepartaimantStatus::has());
|
|
break;
|
|
}
|
|
}
|
|
|
|
$departaimants->saveOrFail();
|
|
|
|
return response()->json([
|
|
'status' => 'OK',
|
|
'map' => $this->getUserDepartaimantsMaps($user->departaimantsCollections)
|
|
]);
|
|
}
|
|
|
|
public function bourseSearch(Request $request, DepartaimantCollectionType $type)
|
|
{
|
|
$state = $request->state;
|
|
$pseudo = empty($request->pseudo) ? null : $request->pseudo;
|
|
|
|
if ($state === 'change') {
|
|
$state = DepartaimantStatus::gives();
|
|
$status = DepartaimantStatus::wants();
|
|
} elseif ($state === 'search') {
|
|
$state = DepartaimantStatus::wants();
|
|
$status = DepartaimantStatus::gives();
|
|
} else {
|
|
return response()->json(['status' => 'KO']);
|
|
}
|
|
|
|
/** @var \App\Models\Customers */
|
|
$customer = Auth::guard('customer')->user();
|
|
$userDep = $customer->departaimantsCollections()->where('type', $type->value)->first();
|
|
$userDepStatus = [];
|
|
foreach ($userDep->getAllDepartaimants() as $dep) {
|
|
if ($dep->status->value === $state->value) {
|
|
$userDepStatus[] = $dep->departaimant;
|
|
}
|
|
}
|
|
|
|
$req = DepartaimantsCollection::getDepartaimantsCollectionWithStatus($userDepStatus, $status, $type);
|
|
if ($pseudo !== null) {
|
|
$req->with('customer')
|
|
->whereHas('customer', function ($query) use ($pseudo) {
|
|
$query->where('username', 'LIKE', "%$pseudo%");
|
|
});
|
|
}
|
|
$req->orderBy('updated_at', 'DESC');
|
|
|
|
var_dump($req->toSql()); //OK
|
|
exit;
|
|
|
|
|
|
$req = DepartaimantsCollection::getCustomersWithStatus($this->maps[$type->value]::essonne(), $status);
|
|
|
|
if ($pseudo !== null) {
|
|
$req->where(['username', 'LIKE', "%$pseudo%"]);
|
|
}
|
|
|
|
echo $req->toSql();
|
|
exit;
|
|
$customers = $req->get();
|
|
|
|
$oCustomers = $customers->map(function ($customer) use ($type) {
|
|
return [
|
|
'username' => $customer->username,
|
|
'hashcode' => $customer->hashcode,
|
|
'date' => $customer->departaimantsCollections()->where('type', $type->value)
|
|
->first()->updated_at->format('d/m/Y')
|
|
];
|
|
});
|
|
|
|
return response()->json(['status' => 'OK', 'customers' => $oCustomers]);
|
|
}
|
|
|
|
private function getUserDepartaimantsMaps($departaimantsCollection)
|
|
{
|
|
$datas = $departaimantsCollection->mapWithKeys(function ($collection) {
|
|
$datas = [];
|
|
foreach ($collection->getAllDepartaimants() as $departaimantItem) {
|
|
$datas[$collection->type->value][] = [
|
|
'id' => $departaimantItem->departaimant->value,
|
|
'name' => $departaimantItem->departaimant->label,
|
|
'state' => $departaimantItem->status->value,
|
|
];
|
|
}
|
|
return $datas;
|
|
})->toArray();
|
|
|
|
return $datas;
|
|
}
|
|
}
|