Files
oficial/app/src/Service/Venta/Pago.php
Juan Pablo Vial 98953cce42 FormaPago Service
2024-03-13 14:38:44 -03:00

179 lines
5.7 KiB
PHP

<?php
namespace Incoviba\Service\Venta;
use DateTimeInterface;
use DateTimeImmutable;
use Incoviba\Common\Implement\Exception\EmptyResult;
use Incoviba\Service\Money;
use PDOException;
use Incoviba\Repository;
use Incoviba\Model;
use Incoviba\Service;
class Pago
{
public function __construct(
protected Repository\Venta\Pago $pagoRepository,
protected Repository\Venta\EstadoPago $estadoPagoRepository,
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository,
protected Service\Money $moneyService
) {}
public function depositar(Model\Venta\Pago $pago, DateTimeInterface $fecha): bool
{
$tipo_estado = $this->tipoEstadoPagoRepository->fetchByDescripcion('depositado');
$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 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 {
$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);
return $this->process($pago);
}
public function getByVenta(int $venta_id): array
{
return array_map([$this, 'process'], $this->pagoRepository->fetchByVenta($venta_id));
}
public function getPendientes(): array
{
return [];
}
public function getDepositados(): array
{
return [];
}
public function getRebotes(): array
{
return [];
}
public function getDevolucionByVenta(int $venta_id): Model\Venta\Pago
{
return $this->process($this->pagoRepository->fetchDevolucionByVenta($venta_id));
}
public function add(array $data): Model\Venta\Pago
{
if (!isset($data['uf'])) {
$data['uf'] = $this->moneyService->getUF(new DateTimeImmutable($data['fecha']));
}
$fields = array_fill_keys([
'valor',
'banco',
'tipo',
'identificador',
'fecha',
'uf',
'pagador',
'asociado'
], 0);
$filtered_data = array_intersect_key($data, $fields);
$pago = $this->pagoRepository->create($filtered_data);
$pago = $this->pagoRepository->save($pago);
$tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado');
$estado = $this->estadoPagoRepository->create([
'pago' => $pago->id,
'fecha' => $pago->fecha->format('Y-m-d'),
'estado' => $tipoEstado->id
]);
$estado = $this->estadoPagoRepository->save($estado);
$pago->currentEstado = $estado;
return $pago;
}
public function delete(Model\Venta\Pago $pago): bool
{
try {
$this->pagoRepository->remove($pago);
return true;
} catch (EmptyResult|PDOException) {
return false;
}
}
protected function process($pago): Model\Venta\Pago
{
$pago->estados = $this->estadoPagoRepository->fetchByPago($pago->id);
$pago->currentEstado = $this->estadoPagoRepository->fetchCurrentByPago($pago->id);
$pago->uf = $this->getUF($pago);
return $pago;
}
protected function getUF(Model\Venta\Pago $pago): ?float
{
if (($pago->uf === null or $pago->uf === 0.0)
and $pago->currentEstado->tipoEstadoPago->descripcion === 'abonado'
and $pago->currentEstado->fecha <= new DateTimeImmutable()) {
$uf = $this->moneyService->getUF($pago->currentEstado->fecha);
if ($uf !== 0.0) {
$this->pagoRepository->edit($pago, ['uf' => $uf]);
return $uf;
}
} elseif ($pago->uf === 0.0) {
$this->pagoRepository->edit($pago, ['uf' => null]);
return null;
}
return $pago->uf;
}
}