/home/dvjjulio/softtrash/app/Poliza.php
<?php

namespace Trash;

use Illuminate\Database\Eloquent\Model;

class Poliza extends Model
{
    //
    protected $table = 'poliza';

    public function getPolizas($data = null){
        $where = $this->filterPoliza($data);
        $sql="SELECT p.id, p.cuenta_id, c.cuenta, p.subcuenta_id, sc.subcuenta, c.tipo, 
                p.nombre, p.referencia, p.status, p.fecha_vencimiento, p.importe, p.iva, p.total, 
                date(p.created_at) created_at
            FROM poliza p
            JOIN cuenta c ON c.id = p.cuenta_id
            JOIN subcuenta sc ON sc.id = p.subcuenta_id
            WHERE p.status_id = 1 $where";
        $data = \DB::select( \DB::raw( $sql ) );
        return $data;
      }

    public function filterPoliza($data) {
        $where = "";
        if ($data == null) return " AND date(p.created_at) = date(NOW()) ";
        if ($data['cuenta'] != 0){
            $where .= ' AND p.cuenta_id = '.$data['cuenta'].' ';
        }
        if ($data['subcuenta'] != 0){
            $where .= ' AND p.subcuenta_id = '.$data['subcuenta'].' ';
        }
        if ($data['tipo'] != ""){
            $where .= ' AND c.tipo = "'.$data['tipo'].'" ';
        }
        if ($data['pagado'] != ""){
            $where .= ' AND p.status = "'.$data['pagado'].'" ';
        }
        if (isset($data['fechaI']) && isset($data['fechaF']) && $data['fechaI'] != $data['fechaF'] || $data['fechaI'] == ""){
            $where .= ' AND p.created_at BETWEEN "'.$data['fechaI'].'" AND "'.$data['fechaF'].'"';
        }
        
        return $where;
    }  
}