mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-19 12:33:56 +00:00
154 lines
3.7 KiB
PHP
154 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Classes\General\Interfaces\Documentable;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
|
|
|
|
class Transaction extends AbstractModel implements Documentable
|
|
{
|
|
protected $table = 'transactions';
|
|
|
|
public function owner(): morphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
/**
|
|
* @return MorphMany
|
|
*/
|
|
public function booking(): morphMany
|
|
{
|
|
//return $this->BelongsTo( Booking::class, 'booking_id', 'id');
|
|
return $this->morphMany(Booking::class, 'owner');
|
|
}
|
|
|
|
/**
|
|
* @return MorphMany
|
|
*/
|
|
public function wallets(): morphMany
|
|
{
|
|
return $this->morphMany(Wallet::class, 'owner');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo
|
|
*/
|
|
public function wallet(): BelongsTo
|
|
{
|
|
return $this->BelongsTo(Wallet::class, 'owner_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* @return MorphMany
|
|
*/
|
|
public function documents(): morphMany
|
|
{
|
|
return $this->morphMany(Document::class, 'owner');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo
|
|
*/
|
|
public function currency(): BelongsTo
|
|
{
|
|
return $this->BelongsTo(Currency::class, 'currency_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo
|
|
*/
|
|
public function original_currency(): BelongsTo
|
|
{
|
|
return $this->BelongsTo(Currency::class, 'original_currency_id', 'id');
|
|
}
|
|
|
|
/**
|
|
* @return HasMany
|
|
*/
|
|
public function transactionDetails(): HasMany
|
|
{
|
|
return $this->HasMany(TransactionDetail::class, 'transaction_id', 'id');
|
|
}
|
|
|
|
public function convert_original_amount()
|
|
{
|
|
if($this->booking()->first()->fix_currency_id !== 1) {
|
|
$currency_rate = $this->currency()->first()->rates()->where('payment_method_type', $this->payment_method)->first();
|
|
|
|
return number_format($this->original_amount / $currency_rate->selling, 2);
|
|
}
|
|
return $this->original_amount;
|
|
}
|
|
|
|
/**
|
|
* @param Builder $query
|
|
* @return Builder
|
|
*/
|
|
public function scopePayments(Builder $query)
|
|
{
|
|
return $query->where('type', TransactionType::PAYMENT);
|
|
}
|
|
|
|
/**
|
|
* @param Builder $query
|
|
* @return Builder
|
|
*/
|
|
public function scopeBills(Builder $query)
|
|
{
|
|
return $query->where('type', TransactionType::BILL);
|
|
}
|
|
|
|
/**
|
|
* @param Builder $query
|
|
* @return Builder
|
|
*/
|
|
public function scopeRefunds(Builder $query)
|
|
{
|
|
return $query->where('type', TransactionType::REFUND);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param Builder $query
|
|
* @return Builder
|
|
*/
|
|
public function scopeInComplete(Builder $query)
|
|
{
|
|
return $query->where(function(Builder $query){
|
|
$query->where(function(Builder $query){
|
|
$query->where('status', ApprovalStatus::PENDING_SUBMISSION)
|
|
->whereDate('expires_on', '>=', Carbon::now());
|
|
})->orWhere(function(Builder $query){
|
|
$query->where('status', ApprovalStatus::PENDING_VERIFICATION);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param Builder $query
|
|
* @return Builder
|
|
*/
|
|
public function scopeComplete(Builder $query)
|
|
{
|
|
return $query->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
|
|
}
|
|
|
|
/**
|
|
* @param Builder $query
|
|
* @return Builder
|
|
*/
|
|
public function scopeTransferred(Builder $query)
|
|
{
|
|
return $query->whereIn('status', [ApprovalStatus::COMPLETED]);
|
|
}
|
|
}
|