headers->set('Authorization', 'Bearer ' . $token); } public function export(Request $request) { ini_set('max_execution_time', '1000000'); ini_set("pcre.backtrack_limit", "5000000"); $companyId = $request->route('id'); $company = Company::find($companyId); $transactions = Transaction::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]); }) ->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', 'asc') ->get(); $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, $transactions) { $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; $pdf = LaravelMpdf::loadView('pages.pdfs.company_account_statement_transaction', ['transactions' => $transactions, 'runningBalance' => $currentBalance, 'company' => $company, 'isPrecise' => $request->route('is_precise') == 'true']); return $pdf->download("{$company->reference}_account_statement_transactions.pdf"); } }