Browse Source

recalculate planned cost after deleting assigned material

pull/3/head
Muhammad Sulaiman Yusuf 3 years ago
parent
commit
12415c6e77
  1. 38
      app/Http/Controllers/AssignMaterialController.php

38
app/Http/Controllers/AssignMaterialController.php

@ -54,7 +54,7 @@ class AssignMaterialController extends Controller
"updated_by"=>$this->currentName "updated_by"=>$this->currentName
); );
$checkStock->update($dataUpdate); $checkStock->update($dataUpdate);
return response()->json(['status'=>'success','message'=>'Assign material successfull created','code'=>200]); return response()->json(['status'=>'success','message'=>'Assign material successfull created','code'=>200]);
}else{ }else{
return response()->json(['status'=>'failed','message'=>'Assign material failed created','code'=>400]); return response()->json(['status'=>'failed','message'=>'Assign material failed created','code'=>400]);
@ -113,11 +113,11 @@ class AssignMaterialController extends Controller
private function updateFromAdd($data){ private function updateFromAdd($data){
$assignMaterial = AssignMaterial::where("activity_id", $data->activity_id)->where("material_id", $data->material_id)->first(); $assignMaterial = AssignMaterial::where("activity_id", $data->activity_id)->where("material_id", $data->material_id)->first();
$newQty = (int)$assignMaterial->qty_planning + (int)$data->qty_planning; $newQty = (int)$assignMaterial->qty_planning + (int)$data->qty_planning;
$newBudget = $assignMaterial->budget > $data->budget ? $assignMaterial->budget : $data->budget; $newBudget = $assignMaterial->budget > $data->budget ? $assignMaterial->budget : $data->budget;
$dataUpdate = array( $dataUpdate = array(
"qty_planning"=>$newQty, "qty_planning"=>$newQty,
"budget"=>$newBudget, "budget"=>$newBudget,
@ -141,7 +141,7 @@ class AssignMaterialController extends Controller
return response()->json(['status'=>'failed','message'=>'Data Assign material not found!','code'=>400], 400); return response()->json(['status'=>'failed','message'=>'Data Assign material not found!','code'=>400], 400);
die(); die();
} }
if($result){ if($result){
return response()->json(['status'=>'success','message'=>'Assign material successfully updated!','code'=>200], 200); return response()->json(['status'=>'success','message'=>'Assign material successfully updated!','code'=>200], 200);
}else{ }else{
@ -166,12 +166,14 @@ class AssignMaterialController extends Controller
); );
$materialResource->update($dataUpdate); $materialResource->update($dataUpdate);
} }
$activity = Activity::where('id', $data->activity_id)->first();
$activity->rencana_biaya -= $data->budget * $data->qty_planning;
$activity->save();
$delete = $data->delete(); $delete = $data->delete();
}else{ }else{
return response()->json(['status'=>'failed','message'=>'Data Assign material not found!','code'=>400], 400); return response()->json(['status'=>'failed','message'=>'Data Assign material not found!','code'=>400], 400);
die(); die();
} }
if($delete){ if($delete){
return response()->json(['status'=>'success','message'=>'Assign material successfully deleted!','code'=>200], 200); 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){ 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") // $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") // ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id")
// // ->groupBy("assign_material_to_activity.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();
$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") $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("m.description")
->groupBy("assign_material_to_activity.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) return Datatables::of($data)
->addIndexColumn() ->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") $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("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") ->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(); ->where("ram.activity_id", $row->activity_id)->first();
return $val_qty_act ? $val_qty_act->qty_sum : '-'; 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") $val_status = AssignMaterial::select("status_activity")
->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')
->where('m.description', '=', $row->material_name) ->where('m.description', '=', $row->material_name)
->where("assign_material_to_activity.activity_id", $row->activity_id)->first(); ->where("assign_material_to_activity.activity_id", $row->activity_id)->first();
return $val_status ? $val_status->status_activity : null; return $val_status ? $val_status->status_activity : null;
}) })
->addColumn('start_activity', function($row){ ->addColumn('start_activity', function($row){
$val_start = AssignMaterial::select("start_activity") $val_start = AssignMaterial::select("start_activity")
->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')
->where('m.description', '=', $row->material_name) ->where('m.description', '=', $row->material_name)
->where("assign_material_to_activity.activity_id", $row->activity_id)->first(); ->where("assign_material_to_activity.activity_id", $row->activity_id)->first();
return $val_start ? $val_start->start_activity : null; return $val_start ? $val_start->start_activity : null;
}) })
->addColumn('finish_activity', function($row){ ->addColumn('finish_activity', function($row){
$val_finish = AssignMaterial::select("finish_activity") $val_finish = AssignMaterial::select("finish_activity")
->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')
->where('m.description', '=', $row->material_name) ->where('m.description', '=', $row->material_name)
->where("assign_material_to_activity.activity_id", $row->activity_id)->first(); ->where("assign_material_to_activity.activity_id", $row->activity_id)->first();
return $val_finish ? $val_finish->finish_activity : null; return $val_finish ? $val_finish->finish_activity : null;
}) })
->addColumn('uom', function($row){ ->addColumn('uom', function($row){
$val_uom = RequestMaterial::select("uom") $val_uom = RequestMaterial::select("uom")
->where('description', '=', $row->material_name)->first(); ->where('description', '=', $row->material_name)->first();
return $val_uom ? $val_uom->uom : null; return $val_uom ? $val_uom->uom : null;
@ -296,14 +298,14 @@ class AssignMaterialController extends Controller
} }
public function ForReportActivityByMaterial(Request $request){ 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") $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") ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id")
->where('assign_material_to_activity.activity_id', $id_activity)->get(); ->where('assign_material_to_activity.activity_id', $id_activity)->get();
foreach ($data as $key) { foreach ($data as $key) {
// for ($i=0; $i < count($data); $i++) { // for ($i=0; $i < count($data); $i++) {
# code... # code...
$val_qty_sum = ReportActivityMaterial::where('assign_material_id', '=', $key->id)->sum("qty"); $val_qty_sum = ReportActivityMaterial::where('assign_material_id', '=', $key->id)->sum("qty");
// $data[] = ['qty' =>$val_qty_sum ? $val_qty_sum : 0]; // $data[] = ['qty' =>$val_qty_sum ? $val_qty_sum : 0];
// array_push($data[$i], ['qty' =>$val_qty_sum ? $val_qty_sum : 0]); // array_push($data[$i], ['qty' =>$val_qty_sum ? $val_qty_sum : 0]);

Loading…
Cancel
Save