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.
 
 
 
 

270 lines
11 KiB

<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\{RequestMaterial, MaterialResource};
use Illuminate\Support\Facades\{DB, Validator};
class RequestMaterialController extends Controller
{
private function sanitizeDecimal($number)
{
$number = str_replace(".", "", $number);
$number = str_replace(",", ".", $number);
return $number;
}
public function add(Request $request)
{
$validator = Validator::make($request->all(), [
'description' => 'required',
'required_date' => 'required',
'qty' => 'required',
'uom' => 'required|string',
'qty_received' => 'nullable',
'fom_date' => 'required',
'pr_date' => 'nullable',
'po_date' => 'nullable',
'received_date' => 'nullable',
'delivery_date' => 'nullable',
'status' => 'nullable|string',
'proyek_id' => 'required|integer',
'price' => 'nullable',
'created_by' => 'nullable|string|max:80',
'updated_by' => 'nullable|string|max:80',
]);
if ($validator->fails()) {
return response()->json(['status' => 'failed', 'message' => 'add data menu failed!', 'code' => 400], 400);
}
$validated = $validator->validated();
$validated['price'] = $this->sanitizeDecimal($validated['price']);
$validated['qty'] = $this->sanitizeDecimal($validated['qty']);
$validated['status'] = "fom";
$validated['created_by'] = $this->currentName;
$result = RequestMaterial::create($validated);
if ($result) {
return response()->json(['status' => 'success', 'message' => 'Data added!', 'data' => $result, 'code' => 200], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'Failed to add!', 'code' => 500], 500);
}
}
public function edit($id)
{
if (empty($id)) {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
}
$result = RequestMaterial::find($id);
if ($result) {
return response()->json(['status' => 'success', 'code' => 200, 'data' => $result], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
}
}
public function delete($id)
{
$data = RequestMaterial::find($id);
if ($data) {
$delete = $data->delete();
if ($delete) {
return response()->json(['status' => 'success', 'message' => 'Data deleted!', 'code' => 200], 200);
} else {
return response()->json(['status' => 'failed', 'message' => 'Failed to delete!', 'code' => 500], 500);
}
} else {
return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
}
}
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);
}
public function list()
{
$data = RequestMaterial::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 request material, please try again later!', 'code' => 400], 400);
}
}
public function update(Request $request, $id)
{
if (empty($id)) {
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
die;
}
$reqMaterial = RequestMaterial::find($id);
$validator = Validator::make($request->all(), [
'description' => 'required',
'required_date' => 'required',
'qty' => 'required',
'uom' => 'required|string',
'qty_received' => 'nullable',
'fom_date' => 'required',
'pr_date' => 'nullable',
'po_date' => 'nullable',
'received_date' => 'nullable',
'delivery_date' => 'nullable',
'status' => 'nullable|string',
'proyek_id' => 'required|integer',
'price' => 'nullable',
'created_by' => 'nullable|string|max:80',
'updated_by' => 'nullable|string|max:80',
]);
if ($validator->fails()) {
return response()->json(['status' => 'failed', 'message' => 'add data menu failed!', 'code' => 400], 400);
}
$validated = $validator->validated();
try {
DB::beginTransaction();
if (!$reqMaterial) {
return response()->json(['status' => 'failed', 'message' => 'data request material not found!', 'code' => 400], 400);
}
if (!isset($validated['status'])) :
if (!$reqMaterial->update($validated)) {
return response()->json(['status' => 'failed', 'message' => 'request material failed updated!', 'code' => 400], 400);
} else {
return response()->json(['status' => 'success', 'message' => 'request material successfully updated!', 'code' => 200], 200);
}
endif;
if ($validated['status'] === "receipt to warehouse") :
$materialResource = MaterialResource::where('name', 'ILIKE', $reqMaterial['description'])->first();
if ($materialResource) {
$reqMaterial->update($validated);
$payloadMQty = $materialResource['qty'] + $reqMaterial['qty_received'];
MaterialResource::whereId($materialResource['id'])->update(['qty' => $payloadMQty]);
return response()->json(['status' => 'success', 'code' => 200], 200);
}
$reqMaterial->update($validated);
$resultMaterial = MaterialResource::create([
"name" => $validated['description'],
"uom" => $validated['uom'],
"unit_price" => 0,
"description" => $validated['description'],
"qty" => $reqMaterial['qty_received'],
"status" => "Barang tersedia",
"created_by" => $this->currentName
]);
if ($resultMaterial) {
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") :
$reqMaterial->update($validated);
$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);
die;
}
$reqMaterial->update($validated);
$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);
die;
}
$materialResourceScnd = MaterialResource::query()
->whereId($materialResource['id'])
->update([
'qty' => $payloadMQty
]);
if ($materialResourceScnd) {
return response()->json(['status' => 'success', 'message' => 'request material status receipt to site successfully updated!', 'code' => 200], 200);
}
endif;
if (!$reqMaterial->update($validated)) {
return response()->json(['status' => 'failed', 'message' => 'request material failed updated!', 'code' => 400], 400);
} else {
return response()->json(['status' => 'success', 'message' => 'request material successfully updated!', 'code' => 200], 200);
}
DB::commit();
} catch (\Throwable $th) {
DB::rollBack();
return response()->json(['status' => 'failed', 'message' => 'request material failed updated!', 'code' => 400], 400);
}
}
private function curlReq($url, $token)
{
$ch = curl_init();
$headers = [
'Authorization: ' . $token
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
if ($response === false) {
$response = curl_error($ch);
}
curl_close($ch);
return json_decode($response);
}
public function getMaterialIntegration()
{
$search = null;
if (empty($search)) {
return response()->json(['status' => 'error', 'message' => 'Empty query string!'], 400);
}
$url = str_replace("SEARCH", $search, config('api.adw') . '/stock_master?name=SEARCH');
$token = config('api.adw_token');
$firstResponse = $this->curlReq($url, $token);
if ($firstResponse->total == 0) {
return response()->json(['status' => 'error', 'message' => 'Data not found!', 'code' => 404], 404);
}
$data = $firstResponse->data;
$currentPage = 1;
if ($firstResponse->last_page > 0) {
do {
$currentPage++;
$response = $this->curlReq($url . '&page=' . $currentPage, $token);
foreach ($response->data as $d) {
array_push($data, $d);
}
} while ($currentPage < 5);
// let the user narrow down the search by themself
// a searching action with 'cable' keyword could take minutes
// because there are >1k items associated with that keyword
}
return response()->json(['status' => 'success', 'data' => $data, 'total' => count($data), 'code' => 200], 200);
}
}