diff --git a/app/Http/Controllers/AssignMaterialController.php b/app/Http/Controllers/AssignMaterialController.php index 0d7b303..e385a95 100644 --- a/app/Http/Controllers/AssignMaterialController.php +++ b/app/Http/Controllers/AssignMaterialController.php @@ -12,13 +12,15 @@ use Datatables; class AssignMaterialController extends Controller { - private function sanitizeDecimal($number) { - $number = str_replace(".","",$number); - $number = str_replace(",",".",$number); + private function sanitizeDecimal($number) + { + $number = str_replace(".", "", $number); + $number = str_replace(",", ".", $number); return $number; } - public function add(Request $request){ + public function add(Request $request) + { $this->validate($request, [ 'activity_id' => 'required', 'material_id' => 'required', @@ -29,8 +31,8 @@ class AssignMaterialController extends Controller $checkStock = RequestMaterial::where("id", $request->material_id)->first(); $currentStock = $checkStock->qty; - if((int)$currentStock < (int)$request->qty_planning){ - return response()->json(['status'=>'failed','message'=>'Stock is not enough!','code'=> 500]); + if ((int)$currentStock < (int)$request->qty_planning) { + return response()->json(['status' => 'failed', 'message' => 'Stock is not enough!', 'code' => 500]); } $start_date = $activity->start_date; @@ -43,56 +45,58 @@ class AssignMaterialController extends Controller $data['budget'] = $checkStock->price; $data['qty_planning'] = $this->sanitizeDecimal($data['qty_planning']); - if ($planDate >= $startDate) { + if ($planDate >= $startDate) { $result = AssignMaterial::create($data); - return response()->json(['status'=>'success','message'=>'Data added!', 'code'=>200], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 400], 400); + return response()->json(['status' => 'success', 'message' => 'Data added!', 'code' => 200], 200); + } else { + return response()->json(['status' => 'failed', 'message' => 'Failed to add data!', 'code' => 400], 400); } } - 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); + 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); $data = AssignMaterial::find($id); - if(!$data) - return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); + 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'=>'success','message'=> 'Data updated!','code'=> 200], 200); + if ($result) + return response()->json(['status' => 'success', 'message' => 'Data updated!', 'code' => 200], 200); - return response()->json(['status'=>'failed','message'=>'Failed to update!','code'=> 500], 500); + return response()->json(['status' => 'failed', 'message' => 'Failed to update!', 'code' => 500], 500); } public function delete($id) { $data = AssignMaterial::where('id', $id)->first(); - $reports = ReportActivityMaterial::where('assign_material_id', $data->id)->get(); - if (isset($reports)) { + $reports = ReportActivityMaterial::where('assign_material_id', $data->id)->get(); + if (isset($reports)) { foreach ($reports as $report) { $report->delete(); } - } + } - if($data->delete()) - return response()->json(['status'=>'success','message'=>'Data deleted!','code'=> 200], 200); + if ($data->delete()) + return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200); - return response()->json(['status'=>'failed','message'=>'Failed to delete!','code'=> 500], 500); + return response()->json(['status' => 'failed', 'message' => 'Failed to delete!', 'code' => 500], 500); } - public function edit($id){ - if(empty($id) || !is_int((int)$id)) - return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); + public function edit($id) + { + if (empty($id) || !is_int((int)$id)) + return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); $result = AssignMaterial::find($id); - if($result) - return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); + if ($result) + return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200); - return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); + return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404); } public function search(Request $request) @@ -103,7 +107,7 @@ class AssignMaterialController extends Controller $countBuilder = $dataBuilder['count']; $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 list() @@ -111,18 +115,21 @@ class AssignMaterialController extends Controller $data = AssignMaterial::all(); $countData = $data->count(); - if($data) - return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); + if ($data) + return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200); - return response()->json(['status'=>'failed','message'=>'Failed to get the data!','code'=> 500], 500); + return response()->json(['status' => 'failed', 'message' => 'Failed to get the data!', 'code' => 500], 500); } - public function datatables(Request $request){ + public function datatables(Request $request) + { $id_activity = $request->query('idact'); $type = $request->query('type') ?? "material"; $data = AssignMaterial::select( - "assign_material_to_activity.*","m.description as material_name", "m.uom as uom" - ) + "assign_material_to_activity.*", + "m.description as material_name", + "m.uom as uom" + ) ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") ->where('assign_material_to_activity.activity_id', $id_activity) ->where('assign_material_to_activity.type', $type) @@ -130,28 +137,33 @@ class AssignMaterialController extends Controller ->get(); return Datatables::of($data) ->addIndexColumn() - ->addColumn('action', function($row){ - $actionBtn = ''; + ->addColumn('action', function ($row) { + $actionBtn = ''; return $actionBtn; }) ->rawColumns(['action'])->make(true); } - public function datatablesForReportActivity(Request $request){ + public function datatablesForReportActivity(Request $request) + { $id_activity = $request->query('idact'); - $data = - AssignMaterial::select( - DB::raw('(SELECT SUM(assign_material_to_activity.qty_planning) FROM assign_material_to_activity WHERE assign_material_to_activity.activity_id = ram.activity_id) as qty_planning'), - DB::raw('SUM(ram.qty) as qty_sum'), - "m.description as material_name", - "assign_material_to_activity.activity_id", - ) - ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") - ->join("report_activity_material as ram", "ram.assign_material_id", "=", "assign_material_to_activity.id") - ->groupBy("m.description") + $query = AssignMaterial::select( + DB::raw('(SELECT SUM(assign_material_to_activity.qty_planning) FROM assign_material_to_activity WHERE assign_material_to_activity.activity_id = ram.activity_id) as qty_planning'), + DB::raw('SUM(ram.qty) as qty_sum'), + "m.description as material_name", + "assign_material_to_activity.activity_id" + ) + ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id"); + if (!AssignMaterial::join("report_activity_material as ram", "ram.assign_material_id", "=", "assign_material_to_activity.id")->where("assign_material_to_activity.activity_id", $id_activity)->exists()) { + $query->leftJoin("report_activity_material as ram", "ram.assign_material_id", "=", "assign_material_to_activity.id"); + } else { + $query->join("report_activity_material as ram", "ram.assign_material_id", "=", "assign_material_to_activity.id"); + } + $data = $query->groupBy("m.description") ->groupBy("ram.activity_id") ->groupBy("assign_material_to_activity.activity_id") - ->where("assign_material_to_activity.activity_id", $id_activity)->get(); + ->where("assign_material_to_activity.activity_id", $id_activity) + ->get(); return Datatables::of($data) ->addIndexColumn() // ->addColumn('qty_planning', function($row){ @@ -164,60 +176,61 @@ class AssignMaterialController extends Controller // ->where("ram.activity_id", $row->activity_id)->first(); // return $val_qty_act ? $val_qty_act->qty_sum : '-'; // }) - ->addColumn('status_activity', function($row){ + ->addColumn('status_activity', function ($row) { $val_status = AssignMaterial::select("status_activity") ->join('m_req_material as m', 'm.id', '=', 'assign_material_to_activity.material_id') ->where('m.description', '=', $row->material_name) ->where("assign_material_to_activity.activity_id", $row->activity_id)->first(); return $val_status ? $val_status->status_activity : null; }) - ->addColumn('start_activity', function($row){ + ->addColumn('start_activity', function ($row) { $val_start = AssignMaterial::select("start_activity") ->join('m_req_material as m', 'm.id', '=', 'assign_material_to_activity.material_id') ->where('m.description', '=', $row->material_name) ->where("assign_material_to_activity.activity_id", $row->activity_id)->first(); return $val_start ? $val_start->start_activity : null; }) - ->addColumn('finish_activity', function($row){ + ->addColumn('finish_activity', function ($row) { $val_finish = AssignMaterial::select("finish_activity") ->join('m_req_material as m', 'm.id', '=', 'assign_material_to_activity.material_id') ->where('m.description', '=', $row->material_name) ->where("assign_material_to_activity.activity_id", $row->activity_id)->first(); return $val_finish ? $val_finish->finish_activity : null; }) - ->addColumn('uom', function($row){ + ->addColumn('uom', function ($row) { $val_uom = RequestMaterial::select("uom") ->where('description', '=', $row->material_name)->first(); return $val_uom ? $val_uom->uom : null; }) - ->addColumn('assign_material_id', function($row){ - $assignMaterial =AssignMaterial::select('assign_material_to_activity.id')->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") - ->where('activity_id', $row->activity_id)->where('m.description', $row->material_name)->first(); + ->addColumn('assign_material_id', function ($row) { + $assignMaterial = AssignMaterial::select('assign_material_to_activity.id')->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") + ->where('activity_id', $row->activity_id)->where('m.description', $row->material_name)->first(); return $assignMaterial ? $assignMaterial->id : null; }) - ->addColumn('action', function($row){ + ->addColumn('action', function ($row) { $dataPlan = AssignMaterial::select('assign_material_to_activity.id')->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") ->where('activity_id', $row->activity_id)->where('m.description', $row->material_name)->first(); - $actionBtn = ''; - $actionBtn .= ''; + $actionBtn = ''; + $actionBtn .= ''; return $actionBtn; }) ->rawColumns(['action'])->make(true); } - public function ForReportActivityByMaterial(Request $request){ + public function ForReportActivityByMaterial(Request $request) + { $id_activity = $request->idact; - $data = AssignMaterial::select("assign_material_to_activity.*","m.description as material_name", "m.uom as uom") + $data = AssignMaterial::select("assign_material_to_activity.*", "m.description as material_name", "m.uom as uom") ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") ->where('assign_material_to_activity.activity_id', $id_activity)->get(); foreach ($data as $key) { $val_qty_sum = ReportActivityMaterial::where('assign_material_id', '=', $key->id)->sum("qty"); } $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 Assign material, please try again later!','code'=>400], 400); + if ($data) { + return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200); + } else { + return response()->json(['status' => 'failed', 'message' => 'failed get Assign material, please try again later!', 'code' => 400], 400); } } } diff --git a/app/Http/Controllers/ImageController.php b/app/Http/Controllers/ImageController.php index 6ecb88c..a7b9de5 100644 --- a/app/Http/Controllers/ImageController.php +++ b/app/Http/Controllers/ImageController.php @@ -1,121 +1,237 @@ -hasFile('files')){ - - $this->validate($request, [ - 'ref_id' => 'required', - 'category' => 'required' - ]); - - $ref_id = $request->ref_id; - $dokumen = $request->file('files'); - $extension = $dokumen->extension(); - $name = $ref_id.date("YmdHis").".".$extension; - - $resultMove = $dokumen->move($this->pathImage, $name); - - if($resultMove){ - $data = [ - 'ref_id' => (int)$ref_id, - 'image' => $name, - 'category' => $request->category - ]; - - $result = Image::create($data); - - if($result){ - return response()->json(['status'=>'success', "name_image"=>$name,'message'=>'image upload is successfully!','code'=>200], 200); - }else{ - unlink($this->pathImage.$name); - return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); - } - - }else{ - return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); - } - }else{ - return response()->json(['status'=>'failed','message'=>'image is required!','code'=>400], 400); - } - } - - public function delete($id) - { - $data = Image::find($id); - - if($data){ - unlink($this->pathImage.$data->image); - $delete = $data->delete(); - }else{ - return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400); - die(); - } - - - if($delete){ - return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400); - } - } - - public function search(Request $request) - { - $payload = $request->all(); - $dataBuilder = $this->setUpPayload($payload, 'm_image'); - $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 deleteByRef($id, $category) - { - $data = Image::where("ref_id", $id)->where("category", $category)->first(); - - if($data){ - unlink($this->pathImage.$data->image); - $delete = $data->delete(); - }else{ - return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400); - die(); - } - - - if($delete){ - return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400); - } - } - - public function getByRefId($id, $category) - { - if(!$id || (int) $id < 0 || $id==""){ - return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); - die(); - } - - if(!$category || $category==""){ - return response()->json(['status'=>'failed','message'=>'category is required!','code'=>400], 400); - die(); - } - - $result = Image::where("ref_id", $id)->where("category", $category)->first(); - - if($result){ - return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'failed get data image, please try again later!','code'=>400], 400); - } - } -} +hasFile('files')){ + + $this->validate($request, [ + 'ref_id' => 'required', + 'category' => 'required' + ]); + + $ref_id = $request->ref_id; + $dokumen = $request->file('files'); + $extension = $dokumen->extension(); + $category = $request->category; + $name = $ref_id.date("YmdHis").".".$extension; + + $resultMove = $dokumen->move($this->pathImage, $name); + + if($resultMove){ + $data = [ + 'ref_id' => (int)$ref_id, + 'image' => $name, + 'category' => $category + ]; + + $result = Image::create($data); + + $company = Company::find((int)$ref_id); + if($company && $category === 'company_logo_login') { + $dataArray = json_decode($company['logo_login'], true); + if(isset($dataArray['slider'])) { + $jsonImageSlider['login'] = $name; + $jsonImageSlider['slider'] = $dataArray['slider']; + } else { + $jsonImageSlider['login'] = $name; + } + $company->update([ + 'logo_login' => $jsonImageSlider + ]); + } + + if($company && $category === 'company_logo_header') { + $company->update([ + 'logo_header' => array('content'=>$name) + ]); + } + + if($company && $category === 'company_favicon') { + $company->update([ + 'favicon_image' => array('content'=>$name) + ]); + } + + if($result){ + return response()->json(['status'=>'success', "name_image"=>$name,'message'=>'image upload is successfully!','code'=>200], 200); + }else{ + unlink($this->pathImage.$name); + return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); + } + + }else{ + return response()->json(['status'=>'failed','message'=>'image upload is failed!','code'=>400], 400); + } + }else{ + return response()->json(['status'=>'failed','message'=>'image is required!','code'=>400], 400); + } + } + + public function uploadMultipleImage(Request $request) { + $this->validate($request, [ + 'ref_id' => 'required', + 'category' => 'required', + 'files.*' => 'required|file|mimes:jpeg,png,jpg,gif|max:2048', + ]); + + if ($request->hasFile('files')) { + $ref_id = $request->ref_id; + $category = $request->category; + $dokumen = $request->file('files'); + $successCount = 0; + $imageSlider = []; + foreach ($dokumen as $file) { + $extension = $file->extension(); + $name = $ref_id.date("YmdHis").$successCount.".".$extension; + + $resultMove = $file->move($this->pathImage, $name); + + if ($resultMove) { + $data = [ + 'ref_id' => (int) $ref_id, + 'image' => $name, + 'category' => $category + ]; + $imageSlider[] = [ + 'content' => $name + ]; + $result = Image::create($data); + if ($result) { + $successCount++; + } else { + unlink($this->pathImage . $name); + } + } + } + $company = Company::find((int)$ref_id); + if($company && $category === 'company_slider_login') { + $dataArray = json_decode($company['logo_login'], true); + if(isset($dataArray['login'])) { + $jsonImageSlider['login'] = $dataArray['login']; + $jsonImageSlider['slider'] = $imageSlider; + } else { + $jsonImageSlider['slider'] = $imageSlider; + } + $company->update([ + 'logo_login' => $jsonImageSlider + ]); + } + if ($successCount > 0 && $company) { + return response()->json(['status' => 'success', 'message' => "$successCount images uploaded successfully!", 'code' => 200], 200); + } else { + return response()->json(['status' => 'failed', 'message' => 'No images uploaded successfully!', 'code' => 400], 400); + } + } else { + return response()->json(['status' => 'failed', 'message' => 'Image is required!', 'code' => 400], 400); + } + } + + + + public function delete($id) + { + $data = Image::find($id); + + if($data){ + unlink($this->pathImage.$data->image); + $delete = $data->delete(); + }else{ + return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400); + die(); + } + + + if($delete){ + return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400); + } + } + + public function search(Request $request) + { + $payload = $request->all(); + $dataBuilder = $this->setUpPayload($payload, 'm_image'); + $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 deleteByRef($id, $category) + { + $data = Image::where("ref_id", intval($id))->where("category", $category)->first(); + + if($data){ + unlink($this->pathImage.$data->image); + $delete = $data->delete(); + }else{ + return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400); + die(); + } + + + if($delete){ + return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400); + } + } + + public function deleteByRefMultiple($id, $category) + { + $successCount = 0; + $data = Image::where([ + ["ref_id", intval($id)], + ["category", $category] + ])->get(); + if($data->isNotEmpty()) { + foreach($data as $img) { + unlink($this->pathImage.$img->image); + $delete = $img->delete(); + if($delete){ + $successCount++; + } + } + } else { + return response()->json(['status'=>'failed','message'=>'data image not found!','code'=>400], 400); + die(); + } + + if($successCount > 0) { + return response()->json(['status'=>'success','message'=>'data image successfully deleted!','code'=>200], 200); + } else { + return response()->json(['status'=>'failed','message'=>'data image failed deleted!','code'=>400], 400); + } + } + + public function getByRefId($id, $category) + { + if(!$id || (int) $id < 0 || $id==""){ + return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); + die(); + } + + if(!$category || $category==""){ + return response()->json(['status'=>'failed','message'=>'category is required!','code'=>400], 400); + die(); + } + + $result = Image::where("ref_id", $id)->where("category", $category)->first(); + + if($result){ + return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'failed get data image, please try again later!','code'=>400], 400); + } + } +} diff --git a/routes/web.php b/routes/web.php index 3716661..2a75e85 100644 --- a/routes/web.php +++ b/routes/web.php @@ -314,7 +314,9 @@ $router->group(['prefix' => 'api', 'middleware' => 'cors'], function () use ($ro $router->post('/image/search', 'ImageController@search'); $router->delete('/image/delete/{id}', 'ImageController@delete'); $router->delete('/image/delete/{id}/{category}', 'ImageController@deleteByRef'); + $router->delete('/image/multiple-delete/{id}/{category}', 'ImageController@deleteByRefMultiple'); $router->post('/image/upload', 'ImageController@uploadImage'); + $router->post('/image/multiple-upload', 'ImageController@uploadMultipleImage'); $router->get('/image/{id}/{category}', 'ImageController@getByRefId'); $router->post('/panic-button/add', 'PanicButtonController@add');