first commit

This commit is contained in:
2025-04-24 23:28:22 +03:30
commit c0d0b95442
116 changed files with 14014 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
<?php
namespace App\Models;
use App\Traits\HasUuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CafeConfig extends Model
{
use HasFactory, HasUuid;
protected $guarded = [];
public function packageNames()
{
return $this->hasMany(PackageName::class);
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace App\Models;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
class OtpTokens extends Model
{
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
}
+39
View File
@@ -0,0 +1,39 @@
<?php
namespace App\Models;
use App\Traits\HasUuid;
use DateTimeInterface;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
class PackageName extends Model
{
use HasUuid, Filterable;
protected $guarded = [];
public function users()
{
return $this->belongsToMany(User::class, 'user_package_name')->withPivot(['id', 'tries'])->withTimestamps();
}
public function getImageAttribute()
{
return url($this->attributes['image']);
}
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
public function products()
{
return $this->hasMany(Product::class);
}
public function cafeConfig()
{
return $this->belongsTo(CafeConfig::class);
}
}
+21
View File
@@ -0,0 +1,21 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Plan extends Model
{
protected $fillable = ['user_id', 'current_day', 'status'];
public function user()
{
return $this->belongsTo(User::class);
}
public function planDays()
{
return $this->hasMany(PlanDay::class);
}
}
+68
View File
@@ -0,0 +1,68 @@
<?php
namespace App\Models;
use App\Traits\HasUuid;
use Carbon\Carbon;
use DateTimeInterface;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasUuid, Filterable;
protected $guarded = [];
const TYPES = [
'permanent' => 1,
'yearly' => 2,
'6months' => 3,
'monthly' => 4,
];
public function users()
{
return $this->belongsToMany(User::class, 'user_product')->withPivot(['expire_at', 'purchase_token', 'gateway'])->withTimestamps();
}
public function packageName()
{
return $this->belongsTo(PackageName::class);
}
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
public function buy($user, $purchaseToken = null, $gateway = null)
{
$expireDate = now();
if ($purchaseUser = $this->users()->where('users.id', $user->id)->wherePivot('expire_at', '>=', now())->first()) {
$expireDate = Carbon::parse($purchaseUser->pivot->expire_at) ?? now();
}
$expireAt = match ($this->type) {
1 => null,
2 => $expireDate->addYear()->format('Y-m-d H:i:s'),
3 => $expireDate->addMonths(6)->format('Y-m-d H:i:s'),
4 => $expireDate->addMonth()->format('Y-m-d H:i:s'),
};
if ($this->users()->where('users.id', $user->id)->exists()) {
$this->users()->updateExistingPivot($user->id, [
'expire_at' => $expireAt,
'purchase_token' => $purchaseToken,
'gateway' => $gateway,
]);
} else {
$this->users()->attach($user->id, [
'expire_at' => $expireAt,
'purchase_token' => $purchaseToken,
'gateway' => $gateway,
]);
}
$user->transactions()->where('authority', $purchaseToken)->update(['status' => Transaction::STATUSES['consumed']]);
}
}
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace App\Models;
use App\Traits\HasUuid;
use DateTimeInterface;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
class Transaction extends Model
{
use HasUuid, Filterable;
protected $guarded = [];
const STATUSES = [
'pending' => 1,
'success' => 2,
'consumed' => 3,
'failed' => 3,
];
const GATEWAYS = [
'asanpardakht' => 1,
'zarinpal' => 2,
'digipay' => 3,
'cafe' => 4,
'myket' => 5,
];
public function user()
{
return $this->belongsTo(User::class);
}
public function product()
{
return $this->belongsTo(Product::class);
}
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
}
+101
View File
@@ -0,0 +1,101 @@
<?php
namespace App\Models;
use App\Traits\HasUuid;
use DateTimeInterface;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasUuid, Filterable;
const GENDERS = [
'male' => 1,
'female' => 2,
];
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $guarded = [];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
protected $appends = [
'full_name'
];
public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
public function getAvatarAttribute()
{
return isset($this->attributes['avatar']) ? url($this->attributes['avatar']) : null;
}
public function setPasswordAttribute($password)
{
$this->attributes['password'] = Hash::make($password);
}
public function otpTokens()
{
return $this->hasMany(OtpTokens::class);
}
public function transactions()
{
return $this->hasMany(Transaction::class);
}
public function packageNames()
{
return $this->belongsToMany(PackageName::class, 'user_package_name')->withPivot(['id', 'tries'])->withTimestamps();
}
public function products()
{
return $this->belongsToMany(Product::class, 'user_product')->withPivot(['expire_at', 'purchase_token', 'gateway'])->withTimestamps();
}
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
public function plans()
{
return $this->hasMany(Plan::class);
}
public function response()
{
return $this->hasOne(UserResponse::class);
}
}