Files

151 lines
4.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Rooyesh\Wallet\models\Transaction;
class Withdraw extends Model
{
use HasFactory, Selector;
protected $fillable = [
'customer_id',
'user_id',
'cash',
'gold',
'note',
2026-06-18 19:32:28 +03:30
'extra_note',
'tracked_number',
'bank_account_id',
2026-07-27 16:05:54 +03:30
'address_id',
'date_at',
'sts',
'type',
'receipt',
'deposit_identifier',
'reject_reason',
];
protected $appends = [
'receipt_url',
];
public function getReceiptUrlAttribute(): ?string
{
return File::publicUrl($this->receipt);
}
static $STS = [
1 => 'در انتظار تایید',
2 => 'واریز شد',
4 => 'واریز توسط ادمین',
5 => 'پاداش معرفی',
9 => 'سود زیر مجموعه',
3 => 'رد شده',
50 => 'در انتظار ارسال',
51 => 'ارسال شده',
52 => 'لغو شده',
2026-07-08 11:25:19 +03:30
53 => 'در انتظار تعیین مجدد قیمت',
];
public function customer()
{
return $this->belongsTo('App\Models\Customer')->select(['id', 'name', 'mobile', 'n_code']);
}
2026-07-27 16:05:54 +03:30
public function address()
{
2026-07-27 16:05:54 +03:30
return $this->belongsTo(Address::class, 'address_id');
}
public function Transaction()
{
return $this->hasMany(Transaction::class, 'withdraw_id', 'id');
}
public function bank_account()
{
return $this->hasOne('App\Models\BankAccount', 'id', 'bank_account_id');
}
2026-07-08 11:25:19 +03:30
/** برداشت فیزیکی — درخواست تحویل محصول */
public static function scopePhysicalWithdrawals($query)
{
return $query->where('note', 'like', '%برداشت فیزیکی%')
->whereBetween('sts', [49, 70]);
}
public static function isPhysicalWithdrawNote(?string $note): bool
{
return $note !== null && str_contains($note, 'برداشت فیزیکی');
}
public static function applyFlowFilter($query, ?string $flowType = 'all')
{
if (!$flowType || $flowType === 'all') {
return $query;
}
if ($flowType === 'withdraw') {
return static::scopeWithdrawals($query);
}
if ($flowType === 'deposit') {
return static::scopeDeposits($query);
}
return $query;
}
/** برداشت: خروج پول/طلا از کیف (درخواست مشتری، برداشت فیزیکی، کسر ادمین) */
public static function scopeWithdrawals($query)
{
return $query->where(function ($q) {
$q->where('note', 'like', '%برداشت%')
->orWhere('type', 2)
->orWhereBetween('sts', [50, 52])
->orWhere(function ($pending) {
$pending->where('sts', 1)
->whereNotNull('bank_account_id');
})
->orWhere(function ($completed) {
$completed->where('sts', 2)
->where('note', 'like', '%برداشت%');
})
->orWhere(function ($deduct) {
2026-07-01 20:59:24 +03:30
$deduct->where('cash', '<', 0)
->orWhere('gold', '<', 0);
});
});
}
2026-09-12 10:12:43 +03:30
/** واریز: ورود پول/طلا به کیف (درگاه، شارژ ادمین، پاداش، کارت هدیه ریالی) — بدون کسر ادمین */
public static function scopeDeposits($query)
{
return $query->where(function ($q) {
$q->where(function ($inner) {
$inner->where('note', 'like', '%واریز%')
->orWhere('type', 1)
2026-09-12 10:12:43 +03:30
->orWhereIn('sts', [4, 5, 9])
// کارت هدیه ریالی (نه فلزی/مسی)
->orWhere(function ($gift) {
$gift->where('note', 'like', '%کارت هدیه%')
->where('cash', '>', 0);
});
})->where(function ($excludeWithdrawNotes) {
$excludeWithdrawNotes->whereNull('note')
->orWhere('note', 'not like', '%برداشت%');
2026-07-01 20:59:24 +03:30
})->whereNotBetween('sts', [50, 52])
->where(function ($positive) {
$positive->where(function ($cash) {
$cash->whereNull('cash')->orWhere('cash', '>=', 0);
})->where(function ($gold) {
$gold->whereNull('gold')->orWhere('gold', '>=', 0);
});
});
});
}
}