From fd991d19e0ea866c0790dff7d4d9aeaa31986c48 Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Sat, 18 Mar 2023 05:02:45 +0800 Subject: [PATCH] import bank record --- .../Imports/ImportBankRecordController.php | 162 ++++++++---------- 1 file changed, 71 insertions(+), 91 deletions(-) diff --git a/app/Http/Controllers/Imports/ImportBankRecordController.php b/app/Http/Controllers/Imports/ImportBankRecordController.php index c7baa997..a8b8b147 100644 --- a/app/Http/Controllers/Imports/ImportBankRecordController.php +++ b/app/Http/Controllers/Imports/ImportBankRecordController.php @@ -20,117 +20,97 @@ class ImportBankRecordController /** * @param Request $request * @return array - * @throws \App\Classes\Exceptions\MalformedRequestException */ public function import(Request $request) { -// $object = new DocumentObject('', $request->input('files'), '', ApprovalStatus::APPROVED, 'imports'); - - echo ' - - - - - - - - - - - - '; - - $collection = Excel::toCollection(new ImportsBankRecord(), 'daily_transaction_nov.xlsx'); + $headers = [ + 'Date', + 'Bank', + 'Description', + 'Credit', + 'Debit', + 'Pay For', + 'System Reference', + 'Human Reference', + 'Multiple', + 'Match?', + ]; $branches = [ 0 => 'MBB Cyber', - 1 => 'MBB SS2' + 1 => 'MBB SS2', ]; + $yes = 'Yes'; + $no = 'No'; + + $table = '
DateBankDescriptionCreditdebitPay ForSystem ReferenceHuman ReferenceMultipleMatch?
'; + + $collection = Excel::toCollection(new ImportsBankRecord(), 'daily_transaction_nov.xlsx'); + foreach ($collection as $key => $sheet){ - $branch = $branches[$key]; + $branch = $branches[$key]; foreach ($sheet as $row) { $date = Date::excelToDateTimeObject($row['date']); $credit = (float) $row['credit']; $debit = (float) $row['debit']; + + $creditTransactions = $this->getTransactions($date, $credit, TransactionType::PAYMENT, Booking::class, PaymentMethodType::WALLET, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + $debitTransactions = $this->getTransactions($date, $debit, null, null, null, null, Group::class); + $systemReference = null; - $multiple = 'No'; - - - - if($credit){ - $payments = Transaction::whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]) - ->where(function ($query){ - return $query->where(function ($query){ - return $query->where('type', TransactionType::PAYMENT)->where('owner_type', Booking::class)->where('payment_method', '!=', PaymentMethodType::WALLET); - })->orWhere(function ($query){ - $query->where('type', TransactionType::TOP_UP); - }); - }) - ->WhereDate('created_at', $date->format('Y-m-d')) - ->where('amount', '>', ($credit - 0.01))->where('amount', '<', ($credit + 0.01)) - ->get(); - - if(count($payments)) { - foreach ($payments as $transaction){ - if($transaction->owner instanceof Booking){ - $systemReference[] = $transaction->owner->marking; - } - - if($transaction->owner instanceof Wallet){ - $systemReference[] = $transaction->bill_no; - } - } - - if(count($systemReference) > 1) { - $multiple = 'Yes'; - } - $systemReference = implode(',', $systemReference); - - } - } - if($debit){ - $bills = Group::WhereDate('created_at', $date->format('Y-m-d')) - ->where('amount', '>', ($debit - 0.01))->where('amount', '<', ($debit + 0.01)) - ->get(); - - if(count($bills)) { - foreach ($bills as $transaction){ - if($transaction->owner instanceof Booking){ - $systemReference[] = $transaction->owner->marking; - } - - if($transaction->owner instanceof Wallet){ - $systemReference[] = $transaction->bill_no; - } - } - - if(count($systemReference) > 1) { - $multiple = 'Yes'; - } - $systemReference = implode(',', $systemReference); - - } + $multiple = count($creditTransactions) + count($debitTransactions) > 1 ? $yes : $no; + foreach ($creditTransactions as $transaction) { + $systemReference[] = $transaction->owner instanceof Booking ? $transaction->owner->marking : $transaction->bill_no; } + foreach ($debitTransactions as $transaction) { + $systemReference[] = $transaction->owner instanceof Booking ? $transaction->owner->marking : $transaction->bill_no; + } + $systemReference = $systemReference ? implode(',', $systemReference) : null; - $matches = $systemReference == $row['remarkreferences'] ? 'Yes' : 'No'; + $matches = $systemReference == $row['remarkreferences'] ? $yes : $no; - echo ' - - - - - - - - - - - '; + $table .= ' + + + + + + + + + + + '; } } -// return []; + + $table .= '
'.implode('', $headers).'
'.$date->format('d-m-Y').''.$branch.''.$row['description'].''.$credit.''.$debit.''.$row['pay_for'].''.$systemReference.''.$row['remarkreferences'].''.$multiple.''.$matches.'
'.$date->format('d-m-Y').''.$branch.''.$row['description'].''.$credit.''.$debit.''.$row['pay_for'].''.$systemReference.''.$row['remarkreferences'].''.$multiple.''.$matches.'
'; + + echo $table; + } + + private function getTransactions($date, $amount, $type, $ownerType, $paymentMethod, $statuses, $model = Transaction::class) { + $query = $model::whereIn('status', $statuses) + ->where(function ($query) use ($ownerType, $paymentMethod, $type) { + if ($ownerType) { + $query->where('owner_type', $ownerType); + } + + if ($paymentMethod) { + $query->where('payment_method', '!=', $paymentMethod); + } + + if ($type) { + $query->where('type', $type); + } + }) + ->whereDate('created_at', $date->format('Y-m-d')) + ->where('amount', '>', ($amount - 0.01)) + ->where('amount', '<', ($amount + 0.01)); + + return $query->get(); } }