Files
portal-api/app/Models/IpPool.php
T
baodev0andClaude Opus 5.5 46aa138eec feat(admin): IP pool allocation, billing exports, service attachments
- IP pool: sheet import (xlsx/csv) matched by address, bulk allocate/revoke
  with notification email (ip_allocated/ip_revoked), ip_pool_logs history.
- Orders/invoices: Excel export on branded template (BrandedReportWriter),
  shared billing filters, order item service info in list responses.
- Web design quote Excel export; activity log export.
- Service attachments with copy-to-invoice; deployment invoice attachments.
- Customer email attachments stored on disk and attached by path.
- Admin password change endpoint; user.fullname backfill migration;
  orders.user_id backfill from logs.
- Disable deploy workflow until CI/CD is set up.
- Ignore stored customer email and service attachment uploads.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 19:58:36 +07:00

147 lines
3.9 KiB
PHP

<?php
namespace App\Models;
use App\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
class IpPool extends Model
{
use HasFactory, SoftDeletes, LogsActivity;
protected $fillable = [
'uuid',
'ip_address',
'ip_block',
'gateway',
'cidr',
'netmask',
'module_network_id',
'status',
'owner_type',
'ip_role',
'parent_ip',
'customer_name',
'customer_id',
'service_name',
'allocated_at',
'revoked_at',
'service_id',
'contact_email',
'contact_cc',
'contact_phone',
'monthly_cost',
'selling_price',
'additional_info',
'last_email_type',
'last_email_at',
'updated_by',
'note',
];
protected $casts = [
'allocated_at' => 'date',
'revoked_at' => 'date',
'last_email_at' => 'datetime',
'monthly_cost' => 'decimal:2',
'selling_price' => 'decimal:2',
];
protected static function boot(): void
{
parent::boot();
static::creating(function (self $model) {
if (empty($model->uuid)) {
$model->uuid = (string) Str::uuid();
}
});
// Keep ip_pool_contacts in sync whenever contact_email/contact_cc change via normal Eloquent saves.
// Bulk import (WithUpserts) bypasses this — call resyncAllContacts() after Excel::import() instead.
static::saved(function (self $model) {
if ($model->wasChanged(['contact_email', 'contact_cc']) || $model->wasRecentlyCreated) {
$model->syncContacts();
}
});
}
public function network()
{
return $this->belongsTo(ModuleNetwork::class, 'module_network_id', 'id');
}
public function customer()
{
return $this->belongsTo(Customer::class);
}
public function logs()
{
return $this->hasMany(IpPoolLog::class)->latest('id');
}
public function service()
{
return $this->belongsTo(Service::class, 'service_id');
}
public function contacts()
{
return $this->hasMany(IpPoolContact::class);
}
/**
* contact_email/contact_cc may hold multiple addresses separated by comma/semicolon
* (e.g. imported from Google Sheet). Split into a clean array, dropping invalid/empty entries.
*/
public static function splitEmails(?string $value): array
{
if (!$value) {
return [];
}
return collect(preg_split('/[,;]+/', $value))
->map(fn ($e) => trim($e))
->filter(fn ($e) => $e !== '' && filter_var($e, FILTER_VALIDATE_EMAIL))
->unique()
->values()
->all();
}
public function syncContacts(): void
{
$rows = [];
foreach (self::splitEmails($this->contact_email) as $email) {
$rows[] = ['ip_pool_id' => $this->id, 'email' => $email, 'type' => 'to'];
}
foreach (self::splitEmails($this->contact_cc) as $email) {
$rows[] = ['ip_pool_id' => $this->id, 'email' => $email, 'type' => 'cc'];
}
$this->contacts()->delete();
if ($rows) {
IpPoolContact::insert($rows);
}
}
/**
* Full resync for bulk paths (CSV import) that bypass the saved() hook above.
*/
public static function resyncAllContacts(): void
{
self::withTrashed()
->where(function ($q) {
$q->whereNotNull('contact_email')->orWhereNotNull('contact_cc');
})
->chunkById(500, function ($pools) {
foreach ($pools as $pool) {
$pool->syncContacts();
}
});
}
}