From 0213f40b11752db00328ef4d93054c7783b3e958 Mon Sep 17 00:00:00 2001 From: ISMAIL MASSERAN Date: Thu, 3 Sep 2026 11:29:13 +0800 Subject: [PATCH] DONE: store mykad image in database --- README.md | 9 +- app/Customer.php | 3 +- app/Http/Controllers/CustomerController.php | 117 +++++++++++++++++- ...000_add_gambar_mykad_to_customer_table.php | 46 +++++++ 4 files changed, 170 insertions(+), 5 deletions(-) create mode 100644 database/migrations/2026_09_03_100000_add_gambar_mykad_to_customer_table.php diff --git a/README.md b/README.md index 3b1ebc0..5636bf2 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,11 @@ FLUSH PRIVILEGES; ``` Gadai=Pembiayaan -Tebus=penyelesaian penuh/ansuran \ No newline at end of file +Tebus=penyelesaian penuh/ansuran + +## Load db dump without using docker +```bash +docker exec -i api_arrahn_staging_mysql mysql -uroot -proot erahn_test < erahn_test.sql +``` + +## Disable the cawangan in arrahn_master_db \ No newline at end of file diff --git a/app/Customer.php b/app/Customer.php index 557e865..b463f8c 100644 --- a/app/Customer.php +++ b/app/Customer.php @@ -62,7 +62,8 @@ class Customer extends Model 'telefon2', 'nota_pekerjaan', 'tagpelanggan', - 'tag_anggota' + 'tag_anggota', + 'gambar_mykad' ]; /** diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index e7c3f16..0b250dd 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use App\Audit; use App\Marhun; use App\BankMaster; +use App\ARFiles; use App\Customer; use App\Gadai; use App\Lelong; @@ -64,7 +65,7 @@ class CustomerController extends Controller ->orWhere('kpbaru','=',$ic) ->first(); - return response()->json($customer); + return response()->json($this->withMykadPhoto($customer)); } public function showOneCustomerByICAdmin(Request $request){ @@ -74,7 +75,7 @@ class CustomerController extends Controller ->orWhere('kpbaru','=',$request->ic) ->first(); - return response()->json($customer); + return response()->json($this->withMykadPhoto($customer)); } public function showOneCustomerByICMobile($ic){ @@ -368,6 +369,8 @@ class CustomerController extends Controller 'tujuangadai'=>$request->tujuangadai ]); + $this->attachMykadPhoto($customer, $request); + $audit = new Audit(); $audit->users = $request->teller; $audit->actions = "Daftar Pelanggan Baru"; @@ -377,7 +380,7 @@ class CustomerController extends Controller $audit->updated_at = $current; $audit->save(); - return response()->json($customer, 201); + return response()->json($this->withMykadPhoto($customer), 201); } public function customerUpdate($nokp,Request $request){ @@ -1124,6 +1127,114 @@ public function AdminsearchCustomerBySAG(Request $request) return response()->json(['message' => 'Customer PDPA updated successfully.'], 200); } + /** + * Save MyKad photo into arrahn_files and link it on customer.gambar_mykad. + */ + protected function attachMykadPhoto($customer, Request $request) + { + if (!$customer) { + return; + } + + $raw = $this->photoBase64FromRequest($request); + if ($raw === '') { + return; + } + + try { + $fileId = $this->storeMykadPhoto($raw, $request->input('photo_mime', $request->json('photo_mime')), $customer); + if (!$fileId) { + return; + } + + Customer::on('mysql7') + ->where('kpbaru', $customer->kpbaru) + ->where('kplama', $customer->kplama) + ->update(['gambar_mykad' => $fileId]); + + $customer->gambar_mykad = $fileId; + } catch (\Throwable $e) { + Log::error('Failed to attach MyKad photo: ' . $e->getMessage()); + } + } + + protected function photoBase64FromRequest(Request $request) + { + $raw = $request->input('photo_base64'); + if ($raw === null || $raw === '') { + $raw = $request->json('photo_base64'); + } + + return is_string($raw) ? $raw : ''; + } + + protected function storeMykadPhoto($raw, $mime, $customer) + { + $commaPos = strpos($raw, ','); + if ($commaPos !== false) { + $raw = substr($raw, $commaPos + 1); + } + $raw = preg_replace('/\s+/', '', $raw); + + if ($raw === '') { + return null; + } + + $decoded = base64_decode($raw, true); + if ($decoded === false) { + $decoded = base64_decode($raw); + } + if ($decoded === false || $decoded === '') { + Log::warning('MyKad photo skipped: invalid base64'); + return null; + } + + if (strlen($raw) > 400000) { + Log::warning('MyKad photo skipped: payload too large'); + return null; + } + + $mime = strtolower((string) ($mime ?: 'image/jpeg')); + if (!in_array($mime, ['image/jpeg', 'image/jpg', 'image/png'], true)) { + $mime = 'image/jpeg'; + } + + $nric = preg_replace('/[^0-9A-Za-z]/', '', (string) ($customer->kpbaru ?: '')); + $ext = $mime === 'image/png' ? 'png' : 'jpg'; + + $file = new ARFiles(); + $file->data = $raw; + $file->filename = ($nric !== '' ? $nric : 'mykad') . '.' . $ext; + $file->type = $mime; + $file->tarikhmasa = Carbon::now()->toDateTimeString(); + $file->save(); + + return $file->id; + } + + protected function withMykadPhoto($customer) + { + if (!$customer) { + return $customer; + } + + $payload = $customer->toArray(); + $payload['photo_base64'] = null; + $payload['photo_mime'] = null; + + if (empty($customer->gambar_mykad)) { + return $payload; + } + + $file = ARFiles::on('mysql7')->where('id', $customer->gambar_mykad)->first(); + if ($file && $file->data) { + $payload['photo_base64'] = $file->data; + $payload['photo_mime'] = $file->type ?: 'image/jpeg'; + } + + return $payload; + } + // public function createNotaArcc($kodcaw, Request $request) // { // $current = Carbon::now(); diff --git a/database/migrations/2026_09_03_100000_add_gambar_mykad_to_customer_table.php b/database/migrations/2026_09_03_100000_add_gambar_mykad_to_customer_table.php new file mode 100644 index 0000000..5f5d52e --- /dev/null +++ b/database/migrations/2026_09_03_100000_add_gambar_mykad_to_customer_table.php @@ -0,0 +1,46 @@ +hasTable('customer') + && !Schema::connection('mysql7')->hasColumn('customer', 'gambar_mykad') + ) { + Schema::connection('mysql7')->table('customer', function (Blueprint $table) { + if (Schema::connection('mysql7')->hasColumn('customer', 'tag_anggota')) { + $table->unsignedBigInteger('gambar_mykad')->nullable()->after('tag_anggota'); + } else { + $table->unsignedBigInteger('gambar_mykad')->nullable(); + } + }); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + if ( + Schema::connection('mysql7')->hasTable('customer') + && Schema::connection('mysql7')->hasColumn('customer', 'gambar_mykad') + ) { + Schema::connection('mysql7')->table('customer', function (Blueprint $table) { + $table->dropColumn('gambar_mykad'); + }); + } + } +}