Browse Source

cleanse code

pull/3/head
Muhammad Sulaiman Yusuf 2 years ago
parent
commit
82e1bd33ae
  1. 122
      app/Http/Controllers/RequestMaterialController.php

122
app/Http/Controllers/RequestMaterialController.php

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

Loading…
Cancel
Save