diff --git a/app/Http/Controllers/ClientsController.php b/app/Http/Controllers/ClientsController.php index f14b3d3..38a9733 100644 --- a/app/Http/Controllers/ClientsController.php +++ b/app/Http/Controllers/ClientsController.php @@ -22,154 +22,21 @@ class ClientsController extends Controller public function index(){ $countries = Models\Country::pluck('en_short_name', 'en_short_name'); $service_type = Models\Service::pluck('name', 'name'); - $staff_members = Models\StaffMember::pluck('name', 'id'); + $staff_members = Models\StaffMember::pluck('name', 'id'); //where('department_id', '1')-> + $currencies = Models\Currency::get(); + // dd($staff_members); $data = [ 'page_title' => 'Dashboard', 'service_types' => $service_type, 'countries' => $countries, - 'staff_members' => $staff_members + 'staff_members' => $staff_members, + 'currencies' => $currencies ]; return view('clients.index', $data); } - - public function showOld(int $id) - { - $showclient = Models\Client::with([ - 'service_info', - 'country_flag_info', - 'auth_user_info', - 'short_code_info', - ])->findOrFail($id); - - $progress_indicators = Models\ClientIndicator::pluck('name', 'name'); - $service_type = Models\Service::pluck('name', 'id'); - $service_type_names = Models\Service::pluck('name', 'name'); - $show_services = Models\ClientCategory::where('client_id', $id)->get(); - - $country_networks = DB::table('network_operators') - ->selectRaw('id, CONCAT(name, " (", country, ")") AS network') - ->orderBy('network') - ->pluck('network', 'network'); - - $auth_users = Models\SystemUser::orderBy('name')->pluck('name', 'id'); - - $networks_raw = [ - 'AirtelTigo GH', - 'MTN GH', - 'Airtel MW', - 'Airtel Zambia', - 'TNM MW', - 'Safaricom Kenya', - 'Airtel Kenya', - 'Telkom Kenya', - 'Orange Kenya', - ]; - - $show_notes_query = Models\ClientNote::with(['created_by_info', 'client_info']) - ->where('client_id', $id) - ->latest(); - - $show_notes = $show_notes_query->take(20)->get(); - $show_notes_highlight = (clone $show_notes_query) - ->where('highlight', 'YES') - ->take(1) - ->get(); - - $voice_codes = Models\ClientShortCode::where('client_id', $id) - ->where('code_type', 'voice') - ->get(); - - $sms_codes = Models\ClientShortCode::where('client_id', $id) - ->where('code_type', 'sms') - ->get(); - - $ussd_codes = Models\ClientShortCode::where('client_id', $id) - ->where('code_type', 'ussd') - ->get(); - - $recent_payments = Models\ClientPayment::where('client_id', $id) - ->latest('id') - ->get(); - - $countries = Models\Country::pluck('en_short_name', 'en_short_name'); - $networks = Models\NetworkOperator::pluck('name', 'id'); - - $support_fees = Models\ClientSupportFees::where('client_id', $id) - ->latest('id') - ->get(); - - $showdocuments = Models\ClientFile::where('client_id', $id)->get(); - - $status_bg = match ($showclient->status) { - 'Live' => 'info', - 'Prospective' => 'warning', - default => 'danger', - }; - - $progress_status_bg = match (true) { - $showclient->progress_indicator_score >= 70 => 'success', - $showclient->progress_indicator_score >= 50 => 'warning', - default => 'danger', - }; - - [$renewal_due, $highlight_colour] = $this->getRenewalDue( - $showclient->contract_validity - ); - - $recurring_arr = [ - 'NO' => 'NO', - 'Monthly' => 'Monthly', - 'Quarterly' => 'Quarterly', - 'Semiannual' => 'Semiannual', - 'Yearly' => 'Yearly', - ]; - - $sender_id_statuses = [ - 'Pending' => 'Pending', - 'Inactive' => 'Inactive', - 'Approved' => 'Approved', - ]; - - $change_account_mgr_permission = Config::get('permissions.CHANGE_ACCOUNT_MANAGERS'); - // dump($change_account_mgr_permission); - $change_account_mgr_permission = $this->hasAnyAccess([$change_account_mgr_permission]) ? 'YES' : 'NO'; - // dd($change_account_mgr_permission); - - // dd($showclient); - return view('clients.show', [ - 'page_title' => 'Client Profile', - 'showclient' => $showclient, - 'show_services' => $show_services, - 'service_type' => $service_type, - 'service_type_names' => $service_type_names, - 'show_notes' => $show_notes, - 'show_notes_highlight' => $show_notes_highlight, - 'status_bg' => $status_bg, - 'progress_status_bg' => $progress_status_bg, - 'voice_codes' => $voice_codes, - 'sms_codes' => $sms_codes, - 'ussd_codes' => $ussd_codes, - 'countries' => $countries, - 'networks' => $networks, - 'progress_indicators' => $progress_indicators, - 'networks_raw' => array_combine($networks_raw, $networks_raw), - 'renewal_due' => $renewal_due, - 'recent_payments' => $recent_payments, - 'highlight_colour' => $highlight_colour, - 'showdocuments' => $showdocuments, - 'support_fees' => $support_fees, - 'recurring_arr' => $recurring_arr, - 'sender_id_statuses' => $sender_id_statuses, - 'change_account_mgr_permisson' => $change_account_mgr_permission, - 'am_list_arr' => $auth_users, - 'country_network_arr' => $country_networks, - 'mnos_arr' => ['' => '-- Select Country first --'], - ]); - } - public function show(int $id) { $showclient = Models\Client::with([ @@ -189,6 +56,10 @@ class ClientsController extends Controller $service_type = Models\Service::pluck('name', 'id'); $service_type_names = Models\Service::pluck('name', 'name'); $show_services = Models\ClientCategory::where('client_id', $id)->get(); + $currencies = Models\Currency::get(); + $staff_members = Models\StaffMember::pluck('name', 'id'); //where('department_id', '1')-> + + $country_networks = DB::table('network_operators') ->selectRaw('id, CONCAT(name, " (", country, ")") AS network') @@ -279,7 +150,7 @@ class ClientsController extends Controller $change_account_mgr_permission = Config::get('permissions.CHANGE_ACCOUNT_MANAGERS'); $change_account_mgr_permission = $this->hasAnyAccess([$change_account_mgr_permission]) ? 'YES' : 'NO'; - + // dd($showclient); return view('clients.show', [ 'page_title' => 'Client Profile', 'showclient' => $showclient, @@ -295,6 +166,8 @@ class ClientsController extends Controller 'ussd_codes' => $ussd_codes, 'countries' => $countries, 'networks' => $networks, + 'currencies' => $currencies, + 'staff_members' => $staff_members, 'progress_indicators' => $progress_indicators, 'networks_raw' => array_combine($networks_raw, $networks_raw), 'renewal_due' => $renewal_due, @@ -551,7 +424,7 @@ class ClientsController extends Controller 'status' => $request->status, 'pay_mode' => $request->payment_mode, 'currency' => $request->currency, - 'auth_user_id' => $request->auth_user_id, + 'auth_user_id' => $request->account_manager, 'created_by' => Auth::user()->id, 'last_modified_by' => Auth::user()->id, 'progress_indicator_score' => 10, @@ -617,7 +490,6 @@ class ClientsController extends Controller public function update(Request $request, $id) { $client = \App\Models\Client::findOrFail($id); - $client->update([ 'name' => $request->name, 'email' => $request->email, @@ -629,7 +501,7 @@ class ClientsController extends Controller 'status' => $request->status, 'currency' => $request->currency, 'country' => $request->country, - // Arrays handled by model casts + 'auth_user_id' => $request->account_manager, 'services' => $request->services, 'message_types' => $request->message_types, 'connections' => $request->connections, @@ -773,8 +645,6 @@ class ClientsController extends Controller dispatch(new SendShortCodeListToFinance($short_code_list, $message_body, $finance_arr)); } - - #$payments = Models\ClientPayment::with('client_info', 'created_by_info')->find($result->id); if ($result) { $data = ['code' => 1, 'msg' => 'Payment Details successfully added']; } @@ -809,6 +679,9 @@ class ClientsController extends Controller $payment->short_code = ($request->short_code) ? $request->short_code : ""; $payment->services = implode(',', $request->services); + if ($request->has('remarks')) { + $payment->remarks = $request->remarks; + } $result = $payment->save(); $client = Models\Client::find($request->client_id); if ($request->has('short_code')) { @@ -1017,13 +890,17 @@ class ClientsController extends Controller 'invoice_amount' => 'required|numeric', 'invoice_date' => 'required|date', 'invoice_status' => 'required|string', + 'remarks' => 'sometimes|string' ]); // Find the record and update it - $payment = Models\ClientPayment::findOrFail($id); // Adjust 'ClientPayment' to your actual model name + // if ($request->has('remarks')) { + // $payment->remarks = $request->remarks; + // } + $payment = Models\ClientPayment::findOrFail($id); $payment->update($validated); - // Return the JSON response for SweetAlert + return response()->json([ 'success' => true, 'message' => 'Payment record updated successfully!' diff --git a/public/assets/js/client-index.js b/public/assets/js/client-index.js index e59a646..f19df23 100644 --- a/public/assets/js/client-index.js +++ b/public/assets/js/client-index.js @@ -6,6 +6,21 @@ $(document).ready(function() { allowClear: true, dropdownParent: $('#createClientModal') }); + // Initialize Currency for Create Modal + $('#clientCurrency').select2({ + theme: 'bootstrap-5', + placeholder: "-- Select Currency --", + allowClear: true, + // Attach to the immediate column wrapper so it moves with the input + dropdownParent: $('#clientCurrency').parent() + }); + $('#edit_currency').select2({ + theme: 'bootstrap-5', + placeholder: "-- Select Currency --", + allowClear: true, + // Attach to the immediate column wrapper so it moves with the input + dropdownParent: $('#edit_currency').parent() + }); let searchTimer; // Initialize the table on page load @@ -80,6 +95,8 @@ $(document).ready(function() { $('#edit_industry').val(client.industry); $('#edit_status').val(client.status); $('#edit_currency').val(client.currency); + $('#edit_currency').val(client.currency).trigger('change'); + $('#edit_account_manager').val(client.auth_user_id).trigger('change'); // $('#edit_country').val(client.country); loadCountries(client.country); // Helper for multi-select Select2 fields @@ -212,6 +229,79 @@ $(document).ready(function() { }); }); + // --------------------------------------------------------- + // AJAX FORM SUBMISSION (Edit Client) + // --------------------------------------------------------- + $('#editClientForm').on('submit', function(e) { + e.preventDefault(); + + let $form = $(this); + let $submitBtn = $form.find('button[type="submit"]'); + let originalText = $submitBtn.html(); + + $submitBtn.html('Updating...').prop('disabled', true); + + // Clear previous validation errors + $form.find('.is-invalid').removeClass('is-invalid'); + $form.find('.invalid-feedback').remove(); + + $.ajax({ + url: $form.attr('action'), + method: 'POST', // @method('PUT') is already included in the serialized data + data: $form.serialize(), + success: function(response) { + $('#editClientModal').modal('hide'); + + Swal.fire({ + icon: 'success', + title: 'Updated!', + text: response.message || 'Client updated successfully!', + showConfirmButton: false, + timer: 1500 + }); + + // Refresh the table data + fetchClients(1); + }, + error: function(xhr) { + if (xhr.status === 401) { + window.location.href = base_url + '/login'; + } else if (xhr.status === 422) { + let errors = xhr.responseJSON.errors; + + $.each(errors, function(key, value) { + // Map validation keys to actual input names if they are arrays + let fieldName = key; + let arrayFields = ['services', 'message_types', 'connections', 'support_phones', 'support_emails', 'rate_emails', 'support_skype']; + if (arrayFields.includes(key)) { + fieldName = key + '[]'; + } + + let $input = $form.find('[name="' + fieldName + '"]'); + if ($input.length) { + // If Select2, target the container for the red border + if ($input.hasClass('select2-hidden-accessible')) { + $input.next('.select2-container').addClass('is-invalid'); + } else { + $input.addClass('is-invalid'); + } + $input.parent().append('