diff --git a/app/Classes/Modules/Accounting/ControllersLogic/ImportAmbankStatementLogic.php b/app/Classes/Modules/Accounting/ControllersLogic/ImportAmbankStatementLogic.php new file mode 100644 index 00000000..a1c253c0 --- /dev/null +++ b/app/Classes/Modules/Accounting/ControllersLogic/ImportAmbankStatementLogic.php @@ -0,0 +1,225 @@ + '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); + + $statementDetails = $collection->first(); + + $statementDateRange = trim(explode(':', $statementDetails[3][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); + $totalDebit = $statementDetails[7][7]; + $totalCredit = $statementDetails[8][7]; + $totalTransactions = $totalDebit + $totalCredit; + $beginBalance = ((float) str_replace(',', '', $statementDetails[6][10])); + $endBalance = ((float) str_replace(',', '', $statementDetails[9][10])); + + $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) { + $sheet->slice(13)->map(function ($row, $index) use ($statement, $sheet, $key) { + if (!$row[0] || !$row[11]) { + return; + } + + $postingDate = carbon::createFromFormat('dM', trim($row[0])); + + $description = trim($row[1]); + + $nextRow = $sheet->slice(13)[$index + 1]; + + if (!$nextRow[0] && $nextRow[1]) { + foreach ($nextRow 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 = $description5 . array_key_exists($i, $descriptionArr) ? trim($descriptionArr[$i]) : ""; + } + } + + $amount = $row[9] ? ((float) str_replace(',', '', trim($row[9]))) : (-((float) str_replace(',', '', $row[8]))); + $endBalance = ((float) str_replace(',', '', $row[11])); + $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; + }); + } else { + if (strpos($sheet->first()->first(), "DATE") === false) { + return; + } + + $extraColumn = 0; + if (count($sheet->first()) > 7 && $sheet->first()[7]) { + $extraColumn = 1; + } + + $sheet->slice(1)->map(function ($row, $index) use ($statement, $sheet, $extraColumn, $key) { + if (!$row[0] || !$row[6 + $extraColumn]) { + return; + } + + $postingDate = carbon::createFromFormat('dM', trim($row[0])); + + $description = trim($row[1]); + + $nextRow = $sheet->slice(1)[$index + 1]; + + if (!$nextRow[0] && $nextRow[1]) { + foreach ($nextRow 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 = $description5 . array_key_exists($i, $descriptionArr) ? trim($descriptionArr[$i]) : ""; + } + } + + $amount = $row[5 + $extraColumn] ? ((float) str_replace(',', '', trim($row[5 + $extraColumn]))) : (-((float) str_replace(',', '', $row[4 + $extraColumn]))); + $endBalance = ((float) str_replace(',', '', $row[6 + $extraColumn])); + $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([]); + + } + +} diff --git a/app/Classes/Modules/Accounting/ControllersLogic/ImportCiefLiteStatementLogic.php b/app/Classes/Modules/Accounting/ControllersLogic/ImportCiefLiteStatementLogic.php new file mode 100644 index 00000000..9a909d71 --- /dev/null +++ b/app/Classes/Modules/Accounting/ControllersLogic/ImportCiefLiteStatementLogic.php @@ -0,0 +1,126 @@ + 'Import Cief Lite Statement Transactions Details', + 'message' => 'You have successfully updated the Cief Lite 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); + + $sheet = $collection->first()->skip(1); + + $dateTo = $sheet->first()[10]; + $dateFrom = $sheet->last()[10]; + $totalTransaction = $sheet->count(); + + $offset = 1; + do { + $dateFrom = $sheet->offsetGet($sheet->count() - $offset)[10]; + $offset += 1; + } while(!$dateFrom); + + + $dateFrom = carbon::parse($dateFrom); + $dateTo = carbon::parse($dateTo); + + $statement = AccountStatement::whereDate('date_from', $dateFrom) + ->whereDate('date_to', $dateTo) + ->where('total_amount', $totalTransaction) + ->first(); + + if(!$statement){ + $statement = new AccountStatement([ + 'date_from' => $dateFrom, + 'date_to' => $dateTo, + 'total_amount' => $totalTransaction, + ]); + } + + $account = StatementAccount::where("name", "CIEF LITE")->first(); + + if (!$account) { + throw new Exception("Statement Account for CIEF LITE not found."); + } + + $account->statements()->save($statement); + + $sheet->map(function ($row) use ($statement) { + if (!$row[0]) { + return; + } + $postingDate = $row[10]; + $transactionDescription = trim($row[1]); + $description2 = trim($row[2]); + $description3 = "Fee " . $row[6]; + $description4 = trim($row[8]); + $transactionRef = trim($row[12]); + $amount = ((float) str_replace(',', '', $row[7])); + $transactionCode = $row[0]; + $transaction = new StatementTransaction([ + 'posting_date' => $postingDate, + 'transaction_description' => $transactionDescription, + 'transaction_description_2' => $description2, + 'transaction_description_3' => $description3, + 'transaction_description_4' => $description4, + 'transaction_ref' => $transactionRef, + 'amount' => $amount, + 'transaction_code' => $transactionCode, + ]); + + // Check if the transaction already exists for this statement + $existingTransaction = StatementTransaction::where('transaction_ref', $transactionRef) + ->where('posting_date', $postingDate) + ->where('amount', $amount) + ->where('transaction_description', $transactionDescription) + ->where('transaction_code', $transactionCode) + ->first(); + + if (!$existingTransaction) { + $statement->transactions()->save($transaction); + } + + return $transaction; + }); + } + + return $this->response([]); + + } + +} diff --git a/app/Http/Controllers/Accounting/BankStatementController.php b/app/Http/Controllers/Accounting/BankStatementController.php index ac631ece..c78fa05e 100644 --- a/app/Http/Controllers/Accounting/BankStatementController.php +++ b/app/Http/Controllers/Accounting/BankStatementController.php @@ -3,7 +3,9 @@ namespace App\Http\Controllers\Accounting; use App\Classes\Jobs\CreateBankStatementTransactionOwners; +use App\Classes\Modules\Accounting\ControllersLogic\ImportAmbankStatementLogic; use App\Classes\Modules\Accounting\ControllersLogic\ImportBankStatementLogic; +use App\Classes\Modules\Accounting\ControllersLogic\ImportCiefLiteStatementLogic; use App\Classes\Modules\Accounting\ControllersLogic\ListBankStatementDetailsLogic; use App\Classes\Modules\Accounting\ControllersLogic\ListBankStatementTransactionsLogic; use App\Classes\Modules\Accounting\ControllersLogic\UpdateBankStatementDetailLogic; @@ -87,6 +89,16 @@ class BankStatementController extends Controller return $logic->execute($request); } + public function importAmbank(Request $request, ImportAmbankStatementLogic $logic): JsonResponse + { + return $logic->execute($request); + } + + public function importCiefLite(Request $request, ImportCiefLiteStatementLogic $logic): JsonResponse + { + return $logic->execute($request); + } + public function rerun() { foreach (StatementTransaction::doesntMapStatement()->get()->chunk(30) as $key => $transaction) { diff --git a/resources/assets/vue/components/accounting/forms/ImportStatementFormComponent.vue b/resources/assets/vue/components/accounting/forms/ImportStatementFormComponent.vue index 54db23df..f4aa76a2 100644 --- a/resources/assets/vue/components/accounting/forms/ImportStatementFormComponent.vue +++ b/resources/assets/vue/components/accounting/forms/ImportStatementFormComponent.vue @@ -10,7 +10,9 @@