Reorden Toku
This commit is contained in:
82
app/src/Repository/Venta/MediosPago/Toku/Customer.php
Normal file
82
app/src/Repository/Venta/MediosPago/Toku/Customer.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
namespace Incoviba\Repository\Venta\MediosPago\Toku;
|
||||
|
||||
use DateTimeImmutable;
|
||||
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
|
||||
{
|
||||
return 'teku_customers';
|
||||
}
|
||||
|
||||
public function create(?array $data = null): Model\Venta\MediosPago\Toku\Customer
|
||||
{
|
||||
$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);
|
||||
})
|
||||
);
|
||||
return $this->parseData(new Model\Venta\MediosPago\Toku\Customer(), $data, $map);
|
||||
}
|
||||
public function save(Define\Model $model): Model\Venta\MediosPago\Toku\Customer
|
||||
{
|
||||
$model->id = $this->saveNew(
|
||||
['rut', 'toku_id', 'created_at'],
|
||||
[implode('', [$model->persona->rut, $model->persona->digito]), $model->toku_id, (new DateTimeImmutable())->format('Y-m-d H:i:s.u')]
|
||||
);
|
||||
return $model;
|
||||
}
|
||||
public function edit(Define\Model $model, array $new_data): Model\Venta\MediosPago\Toku\Customer
|
||||
{
|
||||
return $this->update($model, ['rut', 'toku_id', 'updated_at'], array_merge($new_data, ['updated_at' => (new DateTimeImmutable())->format('Y-m-d H:i:s.u')]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $rut
|
||||
* @return \Incoviba\Model\Venta\MediosPago\Toku\Customer
|
||||
* @throws Implement\Exception\EmptyResult
|
||||
*/
|
||||
public function fetchByRut(string $rut): Model\Venta\MediosPago\Toku\Customer
|
||||
{
|
||||
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'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $toku_id
|
||||
* @return \Incoviba\Model\Venta\MediosPago\Toku\Customer
|
||||
* @throws Implement\Exception\EmptyResult
|
||||
*/
|
||||
public function fetchByTokuId(string $toku_id): Model\Venta\MediosPago\Toku\Customer
|
||||
{
|
||||
$query = $this->connection->getQueryBuilder()
|
||||
->select()
|
||||
->from($this->getTable())
|
||||
->where('toku_id = :toku_id');
|
||||
return $this->fetchOne($query, compact('toku_id'));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user