mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-20 21:13:59 +00:00
152 lines
6.4 KiB
PHP
152 lines
6.4 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Classes\Modules\Transactions\ControllersLogic;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Http\Resources\PaymentTransactionResource;
|
|
use App\Models\Booking;
|
|
use App\Models\Company;
|
|
use App\Models\Transaction;
|
|
use App\Models\Wallet;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class FetchCompanyTransactionStatementLogic extends AbstractControllerLogic
|
|
{
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function notification(): array
|
|
{
|
|
return [
|
|
'title' => 'Retrieved Company Transaction Statement',
|
|
'message' => 'You have successfully retrieved company transaction statement'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* FetchCompanyAccountBalanceLogic constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
public function logic(Request $request): JsonResponse
|
|
{
|
|
$companyId = $request->route('id');
|
|
$filterArray = json_decode($request->input('filters'));
|
|
|
|
$transactions = Transaction::where(function ($query) use ($companyId) {
|
|
$query
|
|
->where('type', TransactionType::PAYMENT)
|
|
->where('owner_type', Booking::class)
|
|
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
|
|
->whereHas('booking', function ($q) use ($companyId) {
|
|
$q->where('company_id', $companyId);
|
|
});
|
|
})
|
|
->orWhere(function ($query) use ($companyId) {
|
|
$query->whereHas('owner', function ($query) use ($companyId) {
|
|
$query->where('owner_id', $companyId);
|
|
$query->where('owner_type', Company::class);
|
|
})
|
|
->where('owner_type', Wallet::class)
|
|
->where('type', '!=', TransactionType::PAYMENT);
|
|
})
|
|
->orWhere(function ($query) use ($companyId) {
|
|
$query
|
|
->where('type', TransactionType::INVOICE)
|
|
->where('owner_type', Booking::class)
|
|
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
|
|
->whereHas('booking', function ($query) use ($companyId) {
|
|
$query->where('company_id', $companyId);
|
|
});
|
|
})
|
|
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
|
|
->orderBy('created_at', 'desc')
|
|
->paginate($filterArray->per_page);
|
|
|
|
$total_incoming = Transaction::where(function ($query) use ($companyId, $transactions) {
|
|
$query->whereHas('owner', function ($query) use ($companyId) {
|
|
$query->where('owner_id', $companyId);
|
|
$query->where('owner_type', Company::class);
|
|
})
|
|
->where('owner_type', Wallet::class)
|
|
->where('type', '!=', TransactionType::PAYMENT);
|
|
})
|
|
->orWhere(function ($query) use ($companyId, $transactions) {
|
|
$query
|
|
->where('type', TransactionType::INVOICE)
|
|
->where('owner_type', Booking::class)
|
|
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
|
|
->whereHas('booking', function ($query) use ($companyId) {
|
|
$query->where('company_id', $companyId);
|
|
});
|
|
})
|
|
->sum(
|
|
DB::raw(
|
|
'if (transactions.type = 2,(transactions.amount / transactions.currency_rate + transactions.service_charge),transactions.amount)'
|
|
)
|
|
);
|
|
|
|
$total_outgoing = Transaction::where(function ($query) use ($companyId, $transactions) {
|
|
$query
|
|
->where('type', TransactionType::PAYMENT)
|
|
->where('owner_type', Booking::class)
|
|
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
|
|
->whereHas('booking', function ($query) use ($companyId) {
|
|
$query->where('company_id', $companyId);
|
|
});
|
|
})
|
|
->sum('transactions.amount');
|
|
|
|
$currentBalance = $total_incoming - $total_outgoing;
|
|
|
|
$incoming = Transaction::where(function ($query) use ($companyId, $transactions) {
|
|
$query->whereHas('owner', function ($query) use ($companyId) {
|
|
$query->where('owner_id', $companyId);
|
|
$query->where('owner_type', Company::class);
|
|
})
|
|
->where('owner_type', Wallet::class)
|
|
->where('type', '!=', TransactionType::PAYMENT)
|
|
->where('id', '>', $transactions->first()->id);
|
|
})
|
|
->orWhere(function ($query) use ($companyId, $transactions) {
|
|
$query
|
|
->where('type', TransactionType::INVOICE)
|
|
->where('owner_type', Booking::class)
|
|
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
|
|
->whereHas('booking', function ($query) use ($companyId) {
|
|
$query->where('company_id', $companyId);
|
|
})
|
|
->where('id', '>', $transactions->first()->id);
|
|
})
|
|
->sum(
|
|
DB::raw(
|
|
'if (transactions.type = 2,(transactions.amount / transactions.currency_rate + transactions.service_charge),transactions.amount)'
|
|
)
|
|
);
|
|
|
|
$outgoing = Transaction::where(function ($query) use ($companyId, $transactions) {
|
|
$query
|
|
->where('type', TransactionType::PAYMENT)
|
|
->where('owner_type', Booking::class)
|
|
->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED])
|
|
->whereHas('booking', function ($query) use ($companyId) {
|
|
$query->where('company_id', $companyId);
|
|
})
|
|
->where('id', '>', $transactions->first()->id);
|
|
})
|
|
->sum('transactions.amount');
|
|
|
|
$runningBalanceInReverse = $currentBalance - $incoming + $outgoing;
|
|
$request['running_balance'] = $runningBalanceInReverse;
|
|
|
|
return $this->collectionResponse(PaymentTransactionResource::collection($transactions));
|
|
}
|
|
}
|