134 lines
4.6 KiB
PHP
134 lines
4.6 KiB
PHP
<?php
|
|
namespace Incoviba\Service\Venta;
|
|
|
|
use PDOException;
|
|
use DateTimeImmutable;
|
|
use DateInterval;
|
|
use IntlDateFormatter;
|
|
use Psr\Log\LoggerInterface;
|
|
use Incoviba\Common\Ideal;
|
|
use Incoviba\Common\Implement\Exception\EmptyResult;
|
|
use Incoviba\Exception\ServiceAction\Create;
|
|
use Incoviba\Repository;
|
|
use Incoviba\Model;
|
|
|
|
class Cuota extends Ideal\Service
|
|
{
|
|
public function __construct(
|
|
LoggerInterface $logger,
|
|
protected Repository\Venta\Cuota $cuotaRepository,
|
|
protected Pago $pagoService,
|
|
protected Repository\Venta\TipoPago $tipoPagoRepository
|
|
) {
|
|
parent::__construct($logger);
|
|
}
|
|
|
|
public function pendientes(): array
|
|
{
|
|
$cuotas = $this->cuotaRepository->fetchPendientes();
|
|
$cuotas_pendientes = [];
|
|
$today = new DateTimeImmutable();
|
|
$formatter = new IntlDateFormatter('es_ES');
|
|
$formatter->setPattern('EEEE dd');
|
|
foreach ($cuotas as $cuota) {
|
|
$date = new DateTimeImmutable($cuota['fecha']);
|
|
$day = clone $date;
|
|
$weekday = $date->format('N');
|
|
if ($weekday > 5) {
|
|
$diff = 7 - $weekday + 1;
|
|
$day = $day->add(new DateInterval("P{$diff}D"));
|
|
}
|
|
$cuotas_pendientes []= [
|
|
'id' => $cuota['cuota_id'],
|
|
'venta_id' => $cuota['venta_id'],
|
|
'Proyecto' => $cuota['Proyecto'],
|
|
'Departamento' => $cuota['Departamento'],
|
|
'Valor' => $cuota['Valor'],
|
|
'Dia' => $formatter->format($day),
|
|
'Numero' => $cuota['Numero'],
|
|
'Propietario' => $cuota['Propietario'],
|
|
'Banco' => $cuota['Banco'],
|
|
'Fecha Cheque' => $date->format('d-m-Y'),
|
|
'Vencida' => $today->diff($date)->days,
|
|
'Fecha ISO' => $date->format('Y-m-d')
|
|
];
|
|
}
|
|
return $cuotas_pendientes;
|
|
}
|
|
public function depositadas(): array
|
|
{
|
|
$cuotas = $this->cuotaRepository->fetchDepositadas();
|
|
$cuotas_depositadas = [];
|
|
$today = new DateTimeImmutable();
|
|
$formatter = new IntlDateFormatter('es_ES');
|
|
$formatter->setPattern('EEEE dd');
|
|
foreach ($cuotas as $cuota) {
|
|
$date = new DateTimeImmutable($cuota['fecha']);
|
|
$deposito = new DateTimeImmutable($cuota['Fecha Depositada']);
|
|
$cuotas_depositadas []= [
|
|
'id' => $cuota['cuota_id'],
|
|
'venta_id' => $cuota['venta_id'],
|
|
'Proyecto' => $cuota['Proyecto'],
|
|
'Departamento' => $cuota['Departamento'],
|
|
'Valor' => $cuota['Valor'],
|
|
'Numero' => $cuota['Numero'],
|
|
'Propietario' => $cuota['Propietario'],
|
|
'Banco' => $cuota['Banco'],
|
|
'Fecha Cheque' => $date->format('d-m-Y'),
|
|
'Fecha ISO' => $date->format('Y-m-d'),
|
|
'Fecha Depositada' => $deposito->format('d-m-Y')
|
|
];
|
|
}
|
|
return $cuotas_depositadas;
|
|
}
|
|
public function getByPie(int $pie_id): array
|
|
{
|
|
try {
|
|
return $this->cuotaRepository->fetchByPie($pie_id);
|
|
} catch (EmptyResult) {
|
|
return [];
|
|
}
|
|
}
|
|
public function getVigenteByPie(int $pie_id): array
|
|
{
|
|
return $this->cuotaRepository->fetchVigenteByPie($pie_id);
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @return Model\Venta\Cuota
|
|
* @throws Create
|
|
*/
|
|
public function add(array $data): Model\Venta\Cuota
|
|
{
|
|
try {
|
|
$tipoPago = $this->tipoPagoRepository->fetchByDescripcion('cheque');
|
|
} catch (EmptyResult $exception) {
|
|
throw new Create(__CLASS__, $exception);
|
|
}
|
|
$fields = array_flip([
|
|
'fecha',
|
|
'banco',
|
|
'valor',
|
|
'identificador'
|
|
]);
|
|
$filtered_data = array_intersect_key($data, $fields);
|
|
|
|
$pago_data = array_merge($filtered_data, ['tipo' => $tipoPago->id]);
|
|
$pago = $this->pagoService->add($pago_data);
|
|
|
|
$data['pago'] = $pago->id;
|
|
$data['estado'] = $pago->currentEstado->tipoEstadoPago->id;
|
|
$filtered_data = $this->cuotaRepository->filterData($data);
|
|
$mapped_data = $filtered_data;
|
|
$mapped_data['valor_$'] = $mapped_data['valor'];
|
|
unset($mapped_data['valor']);
|
|
try {
|
|
$cuota = $this->cuotaRepository->create($mapped_data);
|
|
return $this->cuotaRepository->save($cuota);
|
|
} catch (PDOException $exception) {
|
|
throw new Create(__CLASS__, $exception);
|
|
}
|
|
}
|
|
}
|