|
|
|
@ -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){ |
|
|
|
|