DONE: store mykad image in database

This commit is contained in:
ISMAIL MASSERAN
2026-09-03 11:29:13 +08:00
parent 52c3f5a52f
commit 0213f40b11
4 changed files with 170 additions and 5 deletions
+7
View File
@@ -15,3 +15,10 @@ FLUSH PRIVILEGES;
Gadai=Pembiayaan Gadai=Pembiayaan
Tebus=penyelesaian penuh/ansuran 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
+2 -1
View File
@@ -62,7 +62,8 @@ class Customer extends Model
'telefon2', 'telefon2',
'nota_pekerjaan', 'nota_pekerjaan',
'tagpelanggan', 'tagpelanggan',
'tag_anggota' 'tag_anggota',
'gambar_mykad'
]; ];
/** /**
+114 -3
View File
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Audit; use App\Audit;
use App\Marhun; use App\Marhun;
use App\BankMaster; use App\BankMaster;
use App\ARFiles;
use App\Customer; use App\Customer;
use App\Gadai; use App\Gadai;
use App\Lelong; use App\Lelong;
@@ -64,7 +65,7 @@ class CustomerController extends Controller
->orWhere('kpbaru','=',$ic) ->orWhere('kpbaru','=',$ic)
->first(); ->first();
return response()->json($customer); return response()->json($this->withMykadPhoto($customer));
} }
public function showOneCustomerByICAdmin(Request $request){ public function showOneCustomerByICAdmin(Request $request){
@@ -74,7 +75,7 @@ class CustomerController extends Controller
->orWhere('kpbaru','=',$request->ic) ->orWhere('kpbaru','=',$request->ic)
->first(); ->first();
return response()->json($customer); return response()->json($this->withMykadPhoto($customer));
} }
public function showOneCustomerByICMobile($ic){ public function showOneCustomerByICMobile($ic){
@@ -368,6 +369,8 @@ class CustomerController extends Controller
'tujuangadai'=>$request->tujuangadai 'tujuangadai'=>$request->tujuangadai
]); ]);
$this->attachMykadPhoto($customer, $request);
$audit = new Audit(); $audit = new Audit();
$audit->users = $request->teller; $audit->users = $request->teller;
$audit->actions = "Daftar Pelanggan Baru"; $audit->actions = "Daftar Pelanggan Baru";
@@ -377,7 +380,7 @@ class CustomerController extends Controller
$audit->updated_at = $current; $audit->updated_at = $current;
$audit->save(); $audit->save();
return response()->json($customer, 201); return response()->json($this->withMykadPhoto($customer), 201);
} }
public function customerUpdate($nokp,Request $request){ public function customerUpdate($nokp,Request $request){
@@ -1124,6 +1127,114 @@ public function AdminsearchCustomerBySAG(Request $request)
return response()->json(['message' => 'Customer PDPA updated successfully.'], 200); 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) // public function createNotaArcc($kodcaw, Request $request)
// { // {
// $current = Carbon::now(); // $current = Carbon::now();
@@ -0,0 +1,46 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddGambarMykadToCustomerTable extends Migration
{
/**
* Master DB (mysql7) — stores arrahn_files.id for the MyKad photo.
*
* @return void
*/
public function up()
{
if (
Schema::connection('mysql7')->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');
});
}
}
}