diff --git a/app/src/Service/Contabilidad/Informe/Tesoreria.php b/app/src/Service/Contabilidad/Informe/Tesoreria.php index 26460e4..5bc8e56 100644 --- a/app/src/Service/Contabilidad/Informe/Tesoreria.php +++ b/app/src/Service/Contabilidad/Informe/Tesoreria.php @@ -12,6 +12,20 @@ use Psr\Log\LoggerInterface; class Tesoreria extends Ideal\Service { + protected const ORDEN_SOCIEDADES = [ + 99543380, + 78017310, + 76837020, + 76743916, + 76519798, + 76309587, + 77023196, + 77023201, + 76309592, + 76163347, + 76158424 + ]; + public function __construct(LoggerInterface $logger, protected Repository\Inmobiliaria $inmobiliariaRepository, protected Service\Inmobiliaria\Cuenta $cuentaService, @@ -23,8 +37,8 @@ class Tesoreria extends Ideal\Service { parent::__construct($logger); - $this->movimientos = new class() { - public function __construct() + $this->movimientos = new class(self::ORDEN_SOCIEDADES) { + public function __construct(protected array $ordenSociedades) { $this->dap = new class() { @@ -45,8 +59,28 @@ class Tesoreria extends Ideal\Service } return $this; } + public function updateDap(object $movimiento): void + { + foreach ($this->ingresos as $ingreso) { + if ($movimiento->cuenta->inmobiliaria->rut !== $ingreso->cuenta->inmobiliaria->rut) { + continue; + } + if ($movimiento->fecha->format('Y-m-d') !== $ingreso->fecha->format('Y-m-d')) { + continue; + } + if ($movimiento->documento !== $ingreso->documento) { + continue; + } + $ingreso->glosa = $movimiento->glosa; + break; + } + } public function build(): array { + $this->dap->ingresos = $this->sortBySociedades($this->dap->ingresos); + $this->dap->egresos = $this->sortBySociedades($this->dap->egresos); + $this->ingresos = $this->sortBySociedades($this->ingresos); + $this->egresos = $this->sortBySociedades($this->egresos); return [ 'capital dap' => [ 'ingresos' => $this->dap->ingresos, @@ -56,12 +90,42 @@ class Tesoreria extends Ideal\Service 'egresos' => $this->egresos ]; } + + private function sortBySociedades(array $movimientos): array + { + $temp = []; + foreach ($this->ordenSociedades as $sociedad_rut) { + $date = null; + foreach ($movimientos as $movimiento) { + if ($date === null) { + $date = $movimiento->fecha; + } + if ($movimiento->fecha !== $date) { + if ($movimiento->cuenta->inmobiliaria->rut === $sociedad_rut) { + $temp []= $movimiento; + } + $date = $movimiento->fecha; + continue; + } + if ($movimiento->cuenta->inmobiliaria->rut === $sociedad_rut) { + $temp []= $movimiento; + } + } + } + foreach ($movimientos as $movimiento) { + if (!in_array($movimiento, $temp)) { + $temp []= $movimiento; + } + } + return $temp; + } }; $this->totales = new class() { public int $anterior = 0; public int $actual = 0; public int $ffmm = 0; public int $deposito = 0; + public int $saldo = 0; public function diferencia(): int { @@ -120,8 +184,21 @@ class Tesoreria extends Ideal\Service } catch (Implement\Exception\EmptyResult) { return []; } - $informe = ['inmobiliarias' => []]; + $temp = []; + foreach (self::ORDEN_SOCIEDADES as $sociedad_rut) { + foreach ($inmobiliarias as $inmobiliaria) { + if ($inmobiliaria->rut === $sociedad_rut) { + $temp []= $inmobiliaria; + } + } + } foreach ($inmobiliarias as $inmobiliaria) { + if (!in_array($inmobiliaria, $temp)) { + $temp []= $inmobiliaria; + } + } + $informe = ['inmobiliarias' => []]; + foreach ($temp as $inmobiliaria) { $informe['inmobiliarias'][$inmobiliaria->rut] = $this->buildInmobiliaria($inmobiliaria, $fecha); } $informe['movimientos'] = $this->buildMovimientos(); @@ -201,8 +278,9 @@ class Tesoreria extends Ideal\Service } $data->deposito += $deposito->capital; $this->addTotal(self::TOTAL_DAP, $deposito->capital); + $this->totales->saldo += $deposito->capital; - if ($deposito->inicio === $fecha) { + if ($deposito->inicio->format('Y-m-d') === $fecha->format('Y-m-d')) { $this->addMovimientos(self::DAP_EGRESOS, [(object) [ 'cuenta' => $deposito->cuenta, 'fecha' => $deposito->inicio, @@ -212,17 +290,18 @@ class Tesoreria extends Ideal\Service 'glosa' => 'INVERSION DAP' ]]); } - if ($deposito->termino === $fecha) { + if ($deposito->termino->format('Y-m-d') === $fecha->format('Y-m-d')) { $data->deposito -= $deposito->capital; $this->addTotal(self::TOTAL_DAP, -$deposito->capital); - $this->addMovimientos(self::DAP_INGRESOS, [(object) [ + $this->addMovimientos(self::INGRESOS, [(object) [ 'cuenta' => $deposito->cuenta, 'fecha' => $deposito->termino, 'cargo' => 0, - 'abono' => $deposito->capital, - 'saldo' => $deposito->capital, - 'glosa' => 'RESCATE DAP' + 'abono' => $deposito->futuro - $deposito->capital, + 'saldo' => $deposito->futuro - $deposito->capital, + 'glosa' => 'RESCATE DAP', + 'documento' => $deposito->id ]]); } } @@ -236,6 +315,7 @@ class Tesoreria extends Ideal\Service try { $cartola = $this->cartolaRepository->fetchLastByCuentaAndFecha($cuenta->id, $anterior); $data->anterior = $cartola->saldo; + $this->totales->saldo += $cartola->saldo; } catch (Implement\Exception\EmptyResult) {} if ($data->diferencia() !== 0) { try { @@ -257,6 +337,7 @@ class Tesoreria extends Ideal\Service [$data->anterior, $data->actual] ); } + return $dataInmobiliaria; } protected function buildMovimientos(): array @@ -269,12 +350,16 @@ class Tesoreria extends Ideal\Service } protected function addMovimientos(string $tipo, array $movimientos): Tesoreria { - if (str_contains($tipo, 'dap')) { + if (str_starts_with($tipo, 'dap')) { list($d, $t) = explode('->', $tipo); $this->movimientos->addDap($t, $movimientos); return $this; } foreach ($movimientos as $movimiento) { + if ($tipo === 'ingresos' and str_contains(strtolower($movimiento->glosa), ' dap ')) { + $this->movimientos->updateDap($movimiento); + continue; + } $this->movimientos->{$tipo} []= $movimiento; } return $this; diff --git a/app/src/Service/Contabilidad/Informe/Tesoreria/Excel.php b/app/src/Service/Contabilidad/Informe/Tesoreria/Excel.php index 6c9f813..fe83b77 100644 --- a/app/src/Service/Contabilidad/Informe/Tesoreria/Excel.php +++ b/app/src/Service/Contabilidad/Informe/Tesoreria/Excel.php @@ -12,17 +12,6 @@ use Incoviba\Service; class Excel extends Ideal\Service { protected const CURRENCY_CODE = '_ $* #,##0_ ;_ $* -#,##0_ ;_ $* "-"_ ;_ @_ '; - protected const ORDEN_SOCIEDADES = [ - 99543380, - 78017310, - 76837020, - 76743916, - 76519798, - 76309587, - 77023196, - 77023201, - 76309592, - ]; public function __construct(LoggerInterface $logger, protected string $folder, protected Service\UF $ufService, protected Service\USD $usdService) { @@ -187,14 +176,7 @@ class Excel extends Ideal\Service $this->fillColumns($sheet, $columns, $styles, $startRow); $rowIndex = $startRow + 1; - $sociedades = []; - foreach (self::ORDEN_SOCIEDADES as $sociedad_rut) { - foreach ($data['inmobiliarias'] as $dataSociedad) { - if ($dataSociedad->inmobiliaria->rut === $sociedad_rut) { - $sociedades []= $dataSociedad; - } - } - } + $sociedades = $data['inmobiliarias']; foreach ($sociedades as $dataSociedad) { $rowIndex += $this->fillSociedad($sheet, $dataSociedad, $rowIndex); } @@ -286,7 +268,7 @@ class Excel extends Ideal\Service 'formatCode' => PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DDMMYYYY ] ]); - $sheet->getCell("V{$rowIndex}")->setValue(0)->getStyle()->applyFromArray([ + $sheet->getCell("V{$rowIndex}")->setValue($data['totales']->saldo)->getStyle()->applyFromArray([ 'font' => [ 'bold' => true ], @@ -299,81 +281,7 @@ class Excel extends Ideal\Service $conditional = $this->getConditional(); - $dataMovimientos = [ - 'capital dap' => [ - 'ingresos' => [], - 'egresos' => [] - ], - 'ingresos' => [], - 'egresos' => [] - ]; - foreach (self::ORDEN_SOCIEDADES as $sociedad_rut) { - $date = null; - foreach ($data['movimientos']['capital dap']['ingresos'] as $movimiento) { - if ($date === null) { - $date = $movimiento->fecha; - } - if ($movimiento->fecha !== $date) { - if ($movimiento->cuenta->inmobiliaria->rut === $sociedad_rut) { - $dataMovimientos['movimientos']['capital dap']['ingresos'] [] = $movimiento; - } - $date = $movimiento->fecha; - continue; - } - if ($movimiento->cuenta->inmobiliaria->rut === $sociedad_rut) { - $dataMovimientos['movimientos']['capital dap']['ingresos'] []= $movimiento; - } - } - $date = null; - foreach ($data['movimientos']['capital dap']['egresos'] as $movimiento) { - if ($date === null) { - $date = $movimiento->fecha; - } - if ($movimiento->fecha !== $date) { - if ($movimiento->cuenta->inmobiliaria->rut === $sociedad_rut) { - $dataMovimientos['movimientos']['capital dap']['egresos'] [] = $movimiento; - } - $date = $movimiento->fecha; - continue; - } - if ($movimiento->cuenta->inmobiliaria->rut === $sociedad_rut) { - $dataMovimientos['movimientos']['capital dap']['egresos'] []= $movimiento; - } - } - - $date = null; - foreach ($data['movimientos']['ingresos'] as $movimiento) { - if ($date === null) { - $date = $movimiento->fecha; - } - if ($movimiento->fecha !== $date) { - if ($movimiento->cuenta->inmobiliaria->rut === $sociedad_rut) { - $dataMovimientos['ingresos'] [] = $movimiento; - } - $date = $movimiento->fecha; - continue; - } - if ($movimiento->cuenta->inmobiliaria->rut === $sociedad_rut) { - $dataMovimientos['ingresos'] []= $movimiento; - } - } - $date = null; - foreach ($data['movimientos']['egresos'] as $movimiento) { - if ($date === null) { - $date = $movimiento->fecha; - } - if ($movimiento->fecha !== $date) { - if ($movimiento->cuenta->inmobiliaria->rut === $sociedad_rut) { - $dataMovimientos['egresos'] [] = $movimiento; - } - $date = $movimiento->fecha; - continue; - } - if ($movimiento->cuenta->inmobiliaria->rut === $sociedad_rut) { - $dataMovimientos['egresos'] []= $movimiento; - } - } - } + $dataMovimientos = $data['movimientos']; $styles = [ 'fill' => [ @@ -429,7 +337,7 @@ class Excel extends Ideal\Service $sheet->getCell("V{$rowIndex}")->getStyle()->getNumberFormat() ->setFormatCode(self::CURRENCY_CODE); $totalRow = $rowIndex; - if (!isset($rowIngreso) or $rowIngreso === null) { + if ($rowIngreso === null) { $rowIngreso = $rowIndex; } else { $rowEgreso = $rowIndex; @@ -557,18 +465,20 @@ class Excel extends Ideal\Service foreach ($dataSociedad->cuentas as $cuentaRowIndex => $cuenta) { $this->fillCuenta($sheet, $cuenta, 3, $baseRowIndex + $cuentaRowIndex); } - $sheet->getCell("K{$rowIndex}")->setValue($dataSociedad->total()); - $sheet->getCell("L{$rowIndex}")->setValue($dataSociedad->ffmm()); - $sheet->getCell("M{$rowIndex}")->setValue($dataSociedad->deposito()); - $sheet->getCell("N{$rowIndex}")->setValue($dataSociedad->caja()); + $finalRow = $rowIndex + count($dataSociedad->cuentas) - 1; + + $sheet->getCell("K{$rowIndex}")->setValue("=SUM(F{$rowIndex}:F{$finalRow})"); + $sheet->getCell("L{$rowIndex}")->setValue("=SUM(H{$rowIndex}:H{$finalRow})"); + $sheet->getCell("M{$rowIndex}")->setValue("=SUM(I{$rowIndex}:I{$finalRow})"); + $sheet->getCell("N{$rowIndex}")->setValue("=SUM(J{$rowIndex}:J{$finalRow})"); if (count($dataSociedad->cuentas) > 1) { - $finalRow = $rowIndex + count($dataSociedad->cuentas) - 1; $sheet->mergeCells("B{$rowIndex}:B{$finalRow}"); $sheet->mergeCells("K{$rowIndex}:K{$finalRow}"); $sheet->mergeCells("L{$rowIndex}:L{$finalRow}"); $sheet->mergeCells("M{$rowIndex}:M{$finalRow}"); $sheet->mergeCells("N{$rowIndex}:N{$finalRow}"); + $sheet->getStyle("K{$rowIndex}:N{$finalRow}")->getAlignment()->setVertical(PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER); } return count($dataSociedad->cuentas); }