From 12415c6e779e192e522674182124c9cb6f2314ce Mon Sep 17 00:00:00 2001 From: Muhammad Sulaiman Yusuf Date: Wed, 6 Jul 2022 17:09:44 +0700 Subject: [PATCH] recalculate planned cost after deleting assigned material --- .../Controllers/AssignMaterialController.php | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/app/Http/Controllers/AssignMaterialController.php b/app/Http/Controllers/AssignMaterialController.php index 404b653..f90651b 100644 --- a/app/Http/Controllers/AssignMaterialController.php +++ b/app/Http/Controllers/AssignMaterialController.php @@ -54,7 +54,7 @@ class AssignMaterialController extends Controller "updated_by"=>$this->currentName ); $checkStock->update($dataUpdate); - + return response()->json(['status'=>'success','message'=>'Assign material successfull created','code'=>200]); }else{ return response()->json(['status'=>'failed','message'=>'Assign material failed created','code'=>400]); @@ -113,11 +113,11 @@ class AssignMaterialController extends Controller private function updateFromAdd($data){ $assignMaterial = AssignMaterial::where("activity_id", $data->activity_id)->where("material_id", $data->material_id)->first(); - + $newQty = (int)$assignMaterial->qty_planning + (int)$data->qty_planning; - + $newBudget = $assignMaterial->budget > $data->budget ? $assignMaterial->budget : $data->budget; - + $dataUpdate = array( "qty_planning"=>$newQty, "budget"=>$newBudget, @@ -141,7 +141,7 @@ class AssignMaterialController extends Controller return response()->json(['status'=>'failed','message'=>'Data Assign material not found!','code'=>400], 400); die(); } - + if($result){ return response()->json(['status'=>'success','message'=>'Assign material successfully updated!','code'=>200], 200); }else{ @@ -166,12 +166,14 @@ class AssignMaterialController extends Controller ); $materialResource->update($dataUpdate); } + $activity = Activity::where('id', $data->activity_id)->first(); + $activity->rencana_biaya -= $data->budget * $data->qty_planning; + $activity->save(); $delete = $data->delete(); }else{ return response()->json(['status'=>'failed','message'=>'Data Assign material not found!','code'=>400], 400); die(); } - if($delete){ return response()->json(['status'=>'success','message'=>'Assign material successfully deleted!','code'=>200], 200); @@ -239,19 +241,19 @@ class AssignMaterialController extends Controller } public function datatablesForReportActivity(Request $request){ - $id_activity = $request->query('idact'); + $id_activity = $request->query('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") // // ->groupBy("assign_material_to_activity.activity_id") // ->where("assign_material_to_activity.activity_id", $id_activity)->get(); $data = AssignMaterial::select(AssignMaterial::raw('SUM(qty_planning) as qty_planning'), "m.description as material_name", "assign_material_to_activity.activity_id as activity_id") - ->join("m_req_material as m", "m.id", "=", "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") ->where("assign_material_to_activity.activity_id", $id_activity)->get(); - return Datatables::of($data) + return Datatables::of($data) ->addIndexColumn() - ->addColumn('qty_sum', function($row){ + ->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") @@ -260,28 +262,28 @@ 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; @@ -296,14 +298,14 @@ class AssignMaterialController extends Controller } public function ForReportActivityByMaterial(Request $request){ - $id_activity = $request->idact; + $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) { - // for ($i=0; $i < count($data); $i++) { + foreach ($data as $key) { + // for ($i=0; $i < count($data); $i++) { # code... - + $val_qty_sum = ReportActivityMaterial::where('assign_material_id', '=', $key->id)->sum("qty"); // $data[] = ['qty' =>$val_qty_sum ? $val_qty_sum : 0]; // array_push($data[$i], ['qty' =>$val_qty_sum ? $val_qty_sum : 0]);