Guardar factura

This commit is contained in:
Juan Pablo Vial
2025-02-03 22:17:57 -03:00
parent 71615050f3
commit 59a28a353b
11 changed files with 296 additions and 122 deletions

View File

@ -41,13 +41,18 @@
},
facturas: () => {
document.getElementById(this.ids.facturas).innerHTML = this.venta.draw().facturas(this.formatters)
$('button.guardar').click(clickEvent => {
const index = clickEvent.currentTarget.getAttribute('data-index')
facturas.venta.save().factura({index: index-1})
})
this.venta.watch().facturas()
}
}
},
proporcion() {
if (this.venta.props.facturas.facturas.length === 0) {
return 1
}
return this.venta.props.facturas.facturas.reduce((sum, factura) => {
return sum + factura.props.proporcion
}, 0)
},
setup({ids}) {
this.ids = ids
this.venta = new Venta({
@ -77,7 +82,7 @@
new Unidad({
id: {{$unidad->id}},
tipo: '{{ucwords($unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}}',
descripcion: '{{$unidad->descripcion}}',
descripcion: '{{ucwords($unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}} {{ $unidad->descripcion }} (UF {{ number_format($unidad->precio($venta->currentEstado()->fecha)->valor, 2) }})',
prorrateo: {{$unidad->prorrateo}},
propiedad_unidad_id: {{$unidad->pu_id}},
valor: {{($unidad->valor > 0) ? $unidad->valor : $unidad->precio($venta->currentEstado()->fecha)->valor}}
@ -98,23 +103,100 @@
})
@if (count($facturas) > 0)
this.venta.props.uf = {
fecha: new Date('{{$facturas[0]->fechaUF->format('Y-m-d')}}'),
valor: {{$facturas[0]->valorUF}}
fecha: new Date('{{$facturas[0]->uf->fecha->format('Y-m-d')}}'),
valor: {{$facturas[0]->uf->valor}}
}
@foreach ($facturas as $factura)
this.venta.props.facturas[{{$factura->index-1}}] = new Factura(JSON.parse('{!! json_encode($factura) !!}'))
this.venta.props.facturas[{{$factura->index-1}}].props.saved = true
this.venta.props.propietarios[{{$factura->index-1}}] = new Propietario({
this.venta.props.facturas.facturas[{{$factura->index - 1}}] = new Factura({
id: {{ $factura->id }},
venta: this.venta.props,
index: {{$factura->index}},
fecha: new Date('{{$factura->fecha->format('Y-m-d')}}'),
proporcion: {{$factura->proporcion}},
emisor: {
rut: '{{$factura->venta->proyecto()->inmobiliaria()->rut}}',
nombre: '{{$factura->venta->proyecto()->inmobiliaria()->razon}}',
direccion: '{{$factura->venta->proyecto()->direccion()->simple()}}',
comuna: '{{$factura->venta->proyecto()->direccion()->comuna->id}}',
},
receptor: {
rut: '{{$factura->cliente->rutCompleto()}}',
nombre: '{{$factura->cliente->nombreCompleto()}}',
direccion: '{{$factura->cliente->datos()->direccion->simple()}}',
comuna: '{{$factura->cliente->datos()->direccion->comuna->id}}',
},
terreno: {
@if ($terreno->fecha != null) fecha: new Date('{{$terreno->fecha->add(new DateInterval('P1D'))->format('Y-m-d')}}'),
@else fecha: null,
@endif
valor: {{ $factura->terreno}}
},
unidades: [
@foreach ($factura->unidades as $unidad)
{
unidad: new Unidad({
id: {{$unidad->unidad->id}},
tipo: '{{ucwords($unidad->unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}}',
descripcion: '{{ucwords($unidad->unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}} {{ $unidad->unidad->descripcion }} (UF {{ number_format(array_values(array_filter($venta->propiedad()->unidades, function($uni) use ($unidad, $factura) {return $uni->id === $unidad->unidad->id;}))[0]->precio($venta->currentEstado()->fecha)->valor * $factura->proporcion, 2) }})',
prorrateo: {{$unidad->prorrateo}},
propiedad_unidad_id: {{ array_values(array_filter($venta->propiedad()->unidades, function($uni) use ($unidad) {return $uni->id === $unidad->unidad->id;}))[0]->pu_id }},
valor: {{$unidad->precio}}
}),
descripcion: '{{ucwords($unidad->unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}} {{ $unidad->unidad->descripcion }} (UF {{ number_format(array_values(array_filter($venta->propiedad()->unidades, function($uni) use ($unidad, $factura) {return $uni->id === $unidad->unidad->id;}))[0]->precio($venta->currentEstado()->fecha)->valor * $factura->proporcion, 2) }})',
precio: {{ $unidad->precio }},
prorrateo: {{ $unidad->prorrateo }},
},
@endforeach
],
detalle: {
base: {{ $factura->base() }},
terreno: {{ $factura->terreno * $factura->proporcion }},
neto: {{ $factura->neto() }},
iva: {{ $factura->iva() }},
bruto: {{ $factura->bruto() }},
total: {{ $factura->total() }},
descuento: {{ array_reduce($factura->unidades, function($sum, $unidad) use ($factura) { return $sum + $unidad->prorrateo * $factura->proporcion; }, 0) }}
},
total: {
neto: {{ array_reduce($factura->unidades, function($sum, $unidad) {return $sum + $unidad->precio * $factura->proporcion;}, 0) }},
exento: {{ $factura->terreno * $factura->proporcion }},
iva: {{ $factura->iva() }},
total: {{ $factura->total() }}
},
saved: true,
uf: {
fecha: new Date('{{$factura->uf->fecha->format('Y-m-d')}}'),
valor: {{$factura->uf->valor}}
}
})
this.venta.props.facturas.facturas[{{$factura->index - 1}}].props.saved = true
this.venta.props.propietarios[{{$factura->index - 1}}] = new Propietario({
index: {{$factura->index}},
proporcion: {{$factura->proporcion}},
rut: '{{$factura->receptorRut}}',
nombre: '{{$factura->receptorNombre}}',
direccion: '{{$factura->receptorDireccion}}',
comuna: '{{$factura->receptorComuna}}',
rut: '{{$factura->cliente->rutCompleto()}}',
nombre: '{{$factura->cliente->nombreCompleto()}}',
direccion: '{{$factura->cliente->datos()->direccion->simple()}}',
comuna: '{{$factura->cliente->datos()->direccion->comuna->id}}',
fecha: new Date('{{$factura->fecha->format('Y-m-d')}}'),
})
@endforeach
@else
this.draw().venta()
if (this.proporcion() < 1) {
const p = 1 - this.proporcion()
const propietario = this.venta.add().propietario({
rut: '',
nombre: '',
proporcion: (p*100).toFixed(0)/100,
direccion: '',
comuna: '0'
})
document.getElementById('propietarios').innerHTML = this.venta.draw().propietarios()
document.getElementById('cantidad_propietarios').value = this.venta.props.propietarios.length
this.venta.add().factura(propietario)
this.draw().facturas()
}
@else
const propietario = this.venta.add().propietario({
proporcion: 1,
rut: '{{$venta->propietario()->rut()}}',
@ -122,11 +204,11 @@
direccion: '{{$venta->propietario()->datos->direccion->simple()}}',
comuna: '{{$venta->propietario()->datos->direccion->comuna->id}}'
})
this.draw().venta()
if (typeof propietario !== 'undefined') {
this.venta.add().factura(propietario)
}
@endif
this.draw().venta()
if (typeof propietario !== 'undefined') {
this.venta.add().factura(propietario)
}
this.draw().facturas()
}
}

View File

@ -89,11 +89,11 @@
rut: () => {
return [
'<div class="four wide column">',
'<div class="ui center aligned red segment">',
'<div class="ui center aligned orange segment">',
'<strong>',
`RUT:${this.props.emisor.rut.toUpperCase()}<br/>`,
'FACTURA ELECTRÓNICA<br/>',
`N° #${this.props.venta.id}${this.props.index}`,
`<span class="ui red text">N° ${this.props.venta.id}${this.props.index}</span>`,
'</strong>',
'</div>',
'</div>'
@ -128,34 +128,34 @@
table: ({formatters}) => {
return [
'<div class="row">',
'<table class="ui celled table">',
'<thead>',
'<tr class="grey">',
'<th class="center aligned" colspan="6">DETALLES</th>',
'</tr>',
'<tr class="grey">',
'<th>N°</th>',
'<th class="center aligned">Descripción</th>',
'<th class="center aligned">Cant/Unidad</th>',
'<th class="center aligned">Prec. Unit.</th>',
'<th class="center aligned">Ind</th>',
'<th class="center aligned">Total</th>',
'</tr>',
'</thead>',
'<tbody>',
this.draw().unidades({formatters}),
'</tbody>',
'<tfoot>',
'<tr>',
'<td colspan="6">',
'<br />',
'<br />',
'<br />',
'<br />',
'</td>',
'</tr>',
'</tfoot>',
'</table>',
'<table class="ui celled table">',
'<thead>',
'<tr class="grey">',
'<th class="center aligned" colspan="6">DETALLES</th>',
'</tr>',
'<tr class="grey">',
'<th>N°</th>',
'<th class="center aligned">Descripción</th>',
'<th class="center aligned">Cant/Unidad</th>',
'<th class="center aligned">Prec. Unit.</th>',
'<th class="center aligned">Ind</th>',
'<th class="center aligned">Total</th>',
'</tr>',
'</thead>',
'<tbody>',
this.draw().unidades({formatters}),
'</tbody>',
'<tfoot>',
'<tr>',
'<td colspan="6">',
'<br />',
'<br />',
'<br />',
'<br />',
'</td>',
'</tr>',
'</tfoot>',
'</table>',
'</div>'
].join("\n")
},
@ -279,7 +279,7 @@
'<div class="row">',
'<div class="fourteen wide column"></div>',
'<div class="two wide center aligned column">',
'<div class="ui green message">',
`<div class="ui green message guardar" data-index="${this.props.index}">`,
'<i class="check icon"></i>',
'Guardada',
'</div>',
@ -300,14 +300,30 @@
watch() {
return {
save: () => {
document.querySelector(`button[data-index="${this.props.index}"]`).addEventListener('click', clickEvent => {
document.querySelector(`.guardar[data-index="${this.props.index}"]`).addEventListener('click', clickEvent => {
const index = clickEvent.currentTarget.getAttribute('data-index')
facturas.save().factura(index)
facturas.venta.save().factura({index: index - 1})
})
}
}
}
validate() {
if (this.props.venta.id === null || typeof this.props.venta.id === 'undefined') {
return false
}
if (this.props.index === null || typeof this.props.index === 'undefined') {
return false
}
if (this.props.proporcion === null || typeof this.props.proporcion === 'undefined') {
return false
}
return !(this.props.receptor.rut === '' || this.props.receptor.nombre === '' || this.props.receptor.direccion === '' || this.props.receptor.comuna === '');
}
save() {
if (!this.validate()) {
return
}
let url = '{{$urls->api}}/ventas/facturas/add'
if (this.saved) {
url = `{{$urls->api}}/ventas/facturas/${this.props.id}/edit`
@ -326,6 +342,7 @@
body.set('detalle', JSON.stringify(this.props.detalle))
body.set('total', JSON.stringify(this.props.total))
body.set('uf', JSON.stringify({fecha: [this.props.uf.fecha.getFullYear(), this.props.uf.fecha.getMonth()+1, this.props.uf.fecha.getDate()].join('-'), valor: this.props.uf.valor}))
return APIClient.fetch(url, {method, body}).then(response => {
if (!response) {
return
@ -366,7 +383,7 @@
unidades.forEach(unidad => {
this.props.unidades.push({
unidad: unidad,
descripcion: unidad.descripcion(this.props.proporcion),
descripcion: unidad.changeDescripcion(this.props.proporcion || 1),
precio: unidad.props.valor * this.props.uf.valor * this.props.proporcion,
prorrateo: unidad.props.prorrateo * this.props.proporcion
})
@ -376,10 +393,10 @@
this.props.proporcion = propietario.props.proporcion
this.props.receptor = {
rut: propietario.props.rut,
nombre: propietario.props.nombre,
direccion: propietario.props.direccion,
comuna: propietario.comuna
rut: propietario.props.rut ?? '',
nombre: propietario.props.nombre ?? '',
direccion: propietario.props.direccion ?? '',
comuna: propietario.comuna ?? ''
}
}
}

View File

@ -14,7 +14,11 @@
}
get comuna() {
return $('#comuna_propietario'+this.props.index).dropdown('get text') ?? this.props.comuna
const comuna = $('#comuna_propietario'+this.props.index).dropdown('get text')
if (typeof comuna === 'string') {
return comuna
}
return this.props.comuna ?? ''
}
update() {
return {

View File

@ -13,8 +13,8 @@
this.props = props
}
descripcion(proporcion = 1) {
return [this.props.tipo, this.props.descripcion, `[UF ${facturas.formatters.ufs.format(this.props.valor * proporcion)}]`].join(' ')
changeDescripcion(proporcion = 1) {
return this.descripcion = [this.props.tipo, this.props.descripcion, `[UF ${facturas.formatters.ufs.format(this.props.valor * proporcion)}]`].join(' ')
}
update() {
return {

View File

@ -252,6 +252,11 @@
this.props.propietarios.forEach(propietario => {
propietario.watch().propietario()
})
},
facturas: () => {
this.props.facturas.facturas.forEach(factura => {
factura.watch().save()
})
}
}
}
@ -267,11 +272,11 @@
value: ufFormatter => {
return [
'<div class="ui grid">',
'<div class="three wide column">',
'<div class="ui very segment">',
'Valor Venta: UF ' + ufFormatter.format(this.props.valor),
'</div>',
'</div>',
'<div class="three wide column">',
'<div class="ui very segment">',
'Valor Venta: UF ' + ufFormatter.format(this.props.valor),
'</div>',
'</div>',
'</div>'
].join("\n")
},
@ -377,7 +382,7 @@
},
facturas: formatters => {
const output = []
this.props.facturas.facturas.forEach((factura, index) => {
this.props.facturas.facturas.forEach(factura => {
output.push(factura.draw().factura({formatters}))
})
return output.join("\n")
@ -405,10 +410,10 @@
comuna: this.props.inmobiliaria.comuna
},
receptor: {
rut: '',
nombre: '',
direccion: '',
comuna: ''
rut: propietario.props.rut ?? '',
nombre: propietario.props.nombre ?? '',
direccion: propietario.props.direccion ?? '',
comuna: propietario.props.comuna ?? ''
},
unidades: [],
detalle: {