@ -4,50 +4,12 @@ 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){
$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();
}
$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){
$this->validate($request, [
@ -60,117 +22,57 @@ class AssignMaterialController extends Controller
$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();
return response()->json(['status'=>'failed','message'=>'Stock is not enough!','code'=> 500]);
}
$data = $request->all();
$data['created_by'] = $this->currentName;
$data['budget'] = $price;
$result = AssignMaterial::create($data);
if($result){
$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();
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;
if(!$result)
return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 500]);
$newBudget = $assignMaterial->budget > $data->budget ? $assignMaterial->budget : $data->budget ;
return response()->json(['status'=>'success','message'=>'Data added!', 'code'=>200]);
$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 = =""){
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){
if(!$data)
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
$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);
}
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);
}
public function delete($id)
{
$data = AssignMaterial::find($id );
$data = AssignMaterial::where('id', $id)->first( );
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);
}
$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($data->delete())
return response()->json(['status'=>'success','message'=>'Data deleted!','code'=> 200], 200);
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);
}
return response()->json(['status'=>'failed','message'=>'Failed to delete!','code'=> 500], 500);
}
public function edit($id){
if(!$id || (int) $id < 0 | | $ id = =""){
if(empty($id) || !is_int((int)$id))
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
die();
}
$result = AssignMaterial::find($id);
if($result){
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);
}
return response()->json(['status'=>'failed','message'=>'Data not found!','code'=> 404], 404);
}
public function search(Request $request)
@ -189,11 +91,10 @@ class AssignMaterialController extends Controller
$data = AssignMaterial::all();
$countData = $data->count();
if($data){
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);
}
return response()->json(['status'=>'failed','message'=>'Failed to get the data!','code'=> 500], 500);
}
public function datatables(Request $request){