mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-21 05:23:58 +00:00
226 lines
10 KiB
PHP
226 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Accounting\ControllersLogic;
|
|
|
|
|
|
use App\Classes\Exceptions\MalformedRequestException;
|
|
use App\Classes\General\Abstracts\AbstractControllerLogic;
|
|
use App\Classes\Modules\Documents\DataTransferObjects\DocumentObject;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Models\AccountStatement;
|
|
use App\Models\StatementAccount;
|
|
use App\Models\StatementTransaction;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
class ImportAmbankStatementLogic extends AbstractControllerLogic
|
|
{
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function notification():array {
|
|
return [
|
|
'title' => '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([]);
|
|
|
|
}
|
|
|
|
}
|