Browse Source

recalculate planned cost after deleting assigned material

pull/3/head
Muhammad Sulaiman Yusuf 2 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
);
$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]);

Loading…
Cancel
Save