diff --git a/app/Classes/Modules/Transactions/ControllersLogic/FetchCompanyTransactionStatementLogic.php b/app/Classes/Modules/Transactions/ControllersLogic/FetchCompanyTransactionStatementLogic.php index ea5d297b..1f569e22 100644 --- a/app/Classes/Modules/Transactions/ControllersLogic/FetchCompanyTransactionStatementLogic.php +++ b/app/Classes/Modules/Transactions/ControllersLogic/FetchCompanyTransactionStatementLogic.php @@ -14,6 +14,7 @@ use App\Models\Booking; use App\Models\Company; use App\Models\Transaction; use App\Models\Wallet; +use Carbon\Carbon; use Illuminate\Support\Facades\DB; class FetchCompanyTransactionStatementLogic extends AbstractControllerLogic @@ -41,169 +42,196 @@ class FetchCompanyTransactionStatementLogic extends AbstractControllerLogic $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]) - ->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) { + $transactions = Transaction::where(function ($transactions) use ($companyId) { + $transactions->where(function ($query) use ($companyId) { $query - ->where('type', TransactionType::INVOICE) + ->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)' - ) + }) + ->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') - ->paginate($filterArray->per_page); + ) + ->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) + ->orderBy('created_at', 'desc'); - $total_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('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); + 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.'%'); }); - }) - ->orWhere(function ($query) use ($companyId, $transactions) { + } + + $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); + ->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) { + ->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::INVOICE) + ->where('type', TransactionType::PAYMENT) + ->where('payment_method', '!=', PaymentMethodType::WALLET) ->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(); + ->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'); - $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) { + $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); + ->where('id', '>', $transactions->first()->id) + ->where('type', '!=', TransactionType::PAYMENT); }) - ->sum('transactions.amount'); - - $runningBalanceInReverse = $currentBalance - $incoming + $outgoing; - $request['running_balance'] = $runningBalanceInReverse; + ->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)); } diff --git a/app/Http/Resources/PaymentTransactionResource.php b/app/Http/Resources/PaymentTransactionResource.php index 993235d9..7fcfd552 100644 --- a/app/Http/Resources/PaymentTransactionResource.php +++ b/app/Http/Resources/PaymentTransactionResource.php @@ -37,6 +37,7 @@ class PaymentTransactionResource extends JsonResource } break; case 2: + $booking_marking = $booking->marking; $request['running_balance'] = bcsub($request['running_balance'], $this->amount / $this->currency_rate + $this->service_charge, 7); break; case 5: diff --git a/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue b/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue index d5f28370..ad064490 100644 --- a/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue +++ b/resources/assets/vue/components/bookings/elements/CustomerTransactionHistorySectionComponent.vue @@ -12,7 +12,7 @@