Browse Source

Plan date has to be more than start date

pull/3/head
Wahyu Ramadhan 2 years ago
parent
commit
e18d0c3c5b
  1. 18
      app/Http/Controllers/AssignMaterialController.php

18
app/Http/Controllers/AssignMaterialController.php

@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\AssignMaterial; use App\Models\AssignMaterial;
use App\Models\RequestMaterial; use App\Models\RequestMaterial;
use App\Models\Activity;
use App\Models\ReportActivityMaterial; use App\Models\ReportActivityMaterial;
use Datatables; use Datatables;
@ -23,23 +24,30 @@ class AssignMaterialController extends Controller
'qty_planning' => 'required' 'qty_planning' => 'required'
]); ]);
$activity = Activity::where('id', $request->activity_id)->first();
$checkStock = RequestMaterial::where("id", $request->material_id)->first(); $checkStock = RequestMaterial::where("id", $request->material_id)->first();
$currentStock = $checkStock->qty; $currentStock = $checkStock->qty;
if((int)$currentStock < (int)$request->qty_planning){ if((int)$currentStock < (int)$request->qty_planning){
return response()->json(['status'=>'failed','message'=>'Stock is not enough!','code'=> 500]); 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 = $request->all();
$data['created_by'] = $this->currentName; $data['created_by'] = $this->currentName;
$data['budget'] = $checkStock->price; $data['budget'] = $checkStock->price;
$data['qty_planning'] = $this->sanitizeDecimal($data['qty_planning']); $data['qty_planning'] = $this->sanitizeDecimal($data['qty_planning']);
if ($planDate > $startDate) {
$result = AssignMaterial::create($data); $result = AssignMaterial::create($data);
if(!$result) return response()->json(['status'=>'success','message'=>'Data added!', 'code'=>200], 200);
return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 500]); }else{
return response()->json(['status'=>'failed','message'=>'Failed to add data!','code'=> 400], 400);
return response()->json(['status'=>'success','message'=>'Data added!', 'code'=>200]); }
} }
public function update(Request $request, $id){ public function update(Request $request, $id){

Loading…
Cancel
Save