Files
oficial/app/src/Repository/Venta/Propietario.php

83 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace Incoviba\Repository\Venta;
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Model;
use Incoviba\Repository;
class Propietario extends Ideal\Repository
{
public function __construct(Define\Connection $connection, protected Repository\Direccion $direccionRepository)
{
parent::__construct($connection);
$this->setTable('propietario');
}
protected function getKey(): string
{
return 'rut';
}
public function create(?array $data = null): Define\Model
{
2024-02-13 17:59:46 -03:00
$map = (new Implement\Repository\MapperParser(['rut', 'dv', 'nombres']))
->register('apellido_paterno', (new Implement\Repository\Mapper())
->setProperty('apellidos')
->setFunction(function($data) {
$arr = [
'paterno' => $data['apellido_paterno']
];
if ($data['apellido_materno'] !== '') {
$arr['materno'] = $data['apellido_materno'];
}
return $arr;
2024-12-03 19:08:43 -03:00
})
->setDefault([]))
->register('direccion', (new Implement\Repository\Mapper())
->setProperty('datos')
->setFunction(function($data) {
$datos = new Model\Venta\Datos();
if ($data['direccion'] !== null and $data['direccion'] !== 0) {
$datos->direccion = $this->direccionRepository->fetchById($data['direccion']);
}
return $datos;
}))
->register('representante', (new Implement\Repository\Mapper())
->setFunction(function($data) {
if ($data['representante'] === null or $data['representante'] === 0) {
return null;
}
return $this->fetchById($data['representante']);
}))
2023-09-13 18:51:46 -03:00
->register('otro', (new Implement\Repository\Mapper())
->setFunction(function($data) {
2023-09-28 21:05:16 -03:00
if ($data['otro'] === null) {
2023-09-13 18:51:46 -03:00
return null;
}
2023-09-28 21:05:16 -03:00
return $data['otro'] !== 0;
2023-09-13 18:51:46 -03:00
})
->setDefault(null));
return $this->parseData(new Model\Venta\Propietario(), $data, $map);
}
public function save(Define\Model $model): Define\Model
{
$model->rut = $this->saveNew(
2024-02-13 17:59:46 -03:00
['rut', 'dv', 'nombres', 'apellido_paterno', 'apellido_materno', 'direccion', 'otro', 'representante'],
2024-06-18 22:41:03 -04:00
[$model->rut, $model->dv, $model->nombres, $model->apellidos['paterno'], $model->apellidos['materno'], $model->datos->direccion->id, $model->otro->rut ?? 0, $model->contacto->rut ?? 0]
);
return $model;
}
public function edit(Define\Model $model, array $new_data): Define\Model
{
2023-09-13 18:51:46 -03:00
return $this->update($model, ['dv', 'nombres', 'apellido_paterno', 'apellido_materno', 'direccion', 'otro', 'representante'], $new_data);
}
public function filterData(array $data): array
{
return array_intersect_key($data, array_flip(['rut', 'dv', 'nombres', 'apellido_paterno', 'apellido_materno', 'direccion', 'representante']));
}
}