'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 ($transactions) use ($companyId) { $transactions->where(function ($query) use ($companyId) { $query ->where('type', TransactionType::PAYMENT) ->where('owner_type', Booking::class) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->where('payment_method', '!=', PaymentMethodType::WALLET) ->whereHas('booking', function ($q) use ($companyId) { $q->where('company_id', $companyId); }); }) ->orWhere(function ($query) use ($companyId) { $query->whereHas('owner', function ($q) use ($companyId) { $q->where('owner_id', $companyId); $q->where('owner_type', Company::class); }) ->where('owner_type', Wallet::class) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); // ->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]) ->where('payment_method', '!=', PaymentMethodType::WALLET) ->whereHas('booking', function ($q) use ($companyId) { $q->where('company_id', $companyId); }); }); }) ->groupBy( DB::raw( 'if (transactions.type = 2,transactions.owner_id,transactions.id)' ), DB::raw( 'if (transactions.type = 2,transactions.type,transactions.id)' ) ) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->orderBy('created_at', 'desc'); if (isset($filterArray->startDate)) { $startDate = Carbon::createFromFormat('d-m-Y', $filterArray->startDate)->startOfDay(); $transactions = $transactions->where("created_at", '>=', $startDate); } if (isset($filterArray->endDate)) { $endDate = Carbon::createFromFormat('d-m-Y', $filterArray->endDate)->endOfDay(); $transactions = $transactions->where("created_at", '<=', $endDate); } if (isset($filterArray->marking)) { $transactions = $transactions->where('owner_type', Booking::class) ->whereHas('booking', function ($q) use ($filterArray) { $q->where('marking', 'LIKE', '%'.$filterArray->marking.'%'); }); } $transactions = $transactions->paginate($filterArray->per_page); if ($transactions->count() > 0) { $total_incoming = Transaction::where(function ($query) use ($companyId) { $query->whereHas('owner', function ($q) use ($companyId) { $q->where('owner_id', $companyId); $q->where('owner_type', Company::class); }) ->where('owner_type', Wallet::class) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->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]) ->where('payment_method', '!=', PaymentMethodType::WALLET) ->whereHas('booking', function ($q) use ($companyId) { $q->where('company_id', $companyId); }); }) ->groupBy( DB::raw( 'if (transactions.type = 2,transactions.owner_id,transactions.id)' ), DB::raw( 'if (transactions.type = 2,transactions.type,transactions.id)' ) )->get(); $total_incoming = $total_incoming->sum(function ($transaction) { if ($transaction->type === TransactionType::INVOICE) { return $transaction->amount / $transaction->currency_rate + $transaction->service_charge; } return $transaction->amount; }); $total_outgoing = Transaction::where(function ($query) use ($companyId) { $query ->where('type', TransactionType::PAYMENT) ->where('payment_method', '!=', PaymentMethodType::WALLET) ->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 ($q) use ($companyId) { $q->where('owner_id', $companyId); $q->where('owner_type', Company::class); }) ->where('owner_type', Wallet::class) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->where('type', TransactionType::PAYMENT); }) ->sum('transactions.amount'); $currentBalance = $total_incoming - $total_outgoing; $incoming = Transaction::where(function ($query) use ($companyId, $transactions) { $query->whereHas('owner', function ($q) use ($companyId) { $q->where('owner_id', $companyId); $q->where('owner_type', Company::class); }) ->where('owner_type', Wallet::class) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->where('id', '>', $transactions->first()->id) ->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]) ->where('payment_method', '!=', PaymentMethodType::WALLET) ->where('id', '>', $transactions->first()->id) ->whereHas('booking', function ($q) use ($companyId) { $q->where('company_id', $companyId); }); }) ->groupBy( DB::raw( 'if (transactions.type = 2,transactions.owner_id,transactions.id)' ), DB::raw( 'if (transactions.type = 2,transactions.type,transactions.id)' ) )->get(); $incoming = $incoming->sum(function ($transaction) { if ($transaction->type === TransactionType::INVOICE) { return $transaction->amount / $transaction->currency_rate + $transaction->service_charge; } return $transaction->amount; }); $outgoing = Transaction::where(function ($query) use ($companyId, $transactions) { $query ->where('type', TransactionType::PAYMENT) ->where('payment_method', '!=', PaymentMethodType::WALLET) ->where('owner_type', Booking::class) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->where('id', '>', $transactions->first()->id) ->whereHas('booking', function ($q) use ($companyId) { $q->where('company_id', $companyId); }); }) ->orWhere(function ($query) use ($companyId, $transactions) { $query->whereHas('owner', function ($q) use ($companyId) { $q->where('owner_id', $companyId); $q->where('owner_type', Company::class); }) ->where('owner_type', Wallet::class) ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) ->where('id', '>', $transactions->first()->id) ->where('type', TransactionType::PAYMENT); }) ->sum('transactions.amount'); $runningBalanceInReverse = $currentBalance - $incoming + $outgoing; $request['running_balance'] = $runningBalanceInReverse; if (isset($filterArray->marking)) { $request['running_balance'] = 0; } } return $this->collectionResponse(PaymentTransactionResource::collection($transactions)); } }