2021-09-30 13:28:42 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Http\Request;
|
2021-11-20 11:49:20 +09:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2021-12-14 18:35:09 +09:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2021-10-15 10:37:47 +09:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-09-30 13:28:42 +09:00
|
|
|
|
2021-12-14 18:35:09 +09:00
|
|
|
use App\Models\Father;
|
2021-09-30 13:28:42 +09:00
|
|
|
use App\Models\Child;
|
|
|
|
use App\Models\Meeting;
|
|
|
|
use App\Models\MeetingApprovals;
|
2021-10-15 10:37:47 +09:00
|
|
|
use App\Models\FatherRelation;
|
2021-09-30 13:28:42 +09:00
|
|
|
|
2021-12-14 18:35:09 +09:00
|
|
|
use App\Mail\MeetingEditNotification;
|
|
|
|
use App\Mail\MeetingEditAwareness;
|
|
|
|
|
2021-09-30 13:28:42 +09:00
|
|
|
class MeetingApprovalsController extends Controller {
|
2021-11-11 10:02:28 +09:00
|
|
|
public function countNonApproval (Request $r) {
|
2021-11-13 15:33:48 +09:00
|
|
|
return MeetingApprovals::where('child_id', session()->get('children')['id'])->whereNull('approval_at')->count();
|
2021-11-10 22:38:28 +09:00
|
|
|
}
|
|
|
|
|
2021-11-11 10:02:28 +09:00
|
|
|
public function countIncomplete (Request $r) {
|
2021-11-13 15:33:48 +09:00
|
|
|
$count = 0;
|
|
|
|
|
|
|
|
if (null !== ($list = Meeting::select('id')->where('father_id', (int)session()->get('fathers')['id'])->get())) {
|
|
|
|
foreach ($list as $i => $l) {
|
2021-11-16 19:24:36 +09:00
|
|
|
if (null === ($apr = MeetingApprovals::select('id')->where('meeting_id', (int)$l->id)->get())) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-11-17 19:42:21 +09:00
|
|
|
|
|
|
|
$cnt = MeetingApprovals::select('id')->whereNotNull('approval_at')->where('meeting_id', (int)$l->id)->count();
|
|
|
|
$apr = count($apr);
|
2021-11-16 19:24:36 +09:00
|
|
|
|
2021-11-17 19:42:21 +09:00
|
|
|
if ($apr != 0 && $apr == $cnt) {
|
2021-11-16 19:24:36 +09:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$count++;
|
2021-11-13 15:33:48 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
2021-11-10 23:01:50 +09:00
|
|
|
}
|
|
|
|
|
2021-11-02 14:15:58 +09:00
|
|
|
public function register (Request $r) {
|
2021-11-03 18:25:32 +09:00
|
|
|
if (!isset($r->meeting_id) || !isset($r->children) || count(json_decode($r->children)) == 0) {
|
2021-10-31 22:26:12 +09:00
|
|
|
return ['status_code' => 400];
|
2021-10-29 10:56:15 +09:00
|
|
|
}
|
|
|
|
|
2021-11-08 17:31:46 +09:00
|
|
|
$create = ['meeting_id' => $r->meeting_id];
|
2021-10-29 10:56:15 +09:00
|
|
|
|
|
|
|
try {
|
2021-11-03 18:25:32 +09:00
|
|
|
foreach (json_decode($r->children) as $child) {
|
2021-12-14 18:35:09 +09:00
|
|
|
if (null === ($c = Child::where('id', (int)$child)->first())) {
|
|
|
|
return ['status_code' => 400];
|
|
|
|
}
|
|
|
|
|
2021-11-08 17:31:46 +09:00
|
|
|
$create['child_id'] = $child;
|
2021-11-08 17:45:56 +09:00
|
|
|
MeetingApprovals::create($create);
|
2021-12-14 18:35:09 +09:00
|
|
|
Mail::to($c->email)->send(new MeetingEditNotification(session()->get('fathers')['company'], $r->meeting_id));
|
2021-10-29 10:56:15 +09:00
|
|
|
}
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
Log::critical($e->getMessage());
|
|
|
|
return ['status_code' => 400];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 承知登録に成功
|
|
|
|
return ['status_code' => 200];
|
|
|
|
}
|
|
|
|
|
2021-11-02 14:15:58 +09:00
|
|
|
public function delete (Request $r) {
|
|
|
|
if (!isset($r->meeting_id) || !isset($r->children) || count($r->children) == 0) {
|
2021-10-31 22:26:12 +09:00
|
|
|
return ['status_code' => 400];
|
2021-10-29 10:56:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-11-20 11:49:20 +09:00
|
|
|
DB::beginTransaction();
|
|
|
|
|
2021-11-02 14:15:58 +09:00
|
|
|
foreach ($r->children as $k => $v) {
|
2021-11-20 11:49:20 +09:00
|
|
|
$meap = MeetingApprovals::where('child_id', (int)$v)->where('meeting_id', (int)$r->meeting_id);
|
|
|
|
$meap->delete();
|
2021-11-02 14:15:58 +09:00
|
|
|
}
|
2021-11-20 11:49:20 +09:00
|
|
|
|
|
|
|
DB::commit();
|
2021-10-29 10:56:15 +09:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
Log::critical($e->getMessage());
|
2021-11-20 11:49:20 +09:00
|
|
|
DB::rollback();
|
2021-10-29 10:56:15 +09:00
|
|
|
return ['status_code' => 400];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ['status_code' => 200];
|
|
|
|
}
|
|
|
|
|
2021-10-06 14:50:13 +09:00
|
|
|
public function registerApproval (Request $r) {
|
|
|
|
if (!isset($r->meeting_id) || !isset($r->child_id)) {
|
2021-10-31 22:26:12 +09:00
|
|
|
return ['status_code' => 400, 'error_messages' => ['承認に失敗しました。']];
|
2021-10-06 14:50:13 +09:00
|
|
|
}
|
|
|
|
|
2021-11-05 23:57:27 +09:00
|
|
|
if (null === (MeetingApprovals::where('meeting_id', (int)$r->meeting_id)->where('child_id', (int)$r->child_id)->first())) {
|
2021-10-29 10:56:15 +09:00
|
|
|
return ['status_code' => 400, 'error_messages' => ['承認に失敗しました。']];
|
2021-10-06 14:50:13 +09:00
|
|
|
}
|
|
|
|
|
2021-12-14 18:35:09 +09:00
|
|
|
if (null === ($meet = Meeting::where('id', (int)$r->meeting_id)->first())) {
|
|
|
|
return ['status_code' => 400, 'error_messages' => ['承認に失敗しました。']];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === ($father = Father::where('id', (int)$meet->father_id)->first())) {
|
|
|
|
return ['status_code' => 400, 'error_messages' => ['承認に失敗しました。']];
|
|
|
|
}
|
|
|
|
|
2021-11-08 17:45:56 +09:00
|
|
|
// $update = ['approval_at' => null];
|
|
|
|
$update = ['approval_at' => date('Y-m-d H:i:s')];
|
2021-11-03 20:46:48 +09:00
|
|
|
|
|
|
|
try {
|
2021-11-08 17:31:46 +09:00
|
|
|
MeetingApprovals::where('meeting_id', (int)$r->meeting_id)->where('child_id', (int)$r->child_id)->update($update);
|
2021-12-14 18:35:09 +09:00
|
|
|
Mail::to($father->email)->send(new MeetingEditAwareness(session()->get('children')['last_name'], session()->get('children')['first_name'], $r->meeting_id));
|
2021-11-03 20:46:48 +09:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// 失敗
|
|
|
|
Log::critical($e->getMessage());
|
|
|
|
return ['status_code' => 400, 'error_messages' => ['承認に失敗しました。']];
|
|
|
|
}
|
|
|
|
|
2021-10-29 10:56:15 +09:00
|
|
|
return ['status_code' => 200, 'success_messages' => ['承認しました。']];
|
2021-10-06 14:50:13 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
public function listChildrenOfMeeting (Request $r) {
|
|
|
|
if (!isset($r->meeting_id) || !isset($r->child_id)) {
|
2021-10-31 22:26:12 +09:00
|
|
|
return ['status_code' => 400, 'error_messages' => ['承認に失敗しました。']];
|
2021-10-06 14:50:13 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
$meeting_approvals_select = ['id', 'child_id', 'approval_at'];
|
|
|
|
$update = ['hire_at' => date('Y-m-d H:i:s', strtotime($r->hire_at))];
|
|
|
|
|
2021-10-31 00:50:35 +09:00
|
|
|
if (null === ($params = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$r->meeting_id)->where('child_id', (int)$r->child_id)->get())) {
|
2021-10-29 10:56:15 +09:00
|
|
|
|
2021-10-31 22:26:12 +09:00
|
|
|
return ['status_code' => 400, 'error_messages' => ['承認に失敗しました。']];
|
2021-10-29 10:56:15 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-10-31 00:50:35 +09:00
|
|
|
FatherRelation::where('child_id', (int)$r->child_id)->update($update);
|
2021-10-29 10:56:15 +09:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// 失敗
|
|
|
|
Log::critical($e->getMessage());
|
|
|
|
return ['status_code' => 400, 'error_messages' => ['登録に失敗しました。']];
|
2021-10-06 14:50:13 +09:00
|
|
|
}
|
|
|
|
|
2021-10-31 22:26:12 +09:00
|
|
|
return ['status_code' => 200, 'success_messages' => ['承認しました。']];
|
2021-10-06 14:50:13 +09:00
|
|
|
}
|
2021-09-30 13:28:42 +09:00
|
|
|
|
2021-10-06 14:50:13 +09:00
|
|
|
public function listChildrenOfApprovel (Request $r) {
|
|
|
|
if (!isset($r->meeting_id)) {
|
|
|
|
return ['status_code' => 400];
|
|
|
|
}
|
2021-09-30 13:28:42 +09:00
|
|
|
|
2021-10-06 14:50:13 +09:00
|
|
|
$meeting_approvals_select = ['id', 'child_id', 'approval_at'];
|
2021-12-15 16:22:19 +09:00
|
|
|
$child_select = ['id', 'image', 'last_name', 'first_name', 'tel', 'email'];
|
2021-09-30 13:28:42 +09:00
|
|
|
|
2021-10-31 00:50:35 +09:00
|
|
|
if (null === ($params = MeetingApprovals::select($meeting_approvals_select)->where('meeting_id', (int)$r->meeting_id)->whereNotNull('approval_at')->get())) {
|
2021-10-07 13:51:53 +09:00
|
|
|
// エラーの場合
|
2021-10-31 22:26:12 +09:00
|
|
|
return ['status_code' => 400];
|
2021-10-07 13:51:53 +09:00
|
|
|
}
|
2021-09-30 13:28:42 +09:00
|
|
|
|
2021-10-07 13:51:53 +09:00
|
|
|
foreach ($params as $p) {
|
2021-11-15 12:42:00 +09:00
|
|
|
if (null === ($p->child = Child::select($child_select)->where('id', (int)$p->child_id)->first())) {
|
2021-10-31 22:26:12 +09:00
|
|
|
return ['status_code' => 400];
|
2021-10-07 13:51:53 +09:00
|
|
|
}
|
2021-09-30 13:28:42 +09:00
|
|
|
}
|
|
|
|
|
2021-10-31 22:26:12 +09:00
|
|
|
return ['status_code' => 200, 'params' => $params];
|
2021-09-30 13:28:42 +09:00
|
|
|
}
|
|
|
|
|
2021-10-06 14:50:13 +09:00
|
|
|
public function listChildrenOfUnapprovel (Request $r) {
|
|
|
|
if (!isset($r->meeting_id)) {
|
|
|
|
return ['status_code' => 400];
|
|
|
|
}
|
|
|
|
|
|
|
|
$meeting_select = ['id', 'child_id', 'approval_at'];
|
2021-12-15 16:22:19 +09:00
|
|
|
$child_select = ['id', 'image', 'last_name', 'first_name', 'tel', 'email'];
|
2021-09-30 13:28:42 +09:00
|
|
|
|
2021-10-31 00:50:35 +09:00
|
|
|
if (null === ($params = MeetingApprovals::select($meeting_select)->where('meeting_id', (int)$r->meeting_id)->whereNull('approval_at')->get())) {
|
2021-10-07 13:51:53 +09:00
|
|
|
// エラーの場合
|
2021-10-31 22:26:12 +09:00
|
|
|
return ['status_code' => 400];
|
2021-10-07 13:51:53 +09:00
|
|
|
}
|
2021-09-30 13:28:42 +09:00
|
|
|
|
2021-10-07 13:51:53 +09:00
|
|
|
foreach ($params as $p) {
|
2021-11-15 12:42:00 +09:00
|
|
|
if (null === ($p->child = Child::select($child_select)->where('id', (int)$p->child_id)->first())) {
|
2021-10-31 22:26:12 +09:00
|
|
|
return ['status_code' => 400];
|
2021-10-07 13:51:53 +09:00
|
|
|
}
|
2021-09-30 13:28:42 +09:00
|
|
|
}
|
|
|
|
|
2021-10-31 22:26:12 +09:00
|
|
|
return ['status_code' => 200, 'params' => $params];
|
2021-09-30 13:28:42 +09:00
|
|
|
}
|
|
|
|
|
2021-10-06 14:50:13 +09:00
|
|
|
public function deleteRelationMeeting ($meeting_id) {
|
|
|
|
if (!isset($meeting_id)) {
|
|
|
|
return ['status_code' => 400];
|
|
|
|
}
|
2021-09-30 13:28:42 +09:00
|
|
|
|
2021-10-07 13:51:53 +09:00
|
|
|
try {
|
2021-10-31 00:50:35 +09:00
|
|
|
MeetingApprovals::where('meeting_id', (int)$meeting_id)->delete();
|
2021-10-07 13:51:53 +09:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
Log::critical($e->getMessage());
|
|
|
|
return ['status_code' => 400];
|
2021-10-06 14:50:13 +09:00
|
|
|
}
|
2021-09-30 13:28:42 +09:00
|
|
|
|
2021-10-07 13:51:53 +09:00
|
|
|
return ['status_code' => 200];
|
2021-10-06 14:50:13 +09:00
|
|
|
}
|
2021-09-30 13:28:42 +09:00
|
|
|
|
2021-10-06 14:50:13 +09:00
|
|
|
public function deleteRelationChild ($child_id) {
|
|
|
|
if (!isset($child_id)) {
|
|
|
|
return ['status_code' => 400];
|
|
|
|
}
|
2021-09-30 13:28:42 +09:00
|
|
|
|
2021-10-07 13:51:53 +09:00
|
|
|
try {
|
2021-10-31 00:50:35 +09:00
|
|
|
MeetingApprovals::where('child_id', (int)$child_id)->delete();
|
2021-10-07 13:51:53 +09:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
Log::critical($e->getMessage());
|
|
|
|
return ['status_code' => 400];
|
2021-10-06 14:50:13 +09:00
|
|
|
}
|
|
|
|
|
2021-10-07 13:51:53 +09:00
|
|
|
return ['status_code' => 200];
|
2021-10-06 14:50:13 +09:00
|
|
|
}
|
2021-09-30 13:28:42 +09:00
|
|
|
}
|