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

125 lines
4.0 KiB
PHP
Raw Normal View History

<?php
namespace Incoviba\Service\Venta;
2023-09-07 23:03:21 -03:00
use DateTimeInterface;
use DateTimeImmutable;
2023-09-13 18:51:46 -03:00
use Incoviba\Service\Money;
use PDOException;
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\Money $moneyService
) {}
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);
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 getById(int $pago_id): Model\Venta\Pago
{
$pago = $this->pagoRepository->fetchById($pago_id);
$pago->estados = $this->estadoPagoRepository->fetchByPago($pago_id);
$pago->currentEstado = $this->estadoPagoRepository->fetchCurrentByPago($pago_id);
2023-09-13 18:51:46 -03:00
if (($pago->uf === null or $pago->uf === 0.0) and $pago->fecha < new DateTimeImmutable()) {
$pago->uf = $this->moneyService->getUF($pago->fecha);
if ($pago->uf !== 0.0) {
$this->pagoRepository->edit($pago, ['uf' => $pago->uf]);
}
}
return $pago;
}
2023-09-07 23:03:21 -03:00
public function getPendientes(): array
{
return [];
}
public function getDepositados(): array
{
return [];
}
public function getRebotes(): array
{
return [];
}
2023-09-13 18:51:46 -03:00
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;
}
}