Browse Source

Merge pull request 'update filter datatables' (#38) from Dev-Farhan into staging

Reviewed-on: ibnu/generic-ospro-backend#38
pull/1/head
farhantock 11 months ago
parent
commit
d46ed84025
  1. 127
      app/Http/Controllers/AssignMaterialController.php

127
app/Http/Controllers/AssignMaterialController.php

@ -12,13 +12,15 @@ use Datatables;
class AssignMaterialController extends Controller
{
private function sanitizeDecimal($number) {
$number = str_replace(".","",$number);
$number = str_replace(",",".",$number);
private function sanitizeDecimal($number)
{
$number = str_replace(".", "", $number);
$number = str_replace(",", ".", $number);
return $number;
}
public function add(Request $request){
public function add(Request $request)
{
$this->validate($request, [
'activity_id' => 'required',
'material_id' => 'required',
@ -29,8 +31,8 @@ class AssignMaterialController extends Controller
$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]);
if ((int)$currentStock < (int)$request->qty_planning) {
return response()->json(['status' => 'failed', 'message' => 'Stock is not enough!', 'code' => 500]);
}
$start_date = $activity->start_date;
@ -45,26 +47,27 @@ class AssignMaterialController extends Controller
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);
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);
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);
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);
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);
return response()->json(['status' => 'failed', 'message' => 'Failed to update!', 'code' => 500], 500);
}
public function delete($id)
@ -77,22 +80,23 @@ class AssignMaterialController extends Controller
}
}
if($data->delete())
return response()->json(['status'=>'success','message'=>'Data deleted!','code'=> 200], 200);
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);
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);
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);
if ($result)
return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200);
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
}
public function search(Request $request)
@ -103,7 +107,7 @@ class AssignMaterialController extends Controller
$countBuilder = $dataBuilder['count'];
$dataGet = $builder->get();
$totalRecord = $countBuilder->count();
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200);
return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200);
}
public function list()
@ -111,17 +115,20 @@ class AssignMaterialController extends Controller
$data = AssignMaterial::all();
$countData = $data->count();
if($data)
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200);
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);
return response()->json(['status' => 'failed', 'message' => 'Failed to get the data!', 'code' => 500], 500);
}
public function datatables(Request $request){
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"
"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)
@ -130,28 +137,33 @@ class AssignMaterialController extends Controller
->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>';
->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){
public function datatablesForReportActivity(Request $request)
{
$id_activity = $request->query('idact');
$data =
AssignMaterial::select(
$query = AssignMaterial::select(
DB::raw('(SELECT SUM(assign_material_to_activity.qty_planning) FROM assign_material_to_activity WHERE assign_material_to_activity.activity_id = ram.activity_id) as qty_planning'),
DB::raw('SUM(ram.qty) as qty_sum'),
"m.description as material_name",
"assign_material_to_activity.activity_id",
"assign_material_to_activity.activity_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")
->groupBy("m.description")
->join("m_req_material as m", "m.id", "=", "assign_material_to_activity.material_id");
if (!AssignMaterial::join("report_activity_material as ram", "ram.assign_material_id", "=", "assign_material_to_activity.id")->where("assign_material_to_activity.activity_id", $id_activity)->exists()) {
$query->leftJoin("report_activity_material as ram", "ram.assign_material_id", "=", "assign_material_to_activity.id");
} else {
$query->join("report_activity_material as ram", "ram.assign_material_id", "=", "assign_material_to_activity.id");
}
$data = $query->groupBy("m.description")
->groupBy("ram.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)
->addIndexColumn()
// ->addColumn('qty_planning', function($row){
@ -164,60 +176,61 @@ 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;
})
->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")
->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){
->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 = '<a href="javascript:void(0)" data-id="'.$dataPlan->id.'" data-activity_id="'.$row->activity_id.'" data-material-id="'.$row->material_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="'.$dataPlan->id.'" data-activity_id="'.$row->activity_id.'" data-material-id="'.$row->material_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>';
$actionBtn = '<a href="javascript:void(0)" data-id="' . $dataPlan->id . '" data-activity_id="' . $row->activity_id . '" data-material-id="' . $row->material_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="' . $dataPlan->id . '" data-activity_id="' . $row->activity_id . '" data-material-id="' . $row->material_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){
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")
$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);
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