100 lines
2.7 KiB
PHP
100 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Barobill;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Collection;
|
|
|
|
/**
|
|
* 바로빌 계좌 거래내역 적요/내용 수정 오버라이드 모델
|
|
*/
|
|
class BankTransactionOverride extends Model
|
|
{
|
|
protected $table = 'barobill_bank_transaction_overrides';
|
|
|
|
protected $fillable = [
|
|
'tenant_id',
|
|
'unique_key',
|
|
'modified_summary',
|
|
'modified_cast',
|
|
];
|
|
|
|
/**
|
|
* 테넌트별 조회 스코프
|
|
*/
|
|
public function scopeForTenant($query, int $tenantId)
|
|
{
|
|
return $query->where('tenant_id', $tenantId);
|
|
}
|
|
|
|
/**
|
|
* 고유키로 조회
|
|
*/
|
|
public function scopeByUniqueKey($query, string $uniqueKey)
|
|
{
|
|
return $query->where('unique_key', $uniqueKey);
|
|
}
|
|
|
|
/**
|
|
* 여러 고유키에 대한 오버라이드 조회
|
|
* 레거시(summary 미포함) 형식과 신규(summary 포함) 형식 모두 매칭
|
|
*
|
|
* @return Collection<string, self> key가 unique_key인 컬렉션
|
|
*/
|
|
public static function getByUniqueKeys(int $tenantId, array $uniqueKeys): Collection
|
|
{
|
|
if (empty($uniqueKeys)) {
|
|
return collect();
|
|
}
|
|
|
|
// 레거시 키도 함께 검색
|
|
$allKeys = $uniqueKeys;
|
|
$legacyToNewMap = [];
|
|
foreach ($uniqueKeys as $key) {
|
|
$parts = explode('|', $key);
|
|
if (count($parts) === 6) {
|
|
$legacyKey = implode('|', array_slice($parts, 0, 5));
|
|
$allKeys[] = $legacyKey;
|
|
$legacyToNewMap[$legacyKey] = $key;
|
|
}
|
|
}
|
|
|
|
$results = static::forTenant($tenantId)
|
|
->whereIn('unique_key', array_unique($allKeys))
|
|
->get();
|
|
|
|
// 레거시 키로 매칭된 것을 새 키로 리맵하여 keyBy
|
|
return $results->keyBy(function ($item) use ($legacyToNewMap) {
|
|
return $legacyToNewMap[$item->unique_key] ?? $item->unique_key;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 오버라이드 저장 또는 업데이트
|
|
*/
|
|
public static function saveOverride(
|
|
int $tenantId,
|
|
string $uniqueKey,
|
|
?string $modifiedSummary,
|
|
?string $modifiedCast
|
|
): ?self {
|
|
// 둘 다 null이거나 빈 문자열이면 기존 레코드 삭제
|
|
if (empty($modifiedSummary) && empty($modifiedCast)) {
|
|
static::forTenant($tenantId)->byUniqueKey($uniqueKey)->delete();
|
|
|
|
return null;
|
|
}
|
|
|
|
return static::updateOrCreate(
|
|
[
|
|
'tenant_id' => $tenantId,
|
|
'unique_key' => $uniqueKey,
|
|
],
|
|
[
|
|
'modified_summary' => $modifiedSummary ?: null,
|
|
'modified_cast' => $modifiedCast ?: null,
|
|
]
|
|
);
|
|
}
|
|
}
|