48 lines
1.7 KiB
PHP
48 lines
1.7 KiB
PHP
![]() |
<?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;
|
||
|
}
|
||
|
}
|