"t1.id", 1 => "t1.numero", 2 => "t1.fecha_factura_at", 3 => "t2.nombre", 4 => "t1.base", 5 => "t1.total", 6 => "t1.pendiente", 7 => "t1.creditoAsegurado", 8 => "t1.estado", 9 => "t1.estado_pago", 10 => "t4.nombre", 11 => "DAFEDIFF(days, NOW(), t3.fecha_vencimiento_at)", ]; // Lista de columnas basada en los campos de la tabla, para asignación masiva protected $allowedFields = [ 'pedido_id', 'factura_rectificada_id', 'factura_rectificativa_id', 'cliente_id', 'serie_id', 'numero', 'estado', 'estado_pago', 'fecha_factura_at', 'notas', 'base', 'total', 'pendiente', 'total_pagos', 'creditoAsegurado', 'cliente_nombre', 'cliente_address', 'cliente_cif', 'cliente_pais', 'cliente_cp', 'cliente_ciudad', 'cliente_provincia', 'created_at', 'updated_at', 'deleted_at', 'user_created_id', 'user_updated_id' ]; protected $returnType = "App\Entities\Facturas\FacturaEntity"; protected $useTimestamps = true; protected $useSoftDeletes = true; protected $createdField = "created_at"; protected $updatedField = "updated_at"; public static $labelField = "id"; public function getResource(string $search = "") { $builder = $this->db ->table($this->table . " t1") ->select( "t1.id AS id, t1.numero AS numero, DATE_FORMAT(t1.fecha_factura_at, '%d/%m/%Y') AS fecha_factura_at, t2.nombre AS cliente, t1.base AS base, t1.total AS total, t1.pendiente AS pendiente, t1.creditoAsegurado AS creditoAsegurado, t1.estado AS estado, t1.estado_pago AS estado_pago, GROUP_CONCAT(DISTINCT t4.nombre ORDER BY t4.nombre ASC SEPARATOR ', ') AS forma_pago, DATE_FORMAT(MIN(CASE WHEN t3.fecha_vencimiento_at != '0000-00-00 00:00:00' THEN t3.fecha_vencimiento_at ELSE NULL END), '%d/%m/%Y') AS vencimiento, t2.vencimiento AS dias_vencimiento" ); $builder->join("clientes t2", "t2.id = t1.cliente_id", "left"); $builder->join("facturas_pagos t3", "t3.factura_id = t1.id", "left"); $builder->join("formas_pago t4", "t3.forma_pago_id = t4.id", "left"); $builder->where("t1.deleted_at IS NULL"); $builder->groupBy("t1.id"); // Agrupa por id de la factura return empty($search) ? $builder : $builder ->groupStart() ->like("t1.id", $search) ->orLike("t1.id", $search) ->groupEnd(); } public function getCantidadLineaPedidoFacturada($linea_pedido_id) { $builder = $this->db ->table("facturas_pedidos_lineas t1") ->select("SUM(t1.cantidad) AS cantidad") ->where("t1.pedido_linea_id", $linea_pedido_id); return $builder->get()->getRow()->cantidad; } public function deleteFacturasLineasPedido($factura_id, $pedido_linea_id, $cantidad) { $this->db->table("facturas_pedidos_lineas") ->where("factura_id", $factura_id) ->where("pedido_linea_id", $pedido_linea_id) ->where("cantidad", $cantidad) ->delete(); } }