Advertisement
Guest User

Untitled

a guest
Mar 1st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 70.11 KB | None | 0 0
  1. package com.ak.rudder.control.launcher.process;
  2.  
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.math.BigDecimal;
  7. import java.sql.Connection;
  8. import java.sql.SQLException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Calendar;
  11. import java.util.GregorianCalendar;
  12. import java.util.HashMap;
  13. import java.util.Iterator;
  14. import java.util.Map;
  15. import java.util.Set;
  16. import java.util.Vector;
  17. import java.util.regex.Matcher;
  18. import java.util.regex.Pattern;
  19.  
  20. import com.ak.rudder.core.control.SQLLauncher;
  21. import com.ak.rudder.core.util.StringUtilities;
  22.  
  23.  
  24.  
  25. public class SaftPt {
  26.  
  27. static Connection theConection = null;
  28. static Map tmpSeriesFactura = null;
  29. static Map tmpPaises = null;
  30. static HashMap consulta = null;
  31. static BigDecimal ivaEcotasa = null;
  32.  
  33. public void ejecutar(String aCompany, String aEmpresa, String aTo, String aFrom, String aPathSpool) throws Exception {
  34.  
  35. boolean tmpIsPorCompany=aEmpresa.indexOf(',')!=-1;
  36. SimpleDateFormat mysqldf = new SimpleDateFormat("dd/MM/yyyy");
  37. consulta = new HashMap();
  38. consulta.put("company", aCompany);
  39. consulta.put("empresa", aEmpresa);
  40. consulta.put("fiscalyear", (String) Integer.toString(((Calendar) Calendar.getInstance()).get(Calendar.YEAR)));
  41. consulta.put("submit", "OK");
  42. consulta.put("from", aFrom);
  43. consulta.put("to", aTo);
  44. String tmpCuentaMayorCliente = "43000";
  45. String tmpPath = aPathSpool;
  46. String SQL = "";
  47. String sFichero = "";
  48. Vector errormessages = new Vector();
  49. Vector alertmessages = new Vector();
  50. HashMap empresa = null;
  51. HashMap company = null;
  52. HashMap clientes = new HashMap();
  53. Vector o_clientes = new Vector();
  54. HashMap cliente = null;
  55. HashMap ivas = new HashMap();
  56. Vector o_ivas = new Vector();
  57. HashMap iva = null;
  58. HashMap articulos = new HashMap();
  59. Vector o_articulos = new Vector();
  60. HashMap articulo = null;
  61. HashMap facturas = new HashMap();
  62. Vector o_facturas = new Vector();
  63. HashMap factura = null;
  64. HashMap lineas = new HashMap();
  65. Vector o_lineas = new Vector();
  66. HashMap linea = null;
  67. Iterator it = null;
  68. Iterator it2 = null;
  69. Calendar t1 = null;
  70. Calendar t2 = null;
  71.  
  72. /****
  73. * LLAMADA A LA HASH MANUALMENTE EN CASO DE QUE ALGUNA VEZ FALLE
  74. */
  75. // SaftptHash tmpSaftptHash = new SaftptHash();
  76. //
  77. // try {
  78. // tmpSaftptHash.guardarHash("TE", "07", "7V", "17993");
  79. // tmpSaftptHash.guardarHash("TE", "07", "7V", "17994");
  80. // tmpSaftptHash.guardarHash("TE", "07", "7V", "17995");
  81. // tmpSaftptHash.guardarHash("TE", "07", "7V", "17996");
  82. // } catch (Exception e) {
  83. // e.printStackTrace();
  84. // }
  85.  
  86. java.util.Date dnow = new java.util.Date();
  87. Calendar cnow = new GregorianCalendar();
  88. cnow.setTime(dnow);
  89. SimpleDateFormat sdf_date = new SimpleDateFormat("yyyy-MM-dd");
  90. SimpleDateFormat sdf_db2 = new SimpleDateFormat("yyMMdd");
  91. SimpleDateFormat sdf_pre = new SimpleDateFormat("yyyy");
  92. SimpleDateFormat sdf_kaka = new SimpleDateFormat("yyyyMMdd");
  93. SimpleDateFormat sdf_time = new SimpleDateFormat("HHmmss");
  94.  
  95. if (consulta.containsKey("submit") && !consulta.get("submit").equals("")) {
  96.  
  97. if (consulta.get("empresa").equals("")) {
  98. errormessages.add("El parámetro 'Empresa' está vacío ");
  99. }
  100.  
  101. if (consulta.get("company").equals("")) {
  102. errormessages.add("El parámetro 'Company' está vacío ");
  103. }
  104.  
  105. if (consulta.get("fiscalyear").equals("")) {
  106. errormessages.add("El parámetro 'Año Fiscal' está vacío ");
  107. }
  108.  
  109. try {
  110. mysqldf = new SimpleDateFormat("dd/MM/yy");
  111. consulta.put("dfrom", mysqldf.parse((String) consulta.get("from")));
  112. } catch (Exception e1) {
  113. try {
  114. mysqldf = new SimpleDateFormat("dd/MM/yyyy");
  115. consulta.put("dfrom", mysqldf.parse((String) consulta.get("from")));
  116. } catch (Exception e2) {
  117. try {
  118. mysqldf = new SimpleDateFormat("yyyy-MM-dd");
  119. consulta.put("dfrom", mysqldf.parse((String) consulta.get("from")));
  120. } catch (Exception e3) {
  121. errormessages.add("El parámetro 'Desde' no contiene el formato correcto");
  122. mysqldf = new SimpleDateFormat("dd/MM/yyyy");
  123. consulta.put("dfrom", mysqldf.parse("01/01/2011")); // SimpleDateFormat
  124. }
  125. }
  126. }
  127.  
  128. try {
  129. mysqldf = new SimpleDateFormat("dd/MM/yy");
  130. consulta.put("dto", mysqldf.parse((String) consulta.get("to")));
  131. } catch (Exception e1) {
  132. try {
  133. mysqldf = new SimpleDateFormat("dd/MM/yyyy");
  134. consulta.put("dto",mysqldf.parse((String) consulta.get("to")));
  135. } catch (Exception e2) {
  136. try {
  137. mysqldf = new SimpleDateFormat("yyyy-MM-dd");
  138. consulta.put("dto",mysqldf.parse((String) consulta.get("to")));
  139. } catch (Exception e3) {
  140. errormessages.add("El parámetro 'Hasta' no contiene el formato correcto");
  141. mysqldf = new SimpleDateFormat("dd/MM/yyyy");
  142. consulta.put("dto", mysqldf.parse("31/12/2011")); // SimpleDateFormat
  143. }
  144. }
  145. }
  146.  
  147. if (errormessages.size() == 0) {
  148.  
  149. 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
  150. + "FROM MAEQF WHERE MAQEMP = '"+consulta.get("company") +"' AND MAQTIR = 'Q' AND TRIM(MAEQF.MAQCUE) = '" + consulta.get("empresa").toString().split("'")[1].trim() + "' ";
  151.  
  152. t1 = Calendar.getInstance();
  153. System.out.println("<!-- // SQL[MAEQF]: " + SQL + " -->");
  154.  
  155. empresa = getDataByField(SQL);
  156.  
  157. t2 = Calendar.getInstance();
  158. System.out.println("<!-- // HASH[MAEQF]: " + empresa + " -->");
  159. System.out.println("<!-- // STAMP: " + t2.getTimeInMillis() + " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
  160.  
  161. checkPoblacion(errormessages, empresa, "MAQ");
  162. }
  163.  
  164. if (errormessages.size() == 0) {
  165.  
  166. String tmpDatosCompany = "SELECT MAENOM, MAEDIR, MAEPOB, MAEPRO, MAEDIP, MAETEL, MAETLX, MAECIF "
  167. + "FROM MAEEF A WHERE MAECUE='"+consulta.get("company") +"'";
  168. System.out.println("<!-- // SQL[MAEEF]: " + tmpDatosCompany + " -->");
  169.  
  170. company = getDataByField(tmpDatosCompany);
  171. checkPoblacion(errormessages, company, "MAE");
  172. }
  173.  
  174. // Consulta clientes
  175. if (errormessages.size() == 0) {
  176.  
  177. String tmpClientesConALMGF = ""
  178. + "SELECT ALMGF.ALGCLI FROM ALMGF AS ALMGF WHERE "
  179. + "ALMGF.ALGEMP = '"
  180. + consulta.get("company")
  181. + "' "
  182. + "AND ALMGF.ALGTIA = 'V' "
  183. + "AND ALMGF.ALGSF1 = '' "
  184. + "AND ALMGF.ALGSF2 = '' "
  185. + "AND ALMGF.ALGNUD <> 0 "
  186. + "AND ALMGF.ALGTIR = 'F' "
  187. // + "AND ALMGF.ALGTEM = '"
  188. // + consulta.get("empresa")
  189. // + "' "
  190.  
  191. + "AND ALMGF.ALGTEM in ("+consulta.get("empresa").toString()+") "
  192.  
  193. + "AND ALMGF.ALGEST IN ('FRA', 'ABO') "
  194. + "AND CONCAT(ALGA21, ALGN21) BETWEEN '"
  195. + sdf_kaka.format((java.util.Date) consulta.get("dfrom")) + "' AND '"
  196. + sdf_kaka.format((java.util.Date) consulta.get("dto")) + "' ";
  197.  
  198. Vector tmpResultClientesConALMGF = SQLLauncher.executeQuery(tmpClientesConALMGF, Integer.MAX_VALUE, 0, false);
  199. String strClientes = "";
  200.  
  201. if (tmpResultClientesConALMGF != null && tmpResultClientesConALMGF.size() > 0) {
  202. Vector row = null;
  203.  
  204. for ( int i=0; i<tmpResultClientesConALMGF.size(); i++ ) {
  205. row = (Vector)tmpResultClientesConALMGF.get(i);
  206. strClientes += "'"+row.get(0).toString()+"',";
  207. }
  208. strClientes = strClientes.substring(0,strClientes.length()-1);
  209. }
  210.  
  211. SQL = "SELECT "
  212. + "TRIM(SUBSTR(MAECF.MACCUE, 6)) as MACCUE, "
  213. + "MAECF.MACCIF as MACCIF, "
  214. + "MAECF.MACEMP as MACEMP, "
  215. + "MAECF.MACRPI as MACRPI, "
  216. + "MAECF.MACNOC as MACNOC, "
  217. + "EXCCFCN.EXCTE1 as EXCNOC, "
  218. + "MAECF.MACNOM as MACNOM, "
  219. + "EXCCFCN.EXCTE4 as EXCNOM, "
  220. + "EXCCFPC.EXCTE1 as EXCTE1, "
  221. + "MAECF.MACDIR AS MACDIR, "
  222. + "EXCCFCN.EXCTE2 as EXCDIR, "
  223. + "MAECF.MACPOB AS MACPOB, "
  224. + "EXCCFCN.EXCTE3 as EXCPOB, "
  225. + "MAECF.MACPRO AS MACPRO, "
  226. + "MAECF.MACDIP AS MACDIP "
  227. + "FROM MAECF "
  228. + "LEFT JOIN "
  229. + " EXCCF AS EXCCFPC "
  230. + "ON ( "
  231. + "MAECF.MACEMP = EXCCFPC.EXCEMP "
  232. + "AND MAECF.MACTIR = EXCCFPC.EXCTIR "
  233. + "AND EXCCFPC.EXCTIE = 'PC' "
  234. + "AND MAECF.MACCUE = EXCCFPC.EXCCUE "
  235. + "AND EXCCFPC.EXCFOR = '1' "
  236. + ") "
  237. + "LEFT JOIN "
  238. + " EXCCF AS EXCCFCN "
  239. + "ON ( "
  240. + "MAECF.MACEMP = EXCCFCN.EXCEMP "
  241. + "AND MAECF.MACTIR = EXCCFCN.EXCTIR "
  242. + "AND EXCCFCN.EXCTIE = 'CN' "
  243. + "AND MAECF.MACCUE = EXCCFCN.EXCCUE "
  244. + "AND EXCCFCN.EXCFOR = '1' "
  245. + ") "
  246. + "WHERE MAECF.MACEMP='"
  247. + consulta.get("company")
  248. + "' AND MAECF.MACCUE IN ("+strClientes+")";
  249.  
  250.  
  251. t1 = Calendar.getInstance();
  252. System.out.println("<!-- // SQL[MAECF]: " + SQL + " -->");
  253.  
  254. cargarDatos(SQL, clientes, o_clientes, "MACCUE");
  255.  
  256. t2 = Calendar.getInstance();
  257. System.out.println("<!-- // HASH[MAECF]: " + clientes + " -->");
  258. System.out.println("<!-- // STAMP: " + t2.getTimeInMillis() + " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
  259. }
  260.  
  261. if (errormessages.size() == 0) {
  262.  
  263. SQL = "SELECT MAEJF.MAJELE as MAJELE, MAEJF.MAJNOM as MAJNOM, MAEJF.MAJVA1 as MAJVA1, MAEJF.MAJVA2 as MAJVA2 "
  264. + "FROM MAEJF AS MAEJF " + "WHERE " + "MAEJF.MAJEMP = '" + consulta.get("company") + "' "
  265. + "AND MAEJF.MAJTIR = 'J' AND MAEJF.MAJCUE = 'IV' AND MAEJF.MAJELE IN ( "
  266. + "SELECT DISTINCT "
  267. + "ALMHF.ALHTIV AS ALHTIV "
  268. + "FROM "
  269. + " ALMHF AS ALMHF "
  270. + "WHERE "
  271. + "ALMHF.ALHEMP = '"
  272. + consulta.get("company")
  273. + "' "
  274. + "AND ALMHF.ALHTIA = 'V' "
  275. + "AND ALMHF.ALHSE1 = '' "
  276. + "AND ALMHF.ALHSE2 = '' "
  277. + "AND ALMHF.ALHSF1 = ' ' "
  278. + "AND ALMHF.ALHNUD <> 0 "
  279. + "AND ALMHF.ALHTIR = 'H' "
  280. + "AND ALHENV >= 0 "
  281. + "AND ALHLIN >= 0 "
  282. // + "AND ALMHF.ALHTEM = '"
  283. // + consulta.get("empresa") + "' "
  284.  
  285. + "AND ALMHF.ALHTEM in ("+consulta.get("empresa").toString()+") "
  286.  
  287. + "AND ALMHF.ALHEST IN ('FRA', 'ABO') "
  288. + "AND ALMHF.ALHAFF BETWEEN '"
  289. + sdf_pre.format((java.util.Date) consulta.get("dfrom")).substring(0, 2) + "' "
  290. + "AND '" + sdf_pre.format((java.util.Date) consulta.get("dto")).substring(0, 2) + "' "
  291. + "AND ALMHF.ALHFFR BETWEEN '" + sdf_db2.format((java.util.Date) consulta.get("dfrom")) + "' "
  292. + "AND '" + sdf_db2.format((java.util.Date) consulta.get("dto")) + "' "
  293. + ") " + "ORDER BY " + "MAEJF.MAJNOM";
  294.  
  295. t1 = Calendar.getInstance();
  296. System.out.println("<!-- // SQL[MAEJF]: " + SQL + " -->");
  297.  
  298. cargarDatos(SQL, ivas, o_ivas, "MAJELE");
  299.  
  300. t2 = Calendar.getInstance();
  301. System.out.println("<!-- // HASH[MAEJF]: " + ivas + " -->");
  302. System.out.println("<!-- // STAMP: " + t2.getTimeInMillis() + " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
  303.  
  304. SQL = "SELECT ALGEMP FROM ALMGF AS ALMGF WHERE "
  305. + "ALMGF.ALGEMP = '" + consulta.get("company")+ "' "
  306. + "AND ALMGF.ALGTIA = 'V' "
  307. + "AND ALMGF.ALGSF1 = '' "
  308. + "AND ALMGF.ALGSF2 = '' "
  309. + "AND ALMGF.ALGNUD <> 0 "
  310. + "AND ALMGF.ALGTIR = 'F' "
  311. // + "AND ALMGF.ALGTEM = '"
  312. // + consulta.get("empresa") + "' "
  313.  
  314. + "AND ALMGF.ALGTEM in ("+consulta.get("empresa").toString()+") "
  315.  
  316. + "AND ALMGF.ALGEST IN ('FRA', 'ABO') "
  317. + "AND CONCAT(ALGA21, ALGN21) BETWEEN '"
  318. + sdf_kaka.format((java.util.Date) consulta.get("dfrom")) + "' AND '"
  319. + sdf_kaka.format((java.util.Date) consulta.get("dto")) + "' "
  320. + "AND ALMGF.ALGTIV = '0' ";
  321.  
  322. Vector tmpResultSQL = SQLLauncher.executeQuery(SQL, Integer.MAX_VALUE, 0, false);
  323.  
  324. if (tmpResultSQL != null && tmpResultSQL.size() > 0) {
  325. SQL = "SELECT MAEJF.MAJELE as MAJELE, "
  326. + "MAEJF.MAJNOM as MAJNOM, "
  327. + "MAEJF.MAJVA1 as MAJVA1, "
  328. + "MAEJF.MAJVA2 as MAJVA2 " + "FROM "
  329. + " MAEJF AS MAEJF " + "WHERE "
  330. + "MAEJF.MAJEMP = '" + consulta.get("company")
  331. + "' " + "AND MAEJF.MAJTIR = 'J' "
  332. + "AND MAEJF.MAJCUE = 'IV' "
  333. + "AND MAEJF.MAJELE = '0' ";
  334.  
  335. iva = getDataByField(SQL);
  336.  
  337. if (iva.size() > 0) {
  338. ivas.put(iva.get("MAJELE").toString().trim(), iva);
  339. o_ivas.add("0"); // si no, no pone el iva exento en el desglose superior del XML tabla TaxTable
  340. }
  341. System.out.println("<!-- // HASH[MAEJF]: " + ivas + " -->");
  342. }
  343. }
  344.  
  345. // Consulta articulos
  346. if (errormessages.size() == 0) {
  347. SQL = "SELECT MAEAF.MAACUE AS MAACUE, "
  348. + "MAEAF.MAANOM AS MAANOM, "
  349. + "MAEAF.MAAFAM AS MAAFAM, "
  350. + "AUXAF.AUACO1 AS AUACO1 FROM "
  351. + " MAEAF AS MAEAF LEFT JOIN AUXAF AS AUXAF "
  352. + "ON ( MAEAF.MAAEMP = AUXAF.AUAEMP "
  353. + "AND MAEAF.MAATIR = AUXAF.AUATIR "
  354. + "AND MAEAF.MAACUE = AUXAF.AUACUE ) WHERE "
  355. + "MAEAF.MAAEMP = '" + consulta.get("company") + "' "
  356. + "AND MAEAF.MAATIR = 'A' "
  357. + "AND MAEAF.MAACUE IN ( "
  358. + "SELECT DISTINCT "
  359. + "ALMHF.ALHART AS ALHART "
  360. + "FROM "
  361. + " ALMHF AS ALMHF "
  362. + "WHERE ALMHF.ALHEMP = '"
  363. + consulta.get("company") + "' "
  364. + "AND ALMHF.ALHTIA = 'V' "
  365. + "AND ALMHF.ALHSE1 = '' "
  366. + "AND ALMHF.ALHSE2 = '' "
  367. + "AND ALMHF.ALHSF1 = ' ' "
  368. + "AND ALMHF.ALHNUD <> 0 "
  369. + "AND ALMHF.ALHTIR = 'H' "
  370. + "AND ALHENV >= 0 "
  371. + "AND ALHLIN >= 0 "
  372. // + "AND ALMHF.ALHTEM = '" + consulta.get("empresa") + "' "
  373. + "AND ALMHF.ALHTEM in ("+consulta.get("empresa").toString()+") "
  374.  
  375. + "AND ALMHF.ALHEST IN ('FRA', 'ABO') "
  376. + "AND ALHAFF BETWEEN '"
  377. + sdf_pre.format((java.util.Date) consulta.get("dfrom")).substring(0, 2)
  378. + "' AND '" + sdf_pre.format((java.util.Date) consulta.get("dto")).substring(0, 2) + "' "
  379. + "AND ALHFFR BETWEEN '" + sdf_db2 .format((java.util.Date) consulta.get("dfrom")) + "' AND '"
  380. + sdf_db2.format((java.util.Date) consulta.get("dto")) + "' " + ") ";
  381.  
  382. t1 = Calendar.getInstance();
  383. System.out.println("<!-- // SQL[MAEAF]: " + SQL + " -->");
  384.  
  385. cargarDatos(SQL, articulos, o_articulos, "MAACUE");
  386.  
  387. t2 = Calendar.getInstance();
  388. System.out.println("<!-- // HASH[MAEAF]: " + articulos + " -->");
  389. System.out.println("<!-- // STAMP: " + t2.getTimeInMillis() + " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
  390. }
  391. // Consulta facturas
  392. if (errormessages.size() == 0) {
  393. // Calculamos las ventas
  394. String SQL_ALMGF = "SELECT "
  395. + "CONCAT(CONCAT(ALMGF.ALGSE2, '/'), ALMGF.ALGNUD) AS HASKEY, "
  396. + "ALMGF.ALGEMP AS ALGEMP, "
  397. + "ALMGF.ALGTIA AS ALGTIA, "
  398. + "ALMGF.ALGNUD AS ALGNUD, "
  399. + "ALMGF.ALGALB AS ALGALB, "
  400. + "ALMGF.ALGSE1 AS ALGSE1, "
  401. + "ALMGF.ALGSE2 AS ALGSE2, "
  402. + "ALMGF.ALGTIR AS ALGTIR, "
  403. + "TRIM(SUBSTR(ALMGF.ALGCLI, 6)) AS ALGCLI, "
  404. + "ALMGF.ALGNOC AS ALGNOC, "
  405. + "ALMGF.ALGENV AS ALGENV, "
  406. + "ALMGF.ALGLIN AS ALGLIN, "
  407. + "ALMGF.ALGREF AS ALGREF, "
  408. + "ALMGF.ALGEST AS ALGEST, "
  409. // Totales de la factura
  410. + "ALMGF.ALGVPE AS ALGVPE, "
  411. + "ALMGF.ALGVSE AS ALGVSE, "
  412. + "ALMGF.ALGN10 AS ALGN10, "
  413. + "(ALMGF.ALGVSE + ALMGF.ALGN10) AS ALGSUM, "
  414. // Descuentos
  415. + "ALMGF.ALGDEM AS ALGDEM, "
  416. + "ALMGF.ALGPPG AS ALGPPG, "
  417. // Regargo de Equivalencia
  418. + "ALMGF.ALGTIV AS ALGTIV, "
  419. // Fecha Factura
  420. + "ALMGF.ALGA21 AS ALGA21, "
  421. + "ALMGF.ALGN21 AS ALGN21, "
  422. // Fecha y Hora creación del documento
  423. + "ALMGF.ALGFCR AS ALGFCR, "
  424. + "ALMGF.ALGHCR AS ALGHCR, "
  425. + "ALMGF.ALGOP2 AS ALGOP2, "
  426. + "ALMGF.ALGOPR AS ALGOPR, "
  427. + "ALMGF.ALGTEM AS ALGTEM, "
  428. + "ALMGF.ALGEMP AS ALGEMP, "
  429. + "ALMGF.ALGPOR AS ALGPOR, "
  430. + "ALMGF.ALGA72 AS ALGA72, "
  431. + "ALMGF.ALGN74 AS ALGN74, "
  432. + "ALMGF.ALGN55 AS ALGN55 "
  433. + "FROM "
  434. + " ALMGF AS ALMGF "
  435. + "WHERE "
  436. + "ALMGF.ALGEMP = '"
  437. + consulta.get("company")
  438. + "' "
  439. + "AND ALMGF.ALGTIA = 'V' "
  440. + "AND ALMGF.ALGSF1 = '' "
  441. + "AND ALMGF.ALGSF2 = '' "
  442. + "AND ALMGF.ALGNUD <> 0 "
  443. + "AND ALMGF.ALGTIR = 'F' " // Siempre 'G'
  444. // + "AND ALMGF.ALGTEM = '"
  445. // + consulta.get("empresa")
  446. // + "' "
  447.  
  448. + "AND ALMGF.ALGTEM in ("+consulta.get("empresa").toString()+") "
  449.  
  450. // Seleccion de Articulos
  451. + "AND ALMGF.ALGEST IN ('FRA', 'ABO') "
  452. // AITOR 17/11/201
  453. // Fecha de Facturación = ALGA21 + ALGN21
  454. + "AND CONCAT(ALGA21, ALGN21) BETWEEN '"
  455. + sdf_kaka.format((java.util.Date) consulta.get("dfrom")) + "' AND '"
  456. + sdf_kaka.format((java.util.Date) consulta.get("dto")) + "' ";
  457.  
  458. // Calculamos las lineas
  459. String SQL_ALMHF = "SELECT "
  460. + "CONCAT(CONCAT(ALMHF.ALHSF2, '/'), ALMHF.ALHFRA) AS HASKEY, "
  461. + "CONCAT(CONCAT(ALMHF.ALHLIN, '/'), ALMHF.ALHENV) AS SUBKEY, "
  462. + "ALMHF.ALHNUD AS ALHNUD,"
  463. + "ALMHF.ALHSF1 AS ALHSF1,"
  464. + "ALMHF.ALHSF2 AS ALHSF2,"
  465. + "ALMHF.ALHFRA AS ALHFRA,"
  466. + "ALMHF.ALHLIN AS ALHLIN, "
  467. + "ALMHF.ALHART AS ALHART, "
  468. + "ALMHF.ALHNOA AS ALHNOA, "
  469. + "ALMHF.ALHN07 AS ALHN07, " // Cantidad
  470. + "ALMHF.ALHN09 AS ALHN09, " // C??
  471. + "(ALMHF.ALHPRE/ALMHF.ALHUPV) AS ALHPRE, " // PVP unitario en Almacen
  472. + "ALMHF.ALHIMP AS ALHIMP, " // Importe de la linea
  473. + "(ALMHF.ALHIMP * ALMHF.ALHIVA/100) AS ALHAIT, " // Porcentaje de IVA (Dato derivado)
  474. + "ALMHF.ALHTIV AS ALHTIV, " // Tipo de IVA
  475. + "ALMHF.ALHIVA AS ALHIVA, " // Porcentaje de IVA (Dato derivado)
  476. + "ALMHF.ALHUPV AS ALHUPV, " // Unidad precio de Venta
  477. + "CONCAT(ALMHF.ALHAFP, ALMHF.ALHFPR) AS ALHFPR, " // Fecha de Servicio (en portugal igual a la de factura
  478. + "CONCAT(ALMHF.ALHAFF, ALMHF.ALHFFR) AS ALHFFR, " // Fecha factura
  479. + "ALMHF.ALHTDC AS ALHTDC " // total descuentos linea
  480. + "FROM " + " ALMHF AS ALMHF " + "WHERE "
  481. + "ALMHF.ALHEMP = '"
  482. + consulta.get("company") + "' "
  483. + "AND ALMHF.ALHTIA = 'V' " // Tipo de aplicación : 'V' de Ventas
  484. + "AND ALMHF.ALHSF1 = ' ' "
  485. + "AND ALMHF.ALHNUD <> 0 "
  486. + "AND ALMHF.ALHTIR = 'H' " // Siempre 'H'
  487. + "AND ALHENV >= 0 "
  488. + "AND ALHLIN >= 0 "
  489. // + "AND ALMHF.ALHTEM = '"
  490. // + consulta.get("empresa") + "' "
  491.  
  492. + "AND ALMHF.ALHTEM in ("+consulta.get("empresa").toString()+") "
  493.  
  494. + "AND ALMHF.ALHEST IN ('FRA', 'ABO') "
  495. + "AND ALHAFF BETWEEN '"
  496. + sdf_pre.format((java.util.Date) consulta.get("dfrom")).substring(0, 2)
  497. + "' AND '" + sdf_pre.format((java.util.Date) consulta.get("dto")) .substring(0, 2) + "' "
  498. + "AND ALHFFR BETWEEN '" + sdf_db2.format((java.util.Date) consulta.get("dfrom"))
  499. + "' AND '" + sdf_db2.format((java.util.Date) consulta.get("dto")) + "' ";
  500.  
  501. // Gestionamos primero las inconsistencais
  502. SQL = SQL_ALMHF
  503. + "AND NOT EXISTS ("
  504. + SQL_ALMGF
  505. + " AND (ALMGF.ALGSE2 CONCAT '/' CONCAT ALMGF.ALGNUD) = (ALMHF.ALHSF2 CONCAT '/' CONCAT ALMHF.ALHFRA) "
  506. + ") ";
  507.  
  508. t1 = Calendar.getInstance();
  509. System.out.println("<!-- // SQL[inconsistencias]: " + SQL + " -->");
  510.  
  511. // HashMap elems = web_getElems(SQL);
  512. Vector tmpResultSQL = SQLLauncher.executeQuery(SQL, Integer.MAX_VALUE, 0, false);
  513.  
  514. t2 = Calendar.getInstance();
  515. System.out.println("<!-- // STAMP: " + t2.getTimeInMillis() + " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
  516.  
  517. if (tmpResultSQL.size() > 0) {
  518. errormessages.add("Incoherencia entre lineas y cabecera, factura '" + ((Vector) tmpResultSQL.get(0)).get(4).toString() + "/"
  519. + ((Vector) tmpResultSQL.get(0)).get(2).toString() + "'.");
  520. }
  521.  
  522. if (errormessages.size() == 0) {
  523. t1 = Calendar.getInstance();
  524. System.out.println("<!-- // SQL[ALMGF]: " + SQL_ALMGF + " -->");
  525.  
  526. cargarDatos(SQL_ALMGF, facturas, o_facturas);
  527.  
  528. // si la factura tiene portes, los añadimos como una linea mas
  529. cargarArticuloPortes(consulta, articulos, o_articulos, facturas, o_facturas);
  530.  
  531. cargarArticuloECO(consulta, articulos, o_articulos, facturas, o_facturas);
  532.  
  533. t2 = Calendar.getInstance();
  534. System.out.println("<!-- // STAMP: " + t2.getTimeInMillis() + " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
  535.  
  536. t1 = Calendar.getInstance();
  537. System.out.println("<!-- // SQL[ALMHF]: " + SQL_ALMHF + " -->");
  538.  
  539. cargarLineasAFacturas(SQL_ALMHF, facturas, "lineas", "HASKEY", "SUBKEY");
  540. // web_addSubElems(SQL_ALMHF, facturas, "lineas", "HASKEY", "SUBKEY");
  541.  
  542. t2 = Calendar.getInstance();
  543. System.out.println("<!-- // STAMP: " + t2.getTimeInMillis()+ " : " + (t2.getTimeInMillis() - t1.getTimeInMillis()) + " -->");
  544. }
  545.  
  546. // Verificar que toda factura contenga lineas
  547. it = facturas.keySet().iterator();
  548.  
  549. while (it.hasNext()) {
  550. HashMap elem = (HashMap) facturas.get(it.next());
  551. if (!elem.containsKey("lineas") || ((HashMap) elem.get("lineas")).size() == 0) {
  552. errormessages .add("Factura sin lineas, numero factura '" + elem.get("ALGSE2") + "/" + elem.get("ALGNUD") + "'.");
  553. }
  554. }
  555. }
  556. }
  557.  
  558. if (errormessages.size() == 0 && consulta.containsKey("submit") && !consulta.get("submit").equals("")) {
  559. sFichero = tmpPath + "saftpt" + "-" + sdf_kaka.format((java.util.Date) consulta.get("dfrom")) + "-"
  560. + sdf_kaka.format((java.util.Date) consulta.get("dto")) + "-" + sdf_kaka.format(dnow) + sdf_time.format(dnow) + ".xml";
  561.  
  562. File ficheroSaft = new File(sFichero);
  563. if(!ficheroSaft.exists()) {
  564. ficheroSaft.createNewFile();
  565. }
  566. BufferedWriter bw = new BufferedWriter(new FileWriter(ficheroSaft));
  567. String tmpNif, tmpMaqnom, tmpMaqdir, tmpMaqpob, tmpMaqzip, tmpMaqtel, tmpMaqfax;
  568.  
  569. if(tmpIsPorCompany){
  570. //por company
  571. tmpNif=((String)company.get("MAECIF")).trim();
  572. tmpMaqnom=((String)company.get("MAENOM")).trim();
  573. tmpMaqdir=((String) company.get("MAEDIR")).replace('&', ' ').trim();
  574. tmpMaqpob=((String) company.get("MAEPOB")).trim();
  575. tmpMaqzip=((String) company.get("MAEZIP")).trim();
  576. tmpMaqtel=((String)company.get("MAETEL")).trim();
  577. tmpMaqfax=((String)company.get("MAETLX")).trim();
  578. }else{
  579. //por empresa
  580. tmpNif=((String)empresa.get("MAQCIF")).trim();
  581. tmpMaqnom=((String)empresa.get("MAQNOM")).trim();
  582. tmpMaqdir=((String) empresa.get("MAQDIR")).replace('&', ' ').trim();
  583. tmpMaqpob=((String) empresa.get("MAQPOB")).trim();
  584. tmpMaqzip=((String) empresa.get("MAQZIP")).trim();
  585. tmpMaqtel=((String)empresa.get("MAQTEL")).trim();
  586. tmpMaqfax=((String)empresa.get("MAQTLX")).trim();
  587. }
  588.  
  589. bw.write("<?xml version=\"1.0\" encoding=\"Windows-1252\"?>\r\n");
  590. bw.write("<AuditFile xmlns=\"urn:OECD:StandardAuditFile-Tax:PT_1.03_01\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n");
  591. bw.write("\t<Header>\r\n");
  592. bw.write("\t\t<AuditFileVersion>1.03_01</AuditFileVersion>\r\n");
  593. bw.write("\t\t<CompanyID>" + tmpNif + "</CompanyID>\r\n");
  594. bw.write("\t\t<TaxRegistrationNumber>" + tmpNif + "</TaxRegistrationNumber>\r\n");
  595. bw.write("\t\t<TaxAccountingBasis>F</TaxAccountingBasis>\r\n");
  596. bw.write("\t\t<CompanyName>" + tmpMaqnom + "</CompanyName>\r\n");
  597. bw.write("\t\t<CompanyAddress>\r\n");
  598. bw.write("\t\t\t<AddressDetail>" + tmpMaqdir + "</AddressDetail>\r\n");
  599. bw.write("\t\t\t<City>" + tmpMaqpob + "</City>\r\n");
  600. bw.write("\t\t\t<PostalCode>" + tmpMaqzip + "</PostalCode>\r\n");
  601. bw.write("\t\t\t<Country>PT</Country>\r\n");
  602. bw.write("\t\t</CompanyAddress>\r\n");
  603. bw.write("\t\t<FiscalYear>" + consulta.get("fiscalyear") + "</FiscalYear>\r\n");
  604. bw.write("\t\t<StartDate>");
  605. bw.write(sdf_date.format((java.util.Date) consulta.get("dfrom")));
  606. bw.write("</StartDate>\r\n");
  607. bw.write("\t\t<EndDate>");
  608. bw.write(sdf_date.format((java.util.Date) consulta.get("dto")));
  609. bw.write("</EndDate>\r\n");
  610. bw.write("\t\t<CurrencyCode>" + "EUR" + "</CurrencyCode>\r\n");
  611. bw.write("\t\t<DateCreated>");
  612. bw.write(sdf_date.format(cnow.getTime()));
  613. bw.write("</DateCreated>\r\n");
  614. bw.write("\t\t<TaxEntity>" + "Global" + "</TaxEntity>\r\n");
  615. bw.write("\t\t<ProductCompanyTaxID>" + tmpNif + "</ProductCompanyTaxID>\r\n");
  616. bw.write("\t\t<SoftwareCertificateNumber>" + "0" + "</SoftwareCertificateNumber>\r\n");
  617. bw.write("\t\t<ProductID>Rudder/Gabinete de Informatica AK</ProductID>\r\n");
  618. bw.write("\t\t<ProductVersion>07.00.0000</ProductVersion>\r\n");
  619. bw.write("\t\t<Telephone>" + tmpMaqtel + "</Telephone>\r\n");
  620. bw.write("\t\t<Fax>" + tmpMaqfax + "</Fax>\r\n");
  621. bw.write("\t</Header>\r\n");
  622. bw.write("\t<MasterFiles>\r\n");
  623.  
  624. it = o_clientes.iterator();
  625.  
  626. while (it.hasNext()) {
  627.  
  628. String o = (String) it.next();
  629. cliente = (HashMap) clientes.get(o);
  630.  
  631. bw.write("\t\t<Customer>\r\n");
  632. bw.write("\t\t\t<CustomerID>");
  633. bw.write(tmpCuentaMayorCliente + StringUtilities.fillLeftSpaces(cliente.get("MACCUE").toString().trim(),6));
  634. bw.write("</CustomerID>\r\n");
  635. bw.write("\t\t\t<AccountID>" + "Desconhecido" + "</AccountID>\r\n");
  636. bw.write("\t\t\t<CustomerTaxID>" + cliente.get("MACCIF") + "</CustomerTaxID>\r\n");
  637. bw.write("\t\t\t<CompanyName>");
  638. bw.write(((String) cliente.get("MACNOM")).replace('&', ' '));
  639.  
  640. if (!((String) cliente.get("EXCNOM")).trim().equals("")) {
  641. bw.write("\r\n" + ((String) cliente.get("EXCNOM")));
  642. }
  643.  
  644. bw.write("</CompanyName>\r\n");
  645. bw.write("\t\t\t<BillingAddress>\r\n");
  646.  
  647. String tmpMacdip=(String)cliente.get("MACDIP");
  648. if(tmpMacdip.equals("")) tmpMacdip="PT";
  649. String tmpMacemp=(String)cliente.get("MACEMP");
  650. String tmpCountry="";
  651.  
  652. if (!tmpMacdip.toString().trim().equals("99")) { // Todo lo que no sea 99 se considera Portugal.
  653. tmpCountry="PT";
  654. }else{
  655. tmpCountry = getPaises(tmpMacemp).get(tmpMacdip.trim()).toString();
  656. }
  657. bw.write("\t\t\t\t\r<Country>"+tmpCountry+"</Country>\r\n");
  658.  
  659. bw.write("\t\t\t</BillingAddress>\r\n");
  660. bw.write("\t\t\t<SelfBillingIndicator>0</SelfBillingIndicator>\r\n");
  661. bw.write("\t\t</Customer>\r\n");
  662.  
  663.  
  664. // bw.write("\t\t\t\t<AddressDetail>");
  665. // bw.write(((String) cliente.get("MACDIR")).replace('&', ' '));
  666. //
  667. // if (!((String) cliente.get("EXCDIR")).trim().equals("")) {
  668. // bw.write("\r\n" + ((String) cliente.get("EXCDIR")).replace('&', ' '));
  669. // }
  670. // bw.write("</AddressDetail>\r\n");
  671.  
  672. // String codigoPostal = (String) cliente.get("MACPRO") + (String) cliente.get("MACDIP");
  673. // String poblacion = ((String) cliente.get("MACPOB"));
  674. // String provincia = ((String) cliente.get("MACPRO"));
  675. // String country = "ES";
  676. //
  677. // if ( provincia.equals("99" ) ) {
  678. // country = getPaises(cliente.get("MACEMP").toString()).get(cliente.get("MACDIP").toString().trim()).toString();
  679. // codigoPostal = cliente.get("MACDIP").toString();
  680. // }
  681. //
  682. // if (cliente.get("MACDIP").toString().trim().equals("P")) {
  683. //
  684. // Pattern p = Pattern.compile("[0-9]{4}-[0-9]{3}");
  685. // Matcher m = p.matcher((String) cliente.get("MACPOB"));
  686. //
  687. // if (m.find() == true) {
  688. // codigoPostal = m.group();
  689. // } else {
  690. // alertmessages.add("Cliente sin código postal, código cliente : " + cliente.get("MACCUE") + ", se ha puesto uno fake");
  691. // codigoPostal = "2810-237";
  692. // }
  693. // poblacion = poblacion.substring(codigoPostal.length()).trim(); // Para no repetir el codigo postal en caso de PT
  694. // }
  695. //
  696. // bw.write("\t\t\t\t<City>" + poblacion.trim() + "</City>\r\n");
  697. // bw.write("\t\t\t\t<PostalCode>" + codigoPostal.trim() + " </PostalCode>\r\n");
  698. // bw.write("\t\t\t\t<Country>" + country.trim() + "</Country>\r\n");
  699.  
  700.  
  701. // bw.write("\t\t\t</BillingAddress>\r\n");
  702. // bw.write("\t\t\t<SelfBillingIndicator>0</SelfBillingIndicator>\r\n");
  703. // bw.write("\t\t</Customer>\r\n");
  704. }
  705.  
  706. it = o_articulos.iterator();
  707.  
  708. while (it.hasNext()) {
  709. // Recogemos los datos
  710. String o = (String) it.next();
  711. articulo = (HashMap) articulos.get(o);
  712.  
  713. bw.write("\t\t<Product>\r\n");
  714. bw.write("\t\t\t<ProductType>" + "P" + "</ProductType>\r\n");
  715. bw.write("\t\t\t<ProductCode>" + articulo.get("MAACUE") + "</ProductCode>\r\n");
  716. bw.write("\t\t\t<ProductDescription>" + ((String) articulo.get("MAANOM")).replace('&', ' ').trim() + "</ProductDescription>\r\n");
  717. bw.write("\t\t\t<ProductNumberCode>" + articulo.get("MAACUE") + "</ProductNumberCode>\r\n");
  718. bw.write("\t\t</Product>\r\n");
  719. }
  720.  
  721. bw.write("\t\t<TaxTable>\r\n");
  722.  
  723. it = o_ivas.iterator();
  724.  
  725. while (it.hasNext()) {
  726.  
  727. String index = (String) it.next();
  728. iva = (HashMap) ivas.get(index);
  729.  
  730. bw.write("\t\t\t<TaxTableEntry>\r\n");
  731. bw.write("\t\t\t\t<TaxType>" + "IVA" + "</TaxType>\r\n");
  732. bw.write("\t\t\t\t<TaxCountryRegion>PT</TaxCountryRegion>\r\n");
  733.  
  734. if (((String) iva.get("MAJELE")).equals("0")) {
  735.  
  736. bw.write("\t\t\t\t<TaxCode>" + "ISE" + "</TaxCode>\r\n");
  737. } else {
  738.  
  739. bw.write("\t\t\t\t<TaxCode>" + "NOR" + "</TaxCode>\r\n");
  740. }
  741.  
  742. bw.write("\t\t\t\t<Description>" + iva.get("MAJNOM").toString().trim() + "</Description>\r\n");
  743. bw.write("\t\t\t\t<TaxPercentage>" + iva.get("MAJVA1").toString().trim() + "</TaxPercentage>\r\n");
  744. bw.write("\t\t\t</TaxTableEntry>\r\n");
  745.  
  746. }
  747. bw.write("\t\t</TaxTable>\r\n");
  748. bw.write("\t</MasterFiles>\r\n");
  749.  
  750. // Facturas
  751. it = o_facturas.iterator();
  752. BigDecimal tmp100 = new BigDecimal(100);
  753.  
  754. while (it.hasNext()) {
  755.  
  756. factura = (HashMap) facturas.get((String) it.next());
  757.  
  758. HashMap himportes = new HashMap();
  759. // HashMap hcanones = new HashMap();
  760. Iterator it_ivas = ivas.keySet().iterator();
  761.  
  762. while (it_ivas.hasNext()) {
  763. iva = (HashMap) ivas.get(it_ivas.next());
  764.  
  765. himportes.put(iva.get("MAJELE").toString().trim(), "0");
  766. // hcanones.put(iva.get("MAJELE").toString().trim(), "0");
  767. }
  768.  
  769. if (factura.containsKey("lineas")) {
  770.  
  771. lineas = (HashMap) factura.get("lineas");
  772. Iterator it_lineas = lineas.keySet().iterator();
  773. BigDecimal tmpImportPorTipoIvaImportes = new BigDecimal(0);
  774. //BigDecimal tmpImportPorTipoIvaCanones = new BigDecimal(0);
  775. BigDecimal tmpImporteLinea = new BigDecimal(0);
  776. BigDecimal tmpALHN07 = new BigDecimal(0);
  777. //BigDecimal tmpALHN09 = new BigDecimal(0);
  778. //BigDecimal tmpALHUPV = new BigDecimal(0);
  779. BigDecimal tmpTotalCanones = new BigDecimal(0);
  780. BigDecimal tmpDescuentoProntoPago = new BigDecimal(factura.get("ALGPPG").toString().trim().replaceAll("(\\r|\\n)", "")); // dto pronto pago
  781. BigDecimal tmpDescuentoPieFactura = new BigDecimal(factura.get("ALGDEM").toString().trim().replaceAll("(\\r|\\n)", "")); // dto pie factura
  782. BigDecimal tmpSumaDescuentoProntoPagoLinea = null;
  783. BigDecimal tmpSumaDescuentoPieFacturaLinea = null;
  784. String stiv = "0";
  785.  
  786. while (it_lineas.hasNext()) {
  787.  
  788. linea = (HashMap) lineas.get((String) it_lineas.next());
  789.  
  790. tmpSumaDescuentoProntoPagoLinea = new BigDecimal(0);
  791. tmpSumaDescuentoPieFacturaLinea = new BigDecimal(0);
  792.  
  793. if (!((String) factura.get("ALGTIV")).equals("0")) {
  794.  
  795. stiv = linea.get("ALHTIV").toString().trim();
  796. }
  797.  
  798. // cogemos el importe acumulado por base de iva
  799. tmpImportPorTipoIvaImportes = new BigDecimal(himportes.get(stiv).toString().replaceAll("(\\r|\\n)", ""));
  800.  
  801. // cogemos el campo que tiene calculado el precio por la cantidad
  802. tmpImporteLinea = new BigDecimal(linea.get("ALHIMP").toString().trim().replaceAll(",", ".")).abs();
  803.  
  804. // le aplicamos el descuento pronto pago a la linea
  805. if(tmpDescuentoProntoPago.compareTo(new BigDecimal(0))==1){
  806. tmpSumaDescuentoProntoPagoLinea = tmpImporteLinea.multiply(tmpDescuentoProntoPago).divide(tmp100, BigDecimal.ROUND_HALF_UP);
  807. tmpImporteLinea = tmpImporteLinea.subtract(tmpSumaDescuentoProntoPagoLinea);
  808. }
  809.  
  810. // le aplicamos el descuento pie de factura
  811. if(tmpDescuentoProntoPago.compareTo(new BigDecimal(0))==1){
  812. tmpSumaDescuentoPieFacturaLinea = tmpImporteLinea.multiply(tmpDescuentoPieFactura).divide(tmp100, 2, BigDecimal.ROUND_HALF_UP);
  813. tmpImporteLinea = tmpImporteLinea.subtract(tmpSumaDescuentoPieFacturaLinea);
  814. }
  815.  
  816. linea.put("IMPORTE_LINEA_CON_DESCUENTOS_APLICADOS", tmpImporteLinea.setScale(2, BigDecimal.ROUND_HALF_UP));
  817. linea.put("SUMA_DE_DESCUENTO_PIE_FACTURA_Y_PRONTO_PAGO", tmpSumaDescuentoProntoPagoLinea.add(tmpSumaDescuentoPieFacturaLinea));
  818.  
  819. // Agregamos las importes imponibles, referentes al tipo de iva
  820. himportes.put(stiv, tmpImportPorTipoIvaImportes.add(tmpImporteLinea.setScale(2, BigDecimal.ROUND_HALF_UP)));
  821.  
  822. // // cogemos la ecotasa acumulada por base de iva
  823. //tmpImportPorTipoIvaCanones = new BigDecimal(hcanones.get(stiv).toString().replaceAll("(\\r|\\n)", ""));
  824. //tmpALHN07 = new BigDecimal(linea.get("ALHN07").toString().replaceAll("(\\r|\\n)", ""));
  825. //tmpALHN09 = new BigDecimal(linea.get("ALHN09").toString().replaceAll("(\\r|\\n)", "")).divide(new BigDecimal(100), 4, BigDecimal.ROUND_HALF_UP);
  826. //tmpALHUPV = new BigDecimal(linea.get("ALHUPV").toString().replaceAll("(\\r|\\n)", ""));
  827. // tmpTotalCanones = tmpImportPorTipoIvaCanones.add(tmpALHN09.divide(tmpALHUPV, 4, BigDecimal.ROUND_HALF_UP).multiply(tmpALHN07));
  828. //tmpTotalCanones = tmpImportPorTipoIvaCanones.add(tmpALHN09.multiply(tmpALHN07));
  829. // Agregamos los canones imponibles, referentes al tipo de iva
  830. //hcanones.put(stiv, tmpTotalCanones.setScale(4, BigDecimal.ROUND_HALF_UP));
  831.  
  832. }
  833. }
  834.  
  835. BigDecimal ftbase = new BigDecimal(0);
  836. //BigDecimal ftcanon = new BigDecimal(0);
  837. BigDecimal ftiva = new BigDecimal(0);
  838. BigDecimal ftrecargo = new BigDecimal(0);
  839. BigDecimal fttotal = new BigDecimal(0);
  840.  
  841. // Calculamos los importes de cada una de las bases
  842. it_ivas = ivas.keySet().iterator();
  843.  
  844. while (it_ivas.hasNext()) {
  845.  
  846. iva = (HashMap) ivas.get(it_ivas.next());
  847.  
  848. if (!himportes.containsKey(iva.get("MAJELE").toString().trim()))
  849. continue;
  850.  
  851. BigDecimal fbase = new BigDecimal(0);
  852. //BigDecimal fcanon = new BigDecimal(0);
  853. BigDecimal fiva = new BigDecimal(0);
  854. BigDecimal frecargo = new BigDecimal(0);
  855. BigDecimal ftotal = new BigDecimal(0);
  856. BigDecimal majva1 = new BigDecimal(iva.get("MAJVA1").toString().trim().replaceAll("(\\r|\\n)", ""));
  857. BigDecimal majva2 = new BigDecimal(iva.get("MAJVA2").toString().trim().replaceAll("(\\r|\\n)", ""));
  858.  
  859. fbase = new BigDecimal(himportes.get(iva.get("MAJELE").toString().trim()).toString().replaceAll("(\\r|\\n)", ""));
  860. //fcanon = new BigDecimal(hcanones.get(iva.get("MAJELE").toString().trim()).toString().replaceAll("(\\r|\\n)", ""));
  861.  
  862. if (((String) factura.get("ALGTIV")).equals("S") && majva1.compareTo(new BigDecimal(0)) == 1) {
  863.  
  864. fiva = fbase/*.add(fcanon)*/.multiply(majva1.divide(tmp100, BigDecimal.ROUND_HALF_UP)).add(majva2).divide(tmp100, BigDecimal.ROUND_HALF_UP);
  865. }
  866.  
  867. fiva = fbase/*.add(fcanon)*/.multiply(majva1).divide(tmp100, BigDecimal.ROUND_HALF_UP);
  868.  
  869. if (factura.get("ALGTIV").equals("S")) {
  870.  
  871. frecargo = fbase/*.add(fcanon)*/.multiply(majva2.divide(tmp100, BigDecimal.ROUND_HALF_UP));
  872. }
  873.  
  874. ftotal = fbase/*.add(fcanon)*/.add(fiva).add(frecargo);
  875.  
  876. ftbase = ftbase.add(fbase);
  877. //ftcanon = ftcanon.add(fcanon);
  878. ftiva = ftiva.add(fiva);
  879.  
  880. if (factura.get("ALGTIV").equals("S")) {
  881. ftrecargo.add(frecargo);
  882. }
  883. fttotal = fttotal.add(ftotal);
  884. }
  885.  
  886. factura.put("ALTBAS", ftbase);
  887. //factura.put("ALTCAN", ftcanon);
  888. factura.put("ALTCAN", new BigDecimal(0));
  889. factura.put("ALTIVA", ftiva);
  890. factura.put("ALTREC", ftrecargo);
  891. factura.put("ALTTOT", fttotal);
  892.  
  893. facturas.put(factura.get("ALGNUD"), factura);
  894. }
  895.  
  896. bw.write("\t<SourceDocuments>\r\n");
  897. bw.write("\t\t<SalesInvoices>\r\n");
  898. bw.write("\t\t\t<NumberOfEntries>" + o_facturas.size() + "</NumberOfEntries>\r\n");
  899.  
  900. /********************************* CALCULO TOTALES DE FACTURA **********************************/
  901.  
  902. it2 = o_facturas.iterator();
  903.  
  904. BigDecimal totalFacturasCredito = new BigDecimal(0);
  905. BigDecimal totalFacturasDebito = new BigDecimal(0);
  906. BigDecimal porcentajeIVAPortes = new BigDecimal(0);
  907. BigDecimal importePortesSinIVA = new BigDecimal(0);
  908.  
  909. BigDecimal importeECOTASASinIVA = new BigDecimal(0);
  910. BigDecimal importeECOTASAMasIVA = new BigDecimal(0);
  911. BigDecimal importeECOTASASoloIVA = new BigDecimal(0);
  912.  
  913. BigDecimal importePortesConIVA = new BigDecimal(0);
  914. BigDecimal importePortesSoloIVA = new BigDecimal(0);
  915. BigDecimal facturaImporteNeto = new BigDecimal(0);
  916. BigDecimal facturaBase = new BigDecimal(0);
  917. //BigDecimal facturaCan = new BigDecimal(0);
  918. HashMap resultPortes = null;
  919.  
  920. while (it2.hasNext()) {
  921.  
  922. factura = (HashMap) facturas.get((String) it2.next());
  923.  
  924. facturaImporteNeto = new BigDecimal(0);
  925. facturaBase = new BigDecimal(factura.get("ALTBAS").toString().trim().replaceAll("(\\r|\\n)", ""));
  926. //facturaCan = new BigDecimal(factura.get("ALTCAN").toString().replaceAll("(\\r|\\n)", ""));
  927.  
  928. // tiene portes la factura?
  929. //resultPortes = getPortesFactura(consulta.get("company").toString(), consulta.get("empresa").toString(), factura.get("ALGSE2").toString(), factura.get("ALGNUD").toString());
  930. //resultPortes = getPortesFactura(factura.get("ALGA72").toString(),factura.get("ALGN74").toString(),factura.get("ALGPOR").toString());
  931.  
  932. //importePortesSinIVA = new BigDecimal(resultPortes.get("ALGPOR").toString());
  933. importePortesSinIVA = new BigDecimal(factura.get("ALGPOR").toString().trim());
  934.  
  935. facturaImporteNeto = facturaBase/*.add(facturaCan)*/.add(importePortesSinIVA);
  936.  
  937. if (factura.get("ALGEST").toString().equals("FRA")) {
  938.  
  939. totalFacturasDebito = totalFacturasDebito.add(facturaImporteNeto);
  940.  
  941. } else if (factura.get("ALGEST").toString().equals("ABO")) {
  942.  
  943. totalFacturasCredito = totalFacturasCredito.add(facturaImporteNeto);
  944. }
  945. }
  946.  
  947. bw.write("\t\t\t<TotalDebit>" + totalFacturasDebito.setScale(4, BigDecimal.ROUND_HALF_UP).abs() + "</TotalDebit>\r\n");
  948. bw.write("\t\t\t<TotalCredit>" + totalFacturasCredito.setScale(4, BigDecimal.ROUND_HALF_UP).abs()+ "</TotalCredit>\r\n");
  949.  
  950. /***********************************************************************************************************/
  951.  
  952. String tmpNumeroFactura = "";
  953. String tmpSerieFactura = "";
  954. it = o_facturas.iterator();
  955.  
  956. while (it.hasNext()) {
  957.  
  958. factura = (HashMap) facturas.get((String) it.next());
  959. BigDecimal fbas = new BigDecimal(factura.get("ALTBAS").toString().trim().replaceAll("(\\r|\\n)", ""));
  960. //BigDecimal fcan = new BigDecimal(factura.get("ALTCAN").toString().replaceAll("(\\r|\\n)", ""));
  961. BigDecimal fiva = new BigDecimal(factura.get("ALTIVA").toString().trim().replaceAll("(\\r|\\n)", ""));
  962. BigDecimal ftot = new BigDecimal(factura.get("ALTTOT").toString().trim().replaceAll("(\\r|\\n)", ""));
  963.  
  964. java.util.Date daux1 = new java.util.Date();
  965. java.util.Date daux2 = new java.util.Date();
  966. SimpleDateFormat sdf_datetime1 = new SimpleDateFormat("dd/MM/yyHH:mm:ss");
  967. SimpleDateFormat sdf_datetime3 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
  968.  
  969. daux1 = sdf_kaka.parse(((String) factura.get("ALGA21").toString()) + ((String) factura.get("ALGN21").toString()));
  970. String tmpFecha = factura.get("ALGFCR").toString().substring(8)+ "/"+ factura.get("ALGFCR").toString().substring(5, 7) + "/"
  971. + factura.get("ALGFCR").toString().substring(2, 4);
  972. daux2 = sdf_datetime1.parse(tmpFecha + ((String) factura.get("ALGHCR").toString()));
  973.  
  974. bw.write("\t\t\t<Invoice>\r\n");
  975.  
  976. tmpSerieFactura = factura.get("ALGSE2").toString().trim();
  977.  
  978. // bw.write("\t\t\t\t<InvoiceNo>" + getTmpSeriesFactura(factura).get(tmpSerieFactura) + " " + tmpSerieFactura + "/" + factura.get("ALGNUD") + "</InvoiceNo>\r\n");
  979. bw.write("\t\t\t\t<InvoiceNo>" + getTmpSeriesFactura(factura.get("ALGEST").toString()) + " " + tmpSerieFactura + "/" + factura.get("ALGNUD") + "</InvoiceNo>\r\n");
  980. bw.write("\t\t\t\t<DocumentStatus>\r\n");
  981. bw.write("\t\t\t\t\t<InvoiceStatus>N</InvoiceStatus>\r\n");
  982. bw.write("\t\t\t\t\t<InvoiceStatusDate>" + sdf_datetime3.format(daux2) + "</InvoiceStatusDate>\r\n");
  983. bw.write("\t\t\t\t\t<SourceID>1</SourceID>\r\n");
  984. bw.write("\t\t\t\t\t<SourceBilling>I</SourceBilling>\r\n");
  985. bw.write("\t\t\t\t</DocumentStatus>\r\n");
  986.  
  987. SQL = "SELECT ALSEMP, ALSTIA, ALSSE1, ALSSE2, ALSNUD, ALSTIR, ALSENV, ALSLIN, ALSHAS, ALSSIG FROM ALMSF WHERE "
  988. + "ALSEMP = '" + factura.get("ALGEMP") + "' AND ALSTIA = '" + factura.get("ALGTIA") + "' "
  989. + "AND ALSSE1 = '" + factura.get("ALGSE1") + "' AND ALSSE2 = '" + factura.get("ALGSE2") + "' "
  990. + "AND ALSNUD = '" + factura.get("ALGNUD") + "' AND ALSTIR = '" + factura.get("ALGTIR") + "' "
  991. + "AND ALSENV = '" + factura.get("ALGENV") + "' AND ALSLIN = '" + factura.get("ALGLIN") + "' ";
  992.  
  993. HashMap firma = getDataByField(SQL);
  994.  
  995. if (firma.size() > 0) {
  996.  
  997. bw.write("\t\t\t\t<Hash>" + firma.get("ALSSIG") + "</Hash>\r\n");
  998. } else {
  999. bw.write("\t\t\t\t<Hash>" + "0" + "</Hash>\r\n");
  1000. }
  1001.  
  1002. bw.write("\t\t\t\t<HashControl>1</HashControl>\r\n");
  1003. bw.write("\t\t\t\t<InvoiceDate>" + sdf_date.format(daux1) + "</InvoiceDate>\r\n");
  1004. bw.write("\t\t\t\t<InvoiceType>");
  1005.  
  1006. if (factura.get("ALGEST").equals("FRA")) {
  1007. bw.write("FT");
  1008. } else if (factura.get("ALGEST").equals("ABO")) {
  1009. bw.write("NC");
  1010. }
  1011.  
  1012. bw.write("</InvoiceType>\r\n");
  1013. bw.write("\t\t\t\t<SpecialRegimes>\r\n");
  1014. bw.write("\t\t\t\t\t<SelfBillingIndicator>0</SelfBillingIndicator>\r\n");
  1015. bw.write("\t\t\t\t\t<CashVATSchemeIndicator>0</CashVATSchemeIndicator>\r\n");
  1016. bw.write("\t\t\t\t\t<ThirdPartiesBillingIndicator>0</ThirdPartiesBillingIndicator>\r\n");
  1017. bw.write("\t\t\t\t</SpecialRegimes>\r\n");
  1018. bw.write("\t\t\t\t<SourceID>" + "1" + "</SourceID>\r\n");
  1019. bw.write("\t\t\t\t<SystemEntryDate>" + sdf_datetime3.format(daux2) + "</SystemEntryDate>\r\n");
  1020.  
  1021. if (clientes.containsKey((String) factura.get("ALGCLI"))) {
  1022.  
  1023. cliente = (HashMap) clientes.get((String) factura.get("ALGCLI"));
  1024.  
  1025. bw.write("\t\t\t\t<CustomerID>");
  1026. bw.write(tmpCuentaMayorCliente + StringUtilities.fillLeftSpaces(cliente.get("MACCUE").toString(),6));
  1027. bw.write("</CustomerID>\r\n");
  1028. } else {
  1029. errormessages.add("Cliente Código '" + factura.get("ALGCLI") + "' procedente del Pedido '" + factura.get("ALGNUD") + "' no localizado");
  1030. }
  1031.  
  1032. int l = 0;
  1033. String tmpTaxPointDate = "";
  1034.  
  1035. if (factura.containsKey("lineas")) {
  1036.  
  1037. lineas = (HashMap) factura.get("lineas");
  1038. o_lineas = (Vector) factura.get("o_lineas");
  1039. Iterator it_lineas = o_lineas.iterator();
  1040.  
  1041. BigDecimal unitPrice = new BigDecimal(0);
  1042. BigDecimal cantidad = new BigDecimal(0);
  1043. BigDecimal credit = new BigDecimal(0);
  1044. BigDecimal descuentos = new BigDecimal(0);
  1045. BigDecimal settlement = new BigDecimal(0);
  1046.  
  1047. while (it_lineas.hasNext()) {
  1048.  
  1049. linea = (HashMap) lineas.get((String) it_lineas.next());
  1050.  
  1051. tmpNumeroFactura = linea.get("ALHFRA").toString();
  1052. tmpSerieFactura = linea.get("ALHSF2").toString();
  1053.  
  1054. bw.write("\t\t\t\t<Line>\r\n");
  1055. bw.write("\t\t\t\t\t<LineNumber>" + (++l) + "</LineNumber>\r\n");
  1056. bw.write("\t\t\t\t\t<ProductCode>" + linea.get("ALHART") + "</ProductCode>\r\n");
  1057. bw.write("\t\t\t\t\t<ProductDescription>" + ((String) linea.get("ALHNOA")).replace('&',' ').trim() + "</ProductDescription>\r\n");
  1058.  
  1059. cantidad = new BigDecimal(linea.get("ALHN07").toString());
  1060. bw.write("\t\t\t\t\t<Quantity>" + cantidad.abs().setScale(2,BigDecimal.ROUND_HALF_UP) + "</Quantity>\r\n");
  1061. bw.write("\t\t\t\t\t<UnitOfMeasure>Unid</UnitOfMeasure>\r\n");
  1062.  
  1063. unitPrice = new BigDecimal(linea.get("ALHPRE").toString());
  1064. bw.write("\t\t\t\t\t<UnitPrice>" + unitPrice.abs().setScale(2,BigDecimal.ROUND_HALF_UP) + "</UnitPrice>\r\n");
  1065. bw.write("\t\t\t\t\t<TaxPointDate>");
  1066.  
  1067. try {
  1068. if ( ! linea.get("ALHFFR").toString().equals("") ) {
  1069. String dta = linea.get("ALHFFR").toString();
  1070. tmpTaxPointDate = dta.substring(0,4)+"-"+dta.substring(4,6)+"-"+dta.substring(6);
  1071. }
  1072. // java.util.Date f_extraccion = null;
  1073. // mysqldf = new SimpleDateFormat("yyyyMMdd");
  1074. // f_extraccion = mysqldf.parse((String) linea.get("ALHFPR").toString());
  1075. // tmpTaxPointDate = f_extraccion.toString();
  1076. } catch (Exception e) {
  1077. tmpTaxPointDate = (((String) linea.get("ALHFPR")));
  1078. }
  1079. bw.write(tmpTaxPointDate);
  1080. bw.write("</TaxPointDate>\r\n");
  1081.  
  1082. if (factura.get("ALGEST").equals("ABO")) {
  1083.  
  1084. bw.write("\t\t\t\t\t<References>");
  1085.  
  1086. bw.write("\n\t\t\t\t\t\t<Reference>");
  1087. bw.write(factura.get("ALGOP2").toString() + "/" + factura.get("ALGOPR").toString());
  1088. bw.write("</Reference>\r\n");
  1089.  
  1090. bw.write("\t\t\t\t\t\t<Reason>");
  1091. bw.write(" ");
  1092. bw.write("</Reason>\r\n");
  1093. bw.write("\t\t\t\t\t</References>\r\n");
  1094.  
  1095. }
  1096.  
  1097. bw.write("\t\t\t\t\t<Description>" + ((String) linea.get("ALHNOA")).replace('&',' ').trim() + "</Description>\r\n");
  1098.  
  1099. descuentos = new BigDecimal(linea.get("SUMA_DE_DESCUENTO_PIE_FACTURA_Y_PRONTO_PAGO").toString().replaceAll("(\\r|\\n)", ""));
  1100. settlement = new BigDecimal(linea.get("ALHTDC").toString()).abs().setScale(2);
  1101. //credit = new BigDecimal(linea.get("ALHIMP").toString().replaceAll("(\\r|\\n)", ""));
  1102. credit = new BigDecimal(linea.get("IMPORTE_LINEA_CON_DESCUENTOS_APLICADOS").toString().replaceAll("(\\r|\\n)", ""));
  1103.  
  1104. if (factura.get("ALGEST").equals("ABO")) {
  1105. bw.write("\t\t\t\t\t<DebitAmount>" + credit.abs() + "</DebitAmount>\r\n");
  1106. } else {
  1107. bw.write("\t\t\t\t\t<CreditAmount>" + credit.abs() + "</CreditAmount>\r\n");
  1108. }
  1109.  
  1110. bw.write("\t\t\t\t\t<Tax>\r\n");
  1111. bw.write("\t\t\t\t\t\t<TaxType>IVA</TaxType>\r\n");
  1112. bw.write("\t\t\t\t\t\t<TaxCountryRegion>PT</TaxCountryRegion>\r\n");
  1113.  
  1114. if (factura.get("ALGTIV").equals("0")) {
  1115. bw.write("\t\t\t\t\t\t<TaxCode>ISE</TaxCode>\r\n");
  1116. } else {
  1117. bw.write("\t\t\t\t\t\t<TaxCode>NOR</TaxCode>\r\n");
  1118. }
  1119.  
  1120. if (factura.get("ALGTIV").equals("0")) {
  1121. bw.write("\t\t\t\t\t\t<TaxPercentage>0.00</TaxPercentage>\r\n");
  1122. } else {
  1123. bw.write("\t\t\t\t\t\t<TaxPercentage>" + linea.get("ALHIVA") + "</TaxPercentage>\r\n");
  1124. }
  1125.  
  1126. bw.write("\t\t\t\t\t</Tax>\r\n");
  1127.  
  1128. // if (factura.get("ALGTIV").equals("0")) {
  1129. // bw.write("\t\t\t\t\t<TaxExemptionReason>" + getTmpSeriesFactura(factura).get(tmpSerieFactura) + " " + tmpSerieFactura + "/"
  1130. // + factura.get("ALGNUD") + "</TaxExemptionReason>\r\n");
  1131. // }
  1132. bw.write("\t\t\t\t\t<SettlementAmount>" + settlement.add(descuentos).setScale(2, BigDecimal.ROUND_HALF_UP).abs() +"</SettlementAmount>\r\n");
  1133.  
  1134. bw.write("\t\t\t\t</Line>\r\n");
  1135. }
  1136. }
  1137. // resultPortes = getPortesFactura(consulta.get("company").toString(), consulta.get("empresa").toString(),factura.get("ALGSE2").toString(), factura.get("ALGNUD").toString());
  1138.  
  1139. //importePortesSinIVA = new BigDecimal(resultPortes.get("ALGPOR").toString());
  1140. importePortesSinIVA = new BigDecimal(factura.get("ALGPOR").toString());
  1141. //porcentajeIVAPortes = new BigDecimal(resultPortes.get("ALGN74").toString());
  1142. porcentajeIVAPortes = new BigDecimal(factura.get("ALGN74").toString());
  1143.  
  1144. importePortesConIVA = getImporteMasIVA(importePortesSinIVA, porcentajeIVAPortes);
  1145. importePortesSoloIVA = getImporteIVA(importePortesSinIVA, porcentajeIVAPortes);
  1146.  
  1147. importeECOTASASinIVA = new BigDecimal(factura.get("ALGN55").toString()).abs();
  1148. importeECOTASAMasIVA = getImporteMasIVA(importeECOTASASinIVA, getPorcentajeIVAECOTASA(factura.get("ALGEMP").toString()));
  1149. importeECOTASASoloIVA = getImporteIVA(importeECOTASASinIVA, getPorcentajeIVAECOTASA(factura.get("ALGEMP").toString()));
  1150.  
  1151. if (importeECOTASASinIVA.compareTo(new BigDecimal(0)) == 1) {
  1152.  
  1153. bw.write("\t\t\t\t<Line>\r\n");
  1154. bw.write("\t\t\t\t\t<LineNumber>" + (++l) + "</LineNumber>\r\n");
  1155. bw.write("\t\t\t\t\t<ProductCode>ECOTASA</ProductCode>\r\n");
  1156. bw.write("\t\t\t\t\t<ProductDescription>ECOTASA</ProductDescription>\r\n");
  1157. bw.write("\t\t\t\t\t<Quantity>1</Quantity>\r\n");
  1158. bw.write("\t\t\t\t\t<UnitOfMeasure>Unid</UnitOfMeasure>\r\n");
  1159. bw.write("\t\t\t\t\t<UnitPrice>" + importeECOTASASinIVA + "</UnitPrice>\r\n");
  1160. bw.write("\t\t\t\t\t<TaxPointDate>"+tmpTaxPointDate+"</TaxPointDate>\r\n");
  1161.  
  1162. if (factura.get("ALGEST").equals("ABO")) {
  1163.  
  1164. bw.write("\t\t\t\t\t<References>");
  1165.  
  1166. bw.write("\n\t\t\t\t\t\t<Reference>");
  1167. bw.write(factura.get("ALGOP2").toString() + "/" + factura.get("ALGOPR").toString());
  1168. bw.write("</Reference>\r\n");
  1169.  
  1170. bw.write("\t\t\t\t\t\t<Reason>");
  1171. bw.write(" ");
  1172. bw.write("</Reason>\r\n");
  1173. bw.write("\t\t\t\t\t</References>\r\n");
  1174.  
  1175. }
  1176.  
  1177. bw.write("\t\t\t\t\t<Description>ECOTASA</Description>\r\n");
  1178. bw.write("\t\t\t\t\t<CreditAmount>" + importeECOTASASinIVA + "</CreditAmount>\r\n");
  1179. bw.write("\t\t\t\t\t<Tax>\r\n");
  1180. bw.write("\t\t\t\t\t\t<TaxType>" + "IVA" + "</TaxType>\r\n");
  1181. bw.write("\t\t\t\t\t\t<TaxCountryRegion>" + "PT" + "</TaxCountryRegion>\r\n");
  1182.  
  1183. if (factura.get("ALGTIV").equals("0")) {
  1184. bw.write("\t\t\t\t\t\t<TaxCode>ISE</TaxCode>\r\n");
  1185. } else {
  1186. bw.write("\t\t\t\t\t\t<TaxCode>NOR</TaxCode>\r\n");
  1187. }
  1188.  
  1189. if (factura.get("ALGTIV").equals("0")) {
  1190. bw.write("\t\t\t\t\t\t<TaxPercentage>0.00</TaxPercentage>\r\n");
  1191. } else {
  1192. bw.write("\t\t\t\t\t\t<TaxPercentage>" + getPorcentajeIVAECOTASA(factura.get("ALGEMP").toString()).toString() + "</TaxPercentage>\r\n");
  1193. }
  1194.  
  1195. bw.write("\t\t\t\t\t</Tax>\r\n");
  1196. bw.write("\t\t\t\t</Line>\r\n");
  1197. } else {
  1198. System.out.println("No hay ECOTASA para la factura " + tmpSerieFactura + " " + tmpNumeroFactura);
  1199. }
  1200.  
  1201. if (importePortesConIVA.compareTo(new BigDecimal(0)) == 1) {
  1202.  
  1203. bw.write("\t\t\t\t<Line>\r\n");
  1204. bw.write("\t\t\t\t\t<LineNumber>" + (++l) + "</LineNumber>\r\n");
  1205. bw.write("\t\t\t\t\t<ProductCode>PORTES</ProductCode>\r\n");
  1206. bw.write("\t\t\t\t\t<ProductDescription>PORTES</ProductDescription>\r\n");
  1207. bw.write("\t\t\t\t\t<Quantity>1</Quantity>\r\n");
  1208. bw.write("\t\t\t\t\t<UnitOfMeasure>Unid</UnitOfMeasure>\r\n");
  1209. bw.write("\t\t\t\t\t<UnitPrice>" + importePortesSinIVA + "</UnitPrice>\r\n");
  1210. bw.write("\t\t\t\t\t<TaxPointDate>"+tmpTaxPointDate+"</TaxPointDate>\r\n");
  1211.  
  1212. if (factura.get("ALGEST").equals("ABO")) {
  1213.  
  1214. bw.write("\t\t\t\t\t<References>");
  1215.  
  1216. bw.write("\n\t\t\t\t\t\t<Reference>");
  1217. bw.write(factura.get("ALGOP2").toString() + "/" + factura.get("ALGOPR").toString());
  1218. bw.write("</Reference>\r\n");
  1219.  
  1220. bw.write("\t\t\t\t\t\t<Reason>");
  1221. bw.write(" ");
  1222. bw.write("</Reason>\r\n");
  1223. bw.write("\t\t\t\t\t</References>\r\n");
  1224.  
  1225. }
  1226.  
  1227. bw.write("\t\t\t\t\t<Description>PORTES</Description>\r\n");
  1228. bw.write("\t\t\t\t\t<CreditAmount>" + importePortesSinIVA + "</CreditAmount>\r\n");
  1229. bw.write("\t\t\t\t\t<Tax>\r\n");
  1230. bw.write("\t\t\t\t\t\t<TaxType>" + "IVA" + "</TaxType>\r\n");
  1231. bw.write("\t\t\t\t\t\t<TaxCountryRegion>" + "PT" + "</TaxCountryRegion>\r\n");
  1232.  
  1233. if (factura.get("ALGTIV").equals("0")) {
  1234. bw.write("\t\t\t\t\t\t<TaxCode>ISE</TaxCode>\r\n");
  1235. } else {
  1236. bw.write("\t\t\t\t\t\t<TaxCode>NOR</TaxCode>\r\n");
  1237. }
  1238.  
  1239. if (factura.get("ALGTIV").equals("0")) {
  1240. bw.write("\t\t\t\t\t\t<TaxPercentage>0.00</TaxPercentage>\r\n");
  1241. // bw.write("\t\t\t\t\t\t<TaxExemptionReason> </TaxExemptionReason>\r\n");
  1242. } else {
  1243. bw.write("\t\t\t\t\t\t<TaxPercentage>" + porcentajeIVAPortes + "</TaxPercentage>\r\n");
  1244. }
  1245.  
  1246. bw.write("\t\t\t\t\t</Tax>\r\n");
  1247. bw.write("\t\t\t\t</Line>\r\n");
  1248. } else {
  1249. System.out.println("No hay portes para la factura " + tmpSerieFactura + " " + tmpNumeroFactura);
  1250. }
  1251.  
  1252. bw.write("\t\t\t\t<DocumentTotals>\r\n");
  1253.  
  1254. BigDecimal fact = new BigDecimal(0);
  1255. // fact = fbas.subtract(fdem).subtract(fppg).add(fcan).add(importePortesSinIVA);
  1256. fact = fbas/*.add(fcan)*/.add(importePortesSinIVA).add(importeECOTASASinIVA);
  1257.  
  1258. if ( !factura.get("ALGTIV").equals("0") ) { // se suman los importes solo si no es exento
  1259.  
  1260. ftot = ftot.add(importePortesConIVA).add(importeECOTASAMasIVA); // total mas portes + ecotasa
  1261. fiva = fiva.add(importePortesSoloIVA).add(importeECOTASASoloIVA); // total iva mas iva portes + ecotasa //
  1262. } else {
  1263. ftot = ftot.add(importePortesSinIVA).add(importeECOTASASinIVA); // total
  1264. }
  1265.  
  1266. bw.write("\t\t\t\t\t<TaxPayable>" + fiva.setScale(2, BigDecimal.ROUND_HALF_UP).abs() + "</TaxPayable>\r\n");
  1267. bw.write("\t\t\t\t\t<NetTotal>" + fact.setScale(2, BigDecimal.ROUND_HALF_UP).abs() + "</NetTotal>\r\n");
  1268. bw.write("\t\t\t\t\t<GrossTotal>" + ftot.setScale(2, BigDecimal.ROUND_HALF_UP).abs() + "</GrossTotal>\r\n");
  1269.  
  1270. bw.write("\t\t\t\t</DocumentTotals>\r\n");
  1271. bw.write("\t\t\t</Invoice>\r\n");
  1272. }
  1273. bw.write("\t\t</SalesInvoices>\r\n");
  1274. bw.write("\t</SourceDocuments>\r\n");
  1275. bw.write("</AuditFile>");
  1276. bw.close();
  1277. }
  1278.  
  1279. if (!errormessages.isEmpty()) {
  1280. for (int i = 0; i < errormessages.size(); i++) {
  1281. System.err.println(errormessages.get(i));
  1282. }
  1283. }
  1284. }
  1285.  
  1286. public void checkPoblacion(Vector errormessages, HashMap aHashEmpresaOrCompany, String aPrefijo) {
  1287. Pattern e_p = Pattern.compile("[0-9]{4}-[0-9]{3}");
  1288. Matcher e_m = e_p.matcher((String) aHashEmpresaOrCompany.get(aPrefijo+"POB"));
  1289.  
  1290. if (e_m.find() == true) {
  1291. try {
  1292.  
  1293. String e_zipcode = e_m.group();
  1294. String e_city = ((String) aHashEmpresaOrCompany.get(aPrefijo+"POB")).substring(e_zipcode.length() + 1);
  1295. aHashEmpresaOrCompany.put(aPrefijo+"POB", e_city);
  1296. aHashEmpresaOrCompany.put(aPrefijo+"ZIP", e_zipcode);
  1297.  
  1298. } catch (Exception e) {
  1299. errormessages.add("Datos de Empresa/Company ("+aPrefijo+"POB) mal formados, código Empresa/Company : " + aHashEmpresaOrCompany.get(aPrefijo+"CUE"));
  1300. }
  1301. } else {
  1302. errormessages.add("Empresa/Company sin código postal, código Empresa/Company : " + aHashEmpresaOrCompany.get(aPrefijo+"CUE"));
  1303. }
  1304. }
  1305.  
  1306. /**
  1307. * Este metodo recibida una query devuelve una hash CAMPO=VALOR. El
  1308. * resultado de la query no puede devolver mas de una row.
  1309. */
  1310. public static HashMap getDataByField(String aSQL) {
  1311. return getDataByField(aSQL, "");
  1312. }
  1313.  
  1314. public static HashMap getDataByField(String aSQL, String aCampoClave) {
  1315.  
  1316. HashMap tmpData = new HashMap();
  1317. HashMap tmpDetailData = null;
  1318. boolean tmpMultiples = !aCampoClave.equals("");
  1319. Vector tmpResultSQL = SQLLauncher.executeQuery(aSQL, Integer.MAX_VALUE,0, true);
  1320.  
  1321. if (tmpResultSQL != null && tmpResultSQL.size() > 1) {
  1322.  
  1323. Vector tmpMetadata = (Vector) tmpResultSQL.get(tmpResultSQL.size() - 1); // La metadata se coloca al final del Vector de resultados
  1324. Vector tmpLinea = null;
  1325. String tmpCurrentField = null;
  1326.  
  1327. for (int x = 0; x < tmpResultSQL.size() - 1; x++) { // Recorremos las lineas
  1328.  
  1329. tmpDetailData = new HashMap();
  1330.  
  1331. tmpLinea = (Vector) tmpResultSQL.get(x);
  1332.  
  1333. for (int y = 0; y < tmpMetadata.size(); y++) { // Recorremos los campos
  1334.  
  1335. tmpCurrentField = tmpMetadata.get(y).toString().trim(); // campo
  1336. if(tmpLinea.get(y)==null){
  1337.  
  1338. tmpDetailData.put(tmpCurrentField, ""); // valor
  1339. }else{
  1340. tmpDetailData.put(tmpCurrentField, tmpLinea.get(y).toString().replaceAll("(\\r|\\n)", "")); // valor
  1341. }
  1342. }
  1343.  
  1344. if (tmpMultiples) {
  1345. tmpData.put(tmpDetailData.get(aCampoClave).toString().trim(), tmpDetailData);
  1346. }
  1347. }
  1348. }
  1349. if (tmpMultiples) {
  1350. return tmpData;
  1351. }
  1352. return (tmpDetailData==null) ? new HashMap() : tmpDetailData;
  1353. }
  1354.  
  1355. public static void cargarDatos(String aSQL, HashMap aElems, Vector aO_elems) {
  1356. cargarDatos(aSQL, aElems, aO_elems, "HASKEY");
  1357. }
  1358.  
  1359. public static void cargarDatos(String aSQL, HashMap aElems, Vector aO_elems, String aKey) {
  1360.  
  1361. Vector tmpResultSQL = SQLLauncher.executeQuery(aSQL, Integer.MAX_VALUE, 0, true);
  1362.  
  1363. if (tmpResultSQL != null && tmpResultSQL.size() > 1) {
  1364.  
  1365. HashMap tmpAux = null;
  1366. Vector tmpMetadata = (Vector) tmpResultSQL.get(tmpResultSQL.size() - 1); // La metadata se coloca al final del Vector de resultados
  1367. Vector tmpLinea = null;
  1368. String tmpCurrentField = null;
  1369.  
  1370. for (int x = 0; x < tmpResultSQL.size() - 1; x++) { // Recorremos las lineas
  1371.  
  1372. tmpAux = new HashMap();
  1373. tmpLinea = (Vector) tmpResultSQL.get(x);
  1374.  
  1375. for (int y = 0; y < tmpMetadata.size(); y++) { // Recorremos los campos
  1376.  
  1377. tmpCurrentField = tmpMetadata.get(y).toString(); // campo
  1378. if(tmpLinea.get(y)==null){
  1379.  
  1380. tmpAux.put(tmpCurrentField, ""); // valor
  1381. }else{
  1382. tmpAux.put(tmpCurrentField, tmpLinea.get(y).toString().replaceAll("(\\r|\\n)", "")); // valor
  1383. }
  1384.  
  1385. }
  1386. aO_elems.add(tmpAux.get(aKey).toString().trim());
  1387. aElems.put(tmpAux.get(aKey).toString().trim(), tmpAux);
  1388. }
  1389. }
  1390. }
  1391.  
  1392. public HashMap getClaveValorFromVector(Vector aValores, Vector aClaves){
  1393. HashMap tmpHash = new HashMap();
  1394. for (int p=0; p<aClaves.size(); p++){
  1395. tmpHash.put(aClaves.get(p), aValores.get(p));
  1396. }
  1397. return tmpHash;
  1398. }
  1399.  
  1400. public HashMap cargarLineasAFacturas(String aSQL, HashMap aFacturas, String aVarLineas, String aHashKey, String aSubKey) throws SQLException {
  1401.  
  1402. HashMap tmpLineas = new HashMap();
  1403. HashMap tmpPares = null;
  1404. Vector tmpMeta = null;
  1405. String aCurrentSubKey = null;
  1406. Vector tmpVectorSubkeys = null;
  1407. Vector tmpResultSQL = com.ak.rudder.core.control.SQLLauncher.executeQuery(aSQL, Integer.MAX_VALUE, 0, true);
  1408.  
  1409. if ( tmpResultSQL != null && tmpResultSQL.size() > 1 ) {
  1410.  
  1411. tmpMeta = (Vector)tmpResultSQL.get(tmpResultSQL.size()-1);
  1412.  
  1413. for (int x=0; x<tmpResultSQL.size()-1; x++) {
  1414.  
  1415. tmpPares = getClaveValorFromVector((Vector)tmpResultSQL.get(x), tmpMeta); // cogemos los valores de la linea que devuelve la query
  1416.  
  1417. HashMap aFactura = (HashMap) aFacturas.get(tmpPares.get(aHashKey)); // cogemos factura correspondiente a la linea que tenemos en tmpPares
  1418.  
  1419. aCurrentSubKey = tmpPares.get(aSubKey).toString(); // guardamos la subkey de la linea actual ejemplo [0,1]
  1420.  
  1421. tmpLineas = new HashMap();
  1422. tmpLineas.put(aCurrentSubKey, tmpPares); // montamos hash con la nueva linea y su subkey correspondiente
  1423.  
  1424. if( aFactura.containsKey(aVarLineas)){ // si la factura ya tiene el atributo lineas
  1425.  
  1426. ((HashMap)aFactura.get(aVarLineas)).put(aCurrentSubKey, (HashMap)tmpLineas.get(aCurrentSubKey));
  1427. ((Vector)aFactura.get("o_"+aVarLineas)).add(aCurrentSubKey);
  1428.  
  1429. }else{
  1430.  
  1431. tmpVectorSubkeys = new Vector();
  1432. tmpVectorSubkeys.add(aCurrentSubKey);
  1433.  
  1434. aFactura.put(aVarLineas, tmpLineas);
  1435. aFactura.put("o_"+aVarLineas, tmpVectorSubkeys);
  1436. }
  1437.  
  1438. aFacturas.put(tmpPares.get(aHashKey), aFactura);
  1439. }
  1440. }
  1441.  
  1442. return aFacturas;
  1443. }
  1444.  
  1445. // public HashMap web_addSubElems(String SQL, HashMap elems, String where,
  1446. // String has_key, String sub_key) throws SQLException {
  1447. //
  1448. // boolean hasnext = true;
  1449. // // cogemos las lineas de las faturas del periodo de tiempo indicado arriba
  1450. // ResultSet res = (ResultSet) web_dbQuery(SQL);
  1451. //
  1452. // while (hasnext) {
  1453. // // recorremos el resultado de las lineas
  1454. // HashMap sub_unit = web_dbFetchHash(res);
  1455. //
  1456. // if (sub_unit.size() > 0) {
  1457. //
  1458. // HashMap elem = (HashMap) elems.get( (String) sub_unit.get(has_key) ); // pillamos la factura
  1459. //
  1460. // HashMap sub_hash = new HashMap();
  1461. // Vector sub_vector = new Vector();
  1462. //
  1463. // // Recogemos, si tiene algún valor ya ele lemento
  1464. // if (elem.containsKey(where)) { // si la factura ya tenia el atributo lineas lo cogemos para añadir otra linea
  1465. // sub_hash = (HashMap) elem.get(where); // cogemos lineas
  1466. // sub_vector = (Vector) elem.get("o_" + where); // cogemos o_lineas
  1467. // }
  1468. //
  1469. // // Añadimos dateinfo_unit, ¿¿destrozando contenidos??
  1470. // sub_hash.put( (String) sub_unit.get(sub_key), sub_unit); // en las lineas añadimos la siguiente linea que hemos cogido
  1471. // 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]
  1472. //
  1473. // // Añadimos dateinfo sobre elem
  1474. // elem.put(where, sub_hash); // ponemos en la factura la linea
  1475. // elem.put("o_" + where, sub_vector); // ponemos en la factura el subkey de la key
  1476. //
  1477. // // Reaplilamos eñ HAsh
  1478. // elems.put((String) sub_unit.get(has_key), elem);
  1479. //
  1480. // } else {
  1481. // hasnext = false;
  1482. // }
  1483. // }
  1484. //
  1485. // return elems;
  1486. // }
  1487.  
  1488. // public static HashMap web_dbFetchHash(ResultSet res) throws SQLException {
  1489. //
  1490. // HashMap elem = new HashMap();
  1491. // ResultSetMetaData meta = res.getMetaData();
  1492. //
  1493. // // Get result set meta data
  1494. // int cols = meta.getColumnCount();
  1495. //
  1496. // if (res.next()) {
  1497. // // Get the column names; column indices start from 1
  1498. // for (int n = 1; n < cols + 1; n++) {
  1499. // // elem.put((String) meta.getColumnName(n), ((String)
  1500. // // res.getString(n)).trim() );
  1501. // if (res.getObject(n) != null)
  1502. // elem.put((String) meta.getColumnName(n),
  1503. // ((Object) res.getObject(n)));
  1504. // else
  1505. // elem.put((String) meta.getColumnName(n), "");
  1506. // }
  1507. //
  1508. // }
  1509. //
  1510. // return (elem);
  1511. // }
  1512.  
  1513. // public static ResultSet web_dbQuery(String query) throws SQLException {
  1514. //
  1515. // getConnection();
  1516. //
  1517. // Statement stmt = theConection.createStatement(
  1518. // ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
  1519. //
  1520. // ResultSet res = stmt.executeQuery(query);
  1521. //
  1522. // return res;
  1523. //
  1524. // }
  1525.  
  1526. public static boolean insertarHash(HashMap aElem) {
  1527.  
  1528. String SQL = "";
  1529. Vector keys = new Vector();
  1530. Vector values = new Vector();
  1531. Iterator it = ((Set) aElem.entrySet()).iterator();
  1532.  
  1533. while (it.hasNext()) {
  1534.  
  1535. Map.Entry e = (Map.Entry) it.next();
  1536. keys.add((String) e.getKey());
  1537. values.add((String) e.getValue().toString());
  1538. }
  1539.  
  1540. SQL = "INSERT INTO ALMSF (" + web_strImplode((String[]) keys.toArray(new String[keys.size()]), ", ")+ ") VALUES ('"
  1541. + web_strImplode((String[]) values.toArray(new String[values.size()]),"', '") + "')";
  1542.  
  1543. SQLLauncher.executeUpdate(SQL);
  1544. return true;
  1545. }
  1546.  
  1547. public static String web_strImplode(String[] ary, String delim) {
  1548.  
  1549. String str = "";
  1550. for (int i = 0; i < ary.length; i++) {
  1551. if (i != 0) {
  1552. str += delim;
  1553. }
  1554. str += ary[i];
  1555. }
  1556. return str;
  1557. }
  1558.  
  1559. // private static void getConnection() throws SQLException {
  1560. // if (theConection == null) {
  1561. //
  1562. // DriverManager.registerDriver(new com.ibm.as400.access.AS400JDBCDriver());
  1563. //
  1564. // String tmpUrl = "jdbc:as400://10.10.10.131/SAKFILGI/";
  1565. // String tmpUser = "SAKFILGI";
  1566. // String tmpPass = "AKSAKFILGI";
  1567. //
  1568. // theConection = DriverManager.getConnection(tmpUrl, tmpUser, tmpPass);
  1569. // }
  1570. // }
  1571.  
  1572. public static boolean actualizarHash(HashMap aElem, String aWhere) throws SQLException {
  1573.  
  1574. String tmpSQL = null;
  1575. Vector pairs = new Vector();
  1576. Iterator it = ((Set) aElem.entrySet()).iterator();
  1577.  
  1578. while (it.hasNext()) {
  1579. Map.Entry e = (Map.Entry) it.next();
  1580. pairs.add(" " + (String) e.getKey() + "='" + web_strAddslashes((String) e.getValue().toString()) + "' ");
  1581. }
  1582.  
  1583. tmpSQL = "UPDATE ALMSF SET " + web_strImplode((String[]) pairs.toArray(new String[pairs.size()]), ",") + " WHERE " + aWhere;
  1584. SQLLauncher.executeUpdate(tmpSQL);
  1585. return true;
  1586. }
  1587.  
  1588. public static String web_strAddslashes(String s) {
  1589. String f = null;
  1590. String r = null;
  1591. int n;
  1592.  
  1593. if (s == null)
  1594. return s;
  1595.  
  1596. f = "\\";
  1597. r = "NONE";
  1598. n = s.indexOf(f);
  1599. while (n != -1) {
  1600. s = s.substring(0, n) + r + s.substring(n + f.length());
  1601. n += r.length();
  1602. n = s.indexOf(f, n);
  1603. }
  1604.  
  1605. f = "\'";
  1606. r = "\'\'";
  1607. n = s.indexOf(f);
  1608. while (n != -1) {
  1609. s = s.substring(0, n) + r + s.substring(n + f.length());
  1610. n += r.length();
  1611. n = s.indexOf(f, n);
  1612. }
  1613.  
  1614. f = "\"";
  1615. r = "\\\"";
  1616. n = s.indexOf(f);
  1617. while (n != -1) {
  1618. s = s.substring(0, n) + r + s.substring(n + f.length());
  1619. n += r.length();
  1620. n = s.indexOf(f, n);
  1621. }
  1622.  
  1623. f = "\0";
  1624. r = "\\\0";
  1625. n = s.indexOf(f);
  1626. while (n != -1) {
  1627. s = s.substring(0, n) + r + s.substring(n + f.length());
  1628. n += r.length();
  1629. n = s.indexOf(f, n);
  1630. }
  1631. return s;
  1632. }
  1633.  
  1634. public static HashMap getPortesFactura(String aIvaPortesTipo, String aIvaPortes, String aValuePortes) {
  1635. /*
  1636. HashMap tmpResult = new HashMap();
  1637.  
  1638. String SQLPortes = "SELECT ALGA72, ALGN74, ALGPOR FROM ALMGF WHERE ALGEMP='" + aCompany + "' " + "AND ALGTEM='" + anEmpresa + "' "
  1639. + "AND ALGTIA='V' AND ALGTIR='F' AND ALGSE2='" + aSerieFactura + "' " + "AND ALGNUD='" + aNumeroFactura + "' ";
  1640.  
  1641. Vector tmpResultSQL = SQLLauncher.executeQuery(SQLPortes,Integer.MAX_VALUE, 0, false);
  1642.  
  1643. tmpResult.put("ALGA72",((Vector) tmpResultSQL.get(0)).get(0).toString()); // iva portes tipo
  1644. tmpResult.put("ALGN74",((Vector) tmpResultSQL.get(0)).get(1).toString()); // iva portes
  1645. tmpResult.put("ALGPOR",((Vector) tmpResultSQL.get(0)).get(2).toString()); // valor de los portes
  1646. */
  1647. HashMap tmpResult = new HashMap();
  1648. tmpResult.put("ALGA72", aIvaPortesTipo);
  1649. tmpResult.put("ALGN74", aIvaPortes);
  1650. tmpResult.put("ALGPOR", aValuePortes);
  1651. return tmpResult;
  1652. }
  1653.  
  1654. public static BigDecimal getImporteMasIVA(BigDecimal aImporte,BigDecimal aPorcentaje) {
  1655. return aImporte.multiply(aPorcentaje).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP).add(aImporte);
  1656. }
  1657.  
  1658. public static BigDecimal getImporteIVA(BigDecimal aImporte,BigDecimal aPorcentaje) {
  1659. return aImporte.multiply(aPorcentaje).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
  1660. }
  1661.  
  1662. public static void cargarArticuloECO(HashMap consulta,HashMap articulos, Vector o_articulos, HashMap facturas, Vector o_facturas) {
  1663. HashMap factura;
  1664. boolean tmpEcotasaAdded = false;
  1665. BigDecimal importePortes = new BigDecimal(0);
  1666. HashMap articuloEcotasa = new HashMap();
  1667. articuloEcotasa.put("MAANOM", "ECOTASA");
  1668. articuloEcotasa.put("MAACUE", "ECOTASA");
  1669. articuloEcotasa.put("MAAFAM", "ECOTASA");
  1670. articuloEcotasa.put("AUACO1", "");
  1671.  
  1672. Iterator it2 = o_facturas.iterator();
  1673.  
  1674. while (it2.hasNext() && !tmpEcotasaAdded) {
  1675. factura = (HashMap) facturas.get((String) it2.next());
  1676. System.out.println("Buscando ECOTASA para la factura: " + factura.get("ALGSE2").toString() +" "+ factura.get("ALGNUD").toString() + " ALGPOR = " + factura.get("ALGPOR").toString());
  1677. importePortes = new BigDecimal(factura.get("ALGN55").toString()).abs();
  1678. if (importePortes.compareTo(new BigDecimal(0)) == 1) {
  1679. o_articulos.add("ECOTASA");
  1680. articulos.put("ECOTASA", articuloEcotasa);
  1681. tmpEcotasaAdded = true;
  1682. }
  1683. }
  1684. }
  1685.  
  1686. public static void cargarArticuloPortes(HashMap consulta,HashMap articulos, Vector o_articulos, HashMap facturas, Vector o_facturas) {
  1687. HashMap factura;
  1688. boolean tmpPortesAdded = false;
  1689. BigDecimal importePortes = new BigDecimal(0);
  1690. HashMap articuloPortes = new HashMap();
  1691. articuloPortes.put("MAANOM", "PORTES");
  1692. articuloPortes.put("MAACUE", "PORTES");
  1693. articuloPortes.put("MAAFAM", "PORTES");
  1694. articuloPortes.put("AUACO1", "");
  1695.  
  1696. Iterator it2 = o_facturas.iterator();
  1697.  
  1698. while (it2.hasNext() && !tmpPortesAdded) {
  1699. factura = (HashMap) facturas.get((String) it2.next());
  1700. System.out.println("Buscando portes para la factura: " + factura.get("ALGSE2").toString() +" "+ factura.get("ALGNUD").toString() + " ALGPOR = " + factura.get("ALGPOR").toString());
  1701. importePortes = new BigDecimal(factura.get("ALGPOR").toString()).abs();
  1702. if (importePortes.compareTo(new BigDecimal(0)) == 1) {
  1703. o_articulos.add("PORTES");
  1704. articulos.put("PORTES", articuloPortes);
  1705. tmpPortesAdded = true;
  1706. }
  1707. }
  1708. }
  1709.  
  1710. public static String getCuentaMayorCliente(String aCompany) {
  1711.  
  1712. Vector tmpCuentaMayor = com.ak.rudder.core.control.SQLLauncher.executeQuery("SELECT MAECCL FROM MAEEF WHERE MAECUE='"+aCompany+"'");
  1713. if (tmpCuentaMayor != null) {
  1714. Vector aCM = (Vector)tmpCuentaMayor.elementAt(0);
  1715. return aCM.get(0).toString().trim();
  1716. }
  1717. return "";
  1718. }
  1719.  
  1720. public static Map getPaises(String aCompany) {
  1721. if(tmpPaises == null){
  1722. tmpPaises = new HashMap();
  1723. String tmpSQL = "SELECT MAJELE, MAJNOM FROM MAEJF WHERE MAJEMP='"+aCompany+"' AND MAJCUE='PA'";
  1724. Vector paises = com.ak.rudder.core.control.SQLLauncher.executeQuery(tmpSQL, Integer.MAX_VALUE, 0, false);
  1725. if (paises != null ) {
  1726. Iterator it = paises.iterator();
  1727. Vector row = null;
  1728. while (it.hasNext()){
  1729. row = (Vector) it.next();
  1730. tmpPaises.put(row.get(0).toString().trim(),row.get(1).toString().trim());
  1731. }
  1732. }
  1733. }
  1734. return tmpPaises;
  1735. }
  1736.  
  1737. public static String getTmpSeriesFactura(String tmpEstadoFactura) {
  1738. if(tmpEstadoFactura.trim().equals("FRA")){
  1739. return "NF"; // factura
  1740. }else{
  1741. return "NC"; // note credit - abono
  1742. }
  1743. }
  1744.  
  1745. public static BigDecimal getPorcentajeIVAECOTASA (String aCompany) {
  1746.  
  1747. if ( ivaEcotasa == null) {
  1748.  
  1749. String tmpSQL = "SELECT MAATIV FROM MAEAF WHERE MAAEMP='"+aCompany+"' AND MAACUE = 'ECOTASA'";
  1750. String tmpSQLTI = "";
  1751. Vector aEcotasa = com.ak.rudder.core.control.SQLLauncher.executeQuery(tmpSQL);
  1752. Vector tmpIVAEcotasa = null;
  1753. String tipoIVA = "";
  1754. String valorECOTASA = "";
  1755. if (aEcotasa != null ) {
  1756. Vector rowEcotasa = (Vector)aEcotasa.elementAt(0);
  1757. tipoIVA = rowEcotasa.get(0).toString();
  1758. if ( !tipoIVA.equals("") ){
  1759. tmpSQLTI = "SELECT MAJVA1 FROM MAEJF WHERE MAJEMP='"+aCompany+"' AND MAJCUE='IV' AND MAJELE='"+tipoIVA+"'";
  1760. tmpIVAEcotasa = com.ak.rudder.core.control.SQLLauncher.executeQuery(tmpSQLTI);
  1761. if (tmpIVAEcotasa != null){
  1762. valorECOTASA = ((Vector)tmpIVAEcotasa.get(0)).get(0).toString().replaceAll("(\\r|\\n)", "");
  1763. ivaEcotasa = new BigDecimal(valorECOTASA);
  1764. }
  1765. }
  1766. }
  1767. }
  1768. return ivaEcotasa;
  1769. }
  1770. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement