You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
270 lines
12 KiB
270 lines
12 KiB
2 years ago
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers;
|
||
|
|
||
|
use Illuminate\Http\Request;
|
||
|
use App\Models\ReportActivityMaterial;
|
||
|
use App\Models\Activity;
|
||
|
use App\Models\AssignMaterial;
|
||
|
use Datatables;
|
||
|
|
||
|
class ReportActivityMaterialController extends Controller
|
||
|
{
|
||
|
|
||
|
public function add(Request $request){
|
||
|
//validasi
|
||
|
$this->validate($request, [
|
||
|
'activity_id' => 'required',
|
||
|
// 'assign_material_id' => 'required',
|
||
|
'qty' => 'required'
|
||
|
// 'budget' => 'required'
|
||
|
]);
|
||
|
|
||
|
$valAssignMaterialId = AssignMaterial::where('activity_id', $request->activity_id)->first();
|
||
|
|
||
|
$data = $request->all();
|
||
|
$data['created_by'] = $this->currentName;
|
||
|
$data['assign_material_id'] = $valAssignMaterialId->id;
|
||
|
|
||
|
$checkPrice = AssignMaterial::where("id", $valAssignMaterialId->id)->first();
|
||
|
$price = $checkPrice->budget;
|
||
|
|
||
|
$updateActivity = Activity::find($request->activity_id);
|
||
|
if($updateActivity){
|
||
|
$costActualOld = $updateActivity->biaya_actual == null ? 0 : (int)$updateActivity->biaya_actual;
|
||
|
$costActualNew = $costActualOld + ($price*$request->qty);
|
||
|
$updateActivity->biaya_actual = $costActualNew;
|
||
|
$updateActivity->save();
|
||
|
// return response()->json(['status'=>'success','message'=>'Assign material successfull created, and update cost plan success '.$costPlanNew,'code'=>200]);
|
||
|
$result = ReportActivityMaterial::create($data);
|
||
|
if($result){
|
||
|
$this->countForProgress($request->activity_id);
|
||
|
return response()->json(['status'=>'success','message'=>'Input progress report activity created','code'=>200]);
|
||
|
}else{
|
||
|
return response()->json(['status'=>'failed','message'=>'Input progress report activity failed created','code'=>400]);
|
||
|
}
|
||
|
}else{
|
||
|
return response()->json(['status'=>'success','message'=>'Update cost actual failed','code'=>400]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function updateStatusStartFinish(Request $request){
|
||
|
// $updateData = AssignMaterial::find($request->assign_material_id);
|
||
|
// if($updateData){
|
||
|
// $updateData->start_activity = $request->start_activity;
|
||
|
// $updateData->finish_activity = $request->finish_activity;
|
||
|
// $updateData->status_activity = $request->status_activity;
|
||
|
// $updateData->save();
|
||
|
// if($request->status_activity == 'done'){
|
||
|
// $dataUpdate = array(
|
||
|
// "persentase_progress" => 100,
|
||
|
// "updated_by" => $this->currentName
|
||
|
// );
|
||
|
// Activity::where('id', $updateData->activity_id)->update($dataUpdate);
|
||
|
// }
|
||
|
|
||
|
// return response()->json(['status'=>'success','message'=>'Update data status successfully ', 'code'=>200]);
|
||
|
// }
|
||
|
$payloadUpdate = array(
|
||
|
'start_activity' => $request->start_activity,
|
||
|
'finish_activity' => $request->finish_activity,
|
||
|
'status_activity' => $request->status_activity
|
||
|
);
|
||
|
$updateData = AssignMaterial::where('activity_id',$request->activity_id)
|
||
|
->update($payloadUpdate);
|
||
|
if($updateData){
|
||
|
// $updateData->start_activity = $request->start_activity;
|
||
|
// $updateData->finish_activity = $request->finish_activity;
|
||
|
// $updateData->status_activity = $request->status_activity;
|
||
|
// $updateData->save();
|
||
|
if($request->status_activity == 'done'){
|
||
|
$dataUpdate = array(
|
||
|
"persentase_progress" => 100,
|
||
|
"updated_by" => $this->currentName
|
||
|
);
|
||
|
Activity::where('id', $request->activity_id)->update($dataUpdate);
|
||
|
}
|
||
|
|
||
|
return response()->json(['status'=>'success','message'=>'Update data status successfully ', 'code'=>200]);
|
||
|
}
|
||
|
else{
|
||
|
return response()->json(['status'=>'failed','message'=>'Update data status failed', 'code'=>400]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function delete($id)
|
||
|
{
|
||
|
$data = ReportActivityMaterial::find($id);
|
||
|
|
||
|
if($data){
|
||
|
$delete = $data->delete();
|
||
|
}else{
|
||
|
return response()->json(['status'=>'failed','message'=>'data menu not found!','code'=>400], 400);
|
||
|
die();
|
||
|
}
|
||
|
|
||
|
|
||
|
if($delete){
|
||
|
return response()->json(['status'=>'success','message'=>'data menu successfully deleted!','code'=>200], 200);
|
||
|
}else{
|
||
|
return response()->json(['status'=>'failed','message'=>'data menu failed deleted!','code'=>400], 400);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function countForProgress($id)
|
||
|
{
|
||
|
|
||
|
// $totalJobs = ReportActivityMaterial::where('activity_id', $id)->sum("qty");
|
||
|
$dataActivity = Activity::find($id);
|
||
|
$dataPlan = AssignMaterial::where('activity_id', $id)->get();
|
||
|
$tmpPercentage1 = [];
|
||
|
if(!$dataPlan->isEmpty()){
|
||
|
|
||
|
foreach ($dataPlan as $value) {
|
||
|
# code...
|
||
|
$price = $value->material_id;
|
||
|
$qty_plan = $value->qty_planning;
|
||
|
$status_activity = $value->status_activity;
|
||
|
|
||
|
// get volume actual
|
||
|
$getDataVolActual = ReportActivityMaterial::where('assign_material_id', '=', $value->id)->sum("qty");
|
||
|
$percentage1 = ($getDataVolActual * 100) / $qty_plan;
|
||
|
|
||
|
if($status_activity == 'done'){
|
||
|
$tmpPercentage1[] = $percentage1 > 100 ? 100 : $percentage1;
|
||
|
}else if($status_activity == 'on-progress'){
|
||
|
$tmpPercentage1[] = $percentage1 >= 100 ? 90 : $percentage1;
|
||
|
}else{
|
||
|
$tmpPercentage1[] = $percentage1 >= 100 ? 90 : $percentage1;
|
||
|
}
|
||
|
}
|
||
|
$totalPercentage = array_sum($tmpPercentage1) / count($tmpPercentage1);
|
||
|
}else{
|
||
|
$totalPercentage = 0;
|
||
|
}
|
||
|
|
||
|
// if ($jobsActivity < 1) {
|
||
|
// return false;
|
||
|
// }
|
||
|
|
||
|
// if ($totalJobs < 1) {
|
||
|
// return false;
|
||
|
// }
|
||
|
|
||
|
// $persentaseActual = ($totalJobs/$jobsActivity)*100;
|
||
|
|
||
|
$dataUpdate = array(
|
||
|
"persentase_progress" => $totalPercentage,
|
||
|
"updated_by" => $this->currentName
|
||
|
);
|
||
|
return $dataActivity->update($dataUpdate);
|
||
|
}
|
||
|
|
||
|
public function countForProgressTest()
|
||
|
{
|
||
|
$dataPlan = AssignMaterial::where('activity_id', 807)->get();
|
||
|
// $jobsActivity = (int)$dataActivity->qty_planning;
|
||
|
$tmpPercentage1 = [];
|
||
|
|
||
|
if(!$dataPlan->isEmpty()){
|
||
|
foreach ($dataPlan as $value) {
|
||
|
# code...
|
||
|
// price per material
|
||
|
$price = $value->material_id;
|
||
|
$qty_plan = $value->qty_planning;
|
||
|
$status_activity = $value->status_activity;
|
||
|
|
||
|
// get volume actual
|
||
|
$getDataVolActual = ReportActivityMaterial::where('assign_material_id', '=', $value->id)->sum("qty");
|
||
|
$percentage1 = ($getDataVolActual * 100) / $qty_plan;
|
||
|
// if($qty_plan == null){
|
||
|
// $tmpPercentage1[] = 0
|
||
|
// }else{
|
||
|
$percentage1 = ($getDataVolActual * 100) / $qty_plan;
|
||
|
if($status_activity == 'done'){
|
||
|
$tmpPercentage1[] = $percentage1 > 100 ? 100 : $percentage1;
|
||
|
}else if($status_activity == 'on-progress'){
|
||
|
$tmpPercentage1[] = $percentage1 >= 100 ? 90 : $percentage1;
|
||
|
}else{
|
||
|
$tmpPercentage1[] = $percentage1 >= 100 ? 90 : $percentage1;
|
||
|
}
|
||
|
// }
|
||
|
|
||
|
|
||
|
}
|
||
|
$totalPercentage = array_sum($tmpPercentage1) / count($tmpPercentage1);
|
||
|
}else{
|
||
|
$totalPercentage = 0;
|
||
|
}
|
||
|
|
||
|
return response()->json(['status'=>'success','code'=>200,'data'=>$dataPlan, 'tmpPercentage1'=> $tmpPercentage1, 'totalPercentage'=> $totalPercentage], 200);
|
||
|
}
|
||
|
|
||
|
public function search(Request $request)
|
||
|
{
|
||
|
// payload same like in golang API oke
|
||
|
$payload = $request->all();
|
||
|
// second parameter is table name
|
||
|
$dataBuilder = $this->setUpPayload($payload, 'report_activity_material');
|
||
|
// builder for get data
|
||
|
$builder = $dataBuilder['builder'];
|
||
|
// builder for count data
|
||
|
$countBuilder = $dataBuilder['count'];
|
||
|
// execute builder
|
||
|
$dataGet = $builder->get();
|
||
|
// execute count builder
|
||
|
$totalRecord = $countBuilder->count();
|
||
|
// send response about the result
|
||
|
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200);
|
||
|
}
|
||
|
|
||
|
public function list()
|
||
|
{
|
||
|
$data = ReportActivityMaterial::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 Repport Activity material, please try again later!','code'=>400], 400);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function datatables(Request $request){
|
||
|
$id_activity = $request->query('idAct');
|
||
|
$id_assign_material_id= $request->query('idAmi');
|
||
|
$type = $request->query('type');
|
||
|
if($type == 'plan'){
|
||
|
$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)
|
||
|
->orderBy('assign_material_to_activity.id', 'asc')
|
||
|
->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-ram-delete"><i class="fa fa-trash"></i></a>';
|
||
|
return $actionBtn;
|
||
|
})
|
||
|
->rawColumns(['action'])->make(true);
|
||
|
}else{
|
||
|
// $data = AssignMaterial::select("assign_material_to_activity.*","m.name as material_name", "m.uom as uom")->join("m_material_resource as m", "m.id", "=", "assign_material_to_activity.material_id")->where('assign_material_to_activity.activity_id', $id_activity)->get();
|
||
|
$data = ReportActivityMaterial::select("report_activity_material.*", "u.name as human_resource")
|
||
|
// ->join("assign_material_to_activity as am_ta", "am_ta.id","=", "report_activity_material.assign_material_id")
|
||
|
// ->join("m_req_material as m", "m.id", "=", "am_ta.material_id")
|
||
|
->join("m_users as u", "u.id", "=", "report_activity_material.user_id")
|
||
|
// ->where('report_activity_material.assign_material_id', $id_assign_material_id)
|
||
|
->where('report_activity_material.activity_id', $id_activity)
|
||
|
->orderBy('report_activity_material.report_date', 'asc')
|
||
|
->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-ram-delete"><i class="fa fa-trash"></i></a>';
|
||
|
return $actionBtn;
|
||
|
})
|
||
|
->rawColumns(['action'])->make(true);
|
||
|
}
|
||
|
}
|
||
|
}
|