Files
oficial/app/src/Repository/Venta/MediosPago/Toku/Customer.php

113 lines
3.7 KiB
PHP
Raw Normal View History

2025-05-08 11:13:06 -04:00
<?php
2025-05-10 12:38:14 -04:00
namespace Incoviba\Repository\Venta\MediosPago\Toku;
2025-05-08 11:13:06 -04:00
use DateTimeImmutable;
2025-06-09 12:44:42 -04:00
use PDO;
use PDOException;
2025-05-08 11:13:06 -04:00
use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Model;
use Incoviba\Service;
class Customer extends Ideal\Repository
{
public function __construct(Define\Connection $connection, protected Service\Persona $personaService)
{
parent::__construct($connection);
}
public function getTable(): string
{
2025-05-12 13:57:43 -04:00
return 'toku_customers';
2025-05-08 11:13:06 -04:00
}
2025-05-10 12:38:14 -04:00
public function create(?array $data = null): Model\Venta\MediosPago\Toku\Customer
2025-05-08 11:13:06 -04:00
{
$map = (new Implement\Repository\MapperParser(['toku_id']))
->register('rut', (new Implement\Repository\Mapper())
->setProperty('persona')
->setFunction(function($data) {
$rut = (int) substr($data['rut'], 0, -1);
return $this->personaService->getById($rut);
})
);
2025-05-10 12:38:14 -04:00
return $this->parseData(new Model\Venta\MediosPago\Toku\Customer(), $data, $map);
2025-05-08 11:13:06 -04:00
}
2025-05-10 12:38:14 -04:00
public function save(Define\Model $model): Model\Venta\MediosPago\Toku\Customer
2025-05-08 11:13:06 -04:00
{
$model->id = $this->saveNew(
['rut', 'toku_id', 'created_at'],
2025-05-13 20:10:46 -04:00
[implode('', [$model->persona->rut, $model->persona->digito]), $model->toku_id, (new DateTimeImmutable())->format('Y-m-d H:i:s')]
2025-05-08 11:13:06 -04:00
);
return $model;
}
2025-05-10 12:38:14 -04:00
public function edit(Define\Model $model, array $new_data): Model\Venta\MediosPago\Toku\Customer
2025-05-08 11:13:06 -04:00
{
2025-05-13 20:10:46 -04:00
return $this->update($model, ['rut', 'toku_id', 'updated_at'], array_merge($new_data, ['updated_at' => (new DateTimeImmutable())->format('Y-m-d H:i:s')]));
2025-05-08 11:13:06 -04:00
}
/**
* @param string $rut
2025-06-03 16:52:01 -04:00
* @return Model\Venta\MediosPago\Toku\Customer
2025-05-08 11:13:06 -04:00
* @throws Implement\Exception\EmptyResult
*/
2025-05-10 12:38:14 -04:00
public function fetchByRut(string $rut): Model\Venta\MediosPago\Toku\Customer
2025-05-08 11:13:06 -04:00
{
if (str_contains($rut, '-')) {
$rut = str_replace('-', '', $rut);
}
if (str_contains($rut, '.')) {
$rut = str_replace('.', '', $rut);
}
$rut = strtoupper($rut);
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('rut = :rut');
return $this->fetchOne($query, compact('rut'));
}
2025-05-09 18:05:40 -04:00
/**
* @param string $toku_id
2025-06-03 16:52:01 -04:00
* @return Model\Venta\MediosPago\Toku\Customer
2025-05-09 18:05:40 -04:00
* @throws Implement\Exception\EmptyResult
*/
2025-05-10 12:38:14 -04:00
public function fetchByTokuId(string $toku_id): Model\Venta\MediosPago\Toku\Customer
2025-05-09 18:05:40 -04:00
{
$query = $this->connection->getQueryBuilder()
->select()
->from($this->getTable())
->where('toku_id = :toku_id');
return $this->fetchOne($query, compact('toku_id'));
}
2025-06-09 12:44:42 -04:00
/**
* @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);
}
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'));
}
2025-05-08 11:13:06 -04:00
}