- rate approval: API creates pending proposals, manager approves - customer_credits + credit_settlements tables - CustomerCreditService (grant/settle via wallet) - rebuilt CustomerCreditResource - CustomerFinancialController: real foreign-currency debt - WP login redirect + custom login page wiring
92 lines
2.2 KiB
PHP
92 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use App\Traits\Auditable;
|
|
|
|
class CustomerCredit extends Model
|
|
{
|
|
use Auditable;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'currency_code',
|
|
'amount',
|
|
'settled_amount',
|
|
'status',
|
|
'reason',
|
|
'reference',
|
|
'shipment_id',
|
|
'granted_by',
|
|
'granted_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'amount' => 'decimal:2',
|
|
'settled_amount' => 'decimal:2',
|
|
'granted_at' => 'datetime',
|
|
];
|
|
|
|
// ─── Relationships ───────────────────────────────
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function shipment(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Shipment::class);
|
|
}
|
|
|
|
public function grantedBy(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'granted_by');
|
|
}
|
|
|
|
public function settlements(): HasMany
|
|
{
|
|
return $this->hasMany(CreditSettlement::class)->orderBy('settled_at', 'desc');
|
|
}
|
|
|
|
// ─── Helpers ─────────────────────────────────────
|
|
|
|
/**
|
|
* مانده بدهی به ارز خارجی = amount - settled_amount
|
|
*/
|
|
public function getBalanceAttribute(): float
|
|
{
|
|
return (float) $this->amount - (float) $this->settled_amount;
|
|
}
|
|
|
|
public function isOpen(): bool
|
|
{
|
|
return in_array($this->status, ['open', 'partially_settled'], true);
|
|
}
|
|
|
|
public function isSettled(): bool
|
|
{
|
|
return $this->status === 'settled';
|
|
}
|
|
|
|
/**
|
|
* بازمحاسبه status بر اساس settled_amount.
|
|
*/
|
|
public function recomputeStatus(): void
|
|
{
|
|
$balance = $this->balance;
|
|
|
|
if ($balance <= 0.005) {
|
|
$this->status = 'settled';
|
|
} elseif ((float) $this->settled_amount > 0) {
|
|
$this->status = 'partially_settled';
|
|
} else {
|
|
$this->status = 'open';
|
|
}
|
|
|
|
$this->save();
|
|
}
|
|
} |