diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index 2571438..b247ed8 100755 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -631,46 +631,42 @@ class ActivityController extends Controller public function delete($id) { - DB::beginTransaction(); - $activity_id = (int)$id; - $data = Activity::query() - ->from('m_activity as ma') - ->select('mp.company_id') - ->join('m_proyek as mp','ma.proyek_id','mp.id') - ->where('ma.id', $activity_id); - $cloneQuery = clone $data; + DB::beginTransaction(); + $activity_id = (int)$id; + $data = Activity::query() + ->from('m_activity as ma') + ->select('ma.id') + ->join('m_proyek as mp', 'ma.proyek_id', 'mp.id') + ->where('ma.id', $activity_id); if (!$data->exists()) { - DB::rollBack(); + DB::rollBack(); return response()->json(['status' => 'failed', 'action' => 'error', 'message' => 'Data not found!', 'code' => 404], 404); - } + } - $this->deleteRelative($activity_id, $cloneQuery->first()->company_id); + $this->deleteRelative($activity_id); if (!$data->delete()) { - DB::rollBack(); + DB::rollBack(); return response()->json(['status' => 'failed', 'action' => 'error', 'message' => 'Data activity failed deleted!', 'code' => 500], 500); - } - DB::commit(); + } + DB::rollBack(); return response()->json(['status' => 'success', "action" => "deleted", 'message' => 'Data activity successfully deleted!', 'code' => 200], 200); } - private function deleteRelative($activity_id, $company_id) + private function deleteRelative($activity_id) { - DB::transaction(function() use($activity_id, $company_id) { - UserToActivity::where('activity_id', $activity_id)->delete(); - AssignMaterial::where('activity_id', $activity_id)->delete(); - $dataAd = ActivityDokumen::where("activity_id", $activity_id)->get(); - $company = Company::find($company_id); - if($company) { - $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); - foreach ($dataAd as $ad) { - if (file_exists($destinationPath['pathActivityDocument'] . $ad->file)) { - unlink($destinationPath['pathActivityDocument'] . $ad->file); - } - } - ActivityDokumen::where("activity_id", $activity_id)->delete(); - } - }); + DB::transaction(function () use ($activity_id) { + UserToActivity::where('activity_id', $activity_id)->delete(); + AssignMaterial::where('activity_id', $activity_id)->delete(); + $dataAd = ActivityDokumen::where("activity_id", $activity_id)->get(); + $destinationPath = $this->setCustomeDirectoryUpload(); + foreach ($dataAd as $ad) { + if (file_exists($destinationPath['pathActivityDocument'] . $ad->file)) { + unlink($destinationPath['pathActivityDocument'] . $ad->file); + } + } + ActivityDokumen::where("activity_id", $activity_id)->delete(); + }); } public function getUpdate($id) @@ -1143,28 +1139,28 @@ class ActivityController extends Controller public function uploadTmpImport(Request $request) { - DB::beginTransaction(); + DB::beginTransaction(); if ($request->hasFile('dokumen')) { $document = $request->file('dokumen'); $gantt_id = $request->gantt_id; - $timeNow = Carbon::now(); - - $originalFilename = $document->getClientOriginalName(); - $extension = pathinfo($originalFilename, PATHINFO_EXTENSION); - $filename = pathinfo($originalFilename, PATHINFO_FILENAME); - $name = $filename . '_' . $timeNow->format('d-m-y-His') . '.' . $extension; - - // Limited Storage - $company = Company::where('company_name', $request->company_name)->first(); - if($company) { - $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); - $getLimitStorage = $this->setLimitsStorage($company, $document, $destinationPath['pathTmpImport'], $destinationPath); - } + $timeNow = Carbon::now(); + + $originalFilename = $document->getClientOriginalName(); + $extension = pathinfo($originalFilename, PATHINFO_EXTENSION); + $filename = pathinfo($originalFilename, PATHINFO_FILENAME); + $name = $filename . '_' . $timeNow->format('d-m-y-His') . '.' . $extension; + + // Limited Storage + $company = Company::where('company_name', $request->company_name)->first(); + if ($company) { + $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); + $getLimitStorage = $this->setLimitsStorage($company, $document, $destinationPath['pathTmpImport'], $destinationPath); + } if (isset($getLimitStorage)) { - if($getLimitStorage === false) { - DB::rollBack(); - return response()->json(['status' => 'failed', 'message' => 'Limited storage maximum!', 'code' => 500], 500); - } + if ($getLimitStorage === false) { + DB::rollBack(); + return response()->json(['status' => 'failed', 'message' => 'Limited storage maximum!', 'code' => 500], 500); + } $data = [ 'gantt_id' => (int) $gantt_id, @@ -1175,17 +1171,17 @@ class ActivityController extends Controller $result = TmpImport::create($data); if (!$document->move($destinationPath['pathTmpImport'], $name) && $result) { - unlink($destinationPath['pathTmpImport'].$name); - DB::rollBack(); + unlink($destinationPath['pathTmpImport'] . $name); + DB::rollBack(); return response()->json(['status' => 'failed', 'message' => 'Upload failed!', 'code' => 500], 500); } - DB::commit(); + DB::commit(); return response()->json(['status' => 'success', 'message' => 'Upload successful!', 'code' => 200], 200); } - DB::rollBack(); + DB::rollBack(); return response()->json(['status' => 'failed', 'message' => 'Upload failed!', 'code' => 500], 500); } - DB::rollBack(); + DB::rollBack(); return response()->json(['status' => 'failed', 'message' => 'File is required!', 'code' => 400], 400); } diff --git a/app/Http/Controllers/DivisiController.php b/app/Http/Controllers/DivisiController.php index 540dd40..61ba1e4 100644 --- a/app/Http/Controllers/DivisiController.php +++ b/app/Http/Controllers/DivisiController.php @@ -22,7 +22,7 @@ class DivisiController extends Controller public function add(Request $request) { $this->validate($request, [ - 'name' => 'string|required|unique:m_divisi,name,NULL,id,company_id,' . $request->input('company_id'), + 'name' => 'string|required|unique:m_divisi,name,NULL,id', 'description' => 'nullable|string', 'parent' => 'nullable|integer', 'color' => 'nullable|string|max:10' @@ -32,7 +32,6 @@ class DivisiController extends Controller $data['created_by'] = $this->currentName; $result = Divisi::create($data); - if (!$result) return response()->json(['status' => 'failed', 'message' => 'Failed to add data', 'code' => 500]); @@ -86,7 +85,6 @@ class DivisiController extends Controller { $companyId = $request->input('columns.0.value'); $parentDivisi = Divisi::whereNull('parent') - ->where('company_id', $companyId) ->with('children') ->get(); $divisions = []; diff --git a/app/Http/Controllers/FolderDocumentProyekController.php b/app/Http/Controllers/FolderDocumentProyekController.php index ea46b8c..30dc947 100644 --- a/app/Http/Controllers/FolderDocumentProyekController.php +++ b/app/Http/Controllers/FolderDocumentProyekController.php @@ -47,13 +47,12 @@ class FolderDocumentProyekController extends Controller } } - public function delete($id, $company_id) + public function delete($id) { DB::beginTransaction(); $data = FolderDocumentProyek::find($id); - $company = Company::find($company_id); - if($data && $company) { - $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); + if($data) { + $destinationPath = $this->setCustomeDirectoryUpload(); $dir = $destinationPath['pathDocument']; $dataFile = DokumenProject::where([['type_dokumen', 'project-document-in-folder'],['ref_id', $data['id']]])->orderByRaw('id ASC')->get(); if (is_dir($dir)) { diff --git a/app/Http/Controllers/ProductTransactionController.php b/app/Http/Controllers/ProductTransactionController.php index 07fbb68..cb040c5 100644 --- a/app/Http/Controllers/ProductTransactionController.php +++ b/app/Http/Controllers/ProductTransactionController.php @@ -10,7 +10,6 @@ class ProductTransactionController extends Controller public function add(Request $request) { $this->validate($request, [ - 'company_id' => 'required|integer', 'type_paket' => 'required|string', 'amount' => 'required', 'exp_ospro' => 'required' diff --git a/app/Http/Controllers/ProjectDokumenController.php b/app/Http/Controllers/ProjectDokumenController.php index 5f1e02f..366f624 100644 --- a/app/Http/Controllers/ProjectDokumenController.php +++ b/app/Http/Controllers/ProjectDokumenController.php @@ -3,93 +3,79 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; -use App\Models\{DokumenProject,Company}; +use App\Models\{DokumenProject, Company}; use Carbon\Carbon; use Illuminate\Support\Facades\DB; class ProjectDokumenController extends Controller { - public function dokumenByProyekId($id){ - if(empty($id) || !is_int((int)$id)) { - return response()->json(['status'=>'failed','message'=>'project id is required!','code'=>400], 400); + public function dokumenByProyekId($id) + { + if (empty($id) || !is_int((int)$id)) { + return response()->json(['status' => 'failed', 'message' => 'project id is required!', 'code' => 400], 400); } $document = DokumenProject::where("ref_id", $id)->get(); - if(count($document) == 0) - return response()->json(['status'=>'','message'=>'Data not found!' ,'code'=>404], 404); + if (count($document) == 0) + return response()->json(['status' => '', 'message' => 'Data not found!', 'code' => 404], 404); - return response()->json(['status'=>'success','data'=> $document ,'code'=>200], 200); + return response()->json(['status' => 'success', 'data' => $document, 'code' => 200], 200); } - public function delete($id, $company_id) + public function delete($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); } $document = DokumenProject::find($id); - if(!$document){ - return response()->json(['status'=>'failed','message'=>'Data not found!','code'=>400], 400); + if (!$document) { + return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 400], 400); + } + $destinationPath = $this->setCustomeDirectoryUpload(); + if (file_exists($destinationPath['pathDocument'] . $document['file'])) { + unlink($destinationPath['pathDocument'] . $document['file']); } - $company = Company::find($company_id); - if($company) { - $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); - if(file_exists($destinationPath['pathDocument'].$document['file'])){ - unlink($destinationPath['pathDocument'].$document['file']); - } - } $document->delete(); - return response()->json(['status'=>'success','message'=>'Data deleted!','code'=>200], 200); + return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200); } public function uploadProjectDokumen(Request $request) { - DB::beginTransaction(); - $timeNow = Carbon::now(); - if($request->hasFile('dokumen')){ + DB::beginTransaction(); + $timeNow = Carbon::now(); + if ($request->hasFile('dokumen')) { $document = $request->file('dokumen'); $ref_id = $request->ref_id; - $originalFilename = $document->getClientOriginalName(); - $extension = pathinfo($originalFilename, PATHINFO_EXTENSION); - $filename = pathinfo($originalFilename, PATHINFO_FILENAME); - $name = $filename . '_' . $timeNow->format('d-m-y-His') . '.' . $extension; - // Limited Storage - $company = Company::whereId($request->company_id)->first(); - if($company) { - $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); - $getLimitStorage = $this->setLimitsStorage($company, $document, $destinationPath['pathDocument'],$destinationPath); - } - if(isset($getLimitStorage)) { - if($getLimitStorage === false) { - DB::rollBack(); - return response()->json(['status' => 'failed', 'message' => 'Limited storage maximum!', 'code' => 500], 500); - } - $data = [ - 'ref_id' => (int)$ref_id, - 'file' => $name, - 'type_dokumen' => isset($request->type_dokumen) ? $request->type_dokumen : 'project-document' - ]; - - $result = DokumenProject::create($data); - - if(!$document->move($destinationPath['pathDocument'], $name) && $result) { - unlink($destinationPath['pathDocument'].$name); - DB::rollBack(); - return response()->json(['status'=>'failed','message'=>'Dokumen project gagal diupload!','code'=> 500], 500); - } - DB::commit(); - return response()->json(['status'=>'success','message'=>'Dokumen project berhasil diupload!','code'=>200], 200); + $originalFilename = $document->getClientOriginalName(); + $extension = pathinfo($originalFilename, PATHINFO_EXTENSION); + $filename = pathinfo($originalFilename, PATHINFO_FILENAME); + $name = $filename . '_' . $timeNow->format('d-m-y-His') . '.' . $extension; + + $destinationPath = $this->setCustomeDirectoryUpload(); + $data = [ + 'ref_id' => (int)$ref_id, + 'file' => $name, + 'type_dokumen' => isset($request->type_dokumen) ? $request->type_dokumen : 'project-document' + ]; + + $result = DokumenProject::create($data); + if (!$document->move($destinationPath['pathDocument'], $name) && $result) { + unlink($destinationPath['pathDocument'] . $name); + DB::rollBack(); + return response()->json(['status' => 'failed', 'message' => 'Dokumen project gagal diupload!', 'code' => 500], 500); } - DB::rollBack(); - return response()->json(['status'=>'failed','message'=>'Dokumen project gagal diupload!','code'=> 500], 500); + DB::commit(); + return response()->json(['status' => 'success', 'message' => 'Dokumen project berhasil diupload!', 'code' => 200], 200); } - DB::rollBack(); - return response()->json(['status'=>'failed','message'=>'File is required!','code'=>400], 400); + DB::rollBack(); + return response()->json(['status' => 'failed', 'message' => 'File is required!', 'code' => 400], 400); } - public function searchDocProject(Request $request){ + public function searchDocProject(Request $request) + { $payload = $request->all(); $dataBuilder = $this->setUpPayload($payload, 'document_project'); @@ -98,34 +84,32 @@ class ProjectDokumenController extends Controller $dataGet = $builder->get(); $totalRecord = $countBuilder->count(); - return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); + return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200); } - public function downloadDokumen($id, $company_id) + public function downloadDokumen($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); + } $document = DokumenProject::find($id); - $company = Company::find($company_id); - - if(!$document || !$company) { - return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); - } - - $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); - $pathToFile = $destinationPath['pathDocument'].$document['file']; - - if(!file_exists($pathToFile)) { - return response()->json(['status'=>'failed','message'=>'Directory not found!','code'=> 404], 404); - } - if($company) { - $name = pathinfo($pathToFile, PATHINFO_FILENAME) . "." . pathinfo($pathToFile, PATHINFO_EXTENSION); - } - $headers = [ - 'Content-Disposition' => 'attachment; filename="'.$name.'"' - ]; - - return response()->download($pathToFile, $name, $headers); + + if (!$document) { + return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); + } + + $destinationPath = $this->setCustomeDirectoryUpload(); + $pathToFile = $destinationPath['pathDocument'] . $document['file']; + if (!file_exists($pathToFile)) { + return response()->json(['status' => 'failed', 'message' => 'Directory not found!', 'code' => 404], 404); + } + + $name = pathinfo($pathToFile, PATHINFO_FILENAME) . "." . pathinfo($pathToFile, PATHINFO_EXTENSION); + + $headers = [ + 'Content-Disposition' => 'attachment; filename="' . $name . '"' + ]; + + return response()->download($pathToFile, $name, $headers); } } diff --git a/app/Http/Controllers/ProjectExpenditureController.php b/app/Http/Controllers/ProjectExpenditureController.php index 43f379d..3589400 100644 --- a/app/Http/Controllers/ProjectExpenditureController.php +++ b/app/Http/Controllers/ProjectExpenditureController.php @@ -16,7 +16,6 @@ class ProjectExpenditureController extends Controller // Set Unique $result = ProjectExpenditure::query() ->where([ - ['company_id',$data['company_id']], ['name', $data['name']] ])->first(); if($result) { diff --git a/app/Http/Controllers/ProjectFinancialHealthController.php b/app/Http/Controllers/ProjectFinancialHealthController.php index 7fcabd6..f110580 100644 --- a/app/Http/Controllers/ProjectFinancialHealthController.php +++ b/app/Http/Controllers/ProjectFinancialHealthController.php @@ -16,7 +16,6 @@ class ProjectFinancialHealthController extends Controller // Set Unique $result = ProjectFinancialHealth::query() ->where([ - ['company_id',$data['company_id']], ['name', $data['name']] ])->first(); if($result) { diff --git a/app/Http/Controllers/ProjectInvoiceController.php b/app/Http/Controllers/ProjectInvoiceController.php index 12f9a6f..ea46bc6 100644 --- a/app/Http/Controllers/ProjectInvoiceController.php +++ b/app/Http/Controllers/ProjectInvoiceController.php @@ -16,7 +16,6 @@ class ProjectInvoiceController extends Controller // Set Unique $result = ProjectInvoice::query() ->where([ - ['company_id',$data['company_id']], ['name', $data['name']] ])->first(); if($result) { diff --git a/app/Http/Controllers/ProjectRoleController.php b/app/Http/Controllers/ProjectRoleController.php index 19be106..d81d8df 100644 --- a/app/Http/Controllers/ProjectRoleController.php +++ b/app/Http/Controllers/ProjectRoleController.php @@ -12,7 +12,6 @@ class ProjectRoleController extends Controller $this->validate($request, [ 'name' => 'required', 'description' => 'required', - 'company_id' => 'required' ]); $data = $request->all(); @@ -20,7 +19,6 @@ class ProjectRoleController extends Controller $data['created_by'] = $this->currentName; $result = ProjectRole::create($data); - if($result){ return response()->json(['status'=>'success','message'=>'add data project role successfully!','code'=>200], 200); }else{ diff --git a/app/Http/Controllers/ProjectScheduleHealthController.php b/app/Http/Controllers/ProjectScheduleHealthController.php index 2bae0bd..dcafe1f 100644 --- a/app/Http/Controllers/ProjectScheduleHealthController.php +++ b/app/Http/Controllers/ProjectScheduleHealthController.php @@ -16,7 +16,6 @@ class ProjectScheduleHealthController extends Controller // Set Unique $result = ProjectScheduleHealth::query() ->where([ - ['company_id',$data['company_id']], ['name', $data['name']] ])->first(); if($result) { diff --git a/app/Http/Controllers/RoleController.php b/app/Http/Controllers/RoleController.php index 371ba5c..9847e44 100644 --- a/app/Http/Controllers/RoleController.php +++ b/app/Http/Controllers/RoleController.php @@ -11,9 +11,8 @@ class RoleController extends Controller public function add(Request $request) { $this->validate($request, [ - 'name' => 'string|required|unique:m_roles,name,NULL,id,company_id,' . $request->input('company_id'), + 'name' => 'string|required|unique:m_roles,name,NULL,id', 'description' => 'required', - 'company_id' => 'required' ]); $data = $request->all(); @@ -24,7 +23,6 @@ class RoleController extends Controller } $result = Role::create($data); - if (!$result) return response()->json(['status' => 'failed', 'message' => 'Failed to add data!', 'code' => 500], 500); diff --git a/app/Http/Controllers/UserToProyekController.php b/app/Http/Controllers/UserToProyekController.php index eaadc28..5074c00 100644 --- a/app/Http/Controllers/UserToProyekController.php +++ b/app/Http/Controllers/UserToProyekController.php @@ -113,7 +113,7 @@ class UserToProyekController extends Controller } } - public function delete($id, $company_id) + public function delete($id) { $data = UserToProyek::find($id); if ($data) { @@ -121,7 +121,7 @@ class UserToProyekController extends Controller $proyekId = $data->proyek_id; $delete = $data->delete(); if ($delete) { - $this->deleteRelative($proyekId, $userId, $company_id); + $this->deleteRelative($proyekId, $userId); } } else { return response()->json(['status' => 'failed', 'message' => 'data assign hr to proyek not found!', 'code' => 400], 400); @@ -136,25 +136,22 @@ class UserToProyekController extends Controller } } - private function deleteRelative($proyekId, $userId, $company_id) + private function deleteRelative($proyekId, $userId) { $uta = UserToActivity::where("proyek_id", $proyekId)->where("user_id", $userId)->get(); foreach ($uta as $value) { $dataRa = ReportActivity::where("activity_id", $value->activity_id)->where("user_id", $value->user_id)->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); - } + $destinationPath = $this->setCustomeDirectoryUpload(); + 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(); } - ReportActivity::where("activity_id", $value->activity_id)->where("user_id", $value->user_id)->delete(); + Image::where("ref_id", $ra->id)->where("category", "report_activity")->delete(); } + ReportActivity::where("activity_id", $value->activity_id)->where("user_id", $value->user_id)->delete(); } UserToActivity::where("proyek_id", $proyekId)->where("user_id", $userId)->delete(); } diff --git a/routes/web.php b/routes/web.php index 71144ee..3978bc4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -66,9 +66,9 @@ $router->group(['prefix' => 'api', 'middleware' => 'cors'], function () use ($ro $router->post('/document-project/upload', 'ProjectDokumenController@uploadProjectDokumen'); $router->get('/document-project/get/{id}', 'ProjectDokumenController@dokumenByProyekId'); - $router->delete('/document-project/delete/{id}/{company_id}', 'ProjectDokumenController@delete'); + $router->delete('/document-project/delete/{id}', 'ProjectDokumenController@delete'); $router->post('/document-project/search', 'ProjectDokumenController@searchDocProject'); - $router->get('/document-project/download/{id}/{company_id}', 'ProjectDokumenController@downloadDokumen'); + $router->get('/document-project/download/{id}', 'ProjectDokumenController@downloadDokumen'); $router->post('/document-activity/upload', 'ActivityDokumenController@uploadProjectDokumen'); $router->get('/document-activity/get/{id}', 'ActivityDokumenController@dokumenByActivityId');