040d25423d
GET /api/billing/wallet (баланс + тариф + runway), /transactions (пагинированный balance_transactions с фильтром type), /invoices (saas_invoices, real-but-empty до Б-1). TariffPlan модель + Tenant::tariff() relation + BalanceTransactionFactory. Sprint 2 Plan C, audit E3 (backend). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
87 lines
2.3 KiB
PHP
87 lines
2.3 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
namespace App\Models;
|
||
|
||
use Database\Factories\BalanceTransactionFactory;
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
use Illuminate\Database\Eloquent\Model;
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
||
/**
|
||
* Транзакция баланса тенанта (списание/пополнение лидов и/или рублей).
|
||
*
|
||
* Tenant-aware с RLS. log_hash — append-only audit chain (OPEN-И-15),
|
||
* заполняется триггером `audit_chain_hash()` BEFORE INSERT.
|
||
*
|
||
* Источник: db/schema.sql v8.7 §7.6, table `balance_transactions`.
|
||
*
|
||
* @mixin IdeHelperBalanceTransaction
|
||
*/
|
||
class BalanceTransaction extends Model
|
||
{
|
||
/** @use HasFactory<BalanceTransactionFactory> */
|
||
use HasFactory;
|
||
|
||
public const TYPE_TRIAL_BONUS = 'trial_bonus';
|
||
|
||
public const TYPE_TOPUP = 'topup';
|
||
|
||
public const TYPE_LEAD_CHARGE = 'lead_charge';
|
||
|
||
public const TYPE_REFUND = 'refund';
|
||
|
||
public const TYPE_MANUAL_ADJUSTMENT = 'manual_adjustment';
|
||
|
||
public const TYPE_HISTORICAL_IMPORT = 'historical_import';
|
||
|
||
public const TYPE_CHARGEBACK_WRITEDOWN = 'chargeback_writedown';
|
||
|
||
public const TYPE_CHARGEBACK_REPAYMENT = 'chargeback_repayment';
|
||
|
||
public $timestamps = false;
|
||
|
||
protected $fillable = [
|
||
'tenant_id',
|
||
'type',
|
||
'amount_rub',
|
||
'amount_leads',
|
||
'balance_rub_after',
|
||
'balance_leads_after',
|
||
'description',
|
||
'related_type',
|
||
'related_id',
|
||
'user_id',
|
||
'admin_user_id',
|
||
'created_at',
|
||
];
|
||
|
||
protected function casts(): array
|
||
{
|
||
return [
|
||
'tenant_id' => 'integer',
|
||
'amount_rub' => 'decimal:2',
|
||
'amount_leads' => 'integer',
|
||
'balance_rub_after' => 'decimal:2',
|
||
'balance_leads_after' => 'integer',
|
||
'related_id' => 'integer',
|
||
'user_id' => 'integer',
|
||
'admin_user_id' => 'integer',
|
||
'created_at' => 'datetime',
|
||
];
|
||
}
|
||
|
||
/** @return BelongsTo<Tenant, $this> */
|
||
public function tenant(): BelongsTo
|
||
{
|
||
return $this->belongsTo(Tenant::class);
|
||
}
|
||
|
||
/** @return BelongsTo<User, $this> */
|
||
public function user(): BelongsTo
|
||
{
|
||
return $this->belongsTo(User::class);
|
||
}
|
||
}
|