all(), [ 'asset_type' => 'required|string', 'asset_name' => 'required|string', 'required_date' => 'required', 'qty' => 'required', 'uom' => 'required|string', 'qty_received' => 'nullable|string', 'description' => 'required', 'status' => 'nullable|string', 'proyek_id' => 'nullable|integer', 'ownership_status' => 'nullable|string|max:100', 'ownership_name' => 'nullable|string', 'created_by' => 'nullable|string|max:80', 'updated_by' => 'nullable|string|max:80', ]); if ($validator->fails()) { return response()->json(['status' => 'failed', 'message' => 'Tools Request Project failed created', 'code' => 400]); } $validated = $validator->validated(); $validated['status'] = "fot"; $validated['created_by'] = $this->currentName; $result = ToolsRequest::create($validated); if ($result) { return response()->json(['status' => 'success', 'message' => 'Tools Request Project successfull created', 'code' => 200]); } else { return response()->json(['status' => 'failed', 'message' => 'Tools Request Project failed created', 'code' => 400]); } } public function update(Request $request, $id) { $validator = Validator::make($request->all(), [ 'asset_type' => 'required|string', 'asset_name' => 'required|string', 'required_date' => 'required', 'qty' => 'required', 'uom' => 'required|string', 'qty_received' => 'nullable|string', 'description' => 'required', 'status' => 'nullable|string', 'proyek_id' => 'nullable|integer', 'ownership_status' => 'nullable|string|max:100', 'ownership_name' => 'nullable|string', 'created_by' => 'nullable|string|max:80', 'updated_by' => 'nullable|string|max:80', ]); if ($validator->fails()) { return response()->json(['status' => 'failed', 'message' => 'add data material resource failed!', 'code' => 400], 400); } $validated = $validator->validated(); if (empty($id)) { return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); die(); } if (!isset($validated['status'])) { return response()->json(['status' => 'failed', 'message' => 'status is required!', 'code' => 400], 400); die(); } $data = ToolsRequest::find($id); if ($data) : if ($validated['status'] === "receipt to warehouse") { $dataT = ToolsResource::where('name', $data['asset_name'])->first(); if ($dataT) { $result = $data->update($validated); $payloadMQty = $dataT['qty'] + $data['qty_received']; $resultUpdateM = ToolsResource::where('id', $dataT['id'])->update(['qty' => $payloadMQty]); return response()->json(['status' => 'success', 'message' => 'request material successfully updated!', 'code' => 200], 200); } else { $result = $data->update($validated); $dataPayloadM = array( "name" => $validated['asset_name'], "uom" => $validated['uom'], "unit_price" => 0, "description" => $validated['description'], "qty" => $data->qty_received, "asset_type" => $validated['asset_type'], "created_by" => $this->currentName ); $result = ToolsResource::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); } } } elseif ($validated['status'] === "receipt to site") { $dataT = ToolsResource::where('name', $data['asset_name'])->first(); if ($dataT) { $result = $data->update($validated); $payloadMQty = $dataT['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 = ToolsResource::where('id', $dataT['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 tools is not exist in warehouse!', 'code' => 400], 400); } } else { $resultUpdate = $data->update($validated); 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); } } else : return response()->json(['status' => 'failed', 'message' => 'data request material not found!', 'code' => 400], 400); endif; } public function delete($id) { $data = ToolsRequest::find($id); if ($data) { $delete = $data->delete(); if ($delete) { return response()->json(['status' => 'success', 'message' => 'Tools Request Project successfully deleted!', 'code' => 200], 200); } else { return response()->json(['status' => 'failed', 'message' => 'Tools Request Project failed deleted!', 'code' => 400], 400); } } else { return response()->json(['status' => 'failed', 'message' => 'data Tools Request Project not found!', 'code' => 400], 400); } } public function search(Request $request) { $payload = $request->all(); $dataBuilder = $this->setUpPayload($payload, 'm_req_tools'); $builder = $dataBuilder['builder']; $countBuilder = $dataBuilder['count']; $dataGet = $builder->get(); $totalRecord = $countBuilder->count(); return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200); } public function list() { $data = ToolsRequest::all(); $countData = $data->count(); if ($data) { return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200); } else { return response()->json(['status' => 'failed', 'message' => 'failed get list tools request, please try again later!', 'code' => 400], 400); } } }