Desistir venta
This commit is contained in:
@ -14,6 +14,7 @@ class Venta
|
||||
protected Repository\Venta\TipoEstadoVenta $tipoEstadoVentaRepository,
|
||||
protected Repository\Venta\Credito $creditoRepository,
|
||||
protected Repository\Venta\Escritura $escrituraRepository,
|
||||
protected Repository\Venta\Pago $pagoRepository,
|
||||
protected Venta\Propietario $propietarioService,
|
||||
protected Venta\Propiedad $propiedadService,
|
||||
protected Venta\Pie $pieService,
|
||||
@ -108,63 +109,6 @@ class Venta
|
||||
|
||||
return $venta;
|
||||
}
|
||||
public function escriturar(Model\Venta $venta, array $data): bool
|
||||
{
|
||||
if (in_array($venta->currentEstado()->tipoEstadoVenta->descripcion, ['escriturando', 'firmado por inmobiliaria', 'archivado'])) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
$tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('escriturando');
|
||||
if (isset($data['valor_reajuste']) and $data['valor_reajuste'] !== '') {
|
||||
$fecha = new DateTimeImmutable($data['fecha_reajuste']);
|
||||
$reajusteData = [
|
||||
'valor' => $data['valor_reajuste'],
|
||||
'fecha' => $fecha->format('Y-m-d')
|
||||
];
|
||||
$pie = $venta->formaPago()->pie;
|
||||
$this->pieService->reajustar($pie, $reajusteData);
|
||||
}
|
||||
if (isset($data['valor_pago_pesos']) and $data['valor_pago_pesos'] !== '') {
|
||||
$fecha = new DateTimeImmutable($data['fecha_pago']);
|
||||
$uf = $this->moneyService->getUF($fecha);
|
||||
$valor = $data['valor_pago_ufs'] !== '' ? $data['valor_pago_ufs'] * $uf : $data['valor_pago_pesos'];
|
||||
$escrituraData = [
|
||||
'valor' => $valor,
|
||||
'fecha' => $fecha->format('Y-m-d'),
|
||||
'uf' => $uf
|
||||
];
|
||||
$escritura = $this->escrituraRepository->create($escrituraData);
|
||||
$escritura = $this->escrituraRepository->save($escritura);
|
||||
$this->ventaRepository->edit($venta, ['escritura' => $escritura->id]);
|
||||
}
|
||||
if (isset($data['banco_credito']) and $data['banco_credito'] !== '') {
|
||||
$this->creditoRepository->edit($venta->formaPago()->credito, ['banco' => $data['banco_credito']]);
|
||||
}
|
||||
$fecha = new DateTimeImmutable($data['fecha']);
|
||||
$uf = $this->moneyService->getUF($fecha);
|
||||
if (isset($data['valor_subsidio']) and $data['valor_subsidio'] !== '') {
|
||||
$subsidioData = [
|
||||
'fecha_venta' => $fecha->format('Y-m-d'),
|
||||
'ahorro' => $data['valor_ahorro'],
|
||||
'subsidio' => $data['valor_subsidio'],
|
||||
'uf' => $uf
|
||||
];
|
||||
$subsidio = $this->addSubsidio($subsidioData);
|
||||
$this->ventaRepository->edit($venta, ['subsidio' => $subsidio->id]);
|
||||
}
|
||||
$estadoData = [
|
||||
'venta' => $venta->id,
|
||||
'estado' => $tipoEstado->id,
|
||||
'fecha' => $fecha->format('Y-m-d')
|
||||
];
|
||||
$estado = $this->estadoVentaRepository->create($estadoData);
|
||||
$this->estadoVentaRepository->save($estado);
|
||||
return true;
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function addPropietario(array $data): Model\Venta\Propietario
|
||||
{
|
||||
if (isset($data['natural_uno'])) {
|
||||
@ -344,4 +288,111 @@ class Venta
|
||||
], $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']);
|
||||
$estadoData = [
|
||||
'venta' => $venta->id,
|
||||
'estado' => $tipoEstadoVenta->id,
|
||||
'fecha' => $fecha->format('Y-m-d')
|
||||
];
|
||||
$estado = $this->estadoVentaRepository->create($estadoData);
|
||||
$this->estadoVentaRepository->save($estado);
|
||||
}
|
||||
|
||||
public function escriturar(Model\Venta $venta, array $data): bool
|
||||
{
|
||||
if (in_array($venta->currentEstado()->tipoEstadoVenta->descripcion, ['escriturando', 'firmado por inmobiliaria', 'archivado'])) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
if ($this->validarData($data, ['fecha_reajuste', 'valor_reajuste'])) {
|
||||
$this->reajustarEscritura($venta, $data);
|
||||
}
|
||||
if ($this->validarData($data, ['fecha_pago'], ['valor_pago_pesos', 'valor_pago_ufs'])) {
|
||||
$this->abonoEscritura($venta, $data);
|
||||
}
|
||||
if ($this->validarData($data, ['banco_credito'])) {
|
||||
$this->creditoRepository->edit($venta->formaPago()->credito, ['banco' => $data['banco_credito']]);
|
||||
}
|
||||
if ($this->validarData($data, ['valor_subsidio', 'valor_ahorro', 'fecha'])) {
|
||||
$this->subsidioEscritura($venta, $data);
|
||||
}
|
||||
$tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('escriturando');
|
||||
$this->addEstado($venta, $tipoEstado, ['fecha' => $data['fecha']]);
|
||||
return true;
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
protected function validarData(array $data, array $keys, array $optionals = []): bool
|
||||
{
|
||||
foreach ($keys as $key) {
|
||||
if (!isset($data[$key])) {
|
||||
return false;
|
||||
}
|
||||
if ($data[$key] === '') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
foreach ($optionals as $key) {
|
||||
if (isset($data[$key]) and $data[$key] !== '') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
protected function reajustarEscritura(Model\Venta $venta, array $data): void
|
||||
{
|
||||
$fecha = new DateTimeImmutable($data['fecha_reajuste']);
|
||||
$reajusteData = [
|
||||
'valor' => $data['valor_reajuste'],
|
||||
'fecha' => $fecha->format('Y-m-d')
|
||||
];
|
||||
$pie = $venta->formaPago()->pie;
|
||||
$this->pieService->reajustar($pie, $reajusteData);
|
||||
}
|
||||
protected function abonoEscritura(Model\Venta $venta, array $data): void
|
||||
{
|
||||
$fecha = new DateTimeImmutable($data['fecha_pago']);
|
||||
$uf = $this->moneyService->getUF($fecha);
|
||||
$valor = $data['valor_pago_ufs'] !== '' ? $data['valor_pago_ufs'] * $uf : $data['valor_pago_pesos'];
|
||||
$escrituraData = [
|
||||
'valor' => $valor,
|
||||
'fecha' => $fecha->format('Y-m-d'),
|
||||
'uf' => $uf
|
||||
];
|
||||
$escritura = $this->escrituraRepository->create($escrituraData);
|
||||
$escritura = $this->escrituraRepository->save($escritura);
|
||||
$this->ventaRepository->edit($venta, ['escritura' => $escritura->id]);
|
||||
}
|
||||
protected function subsidioEscritura(Model\Venta $venta, array $data): void
|
||||
{
|
||||
$fecha = new DateTimeImmutable($data['fecha']);
|
||||
$uf = $this->moneyService->getUF($fecha);
|
||||
$subsidioData = [
|
||||
'fecha_venta' => $fecha->format('Y-m-d'),
|
||||
'ahorro' => $data['valor_ahorro'],
|
||||
'subsidio' => $data['valor_subsidio'],
|
||||
'uf' => $uf
|
||||
];
|
||||
$subsidio = $this->addSubsidio($subsidioData);
|
||||
$this->ventaRepository->edit($venta, ['subsidio' => $subsidio->id]);
|
||||
}
|
||||
|
||||
public function desistir(Model\Venta $venta, array $data): bool
|
||||
{
|
||||
try {
|
||||
if ($this->validarData($data, ['fecha', 'devolucion'])) {
|
||||
$pago = $this->pagoRepository->create(['fecha' => $data['fecha'], 'valor' => $data['devolucion']]);
|
||||
$pago = $this->pagoRepository->save($pago);
|
||||
$this->ventaRepository->edit($venta, ['resciliacion' => $pago->id]);
|
||||
}
|
||||
$tipoEstado = $this->tipoEstadoVentaRepository->fetchByDescripcion('desistida');
|
||||
$this->addEstado($venta, $tipoEstado, ['fecha' => $data['fecha']]);
|
||||
return true;
|
||||
} catch (Implement\Exception\EmptyResult) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user