2023-07-28 16:22:20 -04:00
|
|
|
<?php
|
|
|
|
namespace Incoviba\Repository\Venta;
|
|
|
|
|
|
|
|
use Incoviba\Common\Ideal;
|
|
|
|
use Incoviba\Common\Define;
|
2023-08-08 23:53:49 -04:00
|
|
|
use Incoviba\Common\Implement;
|
2023-07-28 16:22:20 -04:00
|
|
|
use Incoviba\Model;
|
2023-08-08 23:53:49 -04:00
|
|
|
use Incoviba\Service;
|
2023-07-28 16:22:20 -04:00
|
|
|
|
|
|
|
class Credito extends Ideal\Repository
|
|
|
|
{
|
2023-08-08 23:53:49 -04:00
|
|
|
public function __construct(Define\Connection $connection, protected Service\Venta\Pago $pagoService)
|
2023-07-28 16:22:20 -04:00
|
|
|
{
|
|
|
|
parent::__construct($connection);
|
|
|
|
$this->setTable('credito');
|
|
|
|
}
|
|
|
|
|
2023-09-13 18:51:46 -03:00
|
|
|
public function create(?array $data = null): Model\Venta\Credito
|
2023-07-28 16:22:20 -04:00
|
|
|
{
|
2023-08-08 23:53:49 -04:00
|
|
|
$map = (new Implement\Repository\MapperParser())
|
|
|
|
->register('pago', (new Implement\Repository\Mapper())
|
|
|
|
->setFunction(function($data) {
|
|
|
|
return $this->pagoService->getById($data['pago']);
|
|
|
|
}));
|
2023-07-28 16:22:20 -04:00
|
|
|
return $this->parseData(new Model\Venta\Credito(), $data, $map);
|
|
|
|
}
|
2023-09-13 18:51:46 -03:00
|
|
|
public function save(Define\Model $model): Model\Venta\Credito
|
2023-07-28 16:22:20 -04:00
|
|
|
{
|
|
|
|
$model->id = $this->saveNew(
|
|
|
|
['banco', 'valor', 'fecha', 'uf', 'abonado', 'fecha_abono', 'pago'],
|
2023-09-13 18:51:46 -03:00
|
|
|
[$model->pago->banco?->id, $model->valor ?? (($model->pago->uf > 0) ? $model->pago->valor / $model->pago->uf : null), $model->pago->fecha->format('Y-m-d'), $model->pago->uf, null, null, $model->pago->id]
|
2023-07-28 16:22:20 -04:00
|
|
|
);
|
|
|
|
return $model;
|
|
|
|
}
|
2023-09-13 18:51:46 -03:00
|
|
|
public function edit(Define\Model $model, array $new_data): Model\Venta\Credito
|
2023-07-28 16:22:20 -04:00
|
|
|
{
|
|
|
|
return $this->update($model, ['banco', 'valor', 'fecha', 'uf', 'abonado', 'fecha_abono', 'pago'], $new_data);
|
|
|
|
}
|
2024-01-08 17:33:42 -03:00
|
|
|
|
|
|
|
public function fetchByValue(int $value): array
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select()
|
|
|
|
->from($this->getTable())
|
|
|
|
->where('valor = ?');
|
|
|
|
return $this->fetchMany($query, [$value]);
|
|
|
|
}
|
|
|
|
public function fetchByPago(int $pago_id): Model\Venta\Credito
|
|
|
|
{
|
|
|
|
$query = $this->connection->getQueryBuilder()
|
|
|
|
->select()
|
|
|
|
->from($this->getTable())
|
|
|
|
->where('pago = ?');
|
|
|
|
return $this->fetchOne($query, [$pago_id]);
|
|
|
|
}
|
2023-07-28 16:22:20 -04:00
|
|
|
}
|