From e18d0c3c5b06faa60a69b484259ec0102546422f Mon Sep 17 00:00:00 2001 From: Wahyu Ramadhan Date: Thu, 23 Feb 2023 18:51:33 +0700 Subject: [PATCH] Plan date has to be more than start date --- .../Controllers/AssignMaterialController.php | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/AssignMaterialController.php b/app/Http/Controllers/AssignMaterialController.php index 16f848a..5a89147 100644 --- a/app/Http/Controllers/AssignMaterialController.php +++ b/app/Http/Controllers/AssignMaterialController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\AssignMaterial; use App\Models\RequestMaterial; +use App\Models\Activity; use App\Models\ReportActivityMaterial; use Datatables; @@ -23,23 +24,30 @@ class AssignMaterialController extends Controller 'qty_planning' => 'required' ]); + $activity = Activity::where('id', $request->activity_id)->first(); + $checkStock = RequestMaterial::where("id", $request->material_id)->first(); $currentStock = $checkStock->qty; if((int)$currentStock < (int)$request->qty_planning){ return response()->json(['status'=>'failed','message'=>'Stock is not enough!','code'=> 500]); } + + $start_date = $activity->start_date; + $start_date = substr($start_date, 0, 19); // remove the timezone offset + $startDate = new \DateTime(date("Y-m-d", strtotime($start_date))); + $planDate = new \DateTime(date("Y-m-d", strtotime($request->plan_date))); $data = $request->all(); $data['created_by'] = $this->currentName; $data['budget'] = $checkStock->price; $data['qty_planning'] = $this->sanitizeDecimal($data['qty_planning']); - $result = AssignMaterial::create($data); - if(!$result) - return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 500]); - - return response()->json(['status'=>'success','message'=>'Data added!', 'code'=>200]); - + if ($planDate > $startDate) { + $result = AssignMaterial::create($data); + return response()->json(['status'=>'success','message'=>'Data added!', 'code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 400], 400); + } } public function update(Request $request, $id){