1 19 package org.openbravo.erpCommon.ad_forms; 20 21 import org.openbravo.erpCommon.utility.SequenceIdData; 22 import org.openbravo.erpCommon.utility.Utility; 23 import org.openbravo.base.secureApp.HttpSecureAppServlet; 24 import org.openbravo.base.secureApp.VariablesSecureApp; 25 import org.openbravo.xmlEngine.XmlDocument; 26 import java.io.*; 27 import javax.servlet.*; 28 import javax.servlet.http.*; 29 30 31 import java.sql.Connection ; 33 34 35 public class MatchingPOReceiptInvoice extends HttpSecureAppServlet { 36 37 38 public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException { 39 VariablesSecureApp vars = new VariablesSecureApp(request); 40 41 if (!Utility.hasFormAccess(this, vars, "", "org.openbravo.erpCommon.ad_forms.MatchingPOReceiptInvoice")) { 42 bdError(response, "AccessTableNoView", vars.getLanguage()); 43 return; 44 } 45 46 if (vars.commandIn("DEFAULT")) { 47 vars.getRequestGlobalVariable("inpsourcedoc", "MatchingPO|sourcedoc"); 48 vars.getRequestGlobalVariable("inpfinaldoc", "MatchingPO|finaldoc"); 49 vars.getRequestGlobalVariable("inpsearchmode", "MatchingPO|searchmode"); 50 vars.getRequestGlobalVariable("inpcBpartnerId", "MatchingPO|cBpartnerId"); 51 vars.getRequestGlobalVariable("inpDesde", "MatchingPO|desde"); 52 vars.getRequestGlobalVariable("inpHasta", "MatchingPO|hasta"); 53 vars.getRequestGlobalVariable("inpmProductId", "MatchingPO|mProductId"); 54 printPageFS(response, vars); 55 } else if (vars.commandIn("RELOAD")) { 56 vars.getGlobalVariable("inpsourcedoc", "MatchingPO|sourcedoc", ""); 57 vars.getGlobalVariable("inpfinaldoc", "MatchingPO|finaldoc", ""); 58 vars.getGlobalVariable("inpsearchmode", "MatchingPO|searchmode", ""); 59 vars.getGlobalVariable("inpcBpartnerId", "MatchingPO|cBpartnerId", ""); 60 vars.getGlobalVariable("inpDesde", "MatchingPO|desde", ""); 61 vars.getGlobalVariable("inpHasta", "MatchingPO|hasta", ""); 62 vars.getGlobalVariable("inpmProductId", "MatchingPO|mProductId", ""); 63 printPageFS(response, vars); 64 } else if (vars.commandIn("FRAME1")) { 65 String strSourceDoc = vars.getGlobalVariable("inpsourcedoc", "MatchingPO|sourcedoc", ""); 66 String strFinalDoc = vars.getGlobalVariable("inpfinaldoc", "MatchingPO|finaldoc", ""); 67 String strSearchMode = vars.getGlobalVariable("inpsearchmode", "MatchingPO|searchmode", ""); 68 String strBpartner = vars.getGlobalVariable("inpcBpartnerId", "MatchingPO|cBpartnerId", ""); 69 String strDesde = vars.getGlobalVariable("inpDesde", "MatchingPO|desde", ""); 70 String strHasta = vars.getGlobalVariable("inpHasta", "MatchingPO|hasta", ""); 71 String strProduct = vars.getGlobalVariable("inpmProductId", "MatchingPO|mProductId", ""); 72 printPageF1(response, vars, strSourceDoc, strFinalDoc, strSearchMode, strBpartner, strDesde, strHasta, strProduct); 73 } else if (vars.commandIn("FIND")) { 74 String strSourceDoc = vars.getRequestGlobalVariable("inpsourcedoc", "MatchingPO|sourcedoc"); 75 String strFinalDoc = vars.getRequestGlobalVariable("inpfinaldoc", "MatchingPO|finaldoc"); 76 String strSearchMode = vars.getRequestGlobalVariable("inpsearchmode", "MatchingPO|searchmode"); 77 String strBpartner = vars.getRequestGlobalVariable("inpcBpartnerId", "MatchingPO|cBpartnerId"); 78 String strDesde = vars.getRequestGlobalVariable("inpDesde", "MatchingPO|desde"); 79 String strHasta = vars.getRequestGlobalVariable("inpHasta", "MatchingPO|hasta"); 80 String strProduct = vars.getRequestGlobalVariable("inpmProductId", "MatchingPO|mProductId"); 81 printPageF2(response, vars, strSourceDoc, strFinalDoc, strSearchMode, strBpartner, strDesde, strHasta, strProduct); 82 } else if (vars.commandIn("FRAME2")) { 83 String strSourceDoc = vars.getGlobalVariable("inpsourcedoc", "MatchingPO|sourcedoc", ""); 84 String strFinalDoc = vars.getGlobalVariable("inpfinaldoc", "MatchingPO|finaldoc", ""); 85 String strSearchMode = vars.getGlobalVariable("inpsearchmode", "MatchingPO|searchmode", ""); 86 String strBpartner = vars.getGlobalVariable("inpcBpartnerId", "MatchingPO|cBpartnerId", ""); 87 String strDesde = vars.getGlobalVariable("inpDesde", "MatchingPO|desde", ""); 88 String strHasta = vars.getGlobalVariable("inpHasta", "MatchingPO|hasta", ""); 89 String strProduct = vars.getGlobalVariable("inpmProductId", "MatchingPO|mProductId", ""); 90 printPageF2(response, vars, strSourceDoc, strFinalDoc, strSearchMode, strBpartner, strDesde, strHasta, strProduct); 91 } else if (vars.commandIn("FRAME3")) { 92 printPageF3(response, vars); 93 } else if (vars.commandIn("FRAME4", "FIND_SECONDARY")) { 94 String strClave = vars.getStringParameter("inpClave"); 95 String strType = vars.getStringParameter("inpType"); 96 String strSameBpartner = vars.getStringParameter("inpSameBpartner", "0"); 97 String strSameProduct = vars.getStringParameter("inpSameProduct", "0"); 98 String strSameQty = vars.getStringParameter("inpSameQty", "0"); 99 String strSourceDoc = vars.getStringParameter("inpsourcedoc"); 100 String strSearchMode = vars.getStringParameter("inpsearchmode", "0"); 101 String strDesde = vars.getStringParameter("inpDesde"); 102 String strHasta = vars.getStringParameter("inpHasta"); 103 printPageF4(response, vars, strClave, strSourceDoc, strType, strSameBpartner, strSameProduct, strSameQty, strSearchMode, strDesde, strHasta); 104 } else if (vars.commandIn("FRAME5")) { 105 printPageF5(response, vars); 106 } else if (vars.commandIn("PROCESS")) { 107 String strMatched = vars.getRequiredStringParameter("inpMatchLine"); 108 String strProduct = vars.getStringParameter("inpmProductId"); 109 String strSearchMode = vars.getStringParameter("inpsearchmode"); 110 String strSourceDoc = vars.getStringParameter("inpsourcedoc"); 111 String strFinalDoc = vars.getStringParameter("inpfinaldoc"); 112 String strMatchTo = vars.getInStringParameter("inpClave"); 113 String strMatchQty = vars.getStringParameter("inpMatchQty"); 114 processMatch(vars, strMatched, strProduct, strSearchMode, strSourceDoc, strFinalDoc, strMatchTo, strMatchQty); 115 response.sendRedirect(strDireccion + request.getServletPath() + "?Command=RELOAD"); 116 } else pageError(response); 117 } 118 119 private void printPageFS(HttpServletResponse response, VariablesSecureApp vars) throws IOException, ServletException{ 120 XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_FS").createXmlDocument(); 121 response.setContentType("text/html; charset=UTF-8"); 122 PrintWriter out = response.getWriter(); 123 out.println(xmlDocument.print()); 124 out.close(); 125 } 126 127 void printPageF1(HttpServletResponse response, VariablesSecureApp vars, String strSourceDoc, String strFinalDoc, String strSearchMode, String strBpartner, String strDesde, String strHasta, String strProduct) throws IOException, ServletException { 128 XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_F1").createXmlDocument(); 129 130 xmlDocument.setParameter("calendar", vars.getLanguage().substring(0,2)); 131 xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";"); 132 xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n"); 133 134 xmlDocument.setParameter("sourceDoc", strSourceDoc); 135 xmlDocument.setParameter("finalDoc", strFinalDoc); 136 xmlDocument.setParameter("searchMode", strSearchMode); 137 xmlDocument.setParameter("bpartner", strBpartner); 138 xmlDocument.setParameter("desde", strDesde); 139 xmlDocument.setParameter("hasta", strHasta); 140 xmlDocument.setParameter("product", strProduct); 141 xmlDocument.setParameter("productName", MatchingPOReceiptInvoiceData.product(this, strProduct)); 142 xmlDocument.setParameter("bpartnerName", MatchingPOReceiptInvoiceData.bpartner(this, strBpartner)); 143 144 response.setContentType("text/html; charset=UTF-8"); 145 PrintWriter out = response.getWriter(); 146 out.println(xmlDocument.print()); 147 out.close(); 148 } 149 150 void printPageF2(HttpServletResponse response, VariablesSecureApp vars, String strSourceDoc, String strFinalDoc, String strSearchMode, String strBpartner, String strDesde, String strHasta, String strProduct) throws IOException, ServletException { 151 XmlDocument xmlDocument = null; 152 if (strSourceDoc.equals("") && strSearchMode.equals("") && strBpartner.equals("") && strDesde.equals("") && strHasta.equals("") && strProduct.equals("")) { 153 String [] discard={"sectionDetail"}; 154 xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_F2", discard).createXmlDocument(); 155 xmlDocument.setData("structure1", MatchingPOReceiptInvoiceData.set()); 156 } else { 157 xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_F2").createXmlDocument(); 158 if (strSourceDoc.equals("1")) { xmlDocument.setData("structure1", MatchingPOReceiptInvoiceData.selectFactura(this, strProduct, strBpartner, "", strDesde, strHasta, Utility.getContext(this, vars, "#User_Org", "MatchingPO"), Utility.getContext(this, vars, "#User_Client", "MatchingPO"), "", (strSearchMode.equals("1")?"0":"lin.QtyInvoiced"))); 160 } else if (strSourceDoc.equals("2")) { xmlDocument.setData("structure1", MatchingPOReceiptInvoiceData.selectAlbaran(this, (strFinalDoc.equals("3")?"M_MatchPO":"M_MatchInv"), strProduct, strBpartner, "", strDesde, strHasta, Utility.getContext(this, vars, "#User_Org", "MatchingPO"), Utility.getContext(this, vars, "#User_Client", "MatchingPO"), "", (strSearchMode.equals("1")?"0":"lin.MovementQty"))); 162 } else if (strSourceDoc.equals("3")) { xmlDocument.setData("structure1", MatchingPOReceiptInvoiceData.select(this, strProduct, strBpartner, "", strDesde, strHasta, Utility.getContext(this, vars, "#User_Org", "MatchingPO"), Utility.getContext(this, vars, "#User_Client", "MatchingPO"), "", (strSearchMode.equals("1")?"0":"lin.QtyOrdered"))); 164 } 165 } 166 167 xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";"); 168 xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n"); 169 response.setContentType("text/html; charset=UTF-8"); 170 PrintWriter out = response.getWriter(); 171 out.println(xmlDocument.print()); 172 out.close(); 173 } 174 175 private void printPageF3(HttpServletResponse response, VariablesSecureApp vars) throws IOException, ServletException{ 176 XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_F3").createXmlDocument(); 177 xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";"); 178 xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n"); 179 response.setContentType("text/html; charset=UTF-8"); 180 PrintWriter out = response.getWriter(); 181 out.println(xmlDocument.print()); 182 out.close(); 183 } 184 185 private MatchingPOReceiptInvoiceData[] getLine(VariablesSecureApp vars, String strClave, String strSourceDoc) throws ServletException { 186 if (strSourceDoc.equals("1")) return MatchingPOReceiptInvoiceData.selectLineFactura(this, strClave); 187 else if (strSourceDoc.equals("2")) return MatchingPOReceiptInvoiceData.selectLineAlbaran(this, strClave); 188 else return MatchingPOReceiptInvoiceData.selectLine(this, strClave); 189 } 190 191 private void printPageF4(HttpServletResponse response, VariablesSecureApp vars, String strClave, String strSourceDoc, String strType, String strSameBpartner, String strSameProduct, String strSameQty, String strSearchMode, String strDesde, String strHasta) throws IOException, ServletException{ 192 XmlDocument xmlDocument = null; 193 String strProduct="", strBpartner="", strQty=""; 194 String [] discard = {"sectionDetail"}; 195 if (strClave.equals("") || strType.equals("")) { 196 xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_F4", discard).createXmlDocument(); 197 xmlDocument.setData("structure1", MatchingPOReceiptInvoiceData.set()); 198 } else { 199 MatchingPOReceiptInvoiceData[] data = null; 200 if (strType.equals("1")) { data = getLine(vars, strClave, strSourceDoc); 202 if (data==null || data.length==0) { 203 xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_F4", discard).createXmlDocument(); 204 xmlDocument.setData("structure1", MatchingPOReceiptInvoiceData.set()); 205 } else { 206 xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_F4").createXmlDocument(); 207 strProduct = data[0].mProductId; 208 strBpartner = data[0].cBpartnerId; 209 strQty = data[0].qty; 210 xmlDocument.setData("structure1", MatchingPOReceiptInvoiceData.selectFactura(this, (strSameProduct.equals("0")?"":data[0].mProductId), (strSameBpartner.equals("0")?"":data[0].cBpartnerId), (strSameQty.equals("0")?"":data[0].qty), strDesde, strHasta, Utility.getContext(this, vars, "#User_Org", "MatchingPO"), Utility.getContext(this, vars, "#User_Client", "MatchingPO"), "", (strSearchMode.equals("1")?"0":"lin.QtyInvoiced"))); 211 } 212 } else if (strType.equals("2")) { data = getLine(vars, strClave, strSourceDoc); 214 if (data==null || data.length==0) { 215 xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_F4", discard).createXmlDocument(); 216 xmlDocument.setData("structure1", MatchingPOReceiptInvoiceData.set()); 217 } else { 218 xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_F4").createXmlDocument(); 219 strProduct = data[0].mProductId; 220 strBpartner = data[0].cBpartnerId; 221 strQty = data[0].qty; 222 xmlDocument.setData("structure1", MatchingPOReceiptInvoiceData.selectAlbaran(this, (strSourceDoc.equals("3")?"M_MatchPO":"M_MatchInv"), (strSameProduct.equals("0")?"":data[0].mProductId), (strSameBpartner.equals("0")?"":data[0].cBpartnerId), (strSameQty.equals("0")?"":data[0].qty), strDesde, strHasta, Utility.getContext(this, vars, "#User_Org", "MatchingPO"), Utility.getContext(this, vars, "#User_Client", "MatchingPO"), "", (strSearchMode.equals("1")?"0":"lin.MovementQty"))); 223 } 224 } else if (strType.equals("3")) { data = getLine(vars, strClave, strSourceDoc); 226 if (data==null || data.length==0) { 227 xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_F4", discard).createXmlDocument(); 228 xmlDocument.setData("structure1", MatchingPOReceiptInvoiceData.set()); 229 } else { 230 xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_F4").createXmlDocument(); 231 strProduct = data[0].mProductId; 232 strBpartner = data[0].cBpartnerId; 233 strQty = data[0].qty; 234 xmlDocument.setData("structure1", MatchingPOReceiptInvoiceData.select(this, (strSameProduct.equals("0")?"":data[0].mProductId), (strSameBpartner.equals("0")?"":data[0].cBpartnerId), (strSameQty.equals("0")?"":data[0].qty), strDesde, strHasta, Utility.getContext(this, vars, "#User_Org", "MatchingPO"), Utility.getContext(this, vars, "#User_Client", "MatchingPO"), "", (strSearchMode.equals("1")?"0":"lin.QtyOrdered"))); 235 } 236 } 237 } 238 xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";"); 239 xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n"); 240 xmlDocument.setParameter("searchMode", strSearchMode); 241 xmlDocument.setParameter("product", strProduct); 242 xmlDocument.setParameter("sourceDoc", strSourceDoc); 243 xmlDocument.setParameter("finalDoc", strType); 244 xmlDocument.setParameter("matchLine", strClave); 245 response.setContentType("text/html; charset=UTF-8"); 246 PrintWriter out = response.getWriter(); 247 out.println(xmlDocument.print()); 248 out.close(); 249 } 250 251 void printPageF5(HttpServletResponse response, VariablesSecureApp vars) throws IOException, ServletException { 252 XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_forms/MatchingPO-Receipt-Invoice_F5").createXmlDocument(); 253 254 xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";"); 255 xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n"); 256 257 response.setContentType("text/html; charset=UTF-8"); 258 PrintWriter out = response.getWriter(); 259 out.println(xmlDocument.print()); 260 out.close(); 261 } 262 263 void processMatch(VariablesSecureApp vars, String strMatched, String strProduct, String strSearchMode, String strSourceDoc, String strFinalDoc, String strMatchTo, String strMatchQty) throws ServletException { 264 if (log4j.isDebugEnabled()) log4j.debug("processing match line: " + strMatched + " - Qty: " + strMatchQty); 265 MatchingPOReceiptInvoiceData[] data = null; 266 if (strFinalDoc.equals("1")) { data = MatchingPOReceiptInvoiceData.selectFactura(this, "", "", "", "", "", Utility.getContext(this, vars, "#User_Org", "MatchingPO"), Utility.getContext(this, vars, "#User_Client", "MatchingPO"), strMatchTo, (strSearchMode.equals("1")?"0":"lin.QtyInvoiced")); 268 } else if (strFinalDoc.equals("2")) { data = MatchingPOReceiptInvoiceData.selectAlbaran(this, (strSourceDoc.equals("3")?"M_MatchPO":"M_MatchInv"), "", "", "", "", "", Utility.getContext(this, vars, "#User_Org", "MatchingPO"), Utility.getContext(this, vars, "#User_Client", "MatchingPO"), strMatchTo, (strSearchMode.equals("1")?"0":"lin.MovementQty")); 270 } else if (strFinalDoc.equals("3")) { data = MatchingPOReceiptInvoiceData.select(this, "", "", "", "", "", Utility.getContext(this, vars, "#User_Org", "MatchingPO"), Utility.getContext(this, vars, "#User_Client", "MatchingPO"), strMatchTo, (strSearchMode.equals("1")?"0":"lin.QtyOrdered")); 272 } 273 if (data==null || data.length==0) return; 274 double totalQty = (strMatchQty.equals("")?0.0D:Double.valueOf(strMatchQty).doubleValue()); 275 Connection conn = null; 276 try { 277 conn = this.getTransactionConnection(); 278 for (int i=0;i<data.length;i++) { 279 if (!strProduct.equals(data[i].mProductId)) continue; 280 double qty=0.0D; 281 if (strSearchMode.equals("0")) qty = Double.valueOf(data[i].qty).doubleValue(); 282 qty -= (data[i].qtyMatched.equals("")?0.0D:Double.valueOf(data[i].qtyMatched).doubleValue()); 283 if (qty > totalQty) qty = totalQty; 284 totalQty -= qty; 285 boolean invoice = true; 286 if (strSourceDoc.equals("3") || strFinalDoc.equals("3")) invoice=false; 287 String M_InOutLine_ID = ""; 288 String Line_ID = ""; 289 if(strSourceDoc.equals("2")) { 290 M_InOutLine_ID = strMatched; 291 Line_ID = data[i].id; 292 } else { 293 M_InOutLine_ID = data[i].id; 294 Line_ID = strMatched; 295 } 296 if (log4j.isDebugEnabled()) log4j.debug("createMatchRecord IsInvoiced: " + invoice + " - M_InOutLine_ID: " + M_InOutLine_ID + " - Line_ID: " + Line_ID + " - M_Product_ID: " + strProduct + " - Qty: " + qty); 297 if (invoice) { 298 String strSequence = SequenceIdData.getSequence(this, "M_MatchInv", vars.getClient()); 299 MatchingPOReceiptInvoiceData.insertInvoice(conn, this, strSequence, vars.getClient(), vars.getOrg(), vars.getUser(), M_InOutLine_ID, Line_ID, strProduct, Double.toString(qty)); 300 } else { 301 String strSequence = SequenceIdData.getSequence(this, "M_MatchPO", vars.getClient()); 302 MatchingPOReceiptInvoiceData.insert(conn, this, strSequence, vars.getClient(), vars.getOrg(), vars.getUser(), M_InOutLine_ID, Line_ID, strProduct, Double.toString(qty)); 303 } 305 } 306 releaseCommitConnection(conn); 307 } catch(Exception e){ 308 try { 309 releaseRollbackConnection(conn); 310 } catch (Exception ignored) {} 311 e.printStackTrace(); 312 throw new ServletException(e); 313 } 314 } 315 316 317 public String getServletInfo() { 318 return "marchingPO Servlet"; 319 } } 321 | Popular Tags |