fix:바로빌 거래 저장 시 잔액 차이 중복 방지 및 기존 중복 자동 정리
This commit is contained in:
@@ -985,19 +985,30 @@ public function save(Request $request): JsonResponse
|
||||
$savedUniqueKeys[] = $uniqueKey;
|
||||
|
||||
// 순수 Query Builder로 Upsert (Eloquent 모델 우회)
|
||||
$existingId = DB::table('barobill_bank_transactions')
|
||||
// balance 제외하고 매칭 → 같은 거래가 잔액만 다르게 들어와도 중복 방지
|
||||
$existingIds = DB::table('barobill_bank_transactions')
|
||||
->where('tenant_id', $tenantId)
|
||||
->where('bank_account_num', $data['bank_account_num'])
|
||||
->where('trans_dt', $transDt)
|
||||
->where('deposit', $data['deposit'])
|
||||
->where('withdraw', $data['withdraw'])
|
||||
->where('balance', $data['balance'])
|
||||
->value('id');
|
||||
->orderByDesc('id')
|
||||
->pluck('id');
|
||||
|
||||
if ($existingIds->isNotEmpty()) {
|
||||
$keepId = $existingIds->first(); // 최신 건 유지
|
||||
|
||||
// 중복 건 삭제 (잔액만 다른 이전 레코드)
|
||||
if ($existingIds->count() > 1) {
|
||||
DB::table('barobill_bank_transactions')
|
||||
->whereIn('id', $existingIds->slice(1)->values())
|
||||
->delete();
|
||||
}
|
||||
|
||||
if ($existingId) {
|
||||
DB::table('barobill_bank_transactions')
|
||||
->where('id', $existingId)
|
||||
->where('id', $keepId)
|
||||
->update([
|
||||
'balance' => $data['balance'],
|
||||
'summary' => $data['summary'],
|
||||
'cast' => $data['cast'],
|
||||
'trans_office' => $data['trans_office'],
|
||||
|
||||
Reference in New Issue
Block a user