Files
sam-manage/app/Models/Barobill/BankTransaction.php
pro cfbe014731 fix:저장된 계정과목 매칭 키 형식 통일
- decimal과 float 형식 차이로 매칭 실패하는 문제 수정
- 금액을 정수로 변환하여 키 생성

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 11:15:18 +09:00

100 lines
2.5 KiB
PHP

<?php
namespace App\Models\Barobill;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use App\Models\Tenants\Tenant;
/**
* 바로빌 계좌 입출금 내역 모델
*
* 바로빌에서 조회한 입출금 내역에 계정과목을 추가하여 저장
*/
class BankTransaction extends Model
{
protected $table = 'barobill_bank_transactions';
protected $fillable = [
'tenant_id',
'bank_account_num',
'bank_code',
'bank_name',
'trans_date',
'trans_time',
'trans_dt',
'deposit',
'withdraw',
'balance',
'summary',
'cast',
'memo',
'trans_office',
'account_code',
'account_name',
];
protected $casts = [
'deposit' => 'decimal:2',
'withdraw' => 'decimal:2',
'balance' => 'decimal:2',
];
/**
* 테넌트 관계
*/
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class);
}
/**
* 거래 고유 키 생성 (매칭용)
* 숫자는 정수로 변환하여 형식 통일
*/
public function getUniqueKeyAttribute(): string
{
return implode('|', [
$this->bank_account_num,
$this->trans_dt,
(int) $this->deposit,
(int) $this->withdraw,
(int) $this->balance,
]);
}
/**
* 바로빌 로그 데이터로부터 고유 키 생성 (정적 메서드)
*/
public static function generateUniqueKey(array $log): string
{
// trans_dt 생성: transDate + transTime
$transDt = ($log['transDate'] ?? '') . ($log['transTime'] ?? '');
return implode('|', [
$log['bankAccountNum'] ?? '',
$transDt,
$log['deposit'] ?? 0,
$log['withdraw'] ?? 0,
$log['balance'] ?? 0,
]);
}
/**
* 테넌트별 거래 내역 조회 (기간별)
*/
public static function getByDateRange(int $tenantId, string $startDate, string $endDate, ?string $accountNum = null)
{
$query = self::where('tenant_id', $tenantId)
->whereBetween('trans_date', [$startDate, $endDate])
->orderBy('trans_date', 'desc')
->orderBy('trans_time', 'desc');
if ($accountNum) {
$query->where('bank_account_num', $accountNum);
}
return $query->get()->keyBy(fn($item) => $item->unique_key);
}
}