KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_callouts > SL_Order_Amt


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_callouts;
20
21 import org.openbravo.base.secureApp.HttpSecureAppServlet;
22 import org.openbravo.base.secureApp.VariablesSecureApp;
23 import org.openbravo.xmlEngine.XmlDocument;
24 import org.openbravo.utils.FormatUtilities;
25 import org.openbravo.erpCommon.utility.*;
26 import java.io.*;
27 import java.math.BigDecimal JavaDoc;
28 import javax.servlet.*;
29 import javax.servlet.http.*;
30
31 public class SL_Order_Amt extends HttpSecureAppServlet {
32   
33   static final BigDecimal JavaDoc ZERO = new BigDecimal JavaDoc(0.0);
34
35   public void init (ServletConfig config) {
36     super.init(config);
37     boolHist = false;
38   }
39
40   public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {
41     VariablesSecureApp vars = new VariablesSecureApp(request);
42     if (vars.commandIn("DEFAULT")) {
43       String JavaDoc strChanged = vars.getStringParameter("inpLastFieldChanged");
44       if (log4j.isDebugEnabled()) log4j.debug("CHANGED: " + strChanged);
45       String JavaDoc strQtyOrdered = vars.getStringParameter("inpqtyordered");
46       String JavaDoc strPriceActual = vars.getStringParameter("inppriceactual");
47       String JavaDoc strDiscount = vars.getStringParameter("inpdiscount");
48       String JavaDoc strPriceLimit = vars.getStringParameter("inppricelimit");
49       String JavaDoc strPriceList = vars.getStringParameter("inppricelist");
50       String JavaDoc strPriceStd = vars.getStringParameter("inppricestd");
51       String JavaDoc strCOrderId = vars.getStringParameter("inpcOrderId");
52       String JavaDoc strProduct = vars.getStringParameter("inpmProductId");
53       String JavaDoc strUOM = vars.getStringParameter("inpcUomId");
54       String JavaDoc strAttribute = vars.getStringParameter("inpmAttributesetinstanceId");
55       String JavaDoc strTabId = vars.getStringParameter("inpTabId");
56       String JavaDoc strQty = vars.getStringParameter("inpqtyordered");
57       
58       try {
59         printPage(response, vars, strChanged, strQtyOrdered, strPriceActual, strDiscount, strPriceLimit, strPriceList, strCOrderId, strProduct, strUOM, strAttribute, strTabId, strQty, strPriceStd);
60       } catch (ServletException ex) {
61         pageErrorCallOut(response);
62       }
63     } else pageError(response);
64   }
65
66   void printPage(HttpServletResponse response, VariablesSecureApp vars, String JavaDoc strChanged, String JavaDoc strQtyOrdered, String JavaDoc strPriceActual, String JavaDoc strDiscount, String JavaDoc strPriceLimit, String JavaDoc strPriceList, String JavaDoc strCOrderId, String JavaDoc strProduct, String JavaDoc strUOM, String JavaDoc strAttribute, String JavaDoc strTabId, String JavaDoc strQty, String JavaDoc strPriceStd) throws IOException, ServletException {
67     if (log4j.isDebugEnabled()){ log4j.debug("Output: dataSheet"); log4j.debug("CHANGED:"+strChanged);}
68
69     XmlDocument xmlDocument = xmlEngine.readXmlTemplate("org/openbravo/erpCommon/ad_callouts/CallOut").createXmlDocument();
70     SLOrderAmtData[] data = SLOrderAmtData.select(this, strCOrderId);
71     SLOrderStockData[] data1 = SLOrderStockData.select(this, strProduct);
72     String JavaDoc strPrecision = "0", strPricePrecision="0";
73     String JavaDoc strStockSecurity = "0";
74     String JavaDoc strEnforceAttribute = "N";
75     String JavaDoc Issotrx = SLOrderStockData.isSotrx(this, strCOrderId);
76     String JavaDoc strStockNoAttribute;
77     String JavaDoc strStockAttribute;
78     if (data1!=null && data1.length>0) {
79       strStockSecurity = data1[0].stock;
80       strEnforceAttribute = data1[0].enforceAttribute;
81     }
82     boolean enforcedLimit=false;
83     //boolean isUnderLimit=false;
84
if (data!=null && data.length>0) {
85       strPrecision = data[0].stdprecision.equals("")?"0":data[0].stdprecision;
86       strPricePrecision = data[0].priceprecision.equals("")?"0":data[0].priceprecision;
87       enforcedLimit = (data[0].enforcepricelimit.equals("Y")?true:false);
88     }
89     int StdPrecision = Integer.valueOf(strPrecision).intValue();
90     int PricePrecision = Integer.valueOf(strPricePrecision).intValue();
91
92     BigDecimal JavaDoc qtyOrdered, priceActual, discount, priceLimit, priceList, stockSecurity, stockNoAttribute, stockAttribute, resultStock, priceStd;
93
94     stockSecurity = new BigDecimal JavaDoc (strStockSecurity);
95     qtyOrdered = (strQtyOrdered.equals("")?ZERO:new BigDecimal JavaDoc(strQtyOrdered));
96     priceActual = (strPriceActual.equals("")?ZERO:(new BigDecimal JavaDoc(strPriceActual))).setScale(PricePrecision, BigDecimal.ROUND_HALF_UP);
97     discount = (strDiscount.equals("")?ZERO:new BigDecimal JavaDoc(strDiscount));
98     priceLimit = (strPriceLimit.equals("")?ZERO:(new BigDecimal JavaDoc(strPriceLimit))).setScale(PricePrecision, BigDecimal.ROUND_HALF_UP);
99     priceList = (strPriceList.equals("")?ZERO:new BigDecimal JavaDoc(strPriceList));
100     priceStd = (strPriceStd.equals("")?ZERO:new BigDecimal JavaDoc(strPriceStd));
101     /*if (enforcedLimit) {
102       String strPriceVersion = "";
103       PriceListVersionComboData[] data1 = PriceListVersionComboData.selectActual(this, data[0].mPricelistId, DateTimeData.today(this));
104       if (data1!=null && data1.length>0) strPriceVersion = data1[0].mPricelistVersionId;
105       BigDecimal lineLimit = new BigDecimal(SLOrderAmtData.selectPriceLimit(this, strPriceVersion, strProduct));
106       if (lineLimit.floatValue() >priceActual.floatValue()) isUnderLimit=true;
107     }*/

108
109
110     StringBuffer JavaDoc resultado = new StringBuffer JavaDoc();
111     resultado.append("var calloutName='SL_Order_Amt';\n\n");
112     resultado.append("var respuesta = new Array(");
113
114     //Calculating prices for offers...
115
SLOrderProductData[] dataOrder = SLOrderProductData.select(this, strCOrderId);
116     if (strChanged.equals("inppriceactual"))
117     {
118       if (log4j.isDebugEnabled()) log4j.debug("priceActual:" + Double.toString(priceActual.doubleValue()));
119       priceStd = new BigDecimal JavaDoc(SLOrderProductData.getOffersStdPrice(this, dataOrder[0].cBpartnerId, strPriceActual.replace("\"", ""), strProduct, dataOrder[0].dateordered, strQty, dataOrder[0].mPricelistId, dataOrder[0].id));
120      // priceList
121
resultado.append("new Array(\"inppricestd\", \"" + priceStd.toString() + "\"),");
122     }
123
124 /*
125     if (strChanged.equals("inppricelist")||strChanged.equals("inpqtyordered"))
126     {
127       if (log4j.isDebugEnabled()) log4j.debug("priceList:" + Double.toString(priceList.doubleValue()));
128       priceActual = new BigDecimal(SLOrderProductData.getOffersPrice(this, dataOrder[0].dateordered, dataOrder[0].cBpartnerId, strProduct, strPriceList.replace("\"", ""), strQty, dataOrder[0].mPricelistId, dataOrder[0].id));
129       resultado.append("new Array(\"inppriceactual\", \"" + priceActual.toString() + "\"),");
130     }
131 */

132
133     //calculating discount
134
if (strChanged.equals("inppricelist")||strChanged.equals("inppriceactual")) {
135       if (priceList.doubleValue() == 0.0) discount = ZERO;
136       else {
137         if (log4j.isDebugEnabled()) log4j.debug("pricelist:" + Double.toString(priceList.doubleValue()));
138         if (log4j.isDebugEnabled()) log4j.debug("priceActual:" + Double.toString(priceActual.doubleValue()));
139         discount = new BigDecimal JavaDoc ((priceList.doubleValue()-priceStd.doubleValue()) / priceList.doubleValue() * 100.0);
140       }
141       if (log4j.isDebugEnabled()) log4j.debug("Discount: " + discount.toString());
142       if (discount.scale() > StdPrecision) discount = discount.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP);
143       if (log4j.isDebugEnabled()) log4j.debug("Discount rounded: " + discount.toString());
144       resultado.append("new Array(\"inpdiscount\", \"" + discount.toString() + "\"),");
145     } else if (strChanged.equals("inpqtyordered")) { // calculate Actual
146
priceActual = new BigDecimal JavaDoc(SLOrderProductData.getOffersPrice(this, dataOrder[0].dateordered, dataOrder[0].cBpartnerId, strProduct, priceStd.toString(), strQty, dataOrder[0].mPricelistId, dataOrder[0].id));
147       if (priceActual.scale() > PricePrecision) priceActual = priceActual.setScale(PricePrecision, BigDecimal.ROUND_HALF_UP);
148       resultado.append("new Array(\"inppriceactual\", \"" + priceActual.toString() + "\"),");
149     } else if (strChanged.equals("inpdiscount")){ //calculate std and actual
150
BigDecimal JavaDoc discount1 = null;
151       if (priceList.doubleValue() != 0) discount1 = new BigDecimal JavaDoc ((priceList.doubleValue()-priceStd.doubleValue()) / priceList.doubleValue() * 100.0).setScale(StdPrecision, BigDecimal.ROUND_HALF_UP);
152       else discount1 = new BigDecimal JavaDoc(0);
153       BigDecimal JavaDoc discount2 = discount.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP);
154       if (discount1.compareTo(discount2)!=0) //checks if rounded discount has changed
155
{
156         priceStd = new BigDecimal JavaDoc (priceList.doubleValue()-(priceList.doubleValue()*discount.doubleValue()/100) );
157         priceActual = new BigDecimal JavaDoc(SLOrderProductData.getOffersPrice(this, dataOrder[0].dateordered, dataOrder[0].cBpartnerId, strProduct, priceStd.toString(), strQty, dataOrder[0].mPricelistId, dataOrder[0].id));
158         if (priceStd.scale() > PricePrecision) priceStd = priceStd.setScale(PricePrecision, BigDecimal.ROUND_HALF_UP);
159         if (priceActual.scale() > PricePrecision) priceActual = priceActual.setScale(PricePrecision, BigDecimal.ROUND_HALF_UP);
160         resultado.append("new Array(\"inppriceactual\", \"" + priceActual.toString() + "\"),");
161         resultado.append("new Array(\"inppricestd\", \"" + priceStd.toString() + "\"),");
162       }
163     }
164
165
166     if (Issotrx.equals("Y")) {
167       if (!strStockSecurity.equals("0")) {
168           if (qtyOrdered.doubleValue() != 0.0) {
169               if (strEnforceAttribute.equals("N")) {
170                 strStockNoAttribute = SLOrderStockData.totalStockNoAttribute(this, strProduct, strUOM);
171                 stockNoAttribute = new BigDecimal JavaDoc (strStockNoAttribute);
172                 resultStock = new BigDecimal JavaDoc (stockNoAttribute.doubleValue() - qtyOrdered.doubleValue());
173                 if (stockSecurity.doubleValue() > resultStock.doubleValue()) {
174                   resultado.append("new Array('MESSAGE', \"" + FormatUtilities.replaceJS(Utility.messageBD(this, "StockLimit", vars.getLanguage())) + "\"),");
175                 }
176               } else {
177                 if (!strAttribute.equals("") && strAttribute != null) {
178                   strStockAttribute = SLOrderStockData.totalStockAttribute(this, strProduct, strUOM, strAttribute);
179                   stockAttribute = new BigDecimal JavaDoc (strStockAttribute);
180                   resultStock = new BigDecimal JavaDoc (stockAttribute.doubleValue() - qtyOrdered.doubleValue());
181                   if (stockSecurity.doubleValue() > resultStock.doubleValue()) {
182                   resultado.append("new Array('MESSAGE', \"" + FormatUtilities.replaceJS(Utility.messageBD(this, "StockLimit", vars.getLanguage())) + "\"),");
183                   }
184                 }
185               }
186           }
187       }
188     }
189     if (log4j.isDebugEnabled()) log4j.debug(resultado.toString());
190     if (!strChanged.equals("inpqtyordered")) { // Check PriceLimit
191
boolean enforced = SLOrderAmtData.listPriceType(this, strPriceList);
192       // Check Price Limit?
193
if (enforced && priceLimit.doubleValue() != 0.0 && priceActual.compareTo(priceLimit) < 0) resultado.append("new Array('MESSAGE', \"" + Utility.messageBD(this, "UnderLimitPrice", vars.getLanguage()) + "\")");
194     }
195
196     // Multiply
197
BigDecimal JavaDoc lineNetAmt = qtyOrdered.multiply(priceActual);
198     if (lineNetAmt.scale() > StdPrecision) lineNetAmt = lineNetAmt.setScale(StdPrecision, BigDecimal.ROUND_HALF_UP);
199     resultado.append("new Array(\"inplinenetamt\", \"" + lineNetAmt.toString() + "\")");
200
201     resultado.append(");");
202     xmlDocument.setParameter("array", resultado.toString());
203     xmlDocument.setParameter("frameName", "frameAplicacion");
204     response.setContentType("text/html; charset=UTF-8");
205     PrintWriter out = response.getWriter();
206     out.println(xmlDocument.print());
207     out.close();
208   }
209 }
210
Popular Tags