diff --git a/app/resources/routes/04_ventas.php b/app/resources/routes/04_ventas.php index 8ae9b6e..270c83b 100644 --- a/app/resources/routes/04_ventas.php +++ b/app/resources/routes/04_ventas.php @@ -30,7 +30,9 @@ $app->group('/venta/{venta_id:[0-9]+}', function($app) { $app->get('[/]', [Ventas::class, 'pie']); }); $app->group('/bono_pie', function($app) { + $app->get('/edit[/]', [Ventas\Bonos::class, 'edit']); $app->get('/add[/]', [Ventas\Bonos::class, 'add']); + $app->get('[/]', [Ventas\Bonos::class, 'edit']); }); $app->group('/escritura', function($app) { $app->group('/cuotas', function($app) { diff --git a/app/resources/routes/api/ventas.php b/app/resources/routes/api/ventas.php index 6374a98..026a2a8 100644 --- a/app/resources/routes/api/ventas.php +++ b/app/resources/routes/api/ventas.php @@ -32,6 +32,7 @@ $app->group('/venta/{venta_id}', function($app) { $app->get('[/]', [Ventas::class, 'comentarios']); }); $app->group('/bono_pie', function($app) { + $app->post('/edit[/]', [Ventas\Bonos::class, 'edit']); $app->post('/add[/]', [Ventas\Bonos::class, 'add']); }); $app->group('/escritura', function($app) { diff --git a/app/resources/routes/ventas/pies/bonos.php b/app/resources/routes/ventas/pies/bonos.php index 93b0970..8328bf3 100644 --- a/app/resources/routes/ventas/pies/bonos.php +++ b/app/resources/routes/ventas/pies/bonos.php @@ -2,5 +2,7 @@ use Incoviba\Controller\Ventas\Bonos; $app->group('/bono_pie', function($app) { + $app->get('/edit[/]', [Bonos::class, 'edit']); $app->get('/add[/]', [Bonos::class, 'add']); + $app->get('[/]', [Bonos::class, 'edit']); }); diff --git a/app/resources/views/ventas/pies/bonos/edit.blade.php b/app/resources/views/ventas/pies/bonos/edit.blade.php new file mode 100644 index 0000000..ab9c216 --- /dev/null +++ b/app/resources/views/ventas/pies/bonos/edit.blade.php @@ -0,0 +1,62 @@ +@extends('ventas.base') + +@section('venta_subtitle') + Editar Bono - Pie +@endsection + +@section('venta_content') +
+

Valor Promesa {{$format->ufs($venta->valor)}}

+ @if (isset($venta->formaPago()->pie)) +

Valor Anticipo {{$format->ufs($venta->formaPago()->pie->valor)}}

+ @endif +

Valor 10% {{$format->ufs($venta->valor * 0.1)}}

+
+
+
+ +
+
+ + +
+
+
+
+ +
+ +
UF
+
+
+ +
+@endsection + +@push('page_scripts') + +@endpush diff --git a/app/resources/views/ventas/show/forma_pago/bono_pie.blade.php b/app/resources/views/ventas/show/forma_pago/bono_pie.blade.php index 1201915..734f0dc 100644 --- a/app/resources/views/ventas/show/forma_pago/bono_pie.blade.php +++ b/app/resources/views/ventas/show/forma_pago/bono_pie.blade.php @@ -4,7 +4,7 @@ @if ($bonoPie !== null) - + @else diff --git a/app/src/Controller/API/Ventas/Bonos.php b/app/src/Controller/API/Ventas/Bonos.php index dce6194..8caf6d0 100644 --- a/app/src/Controller/API/Ventas/Bonos.php +++ b/app/src/Controller/API/Ventas/Bonos.php @@ -7,8 +7,9 @@ use Psr\Http\Message\ServerRequestInterface; use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Common\Ideal\Controller; use Incoviba\Controller\API\withJson; -use Incoviba\Service; +use Incoviba\Exception\ServiceAction\Update; use Incoviba\Repository; +use Incoviba\Service; class Bonos extends Controller { @@ -39,4 +40,25 @@ class Bonos extends Controller } catch (EmptyResult) {} return $this->withJson($response, $output); } + public function edit(ServerRequestInterface $request, ResponseInterface $response, Service\Venta $ventaService, + Service\Venta\BonoPie $bonoPieService, int $venta_id): ResponseInterface + { + $input = $request->getParsedBody(); + $output = [ + 'venta_id' => $venta_id, + 'input' => $input, + 'bono' => null, + 'success' => false, + ]; + try { + try { + $venta = $ventaService->getById($venta_id); + } catch (EmptyResult $exception) { + throw new Update(__CLASS__, $exception); + } + $output['bono'] = $bonoPieService->edit($venta->formaPago()->bonoPie, $input); + $output['success'] = true; + } catch (Update) {} + return $this->withJson($response, $output); + } } diff --git a/app/src/Controller/Ventas/Bonos.php b/app/src/Controller/Ventas/Bonos.php index 510f31a..d710ba1 100644 --- a/app/src/Controller/Ventas/Bonos.php +++ b/app/src/Controller/Ventas/Bonos.php @@ -4,14 +4,23 @@ 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 Bonos { + public function show(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $ventaService, int $venta_id): ResponseInterface + { + $venta = $ventaService->getById($venta_id); + return $view->render($response, 'ventas.pies.bonos.show', compact('venta')); + } public function add(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $ventaService, int $venta_id): ResponseInterface { $venta = $ventaService->getById($venta_id); return $view->render($response, 'ventas.pies.bonos.add', compact('venta')); } + public function edit(ServerRequestInterface $request, ResponseInterface $response, View $view, Service\Venta $ventaService, int $venta_id): ResponseInterface + { + $venta = $ventaService->getById($venta_id); + return $view->render($response, 'ventas.pies.bonos.edit', compact('venta')); + } } diff --git a/app/src/Repository/Venta/BonoPie.php b/app/src/Repository/Venta/BonoPie.php index dd575d1..70afbe8 100644 --- a/app/src/Repository/Venta/BonoPie.php +++ b/app/src/Repository/Venta/BonoPie.php @@ -85,4 +85,9 @@ class BonoPie extends Ideal\Repository ->where('venta.id = ?'); return $this->fetchOne($query, [$venta_id]); } + + public function filterData(array $data): array + { + return array_intersect_key($data, array_flip(['valor', 'pago'])); + } } diff --git a/app/src/Service/Valor.php b/app/src/Service/Valor.php index cebfbd6..573d275 100644 --- a/app/src/Service/Valor.php +++ b/app/src/Service/Valor.php @@ -1,8 +1,14 @@ getDateTime($date); + if (abs((float) $value - (int) $value) > 0 or $force) { + return round($value * $this->ufService->get($date)); + } + return (int) $value; + } + public function toUF(string $value, null|string|DateTimeInterface $date = null, bool $force = false): float + { + $date = $this->getDateTime($date); + if (abs((float) $value - (int) $value) > 0 and !$force) { + return (float) $value; + } + return $value / $this->ufService->get($date); + } + + protected function getDateTime(null|string|DateTimeInterface $date): DateTimeInterface + { + if ($date === null) { + return new DateTimeImmutable(); + } + if (is_string($date)) { + try { + return new DateTimeImmutable($date); + } catch (DateMalformedStringException) { + return new DateTimeImmutable(); + } + } + return $date; + } } diff --git a/app/src/Service/Venta/BonoPie.php b/app/src/Service/Venta/BonoPie.php index c0528f0..aee4a93 100644 --- a/app/src/Service/Venta/BonoPie.php +++ b/app/src/Service/Venta/BonoPie.php @@ -1,32 +1,76 @@ valorService->toPesos($this->valorService->clean($data['valor']), $data['fecha'] ?? null, true); + } + if (!array_key_exists('pago', $data)) { + $pago = $this->pagoService->add($data); + $data['pago'] = $pago->id; + } $filteredData = $this->bonoPieRepository->filterData($data); - if (!key_exists('pago', $filteredData)) { - $pago = $this->pagoService->add($filteredData); - $filteredData['pago'] = $pago->id; - } try { - $bono = $this->bonoPieRepository->fetchByPago($filteredData['pago']); + return $this->bonoPieRepository->fetchByPago($filteredData['pago']); } catch (EmptyResult) { + $filteredData['valor'] = $this->valorService->toUF($data['valor'], $data['fecha'] ?? null, true); $bono = $this->bonoPieRepository->create($filteredData); - $bono = $this->bonoPieRepository->save($bono); + try { + return $this->bonoPieRepository->save($bono); + } catch (PDOException $exception) { + throw new Create(__CLASS__, $exception); + } + } + } + + /** + * @param Model\Venta\BonoPie $bonoPie + * @param array $data + * @return Model\Venta\BonoPie + * @throws Update + */ + public function edit(Model\Venta\BonoPie $bonoPie, array $data): Model\Venta\BonoPie + { + if (array_key_exists('valor', $data)) { + $data['valor'] = $this->valorService->toPesos($this->valorService->clean($data['valor']), $data['fecha'] ?? null, true); + } + if (!array_key_exists('pago', $data)) { + $pago = $this->pagoService->edit($bonoPie->pago, $data); + $data['pago'] = $pago->id; + } + $data['valor'] = $this->valorService->toUF($data['valor'], $data['fecha'] ?? null, true); + $filteredData = $this->bonoPieRepository->filterData($data); + try { + return $this->bonoPieRepository->edit($bonoPie, $filteredData); + } catch (PDOException | EmptyResult $exception) { + throw new Update(__CLASS__, $exception); } - return $bono; } /** diff --git a/app/src/Service/Venta/Pago.php b/app/src/Service/Venta/Pago.php index 2f33edc..8d83b29 100644 --- a/app/src/Service/Venta/Pago.php +++ b/app/src/Service/Venta/Pago.php @@ -5,6 +5,7 @@ use DateTimeInterface; use DateTimeImmutable; use DateMalformedStringException; use Incoviba\Exception\ServiceAction\Read; +use Incoviba\Exception\ServiceAction\Update; use PDOException; use Incoviba\Common\Implement\Exception\EmptyResult; use Incoviba\Repository; @@ -130,15 +131,21 @@ class Pago public function add(array $data): Model\Venta\Pago { - if (!isset($data['uf'])) { + if (array_key_exists('fecha', $data)) { try { - $data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha'])); + $fecha = new DateTimeImmutable($data['fecha']); } catch (DateMalformedStringException) { - $data['uf'] = 0; + $fecha = new DateTimeImmutable(); + } + $data['fecha'] = $fecha->format('Y-m-d'); + if (!array_key_exists('uf', $data)) { + $data['uf'] = $this->ufService->get($fecha); } } + $data['valor'] = $this->valorService->toPesos($this->valorService->clean($data['valor']), $data['fecha']); + $filtered_data = $this->pagoRepository->filterData($data); - $filtered_data['valor'] = round($this->valorService->clean($filtered_data['valor'])); + $pago = $this->pagoRepository->create($filtered_data); $pago = $this->pagoRepository->save($pago); @@ -153,27 +160,34 @@ class Pago return $pago; } + /** + * @param Model\Venta\Pago $pago + * @param array $data + * @return Model\Venta\Pago + * @throws Update + */ public function edit(Model\Venta\Pago $pago, array $data): Model\Venta\Pago { if (array_key_exists('fecha', $data)) { try { - $data['fecha'] = (new DateTimeImmutable($data['fecha']))->format('Y-m-d'); + $fecha = new DateTimeImmutable($data['fecha']); } catch (DateMalformedStringException) { - $data['fecha'] = (new DateTimeImmutable())->format('Y-m-d'); + $fecha = new DateTimeImmutable(); } - } - if (array_key_exists('uf', $data)) { - try { - $data['uf'] = $this->ufService->get(new DateTimeImmutable($data['fecha'])); - } catch (DateMalformedStringException) { - $data['uf'] = 0; + $data['fecha'] = $fecha->format('Y-m-d'); + if (!array_key_exists('uf', $data)) { + $data['uf'] = $this->ufService->get($fecha); } } $filteredData = $this->pagoRepository->filterData($data); if (array_key_exists('valor', $filteredData)) { - $filteredData['valor'] = round($this->valorService->clean($filteredData['valor'])); + $filteredData['valor'] = $this->valorService->toPesos($this->valorService->clean($filteredData['valor']), $filteredData['fecha']); + } + try { + $pago = $this->pagoRepository->edit($pago, $filteredData); + } catch (PDOException | EmptyResult $exception) { + throw new Update(__CLASS__, $exception); } - $pago = $this->pagoRepository->edit($pago, $filteredData); $pago->currentEstado = $this->estadoPagoRepository->fetchCurrentByPago($pago->id); return $pago; }