'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); } }