From 1f3eb8576a5273a715d98352c4a1b7b5183c3e70 Mon Sep 17 00:00:00 2001 From: wahyuun Date: Thu, 14 Mar 2024 15:39:23 +0700 Subject: [PATCH 1/5] update delete relative company --- app/Http/Controllers/CompanyController.php | 39 ++++++++++++++-------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index 3a0cecb..b828469 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -2,9 +2,9 @@ namespace App\Http\Controllers; -use App\Models\Company; -use App\Models\MenuCompany; +use App\Models\{User, RoleMenu, Role, ProductTransaction, MenuCompany, Company}; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; class CompanyController extends Controller { @@ -66,8 +66,6 @@ class CompanyController extends Controller return response()->json(['status' => 'failed', 'message' => 'data Company not found!', 'code' => 400], 400); die(); } - - if ($result) { return response()->json(['status' => 'success', 'message' => 'data Company successfully updated!', 'code' => 200], 200); } else { @@ -77,24 +75,37 @@ class CompanyController extends Controller public function delete($id) { - $data = Company::find($id); - + DB::beginTransaction(); + $data = Company::find((int)$id); if ($data) { - MenuCompany::where('company_id',$id)->delete(); - $delete = $data->delete(); + $this->deleteRelative((int)$data['id']); + if ($data->delete()) { + DB::commit(); + return response()->json(['status' => 'success', 'message' => 'Data Company successfully deleted!', 'code' => 200], 200); + } else { + DB::rollBack(); + return response()->json(['status' => 'failed', 'message' => 'Data Company failed deleted!', 'code' => 400], 400); + } } else { - return response()->json(['status' => 'failed', 'message' => 'data Company not found!', 'code' => 400], 400); + DB::rollBack(); + return response()->json(['status' => 'failed', 'message' => 'Data Company not found!', 'code' => 400], 400); die(); } + } - - if ($delete) { - return response()->json(['status' => 'success', 'message' => 'data Company successfully deleted!', 'code' => 200], 200); - } else { - return response()->json(['status' => 'failed', 'message' => 'data Company failed deleted!', 'code' => 400], 400); + private function deleteRelative($company_id) + { + MenuCompany::where('company_id', $company_id)->delete(); + User::where('company_id', $company_id)->delete(); + $role = Role::where('company_id', $company_id)->first(); + if ($role) { + RoleMenu::where('role_id', $role->id)->delete(); + $role->delete(); } + ProductTransaction::where('company_id', $company_id)->delete(); } + public function search(Request $request) { $payload = $request->all(); From 921bfad15d7c7eb4fe82bc67a7c12c886352840b Mon Sep 17 00:00:00 2001 From: wahyuun Date: Thu, 14 Mar 2024 15:39:57 +0700 Subject: [PATCH 2/5] update register --- .../Controllers/UserRegisterController.php | 177 +++++++++++++----- 1 file changed, 125 insertions(+), 52 deletions(-) diff --git a/app/Http/Controllers/UserRegisterController.php b/app/Http/Controllers/UserRegisterController.php index 009a729..1a31eaf 100644 --- a/app/Http/Controllers/UserRegisterController.php +++ b/app/Http/Controllers/UserRegisterController.php @@ -5,38 +5,49 @@ namespace App\Http\Controllers; use Carbon\Carbon; use Illuminate\Support\Str; use Illuminate\Http\Request; -use Illuminate\Support\Facades\{DB,Validator}; -use App\Models\{Company, HumanResource, Menu, ProductTransaction, RefferalCode, Role, RoleMenu, MenuCompany}; +use Illuminate\Support\Facades\{DB,Log}; +use App\Models\{User, Company, HumanResource, Menu, ProductTransaction, RefferalCode, Role, RoleMenu, MenuCompany}; +const URL_EMAIL = 'https://notifapp.odm-iu.com/service-mail/notif_mail.php'; class UserRegisterController extends Controller { public function add(Request $request) { - DB::transaction(function () use ($request) { - $validator = Validator::make($request->all(), [ - 'refferal' => 'unique:refferal_code,code', - 'cluster' => 'required|string', - 'address' => 'required|string', - 'email' => 'required|unique:' . ($request->type_account === 'Company' ? 'm_company' : 'm_users') . ',email', - 'type_paket' => 'required|in:Basic, Free', - 'phone_no' => 'required', - 'type_account' => 'string|in:Personal, Company', - 'company_name' => 'required|string', - 'username' => 'string|required|unique:' . ($request->type_account === 'Personal' ? 'm_users' : '') . ',username' - ]); - if ($validator->fails()) { - return response()->json(['status' => 'failed', 'message' => $validator->errors()->first(), 'code' => 400], 400); - } - $data = $validator->validated(); + DB::beginTransaction(); + $this->validate($request, [ + 'cluster' => 'required|string', + 'address' => 'required|string', + 'email' => 'required|string', + 'type_paket' => ($request->type_account === 'Personal' ? 'required' : 'nullable') . '|in:Basic,Free', + 'phone_no' => 'required', + 'type_account' => 'string|in:Personal,Company', + 'company_name' => 'required|string', + 'username' => 'required|string' + ]); + try { + $data = $request->all(); + $company_name = $data['company_name']; $type_account = $data['type_account']; - $refferal = $data['refferal']; + $refferal = $request->refferal ? $data['refferal'] : null; $cluster = $data['cluster']; $address = $data['address']; $phone_no = $data['phone_no']; $email = $data['email']; $username = $data['username']; - $type_paket = $data['type_paket']; + $type_paket = $request->type_account === 'Personal' ? $data['type_paket'] : ''; + $company_address = $request->type_account === 'Company' ? $data['companyAddress'] : ''; + $full_name = $request->type_account === 'Personal' ? $data['full_name'] : ''; + + // check email & username already exist + $emailExists = Company::where('email', $email)->exists() || User::where('email', $email)->exists(); + if ($emailExists) { + return response()->json(['status' => 'failed', 'message' => 'Email already exists, please check again!', 'code' => 500], 500); + } + $usernameExists = User::where('username', $username)->exists(); + if ($usernameExists) { + return response()->json(['status' => 'failed', 'message' => 'Username already exists, please check again!', 'code' => 500], 500); + } // Get last registration number $getCompany = $this->getCompany(); @@ -50,7 +61,9 @@ class UserRegisterController extends Controller 'address' => $address, 'type_account' => $type_account, 'username' => $username, - 'type_paket' => $type_paket + 'type_paket' => $type_paket, + 'companyAddress' => $company_address, + 'full_name' => $full_name ); if(empty($refferal)) { @@ -58,47 +71,48 @@ class UserRegisterController extends Controller } else { $getRefferal = $this->getRefferalCode($refferal); if(empty($getRefferal)) { - return response()->json(['status' => 'failed', 'message' => 'Refferal Code Not Found!', 'code' => 404], 404); + return response()->json(['status' => 'failed', 'message' => 'Refferal code not found!', 'code' => 404], 404); } - $addCompany = $this->addCompany($formDataCompany, $getRefferal['id']); + $addCompany = $this->addCompany($formDataCompany, (int)$getRefferal['id']); } if(empty($addCompany)) { return; - die(); } $addRole = $this->addRole((int)$addCompany['id']); if(empty($addRole)) { return; - die(); } $addHR = $this->addHR((int)$addCompany['id'], (int)$addRole['id'], $formDataCompany); if(empty($addHR)) { return; - die(); } $getMenu = $this->getMenu(); if(empty($getMenu)) { return; - die(); } - $this->addTransaction((int)$addCompany['id'], $formDataCompany); + if($request->type_account === 'Personal') { + $this->addTransaction((int)$addCompany['id'], $formDataCompany); + } $addMenuCompany = $this->addMenuCompany($getMenu, (int)$addCompany['id']); if(empty($addMenuCompany)) { return; - die(); } $addMenuRole = $this->addMenuRole($getMenu, (int)$addRole['id']); - - if($addMenuRole) { - return response()->json(['status' => 'success', 'message' => 'Register is successfully!', 'code' => 200], 200); + if(empty($addMenuRole)) { + return; } - - },3); + DB::commit(); + return response()->json(['status' => 'success', 'message' => 'Register is successful, please check your email!', 'generateRandom' => $addHR['generateRandom'], 'code' => 200], 200); + } catch (\Throwable $th) { + DB::rollBack(); + Log::channel('daily')->error($th->getMessage()); + return response()->json(['status' => 'failed', 'message' => 'Failed to register, please check again!', 'code' => 500], 500); + } } protected function getRefferalCode($refferal) @@ -119,11 +133,11 @@ class UserRegisterController extends Controller 'date_register' => Carbon::now(), 'template_id' => 1, 'email' => $formData['email'], - 'address' => $formData['address'], + 'address' => $formData['type_account'] === 'Company' ? $formData['companyAddress'] : $formData['address'], 'phone_no' => $formData['phone_no'], 'type_account' => $formData['type_account'], 'is_active' => true, - 'discount_id' => $id_refferal === null ? null : (int)$id_refferal + 'discount_id' => $id_refferal === null ? null : $id_refferal ); $result = Company::create($formData); return $result; @@ -142,25 +156,27 @@ class UserRegisterController extends Controller protected function addHR($id_company, $id_role, $data) { - $generatePassword = Str::password(10); + $generateRandom = Str::random(8); $formData = array( - 'name'=> $data['company_name'], + 'name'=> $data['type_account'] === 'Company' ? $data['username'] : $data['full_name'], 'phone_number'=> $data['phone_no'], 'email'=> $data['email'], 'username' => $data['username'], - 'password'=> $generatePassword, - 'role_id'=> (int)$id_role, - 'ktp_number'=> $data['type_account'] === 'Company' ? 'CP-' : 'PR-' . Str::random(5), - 'employee_type'=>'employee', + 'password'=> md5($generateRandom), + 'role_id'=> $id_role, + 'ktp_number'=> $data['type_account'] === 'Company' ? 'CP-'. $generateRandom : 'PR-' . $generateRandom, + 'employee_type'=> 'employee', 'address' => $data['address'], 'status_resource'=> 'active', - 'company_id'=> (int)$id_company + 'company_id'=> $id_company ); $result = HumanResource::create($formData); - + if(!empty($result)) { + $this->sendEmail($data['email'], $generateRandom); + } return [ 'result' => $result, - 'generatePassword' => $generatePassword + 'generateRandom' => $generateRandom ]; } @@ -170,7 +186,7 @@ class UserRegisterController extends Controller if($data->exists()){ $data->delete(); } - if (is_array($baseDataMenu) && count($baseDataMenu) > 0 && isset($baseDataMenu)) { + if (is_object($baseDataMenu) && count($baseDataMenu) > 0 && isset($baseDataMenu)) { $countRes = 0; foreach ($baseDataMenu as $menu) { $dataInsert = array( @@ -193,16 +209,16 @@ class UserRegisterController extends Controller if ($countRes > 0) { return $result; } else { - return $result; + die(); } } else { - return false; + die(); } } protected function addMenuRole($baseDataMenu, $id_role) { - if (is_array($baseDataMenu) && count($baseDataMenu) > 0 && isset($baseDataMenu)) { + if (is_object($baseDataMenu) && count($baseDataMenu) > 0 && isset($baseDataMenu)) { $countRes = 0; foreach ($baseDataMenu as $menu) { $dataInsert = array( @@ -219,10 +235,10 @@ class UserRegisterController extends Controller if ($countRes > 0) { return $result; } else { - return $result; + die(); } } else { - return false; + die(); } } @@ -263,11 +279,68 @@ class UserRegisterController extends Controller $lastRegistrationNumber = $company['registration_no']; $lastNumber = (int)preg_replace('/\D/', '', $lastRegistrationNumber); $newNumber = $lastNumber + 1; - // $newRegistrationNumber = Str::beforeLast($lastRegistrationNumber, $lastNumber) . $newNumber; $newRegistrationNumber = 'RG-'. $newNumber; + } else{ + return false; } return [ 'newRegistrationNumber' => $newRegistrationNumber ]; } + + public function sendEmail($email, $password) + { + $user = User::select('email', 'name', 'username')->where('email', $email)->first(); + if (!$user) { + return response()->json(['status' => 'error', 'message' => 'e-mail not found '], 400); + } else { + $this->reqHttpCurl($email, $password, $user->username, $user->name); + return response()->json(['status' => 'error', 'code'=>400, 'message' => 'e-mail not found '], 400); + } + } + + private function reqHttpCurl($email, $password, $username, $name) + { + $postData = [ + "to" => $email, + "name" => $name, + "username" => $username, + "password" => $password, + "from" => "app.integrasia@integrasiautama.com", + "alias_from" => "OSPRO", + "subject" => "Registration OSPRO", + "body" => "registration-ospro" + ]; + + $curl = curl_init(); + + curl_setopt_array($curl, array( + CURLOPT_URL => URL_EMAIL, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30000, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "POST", + CURLOPT_POSTFIELDS => json_encode($postData), + CURLOPT_HTTPHEADER => array( + // Set here requred headers + "accept: */*", + "accept-language: en-US,en;q=0.8", + "content-type: application/json", + ), + )); + + $response = curl_exec($curl); + $err = curl_error($curl); + + curl_close($curl); + + if ($err) { + echo "cURL Error #:" . $err; + } else { + print_r(json_decode($response)); + } + + } } From 6cf4b3aab3118ebc78ce634c736c8489d08fa87f Mon Sep 17 00:00:00 2001 From: wahyuun Date: Thu, 14 Mar 2024 15:40:16 +0700 Subject: [PATCH 3/5] add DB transaction --- app/Http/Controllers/ProjectController.php | 1041 ++++++++++---------- 1 file changed, 530 insertions(+), 511 deletions(-) diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 0f4e54b..bbad373 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -3,46 +3,16 @@ namespace App\Http\Controllers; use DateTime; -use App\Models\Link; -use App\Models\User; -use App\Models\Image; -use App\Models\Company; -use App\Models\Holiday; -use App\Models\Project; -use App\Models\Activity; -use App\Models\ReportK3; use App\Jobs\ProcessSCurve; -use App\Models\AssignTools; -use App\Models\OfficeHours; -use App\Models\ProjectType; -use App\Models\ProjectPhase; -use App\Models\ProjectRisks; -use App\Models\UserToProyek; -use App\Models\VersionGantt; use Illuminate\Http\Request; -use App\Models\HierarchyFtth; -use App\Models\ProjectIssues; -use App\Models\AssignMaterial; -use App\Models\DokumenProject; -use App\Models\ProjectCharter; -use App\Models\ReportActivity; -use App\Models\ShowHideColumn; -use App\Models\UserToActivity; -use App\Models\ActivityDokumen; -use App\Models\CommentActivity; -use App\Models\ProjectApproval; -use App\Models\RequestMaterial; -use App\Models\ProjectMileStone; -use App\Models\ProjectChecklists; -use App\Models\ProductTransaction; -use Illuminate\Support\Facades\DB; -use App\Models\ProjectParticipants; -use App\Models\FolderDocumentProyek; -use App\Models\ProjectToChecklistK3; -use App\Helpers\MasterFunctionsHelper; -use App\Models\ReportActivityMaterial; +use App\Models\{ReportActivityMaterial,ProjectToChecklistK3,FolderDocumentProyek,ProjectParticipants,ProductTransaction,ProjectChecklists, + ProjectMileStone,RequestMaterial,ProjectApproval,CommentActivity,ActivityDokumen,UserToActivity,ShowHideColumn,ReportActivity,ProjectCharter,DokumenProject, + AssignMaterial,ProjectIssues,HierarchyFtth,VersionGantt,UserToProyek,ProjectRisks,ProjectPhase,ProjectType,OfficeHours,AssignTools,ReportK3,Activity,Project, + Holiday,Company,Image,User,Link +}; use Illuminate\Database\Query\Builder; -use Illuminate\Support\Facades\Artisan; +use Illuminate\Support\Facades\{Artisan,DB}; +use App\Helpers\MasterFunctionsHelper; const API_GEOLOCATION = "https://nominatim.oslogdev.com/search/ADDR?format=json&addressdetails=1&limit=1"; @@ -60,56 +30,59 @@ class ProjectController extends Controller 'type_proyek_id' => 'required', 'company_id' => 'required' ]); - - $data = $request->all(); - - $data['created_by'] = $this->currentName; - $data['created_by_id'] = $this->currentId; - - if (empty($data['phase_id'])) { - $data['phase_id'] = 1; - } - - $transaction = ProductTransaction::query() - ->where('company_id', $request->company_id); - $cloneQueryTransaction = clone $transaction; - - $countCreate = false; - $projectResult = Project::query() - ->selectRaw('count(*) as total_project') - ->where('company_id', $request->company_id) - ->first(); - if($transaction->where([['type_paket','Basic'],['amount','!=',null]])->exists()) { - if($projectResult['total_project'] < 10) { - $countCreate = true; + DB::beginTransaction(); + try { + $data = $request->all(); + $data['created_by'] = $this->currentName; + $data['created_by_id'] = $this->currentId; + + if (empty($data['phase_id'])) { + $data['phase_id'] = 1; } - } elseif ($cloneQueryTransaction->where([['type_paket','Free'],['amount',0]])->exists()) { - if($projectResult['total_project'] < 1) { + $transaction = ProductTransaction::query() + ->where('company_id', $request->company_id); + $cloneQueryTransaction = clone $transaction; + + $countCreate = false; + $projectResult = Project::query() + ->selectRaw('count(*) as total_project') + ->where('company_id', $request->company_id) + ->first(); + if($transaction->where([['type_paket','Basic'],['amount','!=',null]])->exists()) { + if($projectResult['total_project'] < 10) { + $countCreate = true; + } + } elseif ($cloneQueryTransaction->where([['type_paket','Free'],['amount',0]])->exists()) { + if($projectResult['total_project'] < 1) { + $countCreate = true; + } + } else { $countCreate = true; } - } else { - $countCreate = true; - } - if($countCreate) { - $result = Project::create($data); - } else { - return response()->json(['status' => 'failed', 'message' => 'Limited to create project!', 'code' => 500], 500); + if($countCreate) { + $result = Project::create($data); + if (!$result) { + return response()->json(['status' => 'failed', 'message' => 'Failed to add data!', 'code' => 500], 500); + } else { + $this->createOfficeHours($result['id']); + $dataResultInsert = Project::where([['nama', $data['nama']],['mulai_proyek', $data['mulai_proyek']]])->first(); + DB::commit(); + return response()->json(['status' => 'success', 'message' => 'Data added!', 'data_result' => $dataResultInsert, 'code' => 200], 200); + } + } else { + return response()->json(['status' => 'failed', 'message' => 'Limited to create project!', 'code' => 500], 500); + } + } catch (\Throwable $th) { + DB::rollBack(); } - - if (!$result) - return response()->json(['status' => 'failed', 'message' => 'Failed to add data!', 'code' => 500], 500); - - $this->createOfficeHours($result->id); - $dataResultInsert = Project::where('nama', $data['nama'])->where('mulai_proyek', $data['mulai_proyek'])->first(); - - return response()->json(['status' => 'success', 'message' => 'Data added!', 'data_result' => $dataResultInsert, 'code' => 200], 200); } public function edit($id) { - if (empty($id) || !is_int((int)$id)) + if (empty($id) || !is_int((int)$id)) { return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); + } // $result = Project::query() // ->from('m_proyek AS mp') @@ -157,28 +130,30 @@ class ProjectController extends Controller ->join('m_divisi as md', 'md.id', '=', 'm_proyek.divisi_id') ->where('m_proyek.id', $id) ->first(); - if (!$result) - return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); - - return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200); + if (!$result) { + return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); + } else { + return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200); + } } public function update(Request $request, $id) { - if (empty($id) || !is_int((int)$id)) - return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); + if (empty($id) || !is_int((int)$id)) { + return response()->json(['status' => 'failed', 'message' => 'Id is required!', 'code' => 400], 400); + } $data = Project::find($id); - if (!$data) - return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); - - $result = $data->update($request->all()); - - if (!$result) - return response()->json(['status' => 'failed', 'message' => 'Failed to update!', 'code' => 500], 500); - - return response()->json(['status' => 'success', 'message' => 'Data updated!', 'code' => 200], 200); + if ($data) { + if ($data->update($request->all())) { + return response()->json(['status' => 'success', 'message' => 'Data updated!', 'code' => 200], 200); + } else { + return response()->json(['status' => 'failed', 'message' => 'Failed to update!', 'code' => 500], 500); + } + } else { + return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); + } } private function createOfficeHours($proyek_id) @@ -209,101 +184,109 @@ class ProjectController extends Controller public function delete($id, $company_id) { + DB::beginTransaction(); $data = Project::find($id); - - if (!$data) + if ($data) { + $this->deleteRelative($id, $company_id); + if ($data->delete()) { + DB::commit(); + return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200); + } else { + DB::rollBack(); + return response()->json(['status' => 'failed', 'message' => 'Delete failed!', 'code' => 500], 500); + } + } else { + DB::rollBack(); return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); - - $this->deleteRelative($id, $company_id); - - if (!$data->delete()) - return response()->json(['status' => 'failed', 'message' => 'Delete failed!', 'code' => 500], 500); - - return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200); + } } private function deleteRelative($project_id, $company_id) { - UserToProyek::where('proyek_id', $project_id)->delete(); - UserToActivity::where('proyek_id', $project_id)->delete(); - AssignMaterial::where('proyek_id', $project_id)->delete(); - ProjectCharter::where('proyek_id', $project_id)->delete(); - ProjectApproval::where('proyek_id', $project_id)->delete(); - ProjectMileStone::where('proyek_id', $project_id)->delete(); - ProjectParticipants::where('proyek_id', $project_id)->delete(); - ProjectChecklists::where('proyek_id', $project_id)->delete(); - ProjectIssues::where('proyek_id', $project_id)->delete(); - ProjectRisks::where('proyek_id', $project_id)->delete(); - ProjectToChecklistK3::where('proyek_id', $project_id)->delete(); - HierarchyFtth::where('project_id', $project_id)->delete(); - AssignTools::where('proyek_id', $project_id)->delete(); - OfficeHours::where('proyek_id', $project_id)->delete(); - ReportK3::where('proyek_id', $project_id)->delete(); - RequestMaterial::where('proyek_id', $project_id)->delete(); - $this->deleteVersionGantt($project_id, $company_id); - $this->deleteDokumenProject($project_id, $company_id); + DB::transaction(function() use($project_id, $company_id) { + UserToProyek::where('proyek_id', $project_id)->delete(); + UserToActivity::where('proyek_id', $project_id)->delete(); + AssignMaterial::where('proyek_id', $project_id)->delete(); + ProjectCharter::where('proyek_id', $project_id)->delete(); + ProjectApproval::where('proyek_id', $project_id)->delete(); + ProjectMileStone::where('proyek_id', $project_id)->delete(); + ProjectParticipants::where('proyek_id', $project_id)->delete(); + ProjectChecklists::where('proyek_id', $project_id)->delete(); + ProjectIssues::where('proyek_id', $project_id)->delete(); + ProjectRisks::where('proyek_id', $project_id)->delete(); + ProjectToChecklistK3::where('proyek_id', $project_id)->delete(); + HierarchyFtth::where('project_id', $project_id)->delete(); + AssignTools::where('proyek_id', $project_id)->delete(); + OfficeHours::where('proyek_id', $project_id)->delete(); + ReportK3::where('proyek_id', $project_id)->delete(); + RequestMaterial::where('proyek_id', $project_id)->delete(); + $this->deleteVersionGantt($project_id, $company_id); + $this->deleteDokumenProject($project_id, $company_id); + },5); } private function deleteVersionGantt($project_id, $company_id) { - $dataVg = VersionGantt::where("proyek_id", $project_id)->pluck("id"); - $vhIds = $dataVg->all(); - $activity = Activity::whereIn("version_gantt_id", $vhIds)->pluck('id'); - $activityIds = $activity->all(); - $dataRa = ReportActivity::whereIn("activity_id", $activityIds)->get(); - $company = Company::find($company_id); - if($company) { - $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); - foreach ($dataRa as $ra) { - $images = Image::where("ref_id", $ra->id)->where("category", "report_activity")->get(); - foreach ($images as $image) { - if (file_exists($destinationPath['pathImage'] . $image->image)) { - unlink($destinationPath['pathImage'] . $image->image); + DB::transaction(function() use($project_id, $company_id) { + $dataVg = VersionGantt::where("proyek_id", $project_id)->pluck("id"); + $vhIds = $dataVg->all(); + $activity = Activity::whereIn("version_gantt_id", $vhIds)->pluck('id'); + $activityIds = $activity->all(); + $dataRa = ReportActivity::whereIn("activity_id", $activityIds)->get(); + $company = Company::find($company_id); + if($company) { + $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); + foreach ($dataRa as $ra) { + $images = Image::where("ref_id", $ra->id)->where("category", "report_activity")->get(); + foreach ($images as $image) { + if (file_exists($destinationPath['pathImage'] . $image->image)) { + unlink($destinationPath['pathImage'] . $image->image); + } } + Image::where("ref_id", $ra->id)->where("category", "report_activity")->delete(); } - Image::where("ref_id", $ra->id)->where("category", "report_activity")->delete(); - } - $dataAd = ActivityDokumen::whereIn("activity_id", $activityIds)->get(); - foreach ($dataAd as $ad) { - if (file_exists($destinationPath['pathActivityDocument'] . $ad->file)) { - unlink($destinationPath['pathActivityDocument'] . $ad->file); + $dataAd = ActivityDokumen::whereIn("activity_id", $activityIds)->get(); + foreach ($dataAd as $ad) { + if (file_exists($destinationPath['pathActivityDocument'] . $ad->file)) { + unlink($destinationPath['pathActivityDocument'] . $ad->file); + } } } - } - ActivityDokumen::whereIn("activity_id", $activityIds)->delete(); - CommentActivity::whereIn("activity_id", $activityIds)->delete(); - Holiday::where("proyek_id", $project_id)->delete(); - VersionGantt::where("proyek_id", $project_id)->delete(); - Link::whereIn("version_gantt_id", $vhIds)->delete(); - ShowHideColumn::whereIn("version_gantt_id", $vhIds)->delete(); - Activity::whereIn("version_gantt_id", $vhIds)->delete(); - ReportActivity::whereIn("activity_id", $activityIds)->delete(); + ActivityDokumen::whereIn("activity_id", $activityIds)->delete(); + CommentActivity::whereIn("activity_id", $activityIds)->delete(); + Holiday::where("proyek_id", $project_id)->delete(); + VersionGantt::where("proyek_id", $project_id)->delete(); + Link::whereIn("version_gantt_id", $vhIds)->delete(); + ShowHideColumn::whereIn("version_gantt_id", $vhIds)->delete(); + Activity::whereIn("version_gantt_id", $vhIds)->delete(); + ReportActivity::whereIn("activity_id", $activityIds)->delete(); + },5); } private function deleteDokumenProject($project_id, $company_id) { - $dataDokumen = DokumenProject::where("type_dokumen", "project-document-out-folder")->where('ref_id', $project_id)->get(); - $company = Company::find($company_id); - if($company) { - $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); + DB::transaction(function() use($project_id, $company_id){ + $dataDokumen = DokumenProject::where([['type_dokumen', 'project-document-out-folder'],['ref_id', $project_id]])->get(); + $company = Company::find($company_id); + if($company) { + $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); + foreach ($dataDokumen as $dokumen) { + if (file_exists($destinationPath['pathDocument'] . $dokumen->file)) { + unlink($destinationPath['pathDocument'] . $dokumen->file); + } + } + } + DokumenProject::where([['type_dokumen', 'project-document-out-folder'],['ref_id', $project_id]])->delete(); + $dataFolder = FolderDocumentProyek::where('proyek_id', $project_id)->pluck("id"); + $ref_ids = $dataFolder->all(); + $dataDokumen = DokumenProject::where("type_dokumen", "project-document-in-folder")->whereIn('ref_id', $ref_ids)->get(); foreach ($dataDokumen as $dokumen) { if (file_exists($destinationPath['pathDocument'] . $dokumen->file)) { unlink($destinationPath['pathDocument'] . $dokumen->file); } } - } - - DokumenProject::where("type_dokumen", "project-document-out-folder")->where('ref_id', $project_id)->delete(); - $dataFolder = FolderDocumentProyek::where('proyek_id', $project_id)->pluck("id"); - $ref_ids = $dataFolder->all(); - $dataDokumen = DokumenProject::where("type_dokumen", "project-document-in-folder")->whereIn('ref_id', $ref_ids)->get(); - foreach ($dataDokumen as $dokumen) { - if (file_exists($destinationPath['pathDocument'] . $dokumen->file)) { - unlink($destinationPath['pathDocument'] . $dokumen->file); - } - } - - DokumenProject::where("type_dokumen", "project-document-in-folder")->whereIn('ref_id', $ref_ids)->delete(); + DokumenProject::where("type_dokumen", "project-document-in-folder")->whereIn('ref_id', $ref_ids)->delete(); + },5); } public function search(Request $request) @@ -313,7 +296,6 @@ class ProjectController extends Controller $dataBuilder = $this->setUpPayload($payload, 'm_proyek'); $builder = $dataBuilder['builder']; $countBuilder = $dataBuilder['count']; - // $builder->where("selfTable.created_by",$this->currentName); $dataGet = $builder->get(); $totalRecord = $countBuilder->count(); @@ -322,94 +304,90 @@ class ProjectController extends Controller public function list() { - $data = Project::select( - 'id', - 'kode_sortname', - 'jumlah_stakeholder', - 'nama', - 'mulai_proyek', - 'akhir_proyek', - 'area_kerja', - 'lokasi_kantor', - 'rencana_biaya', - 'biaya_actual', - 'company', - 'pm_id', - 'type_proyek_id', - 'divisi_id', - 'persentase_progress', - 'keterangan', - 'durasi_proyek', - 'progress_by_worklog', - 'status', - 'currency_symbol', - 'currency_code', - 'currency_name', - 'project_objectives', - 'considered_success_when', - 'potential_risk', - 'testing_environment', - 'currency_code', - 'currency_symbol', - 'currency_name', - 'budget_health', - 'phase_id', - 'calculation_status', - 'created_at', - 'created_by', - 'updated_at', - 'updated_by' - )->orderBy('id', 'desc') - // ->where("created_by",$this->currentName) - ->get(); - $countData = $data->count(); - - if (!$data) - return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); - - $totalPlannedCost = array_sum(array_map('intval', array_column($data->toArray(), 'plannedCost'))); - $totalActualCost = $data->sum('actualCost'); - $manpowers = User::count(); - $projectsOnDanger = Project::where('budget_health', 'overrun')->count(); - $projectPhases = ProjectPhase::orderBy('order', 'asc')->pluck('name'); - $projectTypes = ProjectType::orderBy('id', 'asc')->pluck('name'); - try { - $projectsByPhase = DB::table('m_proyek') - ->select('m_proyek_phase.name', 'm_proyek_phase.color', DB::raw('count(*) as total')) - ->join('m_proyek_phase', 'm_proyek_phase.id', '=', 'm_proyek.phase_id') - ->groupBy('m_proyek_phase.name', 'm_proyek_phase.color') - ->get(); - } catch (\Exception $e) { - return response()->json(['message' => $e->getMessage()]); - } - try { - $projectsByType = DB::table('m_proyek') - ->select('m_type_proyek.name', DB::raw('count(m_type_proyek.id) as total')) - ->join('m_type_proyek', 'm_type_proyek.id', '=', 'm_proyek.type_proyek_id') - ->groupBy('m_type_proyek.name') - ->get(); - } catch (\Exception $e) { - return response()->json(['message' => $e->getMessage()]); - } + DB::beginTransaction(); + try { + $data = Project::select( + 'id', + 'kode_sortname', + 'jumlah_stakeholder', + 'nama', + 'mulai_proyek', + 'akhir_proyek', + 'area_kerja', + 'lokasi_kantor', + 'rencana_biaya', + 'biaya_actual', + 'company', + 'pm_id', + 'type_proyek_id', + 'divisi_id', + 'persentase_progress', + 'keterangan', + 'durasi_proyek', + 'progress_by_worklog', + 'status', + 'currency_symbol', + 'currency_code', + 'currency_name', + 'project_objectives', + 'considered_success_when', + 'potential_risk', + 'testing_environment', + 'currency_code', + 'currency_symbol', + 'currency_name', + 'budget_health', + 'phase_id', + 'calculation_status', + 'created_at', + 'created_by', + 'updated_at', + 'updated_by' + )->orderByDesc('id') + ->get(); + $countData = $data->count(); + if (!$data) { + return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); + } - return response()->json( - [ - 'status' => 'success', - 'code' => 200, - 'data' => $data, - 'totalRecord' => $countData, - 'totalPlannedCost' => $totalPlannedCost, - 'totalActualCost' => $totalActualCost, - 'totalRevenue' => $totalPlannedCost - $totalActualCost, - 'manpowers' => $manpowers, - 'projectsOnDanger' => $projectsOnDanger, - 'projectPhases' => $projectPhases, - 'projectsByPhase' => $projectsByPhase, - 'projectTypes' => $projectTypes, - 'projectsByType' => $projectsByType, - ], - 200 - ); + $totalPlannedCost = array_sum(array_map('intval', array_column($data->toArray(), 'plannedCost'))); + $totalActualCost = $data->sum('actualCost'); + $manpowers = User::count(); + $projectsOnDanger = Project::where('budget_health', 'overrun')->count(); + $projectPhases = ProjectPhase::orderBy('order', 'asc')->pluck('name'); + $projectTypes = ProjectType::orderBy('id', 'asc')->pluck('name'); + $projectsByPhase = DB::table('m_proyek') + ->select('m_proyek_phase.name', 'm_proyek_phase.color', DB::raw('count(*) as total')) + ->join('m_proyek_phase', 'm_proyek_phase.id', '=', 'm_proyek.phase_id') + ->groupBy('m_proyek_phase.name', 'm_proyek_phase.color') + ->get(); + $projectsByType = DB::table('m_proyek') + ->select('m_type_proyek.name', DB::raw('count(m_type_proyek.id) as total')) + ->join('m_type_proyek', 'm_type_proyek.id', '=', 'm_proyek.type_proyek_id') + ->groupBy('m_type_proyek.name') + ->get(); + DB::commit(); + return response()->json( + [ + 'status' => 'success', + 'code' => 200, + 'data' => $data, + 'totalRecord' => $countData, + 'totalPlannedCost' => $totalPlannedCost, + 'totalActualCost' => $totalActualCost, + 'totalRevenue' => $totalPlannedCost - $totalActualCost, + 'manpowers' => $manpowers, + 'projectsOnDanger' => $projectsOnDanger, + 'projectPhases' => $projectPhases, + 'projectsByPhase' => $projectsByPhase, + 'projectTypes' => $projectTypes, + 'projectsByType' => $projectsByType, + ], + 200 + ); + } catch (\Throwable $th) { + DB::rollBack(); + } } public function getListProjectTask($id) @@ -420,10 +398,12 @@ class ProjectController extends Controller ->groupBy("m_proyek.proyek_id") ->get(); - if (!$data) - return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); + if ($data) { + return response()->json(['status' => 'success', 'code' => 200, 'data' => $data], 200); + } else { + return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); + } - return response()->json(['status' => 'success', 'code' => 200, 'data' => $data], 200); } public function getSCurve(Request $request) @@ -469,83 +449,93 @@ class ProjectController extends Controller public static function setSyncDate($activity_id, $activity, $report) { - $status = AssignMaterial::where('activity_id', $activity_id)->first(); - if (!isset($status)) { - $reports = array( - 'activity_id' => $activity_id, - 'min_date' => new DateTime($activity->start_date), - 'max_date' => new DateTime($activity->end_date), - 'status' => 'open' - ); - return $reports; - } - if (!isset($status->status_activity)) { - $status->status_activity = 'open'; - } - if ($status->status_activity != 'done') { - $minDate = date_create($report->report_date); - $maxDate = date_create($report->report_date); - date_add($maxDate, date_interval_create_from_date_string($activity->duration . " days")); - } else { - $material = AssignMaterial::where('activity_id', $activity_id)->first(); - $minDate = date_create($material->start_activity); - $maxDate = date_create($material->finish_activity); - } - $reports = array( - 'activity_id' => $activity_id, - 'min_date' => $minDate, - 'max_date' => $maxDate, - 'status' => $status->status_activity - ); - return $reports; + DB::beginTransaction(); + try { + $status = AssignMaterial::where('activity_id', $activity_id)->first(); + if (!isset($status)) { + DB::commit(); + return [ + 'activity_id' => $activity_id, + 'min_date' => new DateTime($activity->start_date), + 'max_date' => new DateTime($activity->end_date), + 'status' => 'open' + ]; + } + if (!isset($status->status_activity)) { + $status->status_activity = 'open'; + } + if ($status->status_activity != 'done') { + $minDate = date_create($report->report_date); + $maxDate = date_create($report->report_date); + date_add($maxDate, date_interval_create_from_date_string($activity->duration . " days")); + } else { + $material = AssignMaterial::where('activity_id', $activity_id)->first(); + $minDate = date_create($material->start_activity); + $maxDate = date_create($material->finish_activity); + } + DB::commit(); + return [ + 'activity_id' => $activity_id, + 'min_date' => $minDate, + 'max_date' => $maxDate, + 'status' => $status->status_activity + ]; + } catch (\Throwable $th) { + DB::rollBack(); + } } public function synchronizeReport($gantt_id) { - $activities = Activity::where("version_gantt_id", $gantt_id)->get(); - $reports = []; - - foreach ($activities as $activity) { - $activity_id = $activity->id; - $countReports = ReportActivityMaterial::where('activity_id', $activity_id)->count(); - if ($countReports === 1) { - $dataReports = ReportActivityMaterial::where('activity_id', $activity_id)->orderBy('report_date')->get(); - foreach ($dataReports as $dr) { - $reports[] = ProjectController::setSyncDate($activity_id, $activity, $dr); - } - } - if ($countReports > 1) { - $firstReport = ReportActivityMaterial::where('activity_id', $activity_id)->orderBy('report_date')->first(); - $reports[] = ProjectController::setSyncDate($activity_id, $activity, $firstReport); - } - $successor = Link::where('t_activity_id', $activity->id)->first(); - if ($successor) { - $predecessor = Activity::find($successor->s_activity_id); - $activity->start_date = $predecessor->end_date; - $end_date = new DateTime($activity->start_date); - $end_date->modify("+" . $activity->duration . " days"); - $activity->end_date = $end_date->format("Y-m-d H:i:sO"); - } - $activity->save(); - } - for ($i = 0; $i < count($reports); $i++) { - $activity = Activity::find($reports[$i]['activity_id']); - if ($reports[$i]['status'] != 'done') { - $reports[$i]['max_date']->modify('-1 day'); - } else if ($reports[$i]['status'] == 'done') { - $activity->actual_end = $reports[$i]['max_date']->setTime(23, 59, 59); - } - $activity->start_date = $reports[$i]['min_date']; //same early - $activity->end_date = $reports[$i]['max_date']->setTime(23, 59, 59); // same early - $startDate = new DateTime($activity->start_date); - $endDate = new DateTime($activity->end_date); - $duration = MasterFunctionsHelper::countDays($activity->version_gantt_id, $startDate, $endDate); - $activity->duration = $duration; - $activity->actual_start = $reports[$i]['min_date']; - $activity->save(); - } - - return response()->json(['status' => 'success', 'message' => 'Synchronize to report success!', 'code' => 200], 200); + DB::beginTransaction(); + try { + $activities = Activity::where("version_gantt_id", $gantt_id)->get(); + $reports = []; + + foreach ($activities as $activity) { + $activity_id = $activity->id; + $countReports = ReportActivityMaterial::where('activity_id', $activity_id)->count(); + if ($countReports === 1) { + $dataReports = ReportActivityMaterial::where('activity_id', $activity_id)->orderBy('report_date')->get(); + foreach ($dataReports as $dr) { + $reports[] = self::setSyncDate($activity_id, $activity, $dr); + } + } + if ($countReports > 1) { + $firstReport = ReportActivityMaterial::where('activity_id', $activity_id)->orderBy('report_date')->first(); + $reports[] = self::setSyncDate($activity_id, $activity, $firstReport); + } + $successor = Link::where('t_activity_id', $activity->id)->first(); + if ($successor) { + $predecessor = Activity::find($successor->s_activity_id); + $activity->start_date = $predecessor->end_date; + $end_date = new DateTime($activity->start_date); + $end_date->modify("+" . $activity->duration . " days"); + $activity->end_date = $end_date->format("Y-m-d H:i:sO"); + } + $activity->save(); + } + for ($i = 0; $i < count($reports); $i++) { + $activity = Activity::find($reports[$i]['activity_id']); + if ($reports[$i]['status'] != 'done') { + $reports[$i]['max_date']->modify('-1 day'); + } else if ($reports[$i]['status'] == 'done') { + $activity->actual_end = $reports[$i]['max_date']->setTime(23, 59, 59); + } + $activity->start_date = $reports[$i]['min_date']; //same early + $activity->end_date = $reports[$i]['max_date']->setTime(23, 59, 59); // same early + $startDate = new DateTime($activity->start_date); + $endDate = new DateTime($activity->end_date); + $duration = MasterFunctionsHelper::countDays($activity->version_gantt_id, $startDate, $endDate); + $activity->duration = $duration; + $activity->actual_start = $reports[$i]['min_date']; + $activity->save(); + } + DB::commit(); + return response()->json(['status' => 'success', 'message' => 'Synchronize to report success!', 'code' => 200], 200); + } catch (\Throwable $th) { + DB::rollBack(); + } } public function setBaseline($gantt_id) @@ -566,10 +556,7 @@ class ProjectController extends Controller public function setBaselineActivity($activity_id, $gantt_id) { - $activity = Activity::where([ - ["version_gantt_id", $gantt_id], - ['id',$activity_id] - ])->first(); + $activity = Activity::where([["version_gantt_id", $gantt_id],['id',$activity_id]])->first(); $activity->update([ "planned_start" => $activity->start_date, "planned_end" => $activity->end_date, @@ -581,30 +568,32 @@ class ProjectController extends Controller public function getInvoiceIntegration(Request $request) { - $ganttCount = VersionGantt::where('proyek_id', $request->id)->count(); - $search = urlencode($request->search); - if (empty($search)) - return response()->json(['status' => 'error', 'message' => 'Empty query string!'], 400); - $url = str_replace("SEARCH", $search, config('api.adw') . '/project_cost?project_no=SEARCH'); - - $response = MasterFunctionsHelper::curlReq($url); - if (isset($request->gantt_id)) { - $response->data->total_cost = $response->data->total_cost / $ganttCount; - } + DB::beginTransaction(); + try { + $ganttCount = VersionGantt::where('proyek_id', $request->id)->count(); + $search = urlencode($request->search); + if (empty($search)) { + return response()->json(['status' => 'error', 'message' => 'Empty query string!'], 400); + } + $url = str_replace("SEARCH", $search, config('api.adw') . '/project_cost?project_no=SEARCH'); - return response()->json(['status' => 'success', 'data' => $response, 'code' => 200], 200); + $response = MasterFunctionsHelper::curlReq($url); + if (isset($request->gantt_id)) { + $response->data->total_cost = $response->data->total_cost / $ganttCount; + } + DB::commit(); + return response()->json(['status' => 'success', 'data' => $response, 'code' => 200], 200); + } catch (\Throwable $th) { + DB::rollBack(); + } } public function getByUser($id) { - $alias = "utp"; $userProyek = UserToProyek::query() - ->from('assign_hr_to_proyek AS ' . $alias) - ->where([ - ['is_customer', true], - ['user_id', $id] - ]) - ->leftJoin('m_users', $alias . '.user_id', '=', 'm_users.id') - ->leftJoin('m_proyek', $alias . '.proyek_id', '=', 'm_proyek.id') + ->from('assign_hr_to_proyek AS utp') + ->where([['is_customer', true],['user_id', $id]]) + ->leftJoin('m_users', 'utp.user_id', '=', 'm_users.id') + ->leftJoin('m_proyek', 'utp.proyek_id', '=', 'm_proyek.id') ->leftJoin('m_type_proyek', 'm_proyek.type_proyek_id', '=', 'm_type_proyek.id') ->select('m_proyek.*', 'm_type_proyek.name AS join_second_name', 'm_users.name AS join_first_name') ->get(); @@ -612,138 +601,160 @@ class ProjectController extends Controller return response()->json(['status' => 'success', 'code' => 200, 'data' => $userProyek, 'totalRecord' => $totalRecord], 200); } + public function detail(Request $request, $id, $gantt_id = null, $s_curve = null) { - if (empty($id) || !is_int((int)$id)) - return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); - - $result = Project::find($id); - - if (!$result) - return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); - if (!isset($gantt_id)) { - $gantt = MasterFunctionsHelper::getLatestGantt($id); - $ganttId = $gantt['last_gantt_id']; - } else { - $ganttId = $gantt_id; - } - $result->projectManager = User::where('id', $result->pm_id)->value('name'); - - if (isset($s_curve)) { - $result->header = Activity::whereNull('parent_id')->where("proyek_id", $id)->first(); - $actualStartExist = Activity::where('proyek_id', $id)->whereNotNull('actual_start')->exists(); - if ($result['type_proyek_id'] === 9) { - // $actualEndExist = Activity::where('proyek_id', $id)->exists(); - $query = Activity::where('proyek_id', $id); - $maxEndDate = Activity::where('proyek_id', $id)->select('end_date') - ->orderBy('end_date', 'desc') - ->first(); - } else { - // $actualEndExist = Activity::where('version_gantt_id', $ganttId)->exists(); - $maxEndDate = Activity::where('version_gantt_id', $ganttId)->select('end_date') - ->orderBy('end_date', 'desc') - ->first(); - $query = Activity::where('version_gantt_id', $ganttId); - } - } else { - $result->header = Activity::whereNull('parent_id')->where("proyek_id", $id)->where("version_gantt_id", $ganttId)->first(); - $actualStartExist = Activity::where('version_gantt_id', $ganttId)->whereNotNull('actual_start')->exists(); - // $actualEndExist = Activity::where('version_gantt_id', $ganttId)->exists(); - $maxEndDate = Activity::where('version_gantt_id', $ganttId)->select('end_date') - ->orderBy('end_date', 'desc') - ->first(); - $query = Activity::where('version_gantt_id', $ganttId); - } - - if ($actualStartExist) { - $startDate = $query->orderBy('actual_start')->value('start_date'); - } else { - $startDate = $query->orderBy('start_date')->value('start_date'); - } - // if($actualEndExist) - // { - // // $maxEndDate = $query->max('id'); - // // get last end_date - // // $endDate = $query->where('id',$maxEndDate)->first()->end_date; + DB::beginTransaction(); + try { + if (empty($id) || !is_int((int)$id)) { + return response()->json(['status' => 'failed', 'message' => 'Id is required!', 'code' => 400], 400); + } - // } + $result = Project::find($id); + if (!$result) { + return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); + } + if (!isset($gantt_id)) { + $gantt = MasterFunctionsHelper::getLatestGantt($id); + $ganttId = $gantt['last_gantt_id']; + } else { + $ganttId = $gantt_id; + } + $result->projectManager = User::where('id', $result->pm_id)->value('name'); + + if (isset($s_curve)) { + $result->header = Activity::whereNull('parent_id')->where("proyek_id", $id)->first(); + $actualStartExist = Activity::where('proyek_id', $id)->whereNotNull('actual_start')->exists(); + if ($result['type_proyek_id'] === 9) { + // $actualEndExist = Activity::where('proyek_id', $id)->exists(); + $query = Activity::where('proyek_id', $id); + $maxEndDate = Activity::where('proyek_id', $id)->select('end_date') + ->orderBy('end_date', 'desc') + ->first(); + } else { + // $actualEndExist = Activity::where('version_gantt_id', $ganttId)->exists(); + $maxEndDate = Activity::where('version_gantt_id', $ganttId)->select('end_date') + ->orderByDesc('end_date') + ->first(); + $query = Activity::where('version_gantt_id', $ganttId); + } + } else { + $result->header = Activity::whereNull('parent_id')->where("proyek_id", $id)->where("version_gantt_id", $ganttId)->first(); + $actualStartExist = Activity::where('version_gantt_id', $ganttId)->whereNotNull('actual_start')->exists(); + // $actualEndExist = Activity::where('version_gantt_id', $ganttId)->exists(); + $maxEndDate = Activity::where('version_gantt_id', $ganttId)->select('end_date') + ->orderByDesc('end_date') + ->first(); + $query = Activity::where('version_gantt_id', $ganttId); + } - $plannedStart = Activity::where('version_gantt_id', $ganttId) - ->orderBy('planned_start') - ->value('planned_start'); - $plannedEnd = Activity::where('version_gantt_id', $ganttId) - ->orderByDesc('planned_end') - ->value('planned_end'); - if (isset($result->header)) { - $result->header->start_date = $startDate; - $result->header->end_date = $maxEndDate->end_date; - $result->header->planned_start = $plannedStart; - $result->header->planned_end = $plannedEnd; - } - return response()->json(['status' => 'success', 'code' => 200, 'data' => $result, 'gantt' => $ganttId], 200); + if ($actualStartExist) { + $startDate = $query->orderBy('actual_start')->value('start_date'); + } else { + $startDate = $query->orderBy('start_date')->value('start_date'); + } + // if($actualEndExist) + // { + // // $maxEndDate = $query->max('id'); + // // get last end_date + // // $endDate = $query->where('id',$maxEndDate)->first()->end_date; + + // } + + $plannedStart = Activity::where('version_gantt_id', $ganttId) + ->orderBy('planned_start') + ->value('planned_start'); + $plannedEnd = Activity::where('version_gantt_id', $ganttId) + ->orderByDesc('planned_end') + ->value('planned_end'); + if (isset($result->header)) { + $result->header->start_date = $startDate; + $result->header->end_date = $maxEndDate->end_date; + $result->header->planned_start = $plannedStart; + $result->header->planned_end = $plannedEnd; + } + DB::commit(); + return response()->json(['status' => 'success', 'code' => 200, 'data' => $result, 'gantt' => $ganttId], 200); + } catch (\Throwable $th) { + DB::rollBack(); + } } public function getOverdueActivities(Request $request) { - $payload = $request->all(); - if (empty($payload['id']) || !is_int((int)$payload['id'])) - return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); + DB::beginTransaction(); + try { + $payload = $request->all(); + if (empty($payload['id']) || !is_int((int)$payload['id'])) { + return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); + } - $result = Project::find($payload['id']); - - if (!$result) - return response()->json(['status' => 'failed', 'message' => 'Project not found!', 'code' => 404], 404); - //TODO possible overdue bug - $endDate = Activity::where('proyek_id', $payload['id']) - ->orderByDesc('end_date') - ->value('end_date'); - if (isset($payload['till_date'])) { - if (isset($payload['scurve'])) { - $overdueActivities = Activity::join('m_version_gantt', 'm_activity.version_gantt_id', '=', 'm_version_gantt.id')->where('m_activity.proyek_id', $payload['id'])->whereNotNull('parent_id')->where('persentase_progress', '!=', 100)->whereDate('end_date', '<=', $endDate)->orderBy('end_date', 'asc')->get(); - } else { - $overdueActivities = Activity::join('m_version_gantt', 'm_activity.version_gantt_id', '=', 'm_version_gantt.id')->where('version_gantt_id', $payload['gantt'])->whereNotNull('parent_id')->where('persentase_progress', '!=', 100)->whereDate('end_date', '<=', $endDate)->orderBy('end_date', 'asc')->get(); - } - } else { - if (isset($payload['scurve'])) { - $overdueActivities = Activity::join('m_version_gantt', 'm_activity.version_gantt_id', '=', 'm_version_gantt.id')->where('m_activity.proyek_id', $payload['id'])->whereNotNull('parent_id')->where('persentase_progress', '!=', 100)->orderBy('end_date', 'asc')->get(); - } else { - $overdueActivities = Activity::join('m_version_gantt', 'm_activity.version_gantt_id', '=', 'm_version_gantt.id')->where('version_gantt_id', $payload['gantt'])->whereNotNull('parent_id')->where('persentase_progress', '!=', 100)->orderBy('end_date', 'asc')->get(); - } - } - $result->overdueActivities = $overdueActivities; + $result = Project::find($payload['id']); - return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200); + if (!$result) { + return response()->json(['status' => 'failed', 'message' => 'Project not found!', 'code' => 404], 404); + } + //TODO possible overdue bug + $endDate = Activity::where('proyek_id', $payload['id']) + ->orderByDesc('end_date') + ->value('end_date'); + if (isset($payload['till_date'])) { + if (isset($payload['scurve'])) { + $overdueActivities = Activity::join('m_version_gantt', 'm_activity.version_gantt_id', '=', 'm_version_gantt.id')->where('m_activity.proyek_id', $payload['id'])->whereNotNull('parent_id')->where('persentase_progress', '!=', 100)->whereDate('end_date', '<=', $endDate)->orderBy('end_date', 'asc')->get(); + } else { + $overdueActivities = Activity::join('m_version_gantt', 'm_activity.version_gantt_id', '=', 'm_version_gantt.id')->where('version_gantt_id', $payload['gantt'])->whereNotNull('parent_id')->where('persentase_progress', '!=', 100)->whereDate('end_date', '<=', $endDate)->orderBy('end_date', 'asc')->get(); + } + } else { + if (isset($payload['scurve'])) { + $overdueActivities = Activity::join('m_version_gantt', 'm_activity.version_gantt_id', '=', 'm_version_gantt.id')->where('m_activity.proyek_id', $payload['id'])->whereNotNull('parent_id')->where('persentase_progress', '!=', 100)->orderBy('end_date', 'asc')->get(); + } else { + $overdueActivities = Activity::join('m_version_gantt', 'm_activity.version_gantt_id', '=', 'm_version_gantt.id')->where('version_gantt_id', $payload['gantt'])->whereNotNull('parent_id')->where('persentase_progress', '!=', 100)->orderBy('end_date', 'asc')->get(); + } + } + $result->overdueActivities = $overdueActivities; + DB::commit(); + return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200); + } catch (\Throwable $th) { + DB::rollBack(); + } } public function getReportDistribution(Request $request) { - $payload = $request->all(); + DB::beginTransaction(); + try { + $payload = $request->all(); - if (empty($payload['project_id']) || !is_int((int)$payload['project_id'])) - return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); + if (empty($payload['project_id']) || !is_int((int)$payload['project_id'])) { + return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); + } - $reports = DB::table('assign_material_to_activity as ama') - ->select( - 'u.name', - 'a.name as activity_name', - 'ama.qty_planning', - 'ram.qty as qty_real', - 'rm.description as material_name', - 'rm.uom as material_unit', - 'ram.lat', - 'ram.lon', - 'ram.description as report_notes', - 'ram.report_date' - ) - ->join('report_activity_material as ram', 'ram.assign_material_id', '=', 'ama.id') - ->join('m_req_material as rm', 'rm.id', '=', 'ama.material_id') - ->join('m_activity as a', 'a.id', '=', 'ama.activity_id') - ->join('m_users as u', 'u.id', '=', 'ram.user_id') - ->where('ama.proyek_id', '=', $payload['project_id']) - ->whereNotNull('ram.lat') - ->whereBetween('ram.report_date', [$payload['start_date'], $payload['end_date']]); - - return response()->json(['status' => 'success', 'code' => 200, 'data' => $reports], 200); + $reports = AssignMaterial::query() + ->from('assign_material_to_activity AS ama') + ->select( + 'u.name', + 'a.name as activity_name', + 'ama.qty_planning', + 'ram.qty as qty_real', + 'rm.description as material_name', + 'rm.uom as material_unit', + 'ram.lat', + 'ram.lon', + 'ram.description as report_notes', + 'ram.report_date' + ) + ->join('report_activity_material as ram', 'ram.assign_material_id', '=', 'ama.id') + ->join('m_req_material as rm', 'rm.id', '=', 'ama.material_id') + ->join('m_activity as a', 'a.id', '=', 'ama.activity_id') + ->join('m_users as u', 'u.id', '=', 'ram.user_id') + ->where('ama.proyek_id', '=', $payload['project_id']) + ->whereNotNull('ram.lat') + ->whereBetween('ram.report_date', [$payload['start_date'], $payload['end_date']]); + DB::commit(); + return response()->json(['status' => 'success', 'code' => 200, 'data' => $reports], 200); + } catch (\Throwable $th) { + DB::rollBack(); + } } public function getManpower($proyek_id) @@ -763,51 +774,59 @@ class ProjectController extends Controller public function dashboard($id) { - $data = DB::table('m_proyek as mp') - ->select( - 'mp.kode_sortname', - 'mp.nama as name_project', - 'mp.mulai_proyek as start', - 'mp.akhir_proyek as finish', - 'mp.rencana_biaya', - 'mp.company', - 'mp.currency_symbol', - 'mu.name as pm', - 'mp.budget_health' - ) - ->join('m_users as mu', 'mu.id', '=', 'mp.pm_id') - ->where('mp.id', $id) - ->first(); - - if (!$data) - return response()->json(['status' => 'failed', 'message' => 'Data tidak ditemukan!', 'code' => 404], 404); - - $manpowers = UserToProyek::where('proyek_id', $id)->count(); - $rootActivity = Activity::whereNull('parent_id')->where('proyek_id', $id)->orderBy('version_gantt_id', 'desc')->first(); - - $actualCost = @$rootActivity->biaya_actual ?? 0; - $progress = @$rootActivity->persentase_progress ?? 0; - - $commentActivity = DB::table('m_comment_activity as mca') - ->select( - 'mca.activity_id', - 'mca.comment as comment', - 'mca.created_by as comment_by', - 'mca.created_at as comment_created', - 'ma.name as activity' - ) - ->join('m_activity as ma', 'ma.id', '=', 'mca.activity_id') - ->where('ma.proyek_id', $id) - ->orderBy('comment_by') - ->take(2) - ->get(); - - $data->actual_cost = $actualCost; - $data->progress = $progress; - $data->comment = $commentActivity; - $data->man_power = $manpowers; + DB::beginTransaction(); + try { + $data = Project::query() + ->from('m_proyek AS mp') + ->select( + 'mp.kode_sortname', + 'mp.nama as name_project', + 'mp.mulai_proyek as start', + 'mp.akhir_proyek as finish', + 'mp.rencana_biaya', + 'mp.company', + 'mp.currency_symbol', + 'mu.name as pm', + 'mp.budget_health' + ) + ->join('m_users as mu', 'mu.id', '=', 'mp.pm_id') + ->where('mp.id', $id) + ->first(); + + if (!$data) { + return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); + } - return response()->json(['status' => 'success', 'code' => 200, 'data' => $data], 200); + $manpowers = UserToProyek::where('proyek_id', $id)->count(); + $rootActivity = Activity::whereNull('parent_id')->where('proyek_id', $id)->orderByDesc('version_gantt_id')->first(); + + $actualCost = @$rootActivity->biaya_actual ?? 0; + $progress = @$rootActivity->persentase_progress ?? 0; + + $commentActivity = CommentActivity::query() + ->from('m_comment_activity AS mca') + ->select( + 'mca.activity_id', + 'mca.comment as comment', + 'mca.created_by as comment_by', + 'mca.created_at as comment_created', + 'ma.name as activity' + ) + ->join('m_activity as ma', 'ma.id', '=', 'mca.activity_id') + ->where('ma.proyek_id', $id) + ->orderBy('comment_by') + ->take(2) + ->get(); + + $data->actual_cost = $actualCost; + $data->progress = $progress; + $data->comment = $commentActivity; + $data->man_power = $manpowers; + DB::commit(); + return response()->json(['status' => 'success', 'code' => 200, 'data' => $data], 200); + } catch (\Throwable $th) { + DB::rollBack(); + } } private function httpReq($search) From f723348bed2649997fc86814302322074a253ddc Mon Sep 17 00:00:00 2001 From: wahyuun Date: Fri, 15 Mar 2024 13:02:34 +0700 Subject: [PATCH 4/5] update type paket in company register --- app/Http/Controllers/UserRegisterController.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/UserRegisterController.php b/app/Http/Controllers/UserRegisterController.php index 1a31eaf..8b61abb 100644 --- a/app/Http/Controllers/UserRegisterController.php +++ b/app/Http/Controllers/UserRegisterController.php @@ -35,7 +35,7 @@ class UserRegisterController extends Controller $phone_no = $data['phone_no']; $email = $data['email']; $username = $data['username']; - $type_paket = $request->type_account === 'Personal' ? $data['type_paket'] : ''; + $type_paket = $data['type_paket']; $company_address = $request->type_account === 'Company' ? $data['companyAddress'] : ''; $full_name = $request->type_account === 'Personal' ? $data['full_name'] : ''; @@ -94,9 +94,7 @@ class UserRegisterController extends Controller if(empty($getMenu)) { return; } - if($request->type_account === 'Personal') { - $this->addTransaction((int)$addCompany['id'], $formDataCompany); - } + $this->addTransaction((int)$addCompany['id'], $formDataCompany); $addMenuCompany = $this->addMenuCompany($getMenu, (int)$addCompany['id']); if(empty($addMenuCompany)) { From f0e79dbaf45692d84c1fb10e26996d0a7cf267a4 Mon Sep 17 00:00:00 2001 From: wahyuun Date: Fri, 15 Mar 2024 16:26:07 +0700 Subject: [PATCH 5/5] update directory company --- app/Http/Controllers/Controller.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index ce78741..255a081 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -45,10 +45,13 @@ class Controller extends BaseController protected function setCustomeDirectoryUpload($company_name) { - $pathImage = 'assets/' . $company_name . '/image/'; - $pathDocument = 'assets/' . $company_name . '/file/project/'; - $pathTmpImport = 'assets/' . $company_name . '/file/tmpimport/'; - $pathActivityDocument = 'assets/' . $company_name . '/file/activity/'; + $current_date = date('Y-m-d'); + [$year, $month, $day] = explode('-', $current_date); + + $pathImage = 'assets/' . $company_name . ' ' . $year . '-' . $month . '-' . $day . '/image/'; + $pathDocument = 'assets/' . $company_name . ' ' . $year . '-' . $month . '-' . $day . '/file/project/'; + $pathTmpImport = 'assets/' . $company_name . ' ' . $year . '-' . $month . '-' . $day . '/file/tmpimport/'; + $pathActivityDocument = 'assets/' . $company_name . ' ' . $year . '-' . $month . '-' . $day . '/file/activity/'; return [ 'pathImage' => $pathImage,