227 lines
12 KiB
PHP
227 lines
12 KiB
PHP
@extends('layout.base')
|
|
|
|
@section('page_content')
|
|
<div class="ui container">
|
|
<h2 class="ui header">
|
|
Facturación -
|
|
<a href="{{$urls->base}}/proyecto/{{$venta->proyecto()->id}}">
|
|
{{$venta->proyecto()->descripcion}}<span class="ui tiny text"><sub><i
|
|
class="search icon"></i></sub></span>
|
|
</a>
|
|
-
|
|
<a href="{{$urls->base}}/venta/{{$venta->id}}">
|
|
{{$venta->propiedad()->summary()}}
|
|
</a>
|
|
</h2>
|
|
<span id="venta"></span>
|
|
<div id="facturas"></div>
|
|
</div>
|
|
@endsection
|
|
|
|
@include('layout.body.scripts.rut')
|
|
|
|
@push('page_scripts')
|
|
@include('ventas.facturacion.show.factura')
|
|
@include('ventas.facturacion.show.propietario')
|
|
@include('ventas.facturacion.show.unidad')
|
|
@include('ventas.facturacion.show.venta')
|
|
<script>
|
|
const facturas = {
|
|
ids: {},
|
|
venta: null,
|
|
formatters: {
|
|
date: new Intl.DateTimeFormat('es-CL', {year: 'numeric', month: '2-digit', day: '2-digit'}),
|
|
pesos: new Intl.NumberFormat('es-CL', {minimumFractionDigits: 0, maximumFractionDigits: 0}),
|
|
ufs: new Intl.NumberFormat('es-CL', {minimumFractionDigits: 2, maximumFractionDigits: 2}),
|
|
percent: new Intl.NumberFormat('es-CL', {minimumFractionDigits: 4, maximumFractionDigits: 4})
|
|
},
|
|
draw() {
|
|
return {
|
|
venta: () => {
|
|
document.getElementById(this.ids.venta).innerHTML = this.venta.draw().venta(this.formatters.ufs)
|
|
this.venta.watch().venta()
|
|
},
|
|
facturas: () => {
|
|
document.getElementById(this.ids.facturas).innerHTML = this.venta.draw().facturas(this.formatters)
|
|
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({
|
|
id: {{$venta->id}},
|
|
inmobiliaria: {
|
|
rut: '{{$venta->proyecto()->inmobiliaria()->rut()}}',
|
|
nombre: '{{$venta->proyecto()->inmobiliaria()->nombreCompleto()}}',
|
|
direccion: '{{$venta->proyecto()->direccion()->simple()}}',
|
|
comuna: '{{$venta->proyecto()->direccion()->comuna->descripcion}}'
|
|
},
|
|
proyecto: {
|
|
id: {{$venta->proyecto()->id}},
|
|
},
|
|
valor: {{$venta->valor}},
|
|
uf: {
|
|
fecha: new Date('{{$venta->currentEstado()->fecha->add(new DateInterval('P1D'))->format('Y-m-d')}}'),
|
|
valor: {{$uf}}
|
|
},
|
|
estado: {
|
|
fecha: new Date('{{$venta->currentEstado()->fecha->add(new DateInterval('P1D'))->format('Y-m-d')}}')
|
|
},
|
|
last: {
|
|
november: new Date('{{(new DateTimeImmutable())->sub(new DateInterval('P1Y'))->format('Y-11-30')}}')
|
|
},
|
|
unidades: [
|
|
@foreach ($venta->propiedad()->unidades as $unidad)
|
|
new Unidad({
|
|
id: {{$unidad->id}},
|
|
tipo: '{{ucwords($unidad->proyectoTipoUnidad->tipoUnidad->descripcion)}}',
|
|
descripcion: '{{ $unidad->descripcion }}',
|
|
prorrateo: {{$unidad->prorrateo}},
|
|
propiedad_unidad_id: {{$unidad->pu_id}},
|
|
valor: {{($unidad->valor > 0) ? $unidad->valor : $unidad->precio($venta->currentEstado()->fecha)->valor}}
|
|
}),
|
|
@endforeach
|
|
],
|
|
propietarios: [],
|
|
facturas: {
|
|
fecha: new Date('{{$venta->currentEstado()->fecha->add(new DateInterval('P1D'))->format('Y-m-d')}}'),
|
|
terreno: {
|
|
@if ($terreno->fecha != null) fecha: new Date('{{$terreno->fecha->add(new DateInterval('P1D'))->format('Y-m-d')}}'),
|
|
@else fecha: null,
|
|
@endif
|
|
valor: {{($terreno->valor ?? 0) * (1 + $ipc)}}
|
|
},
|
|
facturas: []
|
|
}
|
|
})
|
|
@if (count($facturas) > 0)
|
|
this.venta.props.uf = {
|
|
fecha: new Date('{{$facturas[0]->uf->fecha->format('Y-m-d')}}'),
|
|
valor: {{$facturas[0]->uf->valor}}
|
|
}
|
|
@foreach ($facturas as $factura)
|
|
this.venta.props.facturas.facturas[{{$factura->index - 1}}] = new Factura({
|
|
id: {{ $factura->id }},
|
|
venta: this.venta.props,
|
|
proporcion: {{ $factura->proporcion }},
|
|
index: {{$factura->index}},
|
|
fecha: new Date('{{$factura->fecha->format('Y-m-d')}}'),
|
|
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: '{{ $unidad->unidad->descripcion }}',
|
|
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 * $factura->prorrateo }},
|
|
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->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
|
|
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()}}',
|
|
nombre: '{{$venta->propietario()->nombreCompleto()}}',
|
|
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().facturas()
|
|
}
|
|
}
|
|
$(document).ready(() => {
|
|
facturas.setup({
|
|
ids: {
|
|
venta: 'venta',
|
|
facturas: 'facturas'
|
|
}
|
|
})
|
|
})
|
|
</script>
|
|
@endpush
|