このリポジトリは2023-09-09にアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュ、イシューの作成、プルリクエストはできません。
kikikan/backend/app/Http/Controllers/Api/FatherRelationsController.php

167 行
6.4 KiB
PHP
Raw 通常表示 履歴

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
2021-11-17 02:37:02 +09:00
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
2021-10-15 10:37:47 +09:00
use Illuminate\Support\Facades\Log;
2021-11-17 02:45:26 +09:00
use App\Models\Child;
2021-12-13 22:34:41 +09:00
use App\Models\Father;
use App\Models\FatherRelation;
use App\Models\Meeting;
use App\Models\MeetingApprovals;
2022-01-31 13:21:41 +09:00
use App\Models\TelActivation;
class FatherRelationsController extends Controller {
2022-01-08 17:41:35 +09:00
public function checkNull (Request $r) {
2022-01-08 17:19:23 +09:00
$father_id = request()->route()->action['as'] == 'chknull_parent' ? (int)session()->get('fathers')['id'] : (int)$r->father_id;
if (FatherRelation::select('id')->where('father_id', $father_id)->count() == 0) {
return ['status_code' => 401, 'error_messages' => ['契約上限数に達した為、メンバー追加できません。']];
2022-01-08 17:27:33 +09:00
}
2022-01-08 16:36:34 +09:00
return ['status_code' => 200];
}
2022-01-08 17:27:33 +09:00
2022-01-08 17:41:35 +09:00
public function check (Request $r) {
2022-01-08 17:48:31 +09:00
$father_id = request()->route()->action['as'] == 'chk_parent' ? (int)session()->get('fathers')['id'] : (int)$r->father_id;
2022-01-08 17:19:23 +09:00
$father = Father::select('relation_limit')->where('id', $father_id)->first();
2022-01-08 16:36:34 +09:00
2022-01-31 13:21:41 +09:00
if ($father->relation_limit <= FatherRelation::select('id')->where('father_id', $father_id)->count() + TelActivation::select('id')->where('father_id', $father_id)->count()) {
2022-01-31 11:57:25 +09:00
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(), [
2022-04-28 15:59:13 +09:00
'identity' => 'required|max:20|numeric',
2021-11-17 01:57:46 +09:00
]);
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()) {
2022-01-31 11:57:25 +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 {
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())) {
$rel = new FatherRelation;
$rel->fill($create);
$rel->push();
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,
];
$apr = new MeetingApprovals;
$apr->fill($addapprove);
$apr->push();
}
}
}
DB::commit();
2021-11-17 13:09:21 +09:00
}
else {
DB::rollback();
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());
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-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-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-10-06 14:50:13 +09:00
return ['status_code' => 200, 'success_messages' => ['子の入社日の更新に成功しました。']];
}
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 {
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());
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' => ['子の削除に成功しました。']];
}
}