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 = '
| '.implode(' | ', $headers).' |
';
+ $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 .= '
- | '.$date->format('d-m-Y').' |
- '.$branch.' |
- '.$row['description'].' |
- '.$credit.' |
- '.$debit.' |
- '.$row['pay_for'].' |
- '.$systemReference.' |
- '.$row['remarkreferences'].' |
- '.$multiple.' |
- '.$matches.' |
-
';
}
+
+ 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 .= '
+ | ' . $row->date->format('d-m-Y') . ' |
+ ' . $branch . ' |
+ ' . $row->description . ' |
+ ' . $credit . ' |
+ ' . $debit . ' |
+ ' . $row->pay_for . ' |
+ ' . $systemReference . ' |
+ ' . $row->remark_references . ' |
+ ' . $multiple . ' |
+ ' . $matches . ' |
+
';
}
$table .= '
';
@@ -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;
}
}