KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_process > ExpenseAPInvoice


1 /*
2  *************************************************************************
3  * The contents of this file are subject to the Openbravo Public License
4  * Version 1.0 (the "License"), being the Mozilla Public License
5  * Version 1.1 with a permitted attribution clause; you may not use this
6  * file except in compliance with the License. You may obtain a copy of
7  * the License at http://www.openbravo.com/legal/license.html
8  * Software distributed under the License is distributed on an "AS IS"
9  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
10  * License for the specific language governing rights and limitations
11  * under the License.
12  * The Original Code is Openbravo ERP.
13  * The Initial Developer of the Original Code is Openbravo SL
14  * All portions are Copyright (C) 2001-2006 Openbravo SL
15  * All Rights Reserved.
16  * Contributor(s): ______________________________________.
17  ************************************************************************
18 */

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 // imports for transactions
35
import java.sql.Connection JavaDoc;
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 JavaDoc strVersionNo = "";
43 String JavaDoc strIsHTML = "";
44 String JavaDoc strMailHeader = "";
45 String JavaDoc strMailText = "";
46 String JavaDoc strrMailTextId = "";
47 String JavaDoc strAttachAsset = "";
48 String JavaDoc strProductDownloadURL = "";
49 String JavaDoc strProductVersionNo = "";
50 String JavaDoc 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 JavaDoc strcBpartnerId = vars.getStringParameter("inpcBpartnerId");
67       String JavaDoc strDatereportFrom = vars.getGlobalVariable("inpReportDateFrom", "ExpenseAPInvoice.reportdatefrom", "");
68       String JavaDoc strDatereportTo = vars.getGlobalVariable("inpReportDateTo", "ExpenseAPInvoice.reportdateto", "");
69       String JavaDoc strDateInvoiced = vars.getGlobalVariable("inpDateinvoiced", "ExpenseAPInvoice.dateinvoiced", "");
70       printPage(response, vars, strcBpartnerId, strDatereportFrom, strDatereportTo, strDateInvoiced);
71     } else if (vars.commandIn("SAVE")) {
72       String JavaDoc strcBpartnerId = vars.getStringParameter("inpcBpartnerId", "");
73       String JavaDoc strDatereportFrom = vars.getRequestGlobalVariable("inpReportDateFrom", "ExpenseAPInvoice.reportdatefrom");
74       String JavaDoc strDatereportTo = vars.getRequestGlobalVariable("inpReportDateTo", "ExpenseAPInvoice.reportdateto");
75       String JavaDoc strDateInvoiced = vars.getRequestGlobalVariable("inpDateinvoiced", "ExpenseAPInvocie.dateinvoiced");
76       String JavaDoc 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 JavaDoc processExpense(VariablesSecureApp vars, String JavaDoc strcBpartnerId, String JavaDoc strDatereportFrom, String JavaDoc strDatereportTo, String JavaDoc strDateInvoiced) throws IOException, ServletException {
83     if (log4j.isDebugEnabled()) log4j.debug("Save: Expense AP Invoice");
84     String JavaDoc 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 JavaDoc strcBpartnerIdOld = "";
88     String JavaDoc strcInvoiceId = "";
89     String JavaDoc strPricelistId = "";
90     String JavaDoc strcTaxID = "";
91     String JavaDoc strcBpartnerLocationId = "";
92     String JavaDoc strcInvoiceLineId = "";
93     String JavaDoc strPricestd = "";
94     String JavaDoc strPricelimit = "";
95     String JavaDoc strPricelist = "";
96     float qty = 0;
97     float amount = 0;
98
99     Connection JavaDoc conn = null;
100     try{
101       conn = this.getTransactionConnection();
102 // String docTargetType = ExpenseAPInvoiceData.cDoctypeTarget(this, Utility.getContext(this, vars, "#User_Client", "ExpenseAPInvoice"), Utility.getContext(this, vars, "#User_Org", "ExpenseAPInvoice"));
103
for (int i = 0; i<data.length; i++){
104        String JavaDoc docTargetType = ExpenseAPInvoiceData.cDoctypeTarget(this, data[i].adClientId, data[i].adOrgId);
105        if (strcBpartnerIdOld!=data[i].cBpartnerId){
106        //insert invoice customer header
107
// checks if there are invoices not processed that full filled the requirements
108
String JavaDoc 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 JavaDoc 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.cDoctypeId(this, docTargetType);
119
String JavaDoc strDocType = ExpenseAPInvoiceData.cDoctypeTarget(this, data[i].adClientId, data[i].adOrgId);
120          //strcBpartnerLocationId = ExpenseAPInvoiceData.bPartnerLocation(this, data[i].cBpartnerId);
121
String JavaDoc strSalesrepId = ExpenseAPInvoiceData.salesrepId(this, data[i].cBpartnerId);
122          String JavaDoc strPaymentRule = ExpenseAPInvoiceData.paymentrule(this, data[i].cBpartnerId);
123          String JavaDoc strPaymentterm = ExpenseAPInvoiceData.paymentterm(this, data[i].cBpartnerId);
124
125          //strPricelistId = ExpenseAPInvoiceData.pricelistId(this, data[i].cBpartnerId);
126

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 JavaDoc strmProductUomId = ExpenseAPInvoiceData.mProductUomId(this, data[i].mProductId);
133
134        ExpenseAPInvoiceData[] dataPrice = ExpenseAPInvoiceData.selectPrice(this, data[i].mProductId, strPricelistId, strDateInvoiced);
135
136        //SLInvoiceTaxData[] dataTax = SLInvoiceTaxData.select(this, strcInvoiceId);
137
//if (data!=null && data.length!=0){
138
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 JavaDoc 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        //}
145
//checks if there are lines with the same conditions in the current invoice
146

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         //if new, calculate c_invoiceline_id and qty
152
strcInvoiceLineId = SequenceIdData.getSequence(this, "C_InvoiceLine", data[i].adClientId);
153          qty = Float.valueOf(data[i].qty);
154          String JavaDoc strLine = ExpenseAPInvoiceData.selectLine(conn, this, strcInvoiceId);
155          if (strLine.equals("")) strLine = "10";
156          line+=Integer.valueOf(strLine);
157      
158      //System.out.println("*****************+client: "+ data[i].invoiceprice.equals("")?dataPrice[0].pricestd:data[i].invoiceprice);
159
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          //if there are more lines that full filled the requirements, add the new amount to the old
164
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          //if there are acctidimensions that full filled the requirements
172
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 JavaDoc 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 JavaDoc f){
186       try {
187         releaseRollbackConnection(conn);
188       } catch (Exception JavaDoc ignored) {}
189       f.printStackTrace();
190       log4j.warn("Rollback in transaction");
191       return Utility.messageBD(this, "PriceListVersionNotFound", vars.getLanguage());
192     } catch (Exception JavaDoc e){
193       try {
194          releaseRollbackConnection(conn);
195       } catch (Exception JavaDoc 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 JavaDoc strcBpartnerId, String JavaDoc strDatereportFrom, String JavaDoc strDatereportTo, String JavaDoc strDateInvoiced) throws IOException, ServletException {
206       if (log4j.isDebugEnabled()) log4j.debug("Output: process ExpenseAPInvoice");
207       
208       String JavaDoc[] discard = {""};
209       String JavaDoc strHelp = ExpenseAPInvoiceData.help(this, "S_ExpenseAPInvoice");
210       if (strHelp.equals("")) discard[0] = new String JavaDoc("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("partner", ExpenseAPInvoiceData.bPartnerDescription(this, strcBpartnerId));
224
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 JavaDoc ex) {
232         throw new ServletException(ex);
233       }
234     
235       String JavaDoc 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                 // New interface paramenters
251
try {
252         KeyMap key = new KeyMap(this, vars, "ExpenseAPInvoice.html");
253         xmlDocument.setParameter("keyMap", key.getActionButtonKeyMaps());
254       } catch (Exception JavaDoc 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 JavaDoc 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      ////----
282

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 JavaDoc getServletInfo() {
291     return "Servlet ExpenseAPInvoice";
292   } // end of getServletInfo() method
293
}
294
295
Popular Tags