| 1 19 package org.openbravo.erpCommon.ad_actionButton; 20 21 import org.openbravo.erpCommon.utility.ToolBar; 22 23 import org.openbravo.erpCommon.utility.SequenceIdData; 24 import org.openbravo.erpCommon.reference.*; 25 import org.openbravo.erpCommon.utility.*; 26 import org.openbravo.utils.Replace; 27 import org.openbravo.erpCommon.businessUtility.WindowTabs; 28 import org.openbravo.erpCommon.businessUtility.*; 29 import org.openbravo.erpCommon.ad_callouts.*; 30 import org.openbravo.base.secureApp.HttpSecureAppServlet; 31 import org.openbravo.base.secureApp.VariablesSecureApp; 32 import org.openbravo.xmlEngine.XmlDocument; 33 import java.io.*; 34 import java.math.BigDecimal ; 35 import javax.servlet.*; 36 import javax.servlet.http.*; 37 38 import org.openbravo.erpCommon.ad_combos.OrganizationComboData; 39 40 41 import java.sql.Connection ; 43 44 import org.openbravo.erpCommon.utility.DateTimeData; 45 46 public class ExpenseSOrder extends HttpSecureAppServlet { 47 48 static final BigDecimal ZERO = new BigDecimal (0.0); 49 50 public void init (ServletConfig config) { 51 super.init(config); 52 boolHist = false; 53 } 54 55 public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException { 56 VariablesSecureApp vars = new VariablesSecureApp(request); 57 58 if (vars.commandIn("DEFAULT")) { 59 printPage(response, vars, "", "", "", ""); 60 } else if (vars.commandIn("SAVE")) { 61 String strBPartner = vars.getStringParameter("inpcBpartnerId"); 62 String strDatefrom = vars.getStringParameter("inpDateFrom"); 63 String strDateto = vars.getStringParameter("inpDateTo"); 64 String strOrganization = vars.getStringParameter("organization"); 65 String messageResult = ""; 66 messageResult = processButton(vars, strBPartner, strDatefrom, strDateto, strOrganization); 67 vars.setSessionValue("ExpenseSOrder|message", messageResult); 68 printPage(response, vars, strDatefrom, strDateto, strBPartner, strOrganization); 69 } else pageErrorPopUp(response); 70 } 71 72 73 String processButton(VariablesSecureApp vars, String strBPartner, String strDatefrom, String strDateto, String strOrganization) { 74 StringBuffer textoMensaje = new StringBuffer (); 75 Connection conn=null; 76 try { 77 conn = getTransactionConnection(); 78 ExpenseSOrderData[] data = ExpenseSOrderData.select(this, strBPartner, strDatefrom, DateTimeData.nDaysAfter(this, strDateto,"1"), Utility.getContext(this, vars, "#User_Client", "ExpenseSOrder"), strOrganization.equals("")?Utility.getContext(this, vars, "#User_Org", "ExpenseSOrder"):strOrganization); 79 String strOldOrganization = "-1"; 80 String strOldBPartner = "-1"; 81 String strOldProject = "-1"; 82 String strDocStatus = "DR"; 83 String strDocAction = "CO"; 84 String strCOrderId = ""; 85 String priceactual = ""; 86 String pricelist = ""; 87 String pricelimit = ""; 88 String docType = "0"; 89 String docTargetType = ""; 90 int line = 0; 91 int total=0; 92 for (int i=0;data!=null && i<data.length;i++) { 94 docTargetType = ExpenseSOrderData.cDoctypeTarget(conn, this, data[i].adClientId, data[i].adOrgId); 95 if ((!data[i].cBpartnerId.equals(strOldBPartner) || !data[i].cProjectId.equals(strOldProject)|| !data[i].adOrgId.equals(strOldOrganization)) && !strCOrderId.equals("")) { 96 releaseCommitConnection(conn); 97 String mensaje = processOrder(vars, strCOrderId); 98 if (!mensaje.equals("")) textoMensaje.append(mensaje).append("\\n"); 99 conn = getTransactionConnection(); 100 } 101 if (!data[i].cBpartnerId.equals(strOldBPartner) || !data[i].cProjectId.equals(strOldProject) || !data[i].adOrgId.equals(strOldOrganization)) { 102 line = 0; 103 total++; 104 strCOrderId = SequenceIdData.getSequence(this, "C_Order", vars.getClient()); 105 String strDocumentNo = Utility.getDocumentNo(this, vars, "", "C_Order", Utility.getContext(this, vars, "C_DocTypeTarget_ID", docTargetType), Utility.getContext(this, vars, "C_DocType_ID", docTargetType), false, true); 107 108 strOldBPartner = data[i].cBpartnerId; 109 strOldProject = data[i].cProjectId; 110 strOldOrganization = data[i].adOrgId; 111 112 SEOrderBPartnerData[] data1 = SEOrderBPartnerData.select(this, data[i].cBpartnerId); 113 114 ExpenseSOrderData.insertCOrder(conn, this, strCOrderId, data[i].adClientId, data[i].adOrgId, vars.getUser(), strDocumentNo, strDocStatus, strDocAction, docType, docTargetType, strDateto, strDateto, strDateto, data[i].cBpartnerId, ExpenseSOrderData.cBPartnerLocationId(this, data[i].cBpartnerId), ExpenseSOrderData.billto(this, data[i].cBpartnerId).equals("")?ExpenseSOrderData.cBPartnerLocationId(this, data[i].cBpartnerId):ExpenseSOrderData.billto(this, data[i].cBpartnerId), data[i].cCurrencyId.equals("")?Utility.getContext(this, vars, "$C_Currency_ID", "ExpenseSOrder"):data[i].cCurrencyId, data1[0].paymentrule, data1[0].cPaymenttermId.equals("")?SEOrderBPartnerData.selectPaymentTerm(this, data[i].adClientId):data1[0].cPaymenttermId, data1[0].invoicerule.equals("")?"I":data1[0].invoicerule, data1[0].deliveryrule.equals("")?"A":data1[0].deliveryrule, data1[0].freightcostrule.equals("")?"I":data1[0].freightcostrule, data1[0].deliveryviarule.equals("")?"D":data1[0].deliveryviarule, data[i].mWarehouseId.equals("")?vars.getWarehouse():data[i].mWarehouseId, data[i].mPricelistId, data[i].cProjectId, data[i].cActivityId, data[i].cCampaignId); 115 } 116 117 String strCOrderlineID = SequenceIdData.getSequence(this, "C_OrderLine", vars.getClient()); 118 119 String strPrecision = "0"; 120 String strPricePrecision="0"; 121 String strDiscount = ""; 122 if (line==0) { 123 line = 10; 124 } else { 125 line = line + 10; 126 } 127 if (data[i].invoiceprice == null || data[i].invoiceprice.equals("")) { 128 SEExpenseProductData[] data3 = SEExpenseProductData.select(this, data[i].mProductId, data[i].mPricelistId); 129 for (int j=0;data3!=null && j<data3.length;j++) { 130 if (data3[j].validfrom == null || data3[j].validfrom.equals("") || !DateTimeData.compare(this, DateTimeData.today(this), data3[j].validfrom).equals("-1")){ 131 priceactual = data3[j].pricestd; 132 pricelist = data3[j].pricelist; 133 pricelimit = data3[j].pricelimit; 134 SLOrderAmtData[] data4 = SLOrderAmtData.select(this, strCOrderId); 135 if (data4!=null && data4.length>0) { 136 strPrecision = data4[0].stdprecision.equals("")?"0":data4[0].stdprecision; 137 strPricePrecision = data4[0].priceprecision.equals("")?"0":data4[0].priceprecision; 138 } 139 int StdPrecision = Integer.valueOf(strPrecision).intValue(); 140 int PricePrecision = Integer.valueOf(strPricePrecision).intValue(); 141 142 BigDecimal priceActual, priceList, discount; 143 144 priceActual = (priceactual.equals("")?ZERO:(new BigDecimal (priceactual))); 145 priceActual.setScale(PricePrecision, BigDecimal.ROUND_HALF_UP); 146 priceList = (pricelist.equals("")?ZERO:new BigDecimal (pricelist)); 147 if (priceList.doubleValue() == 0.0) discount = ZERO; 148 else discount = new BigDecimal ((priceList.doubleValue() - priceActual.doubleValue()) / priceList.doubleValue() * 100.0); 149 if (discount.scale() > StdPrecision) discount = discount.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP); 150 strDiscount = discount.toString(); 151 priceactual = priceActual.toString(); 152 pricelist = priceList.toString(); 153 } 154 } 155 if (priceactual.equals("")) priceactual="0"; 156 if (pricelist.equals("")) pricelist="0"; 157 if (pricelimit.equals("")) pricelimit="0"; 158 } else { 159 priceactual = data[i].invoiceprice; 160 pricelist = "0"; 161 pricelimit = "0"; 162 } 163 String strCTaxID = Tax.get(this, data[i].mProductId, DateTimeData.today(this), data[i].adOrgId, data[i].mWarehouseId.equals("")?vars.getWarehouse():data[i].mWarehouseId, ExpenseSOrderData.cBPartnerLocationId(this, data[i].cBpartnerId), ExpenseSOrderData.cBPartnerLocationId(this, data[i].cBpartnerId), data[i].cProjectId, true); 164 165 ExpenseSOrderData.insertCOrderline(conn, this, strCOrderlineID, data[i].adClientId, strOrganization.equals("")?data[i].adOrgId:strOrganization, vars.getUser(), strCOrderId, Integer.toString(line), data[i].cBpartnerId, ExpenseSOrderData.cBPartnerLocationId(this, data[i].cBpartnerId), DateTimeData.today(this), DateTimeData.today(this), data[i].description, data[i].mProductId, data[i].mWarehouseId.equals("")?vars.getWarehouse():data[i].mWarehouseId, data[i].cUomId.equals("")?Utility.getContext(this, vars, "#C_UOM_ID", "ExpenseSOrder"):data[i].cUomId, data[i].qty, data[i].cCurrencyId.equals("")?Utility.getContext(this, vars, "$C_Currency_ID", "ExpenseSOrder"):data[i].cCurrencyId, pricelist, priceactual, pricelimit, strCTaxID, data[i].sResourceassignmentId, strDiscount); 166 167 ExpenseSOrderData.updateTimeExpenseLine(conn, this, strCOrderlineID, data[i].sTimeexpenselineId); 168 } 169 releaseCommitConnection(conn); 170 if (!strCOrderId.equals("")) { 171 172 String mensaje = processOrder(vars, strCOrderId); 173 if (!mensaje.equals("")) textoMensaje.append(mensaje).append("\\n"); 174 } 175 return (textoMensaje.toString() + Utility.messageBD(this, "Created", vars.getLanguage()) + ": " + Integer.toString(total)); 176 180 } catch (Exception e) { 181 try { 182 if (conn!=null) releaseRollbackConnection(conn); 183 } catch (Exception ignored) {} 184 e.printStackTrace(); 185 log4j.warn("Rollback in transaction"); 186 return Utility.messageBD(this, "ProcessRunError", vars.getLanguage()); 187 } 188 } 189 206 207 String processOrder(VariablesSecureApp vars, String strCOrderId) throws ServletException { 208 String pinstance = SequenceIdData.getSequence(this, "AD_PInstance", vars.getClient()); 209 PInstanceProcessData.insertPInstance(this, pinstance, "104", strCOrderId, "N", vars.getUser(), vars.getClient(), vars.getOrg()); 210 ActionButtonData.process104(this, pinstance); 211 212 PInstanceProcessData[] pinstanceData = PInstanceProcessData.select(this, pinstance); 213 String messageResult=""; 214 if (pinstanceData!=null && pinstanceData.length>0) { 215 if (!pinstanceData[0].errormsg.equals("")) { 216 String message = pinstanceData[0].errormsg; 217 if (message.startsWith("@") && message.endsWith("@")) { 218 message = message.substring(1, message.length()-1); 219 if (message.indexOf("@")==-1) messageResult = Utility.messageBD(this, message, vars.getLanguage()); 220 else messageResult = Utility.parseTranslation(this, vars, vars.getLanguage(), "@" + message + "@"); 221 } else { 222 messageResult = Utility.parseTranslation(this, vars, vars.getLanguage(), message); 223 } 224 } else if (!pinstanceData[0].pMsg.equals("")) { 225 String message = pinstanceData[0].pMsg; 226 messageResult = Utility.parseTranslation(this, vars, vars.getLanguage(), message); 227 } else if (pinstanceData[0].result.equals("1")) { 228 messageResult = ""; 229 } else { 230 messageResult = Utility.messageBD(this, "Error", vars.getLanguage()); 231 } 232 } 233 messageResult = Replace.replace(messageResult, "'", "\\'"); 234 if (log4j.isDebugEnabled()) log4j.debug(messageResult); 235 return (messageResult); 236 } 237 238 void printPage(HttpServletResponse response, VariablesSecureApp vars, String strDatefrom,String strDateto,String strBPartner, String strOrganization) 239 throws IOException, ServletException { 240 if (log4j.isDebugEnabled()) log4j.debug("Output: Button process Project set Type"); 241 242 XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_actionButton/ExpenseSOrder").createXmlDocument(); 243 String strMessage = vars.getSessionValue("ExpenseSOrder|message"); 244 vars.removeSessionValue("ExpenseSOrder|message"); 245 246 ToolBar toolbar = new ToolBar(this, vars.getLanguage(), "ExpenseSOrder", false, "", "", "",false, "ad_actionButton", strReplaceWith, false, true); 247 toolbar.prepareSimpleToolBarTemplate(); 248 xmlDocument.setParameter("toolbar", toolbar.toString()); 249 250 251 try { 252 KeyMap key = new KeyMap(this, vars, "ExpenseSOrder.html"); 253 xmlDocument.setParameter("keyMap", key.getActionButtonKeyMaps()); 254 } catch (Exception ex) { 255 throw new ServletException(ex); 256 } 257 try { 258 WindowTabs tabs = new WindowTabs(this, vars, "org.openbravo.erpCommon.ad_actionButton.ExpenseSOrder"); 259 xmlDocument.setParameter("parentTabContainer", tabs.parentTabs()); 260 xmlDocument.setParameter("mainTabContainer", tabs.mainTabs()); 261 xmlDocument.setParameter("childTabContainer", tabs.childTabs()); 262 xmlDocument.setParameter("theme", vars.getTheme()); 263 NavigationBar nav = new NavigationBar(this, vars.getLanguage(), "ExpenseSOrder.html", classInfo.id, classInfo.type, strReplaceWith, tabs.breadcrumb()); 264 xmlDocument.setParameter("navigationBar", nav.toString()); 265 LeftTabsBar lBar = new LeftTabsBar(this, vars.getLanguage(), "ExpenseSOrder.html", strReplaceWith); 266 xmlDocument.setParameter("leftTabs", lBar.manualTemplate()); 267 } catch (Exception ex) { 268 throw new ServletException(ex); 269 } 270 { 271 OBError myMessage = vars.getMessage("ExpenseSOrder"); 272 vars.removeMessage("GenerateInvoicesmanual"); 273 if (myMessage!=null) { 274 xmlDocument.setParameter("messageType", myMessage.getType()); 275 xmlDocument.setParameter("messageTitle", myMessage.getTitle()); 276 xmlDocument.setParameter("messageMessage", myMessage.getMessage()); 277 } 278 } 279 280 xmlDocument.setParameter("calendar", vars.getLanguage().substring(0,2)); 281 xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";"); 282 283 xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n"); 284 xmlDocument.setParameter("paramMessage", (strMessage.equals("")?"":"alert('" + Replace.replace(strMessage, "\"", "\\'") + "');")); 285 xmlDocument.setParameter("Bpartnerdescription", ExpenseSOrderData.selectBpartner(this, strBPartner)); 286 xmlDocument.setParameter("BpartnerId", strBPartner); 287 xmlDocument.setParameter("adOrgId", strOrganization); 288 xmlDocument.setParameter("dateFrom", strDatefrom); 289 xmlDocument.setParameter("dateFromdisplayFormat", vars.getSessionValue("#AD_SqlDateFormat")); 290 xmlDocument.setParameter("dateFromsaveFormat", vars.getSessionValue("#AD_SqlDateFormat")); 291 xmlDocument.setParameter("dateTo", strDateto); 292 xmlDocument.setParameter("dateTodisplayFormat", vars.getSessionValue("#AD_SqlDateFormat")); 293 xmlDocument.setParameter("dateTosaveFormat", vars.getSessionValue("#AD_SqlDateFormat")); 294 xmlDocument.setData("structureOrganizacion", OrganizationComboData.select(this, vars.getRole())); 297 response.setContentType("text/html; charset=UTF-8"); 298 PrintWriter out = response.getWriter(); 299 out.println(xmlDocument.print()); 300 out.close(); 301 } 302 303 public String getServletInfo() { 304 return "Servlet Project set Type"; 305 } } 307 308 | Popular Tags |