| Server IP : 13.235.167.51 / Your IP : 216.73.217.116 Web Server : Apache System : Linux machinox-server 6.17.0-1013-aws #13~24.04.1-Ubuntu SMP Fri Apr 24 21:36:58 UTC 2026 aarch64 User : root ( 0) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/app.machinox.in/app/Models/ |
Upload File : |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Ramsey\Uuid\Uuid;
class Transaction extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = [
'uuid',
'user_id',
'custom_trx_id',
'type',
'ad_id',
'job_id',
'state',
'disc_value',
'disc_percent',
'taxes',
'sub_total',
'grand_total',
'trx_id',
'trx_amount',
'currency',
'trx_date_time',
'trx_pay_mode',
'razorpay_payment_id',
'trx_status',
'payment_status',
];
protected $casts = [
'state' => 'array',
];
// const STATUS_PENDING = 'Pending';
// const STATUS_CANCELED = 'Cancelled';
// const STATUS_FAILED = 'Failed';
// const STATUS_SUCCESS = 'Success';
protected static function boot()
{
parent::boot();
static::creating(function ($transaction) {
$last = Transaction::orderBy('id', 'desc')->first();
$nextNumber = $last ? ((int) filter_var($last->cust_trx_id, FILTER_SANITIZE_NUMBER_INT)) + 1 : 1;
$transaction->cust_trx_id = 'trx' . str_pad($nextNumber, 4, '0', STR_PAD_LEFT);
$transaction->uuid = Uuid::uuid4();
});
}
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
public function plans()
{
return $this->belongsToMany(
SubscriptionPlan::class,
'transaction_subscription_plans',
'transaction_id',
'subs_plan_id'
)->withTimestamps();
}
public function ad()
{
return $this->belongsTo(Advertisement::class, 'ad_id', 'id');
}
public function job()
{
return $this->belongsTo(PostJobs::class, 'job_id', 'id');
}
public function getTransactionByToken(string $token)
{
return $this->where('uuid', $token)->first();
}
public function scopeDefaultModels(Builder $builder): void
{
$builder->with([
'user:id,name',
'plans' => function ($q) {
$q->select('subscription_plans.id', 'type', 'duration');
},
'ad:id,title',
'job:id,job_name',
]);
}
public function scopeSearchKeyword(Builder $builder, $keyword): void
{
$builder->when($keyword, function ($query) use ($keyword) {
$query->where(function ($q) use ($keyword) {
$q->whereHas('plans', function ($planQuery) use ($keyword) {
$planQuery->where('type', 'LIKE', '%' . $keyword . '%')
->orWhere('duration', 'LIKE', '%' . $keyword . '%');
})
->orWhere('status', 'LIKE', '%' . $keyword . '%')
->orWhere('trx_status', 'LIKE', '%' . $keyword . '%')
->orWhere('cust_trx_id', 'LIKE', '%' . $keyword . '%')
->orWhere('trx_id', 'LIKE', '%' . $keyword . '%');
});
});
}
public const PerPageRecord = "10";
public static function GetList($request, $userId = null)
{
$keyword = getValue($request->input('keyword'));
$offset = (int) getValue($request->input('offset'), 0);
$sort_by = getValue($request->input('sort_by'), 'created_at');
$order_by = getValue($request->input('order_by'), 'DESC');
$type = getValue($request->input('type'));
$perPage = self::PerPageRecord;
$query = self::query()
->defaultModels()
->searchKeyword($keyword)
->when($userId, function ($q) use ($userId) {
$q->where('user_id', $userId)
->where('payment_status', 'SUCCESS');
});
$list = $query->orderBy($sort_by, $order_by)
->get()
->map(function ($item) {
if (in_array($item->type, ['NEW', 'RENEW', 'UPGRADE'], true)) {
$item->type = 'SUBSCRIPTION';
}
return $item;
});
if ($type) {
$list = $list->where('type', $type)->values();
}
$total = $list->count();
$currentPage = $offset + 1;
$perPage = self::PerPageRecord;
$totalPages = $perPage > 0 ? (int) ceil($total / $perPage) : 0;
$paged = $list->slice($offset * $perPage, $perPage)->values();
$nextOffset = ($paged->count() === $perPage && $currentPage < $totalPages)
? ($offset + 1)
: -1;
return [
'count' => $total,
'list' => $paged,
'per_page' => $perPage,
'current_page' => $currentPage,
'total_pages' => $totalPages,
'next_offset' => $nextOffset,
];
}
}