validate($request, [ 'company_name' => 'required', 'registration_no' => 'required', 'template_id' => 'required', 'is_active' => 'required', 'type_paket' => 'required|in:Free,Basic', 'base_url' => 'required' ]); $data = $request->all(); $data['created_by'] = $this->currentName; if(isset($data['base_url']) && $data['base_url'] != NULL) { $data['base_url'] = json_encode($data['base_url'], true); } $result = Company::create($data); $addTransaction = $this->addTransaction($result['id'],$data['type_paket']); 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); } } public function edit($id) { if (!$id || (int) $id < 0 || $id == "") { return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); die(); } $result = Company::find($id); if ($result) { return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200); } else { return response()->json(['status' => 'failed', 'message' => 'failed get data Company, please try again later!', 'code' => 400], 400); } } public function update(Request $request, $id) { if (!$id || (int) $id < 0 || $id == "") { return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); } $data = Company::find($id); if(isset($data['base_url']) && $data['base_url'] != NULL) { $data->update(['base_url'=>null]); $data['base_url'] = json_encode($data['base_url'], true); } 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 && $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); } } public function delete($id) { DB::beginTransaction(); $data = Company::find((int)$id); if ($data) { $this->deleteRelative($data); 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 { DB::rollBack(); return response()->json(['status' => 'failed', 'message' => 'Data Company not found!', 'code' => 400], 400); die(); } } private function deleteRelative($data) { $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', $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) { $payload = $request->all(); $dataBuilder = $this->setUpPayload($payload, 'm_company'); $builder = $dataBuilder['builder']; $countBuilder = $dataBuilder['count']; $dataGet = $builder->get(); $totalRecord = $countBuilder->count(); return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200); } public function list() { $data = Company::all(); $countData = $data->count(); if ($data) { return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200); } else { return response()->json(['status' => 'failed', 'message' => 'failed get list Company, please try again later!', 'code' => 400], 400); } } }