2021-09-30 13:26:29 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2021-11-17 02:37:02 +09:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2021-09-30 13:26:29 +09:00
|
|
|
use Illuminate\Http\Request;
|
2021-12-12 15:23:46 +09:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-10-15 10:37:47 +09:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-09-30 13:26:29 +09:00
|
|
|
|
2021-11-17 02:45:26 +09:00
|
|
|
use App\Models\Child;
|
2021-12-13 22:34:41 +09:00
|
|
|
use App\Models\Father;
|
2021-10-01 12:44:45 +09:00
|
|
|
use App\Models\FatherRelation;
|
2021-11-30 14:24:35 +09:00
|
|
|
use App\Models\Meeting;
|
|
|
|
use App\Models\MeetingApprovals;
|
2021-10-01 12:44:45 +09:00
|
|
|
|
2021-09-30 13:26:29 +09:00
|
|
|
class FatherRelationsController extends Controller {
|
2021-12-18 15:54:18 +09:00
|
|
|
public function check () {
|
|
|
|
if (FatherRelation::select('id')->where('father_id', (int)session()->get('fathers')['id'])->count() == 0) {
|
|
|
|
return ['status_code' => 401, 'error_messages' => ['契約上限数に達した為、メンバー追加できません。']];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ['status_code' => 200];
|
|
|
|
}
|
|
|
|
|
2021-11-17 01:50:51 +09:00
|
|
|
public function register (Request $r) {
|
2021-11-17 02:18:36 +09:00
|
|
|
if (!isset($r->father_id)) {
|
2021-11-04 13:28:58 +09:00
|
|
|
return ['status_code' => 400, 'error_messages' => ['子の追加に失敗しました。']];
|
|
|
|
}
|
|
|
|
|
2021-11-17 01:57:46 +09:00
|
|
|
$validate = Validator::make($r->all(), [
|
|
|
|
'identity' => 'required|max:20|alpha_num',
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validate->fails()) {
|
|
|
|
return ['status_code' => 422, 'error_messages' => $validate->errors()];
|
|
|
|
}
|
|
|
|
|
2021-11-04 13:28:58 +09:00
|
|
|
if (null === ($child = Child::select('id')->where('identity', $r->identity)->first())) {
|
|
|
|
return ['status_code' => 400, 'error_messages' => ['子の追加に失敗しました。']];
|
|
|
|
}
|
|
|
|
|
2021-12-13 22:34:41 +09:00
|
|
|
if (null === ($father = Father::select('relation_limit')->where('id', (int)$r->father_id)->first())) {
|
|
|
|
return ['status_code' => 400, 'error_messages' => ['子の追加に失敗しました。']];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($father->relation_limit <= FatherRelation::select('id')->where('father_id', (int)$r->father_id)->count()) {
|
2021-12-17 14:07:43 +09:00
|
|
|
return ['status_code' => 401, 'error_messages' => ['契約上限数に達した為、メンバー追加できません。']];
|
2021-12-13 22:34:41 +09:00
|
|
|
}
|
|
|
|
|
2021-11-04 13:28:58 +09:00
|
|
|
$create = [
|
|
|
|
'father_id' => $r->father_id,
|
|
|
|
'child_id' => $child->id,
|
|
|
|
'hire_at' => date('Y-m-d H:i:s', time()),
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
2021-12-14 16:47:05 +09:00
|
|
|
DB::beginTransaction();
|
2021-11-17 19:46:40 +09:00
|
|
|
if (null === ($fr = FatherRelation::where('child_id', $child->id)->where('father_id', (int)$r->father_id)->first())) {
|
2021-12-14 16:47:05 +09:00
|
|
|
$rel = new FatherRelation;
|
|
|
|
$rel->fill($create);
|
|
|
|
$rel->push();
|
|
|
|
|
2021-11-30 14:24:35 +09:00
|
|
|
if (null !== ($meet = Meeting::select('id', 'is_favorite')->where('father_id', (int)$r->father_id)->get())) {
|
|
|
|
foreach ($meet as $m) {
|
|
|
|
if ($m->is_favorite) {
|
|
|
|
$addapprove = [
|
|
|
|
'meeting_id' => $m->id,
|
|
|
|
'child_id' => $child->id,
|
|
|
|
'approval_at' => null,
|
|
|
|
];
|
|
|
|
|
2021-12-14 16:47:05 +09:00
|
|
|
$apr = new MeetingApprovals;
|
|
|
|
$apr->fill($addapprove);
|
|
|
|
$apr->push();
|
2021-11-30 14:24:35 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-14 16:47:05 +09:00
|
|
|
|
|
|
|
DB::commit();
|
2021-11-17 13:09:21 +09:00
|
|
|
}
|
|
|
|
else {
|
2021-12-14 16:47:05 +09:00
|
|
|
DB::rollback();
|
2021-11-17 14:01:08 +09:00
|
|
|
return ['status_code' => 400, 'error_messages' => ['すでに追加されました']];
|
2021-11-17 13:09:21 +09:00
|
|
|
}
|
2021-11-04 13:28:58 +09:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// 失敗
|
|
|
|
Log::critical($e->getMessage());
|
2021-12-14 16:47:05 +09:00
|
|
|
DB::rollback();
|
2021-11-04 13:28:58 +09:00
|
|
|
return ['status_code' => 400, 'error_messages' => ['子の追加に失敗しました。']];
|
|
|
|
}
|
|
|
|
|
2021-12-13 22:34:41 +09:00
|
|
|
return ['status_code' => 200, 'success_messages' => ['子の追加に成功しました。'], 'params' => ['child_id' => $child->id, 'limit' => $father->relation_limit]];
|
2021-11-04 13:28:58 +09:00
|
|
|
}
|
|
|
|
|
2021-10-01 14:18:52 +09:00
|
|
|
public function updateHireDate (Request $r, $child_id) {
|
2021-11-17 02:29:26 +09:00
|
|
|
if (!isset($child_id) || !isset($r->father_id)) {
|
2021-11-17 01:20:01 +09:00
|
|
|
return ['status_code' => 400, 'error_messages' => ['子の入社日の更新に失敗しました。']];
|
2021-10-01 14:18:52 +09:00
|
|
|
}
|
2021-10-01 12:44:45 +09:00
|
|
|
|
2021-11-17 01:57:46 +09:00
|
|
|
$validate = Validator::make($r->all(), [
|
|
|
|
'hire_at' => 'date',
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validate->fails()) {
|
|
|
|
return ['status_code' => 422, 'error_messages' => $validate->errors()];
|
|
|
|
}
|
|
|
|
|
2021-11-04 13:28:58 +09:00
|
|
|
$update = [
|
|
|
|
'hire_at' => date('Y-m-d H:i:s', strtotime($r->hire_at)),
|
|
|
|
];
|
2021-10-01 12:44:45 +09:00
|
|
|
|
2021-10-06 14:50:13 +09:00
|
|
|
try {
|
2021-11-17 13:51:32 +09:00
|
|
|
FatherRelation::where('father_id', (int)$r->father_id)->where('child_id', (int)$child_id)->update($update);
|
2021-10-06 14:50:13 +09:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// 失敗
|
|
|
|
Log::critical($e->getMessage());
|
2021-11-17 01:20:01 +09:00
|
|
|
return ['status_code' => 400, 'error_messages' => ['子の入社日の更新に失敗しました。']];
|
2021-10-01 14:18:52 +09:00
|
|
|
}
|
2021-09-30 13:26:29 +09:00
|
|
|
|
2021-10-06 14:50:13 +09:00
|
|
|
return ['status_code' => 200, 'success_messages' => ['子の入社日の更新に成功しました。']];
|
2021-09-30 13:26:29 +09:00
|
|
|
}
|
2021-11-04 13:28:58 +09:00
|
|
|
|
|
|
|
public function deleteRelationChild ($child_id) {
|
|
|
|
if (!isset($child_id)) {
|
2021-11-17 01:20:01 +09:00
|
|
|
return ['status_code' => 400, 'error_messages' => ['子の削除に失敗しました。']];
|
2021-11-04 13:28:58 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-12-12 15:23:46 +09:00
|
|
|
DB::beginTransaction();
|
|
|
|
|
|
|
|
$rel = FatherRelation::where('father_id', session()->get('fathers')['id'])->where('child_id', (int)$child_id);
|
|
|
|
|
|
|
|
foreach (Meeting::where('father_id', session()->get('fathers')['id'])->get() as $m) {
|
|
|
|
$apr = MeetingApprovals::where('child_id', (int)$child_id)->where('meeting_id', $m->id);
|
|
|
|
$apr->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
$rel->delete();
|
|
|
|
|
|
|
|
DB::commit();
|
2021-11-04 13:28:58 +09:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// 失敗
|
|
|
|
Log::critical($e->getMessage());
|
2021-12-12 15:23:46 +09:00
|
|
|
DB::rollback();
|
2021-11-17 01:20:01 +09:00
|
|
|
return ['status_code' => 400, 'error_messages' => ['子の削除に失敗しました。']];
|
2021-11-04 13:28:58 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return ['status_code' => 200, 'success_messages' => ['子の削除に成功しました。']];
|
|
|
|
}
|
2021-09-30 13:26:29 +09:00
|
|
|
}
|