Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ak.rudder.control.launcher.process;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.math.BigDecimal;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.GregorianCalendar;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import java.util.Vector;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import com.ak.rudder.core.control.SQLLauncher;
- import com.ak.rudder.core.util.StringUtilities;
- public class SaftPt {
- static Connection theConection = null;
- static Map tmpSeriesFactura = null;
- static Map tmpPaises = null;
- static HashMap consulta = null;
- static BigDecimal ivaEcotasa = null;
- public void ejecutar(String aCompany, String aEmpresa, String aTo, String aFrom, String aPathSpool) throws Exception {
- boolean tmpIsPorCompany=aEmpresa.indexOf(',')!=-1;
- SimpleDateFormat mysqldf = new SimpleDateFormat("dd/MM/yyyy");
- consulta = new HashMap();
- consulta.put("company", aCompany);
- consulta.put("empresa", aEmpresa);
- consulta.put("fiscalyear", (String) Integer.toString(((Calendar) Calendar.getInstance()).get(Calendar.YEAR)));
- consulta.put("submit", "OK");
- consulta.put("from", aFrom);
- consulta.put("to", aTo);
- String tmpCuentaMayorCliente = "43000";
- String tmpPath = aPathSpool;
- String SQL = "";
- String sFichero = "";
- Vector errormessages = new Vector();
- Vector alertmessages = new Vector();
- HashMap empresa = null;
- HashMap company = null;
- HashMap clientes = new HashMap();
- Vector o_clientes = new Vector();
- HashMap cliente = null;
- HashMap ivas = new HashMap();
- Vector o_ivas = new Vector();
- HashMap iva = null;
- HashMap articulos = new HashMap();
- Vector o_articulos = new Vector();
- HashMap articulo = null;
- HashMap facturas = new HashMap();
- Vector o_facturas = new Vector();
- HashMap factura = null;
- HashMap lineas = new HashMap();
- Vector o_lineas = new Vector();
- HashMap linea = null;
- Iterator it = null;
- Iterator it2 = null;
- Calendar t1 = null;
- Calendar t2 = null;
- /****
- * LLAMADA A LA HASH MANUALMENTE EN CASO DE QUE ALGUNA VEZ FALLE
- */
- // SaftptHash tmpSaftptHash = new SaftptHash();
- //
- // try {
- // tmpSaftptHash.guardarHash("TE", "07", "7V", "17993");
- // tmpSaftptHash.guardarHash("TE", "07", "7V", "17994");
- // tmpSaftptHash.guardarHash("TE", "07", "7V", "17995");
- // tmpSaftptHash.guardarHash("TE", "07", "7V", "17996");
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- java.util.Date dnow = new java.util.Date();
- Calendar cnow = new GregorianCalendar();
- cnow.setTime(dnow);
- SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy-MM-dd");
- SimpleDateFormat sdf_db2 = new SimpleDateFormat("yyMMdd");
- SimpleDateFormat sdf_pre = new SimpleDateFormat("yyyy");
- SimpleDateFormat sdf_kaka = new SimpleDateFormat("yyyyMMdd");
- SimpleDateFormat sdf_time = new SimpleDateFormat("HHmmss");
- if (consulta.containsKey("submit") && !consulta.get("submit").equals("")) {
- if (consulta.get("empresa").equals("")) {
- errormessages.add("El parámetro 'Empresa' está vacío ");
- }
- if (consulta.get("company").equals("")) {
- errormessages.add("El parámetro 'Company' está vacío ");
- }
- if (consulta.get("fiscalyear").equals("")) {
- errormessages.add("El parámetro 'Año Fiscal' está vacío ");
- }
- try {
- mysqldf = new SimpleDateFormat("dd/MM/yy");
- consulta.put("dfrom", mysqldf.parse((String) consulta.get("from")));
- } catch (Exception e1) {
- try {
- mysqldf = new SimpleDateFormat("dd/MM/yyyy");
- consulta.put("dfrom", mysqldf.parse((String) consulta.get("from")));
- } catch (Exception e2) {
- try {
- mysqldf = new SimpleDateFormat("yyyy-MM-dd");
- consulta.put("dfrom", mysqldf.parse((String) consulta.get("from")));
- } catch (Exception e3) {
- errormessages.add("El parámetro 'Desde' no contiene el formato correcto");
- mysqldf = new SimpleDateFormat("dd/MM/yyyy");
- consulta.put("dfrom", mysqldf.parse("01/01/2011")); // SimpleDateFormat
- }
- }
- }
- try {
- mysqldf = new SimpleDateFormat("dd/MM/yy");
- consulta.put("dto", mysqldf.parse((String) consulta.get("to")));
- } catch (Exception e1) {
- try {
- mysqldf = new SimpleDateFormat("dd/MM/yyyy");
- consulta.put("dto",mysqldf.parse((String) consulta.get("to")));
- } catch (Exception e2) {
- try {
- mysqldf = new SimpleDateFormat("yyyy-MM-dd");
- consulta.put("dto",mysqldf.parse((String) consulta.get("to")));
- } catch (Exception e3) {
- errormessages.add("El parámetro 'Hasta' no contiene el formato correcto");
- mysqldf = new SimpleDateFormat("dd/MM/yyyy");
- consulta.put("dto", mysqldf.parse("31/12/2011")); // SimpleDateFormat
- }
- }
- }
- if (errormessages.size() == 0) {
- SQL = "SELECT TRIM(MAQCUE) AS MAQCUE, MAQCIF, MAQNOM, MAQNOC, MAQDIR, MAQPOB, MAQPRO, MAQDIP, MAQTEL, MAQTLX " // la primera empresa saft es de la que pillara los datos
- + "FROM MAEQF WHERE MAQEMP = '"+consulta.get("company") +"' AND MAQTIR = 'Q' AND TRIM(MAEQF.MAQCUE) = '" + consulta.get("empresa").toString().split("'")[1].trim() + "' ";
- t1 = Calendar.getInstance();
- System.out.println("<!-- // SQL[MAEQF]: " + SQL + " -->");
- empresa = getDataByField(SQL);
- t2 = Calendar.getInstance();
- System.out.println("<!-- // HASH[MAEQF]: " + empresa + " -->");
- System.out.println("<!-- // STAMP: " + t2.getTimeInMillis() + " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
- checkPoblacion(errormessages, empresa, "MAQ");
- }
- if (errormessages.size() == 0) {
- String tmpDatosCompany = "SELECT MAENOM, MAEDIR, MAEPOB, MAEPRO, MAEDIP, MAETEL, MAETLX, MAECIF "
- + "FROM MAEEF A WHERE MAECUE='"+consulta.get("company") +"'";
- System.out.println("<!-- // SQL[MAEEF]: " + tmpDatosCompany + " -->");
- company = getDataByField(tmpDatosCompany);
- checkPoblacion(errormessages, company, "MAE");
- }
- // Consulta clientes
- if (errormessages.size() == 0) {
- String tmpClientesConALMGF = ""
- + "SELECT ALMGF.ALGCLI FROM ALMGF AS ALMGF WHERE "
- + "ALMGF.ALGEMP = '"
- + consulta.get("company")
- + "' "
- + "AND ALMGF.ALGTIA = 'V' "
- + "AND ALMGF.ALGSF1 = '' "
- + "AND ALMGF.ALGSF2 = '' "
- + "AND ALMGF.ALGNUD <> 0 "
- + "AND ALMGF.ALGTIR = 'F' "
- // + "AND ALMGF.ALGTEM = '"
- // + consulta.get("empresa")
- // + "' "
- + "AND ALMGF.ALGTEM in ("+consulta.get("empresa").toString()+") "
- + "AND ALMGF.ALGEST IN ('FRA', 'ABO') "
- + "AND CONCAT(ALGA21, ALGN21) BETWEEN '"
- + sdf_kaka.format((java.util.Date) consulta.get("dfrom")) + "' AND '"
- + sdf_kaka.format((java.util.Date) consulta.get("dto")) + "' ";
- Vector tmpResultClientesConALMGF = SQLLauncher.executeQuery(tmpClientesConALMGF, Integer.MAX_VALUE, 0, false);
- String strClientes = "";
- if (tmpResultClientesConALMGF != null && tmpResultClientesConALMGF.size() > 0) {
- Vector row = null;
- for ( int i=0; i<tmpResultClientesConALMGF.size(); i++ ) {
- row = (Vector)tmpResultClientesConALMGF.get(i);
- strClientes += "'"+row.get(0).toString()+"',";
- }
- strClientes = strClientes.substring(0,strClientes.length()-1);
- }
- SQL = "SELECT "
- + "TRIM(SUBSTR(MAECF.MACCUE, 6)) as MACCUE, "
- + "MAECF.MACCIF as MACCIF, "
- + "MAECF.MACEMP as MACEMP, "
- + "MAECF.MACRPI as MACRPI, "
- + "MAECF.MACNOC as MACNOC, "
- + "EXCCFCN.EXCTE1 as EXCNOC, "
- + "MAECF.MACNOM as MACNOM, "
- + "EXCCFCN.EXCTE4 as EXCNOM, "
- + "EXCCFPC.EXCTE1 as EXCTE1, "
- + "MAECF.MACDIR AS MACDIR, "
- + "EXCCFCN.EXCTE2 as EXCDIR, "
- + "MAECF.MACPOB AS MACPOB, "
- + "EXCCFCN.EXCTE3 as EXCPOB, "
- + "MAECF.MACPRO AS MACPRO, "
- + "MAECF.MACDIP AS MACDIP "
- + "FROM MAECF "
- + "LEFT JOIN "
- + " EXCCF AS EXCCFPC "
- + "ON ( "
- + "MAECF.MACEMP = EXCCFPC.EXCEMP "
- + "AND MAECF.MACTIR = EXCCFPC.EXCTIR "
- + "AND EXCCFPC.EXCTIE = 'PC' "
- + "AND MAECF.MACCUE = EXCCFPC.EXCCUE "
- + "AND EXCCFPC.EXCFOR = '1' "
- + ") "
- + "LEFT JOIN "
- + " EXCCF AS EXCCFCN "
- + "ON ( "
- + "MAECF.MACEMP = EXCCFCN.EXCEMP "
- + "AND MAECF.MACTIR = EXCCFCN.EXCTIR "
- + "AND EXCCFCN.EXCTIE = 'CN' "
- + "AND MAECF.MACCUE = EXCCFCN.EXCCUE "
- + "AND EXCCFCN.EXCFOR = '1' "
- + ") "
- + "WHERE MAECF.MACEMP='"
- + consulta.get("company")
- + "' AND MAECF.MACCUE IN ("+strClientes+")";
- t1 = Calendar.getInstance();
- System.out.println("<!-- // SQL[MAECF]: " + SQL + " -->");
- cargarDatos(SQL, clientes, o_clientes, "MACCUE");
- t2 = Calendar.getInstance();
- System.out.println("<!-- // HASH[MAECF]: " + clientes + " -->");
- System.out.println("<!-- // STAMP: " + t2.getTimeInMillis() + " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
- }
- if (errormessages.size() == 0) {
- SQL = "SELECT MAEJF.MAJELE as MAJELE, MAEJF.MAJNOM as MAJNOM, MAEJF.MAJVA1 as MAJVA1, MAEJF.MAJVA2 as MAJVA2 "
- + "FROM MAEJF AS MAEJF " + "WHERE " + "MAEJF.MAJEMP = '" + consulta.get("company") + "' "
- + "AND MAEJF.MAJTIR = 'J' AND MAEJF.MAJCUE = 'IV' AND MAEJF.MAJELE IN ( "
- + "SELECT DISTINCT "
- + "ALMHF.ALHTIV AS ALHTIV "
- + "FROM "
- + " ALMHF AS ALMHF "
- + "WHERE "
- + "ALMHF.ALHEMP = '"
- + consulta.get("company")
- + "' "
- + "AND ALMHF.ALHTIA = 'V' "
- + "AND ALMHF.ALHSE1 = '' "
- + "AND ALMHF.ALHSE2 = '' "
- + "AND ALMHF.ALHSF1 = ' ' "
- + "AND ALMHF.ALHNUD <> 0 "
- + "AND ALMHF.ALHTIR = 'H' "
- + "AND ALHENV >= 0 "
- + "AND ALHLIN >= 0 "
- // + "AND ALMHF.ALHTEM = '"
- // + consulta.get("empresa") + "' "
- + "AND ALMHF.ALHTEM in ("+consulta.get("empresa").toString()+") "
- + "AND ALMHF.ALHEST IN ('FRA', 'ABO') "
- + "AND ALMHF.ALHAFF BETWEEN '"
- + sdf_pre.format((java.util.Date) consulta.get("dfrom")).substring(0, 2) + "' "
- + "AND '" + sdf_pre.format((java.util.Date) consulta.get("dto")).substring(0, 2) + "' "
- + "AND ALMHF.ALHFFR BETWEEN '" + sdf_db2.format((java.util.Date) consulta.get("dfrom")) + "' "
- + "AND '" + sdf_db2.format((java.util.Date) consulta.get("dto")) + "' "
- + ") " + "ORDER BY " + "MAEJF.MAJNOM";
- t1 = Calendar.getInstance();
- System.out.println("<!-- // SQL[MAEJF]: " + SQL + " -->");
- cargarDatos(SQL, ivas, o_ivas, "MAJELE");
- t2 = Calendar.getInstance();
- System.out.println("<!-- // HASH[MAEJF]: " + ivas + " -->");
- System.out.println("<!-- // STAMP: " + t2.getTimeInMillis() + " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
- SQL = "SELECT ALGEMP FROM ALMGF AS ALMGF WHERE "
- + "ALMGF.ALGEMP = '" + consulta.get("company")+ "' "
- + "AND ALMGF.ALGTIA = 'V' "
- + "AND ALMGF.ALGSF1 = '' "
- + "AND ALMGF.ALGSF2 = '' "
- + "AND ALMGF.ALGNUD <> 0 "
- + "AND ALMGF.ALGTIR = 'F' "
- // + "AND ALMGF.ALGTEM = '"
- // + consulta.get("empresa") + "' "
- + "AND ALMGF.ALGTEM in ("+consulta.get("empresa").toString()+") "
- + "AND ALMGF.ALGEST IN ('FRA', 'ABO') "
- + "AND CONCAT(ALGA21, ALGN21) BETWEEN '"
- + sdf_kaka.format((java.util.Date) consulta.get("dfrom")) + "' AND '"
- + sdf_kaka.format((java.util.Date) consulta.get("dto")) + "' "
- + "AND ALMGF.ALGTIV = '0' ";
- Vector tmpResultSQL = SQLLauncher.executeQuery(SQL, Integer.MAX_VALUE, 0, false);
- if (tmpResultSQL != null && tmpResultSQL.size() > 0) {
- SQL = "SELECT MAEJF.MAJELE as MAJELE, "
- + "MAEJF.MAJNOM as MAJNOM, "
- + "MAEJF.MAJVA1 as MAJVA1, "
- + "MAEJF.MAJVA2 as MAJVA2 " + "FROM "
- + " MAEJF AS MAEJF " + "WHERE "
- + "MAEJF.MAJEMP = '" + consulta.get("company")
- + "' " + "AND MAEJF.MAJTIR = 'J' "
- + "AND MAEJF.MAJCUE = 'IV' "
- + "AND MAEJF.MAJELE = '0' ";
- iva = getDataByField(SQL);
- if (iva.size() > 0) {
- ivas.put(iva.get("MAJELE").toString().trim(), iva);
- o_ivas.add("0"); // si no, no pone el iva exento en el desglose superior del XML tabla TaxTable
- }
- System.out.println("<!-- // HASH[MAEJF]: " + ivas + " -->");
- }
- }
- // Consulta articulos
- if (errormessages.size() == 0) {
- SQL = "SELECT MAEAF.MAACUE AS MAACUE, "
- + "MAEAF.MAANOM AS MAANOM, "
- + "MAEAF.MAAFAM AS MAAFAM, "
- + "AUXAF.AUACO1 AS AUACO1 FROM "
- + " MAEAF AS MAEAF LEFT JOIN AUXAF AS AUXAF "
- + "ON ( MAEAF.MAAEMP = AUXAF.AUAEMP "
- + "AND MAEAF.MAATIR = AUXAF.AUATIR "
- + "AND MAEAF.MAACUE = AUXAF.AUACUE ) WHERE "
- + "MAEAF.MAAEMP = '" + consulta.get("company") + "' "
- + "AND MAEAF.MAATIR = 'A' "
- + "AND MAEAF.MAACUE IN ( "
- + "SELECT DISTINCT "
- + "ALMHF.ALHART AS ALHART "
- + "FROM "
- + " ALMHF AS ALMHF "
- + "WHERE ALMHF.ALHEMP = '"
- + consulta.get("company") + "' "
- + "AND ALMHF.ALHTIA = 'V' "
- + "AND ALMHF.ALHSE1 = '' "
- + "AND ALMHF.ALHSE2 = '' "
- + "AND ALMHF.ALHSF1 = ' ' "
- + "AND ALMHF.ALHNUD <> 0 "
- + "AND ALMHF.ALHTIR = 'H' "
- + "AND ALHENV >= 0 "
- + "AND ALHLIN >= 0 "
- // + "AND ALMHF.ALHTEM = '" + consulta.get("empresa") + "' "
- + "AND ALMHF.ALHTEM in ("+consulta.get("empresa").toString()+") "
- + "AND ALMHF.ALHEST IN ('FRA', 'ABO') "
- + "AND ALHAFF BETWEEN '"
- + sdf_pre.format((java.util.Date) consulta.get("dfrom")).substring(0, 2)
- + "' AND '" + sdf_pre.format((java.util.Date) consulta.get("dto")).substring(0, 2) + "' "
- + "AND ALHFFR BETWEEN '" + sdf_db2 .format((java.util.Date) consulta.get("dfrom")) + "' AND '"
- + sdf_db2.format((java.util.Date) consulta.get("dto")) + "' " + ") ";
- t1 = Calendar.getInstance();
- System.out.println("<!-- // SQL[MAEAF]: " + SQL + " -->");
- cargarDatos(SQL, articulos, o_articulos, "MAACUE");
- t2 = Calendar.getInstance();
- System.out.println("<!-- // HASH[MAEAF]: " + articulos + " -->");
- System.out.println("<!-- // STAMP: " + t2.getTimeInMillis() + " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
- }
- // Consulta facturas
- if (errormessages.size() == 0) {
- // Calculamos las ventas
- String SQL_ALMGF = "SELECT "
- + "CONCAT(CONCAT(ALMGF.ALGSE2, '/'), ALMGF.ALGNUD) AS HASKEY, "
- + "ALMGF.ALGEMP AS ALGEMP, "
- + "ALMGF.ALGTIA AS ALGTIA, "
- + "ALMGF.ALGNUD AS ALGNUD, "
- + "ALMGF.ALGALB AS ALGALB, "
- + "ALMGF.ALGSE1 AS ALGSE1, "
- + "ALMGF.ALGSE2 AS ALGSE2, "
- + "ALMGF.ALGTIR AS ALGTIR, "
- + "TRIM(SUBSTR(ALMGF.ALGCLI, 6)) AS ALGCLI, "
- + "ALMGF.ALGNOC AS ALGNOC, "
- + "ALMGF.ALGENV AS ALGENV, "
- + "ALMGF.ALGLIN AS ALGLIN, "
- + "ALMGF.ALGREF AS ALGREF, "
- + "ALMGF.ALGEST AS ALGEST, "
- // Totales de la factura
- + "ALMGF.ALGVPE AS ALGVPE, "
- + "ALMGF.ALGVSE AS ALGVSE, "
- + "ALMGF.ALGN10 AS ALGN10, "
- + "(ALMGF.ALGVSE + ALMGF.ALGN10) AS ALGSUM, "
- // Descuentos
- + "ALMGF.ALGDEM AS ALGDEM, "
- + "ALMGF.ALGPPG AS ALGPPG, "
- // Regargo de Equivalencia
- + "ALMGF.ALGTIV AS ALGTIV, "
- // Fecha Factura
- + "ALMGF.ALGA21 AS ALGA21, "
- + "ALMGF.ALGN21 AS ALGN21, "
- // Fecha y Hora creación del documento
- + "ALMGF.ALGFCR AS ALGFCR, "
- + "ALMGF.ALGHCR AS ALGHCR, "
- + "ALMGF.ALGOP2 AS ALGOP2, "
- + "ALMGF.ALGOPR AS ALGOPR, "
- + "ALMGF.ALGTEM AS ALGTEM, "
- + "ALMGF.ALGEMP AS ALGEMP, "
- + "ALMGF.ALGPOR AS ALGPOR, "
- + "ALMGF.ALGA72 AS ALGA72, "
- + "ALMGF.ALGN74 AS ALGN74, "
- + "ALMGF.ALGN55 AS ALGN55 "
- + "FROM "
- + " ALMGF AS ALMGF "
- + "WHERE "
- + "ALMGF.ALGEMP = '"
- + consulta.get("company")
- + "' "
- + "AND ALMGF.ALGTIA = 'V' "
- + "AND ALMGF.ALGSF1 = '' "
- + "AND ALMGF.ALGSF2 = '' "
- + "AND ALMGF.ALGNUD <> 0 "
- + "AND ALMGF.ALGTIR = 'F' " // Siempre 'G'
- // + "AND ALMGF.ALGTEM = '"
- // + consulta.get("empresa")
- // + "' "
- + "AND ALMGF.ALGTEM in ("+consulta.get("empresa").toString()+") "
- // Seleccion de Articulos
- + "AND ALMGF.ALGEST IN ('FRA', 'ABO') "
- // AITOR 17/11/201
- // Fecha de Facturación = ALGA21 + ALGN21
- + "AND CONCAT(ALGA21, ALGN21) BETWEEN '"
- + sdf_kaka.format((java.util.Date) consulta.get("dfrom")) + "' AND '"
- + sdf_kaka.format((java.util.Date) consulta.get("dto")) + "' ";
- // Calculamos las lineas
- String SQL_ALMHF = "SELECT "
- + "CONCAT(CONCAT(ALMHF.ALHSF2, '/'), ALMHF.ALHFRA) AS HASKEY, "
- + "CONCAT(CONCAT(ALMHF.ALHLIN, '/'), ALMHF.ALHENV) AS SUBKEY, "
- + "ALMHF.ALHNUD AS ALHNUD,"
- + "ALMHF.ALHSF1 AS ALHSF1,"
- + "ALMHF.ALHSF2 AS ALHSF2,"
- + "ALMHF.ALHFRA AS ALHFRA,"
- + "ALMHF.ALHLIN AS ALHLIN, "
- + "ALMHF.ALHART AS ALHART, "
- + "ALMHF.ALHNOA AS ALHNOA, "
- + "ALMHF.ALHN07 AS ALHN07, " // Cantidad
- + "ALMHF.ALHN09 AS ALHN09, " // C??
- + "(ALMHF.ALHPRE/ALMHF.ALHUPV) AS ALHPRE, " // PVP unitario en Almacen
- + "ALMHF.ALHIMP AS ALHIMP, " // Importe de la linea
- + "(ALMHF.ALHIMP * ALMHF.ALHIVA/100) AS ALHAIT, " // Porcentaje de IVA (Dato derivado)
- + "ALMHF.ALHTIV AS ALHTIV, " // Tipo de IVA
- + "ALMHF.ALHIVA AS ALHIVA, " // Porcentaje de IVA (Dato derivado)
- + "ALMHF.ALHUPV AS ALHUPV, " // Unidad precio de Venta
- + "CONCAT(ALMHF.ALHAFP, ALMHF.ALHFPR) AS ALHFPR, " // Fecha de Servicio (en portugal igual a la de factura
- + "CONCAT(ALMHF.ALHAFF, ALMHF.ALHFFR) AS ALHFFR, " // Fecha factura
- + "ALMHF.ALHTDC AS ALHTDC " // total descuentos linea
- + "FROM " + " ALMHF AS ALMHF " + "WHERE "
- + "ALMHF.ALHEMP = '"
- + consulta.get("company") + "' "
- + "AND ALMHF.ALHTIA = 'V' " // Tipo de aplicación : 'V' de Ventas
- + "AND ALMHF.ALHSF1 = ' ' "
- + "AND ALMHF.ALHNUD <> 0 "
- + "AND ALMHF.ALHTIR = 'H' " // Siempre 'H'
- + "AND ALHENV >= 0 "
- + "AND ALHLIN >= 0 "
- // + "AND ALMHF.ALHTEM = '"
- // + consulta.get("empresa") + "' "
- + "AND ALMHF.ALHTEM in ("+consulta.get("empresa").toString()+") "
- + "AND ALMHF.ALHEST IN ('FRA', 'ABO') "
- + "AND ALHAFF BETWEEN '"
- + sdf_pre.format((java.util.Date) consulta.get("dfrom")).substring(0, 2)
- + "' AND '" + sdf_pre.format((java.util.Date) consulta.get("dto")) .substring(0, 2) + "' "
- + "AND ALHFFR BETWEEN '" + sdf_db2.format((java.util.Date) consulta.get("dfrom"))
- + "' AND '" + sdf_db2.format((java.util.Date) consulta.get("dto")) + "' ";
- // Gestionamos primero las inconsistencais
- SQL = SQL_ALMHF
- + "AND NOT EXISTS ("
- + SQL_ALMGF
- + " AND (ALMGF.ALGSE2 CONCAT '/' CONCAT ALMGF.ALGNUD) = (ALMHF.ALHSF2 CONCAT '/' CONCAT ALMHF.ALHFRA) "
- + ") ";
- t1 = Calendar.getInstance();
- System.out.println("<!-- // SQL[inconsistencias]: " + SQL + " -->");
- // HashMap elems = web_getElems(SQL);
- Vector tmpResultSQL = SQLLauncher.executeQuery(SQL, Integer.MAX_VALUE, 0, false);
- t2 = Calendar.getInstance();
- System.out.println("<!-- // STAMP: " + t2.getTimeInMillis() + " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
- if (tmpResultSQL.size() > 0) {
- errormessages.add("Incoherencia entre lineas y cabecera, factura '" + ((Vector) tmpResultSQL.get(0)).get(4).toString() + "/"
- + ((Vector) tmpResultSQL.get(0)).get(2).toString() + "'.");
- }
- if (errormessages.size() == 0) {
- t1 = Calendar.getInstance();
- System.out.println("<!-- // SQL[ALMGF]: " + SQL_ALMGF + " -->");
- cargarDatos(SQL_ALMGF, facturas, o_facturas);
- // si la factura tiene portes, los añadimos como una linea mas
- cargarArticuloPortes(consulta, articulos, o_articulos, facturas, o_facturas);
- cargarArticuloECO(consulta, articulos, o_articulos, facturas, o_facturas);
- t2 = Calendar.getInstance();
- System.out.println("<!-- // STAMP: " + t2.getTimeInMillis() + " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
- t1 = Calendar.getInstance();
- System.out.println("<!-- // SQL[ALMHF]: " + SQL_ALMHF + " -->");
- cargarLineasAFacturas(SQL_ALMHF, facturas, "lineas", "HASKEY", "SUBKEY");
- // web_addSubElems(SQL_ALMHF, facturas, "lineas", "HASKEY", "SUBKEY");
- t2 = Calendar.getInstance();
- System.out.println("<!-- // STAMP: " + t2.getTimeInMillis()+ " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
- }
- // Verificar que toda factura contenga lineas
- it = facturas.keySet().iterator();
- while (it.hasNext()) {
- HashMap elem = (HashMap) facturas.get(it.next());
- if (!elem.containsKey("lineas") || ((HashMap) elem.get("lineas")).size() == 0) {
- errormessages .add("Factura sin lineas, numero factura '" + elem.get("ALGSE2") + "/" + elem.get("ALGNUD") + "'.");
- }
- }
- }
- }
- if (errormessages.size() == 0 && consulta.containsKey("submit") && !consulta.get("submit").equals("")) {
- sFichero = tmpPath + "saftpt" + "-" + sdf_kaka.format((java.util.Date) consulta.get("dfrom")) + "-"
- + sdf_kaka.format((java.util.Date) consulta.get("dto")) + "-" + sdf_kaka.format(dnow) + sdf_time.format(dnow) + ".xml";
- File ficheroSaft = new File(sFichero);
- if(!ficheroSaft.exists()) {
- ficheroSaft.createNewFile();
- }
- BufferedWriter bw = new BufferedWriter(new FileWriter(ficheroSaft));
- String tmpNif, tmpMaqnom, tmpMaqdir, tmpMaqpob, tmpMaqzip, tmpMaqtel, tmpMaqfax;
- if(tmpIsPorCompany){
- //por company
- tmpNif=((String)company.get("MAECIF")).trim();
- tmpMaqnom=((String)company.get("MAENOM")).trim();
- tmpMaqdir=((String) company.get("MAEDIR")).replace('&', ' ').trim();
- tmpMaqpob=((String) company.get("MAEPOB")).trim();
- tmpMaqzip=((String) company.get("MAEZIP")).trim();
- tmpMaqtel=((String)company.get("MAETEL")).trim();
- tmpMaqfax=((String)company.get("MAETLX")).trim();
- }else{
- //por empresa
- tmpNif=((String)empresa.get("MAQCIF")).trim();
- tmpMaqnom=((String)empresa.get("MAQNOM")).trim();
- tmpMaqdir=((String) empresa.get("MAQDIR")).replace('&', ' ').trim();
- tmpMaqpob=((String) empresa.get("MAQPOB")).trim();
- tmpMaqzip=((String) empresa.get("MAQZIP")).trim();
- tmpMaqtel=((String)empresa.get("MAQTEL")).trim();
- tmpMaqfax=((String)empresa.get("MAQTLX")).trim();
- }
- bw.write("<?xml version=\"1.0\" encoding=\"Windows-1252\"?>\r\n");
- bw.write("<AuditFile xmlns=\"urn:OECD:StandardAuditFile-Tax:PT_1.03_01\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n");
- bw.write("\t<Header>\r\n");
- bw.write("\t\t<AuditFileVersion>1.03_01</AuditFileVersion>\r\n");
- bw.write("\t\t<CompanyID>" + tmpNif + "</CompanyID>\r\n");
- bw.write("\t\t<TaxRegistrationNumber>" + tmpNif + "</TaxRegistrationNumber>\r\n");
- bw.write("\t\t<TaxAccountingBasis>F</TaxAccountingBasis>\r\n");
- bw.write("\t\t<CompanyName>" + tmpMaqnom + "</CompanyName>\r\n");
- bw.write("\t\t<CompanyAddress>\r\n");
- bw.write("\t\t\t<AddressDetail>" + tmpMaqdir + "</AddressDetail>\r\n");
- bw.write("\t\t\t<City>" + tmpMaqpob + "</City>\r\n");
- bw.write("\t\t\t<PostalCode>" + tmpMaqzip + "</PostalCode>\r\n");
- bw.write("\t\t\t<Country>PT</Country>\r\n");
- bw.write("\t\t</CompanyAddress>\r\n");
- bw.write("\t\t<FiscalYear>" + consulta.get("fiscalyear") + "</FiscalYear>\r\n");
- bw.write("\t\t<StartDate>");
- bw.write(sdf_date.format((java.util.Date) consulta.get("dfrom")));
- bw.write("</StartDate>\r\n");
- bw.write("\t\t<EndDate>");
- bw.write(sdf_date.format((java.util.Date) consulta.get("dto")));
- bw.write("</EndDate>\r\n");
- bw.write("\t\t<CurrencyCode>" + "EUR" + "</CurrencyCode>\r\n");
- bw.write("\t\t<DateCreated>");
- bw.write(sdf_date.format(cnow.getTime()));
- bw.write("</DateCreated>\r\n");
- bw.write("\t\t<TaxEntity>" + "Global" + "</TaxEntity>\r\n");
- bw.write("\t\t<ProductCompanyTaxID>" + tmpNif + "</ProductCompanyTaxID>\r\n");
- bw.write("\t\t<SoftwareCertificateNumber>" + "0" + "</SoftwareCertificateNumber>\r\n");
- bw.write("\t\t<ProductID>Rudder/Gabinete de Informatica AK</ProductID>\r\n");
- bw.write("\t\t<ProductVersion>07.00.0000</ProductVersion>\r\n");
- bw.write("\t\t<Telephone>" + tmpMaqtel + "</Telephone>\r\n");
- bw.write("\t\t<Fax>" + tmpMaqfax + "</Fax>\r\n");
- bw.write("\t</Header>\r\n");
- bw.write("\t<MasterFiles>\r\n");
- it = o_clientes.iterator();
- while (it.hasNext()) {
- String o = (String) it.next();
- cliente = (HashMap) clientes.get(o);
- bw.write("\t\t<Customer>\r\n");
- bw.write("\t\t\t<CustomerID>");
- bw.write(tmpCuentaMayorCliente + StringUtilities.fillLeftSpaces(cliente.get("MACCUE").toString().trim(),6));
- bw.write("</CustomerID>\r\n");
- bw.write("\t\t\t<AccountID>" + "Desconhecido" + "</AccountID>\r\n");
- bw.write("\t\t\t<CustomerTaxID>" + cliente.get("MACCIF") + "</CustomerTaxID>\r\n");
- bw.write("\t\t\t<CompanyName>");
- bw.write(((String) cliente.get("MACNOM")).replace('&', ' '));
- if (!((String) cliente.get("EXCNOM")).trim().equals("")) {
- bw.write("\r\n" + ((String) cliente.get("EXCNOM")));
- }
- bw.write("</CompanyName>\r\n");
- bw.write("\t\t\t<BillingAddress>\r\n");
- String tmpMacdip=(String)cliente.get("MACDIP");
- if(tmpMacdip.equals("")) tmpMacdip="PT";
- String tmpMacemp=(String)cliente.get("MACEMP");
- String tmpCountry="";
- if (!tmpMacdip.toString().trim().equals("99")) { // Todo lo que no sea 99 se considera Portugal.
- tmpCountry="PT";
- }else{
- tmpCountry = getPaises(tmpMacemp).get(tmpMacdip.trim()).toString();
- }
- bw.write("\t\t\t\t\r<Country>"+tmpCountry+"</Country>\r\n");
- bw.write("\t\t\t</BillingAddress>\r\n");
- bw.write("\t\t\t<SelfBillingIndicator>0</SelfBillingIndicator>\r\n");
- bw.write("\t\t</Customer>\r\n");
- // bw.write("\t\t\t\t<AddressDetail>");
- // bw.write(((String) cliente.get("MACDIR")).replace('&', ' '));
- //
- // if (!((String) cliente.get("EXCDIR")).trim().equals("")) {
- // bw.write("\r\n" + ((String) cliente.get("EXCDIR")).replace('&', ' '));
- // }
- // bw.write("</AddressDetail>\r\n");
- // String codigoPostal = (String) cliente.get("MACPRO") + (String) cliente.get("MACDIP");
- // String poblacion = ((String) cliente.get("MACPOB"));
- // String provincia = ((String) cliente.get("MACPRO"));
- // String country = "ES";
- //
- // if ( provincia.equals("99" ) ) {
- // country = getPaises(cliente.get("MACEMP").toString()).get(cliente.get("MACDIP").toString().trim()).toString();
- // codigoPostal = cliente.get("MACDIP").toString();
- // }
- //
- // if (cliente.get("MACDIP").toString().trim().equals("P")) {
- //
- // Pattern p = Pattern.compile("[0-9]{4}-[0-9]{3}");
- // Matcher m = p.matcher((String) cliente.get("MACPOB"));
- //
- // if (m.find() == true) {
- // codigoPostal = m.group();
- // } else {
- // alertmessages.add("Cliente sin código postal, código cliente : " + cliente.get("MACCUE") + ", se ha puesto uno fake");
- // codigoPostal = "2810-237";
- // }
- // poblacion = poblacion.substring(codigoPostal.length()).trim(); // Para no repetir el codigo postal en caso de PT
- // }
- //
- // bw.write("\t\t\t\t<City>" + poblacion.trim() + "</City>\r\n");
- // bw.write("\t\t\t\t<PostalCode>" + codigoPostal.trim() + " </PostalCode>\r\n");
- // bw.write("\t\t\t\t<Country>" + country.trim() + "</Country>\r\n");
- // bw.write("\t\t\t</BillingAddress>\r\n");
- // bw.write("\t\t\t<SelfBillingIndicator>0</SelfBillingIndicator>\r\n");
- // bw.write("\t\t</Customer>\r\n");
- }
- it = o_articulos.iterator();
- while (it.hasNext()) {
- // Recogemos los datos
- String o = (String) it.next();
- articulo = (HashMap) articulos.get(o);
- bw.write("\t\t<Product>\r\n");
- bw.write("\t\t\t<ProductType>" + "P" + "</ProductType>\r\n");
- bw.write("\t\t\t<ProductCode>" + articulo.get("MAACUE") + "</ProductCode>\r\n");
- bw.write("\t\t\t<ProductDescription>" + ((String) articulo.get("MAANOM")).replace('&', ' ').trim() + "</ProductDescription>\r\n");
- bw.write("\t\t\t<ProductNumberCode>" + articulo.get("MAACUE") + "</ProductNumberCode>\r\n");
- bw.write("\t\t</Product>\r\n");
- }
- bw.write("\t\t<TaxTable>\r\n");
- it = o_ivas.iterator();
- while (it.hasNext()) {
- String index = (String) it.next();
- iva = (HashMap) ivas.get(index);
- bw.write("\t\t\t<TaxTableEntry>\r\n");
- bw.write("\t\t\t\t<TaxType>" + "IVA" + "</TaxType>\r\n");
- bw.write("\t\t\t\t<TaxCountryRegion>PT</TaxCountryRegion>\r\n");
- if (((String) iva.get("MAJELE")).equals("0")) {
- bw.write("\t\t\t\t<TaxCode>" + "ISE" + "</TaxCode>\r\n");
- } else {
- bw.write("\t\t\t\t<TaxCode>" + "NOR" + "</TaxCode>\r\n");
- }
- bw.write("\t\t\t\t<Description>" + iva.get("MAJNOM").toString().trim() + "</Description>\r\n");
- bw.write("\t\t\t\t<TaxPercentage>" + iva.get("MAJVA1").toString().trim() + "</TaxPercentage>\r\n");
- bw.write("\t\t\t</TaxTableEntry>\r\n");
- }
- bw.write("\t\t</TaxTable>\r\n");
- bw.write("\t</MasterFiles>\r\n");
- // Facturas
- it = o_facturas.iterator();
- BigDecimal tmp100 = new BigDecimal(100);
- while (it.hasNext()) {
- factura = (HashMap) facturas.get((String) it.next());
- HashMap himportes = new HashMap();
- // HashMap hcanones = new HashMap();
- Iterator it_ivas = ivas.keySet().iterator();
- while (it_ivas.hasNext()) {
- iva = (HashMap) ivas.get(it_ivas.next());
- himportes.put(iva.get("MAJELE").toString().trim(), "0");
- // hcanones.put(iva.get("MAJELE").toString().trim(), "0");
- }
- if (factura.containsKey("lineas")) {
- lineas = (HashMap) factura.get("lineas");
- Iterator it_lineas = lineas.keySet().iterator();
- BigDecimal tmpImportPorTipoIvaImportes = new BigDecimal(0);
- //BigDecimal tmpImportPorTipoIvaCanones = new BigDecimal(0);
- BigDecimal tmpImporteLinea = new BigDecimal(0);
- BigDecimal tmpALHN07 = new BigDecimal(0);
- //BigDecimal tmpALHN09 = new BigDecimal(0);
- //BigDecimal tmpALHUPV = new BigDecimal(0);
- BigDecimal tmpTotalCanones = new BigDecimal(0);
- BigDecimal tmpDescuentoProntoPago = new BigDecimal(factura.get("ALGPPG").toString().trim().replaceAll("(\\r|\\n)", "")); // dto pronto pago
- BigDecimal tmpDescuentoPieFactura = new BigDecimal(factura.get("ALGDEM").toString().trim().replaceAll("(\\r|\\n)", "")); // dto pie factura
- BigDecimal tmpSumaDescuentoProntoPagoLinea = null;
- BigDecimal tmpSumaDescuentoPieFacturaLinea = null;
- String stiv = "0";
- while (it_lineas.hasNext()) {
- linea = (HashMap) lineas.get((String) it_lineas.next());
- tmpSumaDescuentoProntoPagoLinea = new BigDecimal(0);
- tmpSumaDescuentoPieFacturaLinea = new BigDecimal(0);
- if (!((String) factura.get("ALGTIV")).equals("0")) {
- stiv = linea.get("ALHTIV").toString().trim();
- }
- // cogemos el importe acumulado por base de iva
- tmpImportPorTipoIvaImportes = new BigDecimal(himportes.get(stiv).toString().replaceAll("(\\r|\\n)", ""));
- // cogemos el campo que tiene calculado el precio por la cantidad
- tmpImporteLinea = new BigDecimal(linea.get("ALHIMP").toString().trim().replaceAll(",", ".")).abs();
- // le aplicamos el descuento pronto pago a la linea
- if(tmpDescuentoProntoPago.compareTo(new BigDecimal(0))==1){
- tmpSumaDescuentoProntoPagoLinea = tmpImporteLinea.multiply(tmpDescuentoProntoPago).divide(tmp100, BigDecimal.ROUND_HALF_UP);
- tmpImporteLinea = tmpImporteLinea.subtract(tmpSumaDescuentoProntoPagoLinea);
- }
- // le aplicamos el descuento pie de factura
- if(tmpDescuentoProntoPago.compareTo(new BigDecimal(0))==1){
- tmpSumaDescuentoPieFacturaLinea = tmpImporteLinea.multiply(tmpDescuentoPieFactura).divide(tmp100, 2, BigDecimal.ROUND_HALF_UP);
- tmpImporteLinea = tmpImporteLinea.subtract(tmpSumaDescuentoPieFacturaLinea);
- }
- linea.put("IMPORTE_LINEA_CON_DESCUENTOS_APLICADOS", tmpImporteLinea.setScale(2, BigDecimal.ROUND_HALF_UP));
- linea.put("SUMA_DE_DESCUENTO_PIE_FACTURA_Y_PRONTO_PAGO", tmpSumaDescuentoProntoPagoLinea.add(tmpSumaDescuentoPieFacturaLinea));
- // Agregamos las importes imponibles, referentes al tipo de iva
- himportes.put(stiv, tmpImportPorTipoIvaImportes.add(tmpImporteLinea.setScale(2, BigDecimal.ROUND_HALF_UP)));
- // // cogemos la ecotasa acumulada por base de iva
- //tmpImportPorTipoIvaCanones = new BigDecimal(hcanones.get(stiv).toString().replaceAll("(\\r|\\n)", ""));
- //tmpALHN07 = new BigDecimal(linea.get("ALHN07").toString().replaceAll("(\\r|\\n)", ""));
- //tmpALHN09 = new BigDecimal(linea.get("ALHN09").toString().replaceAll("(\\r|\\n)", "")).divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP);
- //tmpALHUPV = new BigDecimal(linea.get("ALHUPV").toString().replaceAll("(\\r|\\n)", ""));
- // tmpTotalCanones = tmpImportPorTipoIvaCanones.add(tmpALHN09.divide(tmpALHUPV, 4, BigDecimal.ROUND_HALF_UP).multiply(tmpALHN07));
- //tmpTotalCanones = tmpImportPorTipoIvaCanones.add(tmpALHN09.multiply(tmpALHN07));
- // Agregamos los canones imponibles, referentes al tipo de iva
- //hcanones.put(stiv, tmpTotalCanones.setScale(4, BigDecimal.ROUND_HALF_UP));
- }
- }
- BigDecimal ftbase = new BigDecimal(0);
- //BigDecimal ftcanon = new BigDecimal(0);
- BigDecimal ftiva = new BigDecimal(0);
- BigDecimal ftrecargo = new BigDecimal(0);
- BigDecimal fttotal = new BigDecimal(0);
- // Calculamos los importes de cada una de las bases
- it_ivas = ivas.keySet().iterator();
- while (it_ivas.hasNext()) {
- iva = (HashMap) ivas.get(it_ivas.next());
- if (!himportes.containsKey(iva.get("MAJELE").toString().trim()))
- continue;
- BigDecimal fbase = new BigDecimal(0);
- //BigDecimal fcanon = new BigDecimal(0);
- BigDecimal fiva = new BigDecimal(0);
- BigDecimal frecargo = new BigDecimal(0);
- BigDecimal ftotal = new BigDecimal(0);
- BigDecimal majva1 = new BigDecimal(iva.get("MAJVA1").toString().trim().replaceAll("(\\r|\\n)", ""));
- BigDecimal majva2 = new BigDecimal(iva.get("MAJVA2").toString().trim().replaceAll("(\\r|\\n)", ""));
- fbase = new BigDecimal(himportes.get(iva.get("MAJELE").toString().trim()).toString().replaceAll("(\\r|\\n)", ""));
- //fcanon = new BigDecimal(hcanones.get(iva.get("MAJELE").toString().trim()).toString().replaceAll("(\\r|\\n)", ""));
- if (((String) factura.get("ALGTIV")).equals("S") && majva1.compareTo(new BigDecimal(0)) == 1) {
- fiva = fbase/*.add(fcanon)*/.multiply(majva1.divide(tmp100, BigDecimal.ROUND_HALF_UP)).add(majva2).divide(tmp100, BigDecimal.ROUND_HALF_UP);
- }
- fiva = fbase/*.add(fcanon)*/.multiply(majva1).divide(tmp100, BigDecimal.ROUND_HALF_UP);
- if (factura.get("ALGTIV").equals("S")) {
- frecargo = fbase/*.add(fcanon)*/.multiply(majva2.divide(tmp100, BigDecimal.ROUND_HALF_UP));
- }
- ftotal = fbase/*.add(fcanon)*/.add(fiva).add(frecargo);
- ftbase = ftbase.add(fbase);
- //ftcanon = ftcanon.add(fcanon);
- ftiva = ftiva.add(fiva);
- if (factura.get("ALGTIV").equals("S")) {
- ftrecargo.add(frecargo);
- }
- fttotal = fttotal.add(ftotal);
- }
- factura.put("ALTBAS", ftbase);
- //factura.put("ALTCAN", ftcanon);
- factura.put("ALTCAN", new BigDecimal(0));
- factura.put("ALTIVA", ftiva);
- factura.put("ALTREC", ftrecargo);
- factura.put("ALTTOT", fttotal);
- facturas.put(factura.get("ALGNUD"), factura);
- }
- bw.write("\t<SourceDocuments>\r\n");
- bw.write("\t\t<SalesInvoices>\r\n");
- bw.write("\t\t\t<NumberOfEntries>" + o_facturas.size() + "</NumberOfEntries>\r\n");
- /********************************* CALCULO TOTALES DE FACTURA **********************************/
- it2 = o_facturas.iterator();
- BigDecimal totalFacturasCredito = new BigDecimal(0);
- BigDecimal totalFacturasDebito = new BigDecimal(0);
- BigDecimal porcentajeIVAPortes = new BigDecimal(0);
- BigDecimal importePortesSinIVA = new BigDecimal(0);
- BigDecimal importeECOTASASinIVA = new BigDecimal(0);
- BigDecimal importeECOTASAMasIVA = new BigDecimal(0);
- BigDecimal importeECOTASASoloIVA = new BigDecimal(0);
- BigDecimal importePortesConIVA = new BigDecimal(0);
- BigDecimal importePortesSoloIVA = new BigDecimal(0);
- BigDecimal facturaImporteNeto = new BigDecimal(0);
- BigDecimal facturaBase = new BigDecimal(0);
- //BigDecimal facturaCan = new BigDecimal(0);
- HashMap resultPortes = null;
- while (it2.hasNext()) {
- factura = (HashMap) facturas.get((String) it2.next());
- facturaImporteNeto = new BigDecimal(0);
- facturaBase = new BigDecimal(factura.get("ALTBAS").toString().trim().replaceAll("(\\r|\\n)", ""));
- //facturaCan = new BigDecimal(factura.get("ALTCAN").toString().replaceAll("(\\r|\\n)", ""));
- // tiene portes la factura?
- //resultPortes = getPortesFactura(consulta.get("company").toString(), consulta.get("empresa").toString(), factura.get("ALGSE2").toString(), factura.get("ALGNUD").toString());
- //resultPortes = getPortesFactura(factura.get("ALGA72").toString(),factura.get("ALGN74").toString(),factura.get("ALGPOR").toString());
- //importePortesSinIVA = new BigDecimal(resultPortes.get("ALGPOR").toString());
- importePortesSinIVA = new BigDecimal(factura.get("ALGPOR").toString().trim());
- facturaImporteNeto = facturaBase/*.add(facturaCan)*/.add(importePortesSinIVA);
- if (factura.get("ALGEST").toString().equals("FRA")) {
- totalFacturasDebito = totalFacturasDebito.add(facturaImporteNeto);
- } else if (factura.get("ALGEST").toString().equals("ABO")) {
- totalFacturasCredito = totalFacturasCredito.add(facturaImporteNeto);
- }
- }
- bw.write("\t\t\t<TotalDebit>" + totalFacturasDebito.setScale(4, BigDecimal.ROUND_HALF_UP).abs() + "</TotalDebit>\r\n");
- bw.write("\t\t\t<TotalCredit>" + totalFacturasCredito.setScale(4, BigDecimal.ROUND_HALF_UP).abs()+ "</TotalCredit>\r\n");
- /***********************************************************************************************************/
- String tmpNumeroFactura = "";
- String tmpSerieFactura = "";
- it = o_facturas.iterator();
- while (it.hasNext()) {
- factura = (HashMap) facturas.get((String) it.next());
- BigDecimal fbas = new BigDecimal(factura.get("ALTBAS").toString().trim().replaceAll("(\\r|\\n)", ""));
- //BigDecimal fcan = new BigDecimal(factura.get("ALTCAN").toString().replaceAll("(\\r|\\n)", ""));
- BigDecimal fiva = new BigDecimal(factura.get("ALTIVA").toString().trim().replaceAll("(\\r|\\n)", ""));
- BigDecimal ftot = new BigDecimal(factura.get("ALTTOT").toString().trim().replaceAll("(\\r|\\n)", ""));
- java.util.Date daux1 = new java.util.Date();
- java.util.Date daux2 = new java.util.Date();
- SimpleDateFormat sdf_datetime1 = new SimpleDateFormat("dd/MM/yyHH:mm:ss");
- SimpleDateFormat sdf_datetime3 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
- daux1 = sdf_kaka.parse(((String) factura.get("ALGA21").toString()) + ((String) factura.get("ALGN21").toString()));
- String tmpFecha = factura.get("ALGFCR").toString().substring(8)+ "/"+ factura.get("ALGFCR").toString().substring(5, 7) + "/"
- + factura.get("ALGFCR").toString().substring(2, 4);
- daux2 = sdf_datetime1.parse(tmpFecha + ((String) factura.get("ALGHCR").toString()));
- bw.write("\t\t\t<Invoice>\r\n");
- tmpSerieFactura = factura.get("ALGSE2").toString().trim();
- // bw.write("\t\t\t\t<InvoiceNo>" + getTmpSeriesFactura(factura).get(tmpSerieFactura) + " " + tmpSerieFactura + "/" + factura.get("ALGNUD") + "</InvoiceNo>\r\n");
- bw.write("\t\t\t\t<InvoiceNo>" + getTmpSeriesFactura(factura.get("ALGEST").toString()) + " " + tmpSerieFactura + "/" + factura.get("ALGNUD") + "</InvoiceNo>\r\n");
- bw.write("\t\t\t\t<DocumentStatus>\r\n");
- bw.write("\t\t\t\t\t<InvoiceStatus>N</InvoiceStatus>\r\n");
- bw.write("\t\t\t\t\t<InvoiceStatusDate>" + sdf_datetime3.format(daux2) + "</InvoiceStatusDate>\r\n");
- bw.write("\t\t\t\t\t<SourceID>1</SourceID>\r\n");
- bw.write("\t\t\t\t\t<SourceBilling>I</SourceBilling>\r\n");
- bw.write("\t\t\t\t</DocumentStatus>\r\n");
- SQL = "SELECT ALSEMP, ALSTIA, ALSSE1, ALSSE2, ALSNUD, ALSTIR, ALSENV, ALSLIN, ALSHAS, ALSSIG FROM ALMSF WHERE "
- + "ALSEMP = '" + factura.get("ALGEMP") + "' AND ALSTIA = '" + factura.get("ALGTIA") + "' "
- + "AND ALSSE1 = '" + factura.get("ALGSE1") + "' AND ALSSE2 = '" + factura.get("ALGSE2") + "' "
- + "AND ALSNUD = '" + factura.get("ALGNUD") + "' AND ALSTIR = '" + factura.get("ALGTIR") + "' "
- + "AND ALSENV = '" + factura.get("ALGENV") + "' AND ALSLIN = '" + factura.get("ALGLIN") + "' ";
- HashMap firma = getDataByField(SQL);
- if (firma.size() > 0) {
- bw.write("\t\t\t\t<Hash>" + firma.get("ALSSIG") + "</Hash>\r\n");
- } else {
- bw.write("\t\t\t\t<Hash>" + "0" + "</Hash>\r\n");
- }
- bw.write("\t\t\t\t<HashControl>1</HashControl>\r\n");
- bw.write("\t\t\t\t<InvoiceDate>" + sdf_date.format(daux1) + "</InvoiceDate>\r\n");
- bw.write("\t\t\t\t<InvoiceType>");
- if (factura.get("ALGEST").equals("FRA")) {
- bw.write("FT");
- } else if (factura.get("ALGEST").equals("ABO")) {
- bw.write("NC");
- }
- bw.write("</InvoiceType>\r\n");
- bw.write("\t\t\t\t<SpecialRegimes>\r\n");
- bw.write("\t\t\t\t\t<SelfBillingIndicator>0</SelfBillingIndicator>\r\n");
- bw.write("\t\t\t\t\t<CashVATSchemeIndicator>0</CashVATSchemeIndicator>\r\n");
- bw.write("\t\t\t\t\t<ThirdPartiesBillingIndicator>0</ThirdPartiesBillingIndicator>\r\n");
- bw.write("\t\t\t\t</SpecialRegimes>\r\n");
- bw.write("\t\t\t\t<SourceID>" + "1" + "</SourceID>\r\n");
- bw.write("\t\t\t\t<SystemEntryDate>" + sdf_datetime3.format(daux2) + "</SystemEntryDate>\r\n");
- if (clientes.containsKey((String) factura.get("ALGCLI"))) {
- cliente = (HashMap) clientes.get((String) factura.get("ALGCLI"));
- bw.write("\t\t\t\t<CustomerID>");
- bw.write(tmpCuentaMayorCliente + StringUtilities.fillLeftSpaces(cliente.get("MACCUE").toString(),6));
- bw.write("</CustomerID>\r\n");
- } else {
- errormessages.add("Cliente Código '" + factura.get("ALGCLI") + "' procedente del Pedido '" + factura.get("ALGNUD") + "' no localizado");
- }
- int l = 0;
- String tmpTaxPointDate = "";
- if (factura.containsKey("lineas")) {
- lineas = (HashMap) factura.get("lineas");
- o_lineas = (Vector) factura.get("o_lineas");
- Iterator it_lineas = o_lineas.iterator();
- BigDecimal unitPrice = new BigDecimal(0);
- BigDecimal cantidad = new BigDecimal(0);
- BigDecimal credit = new BigDecimal(0);
- BigDecimal descuentos = new BigDecimal(0);
- BigDecimal settlement = new BigDecimal(0);
- while (it_lineas.hasNext()) {
- linea = (HashMap) lineas.get((String) it_lineas.next());
- tmpNumeroFactura = linea.get("ALHFRA").toString();
- tmpSerieFactura = linea.get("ALHSF2").toString();
- bw.write("\t\t\t\t<Line>\r\n");
- bw.write("\t\t\t\t\t<LineNumber>" + (++l) + "</LineNumber>\r\n");
- bw.write("\t\t\t\t\t<ProductCode>" + linea.get("ALHART") + "</ProductCode>\r\n");
- bw.write("\t\t\t\t\t<ProductDescription>" + ((String) linea.get("ALHNOA")).replace('&',' ').trim() + "</ProductDescription>\r\n");
- cantidad = new BigDecimal(linea.get("ALHN07").toString());
- bw.write("\t\t\t\t\t<Quantity>" + cantidad.abs().setScale(2,BigDecimal.ROUND_HALF_UP) + "</Quantity>\r\n");
- bw.write("\t\t\t\t\t<UnitOfMeasure>Unid</UnitOfMeasure>\r\n");
- unitPrice = new BigDecimal(linea.get("ALHPRE").toString());
- bw.write("\t\t\t\t\t<UnitPrice>" + unitPrice.abs().setScale(2,BigDecimal.ROUND_HALF_UP) + "</UnitPrice>\r\n");
- bw.write("\t\t\t\t\t<TaxPointDate>");
- try {
- if ( ! linea.get("ALHFFR").toString().equals("") ) {
- String dta = linea.get("ALHFFR").toString();
- tmpTaxPointDate = dta.substring(0,4)+"-"+dta.substring(4,6)+"-"+dta.substring(6);
- }
- // java.util.Date f_extraccion = null;
- // mysqldf = new SimpleDateFormat("yyyyMMdd");
- // f_extraccion = mysqldf.parse((String) linea.get("ALHFPR").toString());
- // tmpTaxPointDate = f_extraccion.toString();
- } catch (Exception e) {
- tmpTaxPointDate = (((String) linea.get("ALHFPR")));
- }
- bw.write(tmpTaxPointDate);
- bw.write("</TaxPointDate>\r\n");
- if (factura.get("ALGEST").equals("ABO")) {
- bw.write("\t\t\t\t\t<References>");
- bw.write("\n\t\t\t\t\t\t<Reference>");
- bw.write(factura.get("ALGOP2").toString() + "/" + factura.get("ALGOPR").toString());
- bw.write("</Reference>\r\n");
- bw.write("\t\t\t\t\t\t<Reason>");
- bw.write(" ");
- bw.write("</Reason>\r\n");
- bw.write("\t\t\t\t\t</References>\r\n");
- }
- bw.write("\t\t\t\t\t<Description>" + ((String) linea.get("ALHNOA")).replace('&',' ').trim() + "</Description>\r\n");
- descuentos = new BigDecimal(linea.get("SUMA_DE_DESCUENTO_PIE_FACTURA_Y_PRONTO_PAGO").toString().replaceAll("(\\r|\\n)", ""));
- settlement = new BigDecimal(linea.get("ALHTDC").toString()).abs().setScale(2);
- //credit = new BigDecimal(linea.get("ALHIMP").toString().replaceAll("(\\r|\\n)", ""));
- credit = new BigDecimal(linea.get("IMPORTE_LINEA_CON_DESCUENTOS_APLICADOS").toString().replaceAll("(\\r|\\n)", ""));
- if (factura.get("ALGEST").equals("ABO")) {
- bw.write("\t\t\t\t\t<DebitAmount>" + credit.abs() + "</DebitAmount>\r\n");
- } else {
- bw.write("\t\t\t\t\t<CreditAmount>" + credit.abs() + "</CreditAmount>\r\n");
- }
- bw.write("\t\t\t\t\t<Tax>\r\n");
- bw.write("\t\t\t\t\t\t<TaxType>IVA</TaxType>\r\n");
- bw.write("\t\t\t\t\t\t<TaxCountryRegion>PT</TaxCountryRegion>\r\n");
- if (factura.get("ALGTIV").equals("0")) {
- bw.write("\t\t\t\t\t\t<TaxCode>ISE</TaxCode>\r\n");
- } else {
- bw.write("\t\t\t\t\t\t<TaxCode>NOR</TaxCode>\r\n");
- }
- if (factura.get("ALGTIV").equals("0")) {
- bw.write("\t\t\t\t\t\t<TaxPercentage>0.00</TaxPercentage>\r\n");
- } else {
- bw.write("\t\t\t\t\t\t<TaxPercentage>" + linea.get("ALHIVA") + "</TaxPercentage>\r\n");
- }
- bw.write("\t\t\t\t\t</Tax>\r\n");
- // if (factura.get("ALGTIV").equals("0")) {
- // bw.write("\t\t\t\t\t<TaxExemptionReason>" + getTmpSeriesFactura(factura).get(tmpSerieFactura) + " " + tmpSerieFactura + "/"
- // + factura.get("ALGNUD") + "</TaxExemptionReason>\r\n");
- // }
- bw.write("\t\t\t\t\t<SettlementAmount>" + settlement.add(descuentos).setScale(2, BigDecimal.ROUND_HALF_UP).abs() +"</SettlementAmount>\r\n");
- bw.write("\t\t\t\t</Line>\r\n");
- }
- }
- // resultPortes = getPortesFactura(consulta.get("company").toString(), consulta.get("empresa").toString(),factura.get("ALGSE2").toString(), factura.get("ALGNUD").toString());
- //importePortesSinIVA = new BigDecimal(resultPortes.get("ALGPOR").toString());
- importePortesSinIVA = new BigDecimal(factura.get("ALGPOR").toString());
- //porcentajeIVAPortes = new BigDecimal(resultPortes.get("ALGN74").toString());
- porcentajeIVAPortes = new BigDecimal(factura.get("ALGN74").toString());
- importePortesConIVA = getImporteMasIVA(importePortesSinIVA, porcentajeIVAPortes);
- importePortesSoloIVA = getImporteIVA(importePortesSinIVA, porcentajeIVAPortes);
- importeECOTASASinIVA = new BigDecimal(factura.get("ALGN55").toString()).abs();
- importeECOTASAMasIVA = getImporteMasIVA(importeECOTASASinIVA, getPorcentajeIVAECOTASA(factura.get("ALGEMP").toString()));
- importeECOTASASoloIVA = getImporteIVA(importeECOTASASinIVA, getPorcentajeIVAECOTASA(factura.get("ALGEMP").toString()));
- if (importeECOTASASinIVA.compareTo(new BigDecimal(0)) == 1) {
- bw.write("\t\t\t\t<Line>\r\n");
- bw.write("\t\t\t\t\t<LineNumber>" + (++l) + "</LineNumber>\r\n");
- bw.write("\t\t\t\t\t<ProductCode>ECOTASA</ProductCode>\r\n");
- bw.write("\t\t\t\t\t<ProductDescription>ECOTASA</ProductDescription>\r\n");
- bw.write("\t\t\t\t\t<Quantity>1</Quantity>\r\n");
- bw.write("\t\t\t\t\t<UnitOfMeasure>Unid</UnitOfMeasure>\r\n");
- bw.write("\t\t\t\t\t<UnitPrice>" + importeECOTASASinIVA + "</UnitPrice>\r\n");
- bw.write("\t\t\t\t\t<TaxPointDate>"+tmpTaxPointDate+"</TaxPointDate>\r\n");
- if (factura.get("ALGEST").equals("ABO")) {
- bw.write("\t\t\t\t\t<References>");
- bw.write("\n\t\t\t\t\t\t<Reference>");
- bw.write(factura.get("ALGOP2").toString() + "/" + factura.get("ALGOPR").toString());
- bw.write("</Reference>\r\n");
- bw.write("\t\t\t\t\t\t<Reason>");
- bw.write(" ");
- bw.write("</Reason>\r\n");
- bw.write("\t\t\t\t\t</References>\r\n");
- }
- bw.write("\t\t\t\t\t<Description>ECOTASA</Description>\r\n");
- bw.write("\t\t\t\t\t<CreditAmount>" + importeECOTASASinIVA + "</CreditAmount>\r\n");
- bw.write("\t\t\t\t\t<Tax>\r\n");
- bw.write("\t\t\t\t\t\t<TaxType>" + "IVA" + "</TaxType>\r\n");
- bw.write("\t\t\t\t\t\t<TaxCountryRegion>" + "PT" + "</TaxCountryRegion>\r\n");
- if (factura.get("ALGTIV").equals("0")) {
- bw.write("\t\t\t\t\t\t<TaxCode>ISE</TaxCode>\r\n");
- } else {
- bw.write("\t\t\t\t\t\t<TaxCode>NOR</TaxCode>\r\n");
- }
- if (factura.get("ALGTIV").equals("0")) {
- bw.write("\t\t\t\t\t\t<TaxPercentage>0.00</TaxPercentage>\r\n");
- } else {
- bw.write("\t\t\t\t\t\t<TaxPercentage>" + getPorcentajeIVAECOTASA(factura.get("ALGEMP").toString()).toString() + "</TaxPercentage>\r\n");
- }
- bw.write("\t\t\t\t\t</Tax>\r\n");
- bw.write("\t\t\t\t</Line>\r\n");
- } else {
- System.out.println("No hay ECOTASA para la factura " + tmpSerieFactura + " " + tmpNumeroFactura);
- }
- if (importePortesConIVA.compareTo(new BigDecimal(0)) == 1) {
- bw.write("\t\t\t\t<Line>\r\n");
- bw.write("\t\t\t\t\t<LineNumber>" + (++l) + "</LineNumber>\r\n");
- bw.write("\t\t\t\t\t<ProductCode>PORTES</ProductCode>\r\n");
- bw.write("\t\t\t\t\t<ProductDescription>PORTES</ProductDescription>\r\n");
- bw.write("\t\t\t\t\t<Quantity>1</Quantity>\r\n");
- bw.write("\t\t\t\t\t<UnitOfMeasure>Unid</UnitOfMeasure>\r\n");
- bw.write("\t\t\t\t\t<UnitPrice>" + importePortesSinIVA + "</UnitPrice>\r\n");
- bw.write("\t\t\t\t\t<TaxPointDate>"+tmpTaxPointDate+"</TaxPointDate>\r\n");
- if (factura.get("ALGEST").equals("ABO")) {
- bw.write("\t\t\t\t\t<References>");
- bw.write("\n\t\t\t\t\t\t<Reference>");
- bw.write(factura.get("ALGOP2").toString() + "/" + factura.get("ALGOPR").toString());
- bw.write("</Reference>\r\n");
- bw.write("\t\t\t\t\t\t<Reason>");
- bw.write(" ");
- bw.write("</Reason>\r\n");
- bw.write("\t\t\t\t\t</References>\r\n");
- }
- bw.write("\t\t\t\t\t<Description>PORTES</Description>\r\n");
- bw.write("\t\t\t\t\t<CreditAmount>" + importePortesSinIVA + "</CreditAmount>\r\n");
- bw.write("\t\t\t\t\t<Tax>\r\n");
- bw.write("\t\t\t\t\t\t<TaxType>" + "IVA" + "</TaxType>\r\n");
- bw.write("\t\t\t\t\t\t<TaxCountryRegion>" + "PT" + "</TaxCountryRegion>\r\n");
- if (factura.get("ALGTIV").equals("0")) {
- bw.write("\t\t\t\t\t\t<TaxCode>ISE</TaxCode>\r\n");
- } else {
- bw.write("\t\t\t\t\t\t<TaxCode>NOR</TaxCode>\r\n");
- }
- if (factura.get("ALGTIV").equals("0")) {
- bw.write("\t\t\t\t\t\t<TaxPercentage>0.00</TaxPercentage>\r\n");
- // bw.write("\t\t\t\t\t\t<TaxExemptionReason> </TaxExemptionReason>\r\n");
- } else {
- bw.write("\t\t\t\t\t\t<TaxPercentage>" + porcentajeIVAPortes + "</TaxPercentage>\r\n");
- }
- bw.write("\t\t\t\t\t</Tax>\r\n");
- bw.write("\t\t\t\t</Line>\r\n");
- } else {
- System.out.println("No hay portes para la factura " + tmpSerieFactura + " " + tmpNumeroFactura);
- }
- bw.write("\t\t\t\t<DocumentTotals>\r\n");
- BigDecimal fact = new BigDecimal(0);
- // fact = fbas.subtract(fdem).subtract(fppg).add(fcan).add(importePortesSinIVA);
- fact = fbas/*.add(fcan)*/.add(importePortesSinIVA).add(importeECOTASASinIVA);
- if ( !factura.get("ALGTIV").equals("0") ) { // se suman los importes solo si no es exento
- ftot = ftot.add(importePortesConIVA).add(importeECOTASAMasIVA); // total mas portes + ecotasa
- fiva = fiva.add(importePortesSoloIVA).add(importeECOTASASoloIVA); // total iva mas iva portes + ecotasa //
- } else {
- ftot = ftot.add(importePortesSinIVA).add(importeECOTASASinIVA); // total
- }
- bw.write("\t\t\t\t\t<TaxPayable>" + fiva.setScale(2, BigDecimal.ROUND_HALF_UP).abs() + "</TaxPayable>\r\n");
- bw.write("\t\t\t\t\t<NetTotal>" + fact.setScale(2, BigDecimal.ROUND_HALF_UP).abs() + "</NetTotal>\r\n");
- bw.write("\t\t\t\t\t<GrossTotal>" + ftot.setScale(2, BigDecimal.ROUND_HALF_UP).abs() + "</GrossTotal>\r\n");
- bw.write("\t\t\t\t</DocumentTotals>\r\n");
- bw.write("\t\t\t</Invoice>\r\n");
- }
- bw.write("\t\t</SalesInvoices>\r\n");
- bw.write("\t</SourceDocuments>\r\n");
- bw.write("</AuditFile>");
- bw.close();
- }
- if (!errormessages.isEmpty()) {
- for (int i = 0; i < errormessages.size(); i++) {
- System.err.println(errormessages.get(i));
- }
- }
- }
- public void checkPoblacion(Vector errormessages, HashMap aHashEmpresaOrCompany, String aPrefijo) {
- Pattern e_p = Pattern.compile("[0-9]{4}-[0-9]{3}");
- Matcher e_m = e_p.matcher((String) aHashEmpresaOrCompany.get(aPrefijo+"POB"));
- if (e_m.find() == true) {
- try {
- String e_zipcode = e_m.group();
- String e_city = ((String) aHashEmpresaOrCompany.get(aPrefijo+"POB")).substring(e_zipcode.length() + 1);
- aHashEmpresaOrCompany.put(aPrefijo+"POB", e_city);
- aHashEmpresaOrCompany.put(aPrefijo+"ZIP", e_zipcode);
- } catch (Exception e) {
- errormessages.add("Datos de Empresa/Company ("+aPrefijo+"POB) mal formados, código Empresa/Company : " + aHashEmpresaOrCompany.get(aPrefijo+"CUE"));
- }
- } else {
- errormessages.add("Empresa/Company sin código postal, código Empresa/Company : " + aHashEmpresaOrCompany.get(aPrefijo+"CUE"));
- }
- }
- /**
- * Este metodo recibida una query devuelve una hash CAMPO=VALOR. El
- * resultado de la query no puede devolver mas de una row.
- */
- public static HashMap getDataByField(String aSQL) {
- return getDataByField(aSQL, "");
- }
- public static HashMap getDataByField(String aSQL, String aCampoClave) {
- HashMap tmpData = new HashMap();
- HashMap tmpDetailData = null;
- boolean tmpMultiples = !aCampoClave.equals("");
- Vector tmpResultSQL = SQLLauncher.executeQuery(aSQL, Integer.MAX_VALUE,0, true);
- if (tmpResultSQL != null && tmpResultSQL.size() > 1) {
- Vector tmpMetadata = (Vector) tmpResultSQL.get(tmpResultSQL.size() - 1); // La metadata se coloca al final del Vector de resultados
- Vector tmpLinea = null;
- String tmpCurrentField = null;
- for (int x = 0; x < tmpResultSQL.size() - 1; x++) { // Recorremos las lineas
- tmpDetailData = new HashMap();
- tmpLinea = (Vector) tmpResultSQL.get(x);
- for (int y = 0; y < tmpMetadata.size(); y++) { // Recorremos los campos
- tmpCurrentField = tmpMetadata.get(y).toString().trim(); // campo
- if(tmpLinea.get(y)==null){
- tmpDetailData.put(tmpCurrentField, ""); // valor
- }else{
- tmpDetailData.put(tmpCurrentField, tmpLinea.get(y).toString().replaceAll("(\\r|\\n)", "")); // valor
- }
- }
- if (tmpMultiples) {
- tmpData.put(tmpDetailData.get(aCampoClave).toString().trim(), tmpDetailData);
- }
- }
- }
- if (tmpMultiples) {
- return tmpData;
- }
- return (tmpDetailData==null) ? new HashMap() : tmpDetailData;
- }
- public static void cargarDatos(String aSQL, HashMap aElems, Vector aO_elems) {
- cargarDatos(aSQL, aElems, aO_elems, "HASKEY");
- }
- public static void cargarDatos(String aSQL, HashMap aElems, Vector aO_elems, String aKey) {
- Vector tmpResultSQL = SQLLauncher.executeQuery(aSQL, Integer.MAX_VALUE, 0, true);
- if (tmpResultSQL != null && tmpResultSQL.size() > 1) {
- HashMap tmpAux = null;
- Vector tmpMetadata = (Vector) tmpResultSQL.get(tmpResultSQL.size() - 1); // La metadata se coloca al final del Vector de resultados
- Vector tmpLinea = null;
- String tmpCurrentField = null;
- for (int x = 0; x < tmpResultSQL.size() - 1; x++) { // Recorremos las lineas
- tmpAux = new HashMap();
- tmpLinea = (Vector) tmpResultSQL.get(x);
- for (int y = 0; y < tmpMetadata.size(); y++) { // Recorremos los campos
- tmpCurrentField = tmpMetadata.get(y).toString(); // campo
- if(tmpLinea.get(y)==null){
- tmpAux.put(tmpCurrentField, ""); // valor
- }else{
- tmpAux.put(tmpCurrentField, tmpLinea.get(y).toString().replaceAll("(\\r|\\n)", "")); // valor
- }
- }
- aO_elems.add(tmpAux.get(aKey).toString().trim());
- aElems.put(tmpAux.get(aKey).toString().trim(), tmpAux);
- }
- }
- }
- public HashMap getClaveValorFromVector(Vector aValores, Vector aClaves){
- HashMap tmpHash = new HashMap();
- for (int p=0; p<aClaves.size(); p++){
- tmpHash.put(aClaves.get(p), aValores.get(p));
- }
- return tmpHash;
- }
- public HashMap cargarLineasAFacturas(String aSQL, HashMap aFacturas, String aVarLineas, String aHashKey, String aSubKey) throws SQLException {
- HashMap tmpLineas = new HashMap();
- HashMap tmpPares = null;
- Vector tmpMeta = null;
- String aCurrentSubKey = null;
- Vector tmpVectorSubkeys = null;
- Vector tmpResultSQL = com.ak.rudder.core.control.SQLLauncher.executeQuery(aSQL, Integer.MAX_VALUE, 0, true);
- if ( tmpResultSQL != null && tmpResultSQL.size() > 1 ) {
- tmpMeta = (Vector)tmpResultSQL.get(tmpResultSQL.size()-1);
- for (int x=0; x<tmpResultSQL.size()-1; x++) {
- tmpPares = getClaveValorFromVector((Vector)tmpResultSQL.get(x), tmpMeta); // cogemos los valores de la linea que devuelve la query
- HashMap aFactura = (HashMap) aFacturas.get(tmpPares.get(aHashKey)); // cogemos factura correspondiente a la linea que tenemos en tmpPares
- aCurrentSubKey = tmpPares.get(aSubKey).toString(); // guardamos la subkey de la linea actual ejemplo [0,1]
- tmpLineas = new HashMap();
- tmpLineas.put(aCurrentSubKey, tmpPares); // montamos hash con la nueva linea y su subkey correspondiente
- if( aFactura.containsKey(aVarLineas)){ // si la factura ya tiene el atributo lineas
- ((HashMap)aFactura.get(aVarLineas)).put(aCurrentSubKey, (HashMap)tmpLineas.get(aCurrentSubKey));
- ((Vector)aFactura.get("o_"+aVarLineas)).add(aCurrentSubKey);
- }else{
- tmpVectorSubkeys = new Vector();
- tmpVectorSubkeys.add(aCurrentSubKey);
- aFactura.put(aVarLineas, tmpLineas);
- aFactura.put("o_"+aVarLineas, tmpVectorSubkeys);
- }
- aFacturas.put(tmpPares.get(aHashKey), aFactura);
- }
- }
- return aFacturas;
- }
- // public HashMap web_addSubElems(String SQL, HashMap elems, String where,
- // String has_key, String sub_key) throws SQLException {
- //
- // boolean hasnext = true;
- // // cogemos las lineas de las faturas del periodo de tiempo indicado arriba
- // ResultSet res = (ResultSet) web_dbQuery(SQL);
- //
- // while (hasnext) {
- // // recorremos el resultado de las lineas
- // HashMap sub_unit = web_dbFetchHash(res);
- //
- // if (sub_unit.size() > 0) {
- //
- // HashMap elem = (HashMap) elems.get( (String) sub_unit.get(has_key) ); // pillamos la factura
- //
- // HashMap sub_hash = new HashMap();
- // Vector sub_vector = new Vector();
- //
- // // Recogemos, si tiene algún valor ya ele lemento
- // if (elem.containsKey(where)) { // si la factura ya tenia el atributo lineas lo cogemos para añadir otra linea
- // sub_hash = (HashMap) elem.get(where); // cogemos lineas
- // sub_vector = (Vector) elem.get("o_" + where); // cogemos o_lineas
- // }
- //
- // // Añadimos dateinfo_unit, ¿¿destrozando contenidos??
- // sub_hash.put( (String) sub_unit.get(sub_key), sub_unit); // en las lineas añadimos la siguiente linea que hemos cogido
- // sub_vector.add( (String) sub_unit.get(sub_key)); // en el atributo o_lineas añadimos la subkey de la nueva linea [0/1],[0/2]
- //
- // // Añadimos dateinfo sobre elem
- // elem.put(where, sub_hash); // ponemos en la factura la linea
- // elem.put("o_" + where, sub_vector); // ponemos en la factura el subkey de la key
- //
- // // Reaplilamos eñ HAsh
- // elems.put((String) sub_unit.get(has_key), elem);
- //
- // } else {
- // hasnext = false;
- // }
- // }
- //
- // return elems;
- // }
- // public static HashMap web_dbFetchHash(ResultSet res) throws SQLException {
- //
- // HashMap elem = new HashMap();
- // ResultSetMetaData meta = res.getMetaData();
- //
- // // Get result set meta data
- // int cols = meta.getColumnCount();
- //
- // if (res.next()) {
- // // Get the column names; column indices start from 1
- // for (int n = 1; n < cols + 1; n++) {
- // // elem.put((String) meta.getColumnName(n), ((String)
- // // res.getString(n)).trim() );
- // if (res.getObject(n) != null)
- // elem.put((String) meta.getColumnName(n),
- // ((Object) res.getObject(n)));
- // else
- // elem.put((String) meta.getColumnName(n), "");
- // }
- //
- // }
- //
- // return (elem);
- // }
- // public static ResultSet web_dbQuery(String query) throws SQLException {
- //
- // getConnection();
- //
- // Statement stmt = theConection.createStatement(
- // ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
- //
- // ResultSet res = stmt.executeQuery(query);
- //
- // return res;
- //
- // }
- public static boolean insertarHash(HashMap aElem) {
- String SQL = "";
- Vector keys = new Vector();
- Vector values = new Vector();
- Iterator it = ((Set) aElem.entrySet()).iterator();
- while (it.hasNext()) {
- Map.Entry e = (Map.Entry) it.next();
- keys.add((String) e.getKey());
- values.add((String) e.getValue().toString());
- }
- SQL = "INSERT INTO ALMSF (" + web_strImplode((String[]) keys.toArray(new String[keys.size()]), ", ")+ ") VALUES ('"
- + web_strImplode((String[]) values.toArray(new String[values.size()]),"', '") + "')";
- SQLLauncher.executeUpdate(SQL);
- return true;
- }
- public static String web_strImplode(String[] ary, String delim) {
- String str = "";
- for (int i = 0; i < ary.length; i++) {
- if (i != 0) {
- str += delim;
- }
- str += ary[i];
- }
- return str;
- }
- // private static void getConnection() throws SQLException {
- // if (theConection == null) {
- //
- // DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());
- //
- // String tmpUrl = "jdbc:as400://10.10.10.131/SAKFILGI/";
- // String tmpUser = "SAKFILGI";
- // String tmpPass = "AKSAKFILGI";
- //
- // theConection = DriverManager.getConnection(tmpUrl, tmpUser, tmpPass);
- // }
- // }
- public static boolean actualizarHash(HashMap aElem, String aWhere) throws SQLException {
- String tmpSQL = null;
- Vector pairs = new Vector();
- Iterator it = ((Set) aElem.entrySet()).iterator();
- while (it.hasNext()) {
- Map.Entry e = (Map.Entry) it.next();
- pairs.add(" " + (String) e.getKey() + "='" + web_strAddslashes((String) e.getValue().toString()) + "' ");
- }
- tmpSQL = "UPDATE ALMSF SET " + web_strImplode((String[]) pairs.toArray(new String[pairs.size()]), ",") + " WHERE " + aWhere;
- SQLLauncher.executeUpdate(tmpSQL);
- return true;
- }
- public static String web_strAddslashes(String s) {
- String f = null;
- String r = null;
- int n;
- if (s == null)
- return s;
- f = "\\";
- r = "NONE";
- n = s.indexOf(f);
- while (n != -1) {
- s = s.substring(0, n) + r + s.substring(n + f.length());
- n += r.length();
- n = s.indexOf(f, n);
- }
- f = "\'";
- r = "\'\'";
- n = s.indexOf(f);
- while (n != -1) {
- s = s.substring(0, n) + r + s.substring(n + f.length());
- n += r.length();
- n = s.indexOf(f, n);
- }
- f = "\"";
- r = "\\\"";
- n = s.indexOf(f);
- while (n != -1) {
- s = s.substring(0, n) + r + s.substring(n + f.length());
- n += r.length();
- n = s.indexOf(f, n);
- }
- f = "\0";
- r = "\\\0";
- n = s.indexOf(f);
- while (n != -1) {
- s = s.substring(0, n) + r + s.substring(n + f.length());
- n += r.length();
- n = s.indexOf(f, n);
- }
- return s;
- }
- public static HashMap getPortesFactura(String aIvaPortesTipo, String aIvaPortes, String aValuePortes) {
- /*
- HashMap tmpResult = new HashMap();
- String SQLPortes = "SELECT ALGA72, ALGN74, ALGPOR FROM ALMGF WHERE ALGEMP='" + aCompany + "' " + "AND ALGTEM='" + anEmpresa + "' "
- + "AND ALGTIA='V' AND ALGTIR='F' AND ALGSE2='" + aSerieFactura + "' " + "AND ALGNUD='" + aNumeroFactura + "' ";
- Vector tmpResultSQL = SQLLauncher.executeQuery(SQLPortes,Integer.MAX_VALUE, 0, false);
- tmpResult.put("ALGA72",((Vector) tmpResultSQL.get(0)).get(0).toString()); // iva portes tipo
- tmpResult.put("ALGN74",((Vector) tmpResultSQL.get(0)).get(1).toString()); // iva portes
- tmpResult.put("ALGPOR",((Vector) tmpResultSQL.get(0)).get(2).toString()); // valor de los portes
- */
- HashMap tmpResult = new HashMap();
- tmpResult.put("ALGA72", aIvaPortesTipo);
- tmpResult.put("ALGN74", aIvaPortes);
- tmpResult.put("ALGPOR", aValuePortes);
- return tmpResult;
- }
- public static BigDecimal getImporteMasIVA(BigDecimal aImporte,BigDecimal aPorcentaje) {
- return aImporte.multiply(aPorcentaje).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP).add(aImporte);
- }
- public static BigDecimal getImporteIVA(BigDecimal aImporte,BigDecimal aPorcentaje) {
- return aImporte.multiply(aPorcentaje).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
- }
- public static void cargarArticuloECO(HashMap consulta,HashMap articulos, Vector o_articulos, HashMap facturas, Vector o_facturas) {
- HashMap factura;
- boolean tmpEcotasaAdded = false;
- BigDecimal importePortes = new BigDecimal(0);
- HashMap articuloEcotasa = new HashMap();
- articuloEcotasa.put("MAANOM", "ECOTASA");
- articuloEcotasa.put("MAACUE", "ECOTASA");
- articuloEcotasa.put("MAAFAM", "ECOTASA");
- articuloEcotasa.put("AUACO1", "");
- Iterator it2 = o_facturas.iterator();
- while (it2.hasNext() && !tmpEcotasaAdded) {
- factura = (HashMap) facturas.get((String) it2.next());
- System.out.println("Buscando ECOTASA para la factura: " + factura.get("ALGSE2").toString() +" "+ factura.get("ALGNUD").toString() + " ALGPOR = " + factura.get("ALGPOR").toString());
- importePortes = new BigDecimal(factura.get("ALGN55").toString()).abs();
- if (importePortes.compareTo(new BigDecimal(0)) == 1) {
- o_articulos.add("ECOTASA");
- articulos.put("ECOTASA", articuloEcotasa);
- tmpEcotasaAdded = true;
- }
- }
- }
- public static void cargarArticuloPortes(HashMap consulta,HashMap articulos, Vector o_articulos, HashMap facturas, Vector o_facturas) {
- HashMap factura;
- boolean tmpPortesAdded = false;
- BigDecimal importePortes = new BigDecimal(0);
- HashMap articuloPortes = new HashMap();
- articuloPortes.put("MAANOM", "PORTES");
- articuloPortes.put("MAACUE", "PORTES");
- articuloPortes.put("MAAFAM", "PORTES");
- articuloPortes.put("AUACO1", "");
- Iterator it2 = o_facturas.iterator();
- while (it2.hasNext() && !tmpPortesAdded) {
- factura = (HashMap) facturas.get((String) it2.next());
- System.out.println("Buscando portes para la factura: " + factura.get("ALGSE2").toString() +" "+ factura.get("ALGNUD").toString() + " ALGPOR = " + factura.get("ALGPOR").toString());
- importePortes = new BigDecimal(factura.get("ALGPOR").toString()).abs();
- if (importePortes.compareTo(new BigDecimal(0)) == 1) {
- o_articulos.add("PORTES");
- articulos.put("PORTES", articuloPortes);
- tmpPortesAdded = true;
- }
- }
- }
- public static String getCuentaMayorCliente(String aCompany) {
- Vector tmpCuentaMayor = com.ak.rudder.core.control.SQLLauncher.executeQuery("SELECT MAECCL FROM MAEEF WHERE MAECUE='"+aCompany+"'");
- if (tmpCuentaMayor != null) {
- Vector aCM = (Vector)tmpCuentaMayor.elementAt(0);
- return aCM.get(0).toString().trim();
- }
- return "";
- }
- public static Map getPaises(String aCompany) {
- if(tmpPaises == null){
- tmpPaises = new HashMap();
- String tmpSQL = "SELECT MAJELE, MAJNOM FROM MAEJF WHERE MAJEMP='"+aCompany+"' AND MAJCUE='PA'";
- Vector paises = com.ak.rudder.core.control.SQLLauncher.executeQuery(tmpSQL, Integer.MAX_VALUE, 0, false);
- if (paises != null ) {
- Iterator it = paises.iterator();
- Vector row = null;
- while (it.hasNext()){
- row = (Vector) it.next();
- tmpPaises.put(row.get(0).toString().trim(),row.get(1).toString().trim());
- }
- }
- }
- return tmpPaises;
- }
- public static String getTmpSeriesFactura(String tmpEstadoFactura) {
- if(tmpEstadoFactura.trim().equals("FRA")){
- return "NF"; // factura
- }else{
- return "NC"; // note credit - abono
- }
- }
- public static BigDecimal getPorcentajeIVAECOTASA (String aCompany) {
- if ( ivaEcotasa == null) {
- String tmpSQL = "SELECT MAATIV FROM MAEAF WHERE MAAEMP='"+aCompany+"' AND MAACUE = 'ECOTASA'";
- String tmpSQLTI = "";
- Vector aEcotasa = com.ak.rudder.core.control.SQLLauncher.executeQuery(tmpSQL);
- Vector tmpIVAEcotasa = null;
- String tipoIVA = "";
- String valorECOTASA = "";
- if (aEcotasa != null ) {
- Vector rowEcotasa = (Vector)aEcotasa.elementAt(0);
- tipoIVA = rowEcotasa.get(0).toString();
- if ( !tipoIVA.equals("") ){
- tmpSQLTI = "SELECT MAJVA1 FROM MAEJF WHERE MAJEMP='"+aCompany+"' AND MAJCUE='IV' AND MAJELE='"+tipoIVA+"'";
- tmpIVAEcotasa = com.ak.rudder.core.control.SQLLauncher.executeQuery(tmpSQLTI);
- if (tmpIVAEcotasa != null){
- valorECOTASA = ((Vector)tmpIVAEcotasa.get(0)).get(0).toString().replaceAll("(\\r|\\n)", "");
- ivaEcotasa = new BigDecimal(valorECOTASA);
- }
- }
- }
- }
- return ivaEcotasa;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement