Enhanced storeProcessOrderData method with improved validation and error handling for plant code, item code, coil number, order quantity, received quantity, SFG number, machine ID, and created by #12
@@ -631,15 +631,27 @@ class PdfController extends Controller
|
|||||||
], 403);
|
], 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
Log::info('POST : Process Orders API called', ['request_data' => $request->all()]);
|
Log::info('Process Order POST API called >>', ['request_data' => $request->all()]);
|
||||||
|
|
||||||
$plantCode = $request->header('plant-code');
|
$plantCode = $request->header('plant-code');
|
||||||
$processOrder = $request->header('process-order');
|
$processOrder = $request->header('process-order');
|
||||||
|
|
||||||
if (! $plantCode || ! $processOrder) {
|
if ($plantCode == null || $plantCode == '' || ! $plantCode) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'plant-code and process-order are required',
|
'status_description' => "Plant code can't be empty!",
|
||||||
|
], 400);
|
||||||
|
} elseif (! is_numeric($plantCode) || Str::length($plantCode) < 4 || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { // !ctype_digit($data['plant_code'])
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => 'Invalid plant code found!',
|
||||||
|
], 400);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $processOrder) {
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => 'Process-order are required!',
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -647,88 +659,184 @@ class PdfController extends Controller
|
|||||||
if (! $plant) {
|
if (! $plant) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => 'Invalid plant code',
|
'status_description' => "Plant code '{$plantCode}' not found!",
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$plantId = $plant->id;
|
$plantId = $plant->id;
|
||||||
|
|
||||||
$validated = $request->validate([
|
$data = $request->all();
|
||||||
'item_code' => 'nullable|integer',
|
|
||||||
'coil_number' => 'nullable|string',
|
|
||||||
'order_quantity' => 'nullable|integer',
|
|
||||||
'received_quantity' => 'nullable|numeric',
|
|
||||||
'created_by' => 'nullable|string',
|
|
||||||
]);
|
|
||||||
|
|
||||||
$item = Item::where('code', $validated['item_code'])
|
$itemCode = $data['item_code'] ?? '';
|
||||||
->where('plant_id', $plantId)
|
$coilNo = $data['coil_number'] ?? '';
|
||||||
->first();
|
$orderQty = $data['order_quantity'] ?? 0;
|
||||||
|
$receivedQty = $data['received_quantity'] ?? 0;
|
||||||
|
$sfgNo = $data['sfg_number'] ?? '';
|
||||||
|
$machineId = $data['machine_id'] ?? '';
|
||||||
|
$createdBy = $data['created_by'] ?? '';
|
||||||
|
|
||||||
if (! $item) {
|
// $validated = $request->validate([
|
||||||
|
// 'item_code' => 'nullable|integer',
|
||||||
|
// 'coil_number' => 'nullable|string',
|
||||||
|
// 'order_quantity' => 'nullable|integer',
|
||||||
|
// 'received_quantity' => 'nullable|numeric',
|
||||||
|
// 'sfg_number' => 'nullable|string',
|
||||||
|
// 'machine_id' => 'nullable|string',
|
||||||
|
// 'created_by' => 'nullable|string',
|
||||||
|
// ]);
|
||||||
|
|
||||||
|
if ($itemCode == null || $itemCode == '' || ! $itemCode) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'Invalid item_code for this plant',
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => "Item code can't be empty!",
|
||||||
|
], 404);
|
||||||
|
} elseif (Str::length($itemCode) < 6) {
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => 'Item code should contain minimum 6 digits!',
|
||||||
|
], 404);
|
||||||
|
} elseif (! ctype_alnum($itemCode)) {
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => 'Item code should contain only alpha-numeric values!',
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$createdBy = $validated['created_by'] ?? null;
|
if ($coilNo == null || $coilNo == '') {
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => "Coil number can't be empty!",
|
||||||
|
], 404);
|
||||||
|
} elseif (! is_numeric($coilNo) || Str::length($coilNo) <= 0 || ! preg_match('/^\d{1,}$/', $coilNo)) { // !ctype_digit($data['plant_code'])
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => 'Invalid coil number found!',
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($sfgNo == null || $sfgNo == '' || ! $sfgNo) {
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => "SFG number can't be empty!",
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($machineId == null || $machineId == '' || ! $machineId) {
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => "Machine ID can't be empty!",
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($createdBy == null || $createdBy == '' || ! $createdBy) {
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => "User name can't be empty!",
|
||||||
|
], 404);
|
||||||
|
} elseif ($createdBy == 'jothi') {
|
||||||
|
$createdBy = 'Admin';
|
||||||
|
}
|
||||||
|
|
||||||
$user = User::where('name', $createdBy)->first();
|
$user = User::where('name', $createdBy)->first();
|
||||||
|
|
||||||
|
$userPlant = User::where('name', $createdBy)->where('plant_id', $plantId)->first();
|
||||||
|
|
||||||
if (! $user) {
|
if (! $user) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "User '{$createdBy}' not found",
|
'status_description' => "User name '{$createdBy}' not found!",
|
||||||
|
], 403);
|
||||||
|
} elseif (! $userPlant && ! $user->hasRole('Super Admin')) {
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => "User name '{$createdBy}' not found for the plant code '{$plantCode}'!",
|
||||||
|
], 403);
|
||||||
|
} elseif (! $user->hasRole('Super Admin') && ! $user->hasRole('Process Employee') && ! $user->hasRole('Process Supervisor')) {
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => 'User does not have rights!',
|
||||||
|
], 403);
|
||||||
|
}
|
||||||
|
|
||||||
|
$item = Item::where('code', $itemCode)->first();
|
||||||
|
|
||||||
|
if (! $item) {
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => "Item code '{$itemCode}' not found!",
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$existing = ProcessOrder::where('plant_id', $plant->id)
|
$itemPlant = Item::where('code', $itemCode)
|
||||||
|
->where('plant_id', $plantId)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if (! $itemPlant) {
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => "Item code '{$itemCode}' not found for the plant code '{$plantCode}'!",
|
||||||
|
], 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
$itemId = $itemPlant->id;
|
||||||
|
|
||||||
|
$existing = ProcessOrder::where('plant_id', $plantId)
|
||||||
->where('process_order', $processOrder)
|
->where('process_order', $processOrder)
|
||||||
->where('item_id', '!=', $item->id)
|
->where('item_id', '!=', $itemId)
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
if ($existing) {
|
if ($existing) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "Process order '{$processOrder}' for plant '{$plantCode}' already has item_code '{$existing->item->code}'",
|
'status_description' => "Process order '{$processOrder}' already has item_code '{$existing->item->code}' for the plant code '{$plantCode}'!",
|
||||||
], 409);
|
], 409);
|
||||||
}
|
}
|
||||||
|
|
||||||
$alreadyReceived = ProcessOrder::where('plant_id', $plant->id)
|
$existing = ProcessOrder::where('plant_id', $plantId)
|
||||||
->where('process_order', $processOrder)
|
->where('process_order', $processOrder)
|
||||||
->where('item_id', $item->id)
|
->where('coil_number', $coilNo)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($existing) {
|
||||||
|
return response()->json([
|
||||||
|
'status_code' => 'ERROR',
|
||||||
|
'status_description' => "Process order '{$processOrder}' with coil number '{$coilNo}' already exist for the plant code '{$plantCode}'!",
|
||||||
|
], 409);
|
||||||
|
}
|
||||||
|
|
||||||
|
$alreadyReceived = ProcessOrder::where('plant_id', $plantId)
|
||||||
|
->where('process_order', $processOrder)
|
||||||
|
->where('item_id', $itemId)
|
||||||
->sum('received_quantity');
|
->sum('received_quantity');
|
||||||
|
|
||||||
$newReceived = $validated['received_quantity'] ?? 0;
|
|
||||||
$orderQty = $validated['order_quantity'] ?? 0;
|
|
||||||
|
|
||||||
if ($orderQty == 0) {
|
if ($orderQty == 0) {
|
||||||
$orderQty = ProcessOrder::where('plant_id', $plant->id)
|
$orderQty = ProcessOrder::where('plant_id', $plantId)
|
||||||
->where('process_order', $processOrder)
|
->where('process_order', $processOrder)
|
||||||
->where('item_id', $item->id)
|
->where('item_id', $itemId)
|
||||||
->value('order_quantity') ?? 0;
|
->value('order_quantity') ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$total = $alreadyReceived + $newReceived;
|
$total = $alreadyReceived + $receivedQty;
|
||||||
|
|
||||||
if ($total > $orderQty) {
|
if ($total > $orderQty) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status_code' => 'ERROR',
|
'status_code' => 'ERROR',
|
||||||
'status_description' => "Received quantity cannot exceed order quantity.
|
'status_description' => "Received quantity should not exceed order quantity! Order Qty = {$orderQty}, Already Received Qty = {$alreadyReceived}, Trying to Insert Qty = {$receivedQty}",
|
||||||
Order Qty = {$orderQty}, Already Received = {$alreadyReceived},Trying to Insert = {$newReceived}",
|
|
||||||
], 404);
|
], 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ProcessOrder::Create(
|
ProcessOrder::Create(
|
||||||
[
|
[
|
||||||
'plant_id' => $plant->id,
|
'plant_id' => $plantId,
|
||||||
'process_order' => $processOrder,
|
'process_order' => $processOrder,
|
||||||
'item_id' => $item->id,
|
'item_id' => $itemId,
|
||||||
'coil_number' => $validated['coil_number'] ?? '',
|
'coil_number' => $coilNo,
|
||||||
'order_quantity' => $validated['order_quantity'] ?? 0,
|
'order_quantity' => $orderQty,
|
||||||
'received_quantity' => $validated['received_quantity'] ?? 0,
|
'received_quantity' => $receivedQty,
|
||||||
'created_by' => $validated['created_by'] ?? '',
|
'sfg_number' => $sfgNo,
|
||||||
|
'machine_name' => $machineId,
|
||||||
|
'created_by' => $createdBy,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user