diff --git a/app/Controller/Informes.php b/app/Controller/Informes.php index 43862a2..cdace2c 100644 --- a/app/Controller/Informes.php +++ b/app/Controller/Informes.php @@ -561,108 +561,7 @@ class Informes $service = new Resumen(); $service->build($id, new \DateTimeImmutable($fecha)); - - return; - - $proyecto = model(Proyecto::class)->findOne($id); - $mes = null; - if ($fecha != null) { - $mes = Carbon::parse($fecha)->addMonths(1)->subDays(1); - } - - $ventas = $proyecto->ventas(); - - usort($ventas, function($a, $b) { - return $a->fecha()->timestamp - $b->fecha()->timestamp; - }); - - $name = 'Contabilidad'; - $hoy = Carbon::now(config('app.timezone')); - $filename = str_replace('ñ', 'n', $name . ' - ' . $proyecto->descripcion . ' - ' . $hoy->format('Y-m-d') . '.xls'); - $informe = new PHPExcel($name, $filename); - - $columnas = [ - 'Propietario', - ['name' => 'Departamento', 'style' => 'general_number'], - ['name' => 'Estacionamientos', 'style' => 'number'], - ['name' => 'Bodegas', 'style' => 'number'], - 'Fecha Venta', - ['name' => 'Mes', 'style' => 'mes'], - 'Tipo', - ['name' => 'm² Ponderados', 'style' => 'amount'], - ['name' => 'Valor Promesa', 'style' => 'amount'], - ['name' => 'Pie [UF]', 'style' => 'amount'], - ['name' => 'Pie [$]', 'style' => 'amount'], - ['name' => 'Abono Escritura', 'style' => 'amount'], - ['name' => 'Crédito', 'style' => 'amount'], - ['name' => 'Cuotas', 'style' => 'number'], - ['name' => 'Cuotas Pagadas', 'style' => 'number'], - ['name' => 'Pie Pagado [UF]', 'style' => 'amount'], - ['name' => 'Pie Pagado [$]', 'style' => 'amount'] - ]; - $informe->addColumns($columnas); - - $data = []; - foreach ($ventas as $venta) { - $info = []; - $info['Propietario'] = mb_strtoupper($venta->propietario()->nombreCompleto()); - $info['Departamento'] = $venta->unidad()->descripcion; - $ests = []; - if ($venta->propiedad()->estacionamientos != '') { - $es = $venta->propiedad()->estacionamientos(); - foreach ($es as $e) { - $ests []= $e->descripcion; - } - } - $info['Estacionamientos'] = implode(', ', $ests); - $bods = []; - if ($venta->propiedad()->bodegas != '') { - $bs = $venta->propiedad()->bodegas(); - foreach ($bs as $b) { - $bods []= $b->descripcion; - } - } - $info['Bodegas'] = implode(', ', $bods); - $info['Fecha Venta'] = $venta->fecha()->format('d.m.Y'); - $info['Mes'] = $venta->fecha()->format('M-y'); - $info['Tipo'] = $venta->unidad()->abreviacion; - $info['m² Ponderados'] = $venta->unidad()->m2('vendible'); - $info['Valor Promesa'] = $venta->valor_uf; - $info['Pie [UF]'] = 0; - $info['Pie [$]'] = 0; - $info['Abono Escritura'] = 0; - $info['Crédito'] = 0; - $info['Cuotas'] = 0; - $info['Cuotas Pagadas'] = 0; - $info['Pie Pagado [UF]'] = 0; - $info['Pie Pagado [$]'] = 0; - if ($venta->pie()) { - $info['Pie [UF]'] = $venta->pie()->valor; - $info['Pie [$]'] = $venta->pie()->valorPesos(); - $info['Abono Escritura'] = ($venta->escritura != 0) ? $venta->escritura()->valor('ufs') : ($venta->valor_uf - $venta->pie()->valor - (($venta->credito != 0) ? $venta->credito()->pago()->valor('ufs') : 0)); - $info['Crédito'] = ($venta->credito != 0) ? $venta->credito()->pago()->valor('ufs') : 0; - $info['Cuotas'] = $venta->pie()->cuotas; - $info['Cuotas Pagadas'] = count($venta->pie()->pagadas($mes)); - $info['Pie Pagado [UF]'] = $venta->pie()->valorPagado('uf', $mes); - $info['Pie Pagado [$]'] = $venta->pie()->valorPagado('pesos', $mes); - } - - $data []= $info; - } - $informe->addData($data); - - $totals = [ - 'Departamento' => 'count', - 'Estacionamientos' => 'count', - 'Bodegas' => 'count', - 'm² Ponderados' => 'sum', - 'Valor Promesa' => 'sum', - 'Pie' => 'sum', - 'Pie Pagado' => 'sum' - ]; - $informe->addTotals($totals); - - return $informe->informe(); + return ''; } else { $proyectos = model(Proyecto::class)->order_by_asc('descripcion')->find_many(); return view('informes.resumen_contabilidad', compact('proyectos')); diff --git a/app/Service/Informe/Contabilidad/Resumen.php b/app/Service/Informe/Contabilidad/Resumen.php index 85f53f8..66f9e10 100644 --- a/app/Service/Informe/Contabilidad/Resumen.php +++ b/app/Service/Informe/Contabilidad/Resumen.php @@ -32,7 +32,21 @@ class Resumen protected function getVentas($id_proyecto) { $proyecto = model(Proyecto::class)->findOne($id_proyecto); - return $proyecto->ventas(); + $ventas = $proyecto->ventas(); + $output = []; + foreach ($ventas as $venta) { + $found = false; + foreach ($output as $v) { + if ($v->unidad()->descripcion == $venta->unidad()->descripcion) { + $found = true; + break; + } + } + if (!$found) { + $output []= $venta; + } + } + return $output; } protected function startOfYear(DateTimeInterface $up_to) { @@ -119,32 +133,29 @@ class Resumen }); } - protected function formatValue(float $value) - { - return number_format($value, 0, ',', '.'); - } protected function buildLibro(Venta $venta, DateTimeInterface $up_to) { $pagos = $this->getPagos($venta, $up_to); - $output = [";Cuenta: Anticipos Dpto {$venta->unidad()->descripcion}"]; + $output = ['', "Cuenta: Anticipos Dpto {$venta->unidad()->descripcion}"]; $debe = 0; $haber = 0; foreach ($pagos as $pago) { if ($pago->valor > 0) { - $output []= implode($this->separator, ['', $pago->fecha->format('d/m/Y'), '', $this->formatValue($pago->valor)]); + $output []= ['', $pago->fecha->format('d/m/Y'), '', $pago->valor]; $debe += $pago->valor; } else { - $output []= implode($this->separator, ['', $pago->fecha->format('d/m/Y'), $this->formatValue(-$pago->valor), '']); + $output []= ['', $pago->fecha->format('d/m/Y'), -$pago->valor, '']; $haber -= $pago->valor; } } - $output []= implode($this->separator, ['', '', $haber, $debe]); + $output []= ['', '', $haber, $debe]; return $output; } public function build(int $id_proyecto, DateTimeInterface $up_to) { + ini_set('max_execution_time', 60 * 60); $ventas = $this->getVentas($id_proyecto); $output = ["Libro Mayor {$ventas[0]->proyecto()->descripcion}", '']; @@ -156,13 +167,8 @@ class Resumen $informe = new Informe(); $informe->createSpreadsheet() ->addArray($output) + ->formatColumn('C') + ->formatColumn('D') ->send($filename); - return; - - header("Content-Type: text/csv; charset=utf-8"); - header('Content-Disposition: attachment; filename="' . $filename . '"'); - header('Cache-Control: max-age=0'); - - file_put_contents('php://output', implode(PHP_EOL, $output)); } } diff --git a/app/Service/Informe/Informe.php b/app/Service/Informe/Informe.php index fac7ad3..8254a10 100644 --- a/app/Service/Informe/Informe.php +++ b/app/Service/Informe/Informe.php @@ -12,15 +12,41 @@ class Informe $this->spreadsheet = new Spreadsheet(); return $this; } + protected function formatArray(array $data) + { + $maxCols = 0; + foreach ($data as $row) { + if (!is_array($row)) { + continue; + } + if (count($row) > $maxCols) { + $maxCols = count($row); + } + } + foreach ($data as &$row) { + if (!is_array($row)) { + $row = [$row]; + } + if (count($row) < $maxCols) { + $row = array_merge($row, array_fill(0, $maxCols - count($row), '')); + } + } + return $data; + } public function addArray(array $data, string $start = 'A1') { - $this->spreadsheet->getActiveSheet()->fromArray($data, null, $start); + $this->spreadsheet->getActiveSheet()->fromArray($this->formatArray($data), null, $start); + return $this; + } + public function formatColumn(string $column) + { + $this->spreadsheet->getActiveSheet()->getStyle("{$column}:{$column}")->getNumberFormat()->setFormatCode('#,##0'); return $this; } public function send(string $filename) { header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); - header("Content-Disposition: attachment;filename='{$filename}'"); + header("Content-Disposition: attachment;filename=\"{$filename}\""); header('Cache-Control: max-age=0'); $writer = IOFactory::createWriter($this->spreadsheet, 'Xlsx');