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.
326 lines
16 KiB
326 lines
16 KiB
2 years ago
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers;
|
||
|
|
||
|
use Illuminate\Http\Request;
|
||
|
use App\Models\AssignMaterial;
|
||
|
use App\Models\MaterialResource;
|
||
|
use App\Models\RequestMaterial;
|
||
|
use App\Models\Activity;
|
||
|
use App\Models\ReportActivityMaterial;
|
||
|
use Datatables;
|
||
|
|
||
|
class AssignMaterialController extends Controller
|
||
|
{
|
||
|
public function add_backup(Request $request){
|
||
|
//validasi
|
||
|
$this->validate($request, [
|
||
|
'activity_id' => 'required',
|
||
|
'material_id' => 'required',
|
||
|
'qty_planning' => 'required',
|
||
|
'budget' => 'required'
|
||
|
]);
|
||
|
|
||
|
$checkStock = MaterialResource::where("id", $request->material_id)->first();
|
||
|
$currentStock = $checkStock->qty;
|
||
|
if((int)$currentStock < (int)$request->qty_planning){
|
||
|
return response()->json(['status'=>'failed','message'=>'Not enough stock in warehouse!','code'=>400]);
|
||
|
die();
|
||
|
}
|
||
|
|
||
|
// $checkData = AssignMaterial::where("activity_id", $request->activity_id)->where("material_id", $request->material_id)
|
||
|
// ->count();
|
||
|
|
||
|
// if($checkData > 0){
|
||
|
// $update = $this->updateFromAdd($request->all());
|
||
|
// if($update){
|
||
|
// return response()->json(['status'=>'success','message'=>'Assign material success!','code'=>200]);
|
||
|
// }else{
|
||
|
// return response()->json(['status'=>'failed','message'=>'Assign material failed','code'=>400]);
|
||
|
// }
|
||
|
// die();
|
||
|
// }
|
||
|
|
||
|
$data = $request->all();
|
||
|
|
||
|
$data['created_by'] = $this->currentName;
|
||
|
|
||
|
$result = AssignMaterial::create($data);
|
||
|
if($result){
|
||
|
$checkStock = MaterialResource::find($request->material_id);
|
||
|
$newStock = (int)$checkStock->qty - (int)$request->qty_planning;
|
||
|
$dataUpdate = array(
|
||
|
"qty"=>$newStock,
|
||
|
"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]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function add(Request $request){
|
||
|
//validasi
|
||
|
$this->validate($request, [
|
||
|
'activity_id' => 'required',
|
||
|
'material_id' => 'required',
|
||
|
'qty_planning' => 'required'
|
||
|
// 'budget' => 'required'
|
||
|
]);
|
||
|
|
||
|
$checkStock = RequestMaterial::where("id", $request->material_id)->first();
|
||
|
$currentStock = $checkStock->qty;
|
||
|
$price = $checkStock->price;
|
||
|
if((int)$currentStock < (int)$request->qty_planning){
|
||
|
return response()->json(['status'=>'failed','message'=>'Not enough stock !','code'=>400]);
|
||
|
die();
|
||
|
}
|
||
|
|
||
|
$data = $request->all();
|
||
|
|
||
|
$data['created_by'] = $this->currentName;
|
||
|
$data['budget'] = $price;
|
||
|
|
||
|
$result = AssignMaterial::create($data);
|
||
|
if($result){
|
||
|
// comment untill fix flow
|
||
|
// $checkStock = MaterialResource::find($request->material_id);
|
||
|
// $newStock = (int)$checkStock->qty - (int)$request->qty_planning;
|
||
|
// $dataUpdate = array(
|
||
|
// "qty"=>$newStock,
|
||
|
// "updated_by"=>$this->currentName
|
||
|
// );
|
||
|
// $checkStock->update($dataUpdate);
|
||
|
|
||
|
// update cost planning
|
||
|
$updateActivity = Activity::find($request->activity_id);
|
||
|
if($updateActivity){
|
||
|
$costPlanOld = $updateActivity==null? 0 : (int)$updateActivity->rencana_biaya;
|
||
|
$costPlanNew = $costPlanOld + ($price*$request->qty_planning);
|
||
|
$updateActivity->rencana_biaya = $costPlanNew;
|
||
|
$updateActivity->save();
|
||
|
// $updateActivity->update(array("rencana_biaya"=>$costPlanNew));
|
||
|
return response()->json(['status'=>'success','message'=>'Assign material successfull created, and update cost plan success '.$costPlanNew,'code'=>200]);
|
||
|
}else{
|
||
|
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]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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,
|
||
|
"updated_by"=>$this->currentName
|
||
|
);
|
||
|
|
||
|
$dataWillUpdate = AssignMaterial::where("activity_id", $data->activity_id)->where("material_id", $data->material_id);
|
||
|
|
||
|
return $dataWillUpdate->update($dataUpdate);
|
||
|
}
|
||
|
|
||
|
public function update(Request $request, $id){
|
||
|
|
||
|
if(!$id || (int) $id < 0 || $id==""){
|
||
|
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
|
||
|
}
|
||
|
$data = AssignMaterial::find($id);
|
||
|
if($data){
|
||
|
$result = $data->update($request->all());
|
||
|
}else{
|
||
|
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{
|
||
|
return response()->json(['status'=>'failed','message'=>'Assign material failed updated!','code'=>400], 400);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function delete($id)
|
||
|
{
|
||
|
$data = AssignMaterial::find($id);
|
||
|
|
||
|
if($data){
|
||
|
$id = $data->material_id;
|
||
|
$stock = $data->qty_planning;
|
||
|
$materialResource = MaterialResource::find($id);
|
||
|
if($materialResource){
|
||
|
$oldStock = $materialResource->qty;
|
||
|
$newStock = $oldStock + $stock;
|
||
|
$dataUpdate = array(
|
||
|
"qty"=>$newStock,
|
||
|
"updated_by"=>$this->currentName
|
||
|
);
|
||
|
$materialResource->update($dataUpdate);
|
||
|
}
|
||
|
$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);
|
||
|
}else{
|
||
|
return response()->json(['status'=>'failed','message'=>'Assign material failed deleted!','code'=>400], 400);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function edit($id){
|
||
|
if(!$id || (int) $id < 0 || $id==""){
|
||
|
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
|
||
|
die();
|
||
|
}
|
||
|
|
||
|
$result = AssignMaterial::find($id);
|
||
|
|
||
|
if($result){
|
||
|
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200);
|
||
|
}else{
|
||
|
return response()->json(['status'=>'failed','message'=>'failed get data Assign material, please try again later!','code'=>400], 400);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public function search(Request $request)
|
||
|
{
|
||
|
// payload same like in golang API oke
|
||
|
$payload = $request->all();
|
||
|
// second parameter is table name
|
||
|
$dataBuilder = $this->setUpPayload($payload, 'assign_material_to_activity');
|
||
|
// 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 = 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.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 = 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("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")
|
||
|
->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>';
|
||
|
// $actionBtn .= '<a href="javascript:void(0)" style="margin-left:5px;" data-id="'.$row->id.'" data-material-name="'.$row->material_name.'" class="success btn btn-success btn-sm btn-update-status" data-toggle="tooltip" title="Input Update Status" data-placement="top"><i class="fa fa-edit"></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) {
|
||
|
// 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]);
|
||
|
}
|
||
|
// $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, function ($query) {
|
||
|
// $query->sum('qty')
|
||
|
// ->from('report_activity_material')
|
||
|
// ->where('report_activity_material.assign_material_id', 'assign_material_to_activity.id');
|
||
|
// })->get();
|
||
|
$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);
|
||
|
}
|
||
|
}
|
||
|
}
|