KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_actionButton > CopyFromOrder


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_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 JavaDoc;
29 import java.io.*;
30 import java.util.*;
31 import javax.servlet.*;
32 import javax.servlet.http.*;
33
34 // imports for transactions
35
import java.sql.Connection JavaDoc;
36
37 import org.openbravo.erpCommon.utility.DateTimeData;
38
39 public class CopyFromOrder extends HttpSecureAppServlet {
40   static final BigDecimal JavaDoc ZERO = new BigDecimal JavaDoc(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 JavaDoc strKey = vars.getGlobalVariable("inpcOrderId", "CopyFromOrder|mInoutId");
52       String JavaDoc strWindowId = vars.getGlobalVariable("inpwindowId", "CopyFromOrder|windowId", "");
53       String JavaDoc strTabName = vars.getGlobalVariable("inpTabId", "CopyFromOrder|adTabId", "");
54       String JavaDoc strBpartner = vars.getGlobalVariable("inpcBpartnerId", "CopyFromOrder|cBpartnerId", "");
55       String JavaDoc 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 JavaDoc strWindowId = vars.getGlobalVariable("inpWindowId", "CopyFromOrder|windowId");
61       String JavaDoc strSOTrx = Utility.getContext(this, vars, "isSOTrx", strWindowId);
62       String JavaDoc strKey = vars.getGlobalVariable("inpcOrderId", "CopyFromOrder|mInoutId");
63       String JavaDoc strTabId = vars.getGlobalVariable("inpTabId", "CopyFromOrder|adTabId");
64       String JavaDoc strBpartner = vars.getGlobalVariable("inpcBpartnerId", "CopyFromOrder|cBpartnerId", "");
65       String JavaDoc 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 JavaDoc strRownum = vars.getRequiredInStringParameter("inpRownumId");
74       String JavaDoc strKey = vars.getRequiredStringParameter("inpcOrderId");
75       String JavaDoc strWindowId = vars.getStringParameter("inpWindowId");
76       String JavaDoc strSOTrx = vars.getStringParameter("inpissotrx");
77       String JavaDoc strTabId = vars.getStringParameter("inpTabId");
78       String JavaDoc strMessage = copyLines(vars, strRownum, strKey, strWindowId, strSOTrx);
79       ActionButtonDefaultData[] tab = ActionButtonDefaultData.windowName(this, strTabId);
80       String JavaDoc 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 JavaDoc copyLines(VariablesSecureApp vars, String JavaDoc strRownum, String JavaDoc strKey, String JavaDoc strWindowId, String JavaDoc strSOTrx)
111       throws IOException, ServletException {
112
113     String JavaDoc strMessageResult = "";
114     int total = 0;
115     if (strRownum.equals("")) return "";
116     Connection JavaDoc 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 JavaDoc strmProductId = vars.getStringParameter("inpmProductId" + strRownum);
126           String JavaDoc strmAttributesetinstanceId = vars.getStringParameter("inpmAttributesetinstanceId" + strRownum);
127           String JavaDoc strPricestd = vars.getStringParameter("inpmPricestdvalue" + strRownum);
128           String JavaDoc strLastpriceso = vars.getStringParameter("inpLastpriceso" + strRownum);
129           String JavaDoc strQty = vars.getStringParameter("inpquantity" + strRownum);
130           String JavaDoc strcTaxId = vars.getStringParameter("inpcTaxId" + strRownum);
131           String JavaDoc strcUOMId = vars.getStringParameter("inpcUOMId" + strRownum);
132           String JavaDoc 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 JavaDoc e){
147       try {
148         releaseRollbackConnection(conn);
149       } catch (Exception JavaDoc 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 JavaDoc strKey, String JavaDoc strWindowId, String JavaDoc strTabId, String JavaDoc strSOTrx, String JavaDoc strBpartner, String JavaDoc 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 JavaDoc strInvoicing = CopyFromOrderRecordData.invoicing(this, strSOTrx, strBpartner, dataOrder[0].adOrgId, dataOrder[0].adClientId);
179     String JavaDoc 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 JavaDoc invoicing, total, totalAverage;
184
185     invoicing = (strInvoicing.equals("")?ZERO:(new BigDecimal JavaDoc(strInvoicing)));
186     total = (strTotal.equals("")?ZERO:new BigDecimal JavaDoc(strTotal));
187     String JavaDoc strTotalAverage = "";
188     if (total==ZERO) {
189       totalAverage = new BigDecimal JavaDoc (invoicing.doubleValue() / total.doubleValue() * 100.0);
190       totalAverage = totalAverage.setScale(2, BigDecimal.ROUND_HALF_UP);
191       strTotalAverage = totalAverage.toString();
192       //int intscale = totalAverage.scale();
193
}
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 JavaDoc getServletInfo() {
207     return "Servlet Copy from order";
208   } // end of getServletInfo() method
209
}
210
211
Popular Tags