package org.adempiere.bseni;
// Librerias Basicas de Java
import java.awt.BorderLayout;
import java.awt.ScrollPane;
import java.awt.image.BufferedImage;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
// Librerias para usar los paneles
import org.zkoss.image.AImage;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zkex.zul.Borderlayout;
import org.zkoss.zkex.zul.Center;
import org.zkoss.zkex.zul.North;
import org.zkoss.zkex.zul.South;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Image;
import org.zkoss.zul.Listitem;
import org.adempiere.webui.component.Button;
import org.adempiere.webui.component.ListModelTable;
import org.adempiere.webui.component.ListboxFactory;
import org.adempiere.webui.component.Panel;
import org.adempiere.webui.component.Tab;
import org.adempiere.webui.component.Tabbox;
import org.adempiere.webui.component.Tabpanel;
import org.adempiere.webui.component.Tabpanels;
import org.adempiere.webui.component.Tabs;
import org.adempiere.webui.component.WListbox;
import org.adempiere.webui.panel.ADForm;
import org.compiere.apps.form.GenForm;
import org.compiere.minigrid.IDColumn;
import org.compiere.minigrid.IMiniTable;
import org.compiere.minigrid.MiniTable;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair;
import org.compiere.util.Msg;
// Librerias para usar las gráficas
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.encoders.EncoderUtil;
import org.jfree.chart.encoders.ImageFormat;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
// Componentes Básicos
import org.adempiere.webui.component.Label;
import org.adempiere.webui.component.Listbox;
import org.adempiere.webui.event.WTableModelEvent;
/**
*
* @author ingenierojosefrancisco@gmail.com
* @created Miércoles 28 de Mayo del 2014
*
*/
public class WConsultaGeneralVentas extends ADForm implements EventListener
{
/**
*
*/
private static final long serialVersionUID = -1560265477256494112L;
// Paneles Principales
private Panel principalPanel = new Panel();
private Panel mnuPanel = new Panel();
private Panel tbPanel = new Panel();
private Tabbox tabbedPane = new Tabbox();
private Tab tabVentas = new Tab();
private Tab tabSucursales = new Tab();
private Tab tabProductos = new Tab();
private Tab tabClientes = new Tab();
private Tabs tabs = new Tabs();
private Tabpanel ventas = new Tabpanel();
private Tabpanel sucursales = new Tabpanel();
private Tabpanel productos = new Tabpanel();
private Tabpanel clientes = new Tabpanel();
private Tabpanels tabpanels = new Tabpanels();
// VENTAS
// -- Paneles Ventas
private Panel graficaPanel = new Panel();
private Panel tablaPanel = new Panel();
// -- Menu Ventas
private int p_anio = 2014;
private int p_mes = 1;
private String p_tipo_venta = "E";
private int p_sucursal = 0;
private String sucursal = "";
private Label lblSeleccionSucursal = new Label("Sucursal: ");
private Listbox cmbSeleccionSucursal = new Listbox();
private Label lblSeleccionAnio = new Label("Seleccione el año de búsqueda: ");
private Listbox cmbSeleccionAnio = new Listbox();
private Label lblTipoVenta = new Label(" Tipo de Venta: ");
private Listbox cmbTipoVenta = new Listbox();
// -- Gráfica Ventas
AImage image;
Image myImage;
private Panel sucursalPanel = new Panel();
/**
* Default constructor.
*/
public WConsultaGeneralVentas()
{
super();
}
@Override
protected void initForm()
{
/***********************************************************/
Borderlayout layoutCenter = new Borderlayout();
layoutCenter.setParent(this);
layoutCenter.setHeight("5%");
layoutCenter.setWidth("100%");
layoutCenter.setStyle("background-color: transparent");
Center center = new Center();
center.setParent(layoutCenter);
center.setFlex(true);
center.setStyle("background-color: transparent");
mnuPanel.setParent(center);
this.LlenarListaSucursales(this.cmbSeleccionSucursal);
this.cmbSeleccionSucursal.setMold("select");
this.cmbSeleccionSucursal.setRows(1);
this.cmbSeleccionSucursal.setSelectedIndex(0);
this.cmbSeleccionSucursal.addEventListener(Events.ON_SELECT, this);
this.LlenarListaAnios(this.cmbSeleccionAnio);
this.cmbSeleccionAnio.setMold("select");
this.cmbSeleccionAnio.setRows(1);
this.cmbSeleccionAnio.setSelectedIndex(0);
this.cmbSeleccionAnio.addEventListener(Events.ON_SELECT, this);
this.cmbTipoVenta.appendItem("VENTAS PRIVADAS", "E");
this.cmbTipoVenta.appendItem("VENTAS INSTITUCIONALES/CONVENIOS", "F");
this.cmbTipoVenta.setMold("select");
this.cmbTipoVenta.setRows(1);
this.cmbTipoVenta.setSelectedIndex(0);
this.cmbTipoVenta.addEventListener(Events.ON_SELECT, this);
mnuPanel.appendChild(this.lblSeleccionSucursal);
mnuPanel.appendChild(this.cmbSeleccionSucursal);
mnuPanel.appendChild(this.lblSeleccionAnio);
mnuPanel.appendChild(this.cmbSeleccionAnio);
mnuPanel.appendChild(this.lblTipoVenta);
mnuPanel.appendChild(this.cmbTipoVenta);
/***********************************************************/
Borderlayout layoutTb = new Borderlayout();
layoutTb.setParent(this);
layoutTb.setHeight("95%");
layoutTb.setWidth("100%");
layoutTb.setStyle("background-color: transparent");
Center centerTb = new Center();
centerTb.setParent(layoutTb);
centerTb.setFlex(true);
centerTb.setStyle("background-color: transparent");
tbPanel.setParent(centerTb);
tabVentas.addEventListener(Events.ON_SELECT, this);
tabVentas.setLabel("VENTAS");
tabSucursales.addEventListener(Events.ON_SELECT, this);
tabSucursales.setLabel("SUCURSALES");
tabProductos.addEventListener(Events.ON_SELECT, this);
tabProductos.setLabel("PRODUCTOS");
tabClientes.addEventListener(Events.ON_SELECT, this);
tabClientes.setLabel("CLIENTES");
tabs.appendChild(tabVentas);
tabs.appendChild(tabSucursales);
tabs.appendChild(tabProductos);
tabs.appendChild(tabClientes);
tabpanels.setWidth("100%");
tabpanels.appendChild(ventas);
tabpanels.appendChild(sucursales);
tabpanels.appendChild(productos);
tabpanels.appendChild(clientes);
tabbedPane.setWidth("100%");
tabbedPane.setHeight("100%");
tabbedPane.appendChild(tabs);
tabbedPane.appendChild(tabpanels);
tbPanel.appendChild(tabbedPane);
/***********************************************************/
Borderlayout layoutNorth = new Borderlayout();
layoutNorth.setParent(this);
layoutNorth.setHeight("50%");
layoutNorth.setWidth("100%");
layoutNorth.setStyle("background-color: transparent");
North north = new North();
north.setParent(layoutNorth);
north.setFlex(true);
north.setStyle("background-color: transparent");
graficaPanel.setParent(north);
ventas.appendChild(graficaPanel);
/***********************************************************/
this.setTitle("Consulta General de Ventas");
this.setBorder("normal");
this.setClosable(false);
this.setWidth("100%");
this.setHeight("100%");
this.setSizable(false);
this.setMaximizable(false);
this.GraficarVentas();
return;
}
private void LlenarListaSucursales(Listbox cmb)
{
String id_compania = String.valueOf(Env.getAD_Client_ID(Env.getCtx()));
String sql = "select distinct s.ad_org_id, s.name from ad_org s where s.ad_client_id = " + id_compania + " order by s.name;";
PreparedStatement ps = null;
ResultSet rs = null;
int k = 0;
try
{
ps = DB.prepareStatement(sql, null);
rs = ps.executeQuery();
while(rs.next())
{
k++;
p_sucursal = rs.getInt(1);
sucursal = rs.getString(2);
cmb.appendItem(sucursal, p_sucursal);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
DB.close(rs, ps);
rs = null;
ps = null;
}
}
private void LlenarListaAnios(Listbox cmb)
{
String id_compania = String.valueOf(Env.getAD_Client_ID(Env.getCtx()));
String sql = "select distinct extract(year from f.dateinvoiced)::int from c_invoice f where f.ad_client_id = "
+ id_compania + "and f.issotrx = 'Y' and f.docstatus = 'CO' order by 1 desc";
PreparedStatement ps = null;
ResultSet rs = null;
int k = 0, anio = 2014;
try
{
ps = DB.prepareStatement(sql, null);
rs = ps.executeQuery();
while(rs.next())
{
k++;
anio = rs.getInt(1);
cmb.appendItem(String.valueOf(anio), anio);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
DB.close(rs, ps);
rs = null;
ps = null;
}
}
private void GraficarVentas()
{
TimeSeriesCollection dataset = this.ObtenerDatosVentas();
// Generate the graph
JFreeChart chart = ChartFactory.createTimeSeriesChart(
null,
// Title
"Meses",
// x-axis Label
"Monto C$",
// y-axis Label
dataset,
// Dataset
true,
// Show Legend
true,
// Use tooltips
false
// Configure chart to generate URLs?
);
// Creando gráfica de ventas
BufferedImage bi = chart.createBufferedImage(900, 350, BufferedImage.TRANSLUCENT , null);
try
{
byte[] bytes = EncoderUtil.encode(bi, ImageFormat.PNG, true);
image = new AImage("", bytes);
myImage = new Image();
myImage.setContent(image);
graficaPanel.appendChild(myImage);
}
catch (Exception e)
{
// TODO: handle exception
}
}
public String ObtenerTituloVentas()
{
String titulo = "VENTAS INSTITUCIONALES/CONVENIOS " + sucursal.toUpperCase() + " ";
if(p_tipo_venta.equals("E"))
titulo = "VENTAS PRIVADAS " + sucursal.toUpperCase() + " ";
else
titulo = "VENTAS INSTITUCIONALES/CONVENIOS " + sucursal.toUpperCase() + " ";
return titulo;
}
public TimeSeriesCollection ObtenerDatosVentas()
{
String id_compania = String.valueOf(Env.getAD_Client_ID(Env.getCtx()));
String sql = "select vg.num_mes, vg.venta_nio from rep_ventas_generales(" + id_compania + ", " + p_sucursal + ", " + p_anio + ", '" + p_tipo_venta + "') vg;";
PreparedStatement ps = null;
ResultSet rs = null;
int k = 0, mes = 0;
double monto = 0.00;
TimeSeries series = new TimeSeries(ObtenerTituloVentas() + p_anio, Day.class);
try
{
ps = DB.prepareStatement(sql, null);
rs = ps.executeQuery();
while(rs.next())
{
k++;
mes = rs.getInt(1);
monto = rs.getDouble(2);
series.add(new Day(1, mes, p_anio), monto);
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
DB.close(rs, ps);
rs = null;
ps = null;
}
// Add the series to your data set
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(series);
return dataset;
}
public void onEvent(Event e) throws Exception
{
Object source = e.getTarget();
if(source == this.cmbSeleccionSucursal)
this.RefrescarDiagramaVentas();
if (source == this.cmbSeleccionAnio)
this.RefrescarDiagramaVentas();
if (source == this.cmbTipoVenta)
this.RefrescarDiagramaVentas();
}
public void RefrescarDiagramaVentas()
{
Listitem listitem1 = this.cmbSeleccionSucursal.getSelectedItem();
p_sucursal = Integer.parseInt(String.valueOf(listitem1.getValue()));
sucursal = listitem1.getLabel();
Listitem listitem2 = this.cmbSeleccionAnio.getSelectedItem();
p_anio = Integer.parseInt(String.valueOf(listitem2.getValue()));
Listitem listitem3 = this.cmbTipoVenta.getSelectedItem();
p_tipo_venta = String.valueOf(listitem3.getValue());
image = null;
this.graficaPanel.removeChild(this.myImage);
this.GraficarVentas();
}
}
No hay comentarios:
Publicar un comentario