1
0
forked from poc/pds

Add updateGR method to PdfController for updating serial numbers with validation

This commit is contained in:
dhanabalan
2025-09-27 16:08:01 +05:30
parent 61e9995892
commit 0c84421719

View File

@@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\GrMaster; use App\Models\GrMaster;
use App\Models\Item;
use App\Models\Plant; use App\Models\Plant;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response; use Illuminate\Support\Facades\Response;
@@ -18,6 +19,187 @@ class PdfController extends Controller
{ {
// //
} }
public function updateGR(Request $request)
{
$expectedUser = env('API_AUTH_USER');
$expectedPw = env('API_AUTH_PW');
$header_auth = $request->header('Authorization');
$expectedToken = $expectedUser . ':' . $expectedPw;
if ("Bearer " . $expectedToken != $header_auth)
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Invalid authorization token!'
], 403);
}
$data = $request->all();
if ($data['plant_code'] == null || $data['plant_code'] == '')
{
// return response("ERROR: Please provide a valid plant code.", 400)
// ->header('Content-Type', 'text/plain');
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Plant code can't be empty!"
], 400);
}
else if (Str::length($data['plant_code']) < 4 || !is_numeric($data['plant_code']) || !preg_match('/^[1-9]\d{3,}$/', $data['plant_code']))//!ctype_digit($data['plant_code'])
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Invalid plant code found!"
], 400);
}
$plant = Plant::where('code', $data['plant_code'])->first();
if (!$plant) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Plant not found!'
], 400);
}
$plantId = $plant->id;
if ($data['gr_number'] == null || $data['gr_number'] == '')
{
return response()->json([
'status_code' => 'ERROR',
'status_description' => "GR Number can't be empty!"
], 400);
}
$scannedBy = $data['scanned_by'] ?? null;
if ($scannedBy == '' || $scannedBy == null) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => "Scanned by can't be empty!"
], 400);
}
$itemCodes = array_column($data['item_codes'], 'item_code');
$duplicateItemCodes = array_unique(array_diff_assoc($itemCodes, array_unique($itemCodes)));
if (count($duplicateItemCodes) > 0) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Duplicate item codes found in request!',
'duplicate_item_codes' => array_values($duplicateItemCodes)
], 400);
}
$allSerials = [];
foreach ($data['item_codes'] as $item) {
if (!isset($item['serial_numbers']) || !is_array($item['serial_numbers'])) {
continue;
}
foreach ($item['serial_numbers'] as $serial) {
$allSerials[] = $serial;
}
}
$duplicateSerials = array_unique(array_diff_assoc($allSerials, array_unique($allSerials)));
if (count($duplicateSerials) > 0) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Duplicate serial numbers found in request!',
'duplicate_serial_numbers' => array_values($duplicateSerials)
], 400);
}
$invalidItemCodes = [];
$invalidPlantItems = [];
foreach ($data['item_codes'] as $item)
{
$itemCode = $item['item_code'] ?? null;
if (!$itemCode) {
$invalidItemCodes[] = "(missing)";
continue;
}
$itemObj = Item::where('code', $itemCode)->first();
if (!$itemObj) {
$invalidItemCodes[] = $itemCode;
continue;
}
$itemPlant = Item::where('plant_id', $plantId)
->where('code', $itemCode)->first();
if (!$itemPlant) {
$invalidPlantItems[] = $itemCode;
}
}
if (count($invalidItemCodes) > 0 || count($invalidPlantItems) > 0) {
return response()->json([
'status_code' => 'ERROR',
'status_description' => 'Some item codes are invalid!',
'not_found_items' => array_values($invalidItemCodes),
'not_in_plant' => array_values($invalidPlantItems)
], 400);
}
// foreach ($data['item_codes'] as $item)
// {
// $itemCode = $item['item_code'];
// $serialNumbers = $item['serial_numbers'];
// $itemObj = Item::where('code', $itemCode)->first();
// if (!$itemObj) {
// return response()->json([
// 'status_code' => 'ERROR',
// 'status_description' => "Item code: $itemCode not found"
// ], 400);
// }
// $itemPlant = Item::where('plant_id', $plantId)
// ->where('code', $itemCode)->first();
// if (!$itemPlant) {
// return response()->json([
// 'status_code' => 'ERROR',
// 'status_description' => "Item code: $itemCode not found for the plant: $data[plant_code]"
// ], 400);
// }
// $itemId = $itemObj->id;
// // Update all serial numbers for this item
// GRMaster::where('plant_id', $plantId)
// ->where('item_id', $itemId)
// ->where('gr_number', $data['gr_number'])
// ->whereIn('serial_number', $serialNumbers)
// ->update(['created_by' => $scannedBy]);
// }
foreach ($data['item_codes'] as $item)
{
$itemCode = $item['item_code'];
$serialNumbers = $item['serial_numbers'];
$itemId = Item::where('code', $itemCode)->value('id');
GRMaster::where('plant_id', $plantId)
->where('item_id', $itemId)
->where('gr_number', $data['gr_number'])
->whereIn('serial_number', $serialNumbers)
->update(['created_by' => $scannedBy]);
}
return response()->json([
'status_code' => 'SUCCESS',
'status_description' => 'Serial numbers updated successfully!'
], 200);
}
/** /**
* Store a newly created resource in storage. * Store a newly created resource in storage.
@@ -200,8 +382,6 @@ class PdfController extends Controller
return response()->json([ return response()->json([
'serial_numbers' => $serialNumbers 'serial_numbers' => $serialNumbers
], 200); ], 200);
} }