Example usage for org.hibernate.criterion Restrictions eq

List of usage examples for org.hibernate.criterion Restrictions eq

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions eq.

Prototype

public static SimpleExpression eq(String propertyName, Object value) 

Source Link

Document

Apply an "equal" constraint to the named property

Usage

From source file:AdminSystemClasses.CustomerInfoGenerator.java

/**
 * A thing that generates values for autofill info for a BankCustomer
 * //from w  w w .j  av  a2  s  .  c o  m
 * @param cus BankCustomer to be autofilled
 * @return BankAccount generated and now associated to cus
 * @throws HibernateException 
 */
public static BankAccount generate(BankCustomer cus) throws HibernateException {
    Random rand = new Random(System.currentTimeMillis());
    Integer cusId;
    Session s = bank.DB.getSession();
    Criteria cr;
    List l;

    //cus.setId(1000);
    do {
        cusId = rand.nextInt(100000000);
        cr = s.createCriteria(BankCustomer.class);
        cr.add(Restrictions.eq("id", cusId));
        l = cr.list();
    } while (!l.isEmpty());

    cus.setId(cusId);

    return generateAccount(cus);
}

From source file:AdminSystemClasses.CustomerInfoGenerator.java

/**
 * A thing that generates a BankAccount for a BankCustomer
 * I made this a separate method because it would be useful to
 * have if a customer could have multiple accounts.
 * //from  ww  w. j  a va2s . c o m
 * @param cus BankCustomer to generate BankAccount for
 * @return BankAccount generated and now associated to cus
 * @throws HibernateException 
 */
private static BankAccount generateAccount(BankCustomer cus) throws HibernateException {
    Random rand = new Random(System.currentTimeMillis());
    Integer c1, c2, acctId, pin;
    String card;
    BankAccount acct;
    Session s = bank.DB.getSession();
    Criteria cr;
    List l;

    do {
        acctId = rand.nextInt(100000000);
        cr = s.createCriteria(BankAccount.class);
        cr.add(Restrictions.eq("accountid", acctId));
        l = cr.list();
    } while (!l.isEmpty());

    do {
        c1 = rand.nextInt(100000000);
        c2 = rand.nextInt(100000000);
        card = String.format("%08d", c1) + String.format("%08d", c2);
        cr = s.createCriteria(BankAccount.class);
        cr.add(Restrictions.eq("cardnumber", card));
        l = cr.list();
    } while (!l.isEmpty());

    pin = rand.nextInt(10000);

    acct = new BankAccount(acctId, cus, new BigDecimal(0), String.format("%04d", pin), card);

    cus.addBankAccount(acct);
    return acct;
}

From source file:AdminSystemClasses.EditATM.java

@Override
public String execute() {
    String msg = null;/* w  w w .  j a v a  2 s .co m*/
    Session sf = DB.getSession();
    Transaction tx = sf.beginTransaction();
    // atm = (BankATM)sf.load(BankATM.class, getAtmId());
    Criteria cr = sf.createCriteria(BankATM.class);
    cr.add(Restrictions.eq("id", getAtmId()));
    if (cr.list().size() > 0) {
        atm = cr.list();

        msg = "success";
    } else {
        msg = "failed";
    }

    return msg;

}

From source file:ajax.getProduct.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//www .  java  2s. co  m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response)
//            throws ServletException, IOException {
//        try (PrintWriter out = response.getWriter()) {
//            String pcode = request.getParameter("pcode");
//            if (pcode != null && pcode.equals("")) {
//                Session con = FactoryManager.getSessionFactory().openSession();
//                Criteria cri = con.createCriteria(Products.class);
//                cri.add(Restrictions.eq("prodCode", pcode));
//                Products p = (Products) cri.list().get(0);
//
//                String resptext = p.getItmCode() + "," + p.getBrand().getBrandName() + "," + p.getCategory().getCatName();
//                out.write(resptext);
////                System.out.println(resptext);
//            }
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try (PrintWriter out = resp.getWriter()) {
        String pcode = req.getParameter("pcode");
        if (pcode != null && !pcode.equals("")) {
            Session con = FactoryManager.getSessionFactory().openSession();
            Criteria cri = con.createCriteria(Products.class);
            cri.add(Restrictions.eq("prodCode", pcode));
            Products p = (Products) cri.list().get(0);

            String resptext = p.getItmCode() + "-" + p.getItmName() + "-" + p.getBrand().getBrandName() + "-"
                    + p.getCategory().getCatName();
            out.write(resptext);
        } else {
            out.write("0");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:akvelon.domain.dao.UsersDAOImpl.java

@Override
public Users getById(int id) {
    Session session = HibernateUtil.getSessionFactory().openSession();

    Criteria crit = session.createCriteria(Users.class).add(Restrictions.eq("id", id));
    Users u = (Users) crit.uniqueResult();
    session.close();/*from   w w w.j  av  a 2s  . co  m*/
    return u;
}

From source file:alma.acs.tmcdb.TestPojosPersistence.java

License:Open Source License

public void testCriteriaAPI() throws Exception {

    createDB();//from  www.  ja v a 2s . c o  m

    try {
        createConfigurationComputerAndTwoNetworkDevices();

        Configuration config = (Configuration) hibernateUtil.getList(Configuration.class).iterator().next();
        assertNotNull(config);

        // Now we test that using the criteria API we can find our objects
        Criteria c = hibernateUtil.getSession().createCriteria(NetworkDevice.class);
        c.add(Restrictions.eq("name", "wall-e"));
        assertEquals(2, c.list().size());

        c = hibernateUtil.getSession().createCriteria(NetworkDevice.class);
        c.add(Restrictions.eq("name", "wall-e"));
        c.add(Restrictions.eq("networkName", "wall-e.eso.org"));
        assertEquals(1, c.list().size());

        c = hibernateUtil.getSession().createCriteria(NetworkDevice.class);
        c.add(Restrictions.eq("configuration", config));
        assertEquals(3, c.list().size());

        c = hibernateUtil.getSession().createCriteria(Configuration.class);
        c.add(Restrictions.eq("configurationName", "rtobarConfig"));
        c.add(Restrictions.lt("creationTime", new Date()));
        assertEquals(1, c.list().size());

        try {
            c = hibernateUtil.getSession().createCriteria(Configuration.class);
            c.add(Restrictions.eq("configuratioName", "rtobarConfig")); // typo: should be configurationName
            c.list();
            fail("Should fail, property 'configuratioName' doesn't exist for Configuration objects");
        } catch (QueryException e) {
        }

    } finally {
        dropDB();
    }

}

From source file:Almacen.consultaPartidaOrden.java

public void busca(String tipo, String valor, String movimiento, String complemento, String complemento2) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {//from  w w w .  j  a v  a2s  .c o  m
        session.beginTransaction().begin();
        if (tipo.compareTo("Compaa") == 0) {
            Class[] types = new Class[] { java.lang.String.class, java.lang.String.class,
                    java.lang.String.class, java.lang.String.class, java.lang.String.class,
                    java.lang.String.class, java.lang.Double.class, java.lang.Double.class,
                    java.lang.Double.class };
            if (t_orden.getText().compareTo("") != 0) {
                if (movimiento.compareTo("Entrada") == 0) {
                    String[] columnas = new String[] { "Id", "N", "#", "N Parte", "Descripcin", "Medida",
                            "Autorizados", "X Surtir", "Entrada" };
                    Query query = session.createQuery(
                            "SELECT DISTINCT part FROM Partida part " + "where part.ordenByIdOrden.idOrden="
                                    + Integer.parseInt(t_orden.getText()) + " and part.so=" + true);
                    List partidas = query.list();

                    model = new MyModel(partidas.size(), columnas, types);
                    t_datos.setModel(model);
                    for (int a = 0; a < partidas.size(); a++) {
                        Partida par = (Partida) partidas.get(a);
                        Movimiento[] mov = (Movimiento[]) par.getMovimientos().toArray(new Movimiento[0]);
                        double entradas = 0, devoluciones = 0;
                        for (int b = 0; b < mov.length; b++) {
                            Almacen alm = mov[b].getAlmacen();
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 4)
                                entradas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 4)
                                devoluciones += mov[b].getCantidad();
                        }
                        double total_almacen = entradas - devoluciones;
                        double total = par.getCantidadAut() - total_almacen;
                        model.setValueAt(par.getIdPartida(), a, 0);
                        model.setValueAt(par.getIdEvaluacion(), a, 1);
                        model.setValueAt(par.getSubPartida(), a, 2);
                        if (par.getEjemplar() != null)
                            model.setValueAt(par.getEjemplar().getIdParte(), a, 3);
                        else
                            model.setValueAt("", a, 3);
                        model.setValueAt(par.getCatalogo().getNombre(), a, 4);
                        model.setValueAt(par.getMed(), a, 5);
                        model.setValueAt(par.getCantidadAut(), a, 6);
                        model.setValueAt(total, a, 7);
                        model.setValueAt(0.0d, a, 8);
                    }
                }
                if (movimiento.compareTo("Salida") == 0) {
                    String[] columnas = new String[] { "Id", "N", "#", "N Parte", "Descripcin", "Medida",
                            "Autorizados", "En almacen", "Devolucin" };
                    Query query = session.createQuery("SELECT DISTINCT part FROM Partida part "
                            + "LEFT JOIN FETCH part.movimientos movPart " + "LEFT JOIN movPart.almacen alm "
                            + "where alm.operacion=4 and part.ordenByIdOrden.idOrden="
                            + Integer.parseInt(t_orden.getText()));
                    List partidas = query.list();
                    model = new MyModel(partidas.size(), columnas, types);
                    t_datos.setModel(model);
                    for (int a = 0; a < partidas.size(); a++) {
                        Partida par = (Partida) partidas.get(a);
                        Movimiento[] mov = (Movimiento[]) session.createCriteria(Movimiento.class)
                                .add(Restrictions.eq("partida.idPartida", par.getIdPartida())).list()
                                .toArray(new Movimiento[0]);
                        double entradas = 0, devoluciones = 0, entregadas = 0, devueltas = 0;
                        ;
                        for (int b = 0; b < mov.length; b++) {
                            Almacen alm = mov[b].getAlmacen();
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 4)
                                entradas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 4)
                                devoluciones += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 5)
                                devueltas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 5)
                                entregadas += mov[b].getCantidad();
                        }
                        double total_Pedido = entradas - devoluciones;
                        double total_operario = entregadas - devueltas;
                        double total = total_Pedido - total_operario;
                        model.setValueAt(par.getIdPartida(), a, 0);
                        model.setValueAt(par.getIdEvaluacion(), a, 1);
                        model.setValueAt(par.getSubPartida(), a, 2);
                        if (par.getEjemplar() != null)
                            model.setValueAt(par.getEjemplar().getIdParte(), a, 3);
                        else
                            model.setValueAt("", a, 3);
                        model.setValueAt(par.getCatalogo().getNombre(), a, 4);
                        model.setValueAt(par.getMed(), a, 5);
                        model.setValueAt(par.getCantidadAut(), a, 6);
                        model.setValueAt(total, a, 7);
                        model.setValueAt(0.0d, a, 8);
                    }
                }
            }
        }
        if (tipo.compareTo("Operarios") == 0) {
            if (t_orden != null) {
                if (movimiento.compareTo("Entrada") == 0) {
                    Class[] types = new Class[] { java.lang.String.class, java.lang.String.class,
                            java.lang.String.class, java.lang.String.class, java.lang.String.class,
                            java.lang.String.class, java.lang.String.class, java.lang.Double.class,
                            java.lang.Double.class };
                    String[] columnas = new String[] { "Id", "Partida", "Tipo", "N Parte", "Descripcin",
                            "Medida", "Operario", "Entregadas" };
                    /*Query query = session.createQuery("SELECT DISTINCT part FROM Partida part "
                        + "LEFT JOIN FETCH part.movimientos movPart "
                        + "LEFT JOIN movPart.almacen alm "
                        + "where alm.operacion=5 and part.ordenByIdOrden.idOrden="+Integer.parseInt(t_orden.getText()));
                    List partidas = query.list();
                            
                    Query query1 = session.createQuery("SELECT DISTINCT partEx FROM PartidaExterna partEx "
                        + "LEFT JOIN FETCH partEx.movimientos movEx "
                        + "LEFT JOIN movEx.almacen alm "
                        + "where alm.operacion=5 and partEx.pedido.partida.ordenByIdOrden.idOrden="+Integer.parseInt(t_orden.getText()));
                    List partidasExternas = query1.list();*/
                    String q1 = "SELECT DISTINCT part FROM Partida part "
                            + "LEFT JOIN FETCH part.movimientos movPart " + "LEFT JOIN movPart.almacen alm "
                            + "where alm.operacion=5 and part.ordenByIdOrden.idOrden="
                            + Integer.parseInt(t_orden.getText()) + complemento;

                    /*String q2="SELECT DISTINCT partEx FROM PartidaExterna partEx "
                        + "LEFT JOIN FETCH partEx.movimientos movEx "
                        + "LEFT JOIN movEx.almacen alm "
                        + "where alm.operacion=5 and partEx.pedido.partida.ordenByIdOrden.idOrden="+Integer.parseInt(t_orden.getText())+complemento2;*/
                    String q2 = "SELECT DISTINCT partEx FROM PartidaExterna partEx "
                            + "LEFT JOIN FETCH partEx.movimientos movEx " + "LEFT JOIN movEx.almacen alm "
                            + "where alm.operacion in (3, 5) and partEx.pedido.orden.idOrden="
                            + Integer.parseInt(t_orden.getText()) + complemento2;

                    Query query = session.createQuery(q1);
                    List partidas = query.list();

                    Query query1 = session.createQuery(q2);
                    List partidasExternas = query1.list();

                    model = new MyModel(partidas.size() + partidasExternas.size(), columnas, types);
                    t_datos.setModel(model);

                    for (int a = 0; a < partidas.size(); a++) {
                        Partida par = (Partida) partidas.get(a);
                        Movimiento[] mov = (Movimiento[]) par.getMovimientos().toArray(new Movimiento[0]);
                        double entregadas = 0, devueltas = 0;
                        for (int b = 0; b < mov.length; b++) {
                            Almacen alm = mov[b].getAlmacen();
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 5)
                                devueltas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 5)
                                entregadas += mov[b].getCantidad();
                        }
                        double total = entregadas - devueltas;
                        model.setValueAt(par.getIdPartida(), a, 0);
                        model.setValueAt(par.getIdEvaluacion() + "-" + par.getSubPartida(), a, 1);
                        if (par.getPedido() == null)
                            model.setValueAt("COM.", a, 2);
                        else
                            model.setValueAt("PED.", a, 2);
                        if (par.getEjemplar() != null)
                            model.setValueAt(par.getEjemplar().getIdParte(), a, 3);
                        else
                            model.setValueAt("", a, 3);
                        model.setValueAt(par.getCatalogo().getNombre(), a, 4);
                        model.setValueAt(par.getMed(), a, 5);
                        model.setValueAt(total, a, 6);
                        model.setValueAt(0.0d, a, 7);
                    }

                    for (int a = 0; a < partidasExternas.size(); a++) {
                        PartidaExterna par = (PartidaExterna) partidasExternas.get(a);
                        Movimiento[] mov = (Movimiento[]) par.getMovimientos().toArray(new Movimiento[0]);
                        double entregadas = 0, devueltas = 0;
                        for (int b = 0; b < mov.length; b++) {
                            Almacen alm = mov[b].getAlmacen();
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 5)
                                devueltas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 5)
                                entregadas += mov[b].getCantidad();
                        }
                        double total = entregadas - devueltas;
                        model.setValueAt(par.getIdPartidaExterna(), a + partidas.size(), 0);
                        //model.setValueAt(par.getPedido().getPartida().getIdEvaluacion()+"-"+par.getPedido().getPartida().getSubPartida(), a+partidas.size(), 1);
                        model.setValueAt("-", a + partidas.size(), 1);
                        model.setValueAt("ADI.", a + partidas.size(), 2);
                        if (par.getNoParte() != null)
                            model.setValueAt(par.getNoParte(), a + partidas.size(), 3);
                        else
                            model.setValueAt("", a + partidas.size(), 3);
                        //model.setValueAt(par.getPedido().getPartida().getCatalogo().getNombre()+"/"+par.getDescripcion(), a+partidas.size(), 4);
                        model.setValueAt(par.getDescripcion(), a + partidas.size(), 4);
                        model.setValueAt(par.getUnidad(), a + partidas.size(), 5);
                        model.setValueAt(total, a + partidas.size(), 6);
                        model.setValueAt(0.0d, a + partidas.size(), 7);
                    }
                }
                if (movimiento.compareTo("Salida") == 0) {
                    Class[] types = new Class[] { java.lang.String.class, java.lang.String.class,
                            java.lang.String.class, java.lang.String.class, java.lang.String.class,
                            java.lang.String.class, java.lang.Double.class, java.lang.Double.class,
                            java.lang.Double.class, java.lang.Boolean.class };
                    String[] columnas = new String[] { "Id", "Partida", "Tipo", "N Parte", "Descripcin",
                            "Medida", "Existencias", "Operario", "Entregadas", "Solicita" };
                    /*Query query = session.createQuery("SELECT DISTINCT part FROM Partida part "
                        + "LEFT JOIN FETCH part.movimientos movPart "
                        + "LEFT JOIN movPart.almacen alm "
                        + "where part.ordenByIdOrden.idOrden="+Integer.parseInt(t_orden.getText())
                        + " and alm!="+null);
                    List partidas = query.list();
                            
                    Query query1 = session.createQuery("SELECT DISTINCT partEx FROM PartidaExterna partEx "
                        + "LEFT JOIN FETCH partEx.movimientos movEx "
                        + "LEFT JOIN movEx.almacen alm "
                        + "where alm.operacion in (3, 5) and partEx.pedido.partida.ordenByIdOrden.idOrden="+Integer.parseInt(t_orden.getText()));
                    List partidasExternas = query1.list();*/

                    String q1 = "SELECT DISTINCT part FROM Partida part "
                            + "LEFT JOIN FETCH part.movimientos movPart " + "LEFT JOIN movPart.almacen alm "
                            + "where part.ordenByIdOrden.idOrden=" + Integer.parseInt(t_orden.getText())
                            + " and alm!=" + null + complemento;

                    /*String q2="SELECT DISTINCT partEx FROM PartidaExterna partEx "
                        + "LEFT JOIN FETCH partEx.movimientos movEx "
                        + "LEFT JOIN movEx.almacen alm "
                        + "where alm.operacion in (3, 5) and partEx.pedido.partida.ordenByIdOrden.idOrden="+Integer.parseInt(t_orden.getText())+complemento2;*/
                    String q2 = "SELECT DISTINCT partEx FROM PartidaExterna partEx "
                            + "LEFT JOIN FETCH partEx.movimientos movEx " + "LEFT JOIN movEx.almacen alm "
                            + "where alm.operacion in (3, 5) and partEx.pedido.orden.idOrden="
                            + Integer.parseInt(t_orden.getText()) + complemento2;

                    Query query = session.createQuery(q1);
                    List partidas = query.list();

                    Query query1 = session.createQuery(q2);
                    List partidasExternas = query1.list();

                    model = new MyModel(partidas.size() + partidasExternas.size(), columnas, types);
                    t_datos.setModel(model);
                    for (int a = 0; a < partidas.size(); a++) {
                        Partida par = (Partida) partidas.get(a);
                        Movimiento[] mov = (Movimiento[]) par.getMovimientos().toArray(new Movimiento[0]);
                        double entradas = 0, devoluciones = 0, entregadas = 0, devueltas = 0;
                        for (int b = 0; b < mov.length; b++) {
                            Almacen alm = mov[b].getAlmacen();
                            //Entradas por pedido interno
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 1)
                                entradas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 1)
                                devoluciones += mov[b].getCantidad();
                            //entrada por compaia
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 4)
                                entradas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 4)
                                devoluciones += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 5)
                                devueltas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 5)
                                entregadas += mov[b].getCantidad();
                        }
                        double total_Pedido = entradas - devoluciones;
                        double total_operario = entregadas - devueltas;
                        double total = total_Pedido - total_operario;
                        model.setValueAt(par.getIdPartida(), a, 0);
                        model.setValueAt(par.getIdEvaluacion() + "-" + par.getSubPartida(), a, 1);
                        if (par.getPedido() == null)
                            model.setValueAt("COM.", a, 2);
                        else
                            model.setValueAt("PED.", a, 2);
                        if (par.getEjemplar() != null)
                            model.setValueAt(par.getEjemplar().getIdParte(), a, 3);
                        else
                            model.setValueAt("", a, 3);
                        model.setValueAt(par.getCatalogo().getNombre(), a, 4);
                        model.setValueAt(par.getMed(), a, 5);
                        model.setValueAt(total, a, 6);
                        model.setValueAt(total_operario, a, 7);
                        model.setValueAt(0.0d, a, 8);
                        //model.setValueAt(par.getPerdidases().size(), a, 9);
                        model.setValueAt(par.getOp(), a, 9);
                    }
                    for (int a = 0; a < partidasExternas.size(); a++) {
                        PartidaExterna par = (PartidaExterna) partidasExternas.get(a);
                        Movimiento[] mov = (Movimiento[]) par.getMovimientos().toArray(new Movimiento[0]);
                        double entradas = 0, devoluciones = 0, entregadas = 0, devueltas = 0;
                        for (int b = 0; b < mov.length; b++) {
                            Almacen alm = mov[b].getAlmacen();
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 3)
                                entradas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 3)
                                devoluciones += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 5)
                                devueltas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 5)
                                entregadas += mov[b].getCantidad();
                        }
                        double total_Pedido = entradas - devoluciones;
                        double total_operario = entregadas - devueltas;
                        double total = total_Pedido - total_operario;
                        model.setValueAt(par.getIdPartidaExterna(), a + partidas.size(), 0);
                        //model.setValueAt(par.getPedido().getPartida().getIdEvaluacion()+"-"+par.getPedido().getPartida().getSubPartida(), a+partidas.size(), 1);
                        model.setValueAt("-", a + partidas.size(), 1);
                        model.setValueAt("ADI.", a + partidas.size(), 2);
                        if (par.getNoParte() != null)
                            model.setValueAt(par.getNoParte(), a + partidas.size(), 3);
                        else
                            model.setValueAt("", a + partidas.size(), 3);
                        //model.setValueAt(par.getPedido().getPartida().getCatalogo().getNombre()+"/"+par.getDescripcion(), a+partidas.size(), 4);
                        model.setValueAt(par.getDescripcion(), a + partidas.size(), 4);
                        model.setValueAt(par.getUnidad(), a + partidas.size(), 5);
                        model.setValueAt(total, a + partidas.size(), 6);
                        model.setValueAt(total_operario, a + partidas.size(), 7);
                        model.setValueAt(0.0d, a + partidas.size(), 8);
                        //model.setValueAt(0, a+partidas.size(), 9);
                        model.setValueAt(par.getOp(), a + partidas.size(), 9);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (session != null)
        if (session.isOpen())
            session.close();
    formatoTabla();
}

From source file:Almacen.consultaPartidaPedido.java

public void busca(String tipo, String valor, String movimiento) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {/*from   www. j a  v  a 2 s  .  c  o  m*/
        session.beginTransaction().begin();
        if (tipo.compareTo("Pedido") == 0) {
            Pedido pedido = (Pedido) session.get(Pedido.class, Integer.parseInt(t_pedido.getText()));
            if (valor.compareTo("Interno") == 0) {
                session.beginTransaction().begin();
                if (movimiento.compareTo("Entrada") == 0) {
                    String[] columnas = new String[] { "Id", "N", "#", "N Parte", "Descripcin", "Medida",
                            "Pedidos", "X Surtir", "Entrada", "Costo c/u", "Total" }; //,"OK"}; //para antes de entrada
                    Class[] types = new Class[] { java.lang.String.class, java.lang.String.class,
                            java.lang.String.class, java.lang.String.class, java.lang.String.class,
                            java.lang.String.class, java.lang.Double.class, java.lang.Double.class,
                            java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
                            //, java.lang.Boolean.class //para antes de entrada
                    };
                    Query query = session.createQuery("SELECT DISTINCT par FROM Partida par "
                            + "LEFT JOIN FETCH par.movimientos movPart " + "LEFT JOIN movPart.almacen alm "
                            + "where par.pedido.idPedido=" + Integer.parseInt(t_pedido.getText())); //where alm.operacion=1 and 
                    //+ "and par.pedido!="+null);
                    List partidas = query.list();
                    model = new MyModel(partidas.size(), columnas, types);
                    t_datos.setModel(model);
                    for (int a = 0; a < partidas.size(); a++) {
                        Partida par = (Partida) partidas.get(a);
                        Movimiento[] mov = (Movimiento[]) par.getMovimientos().toArray(new Movimiento[0]);
                        double entradas = 0, devoluciones = 0;
                        for (int b = 0; b < mov.length; b++) {
                            Almacen alm = mov[b].getAlmacen();
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 1)
                                entradas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 1)
                                devoluciones += mov[b].getCantidad();
                        }
                        double total_almacen = entradas - devoluciones;
                        double total = par.getCantPcp() - total_almacen;

                        model.setValueAt(par.getIdPartida(), a, 0);
                        model.setValueAt(par.getIdEvaluacion(), a, 1);
                        model.setValueAt(par.getSubPartida(), a, 2);
                        if (par.getEjemplar() != null)
                            model.setValueAt(par.getEjemplar().getIdParte(), a, 3);
                        else
                            model.setValueAt("", a, 3);
                        model.setValueAt(par.getCatalogo().getNombre(), a, 4);
                        model.setValueAt(par.getMed(), a, 5);
                        model.setValueAt(par.getCantPcp(), a, 6);
                        model.setValueAt(total, a, 7);
                        model.setValueAt(0.0d, a, 8);

                        if (par.getPcp() != null)
                            model.setValueAt(par.getPcp(), a, 9);
                        else
                            model.setValueAt(par.getPcp(), a, 9);
                        model.setValueAt(0.0d, a, 10);
                        //model.setValueAt(par.getOp(), a, 11); //para antes de entrada
                    }
                }
                if (movimiento.compareTo("Salida") == 0) {
                    String[] columnas = new String[] { "Id", "N", "#", "N Parte", "Descripcin", "Medida",
                            "Pedidos", "En almacen", "Devolucin", "Costo c/u", "Total" };
                    Class[] types = new Class[] { java.lang.String.class, java.lang.String.class,
                            java.lang.String.class, java.lang.String.class, java.lang.String.class,
                            java.lang.String.class, java.lang.Double.class, java.lang.Double.class,
                            java.lang.Double.class, java.lang.Double.class, java.lang.Double.class };
                    Query query = session.createQuery("SELECT DISTINCT par FROM Partida par "
                            + "LEFT JOIN FETCH par.movimientos movPart " + "LEFT JOIN movPart.almacen alm "
                            + "where alm.operacion=1 and par.pedido.idPedido="
                            + Integer.parseInt(t_pedido.getText()));
                    //+ "and par.pedido!="+null);
                    List partidas = query.list();
                    model = new MyModel(partidas.size(), columnas, types);
                    t_datos.setModel(model);
                    for (int a = 0; a < partidas.size(); a++) {
                        Partida par = (Partida) partidas.get(a);
                        Movimiento[] mov = (Movimiento[]) session.createCriteria(Movimiento.class)
                                .add(Restrictions.eq("partida.idPartida", par.getIdPartida())).list()
                                .toArray(new Movimiento[0]);
                        double entradas = 0.0d, devoluciones = 0.0d, entregadas = 0.0d, devueltas = 0.0d;
                        for (int b = 0; b < mov.length; b++) {
                            Almacen alm = mov[b].getAlmacen();
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 1)
                                entradas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 1)
                                devoluciones += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 5)
                                devueltas += mov[b].getCantidad();
                            if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 5)
                                entregadas += mov[b].getCantidad();
                        }
                        double total_Pedido = entradas - devoluciones;
                        double total_operario = entregadas - devueltas;
                        double total = total_Pedido - total_operario;
                        model.setValueAt(par.getIdPartida(), a, 0);
                        model.setValueAt(par.getIdEvaluacion(), a, 1);
                        model.setValueAt(par.getSubPartida(), a, 2);
                        if (par.getEjemplar() != null)
                            model.setValueAt(par.getEjemplar().getIdParte(), a, 3);
                        else
                            model.setValueAt("", a, 3);
                        model.setValueAt(par.getCatalogo().getNombre(), a, 4);
                        model.setValueAt(par.getMed(), a, 5);
                        model.setValueAt(par.getCantPcp(), a, 6);
                        model.setValueAt(total, a, 7);
                        model.setValueAt(0.0d, a, 8);
                        if (par.getPcp() != null)
                            model.setValueAt(par.getPcp(), a, 9);
                        else
                            model.setValueAt(par.getPcp(), a, 9);
                        model.setValueAt(0.0d, a, 10);
                    }
                }
                session.beginTransaction().commit();
            }

            if (valor.compareTo("Externo") == 0) {
                try {
                    session.beginTransaction().begin();
                    if (movimiento.compareTo("Entrada") == 0) {
                        String[] columnas = new String[] { "Id", "N Parte", "Descripcin", "Medida",
                                "Pedidos", "X Surtir", "Entrada", "Costo c/u", "Total" };
                        Class[] types = new Class[] { java.lang.String.class, java.lang.String.class,
                                java.lang.String.class, java.lang.String.class, java.lang.Double.class,
                                java.lang.Double.class, java.lang.Double.class, java.lang.Double.class,
                                java.lang.Double.class };
                        Query query = session.createQuery("SELECT DISTINCT part FROM PartidaExterna part "
                                + "where part.pedido.idPedido=" + Integer.parseInt(t_pedido.getText()));
                        //+ "and part.pedido!="+null);
                        List partidas = query.list();
                        model = new MyModel(partidas.size(), columnas, types);
                        t_datos.setModel(model);
                        for (int a = 0; a < partidas.size(); a++) {
                            PartidaExterna par = (PartidaExterna) partidas.get(a);
                            Movimiento[] mov = (Movimiento[]) par.getMovimientos().toArray(new Movimiento[0]);
                            double entradas = 0.0d, devoluciones = 0.0d;
                            for (int b = 0; b < mov.length; b++) {
                                Almacen alm = mov[b].getAlmacen();
                                if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 2)
                                    entradas += mov[b].getCantidad();
                                if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 2)
                                    devoluciones += mov[b].getCantidad();
                            }
                            double total_almacen = entradas - devoluciones;
                            double total = par.getCantidad() - total_almacen;
                            model.setValueAt(par.getIdPartidaExterna(), a, 0);
                            if (par.getNoParte() != null)
                                model.setValueAt(par.getNoParte(), a, 1);
                            else
                                model.setValueAt("", a, 1);
                            model.setValueAt(par.getDescripcion(), a, 2);
                            model.setValueAt(par.getUnidad(), a, 3);
                            model.setValueAt(par.getCantidad(), a, 4);
                            model.setValueAt(total, a, 5);
                            model.setValueAt(0.0d, a, 6);
                            model.setValueAt(par.getCosto(), a, 7);
                            model.setValueAt(0.0d, a, 8);
                        }
                    }
                    if (movimiento.compareTo("Salida") == 0) {
                        String[] columnas = new String[] { "Id", "N Parte", "Descripcin", "Medida",
                                "Pedidos", "En almacen", "Devolucin", "Costo c/u", "Total" };
                        Class[] types = new Class[] { java.lang.String.class, java.lang.String.class,
                                java.lang.String.class, java.lang.String.class, java.lang.Double.class,
                                java.lang.Double.class, java.lang.Double.class, java.lang.Double.class,
                                java.lang.Double.class };
                        Query query = session.createQuery("SELECT DISTINCT part FROM PartidaExterna part "
                                + "LEFT JOIN FETCH part.movimientos movPart " + "LEFT JOIN movPart.almacen alm "
                                + "where alm.operacion=2 and part.pedido.idPedido="
                                + Integer.parseInt(t_pedido.getText()));
                        //+ "and part.pedido!="+null);
                        List partidas = query.list();
                        model = new MyModel(partidas.size(), columnas, types);
                        t_datos.setModel(model);
                        for (int a = 0; a < partidas.size(); a++) {
                            PartidaExterna par = (PartidaExterna) partidas.get(a);
                            Movimiento[] mov = (Movimiento[]) session
                                    .createCriteria(Movimiento.class).add(Restrictions
                                            .eq("partidaExterna.idPartidaExterna", par.getIdPartidaExterna()))
                                    .list().toArray(new Movimiento[0]);
                            double entradas = 0, devoluciones = 0, entregadas = 0, devueltas = 0;
                            ;
                            for (int b = 0; b < mov.length; b++) {
                                Almacen alm = mov[b].getAlmacen();
                                if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 2)
                                    entradas += mov[b].getCantidad();
                                if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 2)
                                    devoluciones += mov[b].getCantidad();
                                if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 6)
                                    devueltas += mov[b].getCantidad();
                                if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 6)
                                    entregadas += mov[b].getCantidad();
                            }
                            double total_Pedido = entradas - devoluciones;
                            double total_operario = entregadas - devueltas;
                            double total = total_Pedido - total_operario;
                            model.setValueAt(par.getIdPartidaExterna(), a, 0);
                            if (par.getNoParte() != null)
                                model.setValueAt(par.getNoParte(), a, 1);
                            else
                                model.setValueAt("", a, 1);
                            model.setValueAt(par.getDescripcion(), a, 2);
                            model.setValueAt(par.getUnidad(), a, 3);
                            model.setValueAt(par.getCantidad(), a, 4);
                            model.setValueAt(total, a, 5);
                            model.setValueAt(0.0d, a, 6);

                            if (par.getCosto() != null)
                                model.setValueAt(par.getCosto(), a, 7);
                            else
                                model.setValueAt(0.0d, a, 7);
                            model.setValueAt(0.0d, a, 8);
                        }
                    }
                    session.beginTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.beginTransaction().rollback();
                } finally {
                    if (session.isOpen() == true)
                        session.close();
                }
            }

            if (valor.compareTo("Adicional") == 0) {
                try {
                    session.beginTransaction().begin();
                    if (movimiento.compareTo("Entrada") == 0) {
                        String[] columnas = new String[] { "Id", "N", "#", "N Parte", "Descripcin",
                                "Medida", "Pedidos", "X Surtir", "Entrada", "Costo c/u", "Total" };//, "Ok"};//para antes de entrada
                        Class[] types = new Class[] { java.lang.String.class, java.lang.String.class,
                                java.lang.String.class, java.lang.String.class, java.lang.String.class,
                                java.lang.String.class, java.lang.Double.class, java.lang.Double.class,
                                java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
                                //, java.lang.Boolean.class //para antes de entrada
                        };
                        Query query = session.createQuery("SELECT DISTINCT part FROM PartidaExterna part "
                                + "where part.pedido.idPedido=" + Integer.parseInt(t_pedido.getText()));
                        List partidas = query.list();
                        model = new MyModel(partidas.size(), columnas, types);
                        t_datos.setModel(model);
                        for (int a = 0; a < partidas.size(); a++) {
                            PartidaExterna par = (PartidaExterna) partidas.get(a);
                            Movimiento[] mov = (Movimiento[]) par.getMovimientos().toArray(new Movimiento[0]);
                            double entradas = 0, devoluciones = 0;
                            for (int b = 0; b < mov.length; b++) {
                                Almacen alm = mov[b].getAlmacen();
                                if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 3)
                                    entradas += mov[b].getCantidad();
                                if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 3)
                                    devoluciones += mov[b].getCantidad();
                            }
                            double total_almacen = entradas - devoluciones;
                            double total = par.getCantidad() - total_almacen;
                            model.setValueAt(par.getIdPartidaExterna(), a, 0);
                            //model.setValueAt(par.getPedido().getPartida().getIdEvaluacion(), a, 1);
                            //model.setValueAt(par.getPedido().getPartida().getSubPartida(), a, 2);
                            model.setValueAt(0, a, 1);
                            model.setValueAt(0, a, 2);
                            if (par.getNoParte() != null)
                                model.setValueAt(par.getNoParte(), a, 3);
                            else
                                model.setValueAt("", a, 3);
                            //model.setValueAt(par.getPedido().getPartida().getCatalogo().getNombre()+"/"+par.getDescripcion(), a, 4);
                            model.setValueAt(par.getDescripcion(), a, 4);
                            model.setValueAt(par.getUnidad(), a, 5);
                            model.setValueAt(par.getCantidad(), a, 6);
                            model.setValueAt(total, a, 7);
                            model.setValueAt(0.0d, a, 8);
                            model.setValueAt(par.getCosto(), a, 9);
                            model.setValueAt(0.0d, a, 10);
                            //model.setValueAt(par.getOp(), a, 11); //para antes de entrada
                        }
                    }
                    if (movimiento.compareTo("Salida") == 0) {
                        String[] columnas = new String[] { "Id", "N", "#", "N Parte", "Descripcin",
                                "Medida", "Pedidos", "En almacen", "Devolucin", "Costo c/u", "Total" };
                        Class[] types = new Class[] { java.lang.String.class, java.lang.String.class,
                                java.lang.String.class, java.lang.String.class, java.lang.String.class,
                                java.lang.String.class, java.lang.Double.class, java.lang.Double.class,
                                java.lang.Double.class, java.lang.Double.class, java.lang.Double.class };
                        Query query = session.createQuery("SELECT DISTINCT part FROM PartidaExterna part "
                                + "LEFT JOIN FETCH part.movimientos movPart " + "LEFT JOIN movPart.almacen alm "
                                + "where alm.operacion=3 and part.pedido.idPedido="
                                + Integer.parseInt(t_pedido.getText()) + "and part.pedido!=" + null);
                        List partidas = query.list();
                        model = new MyModel(partidas.size(), columnas, types);
                        t_datos.setModel(model);
                        for (int a = 0; a < partidas.size(); a++) {
                            PartidaExterna par = (PartidaExterna) partidas.get(a);
                            Movimiento[] mov = (Movimiento[]) session
                                    .createCriteria(Movimiento.class).add(Restrictions
                                            .eq("partidaExterna.idPartidaExterna", par.getIdPartidaExterna()))
                                    .list().toArray(new Movimiento[0]);
                            double entradas = 0, devoluciones = 0, entregadas = 0, devueltas = 0;
                            ;
                            for (int b = 0; b < mov.length; b++) {
                                Almacen alm = mov[b].getAlmacen();
                                if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 3)
                                    entradas += mov[b].getCantidad();
                                if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 3)
                                    devoluciones += mov[b].getCantidad();
                                if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 5)
                                    devueltas += mov[b].getCantidad();
                                if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 5)
                                    entregadas += mov[b].getCantidad();
                            }
                            double total_Pedido = entradas - devoluciones;
                            double total_operario = entregadas - devueltas;
                            double total = total_Pedido - total_operario;
                            model.setValueAt(par.getIdPartidaExterna(), a, 0);
                            //model.setValueAt(par.getPedido().getPartida().getIdEvaluacion(), a, 1);
                            //model.setValueAt(par.getPedido().getPartida().getSubPartida(), a, 2);
                            model.setValueAt("", a, 1);
                            model.setValueAt("", a, 2);
                            if (par.getNoParte() != null)
                                model.setValueAt(par.getNoParte(), a, 3);
                            else
                                model.setValueAt("", a, 3);
                            //model.setValueAt(par.getPedido().getPartida().getCatalogo().getNombre()+"/"+par.getDescripcion(), a, 4);
                            model.setValueAt(par.getDescripcion(), a, 4);
                            model.setValueAt(par.getUnidad(), a, 5);
                            model.setValueAt(par.getCantidad(), a, 6);
                            model.setValueAt(total, a, 7);
                            model.setValueAt(0.0d, a, 8);
                            model.setValueAt(par.getCosto(), a, 9);
                            model.setValueAt(0.0d, a, 10);
                        }
                    }
                    session.beginTransaction().commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    session.beginTransaction().rollback();
                } finally {
                    if (session.isOpen() == true)
                        session.close();
                }
            }
        }

        if (tipo.compareTo("Venta") == 0) {
            Pedido pedido = (Pedido) session.get(Pedido.class, Integer.parseInt(t_pedido.getText()));
            session.beginTransaction().begin();
            if (movimiento.compareTo("Entrada") == 0) {
                Class[] types = new Class[] { java.lang.String.class, java.lang.String.class,
                        java.lang.String.class, java.lang.String.class, java.lang.Double.class,
                        java.lang.Double.class };
                String[] columnas = new String[] { "Id", "N Parte", "Descripcin", "Medida", "Entregados",
                        "Devueltos" };
                Query query = session.createQuery("SELECT DISTINCT part FROM PartidaExterna part "
                        + "LEFT JOIN FETCH part.movimientos movPart " + "LEFT JOIN movPart.almacen alm "
                        + "where alm.operacion=2 and part.pedido.idPedido="
                        + Integer.parseInt(t_pedido.getText()) + "and part.pedido!=" + null);
                List partidas = query.list();
                model = new MyModel(partidas.size(), columnas, types);
                t_datos.setModel(model);
                for (int a = 0; a < partidas.size(); a++) {
                    PartidaExterna par = (PartidaExterna) partidas.get(a);
                    Movimiento[] mov = (Movimiento[]) session.createCriteria(Movimiento.class)
                            .add(Restrictions.eq("partidaExterna.idPartidaExterna", par.getIdPartidaExterna()))
                            .list().toArray(new Movimiento[0]);
                    double entregados = 0.0d, devoluciones = 0.0d;
                    for (int b = 0; b < mov.length; b++) {
                        Almacen alm = mov[b].getAlmacen();
                        if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 6)
                            devoluciones += mov[b].getCantidad();
                        if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 6)
                            entregados += mov[b].getCantidad();
                    }
                    double total = entregados - devoluciones;
                    model.setValueAt(par.getIdPartidaExterna(), a, 0);
                    if (par.getNoParte() != null)
                        model.setValueAt(par.getNoParte(), a, 1);
                    else
                        model.setValueAt("", a, 1);
                    model.setValueAt(par.getDescripcion(), a, 2);
                    model.setValueAt(par.getUnidad(), a, 3);
                    model.setValueAt(total, a, 4);
                    model.setValueAt(0.0d, a, 5);
                }
            }
            if (movimiento.compareTo("Salida") == 0) {
                Class[] types = new Class[] { java.lang.String.class, java.lang.String.class,
                        java.lang.String.class, java.lang.String.class, java.lang.Double.class,
                        java.lang.Double.class, java.lang.Double.class, java.lang.Double.class };
                String[] columnas = new String[] { "Id", "N Parte", "Descripcin", "Medida", "Pedidos",
                        "Entregadas", "Existencias", "Salida" };
                Query query = session.createQuery("SELECT DISTINCT part FROM PartidaExterna part "
                        + "LEFT JOIN FETCH part.movimientos movPart " + "LEFT JOIN movPart.almacen alm "
                        + "where alm.operacion=2 and part.pedido.idPedido="
                        + Integer.parseInt(t_pedido.getText()) + "and part.pedido!=" + null);
                List partidas = query.list();
                model = new MyModel(partidas.size(), columnas, types);
                t_datos.setModel(model);
                for (int a = 0; a < partidas.size(); a++) {
                    PartidaExterna par = (PartidaExterna) partidas.get(a);
                    Movimiento[] mov = (Movimiento[]) session.createCriteria(Movimiento.class)
                            .add(Restrictions.eq("partidaExterna.idPartidaExterna", par.getIdPartidaExterna()))
                            .list().toArray(new Movimiento[0]);
                    double entradas = 00.d, devoluciones = 0.0d, entregadas = 0.0d, devueltas = 0.0d;
                    for (int b = 0; b < mov.length; b++) {
                        Almacen alm = mov[b].getAlmacen();
                        if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 2)
                            entradas += mov[b].getCantidad();
                        if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 2)
                            devoluciones += mov[b].getCantidad();
                        if (alm.getTipoMovimiento() == 1 && alm.getOperacion() == 6)
                            devueltas += mov[b].getCantidad();
                        if (alm.getTipoMovimiento() == 2 && alm.getOperacion() == 6)
                            entregadas += mov[b].getCantidad();
                    }
                    double total_Existencias = entradas - devoluciones;
                    double total_cliente = entregadas - devueltas;
                    double total = total_Existencias - total_cliente;
                    model.setValueAt(par.getIdPartidaExterna(), a, 0);
                    if (par.getNoParte() != null)
                        model.setValueAt(par.getNoParte(), a, 1);
                    else
                        model.setValueAt("", a, 1);
                    model.setValueAt(par.getDescripcion(), a, 2);
                    model.setValueAt(par.getUnidad(), a, 3);
                    model.setValueAt(par.getCantidad(), a, 4);
                    model.setValueAt(total_cliente, a, 5);
                    model.setValueAt(total, a, 6);
                    model.setValueAt(0.0d, a, 7);
                }
            }
            session.beginTransaction().commit();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (session != null)
        if (session.isOpen())
            session.close();
    formatoTabla(tipo, valor, movimiento);
}

From source file:Almacen.formatosAlmacen.java

void formato() {
    h = new Herramientas(usr, 0);
    h.session(sessionPrograma);//w  ww . ja  v a 2  s .  c  o  m
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        miAlmacen = (Almacen) session.get(Almacen.class, miAlmacen.getIdAlmacen());
        DecimalFormat formatoPorcentaje = new DecimalFormat("#,##0.00");
        formatoPorcentaje.setMinimumFractionDigits(2);

        session.beginTransaction().begin();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);

        PDF reporte = new PDF();
        Date fecha = new Date();
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyyHH-mm-ss");//YYYY-MM-DD HH:MM:SS
        String valor = dateFormat.format(fecha);
        Movimiento[] mov = (Movimiento[]) session.createCriteria(Movimiento.class)
                .add(Restrictions.eq("almacen.idAlmacen", miAlmacen.getIdAlmacen())).list()
                .toArray(new Movimiento[0]);
        Orden ord = null;
        if (mov.length > 0)
            ord = mov[0].getPartida().getOrdenByIdOrden();
        //File folder = new File("reportes/"+ord.getIdOrden());
        //folder.mkdirs();
        reporte.Abrir(PageSize.LETTER, "Almacen",
                "reportes/" + ord.getIdOrden() + "/" + valor + "-" + miAlmacen.getIdAlmacen() + "-almacen.pdf");
        Font font = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD);
        BaseColor contenido = BaseColor.WHITE;
        int centro = Element.ALIGN_CENTER;
        int izquierda = Element.ALIGN_LEFT;
        int derecha = Element.ALIGN_RIGHT;
        float tam[] = new float[] { 20, 20, 80, 190, 20, 30, 50, 50 };
        PdfPTable tabla = reporte.crearTabla(8, tam, 100, Element.ALIGN_LEFT);

        cabeceraCompra(reporte, bf, tabla, miAlmacen, ord);

        int ren = 0;
        double total = 0d;
        if (mov.length > 0) {
            for (int i = 0; i < mov.length; i++) {
                int r = i + 1;
                //N
                tabla.addCell(reporte.celda("" + mov[i].getPartida().getIdEvaluacion(), font, contenido,
                        izquierda, 0, 1, Rectangle.RECTANGLE));

                //#
                tabla.addCell(reporte.celda("" + mov[i].getPartida().getSubPartida(), font, contenido, derecha,
                        0, 1, Rectangle.RECTANGLE));

                if (mov[i].getPartida().getEjemplar() != null) {
                    //No de parte
                    tabla.addCell(reporte.celda("" + mov[i].getPartida().getEjemplar().getIdParte(), font,
                            contenido, derecha, 0, 1, Rectangle.RECTANGLE));
                } else
                    tabla.addCell(reporte.celda(" ", font, contenido, derecha, 0, 1, Rectangle.RECTANGLE));

                //Descripcion
                tabla.addCell(reporte.celda(mov[i].getPartida().getCatalogo().getNombre(), font, contenido,
                        izquierda, 0, 1, Rectangle.RECTANGLE));

                //med
                tabla.addCell(reporte.celda(mov[i].getPartida().getMed(), font, contenido, izquierda, 0, 1,
                        Rectangle.RECTANGLE));

                //cant 
                tabla.addCell(reporte.celda(formatoPorcentaje.format(mov[i].getCantidad()), font, contenido,
                        derecha, 0, 1, Rectangle.RECTANGLE));

                tabla.addCell(reporte.celda(formatoPorcentaje.format(mov[i].getPartida().getPcp()), font,
                        contenido, derecha, 0, 1, Rectangle.RECTANGLE));

                double sum = mov[i].getCantidad() * mov[i].getPartida().getPcp();
                total += sum;
                tabla.addCell(reporte.celda(formatoPorcentaje.format(sum), font, contenido, derecha, 0, 1,
                        Rectangle.RECTANGLE));

                if (ren == 38) {
                    reporte.agregaObjeto(tabla);
                    reporte.writer.newPage();
                    tabla = reporte.crearTabla(8, tam, 100, Element.ALIGN_LEFT);
                    cabeceraCompra(reporte, bf, tabla, miAlmacen, ord);
                    ren = -1;
                }
                ren++;
            }
        }
        tabla.addCell(reporte.celda("Notas:", font, contenido, izquierda, 0, 1, Rectangle.BOTTOM));
        tabla.addCell(reporte.celda(miAlmacen.getNotas(), font, contenido, izquierda, 7, 1, Rectangle.BOTTOM));
        if (miAlmacen.getTipoMovimiento() == 1)
            tabla.addCell(reporte.celda("Entrego: " + miAlmacen.getEntrego(), font, contenido, izquierda, 3, 1,
                    Rectangle.NO_BORDER));
        else
            tabla.addCell(reporte.celda("recibi:" + miAlmacen.getEntrego(), font, contenido, izquierda, 3, 1,
                    Rectangle.NO_BORDER));
        tabla.addCell(reporte.celda("Sub-total:", font, contenido, derecha, 4, 1, Rectangle.NO_BORDER));
        tabla.addCell(reporte.celda(formatoPorcentaje.format(total), font, contenido, derecha, 0, 1,
                Rectangle.RECTANGLE));
        tabla.addCell(reporte.celda("IVA:", font, contenido, derecha, 7, 1, Rectangle.NO_BORDER));
        double iva = total * 0.16d;
        tabla.addCell(reporte.celda(formatoPorcentaje.format(iva), font, contenido, derecha, 0, 1,
                Rectangle.RECTANGLE));
        tabla.addCell(reporte.celda("Total:", font, contenido, derecha, 7, 1, Rectangle.NO_BORDER));
        total += iva;
        tabla.addCell(reporte.celda(formatoPorcentaje.format(total), font, contenido, derecha, 0, 1,
                Rectangle.RECTANGLE));
        session.beginTransaction().rollback();

        reporte.agregaObjeto(tabla);
        reporte.cerrar();
        reporte.visualizar(
                "reportes/" + ord.getIdOrden() + "/" + valor + "-" + miAlmacen.getIdAlmacen() + "-almacen.pdf");

    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, "No se pudo realizar el reporte si el archivo esta abierto.");
    }
    if (session != null)
        if (session.isOpen())
            session.close();
}

From source file:Almacen.formatosOrden.java

void formato() {
    h = new Herramientas(usr, 0);
    h.session(sessionPrograma);//from  www.j  a  va  2  s .c  om
    Session session = HibernateUtil.getSessionFactory().openSession();
    try {
        miAlmacen = (Almacen) session.get(Almacen.class, miAlmacen.getIdAlmacen());
        DecimalFormat formatoPorcentaje = new DecimalFormat("#,##0.00");
        formatoPorcentaje.setMinimumFractionDigits(2);

        session.beginTransaction().begin();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
        PDF reporte = new PDF();
        Date fecha = new Date();
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyyHH-mm-ss");//YYYY-MM-DD HH:MM:SS
        String valor = dateFormat.format(fecha);
        Movimiento[] mov = (Movimiento[]) session.createCriteria(Movimiento.class)
                .add(Restrictions.eq("almacen.idAlmacen", miAlmacen.getIdAlmacen())).list()
                .toArray(new Movimiento[0]);
        Orden ord = null;
        if (mov.length > 0) {
            if (mov[0].getPartida() != null) {
                ord = mov[0].getPartida().getOrdenByIdOrden();
            } else {
                ord = mov[0].getPartidaExterna().getPedido().getOrden();
                //ord=miAlmacen.getPedido().getPartida().getOrdenByIdOrden();
                //ord=mov[0].getOrden();
            }
        }
        File folder = new File("reportes/" + ord.getIdOrden());
        folder.mkdirs();
        reporte.Abrir(PageSize.LETTER, "Almacen",
                "reportes/" + ord.getIdOrden() + "/" + valor + "-" + miAlmacen.getIdAlmacen() + "-almacen.pdf");
        Font font = new Font(Font.FontFamily.HELVETICA, 6, Font.BOLD);
        BaseColor contenido = BaseColor.WHITE;
        int centro = Element.ALIGN_CENTER;
        int izquierda = Element.ALIGN_LEFT;
        int derecha = Element.ALIGN_RIGHT;
        float tam[] = new float[] { 20, 20, 80, 190, 20, 30 };
        PdfPTable tabla = reporte.crearTabla(6, tam, 100, Element.ALIGN_LEFT);
        cabeceraCompra(reporte, bf, tabla, miAlmacen, ord);
        int ren = 0;
        double total = 0d;
        if (mov.length > 0) {
            int renglon = 0;
            for (int i = 0; i < mov.length; i++) {
                int r = i + 1;
                renglon++;
                if (mov[i].getPartida() != null) {
                    tabla.addCell(reporte.celda("" + mov[i].getPartida().getIdEvaluacion(), font, contenido,
                            izquierda, 0, 1, Rectangle.RECTANGLE));
                    tabla.addCell(reporte.celda("" + mov[i].getPartida().getSubPartida(), font, contenido,
                            derecha, 0, 1, Rectangle.RECTANGLE));
                    if (mov[i].getPartida().getEjemplar() != null)
                        tabla.addCell(reporte.celda("" + mov[i].getPartida().getEjemplar().getIdParte(), font,
                                contenido, derecha, 0, 1, Rectangle.RECTANGLE));
                    else
                        tabla.addCell(reporte.celda(" ", font, contenido, derecha, 0, 1, Rectangle.RECTANGLE));
                    tabla.addCell(reporte.celda(mov[i].getPartida().getCatalogo().getNombre(), font, contenido,
                            izquierda, 0, 1, Rectangle.RECTANGLE));
                    tabla.addCell(reporte.celda(mov[i].getPartida().getMed(), font, contenido, izquierda, 0, 1,
                            Rectangle.RECTANGLE));
                    tabla.addCell(reporte.celda(formatoPorcentaje.format(mov[i].getCantidad()), font, contenido,
                            derecha, 0, 1, Rectangle.RECTANGLE));
                } else {
                    tabla.addCell(reporte.celda("-", font, contenido, izquierda, 0, 1, Rectangle.RECTANGLE));
                    tabla.addCell(reporte.celda("", font, contenido, derecha, 0, 1, Rectangle.RECTANGLE));
                    if (mov[i].getPartidaExterna().getNoParte() != null)
                        tabla.addCell(reporte.celda(mov[i].getPartidaExterna().getNoParte(), font, contenido,
                                derecha, 0, 1, Rectangle.RECTANGLE));
                    else
                        tabla.addCell(reporte.celda(" ", font, contenido, derecha, 0, 1, Rectangle.RECTANGLE));
                    tabla.addCell(reporte.celda(mov[i].getPartidaExterna().getDescripcion(), font, contenido,
                            izquierda, 0, 1, Rectangle.RECTANGLE));
                    tabla.addCell(reporte.celda(mov[i].getPartidaExterna().getUnidad(), font, contenido,
                            izquierda, 0, 1, Rectangle.RECTANGLE));
                    tabla.addCell(reporte.celda(formatoPorcentaje.format(mov[i].getCantidad()), font, contenido,
                            derecha, 0, 1, Rectangle.RECTANGLE));
                }
                if (ren == 20)//20
                {
                    reporte.writer.newPage();
                    reporte.agregaObjeto(tabla);
                    reporte.agregaObjeto(new Paragraph(" "));
                    reporte.agregaObjeto(new Paragraph(" "));
                    reporte.agregaObjeto(new Paragraph(" "));
                    reporte.agregaObjeto(new Paragraph(" "));
                    reporte.agregaObjeto(new Paragraph(" "));
                    reporte.agregaObjeto(new Paragraph(" "));
                    reporte.agregaObjeto(new Paragraph(" "));
                    reporte.agregaObjeto(new Paragraph(" "));
                    reporte.agregaObjeto(tabla);
                    reporte.agregaObjeto(new Paragraph(" "));
                    reporte.agregaObjeto(new Paragraph(" "));
                    reporte.agregaObjeto(new Paragraph(" "));
                    reporte.agregaObjeto(new Paragraph(" "));

                    tabla = reporte.crearTabla(6, tam, 100, Element.ALIGN_LEFT);
                    cabeceraCompra(reporte, bf, tabla, miAlmacen, ord);
                    ren = -1;
                    renglon = 0;
                }
                ren++;
            }
            for (renglon = renglon; renglon < 20; renglon++) {
                tabla.addCell(reporte.celda(" ", font, contenido, izquierda, 0, 1, Rectangle.RECTANGLE));
                tabla.addCell(reporte.celda(" ", font, contenido, derecha, 0, 1, Rectangle.RECTANGLE));
                tabla.addCell(reporte.celda(" ", font, contenido, derecha, 0, 1, Rectangle.RECTANGLE));
                tabla.addCell(reporte.celda(" ", font, contenido, izquierda, 0, 1, Rectangle.RECTANGLE));
                tabla.addCell(reporte.celda(" ", font, contenido, izquierda, 0, 1, Rectangle.RECTANGLE));
                tabla.addCell(reporte.celda(" ", font, contenido, derecha, 0, 1, Rectangle.RECTANGLE));
            }
        }
        tabla.addCell(reporte.celda("Notas: ", font, contenido, izquierda, 0, 1, Rectangle.BOTTOM));
        tabla.addCell(reporte.celda(miAlmacen.getNotas(), font, contenido, izquierda, 7, 1, Rectangle.BOTTOM));
        session.beginTransaction().rollback();

        reporte.agregaObjeto(tabla);
        reporte.agregaObjeto(new Paragraph(" "));
        reporte.agregaObjeto(new Paragraph(" "));
        reporte.agregaObjeto(new Paragraph(" "));
        reporte.agregaObjeto(new Paragraph(" "));
        reporte.agregaObjeto(new Paragraph(" "));
        reporte.agregaObjeto(new Paragraph(" "));
        reporte.agregaObjeto(new Paragraph(" "));
        reporte.agregaObjeto(new Paragraph(" "));
        reporte.agregaObjeto(new Paragraph(" "));
        reporte.agregaObjeto(new Paragraph(" "));
        reporte.agregaObjeto(tabla);
        reporte.cerrar();
        reporte.visualizar(
                "reportes/" + ord.getIdOrden() + "/" + valor + "-" + miAlmacen.getIdAlmacen() + "-almacen.pdf");
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        JOptionPane.showMessageDialog(null, "No se pudo realizar el reporte si el archivo esta abierto");
    }
    if (session != null)
        if (session.isOpen())
            session.close();
}