This commit is contained in:
2021-08-18 19:03:58 -04:00
parent b58cda3e4e
commit 4f7241e146
12 changed files with 143 additions and 25 deletions

4
.db.env.sample Normal file
View File

@ -0,0 +1,4 @@
MYSQL_ROOT_PASSWORD=password
MYSQL_DATABASE=database
MYSQL_USER=user
MYSQL_PASSWORD=password

3
.ui.env.sample Normal file
View File

@ -0,0 +1,3 @@
DEBUG=true
BASE_URL=http://localhost:8080
API_URL=http://localhost:8081

View File

@ -41,4 +41,10 @@ class Facturas {
];
return $this->withJson($response, $output);
}
public function add_venta(Request $request, Response $response, $id_factura): Response {
$post = $request->getParsedBody();
$factura = Model::factory(FacturaProyectoOperador::class)->find_one($id_factura);
$output = $factura->addVenta($post);
return $this->withJson($response, $output);
}
}

View File

@ -28,4 +28,20 @@ class Ventas {
];
return $this->withJson($response, $output);
}
public function facturas(Request $request, Response $response, $id_venta): Response {
$venta = Model::factory(Venta::class)->find_one($id_venta);
if (!$venta) {
return $this->withJson($response, ['venta' =>null, 'facturas' => null]);
}
$output = [
'venta' => $venta->as_array(),
'facturas' => null
];
if ($venta->facturas() !== null) {
$output['facturas'] = array_map(function($item) {
return $item->as_array();
}, $venta->facturas());
}
return $this->withJson($response, $output);
}
}

View File

@ -10,6 +10,7 @@ $app->group('/facturas', function($app) {
});
$app->group('/factura/{id_factura}', function($app) {
$app->group('/ventas', function($app) {
$app->post('/add[/]', [Facturas::class, 'add_venta']);
$app->get('[/]', [Facturas::class, 'ventas']);
});
});

View File

@ -8,5 +8,8 @@ $app->group('/venta/{id_venta}', function($app) {
$app->group('/operador', function($app) {
$app->get('[/]', [Ventas::class, 'operador']);
});
$app->group('/facturas', function($app) {
$app->get('[/]', [Ventas::class, 'facturas']);
});
$app->get('[/]', [Ventas::class, 'show']);
});

View File

@ -83,6 +83,10 @@ class FacturaProyectoOperador extends Model {
];
return $output;
}
public function addVenta($data) {
$data['factura_id'] = $this->id;
return FacturaVenta::add($data);
}
public function as_array() {
$arr = parent::as_array();

View File

@ -27,6 +27,37 @@ class FacturaVenta extends Model {
return $this->venta;
}
public static function add($data) {
$fields = [
'factura_id',
'venta_id',
'valor'
];
$input = array_intersect_key($data, array_combine($fields, $fields));
$validate = [
'factura_id',
'venta_id'
];
$orm = Model::factory(FacturaVenta::class);
foreach ($validate as $field) {
$orm = $orm->where($field, $input[$field]);
}
$factura = $orm->find_one();
$created = false;
$found = true;
if (!$factura) {
$found = false;
$factura = FacturaVenta::create($input);
$created = $factura->save();
}
return [
'input' => $input,
'factura' => $factura->as_array(),
'new' => !$found,
'created' => $created
];
}
public function as_array() {
$arr = parent::as_array();
$arr['factura'] = $this->factura()->as_array();

View File

@ -20,11 +20,4 @@ class Unidad extends Model {
}
return $this->proyecto_tipo_unidad;
}
protected $facturas;
public function facturas() {
if ($this->facturas === null) {
$this->facturas = $this->has_many(FacturaUnidad::class, 'unidad_id')->find_many();
}
return $this->facturas;
}
}

View File

@ -72,16 +72,26 @@ class Venta extends Model {
}
return $this->comision;
}
protected $facturas;
public function facturas() {
if ($this->facturas === null) {
$this->facturas = $this->has_many(FacturaVenta::class, 'venta_id')->find_many();
}
return $this->facturas;
}
public function as_array() {
$arr = parent::as_array();
$arr['propietario'] = $this->propietario()->as_array();
$arr['propiedad'] = $this->propiedad()->as_array();
$arr['valor'] = '$ ' . number_format($this->valor_uf, 2, ',', '.');
$arr['valor'] = [
'valor' => $this->valor_uf,
'formateado' => number_format($this->valor_uf, 2, ',', '.') . ' UF'
];
if ($this->operador()) {
$arr['operador'] = $this->operador()->as_array();
$arr['comision'] = (array) $this->comision();
$arr['comision']['formateada'] = '$ ' . number_format($this->comision()->total, 2, ',', '.');
$arr['comision']['formateada'] = number_format($this->comision()->total, 2, ',', '.') . ' UF';
}
return $arr;
}

View File

@ -328,6 +328,7 @@
facturas.asignar($(e.target)).then(() => {
$(e.target).trigger('reset')
$(this.id).modal('toggle')
$(this.id).modal('hide')
})
return false
})

View File

@ -53,6 +53,60 @@
})
}
}
class Facturas {
constructor(venta_id) {
this.venta_id = venta_id
this.facturas = []
}
get() {
return $.ajax({
url: '{{$urls->api}}/venta/' + this.venta_id + '/facturas',
method: 'get',
dataType: 'json'
}).then((data) => {
if (data.facturas === null || data.facturas.length == 0) {
return
}
this.facturas = data.facturas
this.draw()
})
}
draw() {
const parent = $('#' + this.venta_id)
let tbody = parent.find('tbody')
if (tbody.length == 0) {
tbody = $('<tbody></tbody>')
const table = $('<table></table>').attr('class', 'table').append(
$('<thead></thead>').append(
$('<tr></tr>').append(
$('<th></th>').html('Factura')
).append(
$('<th></th>').html('Emisor')
).append(
$('<th></th>').html('Fecha')
).append(
$('<th></th>').html('Valor')
)
)
).append(tbody)
parent.append(table)
}
tbody.html('')
$.each(this.facturas, (i, el) => {
tbody.append(
$('<tr></tr>').append(
$('<td></td>').html(el.factura.factura)
).append(
$('<td></td>').html(el.factura.operador.descripcion)
).append(
$('<td></td>').html(el.factura.fecha.formateada)
).append(
$('<td></td>').html(el.valor.formateado)
)
)
})
}
}
const ventas = {
id: '#ventas',
data: [],
@ -69,9 +123,9 @@
this.draw()
})
},
facturas: (unidad) => {
facturas: (venta) => {
return $.ajax({
url: '{{$urls->api}}/unidad/' + unidad + '/facturas',
url: '{{$urls->api}}/venta/' + venta + '/facturas',
method: 'get',
dataType: 'json'
})
@ -86,34 +140,26 @@
).append(
$('<td></td>').html(el.propietario.nombre_completo)
).append(
$('<td></td>').attr('class', 'text-right').html(el.valor)
$('<td></td>').attr('class', 'text-right').html(el.valor.formateado)
)
if (el.operador) {
row.append(
$('<td></td>').html(el.operador.descripcion)
).append(
$('<td></td>').attr('class', 'text-right').html(el.comision.formateada)
).append(
$('<td></td>').attr('id', el.propiedad.unidades[0].id)
)
this.get().facturas(el.propiedad.unidades[0].id).then((data) => {
const td = $('td#' + data.unidad.id)
if (data.facturas === null || data.facturas.length == 0) {
return
}
$.each(data.facturas, (k, it) => {
console.debug(it)
})
})
} else {
row.append(
$('<td></td>')
).append(
$('<td></td>')
).append(
$('<td></td>')
)
}
row.append(
$('<td></td>').attr('id', el.id)
)
this.data[i].facturas = new Facturas(el.id)
this.data[i].facturas.get()
parent.append(row)
})
}