299 lines
11 KiB
Plaintext
Executable File
299 lines
11 KiB
Plaintext
Executable File
package com.fp.armas.rules.save.solicitudimportacion;
|
||
|
||
import java.math.BigDecimal;
|
||
import java.sql.Timestamp;
|
||
import java.util.ArrayList;
|
||
import java.util.Date;
|
||
import java.util.List;
|
||
|
||
import javax.persistence.EntityManager;
|
||
import javax.persistence.Query;
|
||
|
||
import com.fp.dto.rules.TransactionRule;
|
||
import com.fp.dto.save.SaveRequest;
|
||
import com.fp.persistence.commondb.PersistenceHelper;
|
||
import com.fp.persistence.parmas.fun.TarmRegistroArmas;
|
||
import com.fp.persistence.parmas.fun.TarmTotalPersona;
|
||
import com.fp.persistence.parmas.param.TarmComercianteCupo;
|
||
import com.fp.persistence.parmas.param.TarmTipoArmaExplosivo;
|
||
import com.fp.persistence.parmas.soli.TarmArmas;
|
||
import com.fp.persistence.pgeneral.gene.TgeneCatalogDetail;
|
||
import com.fp.sessionbeans.helper.Sequence;
|
||
|
||
/**
|
||
* Ingresa un registro y actualiza la tabla de cupos
|
||
* @author Andres Cevallos
|
||
*
|
||
*/
|
||
public class IngresoInventario extends TransactionRule {
|
||
|
||
|
||
private static final long serialVersionUID = 1L;
|
||
private static final String JPQL_TIPOARMAEXPLOSIVO = "from TarmTipoArmaExplosivo tae" + " where tae.pk = :pk";
|
||
private static final String JPQL_COMERCIANTECUPO = "from TarmComercianteCupo tcc" + " where tcc.personcodigo = :personcodigo and tcc.clase = :clasecodigo and tcc.tipoarmaexplosivo=:tipoarmaexplosivo and tcc.tiporegistro=:tiporegistro";
|
||
private static final String JPQL_CATALOG = "from TgeneCatalogDetail tgd"+" where tgd.pk.catalog= :catalog and tgd.pk.catalogcode= :catalogcode";
|
||
|
||
|
||
/* (non-Javadoc)
|
||
* @see com.fp.dto.rules.TransactionRule#normalProcess(com.fp.dto.save.SaveRequest)
|
||
*/
|
||
@Override
|
||
@SuppressWarnings("unchecked")
|
||
public SaveRequest normalProcess(SaveRequest pSaveRequest) throws Exception {
|
||
try {
|
||
System.out.println("INGRESA DE INVENTARIO");
|
||
//EntityManager em=PersistenceHelper.getEntityManager();
|
||
List<TarmArmas> listaArmas=(ArrayList<TarmArmas>)pSaveRequest.get("LISTAARMAS");
|
||
List<TarmTotalPersona> listaTotales=(ArrayList<TarmTotalPersona>)pSaveRequest.get("LISTATOTALES");
|
||
TarmRegistroArmas registro=(TarmRegistroArmas)pSaveRequest.get("REGISTRO");
|
||
registro.setTiporegistro("CARINV");
|
||
List<Object> registroarmasL=(List<Object>) pSaveRequest.getSaveBeanModifiedRecords("TREGISTROARMAS");
|
||
List<Object> tarmArmasL=(List<Object>) pSaveRequest.getSaveBeanModifiedRecords("ARMAS");
|
||
List<Object> tarmTotalPersonaL=(List<Object>) pSaveRequest.getSaveBeanModifiedRecords("TOTALCONTROLLER");
|
||
if(registroarmasL==null){
|
||
registroarmasL= new ArrayList<>();
|
||
}
|
||
if(tarmArmasL==null){
|
||
tarmArmasL= new ArrayList<>();
|
||
}
|
||
if(tarmTotalPersonaL==null){
|
||
tarmTotalPersonaL= new ArrayList<>();
|
||
}
|
||
|
||
//Persistiendo el registro
|
||
String cregistro = null;
|
||
Sequence sequence = new Sequence();
|
||
Integer valorRegistro=Integer.valueOf(sequence.getNextValue("CREGISTRO").toString());
|
||
if(valorRegistro!=null){
|
||
cregistro=valorRegistro.toString();
|
||
registro.setPk(Long.parseLong(cregistro));
|
||
registroarmasL.add(registro);
|
||
//em.persist(registro);
|
||
}
|
||
|
||
//Persistiendo la lista de armas
|
||
for (TarmArmas arma : listaArmas) {
|
||
arma.setCregistro(Long.parseLong(cregistro));
|
||
if(arma.isnew){
|
||
String carma = null;
|
||
sequence = new Sequence();
|
||
String valor=sequence.getNextValue("CARMA").toString();
|
||
if(valor!=null){
|
||
carma=valor.toString();
|
||
arma.setPk(carma);
|
||
arma.setCodigoarma(arma.getPk());
|
||
//arma.setUnidadmedidacantidad("Unidad");
|
||
tarmArmasL.add(arma);
|
||
//em.persist(arma);
|
||
}
|
||
}else{
|
||
TarmArmas armaBD=new TarmArmas();
|
||
armaBD=PersistenceHelper.getEntityManager().find(TarmArmas.class, arma.getPk());
|
||
armaBD.setCantidad(arma.getCantidad());
|
||
armaBD.setCantoncode(arma.getCantoncode());
|
||
armaBD.setCitycode(arma.getCitycode());
|
||
armaBD.setCodigoarma(arma.getCodigoarma());
|
||
armaBD.setColor(arma.getColor());
|
||
armaBD.setColorcodigo(arma.getColorcodigo());
|
||
armaBD.setCountrycode(arma.getCountrycode());
|
||
armaBD.setCpaisorigen(arma.getCpaisorigen());
|
||
armaBD.setCregistro(arma.getCregistro());
|
||
armaBD.setCtipoarmaexplosivo(arma.getCtipoarmaexplosivo());
|
||
armaBD.setDireccion(arma.getDireccion());
|
||
armaBD.setEstado(arma.getEstado());
|
||
armaBD.setEstadocodigo(arma.getEstadocodigo());
|
||
armaBD.setFecharegistro(arma.getFecharegistro());
|
||
armaBD.setFemisionpermiso(arma.getFemisionpermiso());
|
||
armaBD.setLote(arma.getLote());
|
||
armaBD.setMarca(arma.getMarca());
|
||
armaBD.setMarcacodigo(arma.getMarcacodigo());
|
||
armaBD.setModelo(arma.getModelo());
|
||
armaBD.setNemonicoarma(arma.getNemonicoarma());
|
||
armaBD.setParroquiacode(arma.getParroquiacode());
|
||
armaBD.setPeso(arma.getPeso());
|
||
armaBD.setPesoAux(arma.getPesoAux());
|
||
armaBD.setPk(arma.getPk());
|
||
armaBD.setProvincecode(arma.getProvincecode());
|
||
armaBD.setRecordversion(arma.getRecordversion());
|
||
armaBD.setSeriecanon(arma.getSeriecanon());
|
||
armaBD.setTipofabricacion(arma.getTipofabricacion());
|
||
armaBD.setUnidadmedidacantidad(arma.getUnidadmedidacantidad());
|
||
armaBD.setUnidadmedidacantidadcodigo(arma.getUnidadmedidacantidadcodigo());
|
||
armaBD.setValorAux(arma.getValorAux());
|
||
armaBD.setCodigoarma(arma.getPk());
|
||
//em.persist(armaBD);
|
||
tarmArmasL.add(arma);
|
||
}
|
||
}
|
||
|
||
//Persistiendo la lista de totales
|
||
for (TarmTotalPersona total : listaTotales) {
|
||
if(total.isnew){
|
||
String ctotal = null;
|
||
sequence = new Sequence();
|
||
Integer valor=Integer.valueOf(sequence.getNextValue("CTOTAL").toString());
|
||
if(valor!=null){
|
||
ctotal=valor.toString();
|
||
total.setPk(ctotal);
|
||
//total.setUnidadmedidapeso("Unidad");
|
||
System.out.println(total);
|
||
tarmTotalPersonaL.add(total);
|
||
// em.persist(total);
|
||
// em.flush();
|
||
}
|
||
}else{
|
||
TarmTotalPersona totalBD=new TarmTotalPersona();
|
||
totalBD=PersistenceHelper.getEntityManager().find(TarmTotalPersona.class, total.getPk());
|
||
totalBD.setClase(total.getClase());
|
||
totalBD.setClasecodigo(total.getClasecodigo());
|
||
totalBD.setCarma(total.getCarma());
|
||
totalBD.setFecha(total.getFecha());
|
||
totalBD.setPersoncodigo(total.getPersoncodigo());
|
||
totalBD.setPk(total.getPk());
|
||
totalBD.setRecordversion(total.getRecordversion());
|
||
totalBD.setSaldoanterior(total.getSaldoanterior());
|
||
totalBD.setTipoarmaexplosivo(total.getTipoarmaexplosivo());
|
||
totalBD.setTipoarmaexplosivocodigo(total.getTipoarmaexplosivocodigo());
|
||
totalBD.setTotal(total.getTotal());
|
||
totalBD.setUnidadmedidapeso(total.getUnidadmedidapeso());
|
||
totalBD.setUnidadmedidapesocodigo(total.getUnidadmedidapesocodigo());
|
||
tarmTotalPersonaL.add(total);
|
||
//em.persist(totalBD);
|
||
}
|
||
}
|
||
// em.flush();
|
||
} catch (Exception e) {
|
||
return pSaveRequest;
|
||
// TODO: handle exception
|
||
}
|
||
return pSaveRequest;
|
||
}
|
||
|
||
@Override
|
||
public SaveRequest reverseProcess(SaveRequest arg0) throws Exception {
|
||
|
||
return arg0;
|
||
}
|
||
|
||
|
||
/**
|
||
* Actualiza la tabla de totales
|
||
* @param tipoarmaexplosivo
|
||
* @param valor
|
||
* @param personcode
|
||
* @return
|
||
*/
|
||
public TarmTotalPersona cargarDatos(TarmTipoArmaExplosivo tipoarmaexplosivo,BigDecimal valor,Integer personcode)
|
||
{
|
||
TarmTotalPersona totalArma = new TarmTotalPersona();
|
||
totalArma.setClase(tipoarmaexplosivo.getClase());
|
||
totalArma.setClasecodigo(tipoarmaexplosivo.getClasecodigo());
|
||
totalArma.setFecha(new Timestamp(new Date().getTime()));
|
||
totalArma.setTipoarmaexplosivo(tipoarmaexplosivo.getTipoarmaexplosivo());
|
||
totalArma.setTipoarmaexplosivocodigo(tipoarmaexplosivo.getTipoarmaexplosivocodigo());
|
||
totalArma.setPersoncodigo(personcode);
|
||
totalArma.setSaldoanterior(BigDecimal.ZERO);
|
||
totalArma.setTotal(valor);
|
||
totalArma.setUnidadmedidapeso(tipoarmaexplosivo.getUnidadmedidapeso());
|
||
totalArma.setUnidadmedidapesocodigo(tipoarmaexplosivo.getUnidadmedidapesocodigo());
|
||
return totalArma;
|
||
|
||
}
|
||
|
||
/**
|
||
* Actualiza la tabla de totales cuando existen datos anteriores
|
||
* @param tipoarmaexplosivo
|
||
* @param valor
|
||
* @param personcode
|
||
* @param saldo
|
||
* @return
|
||
*/
|
||
public TarmTotalPersona cargarDatosAnterior(TarmTipoArmaExplosivo tipoarmaexplosivo,BigDecimal valor,Integer personcode,BigDecimal saldo)
|
||
{
|
||
BigDecimal total = new BigDecimal(0);
|
||
total = total.add(valor);
|
||
total = total.add(saldo);
|
||
TarmTotalPersona totalArma = new TarmTotalPersona();
|
||
totalArma.setClase(tipoarmaexplosivo.getClase());
|
||
totalArma.setClasecodigo(tipoarmaexplosivo.getClasecodigo());
|
||
totalArma.setFecha(new Timestamp(new Date().getTime()));
|
||
totalArma.setTipoarmaexplosivo(tipoarmaexplosivo.getTipoarmaexplosivo());
|
||
totalArma.setTipoarmaexplosivocodigo(tipoarmaexplosivo.getTipoarmaexplosivocodigo());
|
||
totalArma.setPersoncodigo(personcode);
|
||
totalArma.setSaldoanterior(saldo);
|
||
totalArma.setTotal(total);
|
||
totalArma.setUnidadmedidapeso(tipoarmaexplosivo.getUnidadmedidapeso());
|
||
totalArma.setUnidadmedidapesocodigo(tipoarmaexplosivo.getUnidadmedidapesocodigo());
|
||
return totalArma;
|
||
}
|
||
|
||
/**
|
||
* Encuentra el cupo por cliente
|
||
* @param pEntityManager
|
||
* @param personcodigo
|
||
* @param clasecodigo
|
||
* @param tipoarmaexplosivo
|
||
* @param tiporegistro
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static List<TarmComercianteCupo> findCupo(EntityManager pEntityManager, Integer personcodigo, String clasecodigo, String tipoarmaexplosivo, String tiporegistro) throws Exception {
|
||
|
||
Query qry = pEntityManager.createQuery(IngresoInventario.JPQL_COMERCIANTECUPO);
|
||
qry.setParameter("personcodigo", personcodigo);
|
||
qry.setParameter("clasecodigo", clasecodigo);
|
||
qry.setParameter("tipoarmaexplosivo", tipoarmaexplosivo);
|
||
qry.setParameter("tiporegistro", tiporegistro);
|
||
return qry.getResultList();
|
||
}
|
||
|
||
/**
|
||
* Encuentra una lista de tipo de armas y explosivos
|
||
* @param pEntityManager
|
||
* @param pk
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
public static List<TarmTipoArmaExplosivo> findByClase(EntityManager pEntityManager, String pk) throws Exception {
|
||
Query qry = pEntityManager.createQuery(IngresoInventario.JPQL_TIPOARMAEXPLOSIVO);
|
||
qry.setParameter("pk", pk);
|
||
return qry.getResultList();
|
||
}
|
||
|
||
public static List<TgeneCatalogDetail> find(EntityManager pEntityManager, String catalog, String catalogcode) throws Exception {
|
||
Query qry = pEntityManager.createQuery(IngresoInventario.JPQL_CATALOG);
|
||
|
||
qry.setParameter("catalog", catalog);
|
||
qry.setParameter("catalogcode", catalogcode);
|
||
return qry.getResultList();
|
||
}
|
||
/**
|
||
* M<>todo que devuelve la secuencia de un cliente.
|
||
*
|
||
* @return Codigo del nuevo cliente.
|
||
* @throws Exception
|
||
*/
|
||
private Long getCodigoSolicitud() throws Exception {
|
||
Sequence sequence = new Sequence();
|
||
Long codigo = sequence.getNextValue("CREGISTRO").longValue();
|
||
|
||
return codigo;
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* M<>todo que devuelve el codigo de un arma
|
||
* @return
|
||
* @throws Exception
|
||
*/
|
||
private String getCodigoArmas() throws Exception {
|
||
Sequence sequence = new Sequence();
|
||
String codigoArma = sequence.getNextValue("CARMA").toString();
|
||
|
||
return codigoArma;
|
||
|
||
}
|
||
|
||
|
||
} |