From f6110b66514c98e24108fb5645652530a1df8206 Mon Sep 17 00:00:00 2001 From: ISMAIL MASSERAN Date: Wed, 9 Sep 2026 10:27:48 +0800 Subject: [PATCH] DONE: auto add ic photo for existing customer --- app/Http/Controllers/CustomersController.php | 23 ++++++++ public/js/mykad-reader-customer.js | 56 ++++++++++++++++++- .../views/components/print/header.blade.php | 13 +++-- resources/views/customers/v1/index.blade.php | 3 +- resources/views/customers/v2/index.blade.php | 3 +- routes/web.php | 1 + 6 files changed, 91 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/CustomersController.php b/app/Http/Controllers/CustomersController.php index 518115cc..2913fdbc 100644 --- a/app/Http/Controllers/CustomersController.php +++ b/app/Http/Controllers/CustomersController.php @@ -289,6 +289,29 @@ class CustomersController extends Controller return response(["no-data"], 200); } + /** + * Store MyKad photo for an existing customer when gambar_mykad is still empty. + */ + public function storeMykadPhotoIfMissing(Request $request) + { + $icno = preg_replace('/[^0-9A-Za-z]/', '', (string) $request->input('icno')); + if ($icno === '') { + return response()->json(['error' => 'IC number is required.', 'stored' => false], 422); + } + + if (!$request->filled('photo_base64')) { + return response()->json(['error' => 'Photo is required.', 'stored' => false], 422); + } + + $response = Http::withOptions(['verify' => config('app.api_verify')]) + ->post(config('api.url') . '/api/customers/' . $icno . '/mykad-photo', [ + 'photo_base64' => $request->input('photo_base64'), + 'photo_mime' => $request->input('photo_mime') ?: 'image/jpeg', + ]); + + return response()->json($response->json() ?? ['stored' => false], $response->status()); + } + /** * Show the form for creating a new resource. * diff --git a/public/js/mykad-reader-customer.js b/public/js/mykad-reader-customer.js index f07a8560..dce93c83 100644 --- a/public/js/mykad-reader-customer.js +++ b/public/js/mykad-reader-customer.js @@ -234,15 +234,69 @@ async function handleMyKadReader() { } } +function customerNeedsMykadPhoto(customer) { + if (!customer || typeof customer !== 'object') { + return false; + } + + return !customer.gambar_mykad && !customer.photo_base64; +} + +async function storeMykadPhotoForExistingCustomerIfMissing(customer, readerData) { + if (!customerNeedsMykadPhoto(customer)) { + return; + } + + if (!readerData || !readerData.has_photo || !readerData.photo_base64) { + return; + } + + if (typeof store_mykad_photo_endpoint === 'undefined' || !store_mykad_photo_endpoint) { + return; + } + + const csrfToken = document.querySelector('input[name="_token"]'); + const nric = getMyKadNumberWithoutHyphens( + customer.kpbaru || customer.kplama || readerData.nric || '' + ); + + if (!nric) { + return; + } + + try { + await fetch(store_mykad_photo_endpoint, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'X-Requested-With': 'XMLHttpRequest', + 'X-CSRF-TOKEN': csrfToken ? csrfToken.value : '', + }, + credentials: 'same-origin', + body: JSON.stringify({ + icno: nric, + photo_base64: readerData.photo_base64, + photo_mime: readerData.photo_mime || 'image/jpeg', + }), + }); + } catch (error) { + // Do not block customer search if photo backfill fails. + console.warn('Failed to store MyKad photo for existing customer', error); + } +} + function handleMyKadData(data) { return checkIfMyKadExists(data.nric) - .then(response => { + .then(async response => { let isResponseArray = Array.isArray(response); let resolved_response = isResponseArray ? response.pop() : response; if (!resolved_response || resolved_response === 'no-data') { populateDaftarPelangganModal(data); } else { + await storeMykadPhotoForExistingCustomerIfMissing(resolved_response, data); + let input_search_noic = document.querySelector("form[name='frmsearch'] input[name='noic']"); let form_search_noic = document.querySelector("form[name='frmsearch']"); diff --git a/resources/views/components/print/header.blade.php b/resources/views/components/print/header.blade.php index cd65e30a..52507e38 100644 --- a/resources/views/components/print/header.blade.php +++ b/resources/views/components/print/header.blade.php @@ -13,9 +13,9 @@