From 4b8df65f4426d3d90c22d8938c434dcb38b7b0aa Mon Sep 17 00:00:00 2001 From: wahyu Date: Thu, 14 Sep 2023 17:09:07 +0700 Subject: [PATCH] cascade delete, delete report, update status --- .../Controllers/AssignMaterialController.php | 436 +++++++++--------- .../ReportActivityMaterialController.php | 18 +- app/Models/ReportActivityMaterial.php | 146 +++--- 3 files changed, 309 insertions(+), 291 deletions(-) diff --git a/app/Http/Controllers/AssignMaterialController.php b/app/Http/Controllers/AssignMaterialController.php index b3f917a..3d78384 100644 --- a/app/Http/Controllers/AssignMaterialController.php +++ b/app/Http/Controllers/AssignMaterialController.php @@ -1,215 +1,221 @@ -validate($request, [ - 'activity_id' => 'required', - 'material_id' => 'required', - 'qty_planning' => 'required' - ]); - - $activity = Activity::where('id', $request->activity_id)->first(); - - $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]); - } - - $start_date = $activity->start_date; - $start_date = substr($start_date, 0, 19); // remove the timezone offset - $startDate = new \DateTime(date("Y-m-d", strtotime($start_date))); - $planDate = new \DateTime(date("Y-m-d", strtotime($request->plan_date))); - - $data = $request->all(); - $data['created_by'] = $this->currentName; - $data['budget'] = $checkStock->price; - $data['qty_planning'] = $this->sanitizeDecimal($data['qty_planning']); - - 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); - } - } - - 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); - - $result = $data->update($request->all()); - - 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); - } - - public function delete($id) - { - $data = AssignMaterial::where('id', $id)->first(); - - 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); - } - - 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); - - return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); - } - - public function search(Request $request) - { - $payload = $request->all(); - $dataBuilder = $this->setUpPayload($payload, 'assign_material_to_activity'); - $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 = AssignMaterial::all(); - $countData = $data->count(); - - 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); - } - - 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" - ) - ->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) - ->orderBy('plan_date', 'desc') - ->get(); - return Datatables::of($data) - ->addIndexColumn() - ->addColumn('action', function($row){ - $actionBtn = ''; - return $actionBtn; - }) - ->rawColumns(['action'])->make(true); - } - - public function datatablesForReportActivity(Request $request){ - $id_activity = $request->query('idact'); - $data = - AssignMaterial::select( - AssignMaterial::raw('SUM(qty_planning) as qty_planning'), - "m.description as material_name", - "assign_material_to_activity.activity_id", - "assign_material_to_activity.type" - // "assign_material_to_activity.material_id", - ) - ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") - ->groupBy("m.description") - ->groupBy("assign_material_to_activity.activity_id") - ->groupBy("assign_material_to_activity.type") - ->where("assign_material_to_activity.activity_id", $id_activity)->get(); - return Datatables::of($data) - ->addIndexColumn() - ->addColumn('qty_sum', function($row){ - $val_qty_act = AssignMaterial::select(ReportActivityMaterial::raw('SUM(ram.qty) as qty_sum'),"m.description as material_name1") - ->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") - ->where("m.description", strval($row->material_name)) - ->where("ram.activity_id", $row->activity_id)->first(); - return $val_qty_act ? $val_qty_act->qty_sum : '-'; - }) - ->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){ - $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){ - $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){ - $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(); - return $assignMaterial ? $assignMaterial->id : null; - }) - ->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 .= ''; - return $actionBtn; - }) - ->rawColumns(['action'])->make(true); - } - - 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") - ->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); - } - } -} +validate($request, [ + 'activity_id' => 'required', + 'material_id' => 'required', + 'qty_planning' => 'required' + ]); + + $activity = Activity::where('id', $request->activity_id)->first(); + + $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]); + } + + $start_date = $activity->start_date; + $start_date = substr($start_date, 0, 19); // remove the timezone offset + $startDate = new \DateTime(date("Y-m-d", strtotime($start_date))); + $planDate = new \DateTime(date("Y-m-d", strtotime($request->plan_date))); + + $data = $request->all(); + $data['created_by'] = $this->currentName; + $data['budget'] = $checkStock->price; + $data['qty_planning'] = $this->sanitizeDecimal($data['qty_planning']); + + 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); + } + } + + 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); + + $result = $data->update($request->all()); + + 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); + } + + public function delete($id) + { + $data = AssignMaterial::where('id', $id)->first(); + $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); + + 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); + + $result = AssignMaterial::find($id); + + if($result) + return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); + + return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404); + } + + public function search(Request $request) + { + $payload = $request->all(); + $dataBuilder = $this->setUpPayload($payload, 'assign_material_to_activity'); + $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 = AssignMaterial::all(); + $countData = $data->count(); + + 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); + } + + 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" + ) + ->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) + ->orderBy('plan_date', 'desc') + ->get(); + return Datatables::of($data) + ->addIndexColumn() + ->addColumn('action', function($row){ + $actionBtn = ''; + return $actionBtn; + }) + ->rawColumns(['action'])->make(true); + } + + public function datatablesForReportActivity(Request $request){ + $id_activity = $request->query('idact'); + $data = + AssignMaterial::select( + AssignMaterial::raw('SUM(qty_planning) as qty_planning'), + "m.description as material_name", + "assign_material_to_activity.activity_id", + "assign_material_to_activity.type" + // "assign_material_to_activity.material_id", + ) + ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id") + ->groupBy("m.description") + ->groupBy("assign_material_to_activity.activity_id") + ->groupBy("assign_material_to_activity.type") + ->where("assign_material_to_activity.activity_id", $id_activity)->get(); + return Datatables::of($data) + ->addIndexColumn() + ->addColumn('qty_sum', function($row){ + $val_qty_act = AssignMaterial::select(ReportActivityMaterial::raw('SUM(ram.qty) as qty_sum'),"m.description as material_name1") + ->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") + ->where("m.description", strval($row->material_name)) + ->where("ram.activity_id", $row->activity_id)->first(); + return $val_qty_act ? $val_qty_act->qty_sum : '-'; + }) + ->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){ + $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){ + $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){ + $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(); + return $assignMaterial ? $assignMaterial->id : null; + }) + ->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 .= ''; + return $actionBtn; + }) + ->rawColumns(['action'])->make(true); + } + + 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") + ->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); + } + } +} diff --git a/app/Http/Controllers/ReportActivityMaterialController.php b/app/Http/Controllers/ReportActivityMaterialController.php index 54be3e0..86cb638 100644 --- a/app/Http/Controllers/ReportActivityMaterialController.php +++ b/app/Http/Controllers/ReportActivityMaterialController.php @@ -59,6 +59,14 @@ class ReportActivityMaterialController extends Controller "updated_by" => $this->currentName ); Activity::where('id', $request->activity_id)->update($dataUpdate); + } else { + $sumAssignMaterial = AssignMaterial::where('activity_id', $request->activity_id)->sum('qty_planning'); + $sumReportActivityMaterial = ReportActivityMaterial::where('activity_id', $request->activity_id)->sum('qty'); + $dataUpdate = array( + "persentase_progress" => $sumReportActivityMaterial/$sumAssignMaterial*100, + "updated_by" => $this->currentName + ); + Activity::where('id', $request->activity_id)->update($dataUpdate); } return response()->json(['status'=>'success','message'=>'Update data status successfully ', 'code'=>200]); @@ -114,12 +122,14 @@ class ReportActivityMaterialController extends Controller ->addIndexColumn() ->addColumn('action', function($row){ $actionBtn = ''; - $actionBtn .= ''; + if ($row->status_activity != 'done') { + $actionBtn .= ''; + } return $actionBtn; }) ->rawColumns(['action'])->make(true); }else{ - $data = ReportActivityMaterial::select("report_activity_material.*", "u.name as human_resource") + $data = ReportActivityMaterial::select("report_activity_material.*", "u.name as human_resource", "amta.status_activity") ->join("assign_material_to_activity as amta", "amta.id", "=", "report_activity_material.assign_material_id") ->join("m_req_material as m", "m.id", "=", "amta.material_id") ->join("m_users as u", "u.id", "=", "report_activity_material.user_id") @@ -131,7 +141,9 @@ class ReportActivityMaterialController extends Controller ->addIndexColumn() ->addColumn('action', function($row){ $actionBtn = ''; - $actionBtn .= ''; + if ($row->status_activity != 'done') { + $actionBtn .= ''; + } return $actionBtn; }) ->rawColumns(['action'])->make(true); diff --git a/app/Models/ReportActivityMaterial.php b/app/Models/ReportActivityMaterial.php index 44db31c..39849d5 100644 --- a/app/Models/ReportActivityMaterial.php +++ b/app/Models/ReportActivityMaterial.php @@ -1,73 +1,73 @@ -timezone(env('APP_TIMEZONE')) - ->toDateTimeString(); - } - - public static function boot() { - parent::boot(); - - static::created(function($data) { - $activity = Activity::find($data->activity_id); - $assignedMaterial = AssignMaterial::find($data->assign_material_id); - - $biayaActual = $activity->biaya_actual + floatval($assignedMaterial->budget) * floatval($data->qty); - - $dataPlan = AssignMaterial::where('activity_id', $activity->id)->get(); - if($dataPlan[0]->status_activity == 'done'){ - $percentage = 100; - } else { - $totalPlan = $dataPlan->sum('qty_planning'); - $totalVolumeActual = ReportActivityMaterial::where('activity_id', '=', $activity->id)->sum("qty"); - $percentage = ($totalVolumeActual * 100) / $totalPlan; - $percentage = $percentage >= config('app.max_percentage_not_done') ? config('app.max_percentage_not_done') : $percentage; - } - - $activity->update([ - "persentase_progress" => $percentage, - "biaya_actual" => $biayaActual, - ]); - - $activity->save(); - }); - - static::deleted(function($data) { - - $activity = Activity::find($data->activity_id); - $assignedMaterial = AssignMaterial::find($data->assign_material_id); - - $activity->biaya_actual -= floatval($assignedMaterial->budget) * floatval($data->qty); - - $dataPlan = AssignMaterial::where('activity_id', $activity->id)->get(); - $totalPlan = $dataPlan->sum('qty_planning'); - $totalVolumeActual = ReportActivityMaterial::where('activity_id', '=', $activity->id)->sum("qty"); - $percentage = ($totalVolumeActual * 100) / $totalPlan; - $activity->persentase_progress = $percentage >= config('app.max_percentage_not_done') ? config('app.max_percentage_not_done') : $percentage; - $activity->save(); - - }); - - } -} +timezone(env('APP_TIMEZONE')) + ->toDateTimeString(); + } + + public static function boot() { + parent::boot(); + + static::created(function($data) { + $activity = Activity::find($data->activity_id); + $assignedMaterial = AssignMaterial::find($data->assign_material_id); + + $biayaActual = $activity->biaya_actual + floatval($assignedMaterial->budget) * floatval($data->qty); + + $dataPlan = AssignMaterial::where('activity_id', $activity->id)->get(); + if($dataPlan[0]->status_activity == 'done'){ + $percentage = 100; + } else { + $totalPlan = $dataPlan->sum('qty_planning'); + $totalVolumeActual = ReportActivityMaterial::where('activity_id', '=', $activity->id)->sum("qty"); + $percentage = ($totalVolumeActual * 100) / $totalPlan; + $percentage = $percentage >= config('app.max_percentage_not_done') ? config('app.max_percentage_not_done') : $percentage; + } + + $activity->update([ + "persentase_progress" => $percentage, + "biaya_actual" => $biayaActual, + ]); + + $activity->save(); + }); + + static::deleted(function($data) { + + $activity = Activity::find($data->activity_id); + $assignedMaterial = AssignMaterial::find($data->assign_material_id); + + $activity->biaya_actual -= floatval($assignedMaterial->budget) * floatval($data->qty); + + $dataPlan = AssignMaterial::where('activity_id', $activity->id)->get(); + $totalPlan = $dataPlan->sum('qty_planning'); + $totalVolumeActual = ReportActivityMaterial::where('activity_id', '=', $activity->id)->sum("qty"); + $percentage = ($totalVolumeActual * 100) / $totalPlan; + $activity->persentase_progress = $percentage >= config('app.max_percentage_not_done') ? config('app.max_percentage_not_done') : $percentage; + $activity->save(); + + }); + + } +}