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

@ -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']);