- 기존 bank_transactions 테이블과 충돌 방지 - 테이블명을 barobill_bank_transactions로 변경 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
99 lines
2.4 KiB
PHP
99 lines
2.4 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,
|
|
$this->deposit,
|
|
$this->withdraw,
|
|
$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);
|
|
}
|
|
}
|