Files
sam-manage/app/Models/Barobill/BankTransactionOverride.php

81 lines
2.0 KiB
PHP
Raw Normal View History

<?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);
}
/**
* 여러 고유키에 대한 오버라이드 조회
* @return Collection<string, self> key가 unique_key인 컬렉션
*/
public static function getByUniqueKeys(int $tenantId, array $uniqueKeys): Collection
{
if (empty($uniqueKeys)) {
return collect();
}
return static::forTenant($tenantId)
->whereIn('unique_key', $uniqueKeys)
->get()
->keyBy('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,
]
);
}
}