fixes bug#21465
qua: dev:Valdir git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/AdmVenta/Model/trunk/modelWeb@105276 d1611594-4594-4d17-8e1d-87c2c4800839master
parent
53a4f6f935
commit
46e0fb0cf9
|
@ -0,0 +1,17 @@
|
|||
package com.rjconsultores.ventaboletos.service;
|
||||
|
||||
|
||||
import java.io.Reader;
|
||||
import java.util.List;
|
||||
|
||||
import org.zkoss.util.media.Media;
|
||||
|
||||
import com.rjconsultores.ventaboletos.entidad.Empresa;
|
||||
|
||||
public interface ImportacaoClientesSrvpService {
|
||||
public String[] lerArquivo(Reader reader, List<Empresa> empresas);
|
||||
|
||||
public String[] lerArquivoExcel(Media media, List<Empresa> empresas);
|
||||
|
||||
public Integer[] salvarClientes(String[] cliente, List<Empresa> empresas) throws Exception;
|
||||
}
|
|
@ -0,0 +1,718 @@
|
|||
package com.rjconsultores.ventaboletos.service.impl;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||
import org.apache.poi.ss.usermodel.Cell;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.brazilutils.br.cpfcnpj.Cpf;
|
||||
import org.brazilutils.validation.ValidationException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.zkoss.util.media.Media;
|
||||
import org.zkoss.util.resource.Labels;
|
||||
|
||||
import com.rjconsultores.ventaboletos.entidad.Cliente;
|
||||
import com.rjconsultores.ventaboletos.entidad.ClienteDireccion;
|
||||
import com.rjconsultores.ventaboletos.entidad.ClienteFidelidad;
|
||||
import com.rjconsultores.ventaboletos.entidad.Empresa;
|
||||
import com.rjconsultores.ventaboletos.entidad.TarjetaFidelidad;
|
||||
import com.rjconsultores.ventaboletos.entidad.TipoIdentificacion;
|
||||
import com.rjconsultores.ventaboletos.passageirofrequente.vo.ClienteExcelVo;
|
||||
import com.rjconsultores.ventaboletos.service.ClienteService;
|
||||
import com.rjconsultores.ventaboletos.service.ImportacaoClientesSrvpService;
|
||||
import com.rjconsultores.ventaboletos.service.TipoIdentificacionService;
|
||||
import com.rjconsultores.ventaboletos.utilerias.ApplicationProperties;
|
||||
import com.rjconsultores.ventaboletos.utilerias.CustomEnum;
|
||||
import com.rjconsultores.ventaboletos.utilerias.DateUtil;
|
||||
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
|
||||
|
||||
@Service("importacaoClientesSrvpService")
|
||||
public class ImportacaoClientesSrvpServiceImpl implements ImportacaoClientesSrvpService {
|
||||
|
||||
@Autowired
|
||||
TipoIdentificacionService tipoIdentificacionService;
|
||||
@Autowired
|
||||
private ClienteService clienteService;
|
||||
private static Logger log = Logger.getLogger(ImportacaoClientesSrvpServiceImpl.class);
|
||||
|
||||
@Override
|
||||
public String[] lerArquivo(Reader reader, List<Empresa> empresas) {
|
||||
String linha = null;
|
||||
Integer index = 0;
|
||||
StringBuilder qtdeGravados = new StringBuilder();
|
||||
StringBuilder erros = new StringBuilder();
|
||||
|
||||
Integer inseridos = 0;
|
||||
Integer atualizados = 0;
|
||||
|
||||
try {
|
||||
BufferedReader leitor = new BufferedReader(reader);
|
||||
|
||||
while ((linha = leitor.readLine()) != null) {
|
||||
String[] dados = linha.replace("\"", "").split(",");
|
||||
Integer[] gravados = salvarClientes(dados, empresas);
|
||||
inseridos = gravados[0] + inseridos;
|
||||
atualizados = gravados[1] + atualizados;
|
||||
index++;
|
||||
}
|
||||
|
||||
leitor.close();
|
||||
|
||||
qtdeGravados.append("Gravados ").append(inseridos + atualizados).append(" clientes de ").append(index).append(" importados.\n");
|
||||
qtdeGravados.append("Atualizados ").append(atualizados).append(" clientes.\n");
|
||||
qtdeGravados.append("Inseridos ").append(inseridos).append(" novos clientes.");
|
||||
|
||||
String[] resultado = { qtdeGravados.toString(), erros.toString() };
|
||||
return resultado;
|
||||
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
log.error(e);
|
||||
String msg = "Erro ao gravar cliente na linha " + (index + 1) + " do arquivo.";
|
||||
erros.append(msg);
|
||||
String[] resultado = { "Houve erro ao gravar os clientes, consulte o arquivo de erros.", erros.toString() };
|
||||
return resultado;
|
||||
} catch (Exception e) {
|
||||
erros.append("Linha ").append(index).append(" do arquivo de clientes, erro: ").append(e.getCause().getCause()).append("\n");
|
||||
log.error(e);
|
||||
String[] resultado = { e.toString() };
|
||||
return resultado;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer[] salvarClientes(String[] cliente, List<Empresa> empresas) throws Exception {
|
||||
String nomeCliente = cliente[1].replace("\"", "").toUpperCase();
|
||||
Integer inseridos = new Integer(0);
|
||||
Integer atualizados = new Integer(0);
|
||||
Integer[] gravados = { 0, 0 };
|
||||
|
||||
for (Empresa e : empresas) {
|
||||
|
||||
Cliente clienteGravar = clienteService.buscarPorNumeroFidelidade(cliente[0], e);
|
||||
if (clienteGravar == null) {
|
||||
clienteGravar = new Cliente();
|
||||
clienteGravar.setNombcliente(nomeCliente);
|
||||
clienteGravar.setNumIdentificaUno(cliente[3]);
|
||||
TarjetaFidelidad tarjetaFidelidad = new TarjetaFidelidad();
|
||||
tarjetaFidelidad.setActivo(Boolean.TRUE);
|
||||
tarjetaFidelidad.setFecmodif(Calendar.getInstance().getTime());
|
||||
tarjetaFidelidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
|
||||
// cast para Integer para remover 0 a esquerda:
|
||||
tarjetaFidelidad.setNumTarjeta(cliente[0]);
|
||||
|
||||
List<ClienteFidelidad> ls = new ArrayList<ClienteFidelidad>();
|
||||
|
||||
ClienteFidelidad clienteFidelidad = new ClienteFidelidad();
|
||||
clienteFidelidad.setTarjetaFidelidad(tarjetaFidelidad);
|
||||
clienteFidelidad.setActivo(Boolean.TRUE);
|
||||
clienteFidelidad.setFecmodif(Calendar.getInstance().getTime());
|
||||
clienteFidelidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
|
||||
clienteFidelidad.setCliente(clienteGravar);
|
||||
clienteFidelidad.setEmpresa(e);
|
||||
ls.add(clienteFidelidad);
|
||||
|
||||
clienteGravar.setListClienteFidelidad(ls);
|
||||
|
||||
clienteService.suscribir(clienteGravar);
|
||||
inseridos = inseridos + 1;
|
||||
gravados[0] = gravados[0] + inseridos;
|
||||
} else {
|
||||
clienteGravar.setNombcliente(nomeCliente);
|
||||
clienteGravar.setNumIdentificaUno(cliente[3]);
|
||||
clienteService.actualizacion(clienteGravar);
|
||||
|
||||
atualizados = atualizados + 1;
|
||||
gravados[1] = gravados[1] + atualizados;
|
||||
}
|
||||
}
|
||||
return gravados;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] lerArquivoExcel(Media media, List<Empresa> empresas) {
|
||||
|
||||
List<TipoIdentificacion> lsTipoIdentificacion = tipoIdentificacionService.obtenerTodos();
|
||||
TipoIdentificacion tipoIdentificacionUno = null;
|
||||
TipoIdentificacion tipoIdentificacionDoos = null;
|
||||
|
||||
for (TipoIdentificacion t : lsTipoIdentificacion) {
|
||||
if (t.getDesctipo().equalsIgnoreCase("cpf")) {
|
||||
tipoIdentificacionUno = t;
|
||||
} else if (t.getDesctipo().equalsIgnoreCase("rg")) {
|
||||
tipoIdentificacionDoos = t;
|
||||
}
|
||||
if (tipoIdentificacionUno != null && tipoIdentificacionDoos != null)
|
||||
break;
|
||||
}
|
||||
|
||||
Boolean usaCPFComoFidelidade = false;
|
||||
Integer index = 1;
|
||||
StringBuilder qtdeGravados = new StringBuilder();
|
||||
StringBuilder erros = new StringBuilder();
|
||||
Integer inseridos = 0;
|
||||
Integer atualizados = 0;
|
||||
Integer desconsiderados =0;
|
||||
InputStream isMExcel = media.getStreamData();
|
||||
Sheet sheet = null;
|
||||
|
||||
if (media.getFormat().equals("xls")) {
|
||||
HSSFWorkbook wb;
|
||||
try {
|
||||
wb = new HSSFWorkbook(isMExcel);
|
||||
sheet = wb.getSheetAt(0);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
} else if (media.getFormat().equals("xlsx")) {
|
||||
XSSFWorkbook wb;
|
||||
try {
|
||||
wb = new XSSFWorkbook(isMExcel);
|
||||
sheet = wb.getSheetAt(0);
|
||||
} catch (IOException e) {
|
||||
log.error("",e);
|
||||
}
|
||||
}
|
||||
|
||||
int rows = sheet.getPhysicalNumberOfRows();
|
||||
|
||||
log.info(String.format("Quantidade cliente:%s",rows));
|
||||
|
||||
if (validaSheet(sheet)) {
|
||||
usaCPFComoFidelidade = ApplicationProperties.getInstance().isCustomHabilitado(CustomEnum.USA_CPF_COMO_FIDELIDADE.getDescricao());
|
||||
log.info(String.format("usaCPFComoFidelidade:%s",usaCPFComoFidelidade));
|
||||
try {
|
||||
for (index = 1; index < rows; index++) {
|
||||
log.info(String.format("index cliente:%s",index));
|
||||
|
||||
Row row = sheet.getRow(index);
|
||||
if (row == null){
|
||||
log.info("index/row empty");
|
||||
continue;
|
||||
}
|
||||
|
||||
Iterator<Cell> cellIterator = row.cellIterator();
|
||||
ClienteExcelVo cliente = new ClienteExcelVo();
|
||||
|
||||
while (cellIterator.hasNext()) {
|
||||
Cell cell = cellIterator.next();
|
||||
switch (cell.getColumnIndex()) {
|
||||
case 0:
|
||||
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
|
||||
try {
|
||||
cliente.setNumfidelidade(new Long(Math.round(cell.getNumericCellValue())).toString());
|
||||
}catch(Exception e) {
|
||||
log.error("Erro na gravação do registro. Favor revisar",e);
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||
cliente.setNome(cell.getStringCellValue());
|
||||
break;
|
||||
case 2:
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||
cliente.setTipodoc(cell.getStringCellValue());
|
||||
break;
|
||||
case 3:
|
||||
cell.setCellType(Cell.CELL_TYPE_NUMERIC);
|
||||
String numDocString = null;
|
||||
try {
|
||||
numDocString = new Long(Math.round(cell.getNumericCellValue())).toString();
|
||||
}catch(Exception e) {
|
||||
log.error("Erro na gravação do registro. Favor revisar",e);
|
||||
erros.append(cliente.getNumfidelidade() + " - Numero doc nao numerico: " + new Long(Math.round(cell.getNumericCellValue())).toString());
|
||||
erros.append("\n");
|
||||
|
||||
}
|
||||
cliente.setNumerodoc(numDocString);
|
||||
cliente.setCpf(numDocString);
|
||||
cliente.setRg(numDocString);
|
||||
break;
|
||||
case 4:
|
||||
String dt = null;
|
||||
try {
|
||||
if (cell.getCellType() == Cell.CELL_TYPE_STRING && cell.getStringCellValue().length() ==6 ) {
|
||||
dt = cell.getStringCellValue();
|
||||
} else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC ) {
|
||||
String dataString = new Long(Math.round(cell.getNumericCellValue())).toString();
|
||||
if(dataString!=null && dataString.length() ==6) {
|
||||
Date data = DateUtil.getDateFromString(dataString, DateUtil.ddMMaa_sembarra);
|
||||
dt = DateUtil.getStringDate(data, DateUtil.ddMMaa_anodoisdigitos);
|
||||
cliente.setNascimento(dt);
|
||||
}else {
|
||||
cliente.setNascimento(null);
|
||||
erros.append(cliente.getNumfidelidade() + " - Data Nascimento formato incorreto: " + new Long(Math.round(cell.getNumericCellValue())).toString());
|
||||
erros.append("\n");
|
||||
|
||||
}
|
||||
}else {
|
||||
cliente.setNascimento(null);
|
||||
erros.append(cliente.getNumfidelidade() + " - Data Nascimento formato incorreto: " + cell.getStringCellValue().trim().replaceAll(" ","").toString());
|
||||
erros.append("\n");
|
||||
|
||||
}
|
||||
}catch (Exception e) {
|
||||
cliente.setNascimento(null);
|
||||
erros.append(cliente.getNumfidelidade() + " - Data Nascimento formato incorreto: " + cell.getStringCellValue().trim().replaceAll(" ","").toString());
|
||||
erros.append("\n");
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||
cliente.setNacionalidade(cell.getStringCellValue());
|
||||
break;
|
||||
case 6:
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||
cliente.setPaisresidencia(cell.getStringCellValue());
|
||||
break;
|
||||
case 7:
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||
cliente.setOcupacao(cell.getStringCellValue());
|
||||
break;
|
||||
case 8:
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||
cliente.setSexo(cell.getStringCellValue());
|
||||
break;
|
||||
case 9:
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||
cliente.setEstadocivil(cell.getStringCellValue());
|
||||
break;
|
||||
case 10:
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||
cliente.setEmail(cell.getStringCellValue());
|
||||
break;
|
||||
case 11:
|
||||
try {
|
||||
if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
|
||||
cliente.setTelefone(new Long(Math.round(cell.getNumericCellValue())).toString().trim().replaceAll(" ",""));
|
||||
}else {
|
||||
if(StringUtils.isNotBlank(cell.getStringCellValue())) {
|
||||
try {
|
||||
cliente.setTelefone(new Long(cell.getStringCellValue().trim().replaceAll(" ","")).toString());
|
||||
}catch (NumberFormatException e) {
|
||||
cliente.setTelefone("");
|
||||
erros.append(cliente.getNumfidelidade() + " - Numero de Telefone incorreto: " + cell.getStringCellValue().trim().replaceAll(" ","").toString());
|
||||
erros.append("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch(Exception e) {
|
||||
log.error("Erro na gravação do registro. Favor revisar",e);
|
||||
}
|
||||
break;
|
||||
case 12:
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||
cliente.setEndereco(cell.getStringCellValue());
|
||||
break;
|
||||
case 13:
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||
cliente.setCidade(cell.getStringCellValue());
|
||||
break;
|
||||
case 14:
|
||||
try {
|
||||
cliente.setCep(new Long(Math.round(cell.getNumericCellValue())).toString());
|
||||
}catch(Exception e) {
|
||||
log.error("Erro na gravação do registro. Favor revisar",e);
|
||||
}
|
||||
break;
|
||||
case 15:
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||
cliente.setObservacao(cell.getStringCellValue());
|
||||
break;
|
||||
case 16:
|
||||
cell.setCellType(Cell.CELL_TYPE_STRING);
|
||||
cliente.setDesconto(cell.getStringCellValue());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (validaDadosPlanilha(cliente, erros)) {
|
||||
log.info(String.format("cliente:%s cpf:%s RG:%s",cliente.getNome(),cliente.getCpf(), cliente.getRg()));
|
||||
log.info("enviado para gravação...");
|
||||
try{
|
||||
Integer[] gravados = salvarClienteExcel(empresas, usaCPFComoFidelidade, cliente, tipoIdentificacionUno, tipoIdentificacionDoos);
|
||||
inseridos = gravados[0] + inseridos;
|
||||
atualizados = gravados[1] + atualizados;
|
||||
desconsiderados = gravados[2] + desconsiderados;
|
||||
}catch(Throwable e){
|
||||
log.error("Erro na gravação do registro. Favor revisar",e);
|
||||
log.info("continuando importação");
|
||||
}
|
||||
|
||||
} else {
|
||||
log.info(String.format("cliente:%s cpf:%s",cliente.getNome(),cliente.getCpf()));
|
||||
log.info("cliente não validado");
|
||||
erros.append("\n erro ao gravar o cliente na linha ");
|
||||
erros.append(index + 1).append(" do arquivo.");
|
||||
}
|
||||
}
|
||||
qtdeGravados.append("Gravados ").append(inseridos + atualizados).append(" clientes de ").append(index-1).append(" importados.\n");
|
||||
qtdeGravados.append("Desconsiderados ").append(desconsiderados).append(" clientes.\n");
|
||||
qtdeGravados.append("Atualizados ").append(atualizados).append(" clientes.\n");
|
||||
qtdeGravados.append("Inseridos ").append(inseridos).append(" novos clientes.");
|
||||
String[] resultado = { qtdeGravados.toString(), erros.toString() };
|
||||
|
||||
log.info("importação finalizada");
|
||||
|
||||
return resultado;
|
||||
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
e.printStackTrace();
|
||||
String msg = "Erro ao gravar cliente na linha " + (index + 1) + " do arquivo.";
|
||||
erros.append(msg);
|
||||
String[] resultado = { "Houve erro ao gravar os clientes, consulte o arquivo de erros.", erros.toString() };
|
||||
return resultado;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
erros.append("Linha ").append(index).append(" do arquivo de clientes, erro: ").append(e.getCause().getCause()).append("\n");
|
||||
String[] resultado = { "Houve erro ao gravar os clientes, consulte o arquivo de erros.", erros.toString() };
|
||||
return resultado;
|
||||
}
|
||||
|
||||
} else {
|
||||
erros.append("A estrutura do arquivo esta com erro ").append("\n");
|
||||
String[] resultado = { "Houve erro ao gravar os clientes, consulte o arquivo de erros.", erros.toString() };
|
||||
return resultado;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Integer[] salvarClienteExcel(List<Empresa> empresas, Boolean usaCPFComoFidelidade, ClienteExcelVo cliente, TipoIdentificacion tipoIdentificacionUno, TipoIdentificacion tipoIdentificacionDoos) throws ParseException {
|
||||
Integer inseridos = new Integer(0);
|
||||
Integer atualizados = new Integer(0);
|
||||
Integer desconsiderados = new Integer(0);
|
||||
Integer[] gravados = { 0, 0, 0 };
|
||||
Cliente clienteGravar = new Cliente();
|
||||
|
||||
List<Cliente> lsCliente = null;
|
||||
if(StringUtils.isNotEmpty(cliente.getCpf())) {
|
||||
lsCliente = clienteService.buscarPorDocumento(cliente.getCpf());
|
||||
}
|
||||
|
||||
if (lsCliente != null && !lsCliente.isEmpty()) {
|
||||
if (ApplicationProperties.getInstance().isCustomHabilitado(CustomEnum.IS_DESCONSIDERA_CLIENTE_NA_BASE.getDescricao())) {
|
||||
desconsiderados = desconsiderados + 1;
|
||||
gravados[2] = gravados[2] + desconsiderados;
|
||||
return gravados;
|
||||
} else {
|
||||
clienteGravar = lsCliente.get(0);
|
||||
}
|
||||
}else {
|
||||
Cliente clienteGravarAux = clienteService.buscarPorNumeroFidelidade(cliente.getNumfidelidade());
|
||||
if(clienteGravarAux != null) {
|
||||
clienteGravar = clienteGravarAux;
|
||||
}
|
||||
}
|
||||
|
||||
criarFidelidadesTodasEmpresas(clienteGravar, empresas, cliente);
|
||||
|
||||
clienteGravar.setFecnacimiento(StringUtils.isEmpty(cliente.getNascimento()) ? null : retornaDate(cliente.getNascimento()));
|
||||
clienteGravar.setDesccorreo(cliente.getEmail());
|
||||
clienteGravar.setNombcliente(cliente.getNome());
|
||||
clienteGravar.setNumfax(cliente.getFax());
|
||||
clienteGravar.setNumtelefono(cliente.getTelefone());
|
||||
clienteGravar.setNumtelefonodos(cliente.getCelular());
|
||||
clienteGravar.setIndsexo(cliente.getSexo());
|
||||
clienteGravar.setNumIdentificaUno(cliente.getCpf());
|
||||
clienteGravar.setTipoIdentificacionUno(tipoIdentificacionUno);
|
||||
if (!StringUtils.isEmpty(cliente.getRg())) {
|
||||
clienteGravar.setNumIdentificaDos(cliente.getRg());
|
||||
clienteGravar.setTipoIdentificacionDos(tipoIdentificacionDoos);
|
||||
}
|
||||
setDirecion(clienteGravar, cliente);
|
||||
if (clienteGravar.getClienteId() == null) {
|
||||
clienteGravar.setFecCadastro(new Date());
|
||||
clienteService.suscribir(clienteGravar);
|
||||
inseridos = inseridos + 1;
|
||||
gravados[0] = gravados[0] + inseridos;
|
||||
|
||||
} else {
|
||||
clienteService.actualizacion(clienteGravar);
|
||||
atualizados = atualizados + 1;
|
||||
gravados[1] = gravados[1] + atualizados;
|
||||
}
|
||||
|
||||
return gravados;
|
||||
}
|
||||
|
||||
private void setDirecion(Cliente cliente, ClienteExcelVo cExcel) {
|
||||
List<ClienteDireccion> lsDirecion = new ArrayList<ClienteDireccion>();
|
||||
ClienteDireccion direccion = new ClienteDireccion();
|
||||
if ((cliente.getLsClienteDireccion() != null)
|
||||
&& (!cliente.getLsClienteDireccion().isEmpty())) {
|
||||
if (cliente.getLsClienteDireccion().get(0) != null) {
|
||||
direccion = cliente.getLsClienteDireccion().get(0);
|
||||
cliente.getLsClienteDireccion().remove(0);
|
||||
}
|
||||
}
|
||||
direccion.setDesestado(cExcel.getEstado());
|
||||
direccion.setDesciudad(cExcel.getCidade());
|
||||
direccion.setDesccolonia(cExcel.getBairro());
|
||||
direccion.setDesccalle(cExcel.getEndereco());
|
||||
direccion.setActivo(true);
|
||||
direccion.setClienteId(cliente);
|
||||
lsDirecion.add(direccion);
|
||||
cliente.setLsClienteDireccion(lsDirecion);
|
||||
|
||||
}
|
||||
|
||||
private void criarFidelidadesTodasEmpresas(Cliente cliente, List<Empresa> empresas, ClienteExcelVo cExce) {
|
||||
|
||||
Boolean achou = false;
|
||||
List<ClienteFidelidad> fidelidades = null;
|
||||
|
||||
if (cliente.getClienteId() == null) {
|
||||
fidelidades = new ArrayList<ClienteFidelidad>();
|
||||
for (Empresa e : empresas) {
|
||||
fidelidades.add(criarFidelidade(e, cExce));
|
||||
}
|
||||
cliente.setListClienteFidelidad(fidelidades);
|
||||
|
||||
} else {
|
||||
fidelidades = cliente.getListClienteFidelidad();
|
||||
if (fidelidades == null || fidelidades.isEmpty()) {
|
||||
for (Empresa e : empresas) {
|
||||
fidelidades.add(criarFidelidade(e, cExce));
|
||||
}
|
||||
} else {
|
||||
for (Empresa e : empresas) {
|
||||
for (ClienteFidelidad f : fidelidades) {
|
||||
if (f.getEmpresa().equals(e)) {
|
||||
f.setActivo(true);
|
||||
f.setFecmodif(new Date());
|
||||
TarjetaFidelidad tarjetaFidelidad = f.getTarjetaFidelidad();
|
||||
if (tarjetaFidelidad != null) {
|
||||
tarjetaFidelidad.setActivo(true);
|
||||
tarjetaFidelidad.setFecmodif(new Date());
|
||||
}
|
||||
|
||||
achou = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!achou) {
|
||||
fidelidades.add(criarFidelidade(e, cExce));
|
||||
}
|
||||
achou = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private ClienteFidelidad criarFidelidade(Empresa e, ClienteExcelVo cExcel) {
|
||||
ClienteFidelidad clienteFidelidad = new ClienteFidelidad();
|
||||
TarjetaFidelidad tarjetaFidelidad = new TarjetaFidelidad();
|
||||
tarjetaFidelidad.setNumTarjeta(cExcel.getNumfidelidade());
|
||||
tarjetaFidelidad.setActivo(true);
|
||||
tarjetaFidelidad.setFecmodif(Calendar.getInstance().getTime());
|
||||
tarjetaFidelidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
|
||||
|
||||
clienteFidelidad.setEmpresa(e);
|
||||
clienteFidelidad.setActivo(true);
|
||||
clienteFidelidad.setFecmodif(Calendar.getInstance().getTime());
|
||||
clienteFidelidad.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
|
||||
clienteFidelidad.setTarjetaFidelidad(tarjetaFidelidad);
|
||||
|
||||
return clienteFidelidad;
|
||||
}
|
||||
|
||||
public Boolean validaDadosPlanilha(ClienteExcelVo cliente, StringBuilder erros) {
|
||||
|
||||
if (!StringUtils.isEmpty(cliente.getCpf())) {
|
||||
try {
|
||||
Long.parseLong(cliente.getCpf().trim().replace(" ", ""));
|
||||
if (!validarCPF(cliente.getCpf())) {
|
||||
cliente.setCpf(null);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
cliente.setCpf(null);
|
||||
} catch (Exception e) {
|
||||
e.getMessage();
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
erros.append(cliente.getNumfidelidade() + " - Documento e obrigatório.");
|
||||
erros.append("\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(cliente.getNome())) {
|
||||
erros.append(cliente.getNumfidelidade() + " - O NOME e obrigatório.");
|
||||
erros.append("\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!StringUtils.isEmpty(cliente.getSexo())) {
|
||||
if (!(cliente.getSexo().equalsIgnoreCase("f") || cliente.getSexo().equalsIgnoreCase("m") || cliente.getSexo().equalsIgnoreCase("masculino") || cliente.getSexo().equalsIgnoreCase("feminino"))) {
|
||||
//erros.append("Campo sexo foi informado incorretamente.");
|
||||
//return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!StringUtils.isEmpty(cliente.getNascimento()) && cliente.getNascimento().matches("(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)")) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
|
||||
try {
|
||||
dateFormat.parse(cliente.getNascimento());
|
||||
} catch (Exception e) {
|
||||
//erros.append("Campo Nascimento foi informado incorretamente.");
|
||||
//return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private Boolean validaSheet(Sheet sheet) {
|
||||
|
||||
Row row = sheet.getRow(0);
|
||||
Iterator<Cell> cellIterator = row.cellIterator();
|
||||
|
||||
while (cellIterator.hasNext()) {
|
||||
Cell cell = cellIterator.next();
|
||||
switch (cell.getColumnIndex()) {
|
||||
case 0:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.numfidelidade"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
|
||||
case 1:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.nome"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
case 2:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.tipodoc"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
case 3:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.numerodoc"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
|
||||
case 4:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.datanascimento"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
|
||||
case 5:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.nacionalidade"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
|
||||
case 6:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.paisresidencia"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
|
||||
case 7:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.ocupacao"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
|
||||
case 8:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.sexo"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
case 9:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.estadocivil"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
case 10:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.email"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
case 11:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.telefone"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
case 12:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.endereco"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
case 13:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.cidade"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
case 14:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.cep"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
case 15:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.observacao"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
case 16:
|
||||
if (cell.getStringCellValue().equalsIgnoreCase(Labels.getLabel("importarClientesControllerSrvp.HEADERCOLUMN.desconto"))) {
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private Date retornaDate(String date) throws ParseException {
|
||||
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
|
||||
Date data = null;
|
||||
data = (Date) dateFormat.parse(date);
|
||||
return data;
|
||||
|
||||
}
|
||||
|
||||
private Boolean validarCPF(String numDoc) {
|
||||
try {
|
||||
// https://dicasdeprogramacao.com.br/algoritmo-para-validar-cpf
|
||||
// O VAlidador não pegava os CPFs com todos os numeros iguais
|
||||
if (numDoc.length() != 11 || numDoc.equals("00000000000") || numDoc.equals("11111111111") || numDoc.equals("22222222222") ||
|
||||
numDoc.equals("33333333333") || numDoc.equals("44444444444") || numDoc.equals("55555555555") ||
|
||||
numDoc.equals("66666666666") || numDoc.equals("77777777777") || numDoc.equals("88888888888") ||
|
||||
numDoc.equals("99999999999")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Cpf cpf = new Cpf(numDoc);
|
||||
if (!cpf.isValid()) {
|
||||
return false;
|
||||
}
|
||||
} catch (ValidationException e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -33,6 +33,8 @@ public final class DateUtil {
|
|||
*/
|
||||
public static String ddMMaaHHmm = "dd/MM/yyyy hh:mm";
|
||||
public static String ddMMaa = "dd/MM/yyyy";
|
||||
public static String ddMMaa_anodoisdigitos = "dd/MM/yy";
|
||||
public static String ddMMaa_sembarra = "ddMMyy";
|
||||
public static String HHmm = "HH:mm";
|
||||
public static String formatGMT = "yyyy-MM-dd'T'HH:mm:ss";
|
||||
public static String formatJson = "yyyy-MM-dd'T'HH:mm:ss.SSS";
|
||||
|
|
Loading…
Reference in New Issue