Merge branch 'develop' of http://git.provm.cl/Incoviba/oficial into develop

This commit is contained in:
Aldarien
2024-02-14 16:54:10 +00:00
8 changed files with 257 additions and 11 deletions

View File

@ -28,6 +28,9 @@ $app->group('/venta/{venta_id:[0-9]+}', function($app) {
});
$app->get('[/]', [Ventas::class, 'pie']);
});
$app->group('/escritura', function($app) {
$app->get('/add[/]', [Ventas\Escrituras::class, 'add']);
});
$app->get('/escriturar[/]', [Ventas::class, 'escriturar']);
$app->get('/desistir[/]', [Ventas::class, 'desistir']);
$app->get('/desistida[/]', [Ventas::class, 'desistida']);

View File

@ -19,11 +19,17 @@ $app->group('/ventas', function($app) {
$app->group('/escrituras', function($app) {
$app->post('/estados[/]', [Ventas::class, 'escrituras']);
});
$app->group('/by', function($app) {
$app->get('/unidad/{unidad_id}', [Ventas::class, 'unidad']);
});
$app->post('[/]', [Ventas::class, 'proyecto']);
});
$app->group('/venta/{venta_id}', function($app) {
$app->get('/unidades[/]', [Ventas::class, 'unidades']);
$app->get('/comentarios[/]', [Ventas::class, 'comentarios']);
$app->group('/escritura', function($app) {
$app->post('/add[/]', [Ventas\Escrituras::class, 'add']);
});
$app->post('/escriturar[/]', [Ventas::class, 'escriturar']);
$app->group('/desistir', function($app) {
$app->get('/eliminar[/]', [Ventas::class, 'insistir']);

View File

@ -0,0 +1,153 @@
@extends('ventas.base')
@section('venta_subtitle')
Agregar Abono en Escritura
@endsection
@section('venta_content')
<div class="ui basic segment">
<p>Valor Promesa {{$format->ufs($venta->valor)}}</p>
@if (isset($venta->formaPago()->pie))
<p>Valor Anticipo {{$format->ufs($venta->formaPago()->pie->valor)}}</p>
@endif
@if (isset($venta->formaPago()->credito))
<p>Crédito {{$format->ufs($venta->formaPago()->credito->pago->valor())}}</p>
@endif
</div>
<form class="ui form" id="add_form">
<div class="three wide field">
<label for="fecha">Fecha</label>
<div class="ui calendar" id="fecha">
<div class="ui left icon input">
<i class="calendar icon"></i>
<input type="text" name="fecha" />
</div>
</div>
</div>
<div class="fields">
<div class="field">
<label for="valor">Valor</label>
<div class="ui labeled input" id="valor">
<div class="ui basic label">$</div>
<input type="text" name="valor" />
</div>
</div>
<div class="field">
<div class="ui checkbox" id="uf">
<input type="checkbox" name="uf" />
<label>Valor en UFs</label>
</div>
</div>
</div>
<div class="fields">
<div class="three wide field">
<label for="bancos">Banco</label>
<div class="ui search selection dropdown" id="bancos">
<input type="hidden" name="banco" />
<i class="dropdown icon"></i>
<div class="default text">Banco</div>
<div class="menu">
@foreach ($bancos as $banco)
<div class="item" data-value="{{$banco->id}}">{{$banco->nombre}}</div>
@endforeach
</div>
</div>
</div>
<div class="fields">
<div class="field">
<div class="ui radio checkbox">
<input type="radio" name="tipo" value="1" checked="checked" />
<label>Cheque</label>
</div>
</div>
<div class="field">
<div class="ui radio checkbox">
<input type="radio" name="tipo" value="3" />
<label>Vale Vista</label>
</div>
</div>
</div>
</div>
<button class="ui button">Agregar</button>
</form>
@endsection
@push('page_scripts')
<script>
const escritura = {
ids: {},
add() {
return {
escritura: event => {
event.preventDefault()
const body = new FormData(event.currentTarget)
const fecha = $(this.ids.fecha).calendar('get date')
body.set('fecha', [fecha.getFullYear(), fecha.getMonth()+1, fecha.getDate()].join('-'))
const status = $(this.ids.checkbox).checkbox('is checked')
body.set('uf', status)
const url = '{{$urls->api}}/venta/{{$venta->id}}/escritura/add'
fetchAPI(url, {method: 'post', body}).then(response => {
if (!response) {
return
}
return response.json().then(json => {
if (json.status) {
window.location = '{{$urls->base}}/venta/{{$venta->id}}'
}
})
})
return false
}
}
},
toggle() {
return {
checkbox: () => {
const status = $(this.ids.checkbox).checkbox('is checked')
if (status) {
return this.toggle().uf()
}
return this.toggle().pesos()
},
uf: () => {
const valor = $(this.ids.valor)
valor.attr('class', 'ui right labeled input')
valor.find('.label').remove()
valor.append(
$('<div></div>').addClass('ui basic label').html('UF')
)
},
pesos: () => {
const valor = $(this.ids.valor)
valor.attr('class', 'ui labeled input')
valor.find('.label').remove()
valor.prepend(
$('<div></div>').addClass('ui basic label').html('$')
)
}
}
},
setup(ids) {
this.ids = ids
$(this.ids.fecha).calendar(calendar_date_options)
$(this.ids.checkbox).checkbox({
fireOnInit: true,
onChange: this.toggle().checkbox
})
$(this.ids.bancos).dropdown()
$(this.ids.form).submit(this.add().escritura)
}
}
$(document).ready(() => {
escritura.setup({
form: '#add_form',
fecha: '#fecha',
checkbox: '#uf',
valor: '#valor',
bancos: '#bancos'
})
})
</script>
@endpush

View File

@ -13,6 +13,50 @@ class Escrituras
{
use withJson;
public function add(ServerRequestInterface $request, ResponseInterface $response,
Repository\Venta $ventaRepository, Service\Venta $ventaService,
Repository\Venta\Escritura $escrituraRepository, Service\Venta\Pago $pagoService,
Service\UF $ufService, int $venta_id): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'venta_id' => $venta_id,
'input' => $body,
'status' => false
];
try {
$venta = $ventaService->getById($venta_id);
if (isset($venta->formaPago()->escritura)) {
throw new EmptyResult('');
}
$fecha = new DateTimeImmutable($body['fecha']);
$uf = $ufService->get($fecha);
$valor = $body['valor'];
if (str_contains($valor, ',')) {
$valor = str_replace(['.', ','], ['', '.'], $valor);
}
$valor = ((float) $valor) * (($body['uf']) ? $uf : 1);
$data = [
'fecha' => $fecha->format('Y-m-d'),
'valor' => $valor,
'banco' => $body['banco'],
'tipo' => $body['tipo'],
'uf' => $uf
];
$pago = $pagoService->add($data);
$data = [
'valor' => $valor,
'fecha' => $fecha->format('Y-m-d'),
'uf' => $uf,
'pago' => $pago->id
];
$escritura = $escrituraRepository->create($data);
$escrituraRepository->save($escritura);
$ventaRepository->edit($venta, ['escritura' => $escritura->id]);
$output['status'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
public function edit(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta $ventaService, Repository\Venta\EstadoVenta $estadoVentaRepository,
int $venta_id): ResponseInterface

View File

@ -4,6 +4,7 @@ namespace Incoviba\Controller\Ventas;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Alias\View;
use Incoviba\Repository;
use Incoviba\Service;
class Escrituras
@ -20,4 +21,12 @@ class Escrituras
$venta = $ventaService->getById($venta_id);
return $view->render($response, 'ventas.escrituras.informe', compact('venta'));
}
public function add(ServerRequestInterface $request, ResponseInterface $response, View $view,
Service\Venta $ventaService, Repository\Banco $bancoRepository,
int $venta_id): ResponseInterface
{
$venta = $ventaService->getById($venta_id);
$bancos = $bancoRepository->fetchAll('nombre');
return $view->render($response, 'ventas.escrituras.add', compact('venta', 'bancos'));
}
}

View File

@ -26,6 +26,17 @@ class FormaPago implements JsonSerializable
}
return $sum;
}
public function prometido(string $moneda = Pago::UF): float
{
$sum = 0;
if (isset($this->pie)) {
$sum += $this->pie->valor($moneda);
}
if (isset($this->escritura)) {
$sum += $this->escritura->pago->valor($moneda);
}
return $sum;
}
public function total(string $moneda = Pago::UF): float
{
$sum = $this->anticipo($moneda);

View File

@ -33,14 +33,19 @@ class Pie extends Model
});
}
public function valor(string $moneda = Pago::UF): float
{
$proporcion = $this->proporcion();
if ($this->asociado !== null) {
return $this->asociado->valor($moneda) * $proporcion;
}
return array_reduce($this->cuotas(), function(float $sum, Cuota $cuota) use ($moneda) {
return $sum + $cuota->pago->valor($moneda);
}, 0) * $proporcion;
}
public function pagado(string $moneda = Pago::UF): float
{
$proporcion = 1;
if (count($this->asociados()) > 0) {
$proporcion = $this->valor / ((($this->asociado) ? $this->asociado->valor : $this->valor) + array_reduce($this->asociados(), function(float $sum, Pie $pie) {
return $sum + $pie->valor;
}, 0));
}
$proporcion = $this->proporcion();
if ($this->asociado !== null) {
return $this->asociado->pagado($moneda) * $proporcion;
}
@ -49,6 +54,17 @@ class Pie extends Model
return $sum + $cuota->pago->valor($moneda);
}, 0) * $proporcion;
}
protected function proporcion(): float
{
$proporcion = 1;
if (count($this->asociados()) > 0) {
$proporcion = $this->valor / ((($this->asociado) ? $this->asociado->valor : $this->valor) + array_reduce($this->asociados(), function(float $sum, Pie $pie) {
return $sum + $pie->valor;
}, 0));
}
return $proporcion;
}
public ?array $asociados;
public function asociados(): array
{

View File

@ -47,15 +47,19 @@ class EstadoVenta extends Ideal\Repository
public function fetchByVenta(int $venta_id): array
{
$query = "SELECT * FROM `{$this->getTable()}` WHERE `venta` = ?";
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('venta = ?');
return $this->fetchMany($query, [$venta_id]);
}
public function fetchCurrentByVenta(int $venta_id): Model\Venta\EstadoVenta
{
$query = "SELECT a.*
FROM `{$this->getTable()}` a
JOIN (SELECT MAX(`id`) AS 'id', `venta` FROM `{$this->getTable()}` GROUP BY `venta`) e0 ON e0.`id` = a.`id`
WHERE a.`venta` = ?";
$query = $this->connection->getQueryBuilder()
->select('a.*')
->from("{$this->getTable()} a")
->joined("JOIN (SELECT MAX(id) AS id, venta FROM {$this->getTable()} GROUP BY venta) ev0 ON ev0.id = a.id")
->where('a.venta = ?');
return $this->fetchOne($query, [$venta_id]);
}
}