diff --git a/app/Http/Controllers/Barobill/EaccountController.php b/app/Http/Controllers/Barobill/EaccountController.php index 1b51e9f0..943743c1 100644 --- a/app/Http/Controllers/Barobill/EaccountController.php +++ b/app/Http/Controllers/Barobill/EaccountController.php @@ -938,21 +938,20 @@ public function save(Request $request): JsonResponse ]); $savedUniqueKeys[] = $uniqueKey; - // Upsert: 있으면 업데이트, 없으면 생성 - // balance 포함: 같은 금액이라도 잔액이 다르면 별도 거래로 구분 - $existing = BankTransaction::where('tenant_id', $tenantId) + // 순수 Query Builder로 Upsert (Eloquent 모델 우회) + // Eloquent의 decimal cast가 dirty 감지하여 unique 제약조건 위반을 일으키므로 + $existingId = DB::table('barobill_bank_transactions') + ->where('tenant_id', $tenantId) ->where('bank_account_num', $data['bank_account_num']) ->where('trans_dt', $transDt) - ->whereRaw('CAST(deposit AS SIGNED) = ?', [(int) $data['deposit']]) - ->whereRaw('CAST(withdraw AS SIGNED) = ?', [(int) $data['withdraw']]) - ->whereRaw('CAST(balance AS SIGNED) = ?', [(int) $data['balance']]) - ->first(); + ->where('deposit', $data['deposit']) + ->where('withdraw', $data['withdraw']) + ->where('balance', $data['balance']) + ->value('id'); - if ($existing) { - // Query Builder 사용: Eloquent의 decimal:2 cast가 balance를 - // dirty로 잘못 감지하여 unique 제약조건 위반을 일으키는 문제 방지 + if ($existingId) { DB::table('barobill_bank_transactions') - ->where('id', $existing->id) + ->where('id', $existingId) ->update([ 'summary' => $data['summary'], 'cast' => $data['cast'], @@ -963,7 +962,10 @@ public function save(Request $request): JsonResponse ]); $updated++; } else { - BankTransaction::create($data); + DB::table('barobill_bank_transactions')->insert(array_merge($data, [ + 'created_at' => now(), + 'updated_at' => now(), + ])); $saved++; } }