Files
sam-manage/app/Models/Barobill/BankTransaction.php
pro 3c32c5917c feat:입출금내역 계정과목 추가 및 엑셀 다운로드 기능
- BankTransaction 모델: 입출금 내역 저장 (계정과목 포함)
- 바로빌 데이터와 DB 저장 데이터 매칭하여 계정과목 유지
- 계정과목 드롭다운 선택 및 저장 기능
- 엑셀(CSV) 다운로드 기능
- 저장된 행은 녹색 배경으로 표시

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

97 lines
2.3 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 $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);
}
}