1 19 package org.openbravo.erpCommon.ad_actionButton; 20 21 import org.openbravo.erpCommon.utility.SequenceIdData; 22 import org.openbravo.erpCommon.utility.*; 23 import org.openbravo.utils.FormatUtilities; 24 import org.openbravo.utils.Replace; 25 import org.openbravo.base.secureApp.HttpSecureAppServlet; 26 import org.openbravo.base.secureApp.VariablesSecureApp; 27 import org.openbravo.xmlEngine.XmlDocument; 28 import java.math.BigDecimal ; 29 import java.io.*; 30 import java.util.*; 31 import javax.servlet.*; 32 import javax.servlet.http.*; 33 34 import java.sql.Connection ; 36 37 import org.openbravo.erpCommon.utility.DateTimeData; 38 39 public class CopyFromOrder extends HttpSecureAppServlet { 40 static final BigDecimal ZERO = new BigDecimal (0.0); 41 42 public void init (ServletConfig config) { 43 super.init(config); 44 boolHist = false; 45 } 46 47 public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException { 48 VariablesSecureApp vars = new VariablesSecureApp(request); 49 50 if (vars.commandIn("DEFAULT")) { 51 String strKey = vars.getGlobalVariable("inpcOrderId", "CopyFromOrder|mInoutId"); 52 String strWindowId = vars.getGlobalVariable("inpwindowId", "CopyFromOrder|windowId", ""); 53 String strTabName = vars.getGlobalVariable("inpTabId", "CopyFromOrder|adTabId", ""); 54 String strBpartner = vars.getGlobalVariable("inpcBpartnerId", "CopyFromOrder|cBpartnerId", ""); 55 String strmPricelistId = vars.getGlobalVariable("inpmPricelistId", "CopyFromOrder|mPricelistId", ""); 56 printPage_FS(response, vars); 57 } else if (vars.commandIn("FRAME2")) { 58 printPage_F2(response, vars); 59 } else if (vars.commandIn("FRAME1")) { 60 String strWindowId = vars.getGlobalVariable("inpWindowId", "CopyFromOrder|windowId"); 61 String strSOTrx = Utility.getContext(this, vars, "isSOTrx", strWindowId); 62 String strKey = vars.getGlobalVariable("inpcOrderId", "CopyFromOrder|mInoutId"); 63 String strTabId = vars.getGlobalVariable("inpTabId", "CopyFromOrder|adTabId"); 64 String strBpartner = vars.getGlobalVariable("inpcBpartnerId", "CopyFromOrder|cBpartnerId", ""); 65 String strmPricelistId = vars.getGlobalVariable("inpmPricelistId", "CopyFromOrder|mPricelistId", ""); 66 vars.removeSessionValue("CopyFromOrder|inpcOrderId"); 67 vars.removeSessionValue("CopyFromOrder|windowId"); 68 vars.removeSessionValue("CopyFromOrder|adTabId"); 69 vars.removeSessionValue("CopyFromOrder|cBpartnerId"); 70 vars.removeSessionValue("CopyFromOrder|inpmPricelistId"); 71 printPageDataSheet(response, vars, strKey, strWindowId, strTabId, strSOTrx, strBpartner, strmPricelistId); 72 } else if (vars.commandIn("SAVE")) { 73 String strRownum = vars.getRequiredInStringParameter("inpRownumId"); 74 String strKey = vars.getRequiredStringParameter("inpcOrderId"); 75 String strWindowId = vars.getStringParameter("inpWindowId"); 76 String strSOTrx = vars.getStringParameter("inpissotrx"); 77 String strTabId = vars.getStringParameter("inpTabId"); 78 String strMessage = copyLines(vars, strRownum, strKey, strWindowId, strSOTrx); 79 ActionButtonDefaultData[] tab = ActionButtonDefaultData.windowName(this, strTabId); 80 String strWindowPath="", strTabName=""; 81 if (tab!=null && tab.length!=0) { 82 strTabName = FormatUtilities.replace(tab[0].name); 83 strWindowPath = "../" + FormatUtilities.replace(tab[0].description) + "/" + strTabName + "_Relation.html"; 84 } else strWindowPath = strDefaultServlet; 85 if (!strMessage.equals("")) vars.setSessionValue(strWindowId + "|" + strTabName + ".message", strMessage); 86 printPageClosePopUp(response, vars, strWindowPath); 87 } else pageErrorPopUp(response); 88 } 89 90 void printPage_FS(HttpServletResponse response, VariablesSecureApp vars) throws IOException, ServletException { 91 log4j.debug("Output: FrameSet"); 92 XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/CopyFromOrder_FS").createXmlDocument(); 93 response.setContentType("text/html; charset=UTF-8"); 94 PrintWriter out = response.getWriter(); 95 out.println(xmlDocument.print()); 96 out.close(); 97 } 98 99 void printPage_F2(HttpServletResponse response, VariablesSecureApp vars) throws IOException, ServletException { 100 log4j.debug("Output: Frame2"); 101 XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/CopyFromOrder_F2").createXmlDocument(); 102 xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n"); 103 xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";"); 104 response.setContentType("text/html; charset=UTF-8"); 105 PrintWriter out = response.getWriter(); 106 out.println(xmlDocument.print()); 107 out.close(); 108 } 109 110 String copyLines(VariablesSecureApp vars, String strRownum, String strKey, String strWindowId, String strSOTrx) 111 throws IOException, ServletException { 112 113 String strMessageResult = ""; 114 int total = 0; 115 if (strRownum.equals("")) return ""; 116 Connection conn = null; 117 try { 118 conn = getTransactionConnection(); 119 if (strRownum.startsWith("(")) strRownum = strRownum.substring(1, strRownum.length()-1); 120 if (!strRownum.equals("")) { 121 strRownum = Replace.replace(strRownum, "'", ""); 122 StringTokenizer st = new StringTokenizer(strRownum, ",", false); 123 while (st.hasMoreTokens()) { 124 strRownum = st.nextToken().trim(); 125 String strmProductId = vars.getStringParameter("inpmProductId" + strRownum); 126 String strmAttributesetinstanceId = vars.getStringParameter("inpmAttributesetinstanceId" + strRownum); 127 String strPricestd = vars.getStringParameter("inpmPricestdvalue" + strRownum); 128 String strLastpriceso = vars.getStringParameter("inpLastpriceso" + strRownum); 129 String strQty = vars.getStringParameter("inpquantity" + strRownum); 130 String strcTaxId = vars.getStringParameter("inpcTaxId" + strRownum); 131 String strcUOMId = vars.getStringParameter("inpcUOMId" + strRownum); 132 String strCOrderlineID = SequenceIdData.getSequence(this, "C_OrderLine", vars.getClient()); 133 CopyFromOrderRecordData[] order = CopyFromOrderRecordData.select(this, strKey); 134 CopyFromOrderData[] orderlineprice = CopyFromOrderData.selectPrices(this, order[0].dateordered, strmProductId, order[0].mPricelistId); 135 if (orderlineprice==null || orderlineprice.length==0) { 136 orderlineprice = CopyFromOrderData.set(); 137 orderlineprice[0].pricelist ="0"; 138 orderlineprice[0].pricelimit = "0"; 139 } 140 int rowCount = CopyFromOrderData.insertCOrderline(conn, this, strCOrderlineID, order[0].adClientId, order[0].adOrgId, vars.getUser(), 141 strKey, order[0].cBpartnerId, order[0].cBpartnerLocationId, order[0].dateordered, order[0].dateordered, 142 strmProductId, order[0].mWarehouseId.equals("")?vars.getWarehouse():order[0].mWarehouseId, strcUOMId, strQty, order[0].cCurrencyId, orderlineprice[0].pricelist, strLastpriceso, orderlineprice[0].pricelimit, strcTaxId, strmAttributesetinstanceId); 143 } 144 } 145 releaseCommitConnection(conn); 146 } catch (Exception e){ 147 try { 148 releaseRollbackConnection(conn); 149 } catch (Exception ignored) {} 150 e.printStackTrace(); 151 log4j.warn("Rollback in transaction"); 152 return Utility.messageBD(this, "ProcessRunError", vars.getLanguage()); 153 } 154 return strMessageResult.equals("")?Utility.messageBD(this, "Success", vars.getLanguage()):strMessageResult; 155 } 156 157 158 void printPageDataSheet(HttpServletResponse response, VariablesSecureApp vars, String strKey, String strWindowId, String strTabId, String strSOTrx, String strBpartner, String strmPricelistId) throws IOException, ServletException { 159 log4j.debug("Output: Shipment"); 160 161 XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/CopyFromOrder_F1").createXmlDocument(); 162 CopyFromOrderRecordData[] dataOrder = CopyFromOrderRecordData.select(this, strKey); 163 CopyFromOrderData[] data = CopyFromOrderData.select(this, strBpartner, strmPricelistId, dataOrder[0].dateordered, strSOTrx, dataOrder[0].lastDays.equals("")?"0":dataOrder[0].lastDays); 164 xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";"); 165 xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n"); 166 xmlDocument.setParameter("key", strKey); 167 xmlDocument.setParameter("windowId", strWindowId); 168 xmlDocument.setParameter("tabId", strTabId); 169 xmlDocument.setParameter("sotrx", strSOTrx); 170 xmlDocument.setParameter("yearactual", DateTimeData.sysdateYear(this)); 171 xmlDocument.setParameter("lastmonth", dataOrder[0].lastDays.equals("")?"0":dataOrder[0].lastDays); 172 xmlDocument.setParameter("pendingdelivery", strSOTrx.equals("Y")?CopyFromOrderRecordData.pendingDeliverySales(this, strBpartner, dataOrder[0].adOrgId, dataOrder[0].adClientId):CopyFromOrderRecordData.materialReceiptPending(this, strBpartner, dataOrder[0].adOrgId, dataOrder[0].adClientId)); 173 xmlDocument.setParameter("pendingInvoice", strSOTrx.equals("Y")?CopyFromOrderRecordData.pendingInvoiceSales(this, strBpartner, dataOrder[0].adOrgId, dataOrder[0].adClientId):CopyFromOrderRecordData.purchasePendingInvoice(this, strBpartner, dataOrder[0].adOrgId, dataOrder[0].adClientId)); 174 xmlDocument.setParameter("debtpending", CopyFromOrderRecordData.debtPending(this, strBpartner, dataOrder[0].adOrgId, dataOrder[0].adClientId, strSOTrx)); 175 xmlDocument.setParameter("contact", CopyFromOrderRecordData.contact(this, dataOrder[0].adUserId)); 176 xmlDocument.setParameter("lastOrder", CopyFromOrderRecordData.maxDateordered(this, vars.getSqlDateFormat(), strBpartner, strSOTrx, dataOrder[0].adOrgId, dataOrder[0].adClientId)); 177 xmlDocument.setParameter("orgname", dataOrder[0].orgname); 178 String strInvoicing = CopyFromOrderRecordData.invoicing(this, strSOTrx, strBpartner, dataOrder[0].adOrgId, dataOrder[0].adClientId); 179 String strTotal = CopyFromOrderRecordData.invoicingTotal(this, strSOTrx, dataOrder[0].adOrgId, dataOrder[0].adClientId); 180 xmlDocument.setParameter("invoicing", strInvoicing); 181 xmlDocument.setParameter("bpartnername",dataOrder[0].bpartnername); 182 183 BigDecimal invoicing, total, totalAverage; 184 185 invoicing = (strInvoicing.equals("")?ZERO:(new BigDecimal (strInvoicing))); 186 total = (strTotal.equals("")?ZERO:new BigDecimal (strTotal)); 187 String strTotalAverage = ""; 188 if (total==ZERO) { 189 totalAverage = new BigDecimal (invoicing.doubleValue() / total.doubleValue() * 100.0); 190 totalAverage = totalAverage.setScale(2, BigDecimal.ROUND_HALF_UP); 191 strTotalAverage = totalAverage.toString(); 192 } 194 195 xmlDocument.setParameter("totalAverage", strTotalAverage); 196 197 xmlDocument.setData("structure1", data); 198 xmlDocument.setData("structure2", dataOrder); 199 response.setContentType("text/html; charset=UTF-8"); 200 PrintWriter out = response.getWriter(); 201 out.println(xmlDocument.print()); 202 out.close(); 203 204 } 205 206 public String getServletInfo() { 207 return "Servlet Copy from order"; 208 } } 210 211 | Popular Tags |