@ -82,74 +82,66 @@ class RequestMaterialController extends Controller
public function update(Request $request, $id)
public function update(Request $request, $id)
{
{
$dataForm = $request->all();
$dataForm = $request->all();
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);
return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
die();
}
$reqMaterial = RequestMaterial::find($id);
$data = RequestMaterial::find($id);
if(!$reqMaterial)
if($data){
return response()->json(['status'=>'failed','message'=>'data request material not found!','code'=>400], 400);
if(!isset($dataForm['status'])){
if(!isset($dataForm['status'])){
$resultUpdate = $data->update($request->all());
if(!$reqMaterial->update($request->all()))
if($resultUpdate){
return response()->json(['status'=>'success','message'=>'request material successfully updated!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'request material failed updated!','code'=>400], 400);
return response()->json(['status'=>'failed','message'=>'request material failed updated!','code'=>400], 400);
return response()->json(['status'=>'success','message'=>'request material successfully updated!','code'=>200], 200);
}
}
die();
}
if($dataForm['status']=="receipt to warehouse"){
if($dataForm['status']=="receipt to warehouse"){
$dataM = MaterialResource::where('name', 'ilike', $data ->description)->first();
$materialResource = MaterialResource::where('name', 'ilike', $reqMaterial ->description)->first();
if($dataM ){
if($materialResource ){
$result = $data ->update($request->all());
$reqMaterial ->update($request->all());
$payloadMQty = $dataM->qty + $data ->qty_received;
$payloadMQty = $materialResource->qty + $reqMaterial ->qty_received;
$resultUpdateM = MaterialResource::where('id', $dataM ->id)->update(['qty' => $payloadMQty]);
MaterialResource::where('id', $materialResource ->id)->update(['qty' => $payloadMQty]);
return response()->json(['status'=>'success', 'code'=>200], 200);
return response()->json(['status'=>'success', 'code'=>200], 200);
}else{
}
$result = $data->update($request->all());
$dataPayloadM = array(
$reqMaterial->update($request->all());
$preparedMaterialResource = array(
"name" => $dataForm['description'],
"name" => $dataForm['description'],
"uom" => $dataForm['uom'],
"uom" => $dataForm['uom'],
"unit_price" => 0,
"unit_price" => 0,
"description" => $dataForm['description'],
"description" => $dataForm['description'],
"qty" => $data ->qty_received,
"qty" => $reqMaterial ->qty_received,
"status" => "Barang tersedia",
"status" => "Barang tersedia",
"created_by" => $this->currentName
"created_by" => $this->currentName
);
);
$result = MaterialResource::create($dataPayloadM);
if($result){
if(!MaterialResource::create($preparedMaterialResource ))
return response()->json(['status'=>'success','message'=>'add data material resource successfully!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'add data material resource failed!','code'=>400], 400);
return response()->json(['status'=>'failed','message'=>'add data material resource failed!','code'=>400], 400);
}
return response()->json(['status'=>'success','message'=>'add data material resource successfully!','code'=>200], 200);
}
}
}else if($dataForm['status']=="receipt to site"){
if($dataForm['status']=="receipt to site"){
$result = $data->update($request->all());
$reqMaterial->update($request->all());
$dataM = MaterialResource::where('name', 'ilike', $data->description)->first();
if($dataM){
$result = $data->update($request->all());
$payloadMQty = $dataM->qty - $data->qty_received;
if($payloadMQty < 0 ) {
$materialResource = MaterialResource::where('name', 'ilike', $reqMaterial->description)->first();
if(!$materialResource)
return response()->json(['status'=>'failed','message'=>'update status receipt to site request material resource failed, because material is not exist in warehouse!','code'=>400], 400);
return response()->json(['status'=>'failed','message'=>'update status receipt to site request material resource failed, because material is not exist in warehouse!','code'=>400], 400);
die();
}
$reqMaterial->update($request->all());
$resultUpdateM = MaterialResource::where('id', $dataM->id)->update(['qty' => $payloadMQty]) ;
$payloadMQty = $materialResource->qty - $reqMaterial->qty_received ;
return response()->json(['status'=>'success', 'message'=>'request material status receipt to site successfully updated!','code'=>200], 200);
}else{
if($payloadMQty < 0 )
return response()->json(['status'=>'failed','message'=>'update status receipt to site request material resource failed, because material is not exist in warehouse!','code'=>400], 400);
return response()->json(['status'=>'failed','message'=>'update status receipt to site request material resource failed, because material is not exist in warehouse!','code'=>400], 400);
MaterialResource::where('id', $materialResource->id)->update(['qty' => $payloadMQty]);
return response()->json(['status'=>'success', 'message'=>'request material status receipt to site successfully updated!','code'=>200], 200);
}
}
}else{
$resultUpdate = $data->update($request->all());
if(!$reqMaterial->update($request->all()))
if($resultUpdate){
return response()->json(['status'=>'success','message'=>'request material successfully updated!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'request material failed updated!','code'=>400], 400);
return response()->json(['status'=>'failed','message'=>'request material failed updated!','code'=>400], 400);
}
}
return response()->json(['status'=>'success','message'=>'request material successfully updated!','code'=>200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'data request material not found!','code'=>400], 400);
die();
}
}
}
}
}