Files
oficial/app/resources/views/ventas/pies/cuotas.blade.php
2024-01-18 17:15:32 -03:00

304 lines
13 KiB
PHP

@extends('ventas.base')
@section('venta_subtitle')
Cuotas - Pie
@endsection
@section('venta_content')
<table class="ui table" id="cuotas">
<thead>
<tr>
<th>#</th>
<th>Fecha</th>
<th>Fecha ISO</th>
<th>Banco</th>
<th>Identificador</th>
<th class="right aligned">Valor</th>
<th class="right aligned">Valor UF</th>
<th>Estado</th>
<th>Fecha Estado</th>
<th>Fecha Estado ISO</th>
<th></th>
</tr>
</thead>
<tbody>@php
$now = new DateTimeImmutable();
$uf_venta = $venta->uf === 0.0 ? $UF->get($venta->currentEstado()->fecha) : $venta->uf;
@endphp
@foreach ($venta->formaPago()->pie->cuotas() as $cuota)
<tr data-pago="{{$cuota->pago->id}}"
@if (in_array($cuota->pago->currentEstado->tipoEstadoPago->descripcion, ['anulado', 'reemplazado']))
class="disabled"
@endif >
<td>{{$cuota->numero}}</td>
<td>{{$cuota->pago->fecha->format('d-m-Y')}}</td>
<td>{{$cuota->pago->fecha->format('Y-m-d')}}</td>
<td>{{$cuota->pago->banco->nombre}}</td>
<td>{{$cuota->pago->identificador}}</td>
<td class="right aligned">{{$format->pesos($cuota->pago->valor)}}</td>
<td class="right aligned">
@if ($cuota->pago->currentEstado->tipoEstadoPago->descripcion === 'abonado' and $cuota->pago->currentEstado->fecha <= $now)
{{$format->ufs($cuota->pago->valor())}}
@endif
</td>
<td
@if ($cuota->pago->currentEstado->tipoEstadoPago->descripcion === 'abonado')
class="green"
@elseif ($cuota->pago->currentEstado->tipoEstadoPago->descripcion === 'depositado')
class="yellow"
@elseif ($cuota->pago->currentEstado->tipoEstadoPago->activo !== 1)
class="red"
@endif
>{{ucwords($cuota->pago->currentEstado->tipoEstadoPago->descripcion)}}</td>
<td>
@if (in_array($cuota->pago->currentEstado->tipoEstadoPago->descripcion, ['abonado', 'anulado', 'reemplazado']))
{{$cuota->pago->currentEstado->fecha->format('d-m-Y')}}
@elseif (!in_array($cuota->pago->currentEstado->tipoEstadoPago->descripcion, ['anulado', 'reemplazado']))
<div class="ui calendar fecha_estado" data-date="{{$cuota->pago->currentEstado->fecha->format('Y-m-d')}}">
<div class="ui action left icon input">
<i class="calendar icon"></i>
<input type="text" name="fecha_estado" />
<button class="ui green basic icon button accept_estado" data-pago="{{$cuota->pago->id}}" data-estado="{{$cuota->pago->currentEstado->tipoEstadoPago->descripcion}}">
<i class="check icon"></i>
</button>
@if ($cuota->pago->currentEstado->tipoEstadoPago->descripcion === 'depositado')
<button class="ui red basic icon button reject_estado" data-pago="{{$cuota->pago->id}}">
<i class="remove icon"></i>
</button>
@endif
</div>
</div>
@endif
</td>
<td>{{$cuota->pago->currentEstado->fecha->format('Y-m-d')}}</td>
<td>
@if ($cuota->pago->currentEstado->tipoEstadoPago->descripcion !== 'anulado')
<button class="ui mini red icon basic button anular" data-pago="{{$cuota->pago->id}}">
<i class="remove icon"></i>
</button>
@endif
</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<th colspan="5">TOTAL</th>
<th class="right aligned">
{{$format->pesos($total_pesos = array_reduce($venta->formaPago()->pie->cuotas(),
function(float $sum, Incoviba\Model\Venta\Cuota $cuota) {
return $sum + $cuota->pago->valor;
}, 0))}}
</th>
<th class="right aligned">
{{$format->ufs($total = array_reduce($venta->formaPago()->pie->cuotas(),
function(float $sum, Incoviba\Model\Venta\Cuota $cuota) use ($now, $uf_venta) {
return $sum + (($cuota->pago->fecha > $now or $cuota->pago->uf === null) ?
$cuota->pago->valor / $uf_venta :
$cuota->pago->valor());
}, 0.0))}}
</th>
<th colspan="4"></th>
</tr>
<tr>
<th colspan="5">TOTAL PAGADO</th>
<th class="right aligned">
{{$format->pesos($pagado_pesos = array_reduce($venta->formaPago()->pie->cuotas(true),
function(int $sum, Incoviba\Model\Venta\Cuota $cuota) {
return $sum + $cuota->pago->valor;
}, 0))}}
</th>
<th class="right aligned">
{{$format->ufs($pagado = array_reduce($venta->formaPago()->pie->cuotas(true),
function(float $sum, Incoviba\Model\Venta\Cuota $cuota) {
return $sum + $cuota->pago->valor();
}, 0.0))}}
</th>
<th class="right aligned">
{{$format->number($pagado / $total * 100, 2)}}%
</th>
<th colspan="3"></th>
</tr>
<tr>
<th colspan="5">POR PAGAR</th>
<th class="right aligned">
{{$format->pesos($total_pesos - $pagado_pesos)}}
</th>
<th class="right aligned">
{{$format->ufs($total - $pagado)}}
</th>
<th class="right aligned">
{{$format->number(($total - $pagado) / $total * 100, 2)}}%
</th>
<th colspan="3"></th>
</tr>
</tfoot>
</table>
@endsection
@include('layout.body.scripts.datatables')
@push('page_scripts')
<script type="text/javascript">
$(document).ready(() => {
function updateRow({pago_id, fecha, color, estado, remove_fecha=false, add_reject=false, disable=false}) {
const tr = $("tr[data-pago='" + pago_id + "']")
tr.find(':nth-child(7)').attr('class', color).html(estado)
if (remove_fecha) {
tr.find(':nth-child(8)').html(fecha)
}
if (add_reject) {
tr.find(':nth-child(8)>.calendar>.input').append(
$('<button></button>').addClass('ui red basic icon button reject_estado').attr('data-pago', pago_id).append(
$('<i></i>').addClass('remove icon')
).click(event => {
const target = $(event.currentTarget)
const pago_id = target.data('pago')
const fecha = target.parent().parent().calendar('get date')
devolver(target, pago_id, fecha)
})
)
}
if (disable) {
tr.addClass('disabled')
tr.find(':nth-child(9)').html('')
}
return tr
}
function depositar(button, pago_id, fecha) {
const url = '{{$urls->api}}/ventas/pago/' + pago_id + '/depositar'
const body = new FormData()
body.set('fecha', fecha.toISOString())
return fetchAPI(url, {method: 'post', body}).then(response => {
if (!response) {
return
}
return response.json().then(json => {
if (!json.depositado) {
return
}
updateRow({pago_id: json.pago_id, fecha: json.input.fecha, estado: 'Depositado', color: 'yellow', add_reject: true})
button.attr('data-estado', 'depositado')
})
})
}
function abonar(pago_id, fecha) {
const url = '{{$urls->api}}/ventas/pago/' + pago_id + '/abonar'
const body = new FormData()
body.set('fecha', fecha.toISOString())
return fetchAPI(url, {method: 'post', body}).then(response => {
if (!response) {
return
}
return response.json().then(json => {
if (!json.abonado) {
return
}
updateRow({pago_id: json.pago_id, fecha: json.input.fecha, estado: 'Abonado', color: 'green', remove_fecha: true})
})
})
}
function anular(pago_id) {
const url = '{{$urls->api}}/ventas/pago/' + pago_id + '/anular'
return fetchAPI(url).then(response => {
if (!response) {
return
}
return response.json().then(json => {
if (!json.anulado) {
return
}
updateRow({pago_id: json.pago_id, fecha: json.fecha, estado: 'Anulado', color: 'red', remove_fecha: true, disable: true})
/*const tr = $("button[data-id='" + json.pago_id + "']").parent().parent()
tr.addClass('disabled')
tr.find(':nth-child(7)').addClass('red').html('Anulado')
tr.find(':nth-child(8)').html(json.fecha)
tr.find(':nth-child(9)').html('')*/
})
})
}
function devolver(button, pago_id, fecha) {
const url = '{{$urls->api}}/ventas/pago/' + pago_id + '/devolver'
const body = new FormData()
body.set('fecha', fecha.toISOString())
return fetchAPI(url, {method: 'post', body}).then(response => {
if (!response) {
return
}
return response.json().then(json => {
if (!json.devuelto) {
return
}
updateRow({pago_id: json.pago_id, fecha: json.input.fecha, estado: 'Devuelto', color: 'red'})
button.parent().find('.accept_estado').attr('data-estado', 'devuelto')
button.remove()
})
})
}
$('.fecha_estado').calendar({
type: 'date',
formatter: {
date: 'DD-MM-YYYY'
}
})
$('.accept_estado').click(event => {
const target = $(event.currentTarget)
const pago_id = target.data('pago')
const fecha = target.parent().parent().calendar('get date')
if (target.attr('data-estado') === 'depositado') {
return abonar(pago_id, fecha)
}
return depositar(target, pago_id, fecha)
})
$('.reject_estado').click(event => {
const target = $(event.currentTarget)
const pago_id = target.data('pago')
const fecha = target.parent().parent().calendar('get date')
devolver(target, pago_id, fecha)
})
$('.anular').click(event => {
const pago_id = $(event.currentTarget).data('pago')
anular(pago_id)
})
new DataTable('#cuotas', {
language: {
info: 'Mostrando página _PAGE_ de _PAGES_',
infoEmpty: 'No hay cuotas ingresadas',
infoFiltered: '(filtrado de _MAX_ cuotas)',
lengthMenu: 'Mostrando de a _MENU_ cuotas',
zeroRecords: 'No se encotró cuotas con ese criterio',
search: 'Buscar: '
},
pageLength: "25",
columnDefs: [
{
target: 1,
orderData: [2]
},
{
target: 2,
visible: false,
searchable: false
},
{
target: 8,
orderData: [9]
},
{
target: 9,
visible: false,
searchable: false
}
],
order: [
[0, 'asc'],
[2, 'asc']
]
})
})
</script>
@endpush