Files
oficial/app/src/Controller/API/Ventas/Pagos.php

233 lines
8.6 KiB
PHP
Raw Normal View History

2023-12-21 21:06:38 -03:00
<?php
namespace Incoviba\Controller\API\Ventas;
use Exception;
2023-12-21 21:06:38 -03:00
use DateTimeImmutable;
use Incoviba\Model\Venta\Pago;
2023-12-21 21:06:38 -03:00
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Controller\API\withJson;
use Incoviba\Repository;
use Incoviba\Service;
class Pagos
{
use withJson;
2024-03-26 16:41:33 -03:00
public function get(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService,
Service\Format $formatService, int $pago_id): ResponseInterface
{
$output = [
'pago_id' => $pago_id,
'pago' => null
];
try {
$output['pago'] = json_decode(json_encode($pagoService->getById($pago_id)), JSON_OBJECT_AS_ARRAY);
$output['pago']['valor_uf'] = $formatService->ufs($output['pago']['valor_uf']);
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param Repository\Venta\Pago $pagoRepository
* @param int $pago_id
* @return ResponseInterface
* @throws Exception
*/
2023-12-21 21:06:38 -03:00
public function edit(ServerRequestInterface $request, ResponseInterface $response, Repository\Venta\Pago $pagoRepository, int $pago_id): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'input' => $body,
'pago_id' => $pago_id,
2024-10-23 08:39:11 -03:00
'pago' => null,
'editado' => false
2023-12-21 21:06:38 -03:00
];
try {
$pago = $pagoRepository->fetchById($pago_id);
if (isset($body['fecha'])) {
$fecha = new DateTimeImmutable($body['fecha']);
$body['fecha'] = $fecha->format('Y-m-d');
}
$output['pago'] = $pagoRepository->edit($pago, $body);
2024-10-23 08:39:11 -03:00
$output['editado'] = true;
2023-12-21 21:06:38 -03:00
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
2024-11-06 21:28:36 -03:00
public function estado(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Pago $pagoService,
Repository\Venta\EstadoPago $estadoPagoRepository,
int $pago_id): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'pago_id' => $pago_id,
'input' => $body,
'pago' => null,
'editado' => false
];
try {
$pago = $pagoService->getById($pago_id);
$output['pago'] = $pago;
$estado = $estadoPagoRepository->create([
'pago' => $pago->id,
'estado' => $body['estado'],
'fecha' => $body['fecha']
]);
$estadoPagoRepository->save($estado);
$output['editado'] = true;
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param Service\Venta\Pago $pagoService
* @param Service\Format $formatService
* @param int $pago_id
* @return ResponseInterface
* @throws Exception
*/
2024-03-26 16:41:33 -03:00
public function depositar(ServerRequestInterface $request, ResponseInterface $response,
Service\Venta\Pago $pagoService, Service\Format $formatService, int $pago_id): ResponseInterface
2023-12-21 21:06:38 -03:00
{
2024-01-08 21:24:34 -03:00
$body = $request->getParsedBody();
$output = [
'pago_id' => $pago_id,
'input' => $body,
2024-03-26 16:41:33 -03:00
'pago' => null,
2024-01-08 21:24:34 -03:00
'depositado' => false
];
try {
$pago = $pagoService->getById($pago_id);
2024-03-26 16:41:33 -03:00
$fecha = new DateTimeImmutable($body['fecha']);
2024-01-08 21:24:34 -03:00
$output['depositado'] = $pagoService->depositar($pago, $fecha);
2024-03-26 16:41:33 -03:00
$output['pago'] = json_decode(json_encode($pagoService->getById($pago_id)), JSON_OBJECT_AS_ARRAY);
$output['pago']['valor_uf'] = $formatService->ufs($output['pago']['valor_uf']);
2024-01-08 21:24:34 -03:00
} catch (EmptyResult) {}
return $this->withJson($response, $output);
2023-12-21 21:06:38 -03:00
}
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param Service\Venta\Pago $pagoService
* @param Service\Format $formatService
* @param int $pago_id
* @return ResponseInterface
* @throws Exception
*/
2024-03-26 16:41:33 -03:00
public function abonar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService,
Service\Format $formatService, int $pago_id): ResponseInterface
2023-12-21 21:06:38 -03:00
{
2024-01-08 21:24:34 -03:00
$body = $request->getParsedBody();
$output = [
'pago_id' => $pago_id,
'input' => $body,
2024-03-26 16:41:33 -03:00
'pago' => null,
2024-01-08 21:24:34 -03:00
'abonado' => false
];
try {
$pago = $pagoService->getById($pago_id);
2024-03-26 16:41:33 -03:00
$fecha = new DateTimeImmutable($body['fecha']);
2024-01-08 21:24:34 -03:00
$output['abonado'] = $pagoService->abonar($pago, $fecha);
2024-03-26 16:41:33 -03:00
$output['pago'] = json_decode(json_encode($pagoService->getById($pago_id)), JSON_OBJECT_AS_ARRAY);
$output['pago']['valor_uf'] = $formatService->ufs($output['pago']['valor_uf']);
2024-01-08 21:24:34 -03:00
$output['input']['fecha'] = $fecha->format('d-m-Y');
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
/**
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param Service\Venta\Pago $pagoService
* @param int $pago_id
* @return ResponseInterface
* @throws Exception
*/
2024-01-08 21:24:34 -03:00
public function devolver(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService, int $pago_id): ResponseInterface
{
$body = $request->getParsedBody();
$output = [
'pago_id' => $pago_id,
'input' => $body,
'devuelto' => false
];
try {
$pago = $pagoService->getById($pago_id);
$fecha = new DateTimeImmutable($body->fecha);
$output['devuelto'] = $pagoService->devolver($pago, $fecha);
$output['input']['fecha'] = $fecha->format('d-m-Y');
} catch (EmptyResult) {}
return $this->withJson($response, $output);
2023-12-21 21:06:38 -03:00
}
public function para_pendientes(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
{
$pagos = $pagoService->getPendientes();
$pagos_pendientes = array_map(function(Pago $pago) {
return [
'id' => $pago->id
];
}, $pagos);
/*$pagos_pendientes = [];
2023-12-21 21:06:38 -03:00
foreach ($pagos as $pago) {
$pagos_pendientes []= [
'id' => $pago->id
];
}*/
2023-12-21 21:06:38 -03:00
return $this->withJson($response, ['pagos' => $pagos_pendientes, 'total' => count($pagos_pendientes)]);
}
public function para_abonar(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
{
$pagos = $pagoService->getDepositados();
$pagos_depositados = array_map(function(Pago $pago) {
return [
'id' => $pago->id
];
}, $pagos);
/*$pagos_depositados = [];
2023-12-21 21:06:38 -03:00
foreach ($pagos as $pago) {
$pagos_depositados []= [
'id' => $pago->id
];
}*/
2023-12-21 21:06:38 -03:00
return $this->withJson($response, ['pagos' => $pagos_depositados, 'total' => count($pagos_depositados)]);
}
public function rebotes(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService): ResponseInterface
{
$pagos = $pagoService->getRebotes();
$rebotes = array_map(function(Pago $pago) {
return [
'id' => $pago->id
];
}, $pagos);
/*$rebotes = [];
2023-12-21 21:06:38 -03:00
foreach ($pagos as $pago) {
$rebotes []= [
'id' => $pago->id
];
}*/
2023-12-21 21:06:38 -03:00
return $this->withJson($response, ['pagos' => $rebotes, 'total' => count($rebotes)]);
}
2024-01-08 21:24:34 -03:00
public function anular(ServerRequestInterface $request, ResponseInterface $response, Service\Venta\Pago $pagoService, int $pago_id): ResponseInterface
{
$fecha = new DateTimeImmutable();
$output = [
'pago_id' => $pago_id,
'fecha' => $fecha->format('d-m-Y'),
'anulado' => false
];
try {
$pago = $pagoService->getById($pago_id);
$output['anulado'] = $pagoService->anular($pago, $fecha);
} catch (EmptyResult) {}
return $this->withJson($response, $output);
}
2023-12-21 21:06:38 -03:00
}