From d40747ef3ded43fb267c864f92080da3a4ac351e Mon Sep 17 00:00:00 2001 From: Watiah11 Date: Fri, 10 May 2024 15:31:41 +0700 Subject: [PATCH 1/4] fix: role menu --- app/Http/Controllers/RoleMenuController.php | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/RoleMenuController.php b/app/Http/Controllers/RoleMenuController.php index 2a67fca..e8073d9 100644 --- a/app/Http/Controllers/RoleMenuController.php +++ b/app/Http/Controllers/RoleMenuController.php @@ -5,25 +5,28 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Menu; use App\Models\RoleMenu; +use Illuminate\Support\Facades\Log; class RoleMenuController extends Controller { public function add(Request $request) { $this->validate($request, [ - 'role_id' => 'required', - 'menu_id' => 'required', + 'role_id.*' => 'required', + 'menu_id.*' => 'required' ]); $data = $request->all(); - $data['created_by'] = $this->currentName; - - $result = RoleMenu::create($data); - - if($result){ + $countData = 0; + foreach($data as $arrData) { + $arrData['created_by'] = $this->currentName; + $result = RoleMenu::create($arrData); + $countData++; + } + if($result && $countData > 0) { return response()->json(['status'=>'success','message'=>'add data role menu successfully!','code'=>200], 200); - }else{ + } else { return response()->json(['status'=>'failed','message'=>'add data role menu failed!','code'=>400], 400); } } From 1fffcd4fd5d72dd21a161d11b33f09b508aa6759 Mon Sep 17 00:00:00 2001 From: wahyun Date: Tue, 14 May 2024 14:46:23 +0700 Subject: [PATCH 2/4] feat: adding transaction in company management --- app/Http/Controllers/CompanyController.php | 61 ++++++++++++++++++---- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index b828469..67338d3 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -5,6 +5,8 @@ namespace App\Http\Controllers; use App\Models\{User, RoleMenu, Role, ProductTransaction, MenuCompany, Company}; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\File; +use Carbon\Carbon; class CompanyController extends Controller { @@ -14,7 +16,9 @@ class CompanyController extends Controller 'company_name' => 'required', 'registration_no' => 'required', 'template_id' => 'required', - 'is_active' => 'required' + 'is_active' => 'required', + 'type_paket' => 'required|in:Free,Basic', + 'base_url' => 'required' ]); $data = $request->all(); @@ -24,11 +28,12 @@ class CompanyController extends Controller $data['base_url'] = json_encode($data['base_url'], true); } $result = Company::create($data); + $addTransaction = $this->addTransaction($result['id'],$data['type_paket']); - if ($result) { - return response()->json(['status' => 'success','data' => $result, 'message' => 'add Company successfully!', 'code' => 200], 200); + if ($result && $addTransaction) { + return response()->json(['status' => 'success','data' => $result, 'message' => 'Add Company successfully!', 'code' => 200], 200); } else { - return response()->json(['status' => 'failed', 'message' => 'add data Company failed!', 'code' => 400], 400); + return response()->json(['status' => 'failed', 'message' => 'Add data Company failed!', 'code' => 400], 400); } } @@ -62,11 +67,12 @@ class CompanyController extends Controller } if ($data) { $result = $data->update($request->all()); + $updateTransaction = $this->updateTransaction($id,$request->type_paket); } else { return response()->json(['status' => 'failed', 'message' => 'data Company not found!', 'code' => 400], 400); die(); } - if ($result) { + if ($result && $updateTransaction) { return response()->json(['status' => 'success', 'message' => 'data Company successfully updated!', 'code' => 200], 200); } else { return response()->json(['status' => 'failed', 'message' => 'data Company failed updated!', 'code' => 400], 400); @@ -78,7 +84,7 @@ class CompanyController extends Controller DB::beginTransaction(); $data = Company::find((int)$id); if ($data) { - $this->deleteRelative((int)$data['id']); + $this->deleteRelative($data); if ($data->delete()) { DB::commit(); return response()->json(['status' => 'success', 'message' => 'Data Company successfully deleted!', 'code' => 200], 200); @@ -93,18 +99,51 @@ class CompanyController extends Controller } } - private function deleteRelative($company_id) + private function deleteRelative($data) { - MenuCompany::where('company_id', $company_id)->delete(); - User::where('company_id', $company_id)->delete(); - $role = Role::where('company_id', $company_id)->first(); + $directory = 'assets/' . $data['company_name']; + if (is_dir($directory)) { + File::deleteDirectory($directory); + } + + MenuCompany::where('company_id', $data['id'])->delete(); + User::where('company_id', $data['id'])->delete(); + $role = Role::where('company_id', $data['id'])->first(); if ($role) { RoleMenu::where('role_id', $role->id)->delete(); $role->delete(); } - ProductTransaction::where('company_id', $company_id)->delete(); + ProductTransaction::where('company_id', $data['id'])->delete(); } + protected function addTransaction($id_company, $type_paket) + { + $currentDate = Carbon::now(); + $finalDate = $currentDate->copy()->addDays(30); + $formData = array( + 'company_id' => (int)$id_company, + 'type_paket' => $type_paket, + 'exp_ospro' => $finalDate, + 'amount' => $type_paket === 'Free' ? 0 : 250000 + ); + $result = ProductTransaction::create($formData); + if($result) { + return true; + } else { + return false; + } + } + + protected function updateTransaction($id_company, $type_paket) + { + $result = ProductTransaction::where('company_id', (int)$id_company)->first(); + $result->update(['type_paket' => $type_paket]); + if($result) { + return true; + } else { + return false; + } + } public function search(Request $request) { From ca2619ae08e067ca7b35dfd0820babfaddba00cb Mon Sep 17 00:00:00 2001 From: wahyun Date: Tue, 14 May 2024 14:47:01 +0700 Subject: [PATCH 3/4] fix: fixed getSize in multiple upload --- app/Http/Controllers/ImageController.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/ImageController.php b/app/Http/Controllers/ImageController.php index af18b45..1e3c194 100644 --- a/app/Http/Controllers/ImageController.php +++ b/app/Http/Controllers/ImageController.php @@ -75,12 +75,12 @@ class ImageController extends Controller } else { DB::rollBack(); unlink($destinationPath['pathImage'].$name); - return response()->json(['status' => 'failed','message' => 'image upload is failed! pertama' ,'code' => 400], 400); + return response()->json(['status' => 'failed','message' => 'image upload is failed!' ,'code' => 400], 400); } } else { DB::rollBack(); - return response()->json(['status'=>'failed','message'=>'image upload is failed! kedua','code'=>400], 400); + return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); } } else { DB::rollBack(); @@ -105,7 +105,10 @@ class ImageController extends Controller $company = Company::where('company_name', $request->company_name)->first(); if($company) { $totalSize = 0; - $sizeFile = $dokumen->getSize(); + $sizeFile = 0; + foreach ($dokumen as $file) { + $sizeFile += $file->getSize(); + } $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); $folderPath = $destinationPath['pathImage']; @@ -114,7 +117,6 @@ class ImageController extends Controller } $files = scandir($folderPath); - $totalSize = 0; foreach ($files as $file) { if (is_file($folderPath . '/' . $file)) { From e09a0b11d20ba719ccbda9ac7f8bfe7b42c9c8bb Mon Sep 17 00:00:00 2001 From: wahyun Date: Tue, 14 May 2024 14:47:31 +0700 Subject: [PATCH 4/4] fix: update type join --- app/Http/Controllers/ProjectController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index f644ed2..5e3e008 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -120,7 +120,7 @@ class ProjectController extends Controller 'md.name as divisi_name', 'm_proyek.company_id' ) - ->join('m_divisi as md', 'md.id', '=', 'm_proyek.divisi_id') + ->leftJoin('m_divisi as md', 'md.id', '=', 'm_proyek.divisi_id') ->where('m_proyek.id', $id) ->first(); if (!$result) {