pie = $this->pieService->getByVenta($venta_id); } catch (Read) {} try { $formaPago->bonoPie = $this->bonoPieService->getByVenta($venta_id); } catch (Read) {} try { $formaPago->credito = $this->creditoService->getByVenta($venta_id); } catch (Read) {} try { $formaPago->escritura = $this->escrituraRepository->fetchByVenta($venta_id); } catch (Implement\Exception\EmptyResult) {} try { $formaPago->subsidio = $this->subsidioService->getByVenta($venta_id); } catch (Read) {} try { $formaPago->devolucion = $this->pagoService->getDevolucionByVenta($venta_id); } catch (Read) {} try { $formaPago->cuotasAbono = $this->cuotaRepository->fetchByVenta($venta_id); } catch (Implement\Exception\EmptyResult) {} return $formaPago; } public function add(array $data): Model\Venta\FormaPago { $fields = [ 'pie', 'subsidio', 'credito', 'bono_pie' ]; $forma_pago = new Model\Venta\FormaPago(); foreach ($fields as $name) { if (isset($data["has_{$name}"])) { try { $method = 'add' . str_replace(' ', '', ucwords(str_replace('_', ' ', $name))); $obj = $this->{$method}($data); $forma_pago->{$name} = $obj; } catch (Error $error) { $this->logger->critical($error); } } } 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->valorService->clean($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); $mapped_data['ahorro'] = $this->valorService->clean($mapped_data['ahorro']); $mapped_data['subsidio'] = $this->valorService->clean($mapped_data['subsidio']); 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); $mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']); 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); $mapped_data['valor'] = $this->valorService->clean($mapped_data['valor']); return $this->bonoPieService->add($mapped_data); } }