Files
sam-api/app/Models/Tenants/BarobillSetting.php
김보곤 18a6f3e7aa refactor: [barobill] 바로빌 연동 코드 전면 개선
- config/services.php에 barobill 설정 등록 (운영/테스트 모드 분기 정상화)
- BarobillSetting 모델에 BelongsToTenant 적용 및 use_* 필드 casts 추가
- BarobillService API URL을 baroservice.com(SOAP)으로 수정
- BarobillService callApi 메서드 경로 하드코딩 제거 (서비스별 분기)
- BarobillService 예외 이중 래핑 문제 수정
- BarobillController URL 메서드 중복 코드 제거
- 누락 모델 16개 생성 (MNG 패턴 준수, BelongsToTenant 적용)
- 바로빌 전 테이블 options JSON 컬럼 추가 마이그레이션
2026-03-11 18:16:59 +09:00

166 lines
3.9 KiB
PHP

<?php
namespace App\Models\Tenants;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Crypt;
class BarobillSetting extends Model
{
use BelongsToTenant;
protected $fillable = [
'tenant_id',
'corp_num',
'cert_key',
'barobill_id',
'corp_name',
'ceo_name',
'addr',
'biz_type',
'biz_class',
'contact_id',
'contact_name',
'contact_tel',
'is_active',
'auto_issue',
'use_tax_invoice',
'use_bank_account',
'use_card_usage',
'use_hometax',
'verified_at',
'created_by',
'updated_by',
];
protected $casts = [
'is_active' => 'boolean',
'auto_issue' => 'boolean',
'use_tax_invoice' => 'boolean',
'use_bank_account' => 'boolean',
'use_card_usage' => 'boolean',
'use_hometax' => 'boolean',
'verified_at' => 'datetime',
];
protected $hidden = [
'cert_key',
];
// =========================================================================
// 암호화 처리 (cert_key)
// =========================================================================
/**
* cert_key 암호화 저장
*/
public function setCertKeyAttribute(?string $value): void
{
$this->attributes['cert_key'] = $value ? Crypt::encryptString($value) : null;
}
/**
* cert_key 복호화 조회
*/
public function getCertKeyAttribute(?string $value): ?string
{
if (! $value) {
return null;
}
try {
return Crypt::decryptString($value);
} catch (\Exception $e) {
return null;
}
}
// =========================================================================
// 관계 정의
// =========================================================================
/**
* 테넌트 관계
*/
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class);
}
/**
* 생성자 관계
*/
public function creator(): BelongsTo
{
return $this->belongsTo(\App\Models\Members\User::class, 'created_by');
}
/**
* 수정자 관계
*/
public function updater(): BelongsTo
{
return $this->belongsTo(\App\Models\Members\User::class, 'updated_by');
}
// =========================================================================
// 헬퍼 메서드
// =========================================================================
/**
* 연동 가능 여부
*/
public function canConnect(): bool
{
return $this->is_active
&& ! empty($this->corp_num)
&& ! empty($this->attributes['cert_key'])
&& ! empty($this->barobill_id);
}
/**
* 검증 완료 여부
*/
public function isVerified(): bool
{
return $this->verified_at !== null;
}
/**
* 사업자번호 포맷 (하이픈 포함)
*/
public function getFormattedCorpNumAttribute(): string
{
$num = $this->corp_num;
if (strlen($num) === 10) {
return substr($num, 0, 3).'-'.substr($num, 3, 2).'-'.substr($num, 5);
}
return $num;
}
/**
* 활성화된 서비스 목록
*/
public function getActiveServicesAttribute(): array
{
$services = [];
if ($this->use_tax_invoice) {
$services[] = 'tax_invoice';
}
if ($this->use_bank_account) {
$services[] = 'bank_account';
}
if ($this->use_card_usage) {
$services[] = 'card_usage';
}
if ($this->use_hometax) {
$services[] = 'hometax';
}
return $services;
}
}