Files
ERP-upgrade/app/Http/Controllers/SenderidsController.php
2026-08-19 11:55:44 +00:00

144 lines
5.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models;
class SenderidsController extends Controller
{
public function index(Request $request)
{
// Fetch KPI Metrics (Keep your existing counts)
// $totalCount = \App\Models\SenderId::count();
// $approvedCount = \App\Models\SenderId::where('status', 'APPROVED')->count();
// $pendingCount = \App\Models\SenderId::where('status', 'PENDING')->count();
// $rejectedCount = \App\Models\SenderId::where('status', 'REJECTED')->count();
$totalCount = \App\Models\SenderId::count();
$approvedCount = \App\Models\SenderId::where('status', 'LIKE', 'Approved%')->count();
$pendingCount = \App\Models\SenderId::where('status', 'LIKE', 'Applied%')->count();
$rejectedCount = \App\Models\SenderId::where('status', 'Rejected')->count();
// Fetch dynamic lists for the modal dropdowns
$clients = Models\Client::orderBy('name', 'asc')->get();
$networkOperators = Models\NetworkOperator::orderBy('name', 'asc')->get();
// Start the query
$query = \App\Models\SenderId::query();
// 1. Search Filter (Sender ID, MNO, or Supplier)
if ($request->filled('search')) {
$search = $request->search;
$query->where(function($q) use ($search) {
$q->where('senderid', 'like', "%{$search}%")
->orWhere('mno_name', 'like', "%{$search}%")
->orWhere('supplier_name', 'like', "%{$search}%");
});
}
// 2. Direct MNO Filter
if ($request->filled('direct_mno')) {
$query->where('direct_mno', $request->direct_mno);
}
// 3. Status Filter
if ($request->filled('status')) {
$query->where('status', 'LIKE', '%' . $request->status . '%');
}
// Execute query with pagination
$senderIds = $query->orderBy('created_at', 'desc')->paginate(10);
// Crucial: Append the current request query strings to pagination links
$senderIds->appends($request->query());
$data = [
'page_title' => 'Sender IDs',
'totalCount' => $totalCount,
'approvedCount' => $approvedCount,
'pendingCount' => $pendingCount,
'rejectedCount' => $rejectedCount,
'senderIds' => $senderIds,
'clients' => $clients, // <-- Pass Clients
'networkOperators' => $networkOperators, // <-- Pass MNOs
];
return view('senderids.index', $data);
}
public function store(Request $request)
{
$validated = $request->validate([
'senderid' => 'required|string|max:20',
'direct_mno' => 'required|in:YES,NO',
// Conditional Validation Rules
'mno_name' => 'required_if:direct_mno,YES|nullable|string|max:192',
'supplier_name' => 'required_if:direct_mno,NO|nullable|string|max:192',
'bind_name' => 'nullable|string|max:50',
'type' => 'nullable|string|max:25',
'status' => 'nullable|string|max:154',
'remarks' => 'nullable|string|max:250',
], [
'mno_name.required_if' => 'The MNO Name is required when routing directly to an MNO.',
'supplier_name.required_if' => 'The Supplier Name is required when not routing directly to an MNO.',
]);
// Attach creator ID and save
$validated['created_by'] = auth()->id();
// Create the Sender ID
$senderId = \App\Models\SenderId::create($validated);
return response()->json(['success' => true, 'message' => 'Sender ID added successfully!']);
}
public function update(Request $request, $id)
{
$senderId = Models\SenderId::findOrFail($id);
$validated = $request->validate([
'senderid' => 'required|string|max:20',
'direct_mno' => 'required|in:YES,NO',
// Conditional validation rules for update as well
'mno_name' => 'required_if:direct_mno,YES|nullable|string|max:192',
'supplier_name' => 'required_if:direct_mno,NO|nullable|string|max:192',
'bind_name' => 'nullable|string|max:50',
'type' => 'nullable|string|max:25',
'status' => 'nullable|string|max:154',
'remarks' => 'nullable|string|max:250',
], [
'mno_name.required_if' => 'The MNO Name is required when routing directly to an MNO.',
'supplier_name.required_if' => 'The Supplier Name is required when not routing directly to an MNO.',
]);
// Clear out the opposite field depending on the Direct MNO choice to keep data clean
if ($validated['direct_mno'] === 'YES') {
$validated['supplier_name'] = null;
} else {
$validated['mno_name'] = null;
}
$validated['last_modified_by'] = auth()->id();
$senderId->update($validated);
return response()->json([
'success' => true,
'message' => 'Sender ID updated successfully!'
]);
}
public function destroy($id)
{
$senderId = SenderId::findOrFail($id);
$senderId->delete();
return redirect()->route('sender-ids.index')->with('success', 'Sender ID deleted successfully.');
}
}