Files
oficial/app/Service/Informe/Contabilidad/Resumen.php

182 lines
6.2 KiB
PHP
Raw Normal View History

2022-03-07 16:36:21 -03:00
<?php
namespace App\Service\Informe\Contabilidad;
use DateTimeInterface;
use DateTimeImmutable;
2022-03-07 21:23:21 -03:00
use DateInterval;
2022-03-07 22:21:42 -03:00
use App\Service\Informe\Informe;
2022-03-07 16:36:21 -03:00
use Incoviba\old\Proyecto\Proyecto;
use Incoviba\old\Venta\Venta;
use Incoviba\old\Venta\Pago;
/**
* Resumen para revisar el libro mayor de un proyecto para cierto mes (sumando el año anterior)
* Obtiene lo pagado para cada departamento
* - anticipo (cuotas)
* - reajuste
* - abonos
* - credito
* - subsidio
* - bono
* - promociones
*/
class Resumen
{
protected string $separator;
public function __construct(string $separator = ';')
{
$this->separator = $separator;
}
protected function getVentas($id_proyecto)
{
$proyecto = model(Proyecto::class)->findOne($id_proyecto);
2022-03-07 22:45:56 -03:00
$ventas = $proyecto->ventas();
$output = [];
foreach ($ventas as $venta) {
2022-03-07 22:50:17 -03:00
$found = false;
foreach ($output as $v) {
if ($v->unidad()->descripcion == $venta->unidad()->descripcion) {
$found = true;
break;
}
}
if (!$found) {
2022-03-07 22:45:56 -03:00
$output []= $venta;
}
}
return $output;
2022-03-07 16:36:21 -03:00
}
protected function startOfYear(DateTimeInterface $up_to)
{
2022-03-07 21:23:21 -03:00
return new DateTimeImmutable((clone $up_to)->sub(new DateInterval('P1Y'))->format('31-12-Y'));
2022-03-07 16:36:21 -03:00
}
protected function defaultValueDate(DateTimeInterface $up_to)
{
return (object) ['fecha' => $this->startOfYear($up_to), 'valor' => 0];
}
protected function extractValueDate(Pago $pago)
{
return (object) ['fecha' => $pago->fecha(), 'valor' => $pago->valor];
}
protected function getAnticipos(Venta $venta, DateTimeInterface $up_to)
{
2022-03-07 21:20:27 -03:00
if ($venta->pie == 0) {
return [$this->defaultValueDate($up_to)];
}
2022-03-07 16:36:21 -03:00
$cuotas = $venta->pie()->cuotas('fecha', $up_to);
$ly = $this->startOfYear($up_to);
$older = array_reduce($cuotas, function($sum, $item) use ($ly) {
if ($item->pago()->estado()->tipo()->active != 1) {
return $sum;
}
if ($item->pago()->fecha() >= $ly) {
return $sum;
}
return $sum + $item->pago()->valor;
});
2022-03-07 21:20:27 -03:00
$current = array_filter($cuotas, function($item) use ($ly) {
2022-03-07 16:36:21 -03:00
return $item->pago()->fecha() >= $ly;
2022-03-07 21:20:27 -03:00
});
foreach ($current as &$item) {
$item = $this->extractValueDate($item->pago());
}
2022-03-07 16:36:21 -03:00
return array_merge([(object) ['fecha' => $ly, 'valor' => $older]], $current);
}
protected function getReajuste(Venta $venta, DateTimeInterface $up_to)
{
2022-03-07 21:20:27 -03:00
if ($venta->pie == 0 or $venta->pie()->reajuste == null) {
2022-03-07 16:36:21 -03:00
return $this->defaultValueDate($up_to);
}
return $this->extractValueDate($venta->pie()->reajuste());
}
protected function getAbono(Venta $venta, DateTimeInterface $up_to)
{
2022-03-07 21:20:27 -03:00
if ($venta->escritura == 0) {
2022-03-07 16:36:21 -03:00
return $this->defaultValueDate($up_to);
}
return $this->extractValueDate($venta->escritura()->pago());
}
protected function getBono(Venta $venta, DateTimeInterface $up_to)
{
2022-03-07 21:20:27 -03:00
if ($venta->bono_pie == 0) {
2022-03-07 16:36:21 -03:00
return $this->defaultValueDate($up_to);
}
return $this->extractValueDate($venta->bonoPie()->pago());
}
protected function getCredito(Venta $venta, DateTimeInterface $up_to)
{
2022-03-07 21:20:27 -03:00
if ($venta->credito == 0) {
return [$this->defaultValueDate($up_to), $this->defaultValueDate($up_to)];
2022-03-07 16:36:21 -03:00
}
return $this->extractValueDate($venta->credito()->pago());
}
protected function getSubsidio(Venta $venta, DateTimeInterface $up_to)
{
2022-03-07 21:20:27 -03:00
if ($venta->subsidio == 0) {
2022-03-07 21:30:59 -03:00
return [$this->defaultValueDate($up_to), $this->defaultValueDate($up_to)];
2022-03-07 16:36:21 -03:00
}
return [$this->extractValueDate($venta->subsidio()->pago()), $this->extractValueDate($venta->subsidio()->subsidio())];
}
protected function getPagos(Venta $venta, DateTimeInterface $up_to)
{
$pagos = [];
$pagos = array_merge($pagos, $this->getAnticipos($venta, $up_to));
$pagos []= $this->getReajuste($venta, $up_to);
$pagos []= $this->getAbono($venta, $up_to);
$pagos []= $this->getBono($venta, $up_to);
$pagos []= $this->getCredito($venta, $up_to);
$pagos = array_merge($pagos, $this->getSubsidio($venta, $up_to));
return array_filter($pagos, function($item) {
return $item->valor != 0;
});
}
protected function buildLibro(Venta $venta, DateTimeInterface $up_to)
{
$pagos = $this->getPagos($venta, $up_to);
2022-03-07 22:50:17 -03:00
$output = ['', "Cuenta: Anticipos Dpto {$venta->unidad()->descripcion}"];
2022-03-07 16:36:21 -03:00
$debe = 0;
$haber = 0;
foreach ($pagos as $pago) {
if ($pago->valor > 0) {
2022-03-07 22:38:29 -03:00
$output []= ['', $pago->fecha->format('d/m/Y'), '', $pago->valor];
2022-03-07 16:36:21 -03:00
$debe += $pago->valor;
}
else {
2022-03-07 22:38:29 -03:00
$output []= ['', $pago->fecha->format('d/m/Y'), -$pago->valor, ''];
2022-03-07 16:36:21 -03:00
$haber -= $pago->valor;
}
}
2022-03-07 22:25:03 -03:00
$output []= ['', '', $haber, $debe];
2022-03-07 16:36:21 -03:00
return $output;
}
public function build(int $id_proyecto, DateTimeInterface $up_to)
{
2022-03-07 22:59:13 -03:00
ini_set('max_execution_time', 60 * 60);
2022-03-07 16:36:21 -03:00
$ventas = $this->getVentas($id_proyecto);
2022-03-07 21:44:17 -03:00
$output = ["Libro Mayor {$ventas[0]->proyecto()->descripcion}", ''];
2022-03-07 16:36:21 -03:00
foreach ($ventas as $venta) {
2022-03-07 21:44:17 -03:00
$output = array_merge($output, $this->buildLibro($venta, $up_to), [''], ['']);
2022-03-07 16:36:21 -03:00
}
2022-03-07 22:21:42 -03:00
$filename = "Contabilidad - Resumen - {$ventas[0]->proyecto()->descripcion} - {$up_to->format('Y-m-d')}.xlsx";
$informe = new Informe();
$informe->createSpreadsheet()
->addArray($output)
2022-03-07 22:38:29 -03:00
->formatColumn('C')
->formatColumn('D')
2022-03-07 22:21:42 -03:00
->send($filename);
return;
2022-03-07 16:36:21 -03:00
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));
}
}