Cartola Diaria
This commit is contained in:
@ -53,29 +53,29 @@ class Cartolas extends Controller
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function diaria(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\Banco $bancoRepository,
|
||||
Repository\Inmobiliaria\Cuenta $cuentaRepository,
|
||||
Service\Cartola $cartolaService): ResponseInterface
|
||||
{
|
||||
$body = $request->getParsedBody();
|
||||
$output = [
|
||||
'input' => $body,
|
||||
'cartola' => []
|
||||
'cartolas' => []
|
||||
];
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($body['inmobiliaria_rut']);
|
||||
$banco = $bancoRepository->fetchById($body['banco_id']);
|
||||
$fecha = new DateTimeImmutable($body['fecha']);
|
||||
$file = $request->getUploadedFiles()['file'];
|
||||
$output['cartola'] = $cartolaService->diaria($inmobiliaria, $banco, $fecha, $file);
|
||||
} catch (EmptyResult $exception) {
|
||||
$this->logger->debug($exception);
|
||||
$fields = json_decode($body['fields']);
|
||||
foreach ($fields as $field) {
|
||||
try {
|
||||
$cuenta = $cuentaRepository->fetchById($body["cuenta_id{$field}"]);
|
||||
$fecha = new DateTimeImmutable($body["fecha{$field}"]);
|
||||
$file = $request->getUploadedFiles()["file{$field}"];
|
||||
|
||||
$output['cartolas'][$field] = $cartolaService->diaria($cuenta, $fecha, $file);
|
||||
} catch (EmptyResult $exception) {
|
||||
$this->logger->debug($exception);
|
||||
}
|
||||
}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
public function ayer(ServerRequestInterface $request, ResponseInterface $response,
|
||||
Repository\Inmobiliaria $inmobiliariaRepository,
|
||||
Repository\Banco $bancoRepository,
|
||||
Repository\Inmobiliaria\Cuenta $cuentaRepository,
|
||||
Repository\Cartola $cartolaRepository): ResponseInterface
|
||||
{
|
||||
@ -85,14 +85,10 @@ class Cartolas extends Controller
|
||||
'cartola' => []
|
||||
];
|
||||
try {
|
||||
$inmobiliaria = $inmobiliariaRepository->fetchById($body['inmobiliaria_rut']);
|
||||
$banco = $bancoRepository->fetchById($body['banco_id']);
|
||||
$cuenta = $cuentaRepository->fetchByInmobiliariaAndBanco($inmobiliaria->rut, $banco->id);
|
||||
$cuenta = $cuentaRepository->fetchById($body['cuenta_id']);
|
||||
$fecha = new DateTimeImmutable($body['fecha']);
|
||||
$output['cartola'] = $cartolaRepository->fetchByCuentaAndFecha($cuenta->id, $fecha);
|
||||
} catch (EmptyResult $exception) {
|
||||
$this->logger->critical($exception);
|
||||
}
|
||||
} catch (EmptyResult) {}
|
||||
return $this->withJson($response, $output);
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ class Contabilidad extends Controller
|
||||
$inmobiliarias = $this->fetchRedis($redisService, $redisKey);
|
||||
} catch (EmptyRedis) {
|
||||
try {
|
||||
$inmobiliarias = $inmobiliariaRepository->fetchAllActive();
|
||||
$inmobiliarias = $inmobiliariaRepository->fetchAll();
|
||||
$this->saveRedis($redisService, $redisKey, $inmobiliarias, 30 * 24 * 60 * 60);
|
||||
} catch (EmptyResult) {}
|
||||
}
|
||||
|
@ -39,11 +39,9 @@ class Cartola extends Service
|
||||
{
|
||||
return $this->exporter->export($inmobiliaria, $banco, $mes, $movimientos);
|
||||
}
|
||||
public function diaria(Model\Inmobiliaria $inmobiliaria, Model\Banco $banco, DateTimeInterface $fecha, UploadedFileInterface $file): array
|
||||
public function diaria(Model\Inmobiliaria\Cuenta $cuenta, DateTimeInterface $fecha, UploadedFileInterface $file): array
|
||||
{
|
||||
$cuenta = $this->cuentaRepository->fetchByInmobiliariaAndBanco($inmobiliaria->rut, $banco->id);
|
||||
|
||||
$ms = $this->getMovimientosDiarios($banco, $file);
|
||||
$ms = $this->getMovimientosDiarios($cuenta->banco, $file);
|
||||
|
||||
$cartolaData = [
|
||||
'cargos' => 0,
|
||||
@ -59,6 +57,9 @@ class Cartola extends Service
|
||||
$cartolaData['cargos'] += $movimiento->cargo;
|
||||
$cartolaData['abonos'] += $movimiento->abono;
|
||||
}
|
||||
if ($movimiento->fecha->getTimestamp() > $fecha->getTimestamp()) {
|
||||
continue;
|
||||
}
|
||||
$cartolaData['saldo'] = $movimiento->saldo;
|
||||
}
|
||||
$cartola = $this->buildCartola($cuenta, $fecha, $cartolaData);
|
||||
@ -68,21 +69,7 @@ class Cartola extends Service
|
||||
protected function getMovimientosDiarios(Model\Banco $banco, UploadedFileInterface $file): array
|
||||
{
|
||||
$movimientos = $this->bancos[strtolower($banco->nombre)]->process($file);
|
||||
switch (strtolower($banco->nombre)) {
|
||||
case 'security':
|
||||
$movimientos = $this->processMovimientosDiariosSecurity($movimientos);
|
||||
break;
|
||||
case 'itau':
|
||||
case 'santander':
|
||||
break;
|
||||
}
|
||||
return $movimientos;
|
||||
}
|
||||
protected function processMovimientosDiariosSecurity(array $movimientos): array
|
||||
{
|
||||
$movimientos = array_reverse($movimientos);
|
||||
array_shift($movimientos);
|
||||
return $movimientos;
|
||||
return $this->bancos[strtolower($banco->nombre)]->processMovimientosDiarios($movimientos);
|
||||
}
|
||||
protected function buildCartola(Model\Inmobiliaria\Cuenta $cuenta, DateTimeInterface $fecha, array $data): Model\Cartola
|
||||
{
|
||||
@ -98,13 +85,14 @@ class Cartola extends Service
|
||||
protected function buildMovimiento(Model\Inmobiliaria\Cuenta $cuenta, array $data): Model\Movimiento
|
||||
{
|
||||
try {
|
||||
$valor = ($data['cargo'] !== 0 and $data['cargo'] !== null) ? $data['cargo'] : $data['abono'];
|
||||
return $this->movimientoRepository
|
||||
->fetchByCuentaAndFechaAndMonto(
|
||||
$cuenta->id,
|
||||
new DateTimeImmutable($data['fecha']),
|
||||
$data['cargo'] ?? $data['abono']
|
||||
$valor
|
||||
);
|
||||
} catch (Exception\EmptyResult) {
|
||||
} catch (Exception\EmptyResult $exception) {
|
||||
$data['cuenta_id'] = $cuenta->id;
|
||||
$movimiento = $this->movimientoRepository->create($data);
|
||||
return $this->movimientoRepository->save($movimiento);
|
||||
|
@ -8,6 +8,14 @@ use Incoviba\Common\Ideal\Cartola\Banco;
|
||||
|
||||
class Itau extends Banco
|
||||
{
|
||||
const CUENTA_CORRIENTE = 0;
|
||||
const ULTIMOS_MOVIMIENTOS = 1;
|
||||
|
||||
public function processMovimientosDiarios(array $movimientos): array
|
||||
{
|
||||
return array_reverse($movimientos);
|
||||
}
|
||||
|
||||
protected function columnMap(): array
|
||||
{
|
||||
return [
|
||||
@ -15,7 +23,10 @@ class Itau extends Banco
|
||||
'Número de operación' => 'documento',
|
||||
'Descripción' => 'glosa',
|
||||
'Depósitos o abonos' => 'abono',
|
||||
'Giros o cargos' => 'cargo'
|
||||
'Giros o cargos' => 'cargo',
|
||||
'Documentos' => 'documento',
|
||||
'Movimientos' => 'glosa',
|
||||
'Saldos' => 'saldo'
|
||||
];
|
||||
}
|
||||
|
||||
@ -28,53 +39,130 @@ class Itau extends Banco
|
||||
$xlsx = $reader->load($filename);
|
||||
$sheet = $xlsx->getActiveSheet();
|
||||
|
||||
$dates = explode(' - ', $sheet->getCell('C4')->getCalculatedValue());
|
||||
$date = DateTimeImmutable::createFromFormat('d/m/Y', $dates[0]);
|
||||
$year = $date->format('Y');
|
||||
|
||||
$rowIndex = 26;
|
||||
$columns = [];
|
||||
$row = $sheet->getRowIterator($rowIndex)->current();
|
||||
$cols = $row->getColumnIterator('A','G');
|
||||
foreach ($cols as $col) {
|
||||
$columns []= trim($col->getCalculatedValue());
|
||||
}
|
||||
$rowIndex ++;
|
||||
$row = $sheet->getRowIterator($rowIndex)->current();
|
||||
$cols = $row->getColumnIterator('A', 'G');
|
||||
$colIndex = 0;
|
||||
foreach ($cols as $col) {
|
||||
$value = $col->getCalculatedValue();
|
||||
if ($value !== null) {
|
||||
$columns[$colIndex] .= " {$value}";
|
||||
}
|
||||
$colIndex ++;
|
||||
}
|
||||
$rowIndex ++;
|
||||
$data = [];
|
||||
$rows = $sheet->getRowIterator($rowIndex);
|
||||
foreach ($rows as $row) {
|
||||
if ($sheet->getCell("A{$rowIndex}")->getCalculatedValue() === null) {
|
||||
try {
|
||||
switch ($this->identifySheet($sheet)) {
|
||||
case self::CUENTA_CORRIENTE:
|
||||
$data = $this->parseCuentaCorriente($sheet);
|
||||
break;
|
||||
case self::ULTIMOS_MOVIMIENTOS:
|
||||
$data = $this->parseUltimosMovimientos($sheet);
|
||||
break;
|
||||
}
|
||||
} catch (PhpSpreadsheet\Exception $exception) {
|
||||
$this->logger->critical($exception);
|
||||
} finally {
|
||||
unlink($filename);
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
protected function parseCuentaCorriente(PhpSpreadsheet\Worksheet\Worksheet $sheet): array
|
||||
{
|
||||
$found = false;
|
||||
$year = 0;
|
||||
$columns = [];
|
||||
$data = [];
|
||||
foreach ($sheet->getRowIterator() as $row) {
|
||||
if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'Cartola Histórica') {
|
||||
$columnIndex = 'A';
|
||||
foreach ($row->getColumnIterator() as $column) {
|
||||
if ($column->getValue() !== 'Periodo') {
|
||||
continue;
|
||||
}
|
||||
$columnIndex = $column->getColumn();
|
||||
break;
|
||||
}
|
||||
$dates = explode(' - ', $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue());
|
||||
$date = DateTimeImmutable::createFromFormat('d/m/Y', $dates[0]);
|
||||
$year = $date->format('Y');
|
||||
}
|
||||
if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'Movimientos') {
|
||||
$found = true;
|
||||
foreach ($row->getColumnIterator() as $column) {
|
||||
if ($column->getValue() === null) {
|
||||
break;
|
||||
}
|
||||
$columns[$column->getColumn()] = trim($column->getValue());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (!$found) {
|
||||
continue;
|
||||
}
|
||||
if ($sheet->getCell("A{$row->getRowIndex()}")->getValue() === null) {
|
||||
break;
|
||||
}
|
||||
$cols = $row->getColumnIterator('A', 'G');
|
||||
$colIndex = 0;
|
||||
$rowData = [];
|
||||
foreach ($cols as $col) {
|
||||
$value = $col->getCalculatedValue();
|
||||
$col = $columns[$colIndex];
|
||||
if ($col === 'Fecha') {
|
||||
foreach ($columns as $columnIndex => $column) {
|
||||
$value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue();
|
||||
$mapped = $this->columnMap()[$column];
|
||||
if ($mapped === 'fecha') {
|
||||
list($d, $m) = explode('/', $value);
|
||||
$value = "{$year}-{$m}-{$d}";
|
||||
}
|
||||
$rowData[$col] = $value;
|
||||
$colIndex ++;
|
||||
if (in_array($mapped, ['cargo', 'abono', 'saldo'])) {
|
||||
$value = (int) $value;
|
||||
}
|
||||
$rowData[$column] = $value;
|
||||
}
|
||||
$data []= $rowData;
|
||||
$rowIndex ++;
|
||||
}
|
||||
|
||||
unlink($filename);
|
||||
return $data;
|
||||
}
|
||||
protected function parseUltimosMovimientos(PhpSpreadsheet\Worksheet\Worksheet $sheet): array
|
||||
{
|
||||
$found = false;
|
||||
$data = [];
|
||||
$columns = [];
|
||||
foreach ($sheet->getRowIterator() as $row) {
|
||||
if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'Últimos Movimientos') {
|
||||
$found = true;
|
||||
continue;
|
||||
}
|
||||
if (!$found) {
|
||||
continue;
|
||||
}
|
||||
if (count($columns) === 0) {
|
||||
foreach ($row->getColumnIterator() as $cell) {
|
||||
if ($cell->getValue() === null) {
|
||||
break;
|
||||
}
|
||||
$columns[$cell->getColumn()] = trim($cell->getValue());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if ($sheet->getCell("A{$row->getRowIndex()}")->getValue() === null) {
|
||||
break;
|
||||
}
|
||||
$rowData = [];
|
||||
foreach ($columns as $columnIndex => $column) {
|
||||
$value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue();
|
||||
$mapped = $this->columnMap()[$column] ?? $column;
|
||||
if ($mapped === 'fecha') {
|
||||
$value = PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value, 'America/Santiago')->format('Y-m-d');
|
||||
}
|
||||
if (in_array($mapped, ['abono', 'cargo', 'saldo'])) {
|
||||
$value = (int) $value;
|
||||
}
|
||||
$rowData[$column] = $value;
|
||||
}
|
||||
$data []= $rowData;
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function identifySheet(PhpSpreadsheet\Worksheet\Worksheet $sheet): int
|
||||
{
|
||||
foreach ($sheet->getRowIterator(1, 10) as $row) {
|
||||
$value = $sheet->getCell("A{$row->getRowIndex()}")->getValue();
|
||||
if ($value === 'Estado de Cuenta Corriente') {
|
||||
return self::CUENTA_CORRIENTE;
|
||||
}
|
||||
if ($value === 'Consulta Saldos y Últimos movimientos') {
|
||||
return self::ULTIMOS_MOVIMIENTOS;
|
||||
}
|
||||
}
|
||||
throw new PhpSpreadsheet\Exception();
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ class Santander extends Banco
|
||||
'DESCRIPCIÓN MOVIMIENTO' => 'glosa',
|
||||
'FECHA' => 'fecha',
|
||||
'N° DOCUMENTO' => 'documento',
|
||||
'SALDO' => 'saldo'
|
||||
];
|
||||
}
|
||||
protected function parseFile(UploadedFileInterface $uploadedFile): array
|
||||
@ -28,42 +29,47 @@ class Santander extends Banco
|
||||
$xlsx = $reader->load($filename);
|
||||
$sheet = $xlsx->getActiveSheet();
|
||||
|
||||
$rowIndex = 16;
|
||||
$found = false;
|
||||
$columns = [];
|
||||
$row = $sheet->getRowIterator($rowIndex)->current();
|
||||
$cols = $row->getColumnIterator('A', 'H');
|
||||
foreach ($cols as $col) {
|
||||
$columns []= $col->getCalculatedValue();
|
||||
}
|
||||
$rowIndex ++;
|
||||
$rows = $sheet->getRowIterator($rowIndex);
|
||||
$data = [];
|
||||
foreach ($rows as $row) {
|
||||
if ($sheet->getCell("A{$rowIndex}")->getCalculatedValue() === 'Resumen comisiones') {
|
||||
foreach ($sheet->getRowIterator() as $row) {
|
||||
if (!$found and $sheet->getCell("A{$row->getRowIndex()}")->getCalculatedValue() === 'MONTO') {
|
||||
$found = true;
|
||||
foreach ($row->getColumnIterator() as $column) {
|
||||
if ($column->getValue() === null) {
|
||||
break;
|
||||
}
|
||||
$columns[$column->getColumn()] = trim($column->getValue());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (!$found) {
|
||||
continue;
|
||||
}
|
||||
if ($sheet->getCell("A{$row->getRowIndex()}")->getValue() === null) {
|
||||
break;
|
||||
}
|
||||
$cols = $row->getColumnIterator('A', 'H');
|
||||
$colIndex = 0;
|
||||
$rowData = [];
|
||||
foreach ($cols as $col) {
|
||||
$value = $col->getCalculatedValue();
|
||||
$col = $columns[$colIndex];
|
||||
if ($col === 'FECHA') {
|
||||
list($d,$m,$Y) = explode('/', $value);
|
||||
$value = "{$Y}-{$m}-{$d}";
|
||||
foreach ($columns as $columnIndex => $column) {
|
||||
$value = $sheet->getCell("{$columnIndex}{$row->getRowIndex()}")->getCalculatedValue();
|
||||
$mapped = $this->columnMap()[$column] ?? $column;
|
||||
if ($mapped === 'fecha') {
|
||||
$value = implode('-', array_reverse(explode('/', $value)));
|
||||
}
|
||||
$rowData[$col] = $value;
|
||||
$colIndex ++;
|
||||
if ($column === 'MONTO') {
|
||||
$value = (int) $value;
|
||||
}
|
||||
$rowData[$column] = $value;
|
||||
}
|
||||
if ($rowData['CARGO/ABONO'] === 'C') {
|
||||
$rowData['cargo'] = -$rowData['MONTO'];
|
||||
$rowData['MONTO'] = -$rowData['MONTO'];
|
||||
$rowData['cargo'] = $rowData['MONTO'];
|
||||
$rowData['abono'] = 0;
|
||||
} else {
|
||||
$rowData['cargo'] = 0;
|
||||
$rowData['abono'] = $rowData['MONTO'];
|
||||
}
|
||||
$data []= $rowData;
|
||||
$rowIndex ++;
|
||||
}
|
||||
|
||||
return $data;
|
||||
|
@ -9,6 +9,13 @@ use Incoviba\Common\Ideal\Cartola\Banco;
|
||||
|
||||
class Security extends Banco
|
||||
{
|
||||
public function processMovimientosDiarios(array $movimientos): array
|
||||
{
|
||||
$movimientos = array_reverse($movimientos);
|
||||
array_shift($movimientos);
|
||||
return $movimientos;
|
||||
}
|
||||
|
||||
protected function parseFile(UploadedFileInterface $uploadedFile): array
|
||||
{
|
||||
$stream = $uploadedFile->getStream();
|
||||
|
Reference in New Issue
Block a user