Browse Source

clean codes & move logics to model

pull/3/head
Muhammad Sulaiman Yusuf 2 years ago
parent
commit
e547e80250
  1. 433
      app/Http/Controllers/AssignMaterialController.php

433
app/Http/Controllers/AssignMaterialController.php

@ -4,277 +4,178 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\AssignMaterial; use App\Models\AssignMaterial;
use App\Models\MaterialResource;
use App\Models\RequestMaterial; use App\Models\RequestMaterial;
use App\Models\Activity;
use App\Models\ReportActivityMaterial; use App\Models\ReportActivityMaterial;
use Datatables; use Datatables;
class AssignMaterialController extends Controller class AssignMaterialController extends Controller
{ {
public function add_backup(Request $request){
$this->validate($request, [
'activity_id' => 'required',
'material_id' => 'required',
'qty_planning' => 'required',
'budget' => 'required'
]);
$checkStock = MaterialResource::where("id", $request->material_id)->first(); public function add(Request $request){
$currentStock = $checkStock->qty; $this->validate($request, [
if((int)$currentStock < (int)$request->qty_planning){ 'activity_id' => 'required',
return response()->json(['status'=>'failed','message'=>'Not enough stock in warehouse!','code'=>400]); 'material_id' => 'required',
die(); 'qty_planning' => 'required'
} ]);
$checkStock = RequestMaterial::where("id", $request->material_id)->first();
$currentStock = $checkStock->qty;
$data = $request->all(); $price = $checkStock->price;
if((int)$currentStock < (int)$request->qty_planning){
$data['created_by'] = $this->currentName; return response()->json(['status'=>'failed','message'=>'Stock is not enough!','code'=> 500]);
}
$result = AssignMaterial::create($data);
if($result){ $data = $request->all();
$checkStock = MaterialResource::find($request->material_id); $data['created_by'] = $this->currentName;
$newStock = (int)$checkStock->qty - (int)$request->qty_planning; $data['budget'] = $price;
$dataUpdate = array(
"qty"=>$newStock, $result = AssignMaterial::create($data);
"updated_by"=>$this->currentName if(!$result)
); return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 500]);
$checkStock->update($dataUpdate);
return response()->json(['status'=>'success','message'=>'Data added!', 'code'=>200]);
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]);
} 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 add(Request $request){
$this->validate($request, [ $data = AssignMaterial::find($id);
'activity_id' => 'required', if(!$data)
'material_id' => 'required', return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
'qty_planning' => 'required'
]); $result = $data->update($request->all());
$checkStock = RequestMaterial::where("id", $request->material_id)->first(); if($result)
$currentStock = $checkStock->qty; return response()->json(['status'=>'success','message'=> 'Data updated!','code'=> 200], 200);
$price = $checkStock->price;
if((int)$currentStock < (int)$request->qty_planning){ return response()->json(['status'=>'failed','message'=>'Failed to update!','code'=> 500], 500);
return response()->json(['status'=>'failed','message'=>'Not enough stock !','code'=>400]); }
die();
} public function delete($id)
{
$data = $request->all(); $data = AssignMaterial::where('id', $id)->first();
$data['created_by'] = $this->currentName; if($data->delete())
$data['budget'] = $price; return response()->json(['status'=>'success','message'=>'Data deleted!','code'=> 200], 200);
$result = AssignMaterial::create($data); return response()->json(['status'=>'failed','message'=>'Failed to delete!','code'=> 500], 500);
if($result){ }
$updateActivity = Activity::find($request->activity_id); public function edit($id){
if($updateActivity){ if(empty($id) || !is_int((int)$id))
$costPlanOld = $updateActivity==null? 0 : (int)$updateActivity->rencana_biaya; return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
$costPlanNew = $costPlanOld + ($price*$request->qty_planning);
$updateActivity->rencana_biaya = $costPlanNew; $result = AssignMaterial::find($id);
$updateActivity->save();
return response()->json(['status'=>'success','message'=>'Assign material successfull created, and update cost plan success '.$costPlanNew,'code'=>200]); if($result)
}else{ return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200);
return response()->json(['status'=>'success','message'=>'Assign material successfull created','code'=>200]);
} return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
}else{ }
return response()->json(['status'=>'failed','message'=>'Assign material failed created','code'=>400]);
} public function search(Request $request)
} {
$payload = $request->all();
private function updateFromAdd($data){ $dataBuilder = $this->setUpPayload($payload, 'assign_material_to_activity');
$assignMaterial = AssignMaterial::where("activity_id", $data->activity_id)->where("material_id", $data->material_id)->first(); $builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count'];
$newQty = (int)$assignMaterial->qty_planning + (int)$data->qty_planning; $dataGet = $builder->get();
$totalRecord = $countBuilder->count();
$newBudget = $assignMaterial->budget > $data->budget ? $assignMaterial->budget : $data->budget; return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200);
}
$dataUpdate = array(
"qty_planning"=>$newQty, public function list()
"budget"=>$newBudget, {
"updated_by"=>$this->currentName $data = AssignMaterial::all();
); $countData = $data->count();
$dataWillUpdate = AssignMaterial::where("activity_id", $data->activity_id)->where("material_id", $data->material_id); if($data)
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200);
return $dataWillUpdate->update($dataUpdate);
} return response()->json(['status'=>'failed','message'=>'Failed to get the data!','code'=> 500], 500);
}
public function update(Request $request, $id){
public function datatables(Request $request){
if(!$id || (int) $id < 0 || $id==""){ $id_activity = $request->query('idact');
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); $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();
} return Datatables::of($data)
$data = AssignMaterial::find($id); ->addIndexColumn()
if($data){ ->addColumn('action', function($row){
$result = $data->update($request->all()); $actionBtn = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="delete btn btn-danger btn-sm btn-material-delete"><i class="fa fa-trash"></i></a>';
}else{ return $actionBtn;
return response()->json(['status'=>'failed','message'=>'Data Assign material not found!','code'=>400], 400); })
die(); ->rawColumns(['action'])->make(true);
} }
if($result){ public function datatablesForReportActivity(Request $request){
return response()->json(['status'=>'success','message'=>'Assign material successfully updated!','code'=>200], 200); $id_activity = $request->query('idact');
}else{ $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")
return response()->json(['status'=>'failed','message'=>'Assign material failed updated!','code'=>400], 400); ->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();
public function delete($id) return Datatables::of($data)
{ ->addIndexColumn()
$data = AssignMaterial::find($id); ->addColumn('qty_sum', function($row){
$val_qty_act = AssignMaterial::select(ReportActivityMaterial::raw('SUM(ram.qty) as qty_sum'),"m.description as material_name1")
if($data){ ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id")
$id = $data->material_id; ->join("report_activity_material as ram", "ram.assign_material_id", "=", "assign_material_to_activity.id")
$stock = $data->qty_planning; ->groupBy("m.description")
$materialResource = MaterialResource::find($id); ->where("m.description", strval($row->material_name))
if($materialResource){ ->where("ram.activity_id", $row->activity_id)->first();
$oldStock = $materialResource->qty; return $val_qty_act ? $val_qty_act->qty_sum : '-';
$newStock = $oldStock + $stock; })
$dataUpdate = array( ->addColumn('status_activity', function($row){
"qty"=>$newStock, $val_status = AssignMaterial::select("status_activity")
"updated_by"=>$this->currentName ->join('m_req_material as m', 'm.id', '=', 'assign_material_to_activity.material_id')
); ->where('m.description', '=', $row->material_name)
$materialResource->update($dataUpdate); ->where("assign_material_to_activity.activity_id", $row->activity_id)->first();
} return $val_status ? $val_status->status_activity : null;
$activity = Activity::where('id', $data->activity_id)->first(); })
$activity->rencana_biaya -= $data->budget * $data->qty_planning; ->addColumn('start_activity', function($row){
$activity->save(); $val_start = AssignMaterial::select("start_activity")
$delete = $data->delete(); ->join('m_req_material as m', 'm.id', '=', 'assign_material_to_activity.material_id')
}else{ ->where('m.description', '=', $row->material_name)
return response()->json(['status'=>'failed','message'=>'Data Assign material not found!','code'=>400], 400); ->where("assign_material_to_activity.activity_id", $row->activity_id)->first();
die(); return $val_start ? $val_start->start_activity : null;
} })
->addColumn('finish_activity', function($row){
if($delete){ $val_finish = AssignMaterial::select("finish_activity")
return response()->json(['status'=>'success','message'=>'Assign material successfully deleted!','code'=>200], 200); ->join('m_req_material as m', 'm.id', '=', 'assign_material_to_activity.material_id')
}else{ ->where('m.description', '=', $row->material_name)
return response()->json(['status'=>'failed','message'=>'Assign material failed deleted!','code'=>400], 400); ->where("assign_material_to_activity.activity_id", $row->activity_id)->first();
} return $val_finish ? $val_finish->finish_activity : null;
} })
->addColumn('uom', function($row){
public function edit($id){ $val_uom = RequestMaterial::select("uom")
if(!$id || (int) $id < 0 || $id==""){ ->where('description', '=', $row->material_name)->first();
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); return $val_uom ? $val_uom->uom : null;
die(); })
} ->addColumn('action', function($row){
$actionBtn = '<a href="javascript:void(0)" data-id="'.$row->id.'" data-material-name="'.$row->material_name.'" class="primary btn btn-primary btn-sm btn-lihat-plan" data-toggle="tooltip" title="Lihat Plan" data-placement="top"><i class="fa fa-align-justify"></i></a>';
$result = AssignMaterial::find($id); $actionBtn .= '<a href="javascript:void(0)" data-id="'.$row->id.'" data-material-name="'.$row->material_name.'" class="warning btn btn-warning btn-sm btn-lihat-actual" data-toggle="tooltip" title="Input Progress" data-placement="top"><i class="fa fa-edit" aria-hidden="true"></i></a>';
return $actionBtn;
if($result){ })
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); ->rawColumns(['action'])->make(true);
}else{ }
return response()->json(['status'=>'failed','message'=>'failed get data Assign material, please try again later!','code'=>400], 400);
} 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")
public function search(Request $request) ->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id")
{ ->where('assign_material_to_activity.activity_id', $id_activity)->get();
$payload = $request->all(); foreach ($data as $key) {
$dataBuilder = $this->setUpPayload($payload, 'assign_material_to_activity'); # code...
$builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count']; $val_qty_sum = ReportActivityMaterial::where('assign_material_id', '=', $key->id)->sum("qty");
$dataGet = $builder->get(); }
$totalRecord = $countBuilder->count(); $countData = $data->count();
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); if($data){
} return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200);
}else{
public function list() return response()->json(['status'=>'failed','message'=>'failed get Assign material, please try again later!','code'=>400], 400);
{ }
$data = AssignMaterial::all(); }
$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);
}
}
public function datatables(Request $request){
$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")->where('assign_material_to_activity.activity_id', $id_activity)->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$actionBtn = '<a href="javascript:void(0)" data-id="'.$row->id.'" class="delete btn btn-danger btn-sm btn-material-delete"><i class="fa fa-trash"></i></a>';
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 as activity_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)
->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('action', function($row){
$actionBtn = '<a href="javascript:void(0)" data-id="'.$row->id.'" data-material-name="'.$row->material_name.'" class="primary btn btn-primary btn-sm btn-lihat-plan" data-toggle="tooltip" title="Lihat Plan" data-placement="top"><i class="fa fa-align-justify"></i></a>';
$actionBtn .= '<a href="javascript:void(0)" data-id="'.$row->id.'" data-material-name="'.$row->material_name.'" class="warning btn btn-warning btn-sm btn-lihat-actual" data-toggle="tooltip" title="Input Progress" data-placement="top"><i class="fa fa-edit" aria-hidden="true"></i></a>';
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) {
# code...
$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);
}
}
} }

Loading…
Cancel
Save