<?php

namespace App\Models\Presupuestos;

class ImportadorModel extends \App\Models\BaseModel
{
    protected $table = 'pedido_libro';
    protected $primaryKey = 'id';
    protected $DBGroup = 'old_erp';

    public function getClientList(){

        $db = \Config\Database::connect($this->DBGroup); // Conectar a olderp
        $builder = $db->table('customers');
        $builder->select('id, name');
        $builder->where('deleted_at', NULL);
        $query = $builder->get();
        return $query->getResultObject();
    }


    public function getPresupuestosList($clienteId, $search = ""){
        
        $db = \Config\Database::connect($this->DBGroup); // Conectar a olderp
        $builder = $db->table('pedido_libro');
        $builder->select('id as id, CONCAT(id, " - ", titulo) as name');
        $builder->where('customer_id', $clienteId);
        $builder->whereIn('estado', ['finalizado', 'validado']);
        $builder->where('deleted_at', NULL);
        $builder->orderBy('updated_at', 'DESC');
        
        return empty($search) ? 
            $builder->get()->getResultObject() : 
            $builder->groupStart()->
                like('titulo', $search)->
                orLike('id', $search)-> 
                groupEnd()->get()->getResultObject();
    }

    public function getPresupuestoForImport($id){
        
        $db = \Config\Database::connect($this->DBGroup); // Conectar a olderp
        $builder = $db->table('pedido_libro t1')
            ->select('t1.paginas, t1.tirada, t1.papel_formato_personalizado, 
                t1.papel_formato_ancho as papel_formato_personalizado_ancho, 
                t1.papel_formato_alto as papel_formato_personalizado_alto, 
                t2.ancho as papel_formato_ancho, t2.alto as papel_formato_alto, 
                t1.ferro as ferro,t1.ferro_digital as ferro_digital, t1.marcapaginas as marcapaginas')
            ->join('papel_formato t2', 't1.papel_formato_id = t2.id', 'left')
            ->where('t1.id', $id)
            ->where('t1.deleted_at', NULL);
        $query = $builder->get();
        $datosGenerales =  $query->getRow();

        $builder = $db->table('pedido_libro_manipulado')
            ->select('nombre')
            ->where('pedido_libro_id', $id);
        $query = $builder->get();
        $manipulados = $query->getResultObject();

        $builder = $db->table('pedido_libro_acabado')
            ->select('nombre')
            ->where('pedido_libro_id', $id);
        $query = $builder->get();
        $acabados = $query->getResultObject();

        $builder = $db->table('pedido_libro_preimpresion')
            ->select('nombre')
            ->where('pedido_libro_id', $id)
            ->where('nombre', 'Prototipo');
        $query = $builder->countAllResults();
        if($query > 0){
            $datosGenerales->prototipo = 1;
        }
        else{
            $datosGenerales->prototipo = 0;
        }

        $builder = $db->table('pedido_libro_linea t1')
            ->select('t1.tipo as tipo, t1.hq as hq, t1.paginas as paginas, t1.papel_id as papel_id, 
            t2.nombre as papel_nombre, t2.code as papel_code, t1.gramaje as gramaje, t1.rotativa_pag_color as rotativa_pag_color, 
            t1.rotativa_impresion as rotativa_impresion, t1.solapas_ancho as solapas_ancho')
            ->join('papel_generico t2', 't1.papel_id = t2.id', 'left')
            ->where('pedido_libro_id', $id);
        $query = $builder->get();
        $lineas = $query->getResultObject();

        
        return [
            'datosGenerales' => $datosGenerales,
            'manipulados' => $manipulados,
            'acabados' => $acabados,
            'lineas' => $lineas
        ];

    }

    public function getDatosGuardar($id){
        
        $db = \Config\Database::connect($this->DBGroup); // Conectar a olderp
        $db = \Config\Database::connect($this->DBGroup); // Conectar a olderp
        $builder = $db->table('pedido_libro t1')
            ->select('
                t1.titulo, t1.autor, t1.coleccion, t1.isbn, t1.customer_reference as referencia_cliente,
                t1.envios_recogecliente as entrega_taller, t1.comentarios as comentarios_cliente, t1.comentarios_safekat, t1.comentarios_pdf')
            ->join('papel_formato t2', 't1.papel_formato_id = t2.id', 'left')
            ->where('t1.id', $id)
            ->where('t1.deleted_at', NULL);
        $query = $builder->get();
        $datosGenerales =  $query->getRow();
        return $query->getRow();
    }

    public function getDirecciones($id){
        
        $db = \Config\Database::connect($this->DBGroup); // Conectar a olderp
        $builder = $db->table('pedido_libro_envios t1')
            ->select('t1.ejemplares as unidades, t1.att, t1.email, t1.direccion, t1.pais, 
                t2.nombre as provincia, t1.ciudad as municipio, t1.cp, t1.telefono')
            ->join(('provincias t2'), 't1.provincia = t2.code', 'left')
            ->where('t1.pedido_libro_id', $id);
        $query = $builder->get();
        return $query->getResultObject();
    }



}
