<?php
namespace Incoviba\Repository\Venta;

use Incoviba\Common\Define;
use Incoviba\Common\Ideal;
use Incoviba\Common\Implement;
use Incoviba\Model;

class TipoPago extends Ideal\Repository
{
    public function __construct(Define\Connection $connection)
    {
        parent::__construct($connection);
        $this->setTable('tipo_pago');
    }

    public function create(?array $data = null): Model\Venta\TipoPago
    {
        $map = new Implement\Repository\MapperParser(['descripcion']);
        return $this->parseData(new Model\Venta\TipoPago(), $data, $map);
    }
    public function save(Define\Model $model): Model\Venta\TipoPago
    {
        $model->id = $this->saveNew(
            ['descripcion'],
            [$model->descripcion]
        );
        return $model;
    }
    public function edit(Define\Model $model, array $new_data): Model\Venta\TipoPago
    {
        return $this->update($model, ['descripcion'], $new_data);
    }

    public function fetchByDescripcion(string $descripcion): Model\Venta\TipoPago
    {
        $query = "SELECT * FROM `{$this->getTable()}` WHERE `descripcion` = ?";
        return $this->fetchOne($query, [$descripcion]);
    }
}
