From 85d38c36459a8f44ffecdddc0fb2b03b39d9e805 Mon Sep 17 00:00:00 2001 From: Omair Saleh Date: Sat, 18 Mar 2023 05:21:38 +0800 Subject: [PATCH] import bank record --- .../Imports/ImportBankRecordController.php | 125 +++++++++++------- 1 file changed, 78 insertions(+), 47 deletions(-) diff --git a/app/Http/Controllers/Imports/ImportBankRecordController.php b/app/Http/Controllers/Imports/ImportBankRecordController.php index 43799bfd..3c0ebb1b 100644 --- a/app/Http/Controllers/Imports/ImportBankRecordController.php +++ b/app/Http/Controllers/Imports/ImportBankRecordController.php @@ -10,6 +10,8 @@ use App\Models\Booking; use App\Models\Group; use App\Models\Transaction; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\DB; use Maatwebsite\Excel\Facades\Excel; use PhpOffice\PhpSpreadsheet\Shared\Date; @@ -19,7 +21,8 @@ class ImportBankRecordController * @param Request $request * @return array */ - public function import(Request $request) { + public function import(Request $request) + { $headers = [ 'Date', 'Bank', @@ -41,55 +44,59 @@ class ImportBankRecordController $yes = 'Yes'; $no = 'No'; - $table = ''; + $table = '
'.implode('', $headers).'
'; - $collection = Excel::toCollection(new ImportsBankRecord(), 'daily_transaction_nov.xlsx'); + $cacheKey = 'bank_transaction_data'; - foreach ($collection as $key => $sheet){ - $branch = $branches[$key]; - foreach ($sheet as $row) { - $date = Date::excelToDateTimeObject($row['date']); - $credit = (float) $row['credit']; - $debit = (float) $row['debit']; - $creditTransactions = []; - $debitTransactions = []; + $data = Cache::get($cacheKey); - $systemReference = null; + if (!$data) { + $data = $this->fetchDataFromDatabase(); + Cache::put($cacheKey, $data, now()->addMinutes(60)); + } - if($credit){ - $creditTransactions = $this->getTransactions($date, $credit, TransactionType::PAYMENT, Booking::class, PaymentMethodType::WALLET, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); - foreach ($creditTransactions as $transaction) { - $systemReference[] = $transaction->owner instanceof Booking ? $transaction->owner->marking : $transaction->bill_no; - } + foreach ($data as $row) { + $branch = $branches[$row->branch_id]; + + $credit = (float)$row->credit; + $debit = (float)$row->debit; + $creditTransactions = []; + $debitTransactions = []; + + $systemReference = null; + + if ($credit) { + $creditTransactions = $this->getTransactions($row->date, $credit, TransactionType::PAYMENT, Booking::class, PaymentMethodType::WALLET, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]); + foreach ($creditTransactions as $transaction) { + $systemReference[] = $transaction->owner instanceof Booking ? $transaction->owner->marking : $transaction->bill_no; } - - if($debit){ - $debitTransactions = $this->getTransactions($date, $debit, null, null, null, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED], Group::class); - foreach ($debitTransactions as $transaction) { - $systemReference[] = $transaction->reference; - } - } - - - $multiple = count($creditTransactions) + count($debitTransactions) > 1 ? $yes : $no; - - $systemReference = $systemReference ? implode(',', $systemReference) : null; - - $matches = $systemReference == $row['remarkreferences'] ? $yes : $no; - - $table .= ' - - - - - - - - - - - '; } + + if ($debit) { + $debitTransactions = $this->getTransactions($row->date, $debit, null, null, null, [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED], Group::class); + foreach ($debitTransactions as $transaction) { + $systemReference[] = $transaction->reference; + } + } + + $multiple = count($creditTransactions) + count($debitTransactions) > 1 ? $yes : $no; + + $systemReference = $systemReference ? implode(',', $systemReference) : null; + + $matches = $systemReference == $row->remark_references ? $yes : $no; + + $table .= ' + + + + + + + + + + + '; } $table .= '
' . implode('', $headers) . '
'.$date->format('d-m-Y').''.$branch.''.$row['description'].''.$credit.''.$debit.''.$row['pay_for'].''.$systemReference.''.$row['remarkreferences'].''.$multiple.''.$matches.'
' . $row->date->format('d-m-Y') . '' . $branch . '' . $row->description . '' . $credit . '' . $debit . '' . $row->pay_for . '' . $systemReference . '' . $row->remark_references . '' . $multiple . '' . $matches . '
'; @@ -97,7 +104,30 @@ class ImportBankRecordController echo $table; } - private function getTransactions($date, $amount, $type, $ownerType, $paymentMethod, $statuses, $model = Transaction::class) { + private function fetchDataFromDatabase() + { + $data = DB::table('bank_transactions') + ->select([ + 'date', + 'branch_id', + 'description', + 'credit', + 'debit', + 'pay_for', + 'remark_references', + ]) + ->get(); + + $data = $data->map(function ($row) { + $row->date = Date::excelToDateTimeObject($row->date); + return $row; + }); + + return $data; + } + + 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) { @@ -114,8 +144,9 @@ class ImportBankRecordController }) ->whereDate('created_at', $date->format('Y-m-d')) ->where('amount', '>', ($amount - 0.01)) - ->where('amount', '<', ($amount + 0.01)); + ->where('amount', '<', ($amount + 0.01)) + ->cursor(); - return $query->get(); + return $query; } }