Custom Backend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.0 KiB

3 years ago
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\RequestMaterial;
use App\Models\MaterialResource;
class RequestMaterialController extends Controller
{
private function sanitizeDecimal($number)
{
$number = str_replace(".", "", $number);
$number = str_replace(",", ".", $number);
return $number;
}
public function add(Request $request)
{
2 years ago
$this->validate($request, [
'proyek_id' => 'required',
'description' => 'required',
'required_date' => 'required',
'qty' => 'required',
'uom' => 'required',
'fom_date' => 'required',
]);
$data = $request->all();
$data['price'] = $this->sanitizeDecimal($data['price']);
$data['qty'] = $this->sanitizeDecimal($data['qty']);
2 years ago
$data['status'] = "fom";
$data['created_by'] = $this->currentName;
$result = RequestMaterial::create($data);
if (!$result)
return response()->json(['status' => 'failed', 'message' => 'Failed to add!', 'code' => 500], 500);
2 years ago
return response()->json(['status' => 'success', 'message' => 'Data added!', 'data' => $result, 'code' => 200], 200);
2 years ago
}
public function edit($id)
{
if (empty($id) || !is_int((int)$id))
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
2 years ago
$result = RequestMaterial::find($id);
if ($result)
return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200);
2 years ago
return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
2 years ago
}
public function delete($id)
{
$data = RequestMaterial::find($id);
if ($data)
2 years ago
$delete = $data->delete();
return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
2 years ago
if ($delete)
return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200);
2 years ago
return response()->json(['status' => 'failed', 'message' => 'Failed to delete!', 'code' => 500], 500);
2 years ago
}
public function search(Request $request)
{
$payload = $request->all();
$dataBuilder = $this->setUpPayload($payload, 'm_req_material');
$builder = $dataBuilder['builder'];
$countBuilder = $dataBuilder['count'];
$dataGet = $builder->get();
$totalRecord = $countBuilder->count();
return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200);
2 years ago
}
public function list()
{
$data = RequestMaterial::all();
$countData = $data->count();
if ($data)
return response()->json(['status' => 'success', 'code' => 200, 'data' => $data, 'totalRecord' => $countData], 200);
2 years ago
return response()->json(['status' => 'failed', 'message' => 'failed get list request material, please try again later!', 'code' => 400], 400);
2 years ago
}
public function update(Request $request, $id)
{
$dataForm = $request->all();
if (empty($id) || !is_int((int)$id))
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
2 years ago
$reqMaterial = RequestMaterial::find($id);
if (!$reqMaterial)
return response()->json(['status' => 'failed', 'message' => 'data request material not found!', 'code' => 400], 400);
2 years ago
if (!isset($dataForm['status'])) {
if (!$reqMaterial->update($request->all()))
return response()->json(['status' => 'failed', 'message' => 'request material failed updated!', 'code' => 400], 400);
return response()->json(['status' => 'success', 'message' => 'request material successfully updated!', 'code' => 200], 200);
2 years ago
}
2 years ago
if ($dataForm['status'] == "receipt to warehouse") {
2 years ago
$materialResource = MaterialResource::where('name', 'ilike', $reqMaterial->description)->first();
if ($materialResource) {
2 years ago
$reqMaterial->update($request->all());
$payloadMQty = $materialResource->qty + $reqMaterial->qty_received;
MaterialResource::where('id', $materialResource->id)->update(['qty' => $payloadMQty]);
return response()->json(['status' => 'success', 'code' => 200], 200);
2 years ago
}
2 years ago
$reqMaterial->update($request->all());
$preparedMaterialResource = array(
"name" => $dataForm['description'],
"uom" => $dataForm['uom'],
"unit_price" => 0,
"description" => $dataForm['description'],
"qty" => $reqMaterial->qty_received,
"status" => "Barang tersedia",
"created_by" => $this->currentName
);
if (!MaterialResource::create($preparedMaterialResource))
return response()->json(['status' => 'failed', 'message' => 'add data material resource failed!', 'code' => 400], 400);
2 years ago
return response()->json(['status' => 'success', 'message' => 'add data material resource successfully!', 'code' => 200], 200);
2 years ago
}
if ($dataForm['status'] == "receipt to site") {
2 years ago
$reqMaterial->update($request->all());
$materialResource = MaterialResource::where('name', 'ilike', $reqMaterial->description)->first();
if (!$materialResource)
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);
2 years ago
$reqMaterial->update($request->all());
$payloadMQty = $materialResource->qty - $reqMaterial->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);
2 years ago
MaterialResource::where('id', $materialResource->id)->update(['qty' => $payloadMQty]);
return response()->json(['status' => 'success', 'message' => 'request material status receipt to site successfully updated!', 'code' => 200], 200);
2 years ago
}
2 years ago
if (!$reqMaterial->update($request->all()))
return response()->json(['status' => 'failed', 'message' => 'request material failed updated!', 'code' => 400], 400);
return response()->json(['status' => 'success', 'message' => 'request material successfully updated!', 'code' => 200], 200);
}
3 years ago
}