diff --git a/app/resources/views/ventas/show/propiedad.blade.php b/app/resources/views/ventas/show/propiedad.blade.php index 5bba106..4fa104c 100644 --- a/app/resources/views/ventas/show/propiedad.blade.php +++ b/app/resources/views/ventas/show/propiedad.blade.php @@ -50,7 +50,7 @@ @if ($unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'departamento') - {{$format->number(($unidad->valor ?? $precio) / $unidad->proyectoTipoUnidad->vendible(), 2)}} UF/m² + {{$format->number((($unidad->valor === null or $unidad->valor === 0.0) ? $precio : $unidad->valor) / $unidad->proyectoTipoUnidad->vendible(), 2)}} UF/m² @endif diff --git a/app/src/Model/Venta.php b/app/src/Model/Venta.php index a49d36e..4ae2eba 100644 --- a/app/src/Model/Venta.php +++ b/app/src/Model/Venta.php @@ -3,9 +3,6 @@ namespace Incoviba\Model; use DateTimeInterface; use Incoviba\Common\Ideal; -use Incoviba\Controller\Ventas; -use Incoviba\Model\Venta\FormaPago; -use Incoviba\Model\Venta\Pago; class Venta extends Ideal\Model { @@ -18,7 +15,7 @@ class Venta extends Ideal\Model public bool $relacionado; protected ?Venta\Entrega $entrega; public float $uf; - protected ?Pago $resciliacion; + protected ?Venta\Pago $resciliacion; public ?array $estados; public ?Venta\EstadoVenta $currentEstado; @@ -44,6 +41,11 @@ class Venta extends Ideal\Model } return $this->formaPago; } + public function setFormaPago(Venta\FormaPago $formaPago): Venta + { + $this->formaPago = $formaPago; + return $this; + } public function entrega(): ?Venta\Entrega { if (!isset($this->entrega)) { @@ -94,9 +96,9 @@ class Venta extends Ideal\Model } return $this->valor_util; } - public function saldo(string $moneda = Pago::UF): float + public function saldo(string $moneda = Venta\Pago::UF): float { - $valor = $this->valor * (($moneda === Pago::UF) ? 1 : $this->uf); + $valor = $this->valor * (($moneda === Venta\Pago::UF) ? 1 : $this->uf); return $valor - $this->formaPago()->total($moneda); } diff --git a/app/src/Model/Venta/Propiedad.php b/app/src/Model/Venta/Propiedad.php index 47e8094..829cb54 100644 --- a/app/src/Model/Venta/Propiedad.php +++ b/app/src/Model/Venta/Propiedad.php @@ -6,8 +6,7 @@ use Incoviba\Model; class Propiedad extends Ideal\Model { - public array $unidades; - + public array $unidades = []; protected array $departamentos; public function departamentos(): array { diff --git a/app/src/Repository/Venta.php b/app/src/Repository/Venta.php index a6a7a0f..ef392f1 100644 --- a/app/src/Repository/Venta.php +++ b/app/src/Repository/Venta.php @@ -132,9 +132,9 @@ class Venta extends Ideal\Repository ['propietario', 'propiedad', 'pie', 'bono_pie', 'credito', 'escritura', 'subsidio', 'escriturado', 'entrega', 'entregado', 'fecha', 'valor_uf', 'estado', 'fecha_ingreso', 'avalchile', 'agente', 'uf', 'relacionado', 'promocion', 'resciliacion', 'devolucion'], - [$model->propietario()->rut, $model->propiedad()->id, $model->formaPago()->pie?->id, $model->formaPago()->bonoPie?->id, - $model->formaPago()->credito?->id, $model->formaPago()->escritura?->id, $model->formaPago()->subsidio?->id, - $model->formaPago()->escritura !== null ? $model->formaPago()->escritura->pago->fecha->format('Y-m-d') : null, + [$model->propietario()->rut, $model->propiedad()->id, $model->formaPago()?->pie?->id, $model->formaPago()?->bonoPie?->id, + $model->formaPago()?->credito?->id, $model->formaPago()?->escritura?->id, $model->formaPago()?->subsidio?->id, + $model->formaPago()?->escritura !== null ? $model->formaPago()?->escritura->pago->fecha->format('Y-m-d') : null, null, null, $model->fecha->format('Y-m-d'), $model->valor, 1, $model->fechaIngreso->format('Y-m-d'), null, null, $model->uf, $model->relacionado ? 1 : 0, null, null, null] ); diff --git a/app/src/Repository/Venta/Propiedad.php b/app/src/Repository/Venta/Propiedad.php index 1549295..7b74fcc 100644 --- a/app/src/Repository/Venta/Propiedad.php +++ b/app/src/Repository/Venta/Propiedad.php @@ -18,22 +18,23 @@ class Propiedad extends Ideal\Repository public function create(?array $data = null): Model\Venta\Propiedad { $map = (new Implement\Repository\MapperParser()) - ->register('unidad_principal', (new Implement\Repository\Mapper()) - ->setProperty('unidades') - ->setFunction(function($data) { - if (isset($data['id'])) { - return $this->unidadService->getByPropiedad($data['id']); - } - return [$this->unidadService->getById($data['unidad_principal'])]; - })) ->register('estado', new Implement\Repository\Mapper\Boolean('estado')); return $this->parseData(new Model\Venta\Propiedad(), $data, $map); } + public function load(array $data_row): Define\Model + { + $propiedad = parent::load($data_row); + if (isset($propiedad->id)) { + $propiedad->unidades = $this->unidadService->getByPropiedad($propiedad->id); + } + return $propiedad; + } + public function save(Define\Model $model): Model\Venta\Propiedad { $model->id = $this->saveNew( ['unidad_principal', 'estacionamientos', 'bodegas', 'estado'], - [$model->departamentos()[0]->id, + [null, implode(',', array_map(function(Model\Venta\Unidad $unidad) {return $unidad->id;}, $model->estacionamientos())), implode(',', array_map(function(Model\Venta\Unidad $unidad) {return $unidad->id;}, $model->bodegas())), 1] diff --git a/app/src/Service/Venta.php b/app/src/Service/Venta.php index 34bee46..ec8500b 100644 --- a/app/src/Service/Venta.php +++ b/app/src/Service/Venta.php @@ -104,7 +104,7 @@ class Venta extends Service $data['uf'] = $this->moneyService->getUF($fecha); $propietario = $this->addPropietario($data); $propiedad = $this->addPropiedad($data); - $forma_pago = $this->addFormaPago($data); + $formaPago = $this->addFormaPago($data); $venta_data = [ 'propietario' => $propietario->rut, 'propiedad' => $propiedad->id, @@ -116,14 +116,15 @@ class Venta extends Service $map = ['pie', 'subsidio', 'credito', 'bono_pie']; foreach ($map as $field) { $name = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $field)))); - if (isset($forma_pago->{$name})) { - $venta_data[$field] = $forma_pago->{$name}->id; + if (isset($formaPago->{$name})) { + $venta_data[$field] = $formaPago->{$name}->id; } } try { return $this->ventaRepository->fetchByPropietarioAndPropiedad($propietario->rut, $propiedad->id); } catch (Implement\Exception\EmptyResult) { $venta = $this->ventaRepository->create($venta_data); + $venta->setFormaPago($formaPago); $venta = $this->ventaRepository->save($venta); $tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('vigente'); @@ -231,7 +232,7 @@ class Venta extends Service protected function addPropiedad(array $data): Model\Venta\Propiedad { $ids = array_filter($data, function($key) { - return str_contains($key, 'unidad'); + return str_starts_with($key, 'unidad'); }, ARRAY_FILTER_USE_KEY); return $this->propiedadService->addPropiedad($ids); @@ -239,85 +240,7 @@ class Venta extends Service protected function addFormaPago(array $data): Model\Venta\FormaPago { return $this->formaPagoService->add($data); - /*$fields = [ - 'pie', - 'subsidio', - 'credito', - 'bono_pie' - ]; - $forma_pago = new Model\Venta\FormaPago(); - foreach ($fields as $name) { - if (isset($data["has_{$name}"])) { - $method = 'add' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name))); - $obj = $this->{$method}($data); - $forma_pago->{$name} = $obj; - } - } - return $forma_pago;*/ } - /*protected function addPie(array $data): Model\Venta\Pie - { - $fields = array_fill_keys([ - 'fecha_venta', - 'pie', - 'cuotas', - 'uf' - ], 0); - $filtered_data = array_intersect_key($data, $fields); - $mapped_data = array_combine([ - 'fecha', - 'valor', - 'cuotas', - 'uf' - ], $filtered_data); - $mapped_data['valor'] = $this->cleanValue($mapped_data['valor']); - return $this->pieService->add($mapped_data); - } - protected function addSubsidio(array $data): Model\Venta\Subsidio - { - $fields = array_fill_keys([ - 'fecha_venta', - 'ahorro', - 'subsidio', - 'uf' - ], 0); - $filtered_data = array_intersect_key($data, $fields); - $mapped_data = array_combine([ - 'fecha', - 'ahorro', - 'subsidio', - 'uf' - ], $filtered_data); - return $this->subsidioService->add($mapped_data); - } - protected function addCredito(array $data): Model\Venta\Credito - { - $fields = array_fill_keys([ - 'fecha_venta', - 'credito', - 'uf' - ], 0); - $filtered_data = array_intersect_key($data, $fields); - $mapped_data = array_combine([ - 'fecha', - 'valor', - 'uf' - ], $filtered_data); - return $this->creditoService->add($mapped_data); - } - protected function addBonoPie(array $data): Model\Venta\BonoPie - { - $fields = array_fill_keys([ - 'fecha_venta', - 'bono_pie' - ], 0); - $filtered_data = array_intersect_key($data, $fields); - $mapped_data = array_combine([ - 'fecha', - 'valor' - ], $filtered_data); - return $this->bonoPieService->add($mapped_data); - }*/ protected function addEstado(Model\Venta $venta, Model\Venta\TipoEstadoVenta $tipoEstadoVenta, array $data): void { $fecha = new DateTimeImmutable($data['fecha']); diff --git a/app/src/Service/Venta/FormaPago.php b/app/src/Service/Venta/FormaPago.php index 6a113ef..f939db8 100644 --- a/app/src/Service/Venta/FormaPago.php +++ b/app/src/Service/Venta/FormaPago.php @@ -77,7 +77,6 @@ class FormaPago extends Ideal\Service 'uf' ], 0); $filtered_data = array_intersect_key($data, $fields); - $this->logger->critical(var_export($filtered_data,true)); $mapped_data = array_combine([ 'fecha', 'valor', diff --git a/app/src/Service/Venta/Propiedad.php b/app/src/Service/Venta/Propiedad.php index 58788c3..8ac1440 100644 --- a/app/src/Service/Venta/Propiedad.php +++ b/app/src/Service/Venta/Propiedad.php @@ -3,18 +3,23 @@ namespace Incoviba\Service\Venta; use PDO; use PDOException; +use Psr\Log\LoggerInterface; use Incoviba\Common\Define; +use Incoviba\Common\Ideal\Service; use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Repository; use Incoviba\Model; -class Propiedad +class Propiedad extends Service { public function __construct( + LoggerInterface $logger, protected Repository\Venta\Propiedad $propiedadRepository, protected Repository\Venta\Unidad $unidadRepository, protected Define\Connection $connection - ) {} + ) { + parent::__construct($logger); + } public function addPropiedad(array $ids): Model\Venta\Propiedad {