Files
sam-api/app/Models/Barobill/BarobillPricingPolicy.php

83 lines
2.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Models\Barobill;
use Illuminate\Database\Eloquent\Model;
class BarobillPricingPolicy extends Model
{
protected $table = 'barobill_pricing_policies';
public const TYPE_CARD = 'card';
public const TYPE_TAX_INVOICE = 'tax_invoice';
public const TYPE_BANK_ACCOUNT = 'bank_account';
protected $fillable = [
'service_type',
'name',
'description',
'free_quota',
'free_quota_unit',
'additional_unit',
'additional_unit_label',
'additional_price',
'is_active',
'sort_order',
];
protected $casts = [
'free_quota' => 'integer',
'additional_unit' => 'integer',
'additional_price' => 'integer',
'is_active' => 'boolean',
'sort_order' => 'integer',
];
// =========================================================================
// 스코프
// =========================================================================
public function scopeActive($query)
{
return $query->where('is_active', true);
}
// =========================================================================
// 헬퍼 메서드
// =========================================================================
public static function getByServiceType(string $serviceType): ?self
{
return static::active()->where('service_type', $serviceType)->first();
}
public static function getAllActive()
{
return static::active()->orderBy('sort_order')->get();
}
public function getServiceTypeLabelAttribute(): string
{
return match ($this->service_type) {
self::TYPE_CARD => '카드조회',
self::TYPE_TAX_INVOICE => '전자세금계산서',
self::TYPE_BANK_ACCOUNT => '계좌조회',
default => $this->service_type,
};
}
public function calculateBilling(int $usageCount): int
{
if ($usageCount <= $this->free_quota) {
return 0;
}
$excess = $usageCount - $this->free_quota;
$units = (int) ceil($excess / max($this->additional_unit, 1));
return $units * $this->additional_price;
}
}