'Import Ambank Statement Transactions Details', 'message' => 'You have successfully updated the Ambank Statement Transactions Details' ]; } /** * @param Request $request * @return JsonResponse * @throws MalformedRequestException */ public function logic(Request $request) : JsonResponse { $object = new DocumentObject('', $request->input('files'), '', ApprovalStatus::APPROVED, 'imports'); foreach ($object->getFiles() as $file){ $collection = Excel::toCollection(null, json_decode($file)->file_info->original->file, null, null, true); $statementDetailRows = $collection->first()->slice(0,13); $dateInfoKey = $this->findInfoIndexFromExcelCollection($statementDetailRows, "STATEMENT DATE"); $statementDateRange = trim(explode(':', $this->extractInfoFromExcelCollection($statementDetailRows[$dateInfoKey])[0])[1]); $dateFrom = trim(explode('-', $statementDateRange)[0]); $dateTo = trim(explode('-', $statementDateRange)[1]); $dateFrom = carbon::createFromFormat('d/m/Y', $dateFrom); $dateTo = carbon::createFromFormat('d/m/Y', $dateTo); $dateTo = carbon::parse($dateTo); $totalDebitKey = $this->findInfoIndexFromExcelCollection($statementDetailRows, "TOTAL DEBIT"); $totalDebit = $this->extractInfoFromExcelCollection($statementDetailRows[$totalDebitKey])[1]; $totalCreditKey = $this->findInfoIndexFromExcelCollection($statementDetailRows, "TOTAL CREDIT"); $totalCredit = $this->extractInfoFromExcelCollection($statementDetailRows[$totalCreditKey])[1]; $totalTransactions = $totalDebit + $totalCredit; $beginBalanceKey = $this->findInfoIndexFromExcelCollection($statementDetailRows, "OPENING BALANCE"); $beginBalance = ((float) str_replace(',', '', $this->extractInfoFromExcelCollection($statementDetailRows[$beginBalanceKey])[1])); $endBalanceKey = $this->findInfoIndexFromExcelCollection($statementDetailRows, "CLOSING BALANCE"); $endBalance = ((float) str_replace(',', '', $this->extractInfoFromExcelCollection($statementDetailRows[$endBalanceKey])[1])); $account = StatementAccount::where('number', 8881040198515)->first(); if (!$account) { throw new Exception("Statement Account for CIEF LITE not found."); } $statement = AccountStatement::whereDate('date_from', $dateFrom) ->whereDate('date_to', $dateTo) ->where('total_amount', $totalTransactions) ->where('begin_balance', $beginBalance) ->where('end_balance', $endBalance) ->first(); if(!$statement){ $statement = new AccountStatement([ 'date_from' => $dateFrom, 'date_to' => $dateTo, 'total_amount' => $totalTransactions, 'begin_balance' => $beginBalance, 'end_balance' => $endBalance, ]); } $account->statements()->save($statement); $collection->map(function ($sheet, $key) use ($statement) { if ($key === 0) { $headerColumnKey = $this->findInfoIndexFromExcelCollection($sheet->slice(11), "DATE"); $dateColumnKey = $this->findInfoIndexFromExcelCollection($sheet->slice(11)[$headerColumnKey], "DATE"); $descriptionColumnKey = $this->findInfoIndexFromExcelCollection($sheet->slice(11)[$headerColumnKey], "TRANSACTION"); $debitColumnKey = $this->findInfoIndexFromExcelCollection($sheet->slice(11)[$headerColumnKey], "DEBIT"); $creditColumnKey = $this->findInfoIndexFromExcelCollection($sheet->slice(11)[$headerColumnKey], "CREDIT"); $balanceColumnKey = $this->findInfoIndexFromExcelCollection($sheet->slice(11)[$headerColumnKey], "BALANCE"); } else { if (strpos($sheet->first()->first(), "DATE") === false) { return; } $headerColumnKey = $this->findInfoIndexFromExcelCollection($sheet, "DATE"); $dateColumnKey = $this->findInfoIndexFromExcelCollection($sheet[$headerColumnKey], "DATE"); $descriptionColumnKey = $this->findInfoIndexFromExcelCollection($sheet[$headerColumnKey], "TRANSACTION"); $debitColumnKey = $this->findInfoIndexFromExcelCollection($sheet[$headerColumnKey], "DEBIT"); $creditColumnKey = $this->findInfoIndexFromExcelCollection($sheet[$headerColumnKey], "CREDIT"); $balanceColumnKey = $this->findInfoIndexFromExcelCollection($sheet[$headerColumnKey], "BALANCE"); } $sheet->slice($headerColumnKey + 1)->map(function ($row, $index) use ( $statement, $sheet, $dateColumnKey, $descriptionColumnKey, $debitColumnKey, $creditColumnKey, $balanceColumnKey, $key) { // if date or balance is null or empty, skip the row if (!$row[$dateColumnKey] || !$row[$balanceColumnKey]) { return; } $postingDate = carbon::createFromFormat('dM', trim($row[$dateColumnKey])); $description = trim($row[$descriptionColumnKey]); $nextRow = $sheet[$index + 1]; if (!$nextRow[$dateColumnKey] && $nextRow[$descriptionColumnKey]) { $nextRowInfoArr = $this->extractInfoFromExcelCollection($nextRow); foreach ($nextRowInfoArr as $col) { if ($col) { $description .= ' '. $col; } } } $descriptionArr = explode(',', $description); $transactionDescription = array_key_exists(0, $descriptionArr) ? trim($descriptionArr[0]) : ""; $description2 = array_key_exists(1, $descriptionArr) ? trim($descriptionArr[1]) : ""; $description3 = array_key_exists(2, $descriptionArr) ? trim($descriptionArr[2]) : ""; $description4 = array_key_exists(3, $descriptionArr) ? trim($descriptionArr[3]) : ""; $description5 = array_key_exists(4, $descriptionArr) ? trim($descriptionArr[4]) : ""; if (count($descriptionArr) > 5) { for ($i = 5; $i < count($descriptionArr); $i++) { $description5 = array_key_exists($i, $descriptionArr) ? $description5 . " " . trim($descriptionArr[$i]) : $description5; } } $amount = $row[$creditColumnKey] ? ((float) str_replace(',', '', trim($row[$creditColumnKey]))) : (-((float) str_replace(',', '', $row[$debitColumnKey]))); $endBalance = ((float) str_replace(',', '', $row[$balanceColumnKey])); $transaction = new StatementTransaction([ 'posting_date' => $postingDate, 'transaction_description' => $transactionDescription, 'transaction_description_2' => $description2, 'transaction_description_3' => $description3, 'transaction_description_4' => $description4, 'transaction_description_5' => $description5, 'amount' => $amount, 'end_balance' => $endBalance, ]); // Check if the transaction already exists for this statement $existingTransaction = StatementTransaction::whereDate('posting_date', $postingDate) ->where('amount', $amount) ->where('transaction_description', $transactionDescription) ->whereRaw("CAST(REPLACE(end_balance,',','') AS DECIMAL(15,2)) = ?",[$endBalance]) ->first(); if (!$existingTransaction) { $statement->transactions()->save($transaction); } return $transaction; }); }); } return $this->response([]); } private function findInfoIndexFromExcelCollection($collection, $keyword) { foreach ($collection as $key => $row) { if ($row instanceof Collection) { $infoArr = $this->extractInfoFromExcelCollection($row); foreach ($infoArr as $info) { if (str_contains(strtolower($info), strtolower($keyword))) { return $key; } } } else { if (str_contains(strtolower($row), strtolower($keyword))) { return $key; } } } } // return an array with information of the row start from 0 index private function extractInfoFromExcelCollection($collection) { return array_values(array_filter($collection->toArray())); } }