2024-06-18
This commit is contained in:
129
app/src/Repository/Venta/Factura.php
Normal file
129
app/src/Repository/Venta/Factura.php
Normal file
@ -0,0 +1,129 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Venta;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Common\Implement\Exception\EmptyResult;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class Factura extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Implement\Connection $connection, protected Repository\Venta $ventaRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('facturas');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Venta\Factura
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser(['index']))
|
||||
->register('venta_id', (new Implement\Repository\Mapper())
|
||||
->setProperty('venta')
|
||||
->setFunction(function($data) {
|
||||
return $this->ventaRepository->fetchById($data['venta_id']);
|
||||
}));
|
||||
$factura = $this->parseData(new Model\Venta\Factura(), $data, $map);
|
||||
$json = json_decode($data['data']);
|
||||
$factura->proporcion = $json->proporcion;
|
||||
$factura->emisorRut = $json->emisor->rut;
|
||||
$factura->emisorNombre = $json->emisor->nombre;
|
||||
$factura->emisorDireccion = $json->emisor->direccion;
|
||||
$factura->receptorRut = $json->receptor->rut;
|
||||
$factura->receptorNombre = $json->receptor->nombre;
|
||||
$factura->receptorDireccion = $json->receptor->direccion;
|
||||
$factura->receptorComuna = $json->receptor->comuna;
|
||||
$factura->fecha = new DateTimeImmutable($json->fecha);
|
||||
$factura->unidades = $json->unidades;
|
||||
$factura->detalleBase = $json->detalle->base;
|
||||
$factura->detalleTerreno = $json->detalle->terreno;
|
||||
$factura->detalleNeto = $json->detalle->neto;
|
||||
$factura->detalleIva = $json->detalle->iva;
|
||||
$factura->detalleBruto = $json->detalle->bruto;
|
||||
$factura->detalleDescuento = $json->detalle->descuento;
|
||||
$factura->detalleTotal = $json->detalle->total;
|
||||
$factura->totalNeto = $json->total->neto;
|
||||
$factura->totalExento = $json->total->exento;
|
||||
$factura->totalIva = $json->total->iva;
|
||||
$factura->totalTotal = $json->total->total;
|
||||
$factura->fechaUF = new DateTimeImmutable($json->uf->fecha);
|
||||
$factura->valorUF = $json->uf->valor;
|
||||
return $factura;
|
||||
}
|
||||
public function save(Define\Model $model): Model\Venta\Factura
|
||||
{
|
||||
$model->id = $this->saveNew([
|
||||
'venta_id',
|
||||
'index',
|
||||
'data'
|
||||
], [
|
||||
$model->venta->id,
|
||||
$model->index,
|
||||
json_encode([
|
||||
'proporcion' => $model->proporcion,
|
||||
'emisor' => [
|
||||
'rut' => $model->emisorRut,
|
||||
'nombre' => $model->emisorNombre,
|
||||
'direccion' => $model->emisorDireccion
|
||||
],
|
||||
'receptor' => [
|
||||
'rut' => $model->receptorRut,
|
||||
'nombre' => $model->receptorNombre,
|
||||
'direccion' => $model->receptorDireccion,
|
||||
'comuna' => $model->receptorComuna
|
||||
],
|
||||
'fecha' => $model->fecha->format('Y-m-d'),
|
||||
'unidades' => $model->unidades,
|
||||
'detalle' => [
|
||||
'base' => $model->detalleBase,
|
||||
'terreno' => $model->detalleTerreno,
|
||||
'neto' => $model->detalleNeto,
|
||||
'iva' => $model->detalleIva,
|
||||
'bruto' => $model->detalleBruto,
|
||||
'descuento' => $model->detalleDescuento,
|
||||
'total' => $model->detalleTotal
|
||||
],
|
||||
'total' => [
|
||||
'neto' => $model->totalNeto,
|
||||
'exento' => $model->totalExento,
|
||||
'iva' => $model->totalIva,
|
||||
'total' => $model->totalTotal
|
||||
],
|
||||
'uf' => [
|
||||
'fecha' => $model->fechaUF->format('Y-m-d'),
|
||||
'valor' => $model->valorUF
|
||||
]
|
||||
])
|
||||
]);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Venta\Factura
|
||||
{
|
||||
return $this->update($model, ['venta_id', 'index', 'data'], $new_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws EmptyResult
|
||||
*/
|
||||
public function fetchByVenta(int $venta_id): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('venta_id = :venta_id');
|
||||
return $this->fetchMany($query, ['venta_id' => $venta_id]);
|
||||
}
|
||||
/**
|
||||
* @throws EmptyResult
|
||||
*/
|
||||
public function fetchByVentaAndIndex(int $venta_id, int $index): Model\Venta\Factura
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('venta_id = :venta_id AND index = :index');
|
||||
return $this->fetchOne($query, ['venta_id' => $venta_id, 'index' => $index]);
|
||||
}
|
||||
}
|
60
app/src/Repository/Venta/Factura/Estado.php
Normal file
60
app/src/Repository/Venta/Factura/Estado.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Venta\Factura;
|
||||
|
||||
use Incoviba\Common\Define;
|
||||
use Incoviba\Common\Ideal;
|
||||
use Incoviba\Common\Implement;
|
||||
use Incoviba\Model;
|
||||
use Incoviba\Repository;
|
||||
|
||||
class Estado extends Ideal\Repository
|
||||
{
|
||||
public function __construct(Define\Connection $connection, protected Repository\Venta\Factura $facturaRepository, protected Repository\Venta\Factura\Estado\Tipo $tipoRepository)
|
||||
{
|
||||
parent::__construct($connection);
|
||||
$this->setTable('estados_facturas');
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Venta\Factura\Estado
|
||||
{
|
||||
$map = (new Implement\Repository\MapperParser())
|
||||
->register('factura_id', (new Implement\Repository\Mapper())
|
||||
->setProperty('factura')
|
||||
->setFunction(function($data) {
|
||||
return $this->facturaRepository->fetchById($data['factura_id']);
|
||||
}))
|
||||
->register('fecha', new Implement\Repository\Mapper\DateTime('fecha'))
|
||||
->register('tipo_id', (new Implement\Repository\Mapper())
|
||||
->setProperty('tipo')
|
||||
->setFunction(function($data) {
|
||||
return $this->tipoRepository->fetchById($data['tipo_id']);
|
||||
}));
|
||||
return $this->parseData(new Model\Venta\Factura\Estado(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Venta\Factura\Estado
|
||||
{
|
||||
$model->id = $this->saveNew([
|
||||
'factura_id',
|
||||
'fecha',
|
||||
'tipo_id'
|
||||
], [
|
||||
$model->factura->id,
|
||||
$model->fecha->format('Y-m-d'),
|
||||
$model->tipo->id
|
||||
]);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Venta\Factura\Estado
|
||||
{
|
||||
return $this->update($model, ['factura_id', 'fecha', 'tipo_id'], $new_data);
|
||||
}
|
||||
|
||||
public function fetchByFactura(int $factura_id): array
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('factura_id = :factura_id');
|
||||
return $this->fetchMany($query, ['factura_id' => $factura_id]);
|
||||
}
|
||||
}
|
17
app/src/Repository/Venta/Factura/Estado/Tipo.php
Normal file
17
app/src/Repository/Venta/Factura/Estado/Tipo.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Venta\Factura\Estado;
|
||||
|
||||
use Incoviba\Repository;
|
||||
use Incoviba\Model;
|
||||
|
||||
class Tipo extends Repository\Tipo
|
||||
{
|
||||
public function getTable(): string
|
||||
{
|
||||
return 'tipos_estados_facturas';
|
||||
}
|
||||
protected function getBlank(): Model\Venta\Factura\Estado\Tipo
|
||||
{
|
||||
return new Model\Venta\Factura\Estado\Tipo;
|
||||
}
|
||||
}
|
@ -65,7 +65,7 @@ class Propietario extends Ideal\Repository
|
||||
{
|
||||
$model->rut = $this->saveNew(
|
||||
['rut', 'dv', 'nombres', 'apellido_paterno', 'apellido_materno', 'direccion', 'otro', 'representante'],
|
||||
[$model->rut, $model->dv, $model->nombres, $model->apellidos['paterno'], $model->apellidos['materno'], $model->datos->direccion->id, $model->otro->rut ?? 0, $model->representante->rut ?? 0]
|
||||
[$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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user