first commit

This commit is contained in:
2025-09-10 15:28:46 +03:30
commit 00edec179a
204 changed files with 21024 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);
}
}
+10
View File
@@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class OldPurchase extends Model
{
protected $guarded = [];
}
+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');
}
}
+55
View File
@@ -0,0 +1,55 @@
<?php
namespace App\Models;
use App\Traits\HasUuid;
use DateTimeInterface;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class PackageName extends Model implements HasMedia
{
use HasUuid, Filterable, InteractsWithMedia;
protected $guarded = [];
const SOURCES = [
'unknown' => 0,
'google_play' => 1,
'cafe' => 2,
'myket' => 3,
'web' => 4,
];
public function registerMediaCollections(): void
{
$this->addMediaCollection('firebase_json')->singleFile();
}
public function users()
{
return $this->belongsToMany(User::class, 'user_package_name')->withPivot(['id', 'tries', 'source', 'fcm_token'])->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);
}
}
+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->addDays(30 * 12)->format('Y-m-d H:i:s'),
3 => $expireDate->addDays(30 * 6)->format('Y-m-d H:i:s'),
4 => $expireDate->addDays(30 * 1)->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']]);
}
}
+27
View File
@@ -0,0 +1,27 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Reminder extends Model
{
use HasFactory;
protected $guarded = [];
protected $casts = [
'data' => 'array',
'last_repeat' => 'date',
];
public function packageName()
{
return $this->belongsTo(PackageName::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
+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');
}
}
+82
View File
@@ -0,0 +1,82 @@
<?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;
use Illuminate\Support\Str;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasUuid, Filterable;
const GENDERS = [
'male' => 1,
'female' => 2,
];
protected $guarded = [];
protected $hidden = [
'password',
];
protected $appends = [
'full_name'
];
// protected static function booted()
// {
// static::creating(function ($user) {
// if (empty($user->uuid)) {
// $user->uuid = (string) Str::uuid();
// }
// });
// }
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 reminders()
{
return $this->hasMany(Reminder::class);
}
public function packageNames()
{
return $this->belongsToMany(PackageName::class, 'user_package_name')->withPivot(['id', 'tries', 'source', 'fcm_token'])->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');
}
}