src/org/openbravo/erpCommon/ad_callouts/SL_Order_Amt.java
author Guillermo Alvarez de Eulate <guillermo.alvarez@openbravo.com>
Thu, 06 Feb 2014 11:27:59 +0100
changeset 21925 c385f03ea176
parent 21853 6528ffadbc93
child 22006 34cb4c191d5e
permissions -rw-r--r--
fixed issue 25441: Price is set fine using unit price when qty is changed in orderline
carlos@0
     1
/*
carlos@0
     2
 *************************************************************************
carlos@0
     3
 * The contents of this file are subject to the Openbravo  Public  License
priya@9072
     4
 * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
carlos@0
     5
 * Version 1.1  with a permitted attribution clause; you may not  use this
carlos@0
     6
 * file except in compliance with the License. You  may  obtain  a copy of
carlos@0
     7
 * the License at http://www.openbravo.com/legal/license.html 
carlos@0
     8
 * Software distributed under the License  is  distributed  on  an "AS IS"
carlos@0
     9
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
carlos@0
    10
 * License for the specific  language  governing  rights  and  limitations
carlos@0
    11
 * under the License. 
carlos@0
    12
 * The Original Code is Openbravo ERP. 
ggi@6701
    13
 * The Initial Developer of the Original Code is Openbravo SLU 
gaurav@21853
    14
 * All portions are Copyright (C) 2001-2014 Openbravo SLU 
carlos@0
    15
 * All Rights Reserved. 
carlos@0
    16
 * Contributor(s):  ______________________________________.
carlos@0
    17
 ************************************************************************
martin@2462
    18
 */
carlos@0
    19
package org.openbravo.erpCommon.ad_callouts;
carlos@0
    20
martin@2598
    21
import java.io.IOException;
martin@2598
    22
import java.io.PrintWriter;
martin@2598
    23
import java.math.BigDecimal;
gorkaion@17345
    24
import java.math.RoundingMode;
martin@2598
    25
martin@2598
    26
import javax.servlet.ServletConfig;
martin@2598
    27
import javax.servlet.ServletException;
martin@2598
    28
import javax.servlet.http.HttpServletRequest;
martin@2598
    29
import javax.servlet.http.HttpServletResponse;
martin@2598
    30
carlos@0
    31
import org.openbravo.base.secureApp.HttpSecureAppServlet;
carlos@0
    32
import org.openbravo.base.secureApp.VariablesSecureApp;
guillermo@21276
    33
import org.openbravo.base.weld.WeldUtils;
guillermo@21276
    34
import org.openbravo.common.hooks.OrderLineQtyChangedHookManager;
guillermo@21276
    35
import org.openbravo.common.hooks.OrderLineQtyChangedHookObject;
hari@17319
    36
import org.openbravo.dal.service.OBDal;
asier@19229
    37
import org.openbravo.erpCommon.businessUtility.PriceAdjustment;
martin@2598
    38
import org.openbravo.erpCommon.utility.Utility;
gorkaion@17372
    39
import org.openbravo.financial.FinancialUtils;
asier@19229
    40
import org.openbravo.model.common.order.Order;
asier@19229
    41
import org.openbravo.model.common.plm.Product;
gorkaion@17360
    42
import org.openbravo.model.pricing.pricelist.PriceList;
martin@2598
    43
import org.openbravo.utils.FormatUtilities;
carlos@0
    44
import org.openbravo.xmlEngine.XmlDocument;
carlos@0
    45
carlos@0
    46
public class SL_Order_Amt extends HttpSecureAppServlet {
martin@2868
    47
  private static final long serialVersionUID = 1L;
carlos@0
    48
gorkaion@17482
    49
  private static final BigDecimal ZERO = BigDecimal.ZERO;
carlos@0
    50
victor@7431
    51
  @Override
martin@2868
    52
  public void init(ServletConfig config) {
martin@2868
    53
    super.init(config);
martin@2868
    54
    boolHist = false;
martin@2868
    55
  }
martin@2868
    56
victor@7431
    57
  @Override
martin@2868
    58
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,
martin@2868
    59
      ServletException {
martin@2868
    60
    VariablesSecureApp vars = new VariablesSecureApp(request);
martin@2868
    61
    if (vars.commandIn("DEFAULT")) {
martin@2868
    62
      String strChanged = vars.getStringParameter("inpLastFieldChanged");
gorkaion@17482
    63
      log4j.debug("CHANGED: " + strChanged);
ivan@4712
    64
      String strQtyOrdered = vars.getNumericParameter("inpqtyordered");
ivan@4712
    65
      String strPriceActual = vars.getNumericParameter("inppriceactual");
ivan@4712
    66
      String strDiscount = vars.getNumericParameter("inpdiscount");
ivan@4712
    67
      String strPriceLimit = vars.getNumericParameter("inppricelimit");
ivan@4712
    68
      String strPriceList = vars.getNumericParameter("inppricelist");
ivan@4712
    69
      String strPriceStd = vars.getNumericParameter("inppricestd");
martin@2868
    70
      String strCOrderId = vars.getStringParameter("inpcOrderId");
martin@2868
    71
      String strProduct = vars.getStringParameter("inpmProductId");
martin@2868
    72
      String strUOM = vars.getStringParameter("inpcUomId");
martin@2868
    73
      String strAttribute = vars.getStringParameter("inpmAttributesetinstanceId");
ivan@4712
    74
      String strQty = vars.getNumericParameter("inpqtyordered");
asier@19229
    75
      boolean cancelPriceAd = "Y".equals(vars.getStringParameter("inpcancelpricead"));
victor@7431
    76
      String strLineNetAmt = vars.getNumericParameter("inplinenetamt");
meeravalli@17308
    77
      String strTaxId = vars.getStringParameter("inpcTaxId");
hari@17319
    78
      String strGrossUnitPrice = vars.getNumericParameter("inpgrossUnitPrice");
gorkaion@17485
    79
      String strGrossPriceList = vars.getNumericParameter("inpgrosspricelist");
david@18197
    80
      String strGrossBaseUnitPrice = vars.getNumericParameter("inpgrosspricestd");
hari@17324
    81
      String strtaxbaseamt = vars.getNumericParameter("inptaxbaseamt");
martin@2868
    82
martin@2868
    83
      try {
martin@2868
    84
        printPage(response, vars, strChanged, strQtyOrdered, strPriceActual, strDiscount,
gorkaion@17482
    85
            strPriceLimit, strPriceList, strCOrderId, strProduct, strUOM, strAttribute, strQty,
gorkaion@17485
    86
            strPriceStd, cancelPriceAd, strLineNetAmt, strTaxId, strGrossUnitPrice,
david@18197
    87
            strGrossPriceList, strtaxbaseamt, strGrossBaseUnitPrice);
martin@2868
    88
      } catch (ServletException ex) {
martin@2868
    89
        pageErrorCallOut(response);
martin@2868
    90
      }
martin@2868
    91
    } else
martin@2868
    92
      pageError(response);
martin@2868
    93
  }
martin@2868
    94
stefan@3960
    95
  private void printPage(HttpServletResponse response, VariablesSecureApp vars, String strChanged,
gorkaion@17485
    96
      String strQtyOrdered, String _strPriceActual, String strDiscount, String strPriceLimit,
gorkaion@17482
    97
      String strPriceList, String strCOrderId, String strProduct, String strUOM,
asier@19229
    98
      String strAttribute, String strQty, String strPriceStd, boolean cancelPriceAd,
gorkaion@17485
    99
      String strLineNetAmt, String strTaxId, String strGrossUnitPrice, String strGrossPriceList,
david@18197
   100
      String strTaxBaseAmt, String strGrossBaseUnitPrice) throws IOException, ServletException {
martin@2868
   101
    XmlDocument xmlDocument = xmlEngine.readXmlTemplate(
martin@2868
   102
        "org/openbravo/erpCommon/ad_callouts/CallOut").createXmlDocument();
guillermo@21276
   103
martin@2868
   104
    SLOrderAmtData[] data = SLOrderAmtData.select(this, strCOrderId);
martin@2868
   105
    SLOrderStockData[] data1 = SLOrderStockData.select(this, strProduct);
martin@2868
   106
    String strPrecision = "0", strPricePrecision = "0";
martin@2868
   107
    String strStockSecurity = "0";
martin@2868
   108
    String strEnforceAttribute = "N";
gorkaion@17482
   109
    String isSOTrx = SLOrderStockData.isSotrx(this, strCOrderId);
martin@2868
   110
    String strStockNoAttribute;
martin@2868
   111
    String strStockAttribute;
gorkaion@17485
   112
    String strPriceActual = _strPriceActual;
guillermo@21925
   113
    PriceList currentPriceList = OBDal.getInstance().get(PriceList.class, data[0].mPricelistId);
guillermo@21925
   114
    boolean isTaxIncludedPriceList = currentPriceList.isPriceIncludesTax();
gorkaion@17496
   115
    boolean isGrossUnitPriceChanged = strChanged.equals("inpgrossUnitPrice");
guillermo@21276
   116
    boolean forceSetPriceStd = false;
martin@2868
   117
    if (data1 != null && data1.length > 0) {
martin@2868
   118
      strStockSecurity = data1[0].stock;
martin@2868
   119
      strEnforceAttribute = data1[0].enforceAttribute;
martin@2868
   120
    }
martin@2868
   121
    // boolean isUnderLimit=false;
martin@2868
   122
    if (data != null && data.length > 0) {
martin@2868
   123
      strPrecision = data[0].stdprecision.equals("") ? "0" : data[0].stdprecision;
martin@2868
   124
      strPricePrecision = data[0].priceprecision.equals("") ? "0" : data[0].priceprecision;
martin@2868
   125
    }
gorkaion@17482
   126
    int stdPrecision = Integer.valueOf(strPrecision).intValue();
gorkaion@17482
   127
    int pricePrecision = Integer.valueOf(strPricePrecision).intValue();
carlos@0
   128
gorkaion@17485
   129
    BigDecimal qtyOrdered, priceActual, priceLimit, netPriceList, stockSecurity, stockNoAttribute, stockAttribute, resultStock, priceStd, lineNetAmt, taxBaseAmt;
martin@2868
   130
    stockSecurity = new BigDecimal(strStockSecurity);
martin@2868
   131
    qtyOrdered = (strQtyOrdered.equals("") ? ZERO : new BigDecimal(strQtyOrdered));
jon@15245
   132
    priceActual = (strPriceActual.equals("") ? ZERO : (new BigDecimal(strPriceActual))).setScale(
gorkaion@17482
   133
        pricePrecision, BigDecimal.ROUND_HALF_UP);
martin@2868
   134
    priceLimit = (strPriceLimit.equals("") ? ZERO : (new BigDecimal(strPriceLimit))).setScale(
gorkaion@17482
   135
        pricePrecision, BigDecimal.ROUND_HALF_UP);
gorkaion@17485
   136
    netPriceList = (strPriceList.equals("") ? ZERO : (new BigDecimal(strPriceList))).setScale(
gorkaion@17482
   137
        pricePrecision, BigDecimal.ROUND_HALF_UP);
jon@15245
   138
    priceStd = (strPriceStd.equals("") ? ZERO : (new BigDecimal(strPriceStd))).setScale(
gorkaion@17482
   139
        pricePrecision, BigDecimal.ROUND_HALF_UP);
gorkaion@17482
   140
    lineNetAmt = (strLineNetAmt.equals("") ? ZERO : (new BigDecimal(strLineNetAmt))).setScale(
gorkaion@17482
   141
        pricePrecision, BigDecimal.ROUND_HALF_UP);
hari@17324
   142
    taxBaseAmt = (strTaxBaseAmt.equals("") ? ZERO : (new BigDecimal(strTaxBaseAmt))).setScale(
gorkaion@17482
   143
        pricePrecision, BigDecimal.ROUND_HALF_UP);
gorkaion@17485
   144
    BigDecimal grossUnitPrice = (strGrossUnitPrice.equals("") ? ZERO : new BigDecimal(
gorkaion@17485
   145
        strGrossUnitPrice).setScale(pricePrecision, BigDecimal.ROUND_HALF_UP));
guillermo@21276
   146
gorkaion@17485
   147
    BigDecimal grossPriceList = (strGrossPriceList.equals("") ? ZERO : new BigDecimal(
gorkaion@17485
   148
        strGrossPriceList).setScale(pricePrecision, BigDecimal.ROUND_HALF_UP));
david@18197
   149
    BigDecimal grossBaseUnitPrice = (strGrossBaseUnitPrice.equals("") ? ZERO : new BigDecimal(
david@18197
   150
        strGrossBaseUnitPrice).setScale(pricePrecision, BigDecimal.ROUND_HALF_UP));
martin@2868
   151
guillermo@21276
   152
    // A hook has been created. This hook will be raised when the qty is changed having selected a
guillermo@21276
   153
    // product
guillermo@21276
   154
    if (!strProduct.equals("") && strChanged.equals("inpqtyordered")) {
guillermo@21276
   155
      try {
guillermo@21276
   156
        OrderLineQtyChangedHookObject hookObject = new OrderLineQtyChangedHookObject();
guillermo@21276
   157
        hookObject.setProductId(strProduct);
guillermo@21276
   158
        hookObject.setQty(qtyOrdered);
guillermo@21276
   159
        hookObject.setOrderId(strCOrderId);
guillermo@21276
   160
        hookObject.setPricePrecision(pricePrecision);
guillermo@21925
   161
        hookObject.setPriceList(currentPriceList);
guillermo@21276
   162
        if (isTaxIncludedPriceList) {
guillermo@21925
   163
          hookObject.setListPrice(grossPriceList);
guillermo@21925
   164
          hookObject.setPrice(grossBaseUnitPrice);
guillermo@21276
   165
        } else {
guillermo@21925
   166
          hookObject.setListPrice(netPriceList);
guillermo@21925
   167
          hookObject.setPrice(priceStd);
guillermo@21276
   168
        }
guillermo@21276
   169
guillermo@21276
   170
        hookObject.setChanged(strChanged);
guillermo@21276
   171
        WeldUtils.getInstanceFromStaticBeanManager(OrderLineQtyChangedHookManager.class)
guillermo@21276
   172
            .executeHooks(hookObject);
guillermo@21276
   173
        if (isTaxIncludedPriceList) {
guillermo@21276
   174
          if (grossBaseUnitPrice.compareTo(hookObject.getPrice()) != 0) {
guillermo@21276
   175
            grossBaseUnitPrice = hookObject.getPrice();
guillermo@21276
   176
            isGrossUnitPriceChanged = true;
guillermo@21276
   177
          }
guillermo@21276
   178
        } else {
guillermo@21276
   179
          if (priceStd.compareTo(hookObject.getPrice()) != 0) {
guillermo@21276
   180
            priceStd = hookObject.getPrice();
guillermo@21276
   181
            forceSetPriceStd = true;
guillermo@21276
   182
          }
guillermo@21276
   183
        }
guillermo@21276
   184
      } catch (Exception e) {
guillermo@21276
   185
        // TODO Auto-generated catch block
guillermo@21276
   186
        e.printStackTrace();
guillermo@21276
   187
      }
guillermo@21276
   188
    }
martin@2868
   189
    StringBuffer resultado = new StringBuffer();
martin@2868
   190
    resultado.append("var calloutName='SL_Order_Amt';\n\n");
martin@2868
   191
    resultado.append("var respuesta = new Array(");
martin@2868
   192
asier@19229
   193
    Order order = OBDal.getInstance().get(Order.class, strCOrderId);
asier@19229
   194
    Product product = OBDal.getInstance().get(Product.class, strProduct);
asier@19229
   195
david@6474
   196
    if (strChanged.equals("inplinenetamt")) {
gorkaion@17482
   197
      priceActual = lineNetAmt.divide(qtyOrdered, pricePrecision, BigDecimal.ROUND_HALF_UP);
gorkaion@17482
   198
      if (priceActual.compareTo(BigDecimal.ZERO) == 0) {
gorkaion@17482
   199
        lineNetAmt = BigDecimal.ZERO;
gorkaion@17482
   200
      }
david@6474
   201
    }
asier@19229
   202
asier@19229
   203
    if (strChanged.equals("inpqtyordered") && !cancelPriceAd) {
asier@19229
   204
      if (isTaxIncludedPriceList) {
asier@19229
   205
        grossUnitPrice = PriceAdjustment.calculatePriceActual(order, product, qtyOrdered,
asier@19229
   206
            grossBaseUnitPrice);
asier@19229
   207
        BigDecimal grossAmount = grossUnitPrice.multiply(qtyOrdered).setScale(stdPrecision,
asier@19229
   208
            RoundingMode.HALF_UP);
asier@19229
   209
        priceActual = FinancialUtils.calculateNetFromGross(strTaxId, grossAmount, pricePrecision,
asier@19229
   210
            taxBaseAmt, qtyOrdered);
asier@19229
   211
        resultado.append("new Array(\"inpgrossUnitPrice\", " + grossUnitPrice.toString() + "),");
asier@19229
   212
      } else {
asier@19229
   213
        priceActual = PriceAdjustment.calculatePriceActual(order, product, qtyOrdered, priceStd);
asier@19229
   214
      }
asier@19229
   215
      resultado.append("new Array(\"inppriceactual\", " + priceActual + "),");
asier@19229
   216
    }
martin@2868
   217
    // Calculating prices for offers...
guillermo@21276
   218
    if (strChanged.equals("inppriceactual") || strChanged.equals("inplinenetamt")
guillermo@21276
   219
        || forceSetPriceStd) {
gorkaion@17485
   220
      log4j.debug("priceActual:" + priceActual.toString());
asier@19229
   221
      if (!cancelPriceAd) {
asier@19229
   222
        priceStd = PriceAdjustment.calculatePriceStd(order, product, qtyOrdered, priceActual);
asier@19229
   223
      } else {
asier@19229
   224
        priceStd = priceActual;
asier@19229
   225
      }
asier@18356
   226
      resultado.append("new Array(\"inppricestd\", " + priceStd.toString() + "),");
gorka@3491
   227
    }
gorka@3491
   228
gorka@3491
   229
    if (strChanged.equals("inpcancelpricead")) {
asier@19229
   230
      if (cancelPriceAd) {
adrianromero@6817
   231
        resultado.append("new Array(\"inppriceactual\", " + strPriceStd + "),");
gorka@3491
   232
      }
carlos@0
   233
    }
carlos@0
   234
asier@18360
   235
    // if taxinclusive field is changed then modify net unit price and gross price
asier@18360
   236
    if (isGrossUnitPriceChanged || (strChanged.equals("inpcTaxId") && isTaxIncludedPriceList)) {
asier@18360
   237
      BigDecimal grossAmount = grossUnitPrice.multiply(qtyOrdered).setScale(stdPrecision,
asier@18360
   238
          RoundingMode.HALF_UP);
asier@18360
   239
asier@18360
   240
      final BigDecimal netUnitPrice = FinancialUtils.calculateNetFromGross(strTaxId, grossAmount,
asier@18360
   241
          pricePrecision, taxBaseAmt, qtyOrdered);
asier@18360
   242
asier@18360
   243
      priceActual = netUnitPrice;
asier@18360
   244
      priceStd = netUnitPrice;
asier@18360
   245
      grossBaseUnitPrice = grossUnitPrice;
asier@18360
   246
      resultado.append("new Array(\"inpgrosspricestd\", " + grossBaseUnitPrice.toString() + "),");
asier@18360
   247
asier@19229
   248
      resultado.append("new Array(\"inppriceactual\"," + priceActual.toString() + "),");
gaurav@21853
   249
      resultado.append("new Array(\"inppricelist\"," + netUnitPrice.toString() + "),");
asier@18360
   250
      resultado.append("new Array(\"inppricelimit\", " + netUnitPrice.toString() + "),");
asier@18360
   251
      resultado.append("new Array(\"inppricestd\"," + netUnitPrice.toString() + "),");
asier@18360
   252
    }
asier@18360
   253
asier@19229
   254
    if (isGrossUnitPriceChanged || (strChanged.equals("inpcTaxId") && isTaxIncludedPriceList)) {
asier@19229
   255
      BigDecimal grossAmount = grossUnitPrice.multiply(qtyOrdered).setScale(stdPrecision,
asier@19229
   256
          RoundingMode.HALF_UP);
asier@19229
   257
asier@19229
   258
      final BigDecimal netUnitPrice = FinancialUtils.calculateNetFromGross(strTaxId, grossAmount,
asier@19229
   259
          pricePrecision, taxBaseAmt, qtyOrdered);
asier@19229
   260
asier@19229
   261
      priceActual = netUnitPrice;
asier@19229
   262
asier@19229
   263
      if (cancelPriceAd) {
asier@19229
   264
        grossBaseUnitPrice = grossUnitPrice;
asier@19229
   265
        priceStd = netUnitPrice;
asier@19229
   266
      } else {
asier@19229
   267
        grossBaseUnitPrice = PriceAdjustment.calculatePriceStd(order, product, qtyOrdered,
asier@19229
   268
            grossUnitPrice);
asier@19229
   269
        BigDecimal baseGrossAmount = grossBaseUnitPrice.multiply(qtyOrdered).setScale(stdPrecision,
asier@19229
   270
            RoundingMode.HALF_UP);
asier@19229
   271
        priceStd = FinancialUtils.calculateNetFromGross(strTaxId, baseGrossAmount, pricePrecision,
asier@19229
   272
            taxBaseAmt, qtyOrdered);
asier@19229
   273
      }
asier@19229
   274
asier@19229
   275
      resultado.append("new Array(\"inpgrosspricestd\", " + grossBaseUnitPrice.toString() + "),");
asier@19229
   276
asier@19229
   277
      resultado.append("new Array(\"inppriceactual\"," + priceActual.toString() + "),");
gaurav@21853
   278
      resultado.append("new Array(\"inppricelist\"," + netUnitPrice.toString() + "),");
asier@19229
   279
      resultado.append("new Array(\"inppricelimit\", " + netUnitPrice.toString() + "),");
asier@19229
   280
      resultado.append("new Array(\"inppricestd\"," + priceStd.toString() + "),");
asier@19229
   281
    }
asier@19229
   282
martin@2868
   283
    // calculating discount
david@6474
   284
    if (strChanged.equals("inppricelist") || strChanged.equals("inppriceactual")
gorkaion@17485
   285
        || strChanged.equals("inplinenetamt") || strChanged.equals("inpgrosspricelist")
guillermo@21276
   286
        || strChanged.equals("inpgrossUnitPrice") || strChanged.equals("inpqtyordered")) {
gorkaion@17485
   287
      BigDecimal priceList = BigDecimal.ZERO;
gorkaion@17485
   288
      BigDecimal unitPrice = BigDecimal.ZERO;
gorkaion@17485
   289
      BigDecimal discount;
gorkaion@17485
   290
gorkaion@17485
   291
      if (isTaxIncludedPriceList) {
gorkaion@17485
   292
        priceList = grossPriceList;
asier@18358
   293
        unitPrice = grossBaseUnitPrice;
gorkaion@17485
   294
      } else {
gorkaion@17485
   295
        priceList = netPriceList;
gorkaion@17485
   296
        unitPrice = priceStd;
gorkaion@17485
   297
      }
gorkaion@17482
   298
      if (priceList.compareTo(BigDecimal.ZERO) == 0) {
martin@2868
   299
        discount = ZERO;
gorkaion@17482
   300
      } else {
gorkaion@17485
   301
        log4j.debug("pricelist:" + priceList.toString());
gorkaion@17485
   302
        log4j.debug("unit price:" + unitPrice.toString());
asier@18356
   303
        discount = priceList.subtract(unitPrice).multiply(new BigDecimal("100"))
asier@18356
   304
            .divide(priceList, stdPrecision, BigDecimal.ROUND_HALF_EVEN);
gorkaion@17482
   305
      }
gorkaion@17482
   306
      log4j.debug("Discount rounded: " + discount.toString());
ivan@4801
   307
      resultado.append("new Array(\"inpdiscount\", " + discount.toString() + "),");
gorkaion@17485
   308
gorka@4619
   309
    } else if (strChanged.equals("inpdiscount")) { // calculate std and actual
gorkaion@17485
   310
      BigDecimal origDiscount = null;
gorkaion@17485
   311
      BigDecimal priceList;
gorkaion@17485
   312
      if (isTaxIncludedPriceList) {
gorkaion@17485
   313
        priceList = grossPriceList;
gorkaion@17485
   314
      } else {
gorkaion@17485
   315
        priceList = netPriceList;
gorkaion@17485
   316
      }
gorkaion@17482
   317
      if (priceList.compareTo(BigDecimal.ZERO) != 0) {
david@18197
   318
        BigDecimal baseUnitPrice = BigDecimal.ZERO;
gorkaion@17485
   319
        if (isTaxIncludedPriceList) {
david@18197
   320
          baseUnitPrice = grossBaseUnitPrice;
gorkaion@17485
   321
        } else {
david@18197
   322
          baseUnitPrice = priceStd;
gorkaion@17485
   323
        }
david@18197
   324
        origDiscount = priceList.subtract(baseUnitPrice).multiply(new BigDecimal("100"))
gorkaion@17485
   325
            .divide(priceList, stdPrecision, BigDecimal.ROUND_HALF_UP);
gorkaion@17482
   326
      } else {
gorkaion@17485
   327
        origDiscount = BigDecimal.ZERO;
gorkaion@17482
   328
      }
gorkaion@17485
   329
      BigDecimal newDiscount = (strDiscount.equals("") ? ZERO : new BigDecimal(strDiscount)
gorkaion@17485
   330
          .setScale(stdPrecision, BigDecimal.ROUND_HALF_UP));
gorkaion@17485
   331
gorkaion@17485
   332
      if (origDiscount.compareTo(newDiscount) != 0) {
pandeeswari@20845
   333
        BigDecimal baseUnitPrice = priceList.subtract(
pandeeswari@20845
   334
            priceList.multiply(newDiscount).divide(new BigDecimal("100"))).setScale(pricePrecision,
pandeeswari@20845
   335
            BigDecimal.ROUND_HALF_UP);
gorkaion@17485
   336
        if (isTaxIncludedPriceList) {
asier@19229
   337
          grossUnitPrice = PriceAdjustment.calculatePriceActual(order, product, qtyOrdered,
asier@19229
   338
              baseUnitPrice);
asier@19229
   339
          resultado.append("new Array(\"inpgrosspricestd\", " + baseUnitPrice.toString() + "),");
gorkaion@17485
   340
          resultado.append("new Array(\"inpgrossUnitPrice\", " + grossUnitPrice.toString() + "),");
asier@18508
   341
asier@18508
   342
          // set also net prices
asier@18508
   343
          BigDecimal grossAmount = grossUnitPrice.multiply(qtyOrdered).setScale(stdPrecision,
asier@18508
   344
              RoundingMode.HALF_UP);
asier@18508
   345
asier@18508
   346
          final BigDecimal netUnitPrice = FinancialUtils.calculateNetFromGross(strTaxId,
asier@18508
   347
              grossAmount, pricePrecision, taxBaseAmt, qtyOrdered);
asier@18508
   348
asier@18508
   349
          priceStd = netUnitPrice;
gorkaion@17485
   350
        } else {
david@18197
   351
          priceStd = baseUnitPrice;
gorkaion@17482
   352
        }
asier@19229
   353
asier@19229
   354
        if (!cancelPriceAd) {
asier@19229
   355
          priceActual = PriceAdjustment.calculatePriceActual(order, product, qtyOrdered, priceStd);
asier@19229
   356
        } else {
asier@19229
   357
          priceActual = priceStd;
asier@19229
   358
        }
asier@19229
   359
        resultado.append("new Array(\"inppriceactual\", " + priceActual.toString() + "),");
asier@18508
   360
        resultado.append("new Array(\"inppricestd\", " + priceStd.toString() + "),");
martin@2868
   361
      }
martin@2868
   362
    }
martin@2868
   363
gorkaion@17482
   364
    if (isSOTrx.equals("Y") && !strStockSecurity.equals("0")
gorkaion@17482
   365
        && qtyOrdered.compareTo(BigDecimal.ZERO) != 0) {
gorkaion@17482
   366
      if (strEnforceAttribute.equals("N")) {
gorkaion@17482
   367
        strStockNoAttribute = SLOrderStockData.totalStockNoAttribute(this, strProduct, strUOM);
gorkaion@17482
   368
        stockNoAttribute = new BigDecimal(strStockNoAttribute);
gorkaion@17482
   369
        resultStock = stockNoAttribute.subtract(qtyOrdered);
gorkaion@17482
   370
        if (stockSecurity.compareTo(resultStock) > 0) {
gorkaion@17482
   371
          resultado
gorkaion@17482
   372
              .append("new Array('MESSAGE', \""
adrianromero@11613
   373
                  + FormatUtilities.replaceJS(Utility.messageBD(this, "StockLimit",
adrianromero@11613
   374
                      vars.getLanguage())) + "\"),");
gorkaion@17482
   375
        }
gorkaion@17482
   376
      } else if (!strAttribute.equals("") && strAttribute != null) {
gorkaion@17482
   377
        strStockAttribute = SLOrderStockData.totalStockAttribute(this, strProduct, strUOM,
gorkaion@17482
   378
            strAttribute);
gorkaion@17482
   379
        stockAttribute = new BigDecimal(strStockAttribute);
gorkaion@17482
   380
        resultStock = stockAttribute.subtract(qtyOrdered);
gorkaion@17482
   381
        if (stockSecurity.compareTo(resultStock) > 0) {
gorkaion@17482
   382
          resultado
gorkaion@17482
   383
              .append("new Array('MESSAGE', \""
gorkaion@17482
   384
                  + FormatUtilities.replaceJS(Utility.messageBD(this, "StockLimit",
gorkaion@17482
   385
                      vars.getLanguage())) + "\"),");
martin@2462
   386
        }
martin@2868
   387
      }
martin@2868
   388
    }
gorkaion@17482
   389
    log4j.debug(resultado.toString());
gorkaion@17482
   390
    if (!strChanged.equals("inpqtyordered") || strChanged.equals("inplinenetamt")) {
gorkaion@17482
   391
      // Check PriceLimit
martin@2868
   392
      boolean enforced = SLOrderAmtData.listPriceType(this, strPriceList);
martin@2868
   393
      // Check Price Limit?
martin@2868
   394
      if (enforced && priceLimit.compareTo(BigDecimal.ZERO) != 0
gorkaion@17482
   395
          && priceActual.compareTo(priceLimit) < 0) {
martin@2868
   396
        resultado.append("new Array('MESSAGE', \""
martin@2868
   397
            + Utility.messageBD(this, "UnderLimitPrice", vars.getLanguage()) + "\")");
gorkaion@17482
   398
      }
martin@2868
   399
    }
carlos@0
   400
meeravalli@17308
   401
    // if net unit price changed then modify tax inclusive unit price
meeravalli@17308
   402
    if (strChanged.equals("inppriceactual")) {
meeravalli@17310
   403
      priceActual = new BigDecimal(strPriceActual.trim());
meeravalli@17308
   404
      log4j.debug("Net unit price results: " + resultado.toString());
meeravalli@17308
   405
    }
martin@2868
   406
    // Multiply
asier@19229
   407
    if (cancelPriceAd) {
gorkaion@17482
   408
      lineNetAmt = qtyOrdered.multiply(priceStd);
gorka@4619
   409
    } else {
david@6474
   410
      if (!strChanged.equals("inplinenetamt")) {
gorkaion@17482
   411
        lineNetAmt = qtyOrdered.multiply(priceActual);
gorkaion@17482
   412
        if (lineNetAmt.scale() > stdPrecision)
gorkaion@17482
   413
          lineNetAmt = lineNetAmt.setScale(stdPrecision, BigDecimal.ROUND_HALF_UP);
david@6474
   414
      }
gorka@4619
   415
    }
hari@17324
   416
    if (strChanged.equals("inplinenetamt")) {
adrianromero@6817
   417
      resultado.append("new Array(\"inppriceactual\", " + priceActual.toString() + "),");
gorkaion@17482
   418
      resultado.append("new Array(\"inptaxbaseamt\", " + lineNetAmt.toString() + "),");
hari@17324
   419
    }
gorkaion@17482
   420
    if (!strChanged.equals("inplinenetamt") || priceActual.compareTo(BigDecimal.ZERO) == 0) {
gorkaion@17482
   421
      resultado.append("new Array(\"inplinenetamt\", " + lineNetAmt.toString() + "),");
gorkaion@17482
   422
    }
gorkaion@17494
   423
    if (!strChanged.equals("inplineGrossAmount")) {
gorkaion@17494
   424
      BigDecimal grossLineAmt = grossUnitPrice.multiply(qtyOrdered).setScale(stdPrecision,
gorkaion@17494
   425
          BigDecimal.ROUND_HALF_UP);
gorkaion@17494
   426
      resultado.append("new Array(\"inplineGrossAmount\", " + grossLineAmt.toString() + "),");
gorkaion@17494
   427
    }
javier@20586
   428
    resultado.append("new Array(\"inptaxbaseamt\", " + lineNetAmt.toString() + "),");
hari@17324
   429
    resultado.append("new Array(\"dummy\", \"\" )");
martin@2462
   430
martin@2868
   431
    resultado.append(");");
martin@2868
   432
    xmlDocument.setParameter("array", resultado.toString());
gorkaion@17344
   433
    log4j.debug("Callout for field changed: " + strChanged + " is " + resultado.toString());
martin@2868
   434
    xmlDocument.setParameter("frameName", "appFrame");
martin@2868
   435
    response.setContentType("text/html; charset=UTF-8");
martin@2868
   436
    PrintWriter out = response.getWriter();
martin@2868
   437
    out.println(xmlDocument.print());
martin@2868
   438
    out.close();
martin@2868
   439
  }
guillermo@21276
   440
carlos@0
   441
}