139 lines
3.8 KiB
PHP
139 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Barobill;
|
|
|
|
use App\Models\Tenants\Tenant;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
/**
|
|
* 바로빌 계좌 입출금 내역 모델
|
|
*
|
|
* 바로빌에서 조회한 입출금 내역에 계정과목을 추가하여 저장
|
|
*/
|
|
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',
|
|
'client_code',
|
|
'client_name',
|
|
'is_manual',
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_manual' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* 테넌트 관계
|
|
*/
|
|
public function tenant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Tenant::class);
|
|
}
|
|
|
|
/**
|
|
* 적요(summary)에서 상대계좌예금주명(cast/remark2) 중복 제거
|
|
* 바로빌 API 응답에서 TransRemark1에 TransRemark2가 포함되는 경우 정리
|
|
*/
|
|
public static function cleanSummary(string $summary, string $remark): string
|
|
{
|
|
if (empty($remark) || empty($summary) || ! str_contains($summary, $remark)) {
|
|
return $summary;
|
|
}
|
|
|
|
$result = rtrim($summary);
|
|
|
|
while (str_ends_with($result, $remark) && strlen($result) > strlen($remark)) {
|
|
$result = rtrim(substr($result, 0, -strlen($remark)));
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 거래 고유 키 생성 (매칭용)
|
|
* 숫자는 정수로 변환하여 형식 통일
|
|
*/
|
|
public function getUniqueKeyAttribute(): string
|
|
{
|
|
$cleanSummary = self::cleanSummary($this->summary ?? '', $this->cast ?? '');
|
|
|
|
return implode('|', [
|
|
$this->bank_account_num,
|
|
$this->trans_dt,
|
|
(int) $this->deposit,
|
|
(int) $this->withdraw,
|
|
(int) $this->balance,
|
|
$cleanSummary,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 바로빌 로그 데이터로부터 고유 키 생성 (정적 메서드)
|
|
*/
|
|
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,
|
|
$log['summary'] ?? '',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 캐시된 API 거래 내역 조회 (월별)
|
|
* is_manual=false인 거래만 조회 (API에서 자동 저장된 데이터)
|
|
*/
|
|
public static function getCachedByMonth(int $tenantId, string $accNum, string $startDate, string $endDate): \Illuminate\Database\Eloquent\Collection
|
|
{
|
|
return self::where('tenant_id', $tenantId)
|
|
->where('bank_account_num', $accNum)
|
|
->where('is_manual', false)
|
|
->whereBetween('trans_date', [$startDate, $endDate])
|
|
->orderBy('trans_date', 'desc')
|
|
->orderBy('trans_time', 'desc')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* 테넌트별 거래 내역 조회 (기간별)
|
|
*/
|
|
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);
|
|
}
|
|
}
|