2023-09-12

This commit is contained in:
Juan Pablo Vial
2023-09-13 18:51:46 -03:00
parent fa15da1ee2
commit 0cd357b6cb
47 changed files with 1225 additions and 102 deletions

View File

@ -0,0 +1,47 @@
<?php
namespace Incoviba\Service\Venta;
use DateTimeImmutable;
use Incoviba\Repository;
use Incoviba\Model;
use Incoviba\Service\Money;
class Credito
{
public function __construct(
protected Repository\Venta\Credito $creditoRepository,
protected Repository\Venta\Pago $pagoRepository,
protected Repository\Venta\TipoPago $tipoPagoRepository,
protected Repository\Venta\EstadoPago $estadoPagoRepository,
protected Repository\Venta\TipoEstadoPago $tipoEstadoPagoRepository,
protected Money $moneyService
) {}
public function add(array $data): Model\Venta\Credito
{
$fecha = new DateTimeImmutable($data['fecha']);
$uf = $data['uf'] ?? $this->moneyService->getUF($fecha);
$tipoPago = $this->tipoPagoRepository->fetchByDescripcion('carta de resguardo');
$pago = $this->addPago(['fecha' => $fecha->format('Y-m-d'), 'valor' => $data['valor'] * $uf, 'uf' => $uf, 'tipo' => $tipoPago->id]);
$credito = $this->creditoRepository->create([
'valor' => $data['valor'],
'fecha' => $fecha->format('Y-m-d'),
'pago' => $pago->id
]);
return $this->creditoRepository->save($credito);
}
protected function addPago(array $data): Model\Venta\Pago
{
$pago = $this->pagoRepository->create($data);
$pago = $this->pagoRepository->save($pago);
$tipoEstado = $this->tipoEstadoPagoRepository->fetchByDescripcion('no pagado');
$data = [
'pago' => $pago->id,
'fecha' => $pago->fecha->format('Y-m-d'),
'estado' => $tipoEstado->id
];
$estado = $this->estadoPagoRepository->create($data);
$this->estadoPagoRepository->save($estado);
return $pago;
}
}