package javaapp;
// Added by Bob, needed for AwareIM
import java.util.Collection;
import com.bas.shared.functions.IFunctionLibrary;
import com.bas.shared.data.FunctionDescription;
import com.bas.shared.ruleparser.INodeHelper;
import com.bas.shared.ruleparser.ISQLBuilderHelper;
// implements clause added by Bob, needed for AwareIM
public class fjCortes implements IFunctionLibrary {
// START of code added by Bob, needed for AwareIM
public static final String FUNCTION_VERIFICADORA = "DIGITO_NIT";
public static final String FUNCTION_MONTO_ESCRITO = "NUM_A_LETRAS";
public fjCortes ()
{
}
// This method is required by AwareIM. It just returns the library (class) name.
public String getName () { return "fjCortes"; }
// This method is required by AwareIM. It checks the input against the functions in this library.
// If there is a match, it returns true, otherwise it returns false.
public boolean hasFunction (String functionName, int paramNmb)
{
if (paramNmb == 0)
{
return functionName.equalsIgnoreCase(FUNCTION_VERIFICADORA);
}
else if (paramNmb == 1)
{
return functionName.equalsIgnoreCase(FUNCTION_MONTO_ESCRITO);
}
return false;
}
// This method is required by AwareIM. This returns all the functions in this library along with
// a short description
public FunctionDescription [] getAvailableFunctions ()
{
FunctionDescription [] funct = new FunctionDescription [2];
funct [0] = new FunctionDescription (FUNCTION_VERIFICADORA, "Parametro:String \nDevuelve: dígito de verificación para el NIT Colombiano según algoritmo DIAN");
funct [1] = new FunctionDescription (FUNCTION_MONTO_ESCRITO, "Parametro: valor con formato String \nDevuelve: cantidad numerica en un monto escrito");
return funct;
}
// This method is required by AwareIM. This is where you code your actual function.
// This calls your original program and returns the text
public Object calculate (String functionName, Object[] params, INodeHelper helper)
{
String resultString="";
// if (params != null && params.length == 0)
// {
if (functionName.equalsIgnoreCase(FUNCTION_VERIFICADORA))
{
String theNumber=(String) params[0];
System.out.println("Parametro Actual "+theNumber);
resultString=dVerificar(theNumber);
}
// }
// else if (params != null && params.length == 1)
// {
if (functionName.equalsIgnoreCase(FUNCTION_MONTO_ESCRITO))
{
String theNumber=(String) params[1];
resultString=toLetras(theNumber);
}
// }
return resultString;
}
// This method is required by AwareIM. This method must return the SQL representation of the function
// if it supports one. The SQL representation is essential if a function is to be used in queries.
// Since this function does not have a SQL representation, this method does not do anything.
public String toSQL (String functionName, Collection parameters, ISQLBuilderHelper sqlHelper)
throws Exception {
return null;
}
// This method is required by AwareIM.
// It returns the class type of the return values of your functions.
public Class getTypeClass (String functionName, Collection params) {
return String.class;
}
// END of code added by Bob, needed for AwareIM
// Funcion que con el numero del NIT devuelve el dígito de verificación segun algoritmo de la DIAN
public String dVerificar(String str)
{
int[] nums = {3, 7, 13, 17, 19, 23, 29, 37, 41, 43, 47, 53, 59, 67, 71};
int sum = 0;
// String str = String.valueOf(nit);
for (int i = str.length() - 1, j=0; i >= 0; i--, j++) {
sum += Character.digit(str.charAt(i), 10) * nums[j];}
byte dv = (byte)((sum % 11) > 1 ? (11 - (sum % 11)) : (sum % 11));
return String.valueOf(dv);
}
// Inicia la funcion que al ingresar un valor numerico hace los calculos necesarios para devolver un monto escrito del valor ingresado
private String Unidad[] = {"Cero","Uno","Dos","Tres","Cuatro","Cinco","Seis","Siete","Ocho","Nueve","Diez","Once","Doce","Trece","Catorce","Quince", "Dieciseis","Diecisiete","Dieciocho","Diecinueve","Veinte"};
private String Decena[] = {"Veinti", "Treinta", "Cuarenta", "Cincuenta", "Sesenta", "Setenta", "Ochenta", "Noventa"};
private String Centena[] ={"Cien","Doscientos","Trescientos","Cuatrocientos","Quinientos","Seiscientos","Setecientos","Ochosientos","Novecientos","Mil", "Un Millón","Millones","Un Billón","Billones"};
private String getUnidad(long numero)
{String aux="";
for(int p=0;p<=20;p++)
{if(numero== p)
{ aux = Unidad[p] + " ";
return aux;
}
}
return " ";
}
private String getDecena(long numero)
{String aux="";
long pf=numero%10;
long pi=(numero/10);
int p=0;
boolean sal=false;
//pf: parte final del numero,pi: parte inicial del numero
while(p<=8 & sal==false)
{if(pi== p+2)
{ aux = Decena[p];
sal=true;
}
p++;
}
if (pf==0)
return aux+" ";
if(numero>20 & numero<30)
return(aux+getUnidad(pf)+" ");
return aux+" y "+getUnidad(pf)+" ";
}
private String getCentena(long numero)
{String aux="",aux2="";
long pf=numero%100;
long pi=(numero/100);
int p=0;
boolean sal=false;
while(p<=10 & sal==false)
{if(pi== p+1)
{ aux = Centena[p];
sal=true;
}
p++;
}
if (pf==0)
return aux;
if (pf<21)
aux2=getUnidad(pf);
else
aux2=getDecena(pf);
if (numero<200)
return(aux+"to "+aux2+" ");
else
return(aux+" "+aux2+" ");
}
private String getMil(long numero)
{ String aux="",aux2="";
long pf=numero%1000;
long pi=(numero/1000);
long p=0;
if(numero==1000) return "Mil";
if (numero>1000 & numero < 1999)
aux=Centena[9]+" ";
else
aux=resolverIntervalo(pi)+Centena[9]+" ";
if (pf!=0)
return(aux+resolverIntervalo(pf)+" ");
return aux;
}
private String getMillon(long numero)
{ String aux="",aux2="";
if (numero==1000000)
{
return aux= "Un Millón de";
}
long pf=numero%1000000;
long pi=(numero/1000000);
long p=0;
if (numero>1000000 & numero < 1999999)
aux=Centena[10]+" ";
else
aux=resolverIntervalo(pi)+Centena[11]+" ";
if (pf!=0)
return(aux+resolverIntervalo(pf)+" ");
return aux;
}
private String getBillon(long numero)
{ String aux="",aux2="";
long pf=numero%1000000000;
long pi=(numero/1000000000);
long p=0;
if (numero>1000000000 & numero <1999999999)
aux=Centena[12]+" ";
else
aux=resolverIntervalo(pi)+Centena[13]+" ";
if (pf!=0)
return(aux+resolverIntervalo(pf)+" ");
return aux;
}
private String resolverIntervalo(long numero)
{ if(numero>=0 & numero<=20)
return getUnidad(numero);
if(numero>=21 & numero<=99)
return getDecena(numero);
if(numero>=100 & numero<=999)
return getCentena(numero);
if(numero>=1000 & numero<=999999)
return getMil(numero);
if(numero>=1000000 & numero<=999999999)
return getMillon(numero);
if(numero>=1000000000 & numero<=2000000000)
return getBillon(numero);
return "<<El número esta fuera del rango>>";
}
public String toLetras(String cantidad)
{
boolean isCent=false;
long numerosEnteros=0;
int numerosDecimales=0;
if (cantidad.startsWith("."))
{ isCent=true;
String enteros=cantidad.substring(1);
System.out.println(enteros);
try
{
numerosEnteros=Long.parseLong(enteros);
}
catch (NumberFormatException e){return "Formato Invalido";}
}
else
{
if (cantidad.indexOf(".")!=-1)
{
String enteros=cantidad.substring(0,cantidad.indexOf("."));
String decimales=cantidad.substring(cantidad.indexOf(".")+1);
try{
numerosEnteros=Long.parseLong(enteros);
numerosDecimales=Integer.parseInt(decimales);
}
catch (NumberFormatException e){return "Formato Invalido";}
}
else
{
try
{
numerosEnteros=Long.parseLong(cantidad);
}
catch (NumberFormatException e){return "Formato Invalido";}
}
}
if(numerosEnteros>1|numerosEnteros==0)
{
if (isCent)
{
String letras="Cero Pesos y "+resolverIntervalo(numerosEnteros)+"/100 M.N";
letras=letras.replaceAll(" "," ");
letras=letras.replaceAll(" "," ");
return letras.toUpperCase() ;
}
else
{
String letras=resolverIntervalo(numerosEnteros)+" Pesos "+numerosDecimales + "/100 M.N.";
letras=letras.replaceAll(" "," ");
letras=letras.replaceAll(" "," ");
return letras.toUpperCase() ;
}
}
if (numerosEnteros==1)
{
return ("UN PESO "+numerosDecimales +"/100 M.N.");
}
return null;
}
}