Files
oficial/app/src/Service/Venta/Pago.php

262 lines
8.6 KiB
PHP
Raw Normal View History

<?php
namespace Incoviba\Service\Venta;
2023-09-07 23:03:21 -03:00
use DateTimeInterface;
use DateTimeImmutable;
2025-03-03 14:57:22 -03:00
use DateMalformedStringException;
2025-05-10 12:39:31 -04:00
use Incoviba\Exception\ServiceAction\Create;
2025-03-03 14:57:22 -03:00
use Incoviba\Exception\ServiceAction\Read;
use Incoviba\Exception\ServiceAction\Update;
use PDOException;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Repository;
use Incoviba\Model;
2023-09-13 18:51:46 -03:00
use Incoviba\Service;
class Pago
{
2023-09-13 18:51:46 -03:00
public function __construct(
protected Repository\Venta\Pago $pagoRepository,
protected Repository\Venta\EstadoPago $estadoPagoRepository,
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository,
protected Service\UF $ufService,
protected Service\Valor $valorService
2023-09-13 18:51:46 -03:00
) {}
2023-09-07 23:03:21 -03:00
public function depositar(Model\Venta\Pago $pago, DateTimeInterface $fecha): bool
{
$tipo_estado = $this->tipoEstadoPagoRepository->fetchByDescripcion('depositado');
$data = [
'pago' => $pago->id,
'estado' => $tipo_estado->id,
2023-09-07 23:03:21 -03:00
'fecha' => $fecha->format('Y-m-d')
];
try {
$estado = $this->estadoPagoRepository->create($data);
$this->estadoPagoRepository->save($estado);
2024-10-31 17:15:51 -03:00
$pago = $this->process($this->pagoRepository->fetchById($pago->id));
$this->getUF($pago);
2023-09-07 23:03:21 -03:00
return true;
2025-05-15 10:06:00 -04:00
} catch (PDOException | EmptyResult) {
2023-09-07 23:03:21 -03:00
return false;
}
}
public function abonar(Model\Venta\Pago $pago, DateTimeInterface $fecha): bool
{
$tipo_estado = $this->tipoEstadoPagoRepository->fetchByDescripcion('abonado');
$data = [
'pago' => $pago->id,
'estado' => $tipo_estado->id,
'fecha' => $fecha->format('Y-m-d')
];
try {
$estado = $this->estadoPagoRepository->create($data);
$this->estadoPagoRepository->save($estado);
return true;
} catch (PDOException) {
return false;
}
}
public function devolver(Model\Venta\Pago $pago, DateTimeInterface $fecha): bool
{
$tipo_estado = $this->tipoEstadoPagoRepository->fetchByDescripcion('devuelto');
$data = [
'pago' => $pago->id,
'estado' => $tipo_estado->id,
'fecha' => $fecha->format('Y-m-d')
];
try {
2024-01-08 21:24:34 -03:00
$estado = $this->estadoPagoRepository->create($data);
$this->estadoPagoRepository->save($estado);
return true;
} catch (PDOException) {
return false;
}
}
public function anular(Model\Venta\Pago $pago, DateTimeInterface $fecha): bool
{
$tipo_estado = $this->tipoEstadoPagoRepository->fetchByDescripcion('anulado');
$data = [
'pago' => $pago->id,
'estado' => $tipo_estado->id,
'fecha' => $fecha->format('Y-m-d')
];
try {
$estado = $this->estadoPagoRepository->create($data);
$this->estadoPagoRepository->save($estado);
return true;
} catch (PDOException) {
return false;
}
}
public function getById(?int $pago_id): ?Model\Venta\Pago
{
if ($pago_id === null) {
return null;
}
$pago = $this->pagoRepository->fetchById($pago_id);
2023-10-19 18:20:37 -03:00
return $this->process($pago);
}
public function getByVenta(int $venta_id): array
{
return array_map([$this, 'process'], $this->pagoRepository->fetchByVenta($venta_id));
}
2023-09-07 23:03:21 -03:00
public function getPendientes(): array
{
return [];
}
public function getDepositados(): array
{
return [];
}
public function getRebotes(): array
{
return [];
}
2025-03-03 14:57:22 -03:00
/**
* @param int $venta_id
* @return Model\Venta\Pago
* @throws Read
*/
2024-03-13 14:38:44 -03:00
public function getDevolucionByVenta(int $venta_id): Model\Venta\Pago
{
2025-03-03 14:57:22 -03:00
try {
return $this->process($this->pagoRepository->fetchDevolucionByVenta($venta_id));
} catch (EmptyResult $exception) {
throw new Read(__CLASS__, $exception);
}
2024-03-13 14:38:44 -03:00
}
2023-09-13 18:51:46 -03:00
2025-05-10 12:39:31 -04:00
/**
* @param array $data
* @return Model\Venta\Pago
* @throws Create
*/
2023-09-13 18:51:46 -03:00
public function add(array $data): Model\Venta\Pago
{
if (array_key_exists('fecha', $data)) {
2025-02-24 14:02:12 -03:00
try {
$fecha = new DateTimeImmutable($data['fecha']);
2025-03-03 14:57:22 -03:00
} catch (DateMalformedStringException) {
$fecha = new DateTimeImmutable();
}
$data['fecha'] = $fecha->format('Y-m-d');
if (!array_key_exists('uf', $data)) {
$data['uf'] = $this->ufService->get($fecha);
2025-02-24 14:02:12 -03:00
}
2023-09-13 18:51:46 -03:00
}
$data['valor'] = $this->valorService->toPesos($this->valorService->clean($data['valor']), $data['fecha']);
$filtered_data = $this->pagoRepository->filterData($data);
2025-05-10 12:39:31 -04:00
try {
$pago = $this->pagoRepository->create($filtered_data);
$pago = $this->pagoRepository->save($pago);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
2023-09-13 18:51:46 -03:00
$tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado');
2025-05-10 12:39:31 -04:00
try {
$estado = $this->estadoPagoRepository->create([
'pago' => $pago->id,
'fecha' => $pago->fecha->format('Y-m-d'),
'estado' => $tipoEstado->id
]);
$estado = $this->estadoPagoRepository->save($estado);
} catch (PDOException $exception) {
throw new Create(__CLASS__, $exception);
}
2023-09-13 18:51:46 -03:00
$pago->currentEstado = $estado;
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)) {
2025-02-24 14:02:12 -03:00
try {
$fecha = new DateTimeImmutable($data['fecha']);
2025-03-03 14:57:22 -03:00
} catch (DateMalformedStringException) {
$fecha = new DateTimeImmutable();
2025-02-24 14:02:12 -03:00
}
$data['fecha'] = $fecha->format('Y-m-d');
if (!array_key_exists('uf', $data)) {
$data['uf'] = $this->ufService->get($fecha);
2025-02-24 14:02:12 -03:00
}
}
$filteredData = $this->pagoRepository->filterData($data);
2024-11-29 18:09:55 -03:00
if (array_key_exists('valor', $filteredData)) {
$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);
2024-11-29 18:09:55 -03:00
}
$pago->currentEstado = $this->estadoPagoRepository->fetchCurrentByPago($pago->id);
return $pago;
}
public function delete(Model\Venta\Pago $pago): bool
{
try {
$this->pagoRepository->remove($pago);
return true;
2025-03-03 14:57:22 -03:00
} catch (PDOException) {
return false;
}
}
2025-02-24 16:53:12 -03:00
public function updateEstado(Model\Venta\Pago $pago, array $data): Model\Venta\Pago
{
if ($pago->currentEstado->tipoEstadoPago->id === $data['estado']) {
return $pago;
}
$data['pago'] = $pago->id;
$estado = $this->estadoPagoRepository->create($data);
$this->estadoPagoRepository->save($estado);
return $this->process($this->pagoRepository->fetchById($pago->id));
}
2023-10-19 18:20:37 -03:00
protected function process($pago): Model\Venta\Pago
{
$pago->estados = $this->estadoPagoRepository->fetchByPago($pago->id);
$pago->currentEstado = $this->estadoPagoRepository->fetchCurrentByPago($pago->id);
2024-03-26 16:41:33 -03:00
if ($pago->uf === null or $pago->uf === 0.0) {
$pago->uf = $this->getUF($pago);
}
2024-01-08 13:16:12 -03:00
return $pago;
}
protected function getUF(Model\Venta\Pago $pago): ?float
{
2024-03-26 16:41:33 -03:00
if (in_array($pago->currentEstado->tipoEstadoPago->descripcion, ['depositado', 'abonado'])
2023-11-28 21:38:24 -03:00
and $pago->currentEstado->fecha <= new DateTimeImmutable()) {
2024-03-26 16:41:33 -03:00
$uf = $this->ufService->get($pago->currentEstado->fecha);
if ($pago->uf === $uf) {
return $uf;
}
2024-01-08 13:16:12 -03:00
if ($uf !== 0.0) {
try {
$this->pagoRepository->edit($pago, ['uf' => $uf]);
} catch (EmptyResult) {}
2024-01-08 13:16:12 -03:00
return $uf;
2023-10-19 18:20:37 -03:00
}
2024-01-08 13:16:12 -03:00
} elseif ($pago->uf === 0.0) {
try {
$this->pagoRepository->edit($pago, ['uf' => null]);
} catch (EmptyResult) {}
2024-01-08 13:16:12 -03:00
return null;
2023-10-19 18:20:37 -03:00
}
2024-01-08 13:16:12 -03:00
return $pago->uf;
2023-10-19 18:20:37 -03:00
}
}