mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-21 05:23:58 +00:00
127 lines
4.3 KiB
PHP
127 lines
4.3 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 ImportCiefLiteStatementLogic extends AbstractControllerLogic
|
|
{
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
protected function notification():array {
|
|
return [
|
|
'title' => '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([]);
|
|
|
|
}
|
|
|
|
}
|