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 @@
- {{ showingPreciseAmount ? 'Showing Precise Wallet Transaction' : 'Show Precise Wallet Transaction'}} + {{ showingPreciseAmount ? 'Showing Precise Transaction' : 'Show Precise Transaction'}} {{ showingPreciseAmount ? 'Download Precise Transaction' : 'Download Transaction'}}
@@ -23,6 +23,29 @@
+
+
+ + + + +
+
+ + + + +
+
+ + + + +
+
+ +
+
@@ -39,16 +62,6 @@ - -
@@ -71,6 +84,9 @@ export default { isLoading: false, transaction: null, attention: false, + reference_no: null, + startDate: null, + endDate: null, showingPreciseAmount: false, showingTransactionCount: 10, options: { @@ -78,17 +94,7 @@ export default { } } }, - computed: { - pendingQueue () { - // return this.$store.getters.isInCompleteQueue(this.section); - } - }, watch: { - pendingQueue(inComplete){ - if(inComplete){ - this.fetchTransaction(); - } - }, showingTransactionCount() { this.options.per_page = this.showingTransactionCount this.key ++; @@ -96,63 +102,32 @@ export default { }, validations: { showingTransactionCount: { }, - }, - created(){ - this.$store.dispatch('updateListQueue', {'name': this.section}); + reference_no: { }, + startDate: { }, + endDate: { }, }, methods: { - fetchTransaction(){ - this.isLoading = true; - this.submit(route('api.transaction.company.transaction.fetch', this.id), 'get', this.section, false, false) - }, - convertTransactionType(type){ - var transactionTypeArray = []; - transactionTypeArray[0] = 'Payment Attempt'; - transactionTypeArray[1] = 'Payment'; - transactionTypeArray[2] = 'Invoice'; - transactionTypeArray[3] = 'Bill'; - transactionTypeArray[4] = 'Proforma'; - transactionTypeArray[5] = 'Top Up'; - transactionTypeArray[6] = 'Refund'; - transactionTypeArray[7] = 'Purchase Order'; - transactionTypeArray[8] = 'Supplier Deliver'; - transactionTypeArray[9] = 'Credit Note'; - transactionTypeArray[11] = 'Debit Note'; - transactionTypeArray[10] = 'Withdraw'; - transactionTypeArray[12] = 'Transfer Fee'; - transactionTypeArray[13] = 'Cash Back'; - return transactionTypeArray[type]; - }, - convertPaymentMethod(paymentMethod){ - var paymentMethodArray = []; - paymentMethodArray[1] = 'Cash'; - paymentMethodArray[2] = 'Cheque'; - paymentMethodArray[3] = 'ba'; - paymentMethodArray[4] = 'Wallet'; - paymentMethodArray[5] = 'Payment Gateway'; - return paymentMethodArray[paymentMethod]; - }, - remainingBalance(index) { - let tempBalance = 0; - - if(this.transaction){ - let transactions = this.transaction.slice().reverse(); - transactions.slice(0, transactions.length - index).map(function(transaction) { - if (transaction.type === 2) { - tempBalance += (transaction.amount / transaction.currency_rate + transaction.service_charge); - } else { - [1, 11].includes(transaction.type) ? tempBalance -= (transaction.amount) : tempBalance += (transaction.amount); - } - return tempBalance - }, 0); - } - - return (Math.round((tempBalance + Number.EPSILON) * 100) / 100).toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); - }, successHandler(response){ this.isLoading = false; this.transaction = response.payload.data; - } + }, + submitSearch() { + delete this.options.marking; + delete this.options.startDate; + delete this.options.endDate; + + if (this.reference_no) { + this.options.marking = this.reference_no + } + if (this.startDate) { + this.options.startDate = this.startDate + } + if (this.endDate) { + this.options.endDate = this.endDate + } + + this.key ++; + }, } }