From 26bac169de8b7cd68d9b2754b7b1cf6ff9d6bf58 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 7 Mar 2022 22:25:03 -0300 Subject: [PATCH 01/10] FIX: format --- app/Service/Informe/Contabilidad/Resumen.php | 8 ++++---- app/Service/Informe/Informe.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/Service/Informe/Contabilidad/Resumen.php b/app/Service/Informe/Contabilidad/Resumen.php index 85f53f8..0d39df0 100644 --- a/app/Service/Informe/Contabilidad/Resumen.php +++ b/app/Service/Informe/Contabilidad/Resumen.php @@ -126,20 +126,20 @@ class Resumen 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'), '', $this->formatValue($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'), $this->formatValue(-$pago->valor), '']; $haber -= $pago->valor; } } - $output []= implode($this->separator, ['', '', $haber, $debe]); + $output []= ['', '', $haber, $debe]; return $output; } diff --git a/app/Service/Informe/Informe.php b/app/Service/Informe/Informe.php index fac7ad3..25660b1 100644 --- a/app/Service/Informe/Informe.php +++ b/app/Service/Informe/Informe.php @@ -20,7 +20,7 @@ class Informe 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'); From e8aa00e12159a116be186062703e02bbe4e7b370 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 7 Mar 2022 22:31:07 -0300 Subject: [PATCH 02/10] FIX: columns --- app/Service/Informe/Informe.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/Service/Informe/Informe.php b/app/Service/Informe/Informe.php index 25660b1..ea9ecca 100644 --- a/app/Service/Informe/Informe.php +++ b/app/Service/Informe/Informe.php @@ -12,9 +12,24 @@ class Informe $this->spreadsheet = new Spreadsheet(); return $this; } + protected function formatArray(array $data) + { + $maxCols = 0; + foreach ($data as $row) { + if (count($row) > $maxCols) { + $maxCols = count($row); + } + } + foreach ($data as &$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 send(string $filename) From fbce7089a80b2c78e158ef2fe2f2fac561f134dd Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 7 Mar 2022 22:34:04 -0300 Subject: [PATCH 03/10] FIX: non array rows --- app/Service/Informe/Informe.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/Service/Informe/Informe.php b/app/Service/Informe/Informe.php index ea9ecca..96af23c 100644 --- a/app/Service/Informe/Informe.php +++ b/app/Service/Informe/Informe.php @@ -16,11 +16,17 @@ class Informe { $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), '')); } From 517276da1d76b66f462f53ea42062f68eefcd5e3 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 7 Mar 2022 22:38:29 -0300 Subject: [PATCH 04/10] FIX: format in excel --- app/Service/Informe/Contabilidad/Resumen.php | 10 ++++------ app/Service/Informe/Informe.php | 5 +++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/Service/Informe/Contabilidad/Resumen.php b/app/Service/Informe/Contabilidad/Resumen.php index 0d39df0..3503e1f 100644 --- a/app/Service/Informe/Contabilidad/Resumen.php +++ b/app/Service/Informe/Contabilidad/Resumen.php @@ -119,10 +119,6 @@ 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); @@ -131,11 +127,11 @@ class Resumen $haber = 0; foreach ($pagos as $pago) { if ($pago->valor > 0) { - $output []= ['', $pago->fecha->format('d/m/Y'), '', $this->formatValue($pago->valor)]; + $output []= ['', $pago->fecha->format('d/m/Y'), '', $pago->valor]; $debe += $pago->valor; } else { - $output []= ['', $pago->fecha->format('d/m/Y'), $this->formatValue(-$pago->valor), '']; + $output []= ['', $pago->fecha->format('d/m/Y'), -$pago->valor, '']; $haber -= $pago->valor; } } @@ -156,6 +152,8 @@ class Resumen $informe = new Informe(); $informe->createSpreadsheet() ->addArray($output) + ->formatColumn('C') + ->formatColumn('D') ->send($filename); return; diff --git a/app/Service/Informe/Informe.php b/app/Service/Informe/Informe.php index 96af23c..656ebf4 100644 --- a/app/Service/Informe/Informe.php +++ b/app/Service/Informe/Informe.php @@ -38,6 +38,11 @@ class Informe $this->spreadsheet->getActiveSheet()->fromArray($this->formatArray($data), null, $start); return $this; } + public function formatColumn(string $column) + { + $this->spreadsheet->getActiveSheet()->getStyle("{$column}:{$column}")->setFormatCode('#,##0'); + return $this; + } public function send(string $filename) { header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); From 1547b244121bde6949a7cafa6a2976afc10cf2f5 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 7 Mar 2022 22:40:55 -0300 Subject: [PATCH 05/10] FIX: getNumberFormat --- app/Service/Informe/Informe.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Service/Informe/Informe.php b/app/Service/Informe/Informe.php index 656ebf4..8254a10 100644 --- a/app/Service/Informe/Informe.php +++ b/app/Service/Informe/Informe.php @@ -40,7 +40,7 @@ class Informe } public function formatColumn(string $column) { - $this->spreadsheet->getActiveSheet()->getStyle("{$column}:{$column}")->setFormatCode('#,##0'); + $this->spreadsheet->getActiveSheet()->getStyle("{$column}:{$column}")->getNumberFormat()->setFormatCode('#,##0'); return $this; } public function send(string $filename) From 16fb8f0a96b75941c11577ab287c85e957602cd4 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 7 Mar 2022 22:43:34 -0300 Subject: [PATCH 06/10] Filter duplicates --- app/Service/Informe/Contabilidad/Resumen.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Service/Informe/Contabilidad/Resumen.php b/app/Service/Informe/Contabilidad/Resumen.php index 3503e1f..4a80b78 100644 --- a/app/Service/Informe/Contabilidad/Resumen.php +++ b/app/Service/Informe/Contabilidad/Resumen.php @@ -32,7 +32,7 @@ class Resumen protected function getVentas($id_proyecto) { $proyecto = model(Proyecto::class)->findOne($id_proyecto); - return $proyecto->ventas(); + return array_unique($proyecto->ventas()); } protected function startOfYear(DateTimeInterface $up_to) { @@ -122,7 +122,7 @@ class Resumen 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) { From e51d87f83fd28387798582cceb125c6a1c764def Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 7 Mar 2022 22:45:56 -0300 Subject: [PATCH 07/10] FIX: filter --- app/Service/Informe/Contabilidad/Resumen.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/Service/Informe/Contabilidad/Resumen.php b/app/Service/Informe/Contabilidad/Resumen.php index 4a80b78..729bdc5 100644 --- a/app/Service/Informe/Contabilidad/Resumen.php +++ b/app/Service/Informe/Contabilidad/Resumen.php @@ -32,7 +32,14 @@ class Resumen protected function getVentas($id_proyecto) { $proyecto = model(Proyecto::class)->findOne($id_proyecto); - return array_unique($proyecto->ventas()); + $ventas = $proyecto->ventas(); + $output = []; + foreach ($ventas as $venta) { + if (!in_array($venta, $output)) { + $output []= $venta; + } + } + return $output; } protected function startOfYear(DateTimeInterface $up_to) { From 33417a6acd48f9fe94693df2e5faf0cece564364 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 7 Mar 2022 22:50:17 -0300 Subject: [PATCH 08/10] FIX: filter --- app/Service/Informe/Contabilidad/Resumen.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/Service/Informe/Contabilidad/Resumen.php b/app/Service/Informe/Contabilidad/Resumen.php index 729bdc5..0562a30 100644 --- a/app/Service/Informe/Contabilidad/Resumen.php +++ b/app/Service/Informe/Contabilidad/Resumen.php @@ -35,7 +35,14 @@ class Resumen $ventas = $proyecto->ventas(); $output = []; foreach ($ventas as $venta) { - if (!in_array($venta, $output)) { + $found = false; + foreach ($output as $v) { + if ($v->unidad()->descripcion == $venta->unidad()->descripcion) { + $found = true; + break; + } + } + if (!$found) { $output []= $venta; } } @@ -129,7 +136,7 @@ class Resumen 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) { From 7d3e75c3163c59f36fb2f21036d6c4e20bc4a739 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 7 Mar 2022 22:59:13 -0300 Subject: [PATCH 09/10] FIX: max execution time --- app/Service/Informe/Contabilidad/Resumen.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Service/Informe/Contabilidad/Resumen.php b/app/Service/Informe/Contabilidad/Resumen.php index 0562a30..a0b1167 100644 --- a/app/Service/Informe/Contabilidad/Resumen.php +++ b/app/Service/Informe/Contabilidad/Resumen.php @@ -155,6 +155,7 @@ class Resumen 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}", '']; From b8babf2906ff1dbbb738a470c251b4757b8bd6c8 Mon Sep 17 00:00:00 2001 From: Juan Pablo Vial Date: Mon, 7 Mar 2022 23:49:26 -0300 Subject: [PATCH 10/10] Clean up --- app/Controller/Informes.php | 103 +------------------ app/Service/Informe/Contabilidad/Resumen.php | 7 -- 2 files changed, 1 insertion(+), 109 deletions(-) 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 a0b1167..66f9e10 100644 --- a/app/Service/Informe/Contabilidad/Resumen.php +++ b/app/Service/Informe/Contabilidad/Resumen.php @@ -170,12 +170,5 @@ class Resumen ->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)); } }