FIX: Venta

This commit is contained in:
Juan Pablo Vial
2024-03-13 22:43:37 -03:00
parent 57f9169cc7
commit 9c335fd350
8 changed files with 35 additions and 106 deletions

View File

@ -50,7 +50,7 @@
</td> </td>
<td class="right aligned"> <td class="right aligned">
@if ($unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'departamento') @if ($unidad->proyectoTipoUnidad->tipoUnidad->descripcion === 'departamento')
{{$format->number(($unidad->valor ?? $precio) / $unidad->proyectoTipoUnidad->vendible(), 2)}} UF/ {{$format->number((($unidad->valor === null or $unidad->valor === 0.0) ? $precio : $unidad->valor) / $unidad->proyectoTipoUnidad->vendible(), 2)}} UF/
@endif @endif
</td> </td>
<td class="center aligned"> <td class="center aligned">

View File

@ -3,9 +3,6 @@ namespace Incoviba\Model;
use DateTimeInterface; use DateTimeInterface;
use Incoviba\Common\Ideal; use Incoviba\Common\Ideal;
use Incoviba\Controller\Ventas;
use Incoviba\Model\Venta\FormaPago;
use Incoviba\Model\Venta\Pago;
class Venta extends Ideal\Model class Venta extends Ideal\Model
{ {
@ -18,7 +15,7 @@ class Venta extends Ideal\Model
public bool $relacionado; public bool $relacionado;
protected ?Venta\Entrega $entrega; protected ?Venta\Entrega $entrega;
public float $uf; public float $uf;
protected ?Pago $resciliacion; protected ?Venta\Pago $resciliacion;
public ?array $estados; public ?array $estados;
public ?Venta\EstadoVenta $currentEstado; public ?Venta\EstadoVenta $currentEstado;
@ -44,6 +41,11 @@ class Venta extends Ideal\Model
} }
return $this->formaPago; return $this->formaPago;
} }
public function setFormaPago(Venta\FormaPago $formaPago): Venta
{
$this->formaPago = $formaPago;
return $this;
}
public function entrega(): ?Venta\Entrega public function entrega(): ?Venta\Entrega
{ {
if (!isset($this->entrega)) { if (!isset($this->entrega)) {
@ -94,9 +96,9 @@ class Venta extends Ideal\Model
} }
return $this->valor_util; 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); return $valor - $this->formaPago()->total($moneda);
} }

View File

@ -6,8 +6,7 @@ use Incoviba\Model;
class Propiedad extends Ideal\Model class Propiedad extends Ideal\Model
{ {
public array $unidades; public array $unidades = [];
protected array $departamentos; protected array $departamentos;
public function departamentos(): array public function departamentos(): array
{ {

View File

@ -132,9 +132,9 @@ class Venta extends Ideal\Repository
['propietario', 'propiedad', 'pie', 'bono_pie', 'credito', 'escritura', 'subsidio', 'escriturado', ['propietario', 'propiedad', 'pie', 'bono_pie', 'credito', 'escritura', 'subsidio', 'escriturado',
'entrega', 'entregado', 'fecha', 'valor_uf', 'estado', 'fecha_ingreso', 'avalchile', 'agente', 'uf', 'entrega', 'entregado', 'fecha', 'valor_uf', 'estado', 'fecha_ingreso', 'avalchile', 'agente', 'uf',
'relacionado', 'promocion', 'resciliacion', 'devolucion'], 'relacionado', 'promocion', 'resciliacion', 'devolucion'],
[$model->propietario()->rut, $model->propiedad()->id, $model->formaPago()->pie?->id, $model->formaPago()->bonoPie?->id, [$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()?->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->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->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] null, null, $model->uf, $model->relacionado ? 1 : 0, null, null, null]
); );

View File

@ -18,22 +18,23 @@ class Propiedad extends Ideal\Repository
public function create(?array $data = null): Model\Venta\Propiedad public function create(?array $data = null): Model\Venta\Propiedad
{ {
$map = (new Implement\Repository\MapperParser()) $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')); ->register('estado', new Implement\Repository\Mapper\Boolean('estado'));
return $this->parseData(new Model\Venta\Propiedad(), $data, $map); 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 public function save(Define\Model $model): Model\Venta\Propiedad
{ {
$model->id = $this->saveNew( $model->id = $this->saveNew(
['unidad_principal', 'estacionamientos', 'bodegas', 'estado'], ['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->estacionamientos())),
implode(',', array_map(function(Model\Venta\Unidad $unidad) {return $unidad->id;}, $model->bodegas())), implode(',', array_map(function(Model\Venta\Unidad $unidad) {return $unidad->id;}, $model->bodegas())),
1] 1]

View File

@ -104,7 +104,7 @@ class Venta extends Service
$data['uf'] = $this->moneyService->getUF($fecha); $data['uf'] = $this->moneyService->getUF($fecha);
$propietario = $this->addPropietario($data); $propietario = $this->addPropietario($data);
$propiedad = $this->addPropiedad($data); $propiedad = $this->addPropiedad($data);
$forma_pago = $this->addFormaPago($data); $formaPago = $this->addFormaPago($data);
$venta_data = [ $venta_data = [
'propietario' => $propietario->rut, 'propietario' => $propietario->rut,
'propiedad' => $propiedad->id, 'propiedad' => $propiedad->id,
@ -116,14 +116,15 @@ class Venta extends Service
$map = ['pie', 'subsidio', 'credito', 'bono_pie']; $map = ['pie', 'subsidio', 'credito', 'bono_pie'];
foreach ($map as $field) { foreach ($map as $field) {
$name = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $field)))); $name = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $field))));
if (isset($forma_pago->{$name})) { if (isset($formaPago->{$name})) {
$venta_data[$field] = $forma_pago->{$name}->id; $venta_data[$field] = $formaPago->{$name}->id;
} }
} }
try { try {
return $this->ventaRepository->fetchByPropietarioAndPropiedad($propietario->rut, $propiedad->id); return $this->ventaRepository->fetchByPropietarioAndPropiedad($propietario->rut, $propiedad->id);
} catch (Implement\Exception\EmptyResult) { } catch (Implement\Exception\EmptyResult) {
$venta = $this->ventaRepository->create($venta_data); $venta = $this->ventaRepository->create($venta_data);
$venta->setFormaPago($formaPago);
$venta = $this->ventaRepository->save($venta); $venta = $this->ventaRepository->save($venta);
$tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('vigente'); $tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('vigente');
@ -231,7 +232,7 @@ class Venta extends Service
protected function addPropiedad(array $data): Model\Venta\Propiedad protected function addPropiedad(array $data): Model\Venta\Propiedad
{ {
$ids = array_filter($data, function($key) { $ids = array_filter($data, function($key) {
return str_contains($key, 'unidad'); return str_starts_with($key, 'unidad');
}, ARRAY_FILTER_USE_KEY); }, ARRAY_FILTER_USE_KEY);
return $this->propiedadService->addPropiedad($ids); return $this->propiedadService->addPropiedad($ids);
@ -239,85 +240,7 @@ class Venta extends Service
protected function addFormaPago(array $data): Model\Venta\FormaPago protected function addFormaPago(array $data): Model\Venta\FormaPago
{ {
return $this->formaPagoService->add($data); 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 protected function addEstado(Model\Venta $venta, Model\Venta\TipoEstadoVenta $tipoEstadoVenta, array $data): void
{ {
$fecha = new DateTimeImmutable($data['fecha']); $fecha = new DateTimeImmutable($data['fecha']);

View File

@ -77,7 +77,6 @@ class FormaPago extends Ideal\Service
'uf' 'uf'
], 0); ], 0);
$filtered_data = array_intersect_key($data, $fields); $filtered_data = array_intersect_key($data, $fields);
$this->logger->critical(var_export($filtered_data,true));
$mapped_data = array_combine([ $mapped_data = array_combine([
'fecha', 'fecha',
'valor', 'valor',

View File

@ -3,18 +3,23 @@ namespace Incoviba\Service\Venta;
use PDO; use PDO;
use PDOException; use PDOException;
use Psr\Log\LoggerInterface;
use Incoviba\Common\Define; use Incoviba\Common\Define;
use Incoviba\Common\Ideal\Service;
use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Repository; use Incoviba\Repository;
use Incoviba\Model; use Incoviba\Model;
class Propiedad class Propiedad extends Service
{ {
public function __construct( public function __construct(
LoggerInterface $logger,
protected Repository\Venta\Propiedad $propiedadRepository, protected Repository\Venta\Propiedad $propiedadRepository,
protected Repository\Venta\Unidad $unidadRepository, protected Repository\Venta\Unidad $unidadRepository,
protected Define\Connection $connection protected Define\Connection $connection
) {} ) {
parent::__construct($logger);
}
public function addPropiedad(array $ids): Model\Venta\Propiedad public function addPropiedad(array $ids): Model\Venta\Propiedad
{ {