Browse Source

clean codes & move logics to model

pull/3/head
Muhammad Sulaiman Yusuf 2 years ago
parent
commit
e547e80250
  1. 147
      app/Http/Controllers/AssignMaterialController.php

147
app/Http/Controllers/AssignMaterialController.php

@ -4,50 +4,12 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\AssignMaterial; use App\Models\AssignMaterial;
use App\Models\MaterialResource;
use App\Models\RequestMaterial; use App\Models\RequestMaterial;
use App\Models\Activity;
use App\Models\ReportActivityMaterial; use App\Models\ReportActivityMaterial;
use Datatables; use Datatables;
class AssignMaterialController extends Controller 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){ public function add(Request $request){
$this->validate($request, [ $this->validate($request, [
@ -60,117 +22,57 @@ class AssignMaterialController extends Controller
$currentStock = $checkStock->qty; $currentStock = $checkStock->qty;
$price = $checkStock->price; $price = $checkStock->price;
if((int)$currentStock < (int)$request->qty_planning){ if((int)$currentStock < (int)$request->qty_planning){
return response()->json(['status'=>'failed','message'=>'Not enough stock !','code'=>400]); return response()->json(['status'=>'failed','message'=>'Stock is not enough!','code'=> 500]);
die();
} }
$data = $request->all(); $data = $request->all();
$data['created_by'] = $this->currentName; $data['created_by'] = $this->currentName;
$data['budget'] = $price; $data['budget'] = $price;
$result = AssignMaterial::create($data); $result = AssignMaterial::create($data);
if($result){ if(!$result)
return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 500]);
$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;
$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){ public function update(Request $request, $id){
if(empty($id) || !is_int((int)$id))
if(!$id || (int) $id < 0 || $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);
}
$data = AssignMaterial::find($id); $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()); $result = $data->update($request->all());
}else{
return response()->json(['status'=>'failed','message'=>'Data Assign material not found!','code'=>400], 400);
die();
}
if($result){ if($result)
return response()->json(['status'=>'success','message'=>'Assign material successfully updated!','code'=>200], 200); return response()->json(['status'=>'success','message'=> 'Data updated!','code'=> 200], 200);
}else{
return response()->json(['status'=>'failed','message'=>'Assign material failed updated!','code'=>400], 400); return response()->json(['status'=>'failed','message'=>'Failed to update!','code'=> 500], 500);
}
} }
public function delete($id) public function delete($id)
{ {
$data = AssignMaterial::find($id); $data = AssignMaterial::where('id', $id)->first();
if($data){ if($data->delete())
$id = $data->material_id; return response()->json(['status'=>'success','message'=>'Data deleted!','code'=> 200], 200);
$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($delete){ return response()->json(['status'=>'failed','message'=>'Failed to delete!','code'=> 500], 500);
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){ 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); return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400);
die();
}
$result = AssignMaterial::find($id); $result = AssignMaterial::find($id);
if($result){ if($result)
return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); 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) public function search(Request $request)
@ -189,11 +91,10 @@ class AssignMaterialController extends Controller
$data = AssignMaterial::all(); $data = AssignMaterial::all();
$countData = $data->count(); $countData = $data->count();
if($data){ if($data)
return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); 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){ public function datatables(Request $request){

Loading…
Cancel
Save