1 19 package org.openbravo.erpCommon.ad_process; 20 21 import org.openbravo.erpCommon.utility.*; 22 import org.openbravo.erpCommon.businessUtility.*; 23 24 import org.openbravo.utils.Replace; 25 26 import org.openbravo.base.secureApp.HttpSecureAppServlet; 27 import org.openbravo.base.secureApp.VariablesSecureApp; 28 import org.openbravo.xmlEngine.XmlDocument; 29 import java.io.*; 30 import javax.servlet.*; 31 import javax.servlet.http.*; 32 import org.apache.log4j.Logger; 33 34 import java.sql.Connection ; 36 37 import org.openbravo.erpCommon.utility.DateTimeData; 38 39 public class ExpenseAPInvoice extends HttpSecureAppServlet { 40 static Logger log4j = Logger.getLogger(ExpenseAPInvoice.class); 41 42 String strVersionNo = ""; 43 String strIsHTML = ""; 44 String strMailHeader = ""; 45 String strMailText = ""; 46 String strrMailTextId = ""; 47 String strAttachAsset = ""; 48 String strProductDownloadURL = ""; 49 String strProductVersionNo = ""; 50 String strProductRMailTextID = ""; 51 52 public void init (ServletConfig config) { 53 super.init(config); 54 boolHist = false; 55 } 56 57 public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException { 58 VariablesSecureApp vars = new VariablesSecureApp(request); 59 60 if (!Utility.hasProcessAccess(this, vars, "", "S_ExpenseAPInvoice")) { 61 bdError(response, "AccessTableNoView", vars.getLanguage()); 62 return; 63 } 64 65 if (vars.commandIn("DEFAULT")) { 66 String strcBpartnerId = vars.getStringParameter("inpcBpartnerId"); 67 String strDatereportFrom = vars.getGlobalVariable("inpReportDateFrom", "ExpenseAPInvoice.reportdatefrom", ""); 68 String strDatereportTo = vars.getGlobalVariable("inpReportDateTo", "ExpenseAPInvoice.reportdateto", ""); 69 String strDateInvoiced = vars.getGlobalVariable("inpDateinvoiced", "ExpenseAPInvoice.dateinvoiced", ""); 70 printPage(response, vars, strcBpartnerId, strDatereportFrom, strDatereportTo, strDateInvoiced); 71 } else if (vars.commandIn("SAVE")) { 72 String strcBpartnerId = vars.getStringParameter("inpcBpartnerId", ""); 73 String strDatereportFrom = vars.getRequestGlobalVariable("inpReportDateFrom", "ExpenseAPInvoice.reportdatefrom"); 74 String strDatereportTo = vars.getRequestGlobalVariable("inpReportDateTo", "ExpenseAPInvoice.reportdateto"); 75 String strDateInvoiced = vars.getRequestGlobalVariable("inpDateinvoiced", "ExpenseAPInvocie.dateinvoiced"); 76 String strMessage = processExpense(vars, strcBpartnerId, strDatereportFrom, strDatereportTo, strDateInvoiced); 77 if (!strMessage.equals("")) vars.setSessionValue("ExpenseAPInvoice.message", strMessage); 78 printPage(response, vars, strcBpartnerId, strDatereportFrom, strDatereportTo, strDateInvoiced); 79 } else pageErrorPopUp(response); 80 } 81 82 String processExpense(VariablesSecureApp vars, String strcBpartnerId, String strDatereportFrom, String strDatereportTo, String strDateInvoiced) throws IOException, ServletException { 83 if (log4j.isDebugEnabled()) log4j.debug("Save: Expense AP Invoice"); 84 String strMessageResult = ""; 85 int line = 0; 86 ExpenseAPInvoiceData[] data = ExpenseAPInvoiceData.select(this, Utility.getContext(this, vars, "#User_Client", "ExpenseAPInvoice"), Utility.getContext(this, vars, "#User_Org", "ExpenseAPInvoice"), strDatereportFrom, DateTimeData.nDaysAfter(this, strDatereportTo, "1"), strcBpartnerId); 87 String strcBpartnerIdOld = ""; 88 String strcInvoiceId = ""; 89 String strPricelistId = ""; 90 String strcTaxID = ""; 91 String strcBpartnerLocationId = ""; 92 String strcInvoiceLineId = ""; 93 String strPricestd = ""; 94 String strPricelimit = ""; 95 String strPricelist = ""; 96 float qty = 0; 97 float amount = 0; 98 99 Connection conn = null; 100 try{ 101 conn = this.getTransactionConnection(); 102 for (int i = 0; i<data.length; i++){ 104 String docTargetType = ExpenseAPInvoiceData.cDoctypeTarget(this, data[i].adClientId, data[i].adOrgId); 105 if (strcBpartnerIdOld!=data[i].cBpartnerId){ 106 String strcInvoiceIdOld = ExpenseAPInvoiceData.selectInvoiceHeader(conn, this, data[i].adClientId, data[i].adOrgId, strDateInvoiced, data[i].cBpartnerId, data[i].cCurrencyId, data[i].cProjectId, data[i].cActivityId, data[i].cCampaignId); 109 strPricelistId = ExpenseAPInvoiceData.pricelistId(this, data[i].cBpartnerId); 110 strcBpartnerLocationId = ExpenseAPInvoiceData.bPartnerLocation(this, data[i].cBpartnerId); 111 112 113 114 115 if (strcInvoiceIdOld.equals("")) { 116 strcInvoiceId = SequenceIdData.getSequence(this, "C_Invoice", data[i].adClientId); 117 String strDocumentno = Utility.getDocumentNo(this, vars, "", "C_Invoice", Utility.getContext(this, vars, "C_DocTypeTarget_ID", docTargetType) , Utility.getContext(this, vars, "C_DocType_ID", docTargetType), false, true); 118 String strDocType = ExpenseAPInvoiceData.cDoctypeTarget(this, data[i].adClientId, data[i].adOrgId); 120 String strSalesrepId = ExpenseAPInvoiceData.salesrepId(this, data[i].cBpartnerId); 122 String strPaymentRule = ExpenseAPInvoiceData.paymentrule(this, data[i].cBpartnerId); 123 String strPaymentterm = ExpenseAPInvoiceData.paymentterm(this, data[i].cBpartnerId); 124 125 127 ExpenseAPInvoiceData.insert(conn, this, strcInvoiceId, "N", "", "N", "N", "N", "N", "N", "N", "N", "N", "N", data[i].adClientId, data[i].adOrgId, "", "", strDocumentno, "", "", "Y", docTargetType, strDateInvoiced, strDateInvoiced, data[i].cBpartnerId, strcBpartnerLocationId, vars.getUser(), strPricelistId, data[i].cCurrencyId, strSalesrepId, "N", "", "", strPaymentRule, strPaymentterm, "N", "N", data[i].cProjectId, data[i].cActivityId, data[i].cCampaignId, vars.getOrg(), "", "", "0", "0", "DR", strDocType, "N", "CO", "N", vars.getUser(), vars.getUser()); 128 } 129 else strcInvoiceId = strcInvoiceIdOld; 130 } 131 132 String strmProductUomId = ExpenseAPInvoiceData.mProductUomId(this, data[i].mProductId); 133 134 ExpenseAPInvoiceData[] dataPrice = ExpenseAPInvoiceData.selectPrice(this, data[i].mProductId, strPricelistId, strDateInvoiced); 135 136 strPricestd = data[i].invoiceprice.equals("")?dataPrice[0].pricestd:data[i].invoiceprice; 139 strPricelimit = data[i].invoiceprice.equals("")?dataPrice[0].pricelimit:data[i].invoiceprice; 140 strPricelist = data[i].invoiceprice.equals("")?dataPrice[0].pricelist:data[i].invoiceprice; 141 String bpartnerLocationShip = ExpenseAPInvoiceData.shipto(this, data[i].cBpartnerId); 142 143 strcTaxID = Tax.get(this, data[i].mProductId, strDateInvoiced, data[i].adOrgId, vars.getWarehouse(), strcBpartnerLocationId, bpartnerLocationShip, data[i].cProjectId, false); 144 147 ExpenseAPInvoiceData[] dataInvoiceline = ExpenseAPInvoiceData.selectInvoiceLine(conn, this, strcInvoiceId, data[i].adClientId, data[i].adOrgId, data[i].mProductId, data[i].cUomId, strPricestd, strPricelist, strPricelimit, data[i].description, strcTaxID); 148 System.out.println("dataInvoiceline: "+dataInvoiceline.length); 149 150 if (dataInvoiceline == null || dataInvoiceline.length == 0){ 151 strcInvoiceLineId = SequenceIdData.getSequence(this, "C_InvoiceLine", data[i].adClientId); 153 qty = Float.valueOf(data[i].qty); 154 String strLine = ExpenseAPInvoiceData.selectLine(conn, this, strcInvoiceId); 155 if (strLine.equals("")) strLine = "10"; 156 line+=Integer.valueOf(strLine); 157 158 ExpenseAPInvoiceData.insertLine(conn, this, data[i].adClientId, data[i].adOrgId, strcInvoiceId, "", String.valueOf(line), "", data[i].mProductId, "", data[i].description, "", strmProductUomId, String.valueOf(qty), data[i].cUomId, strPricestd, strPricelist, strcTaxID, String.valueOf(Float.valueOf(strPricestd)*qty), "", strPricestd, strPricelimit, "", "", "", "Y", "0", "", "", strcInvoiceLineId, "", "N", vars.getUser(), vars.getUser()); 160 System.out.println("sigue"); 161 162 } else { 163 strcInvoiceLineId = dataInvoiceline[0].cInvoicelineId; 165 qty = Float.valueOf(dataInvoiceline[0].qtyinvoiced)+Float.valueOf(data[i].qty); 166 ExpenseAPInvoiceData.updateInvoiceline(conn, this, String.valueOf(qty), String.valueOf(Float.valueOf(strPricestd)*qty), strcInvoiceLineId); 167 168 } 169 170 if (!data[i].cProjectId.equals("")){ 171 ExpenseAPInvoiceData[] dataAcctdimension = ExpenseAPInvoiceData.selectAcctdimension(conn, this, data[i].adClientId, data[i].adOrgId, strcInvoiceLineId, data[i].cProjectId, data[i].cCampaignId); 173 if (dataAcctdimension == null || dataAcctdimension.length == 0) { 174 String strcInvoicelineAcctdimension = SequenceIdData.getSequence(this, "C_InvoiceLine_AcctDimension", data[i].adClientId); 175 ExpenseAPInvoiceData.insertInvoicelineAcctdimension(conn, this, strcInvoicelineAcctdimension, data[i].adClientId, data[i].adOrgId, "Y", vars.getUser(), vars.getUser(), strcInvoiceLineId, String.valueOf(qty*Float.valueOf(strPricestd)), data[i].cProjectId, data[i].cCampaignId, "", ""); 176 } else { 177 amount = Float.valueOf(dataAcctdimension[0].amt)+(Float.valueOf(data[i].qty)*Float.valueOf(strPricestd)); 178 ExpenseAPInvoiceData.updateAcctdimension(conn, this, String.valueOf(amount), dataAcctdimension[0].cInvoicelineAcctdimensionId); 179 } 180 } 181 ExpenseAPInvoiceData.updateExpense(conn, this, strcInvoiceLineId, data[i].sTimeexpenselineId); 182 183 } 184 releaseCommitConnection(conn); 185 } catch (ArrayIndexOutOfBoundsException f){ 186 try { 187 releaseRollbackConnection(conn); 188 } catch (Exception ignored) {} 189 f.printStackTrace(); 190 log4j.warn("Rollback in transaction"); 191 return Utility.messageBD(this, "PriceListVersionNotFound", vars.getLanguage()); 192 } catch (Exception e){ 193 try { 194 releaseRollbackConnection(conn); 195 } catch (Exception ignored) {} 196 e.printStackTrace(); 197 log4j.warn("Rollback in transaction"); 198 return Utility.messageBD(this, "ProcessRunError", vars.getLanguage()); 199 } 200 return strMessageResult.equals("")?Utility.messageBD(this, "Success", vars.getLanguage()):strMessageResult; 201 202 } 203 204 205 void printPage(HttpServletResponse response, VariablesSecureApp vars, String strcBpartnerId, String strDatereportFrom, String strDatereportTo, String strDateInvoiced) throws IOException, ServletException { 206 if (log4j.isDebugEnabled()) log4j.debug("Output: process ExpenseAPInvoice"); 207 208 String [] discard = {""}; 209 String strHelp = ExpenseAPInvoiceData.help(this, "S_ExpenseAPInvoice"); 210 if (strHelp.equals("")) discard[0] = new String ("helpDiscard"); 211 XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_process/ExpenseAPInvoice").createXmlDocument(); 212 213 ToolBar toolbar = new ToolBar(this, vars.getLanguage(), "ExpenseAPInvoice", false, "", "", "",false, "ad_process", strReplaceWith, false, true); 214 toolbar.prepareSimpleToolBarTemplate(); 215 xmlDocument.setParameter("toolbar", toolbar.toString()); 216 217 xmlDocument.setParameter("calendar", vars.getLanguage().substring(0,2)); 218 xmlDocument.setParameter("language", "LNG_POR_DEFECTO=\"" + vars.getLanguage() + "\";"); 219 xmlDocument.setParameter("direction", "var baseDirection = \"" + strReplaceWith + "/\";\n"); 220 xmlDocument.setParameter("help", strHelp); 221 xmlDocument.setParameter("dateFrom", strDatereportFrom); 222 xmlDocument.setParameter("dateTo", strDatereportTo); 223 xmlDocument.setParameter("description", ExpenseAPInvoiceData.description(this, "S_ExpenseAPInvoice")); 225 226 try { 227 ComboTableData comboTableData = new ComboTableData(vars, this, "TABLE", "C_BPartner_ID", "C_BPartner Employee w Address", "", Utility.getContext(this, vars, "#User_Client",""), Utility.getContext(this, vars, "#AD_Client_ID", "ExpenseAPInvoice"), 0); 228 Utility.fillSQLParameters(this, vars, null, comboTableData, "ExpenseAPInvoice", ""); 229 xmlDocument.setData("reportC_BPARTNERID","liststructure", comboTableData.select(false)); 230 comboTableData = null; 231 } catch (Exception ex) { 232 throw new ServletException(ex); 233 } 234 235 String strMessage = vars.getSessionValue("ExpenseAPInvoice.message"); 236 if (!strMessage.equals("")) { 237 vars.removeSessionValue("ExpenseAPInvoice.message"); 238 strMessage = "alert('" + Replace.replace(strMessage, "'", "\'") + "');"; 239 } 240 241 242 xmlDocument.setParameter("dateFromdisplayFormat", vars.getSessionValue("#AD_SqlDateFormat")); 243 xmlDocument.setParameter("dateFromsaveFormat", vars.getSessionValue("#AD_SqlDateFormat")); 244 xmlDocument.setParameter("dateTodisplayFormat", vars.getSessionValue("#AD_SqlDateFormat")); 245 xmlDocument.setParameter("dateTosaveFormat", vars.getSessionValue("#AD_SqlDateFormat")); 246 xmlDocument.setParameter("dateInvdisplayFormat", vars.getSessionValue("#AD_SqlDateFormat")); 247 xmlDocument.setParameter("dateInvsaveFormat", vars.getSessionValue("#AD_SqlDateFormat")); 248 249 250 try { 252 KeyMap key = new KeyMap(this, vars, "ExpenseAPInvoice.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_process.ExpenseAPInvoice"); 259 260 xmlDocument.setParameter("parentTabContainer", tabs.parentTabs()); 261 xmlDocument.setParameter("mainTabContainer", tabs.mainTabs()); 262 xmlDocument.setParameter("childTabContainer", tabs.childTabs()); 263 xmlDocument.setParameter("theme", vars.getTheme()); 264 NavigationBar nav = new NavigationBar(this, vars.getLanguage(), "ExpenseAPInvoice.html", classInfo.id, classInfo.type, strReplaceWith, tabs.breadcrumb()); 265 xmlDocument.setParameter("navigationBar", nav.toString()); 266 LeftTabsBar lBar = new LeftTabsBar(this, vars.getLanguage(), "ExpenseAPInvoice.html", strReplaceWith); 267 xmlDocument.setParameter("leftTabs", lBar.manualTemplate()); 268 } catch (Exception ex) { 269 throw new ServletException(ex); 270 } 271 { 272 OBError myMessage = vars.getMessage("ExpenseAPInvoice"); 273 vars.removeMessage("ExpenseAPInvoice"); 274 if (myMessage!=null) { 275 xmlDocument.setParameter("messageType", myMessage.getType()); 276 xmlDocument.setParameter("messageTitle", myMessage.getTitle()); 277 xmlDocument.setParameter("messageMessage", myMessage.getMessage()); 278 } 279 } 280 281 283 xmlDocument.setParameter("body", strMessage); 284 response.setContentType("text/html; charset=UTF-8"); 285 PrintWriter out = response.getWriter(); 286 out.println(xmlDocument.print()); 287 out.close(); 288 } 289 290 public String getServletInfo() { 291 return "Servlet ExpenseAPInvoice"; 292 } } 294 295
| Popular Tags
|