Viewing file: Reportes.php (7.88 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class reportes extends CI_Controller {
function __construct() { parent::__construct(); $this->load->model('consultas_model'); /* Cargamos la base de datos */ $this->load->database(); $this->load->library('javascript'); /* Cargamos la libreria*/ $this->load->library('session'); /* Añadimos el helper al controlador */ $this->load->helper('url'); error_reporting(E_ALL ^ (E_NOTICE | E_WARNING)); } public function index() { $data = array( "contenido" => "reporte_view", "titulo" =>"Reporte" ); $this->load->view('template',$data); } public function reporteVentas() { $buscador = $this->input->post('comboCliente'); $idCliente=null; if($buscador !=null){ $idCliente=$this->consultas_model->consulta_clientesByName($buscador); } $data = array( "contenido" =>"reporte_ventas_view", "nombre"=>$buscador, "titulo" =>"REPORTE VENTAS" ); $this->load->view('template',$data); } public function reporteProductos() { $buscador = $this->input->post('comboCliente'); $idCliente=null; if($buscador !=null){ $idCliente=$this->consultas_model->consulta_clientesByName($buscador); } $data = array( "contenido" =>"reporte_productos", "nombre"=>$buscador, "filtro"=>$buscador, "titulo" =>"REPORTE DE PRODUCTOS MAS VENDIDOS (TOP 20)" ); $this->load->view('template',$data); } function to_excel(){ /* header('Content-type: application/x-msdownload'); //Filename es el nombre del archivo excel, en este caso webcomparte.doc header("Content-Disposition: attachment; filename=webcomparte.xls"); header("Pragma: no-cache"); header("Expires: 0"); $headers = ''; // just creating the var for field headers to append to below $data = ''; // just creating the var for field data to append to below $cabecera=['Nombre','Descripcion','Codigo','Precio Ingreso','Precio Venta','Vendidos','Categoria','Modelo','Stock','Imagen']; $query =$this->db->query("select b.nombre_producto,b.descripcion,b.numero,b.precio_ingreso,b.precio_venta,b.cantidad_vendida,c.categoria,a.modelo,d.stock,b.imagen from productos b left join modelos a ON a.id=b.id_modelo left join categorias_productos c ON c.id=b.id_categoria left join stock_productos d ON d.nombre_producto=b.nombre_producto"); if ($query->num_rows() == 0) { echo '<p>No hay datos para mostrar.</p>'; } else { echo "<table border=1>\n"; echo "<tr>"; foreach ($cabecera as $field) { echo "<td>".$field."</td>"; } echo "</tr>\n"; echo "<tr HEIGHT=130>"; $contador=0; foreach ($query->result()[0] as $row) { $contador++; if($contador =='10'){ echo "<td><img src='".base_url()."uploads/PIC_0116.JPG' WIDTH=130 HEIGHT=130></td>\n"; $contador=0; }else{ echo "<td>".$row."</td>"; }
} echo "</tr>\n"; }
//Si quieren agregar mas filas o columnas he aqui el momento echo "</table>\n";*/ $this->load->library('pHPExcel'); $PHPExcel = new PHPExcel(); $PHPExcel->getProperties()->setTitle("Productos Antonella"); //Título del Excel $campo_excel = 1; $letra = 'A'; $tipo_string = array(); $query ="select a.nombre_producto as nombre_producto,a.descripcion as descripcion,a.numero as numero, a.precio_ingreso as precio_ingreso, a.precio_venta as precio_venta,c.nombre as nombre,b.categoria as categoria, e.modelo as modelo,d.stock as stock,sum(f.cantidad) as cantidad_vendida from productos a left join categorias_productos b ON b.id=a.id_categoria left join detalle_pedido f ON f.nombre_producto=a.nombre_producto left join proveedores c ON c.id = a.id_proveedor left join stock_productos d ON d.nombre_producto = a.nombre_producto left join modelos e ON e.id = a.id_modelo group by 6,1 order by 10 desc;";
$query =$this->db->query($query); #Imprimo los títulos foreach($query->result() as $filas) { $columna_excel = '0'; foreach($filas AS $titulo=>$fila) { #Las columnas todas en String $PHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($columna_excel, $campo_excel, $titulo, PHPExcel_Cell_DataType::TYPE_STRING); #Si la columna la queremos en string la metemos en el array if ($titulo=='codigo' || $titulo=='refprov' || $titulo=='CODIGO_' || $titulo=='ean' || $titulo=='CODIGO') { $tipo_string[] = $columna_excel; //Esta columna será tipo String } $columna_excel++; $letra++; //Para saber cuantas columnas tengo (en letra) y al final adelante usar un loop para autoajustar el texto } $campo_excel++; break; } function tipo_string($columna, $tipo_string) { #Si la columna está en el array convertimos la celda a String if (empty($tipo_string)) return false; foreach($tipo_string AS $string) { if ($columna == $string) return true; } } #Imprimo los campos foreach($query->result() AS $filas) { $columna_excel = '0'; foreach($filas AS $fila) { if (tipo_string($columna_excel, $tipo_string)) { $PHPExcel->getActiveSheet()->setCellValueExplicitByColumnAndRow($columna_excel, $campo_excel, $fila, PHPExcel_Cell_DataType::TYPE_STRING); } else { $PHPExcel->getActiveSheet()->SetCellValueByColumnAndRow($columna_excel, $campo_excel, $fila); } $columna_excel++; } $campo_excel++; } #Ajusto ancho de las columnas al texto for ($col = 'A'; $col != $letra; $col++) { $PHPExcel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true); } $fecha = DATE('Y-m-d'); //Excel 2007 /* header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="'.$fecha.'_informe_'.$nombre.'.xlsx"'); header('Cache-Control: max-age=0'); $PHPExcel = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel2007'); */ //Excel 2003 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="'.$fecha.'_informe_productos.xls"'); header('Cache-Control: max-age=0'); $PHPExcel = PHPExcel_IOFactory::createWriter($PHPExcel, 'Excel5'); //Creamos el Archivo $PHPExcel->save('php://output'); } function to_excel2() { $filename='Listado Productos'; $headers = ''; // just creating the var for field headers to append to below $data = ''; // just creating the var for field data to append to below $cabecera=['Nombre','Descripcion','Codigo','Precio Ingreso','Precio Venta','Vendidos','Categoria','Modelo','Stock','Imagen']; $query =$this->db->query("select b.nombre_producto,b.descripcion,b.numero,b.precio_ingreso,b.precio_venta,b.cantidad_vendida,c.categoria,a.modelo,d.stock,e.nombre from productos b left join modelos a ON a.id=b.id_modelo left join proveedores e ON e.id=b.id_proveedor left join categorias_productos c ON c.id=b.id_categoria left join stock_productos d ON d.nombre_producto=b.nombre_producto group by 10 order by cantidad_vendida desc;"); if ($query->num_rows() == 0) { echo '<p>No hay datos para mostrar.</p>'; } else { foreach ($cabecera as $field) { $headers .= $field . "\t"; } foreach ($query->result() as $row) { $line = ''; $contador=0; foreach($row as $value) { $contador ++; if ((!isset($value)) OR ($value == "")) { $value = "\t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; if($contador ==10){ $contador=0; $value ="<table border=1><tr><td valign=bottom><img src='".base_url()."uploads/PIC_0116.JPG' style='width:200px; height:150px;'></td></tr></table>"; } } $line .= $value; } $data .= trim($line)."\n"; } $data = str_replace("\r","",$data); header("Content-type: application/x-msdownload"); header("Content-Disposition: attachment; filename=$filename.xls"); echo mb_convert_encoding("$headers\n$data",'utf-16','utf-8'); } } }
|