Files
sam-manage/app/Models/Barobill/BankTransaction.php
김보곤 8291cdc39b feat: [database] codebridge DB 분리 - 118개 MNG 전용 테이블 connection 설정
- config/database.php에 codebridge connection 추가
- 78개 MNG 전용 모델에 $connection = 'codebridge' 설정
  - Admin (15): PM, 로드맵, API Explorer
  - Sales (16): 영업파트너, 수수료, 가망고객
  - Finance (9): 법인카드, 자금관리, 홈택스
  - Barobill (12): 은행/카드 동기화 관리
  - Interview (1), ESign (6), Equipment (2)
  - AI (3), Audit (3), 기타 (11)
2026-03-07 11:31:27 +09:00

140 lines
3.9 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 $connection = 'codebridge';
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);
}
}