109 lines
3.5 KiB
PHP
109 lines
3.5 KiB
PHP
<?php
|
|
namespace Incoviba\Repository\Venta\MediosPago\Toku;
|
|
|
|
use DateTimeImmutable;
|
|
use PDO;
|
|
use PDOException;
|
|
use Incoviba\Common\Define;
|
|
use Incoviba\Common\Ideal;
|
|
use Incoviba\Common\Implement;
|
|
use Incoviba\Model;
|
|
use Incoviba\Repository;
|
|
|
|
class Invoice extends Ideal\Repository
|
|
{
|
|
public function __construct(Define\Connection $connection, protected Repository\Venta\Cuota $cuotaRepository)
|
|
{
|
|
parent::__construct($connection);
|
|
}
|
|
|
|
public function getTable(): string
|
|
{
|
|
return 'toku_invoices';
|
|
}
|
|
public function create(?array $data = null): Model\Venta\MediosPago\Toku\Invoice
|
|
{
|
|
$map = (new Implement\Repository\MapperParser(['toku_id']))
|
|
->register('cuota_id', (new Implement\Repository\Mapper())
|
|
->setProperty('cuota')
|
|
->setFunction(function($data) {
|
|
return $this->cuotaRepository->fetchById($data['cuota_id']);
|
|
}));
|
|
return $this->parseData(new Model\Venta\MediosPago\Toku\Invoice(), $data, $map);
|
|
}
|
|
public function save(Define\Model $model): Model\Venta\MediosPago\Toku\Invoice
|
|
{
|
|
$model->id = $this->saveNew(
|
|
['cuota_id', 'toku_id', 'created_at'],
|
|
[$model->cuota->id, $model->toku_id, (new DateTimeImmutable())->format('Y-m-d H:i:s')]
|
|
);
|
|
return $model;
|
|
}
|
|
public function edit(Define\Model $model, array $new_data): Model\Venta\MediosPago\Toku\Invoice
|
|
{
|
|
return $this->update($model, ['cuota_id', 'toku_id', 'updated_at'], array_merge($new_data, ['updated_at' => (new DateTimeImmutable())->format('Y-m-d H:i:s')]));
|
|
}
|
|
|
|
/**
|
|
* @param int $cuota_id
|
|
* @return \Incoviba\Model\Venta\MediosPago\Toku\Invoice
|
|
* @throws Implement\Exception\EmptyResult
|
|
*/
|
|
public function fetchByCuota(int $cuota_id): Model\Venta\MediosPago\Toku\Invoice
|
|
{
|
|
$query = $this->connection->getQueryBuilder()
|
|
->select()
|
|
->from($this->getTable())
|
|
->where('cuota_id = :cuota_id');
|
|
return $this->fetchOne($query, compact('cuota_id'));
|
|
}
|
|
|
|
/**
|
|
* @param string $toku_id
|
|
* @return \Incoviba\Model\Venta\MediosPago\Toku\Invoice
|
|
* @throws Implement\Exception\EmptyResult
|
|
*/
|
|
public function fetchByTokuId(string $toku_id): Model\Venta\MediosPago\Toku\Invoice
|
|
{
|
|
$query = $this->connection->getQueryBuilder()
|
|
->select()
|
|
->from($this->getTable())
|
|
->where('toku_id = :toku_id');
|
|
return $this->fetchOne($query, compact('toku_id'));
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws Implement\Exception\EmptyResult
|
|
*/
|
|
public function fetchAllTokuIds(): array
|
|
{
|
|
$query = $this->connection->getQueryBuilder()
|
|
->select('toku_id')
|
|
->from($this->getTable());
|
|
try {
|
|
$statement = $this->connection->query($query);
|
|
} catch (PDOException $exception) {
|
|
throw new Implement\Exception\EmptyResult($query, $exception);
|
|
}
|
|
if ($statement->rowCount() === 0) {
|
|
throw new Implement\Exception\EmptyResult($query);
|
|
}
|
|
return $statement->fetchAll(PDO::FETCH_COLUMN);
|
|
}
|
|
|
|
/**
|
|
* @param string $toku_id
|
|
* @return void
|
|
* @throws PDOException
|
|
*/
|
|
public function removeByTokuId(string $toku_id): void
|
|
{
|
|
$query = $this->connection->getQueryBuilder()
|
|
->delete()
|
|
->from($this->getTable())
|
|
->where('toku_id = :toku_id');
|
|
$this->connection->execute($query, compact('toku_id'));
|
|
}
|
|
}
|