Fixes issue: 21581. Allows to Insert new lines and update lines
authorDavid Miguelez <david.miguelez@openbravo.com>
Tue, 11 Sep 2012 18:23:54 +0200
changeset 17939 3e0e98fb9275
parent 17938 a8cb5918bad6
child 17940 32b2e7f42f70
Fixes issue: 21581. Allows to Insert new lines and update lines
that are not related to a processed Return Shipment/Receipt.
Lines that are related to a processed Return Shipment/Receipt
can not be neither deleted nor updated.
src-db/database/model/views/C_RM_ORDER_PICK_EDIT_LINES.xml
src-db/database/sourcedata/AD_COLUMN.xml
src-db/database/sourcedata/AD_FIELD.xml
src-db/database/sourcedata/AD_TAB.xml
src/org/openbravo/common/actionhandler/SRMOPickEditLines.java
src/org/openbravo/dal/service/OBDao.java
--- a/src-db/database/model/views/C_RM_ORDER_PICK_EDIT_LINES.xml	Thu Sep 13 13:27:42 2012 +0200
+++ b/src-db/database/model/views/C_RM_ORDER_PICK_EDIT_LINES.xml	Tue Sep 11 18:23:54 2012 +0200
@@ -16,7 +16,7 @@
 WHEN 'N' THEN ol.priceactual
 ELSE ol.gross_unit_price
 END
-END AS priceactual, rol.c_return_reason_id, o.documentno AS orderno, COALESCE(rol.issotrx, i.issotrx) AS issotrx, (-1) * retol.returnedqty AS returnedqty, COALESCE(rol.c_bpartner_id, i.c_bpartner_id) AS c_bpartner_id, COALESCE(rol.c_tax_id, ol.c_tax_id) AS c_tax_id, pl.istaxincluded
+END AS priceactual, rol.c_return_reason_id, o.documentno AS orderno, COALESCE(rol.issotrx, i.issotrx) AS issotrx, (-1) * retol.returnedqty AS returnedqty, COALESCE(rol.c_bpartner_id, i.c_bpartner_id) AS c_bpartner_id, COALESCE(rol.c_tax_id, ol.c_tax_id) AS c_tax_id, pl.istaxincluded, rol.c_orderline_id
 FROM m_inoutline il
 JOIN m_inout i ON il.m_inout_id = i.m_inout_id AND i.processed = 'Y' AND i.docstatus <> 'VO'
 JOIN c_doctype dt ON i.c_doctype_id = dt.c_doctype_id AND dt.isreturn = 'N'
--- a/src-db/database/sourcedata/AD_COLUMN.xml	Thu Sep 13 13:27:42 2012 +0200
+++ b/src-db/database/sourcedata/AD_COLUMN.xml	Tue Sep 11 18:23:54 2012 +0200
@@ -285535,6 +285535,41 @@
 <!--B7D39D63B1734983ABB6829E13A6F46F-->  <ISUSEDSEQUENCE><![CDATA[N]]></ISUSEDSEQUENCE>
 <!--B7D39D63B1734983ABB6829E13A6F46F--></AD_COLUMN>
 
+<!--B7D8C41F83BC45BD87897C0E29ED53EF--><AD_COLUMN>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <AD_COLUMN_ID><![CDATA[B7D8C41F83BC45BD87897C0E29ED53EF]]></AD_COLUMN_ID>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <NAME><![CDATA[Sales Order Line]]></NAME>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <DESCRIPTION><![CDATA[A unique and often automatically generated identifier for a sales order line.]]></DESCRIPTION>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <HELP><![CDATA[A unique identifier and a reference to a sales order line (product).]]></HELP>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <COLUMNNAME><![CDATA[C_Orderline_ID]]></COLUMNNAME>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <AD_TABLE_ID><![CDATA[38A7A1273EA34C93A0EA49CE77B6FB14]]></AD_TABLE_ID>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <AD_REFERENCE_ID><![CDATA[19]]></AD_REFERENCE_ID>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <FIELDLENGTH><![CDATA[32]]></FIELDLENGTH>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISKEY><![CDATA[N]]></ISKEY>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISPARENT><![CDATA[N]]></ISPARENT>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISMANDATORY><![CDATA[N]]></ISMANDATORY>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISUPDATEABLE><![CDATA[Y]]></ISUPDATEABLE>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISIDENTIFIER><![CDATA[N]]></ISIDENTIFIER>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <SEQNO><![CDATA[280]]></SEQNO>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISTRANSLATED><![CDATA[N]]></ISTRANSLATED>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISENCRYPTED><![CDATA[N]]></ISENCRYPTED>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISSELECTIONCOLUMN><![CDATA[N]]></ISSELECTIONCOLUMN>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <AD_ELEMENT_ID><![CDATA[561]]></AD_ELEMENT_ID>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISSESSIONATTR><![CDATA[N]]></ISSESSIONATTR>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISSECONDARYKEY><![CDATA[N]]></ISSECONDARYKEY>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISDESENCRYPTABLE><![CDATA[N]]></ISDESENCRYPTABLE>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <DEVELOPMENTSTATUS><![CDATA[RE]]></DEVELOPMENTSTATUS>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <POSITION><![CDATA[27]]></POSITION>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISTRANSIENT><![CDATA[N]]></ISTRANSIENT>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISAUTOSAVE><![CDATA[Y]]></ISAUTOSAVE>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <VALIDATEONNEW><![CDATA[Y]]></VALIDATEONNEW>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <IMAGESIZEVALUESACTION><![CDATA[N]]></IMAGESIZEVALUESACTION>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF-->  <ISUSEDSEQUENCE><![CDATA[N]]></ISUSEDSEQUENCE>
+<!--B7D8C41F83BC45BD87897C0E29ED53EF--></AD_COLUMN>
+
 <!--B7E494705B883B25E040007F0100742F--><AD_COLUMN>
 <!--B7E494705B883B25E040007F0100742F-->  <AD_COLUMN_ID><![CDATA[B7E494705B883B25E040007F0100742F]]></AD_COLUMN_ID>
 <!--B7E494705B883B25E040007F0100742F-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src-db/database/sourcedata/AD_FIELD.xml	Thu Sep 13 13:27:42 2012 +0200
+++ b/src-db/database/sourcedata/AD_FIELD.xml	Tue Sep 11 18:23:54 2012 +0200
@@ -182098,6 +182098,35 @@
 <!--3C5AAC98B6A4464C940B68DE5B85ACCC-->  <EM_OBUIAPP_SHOWSUMMARY><![CDATA[N]]></EM_OBUIAPP_SHOWSUMMARY>
 <!--3C5AAC98B6A4464C940B68DE5B85ACCC--></AD_FIELD>
 
+<!--3C5C84369EA4411797D244727CF0E389--><AD_FIELD>
+<!--3C5C84369EA4411797D244727CF0E389-->  <AD_FIELD_ID><![CDATA[3C5C84369EA4411797D244727CF0E389]]></AD_FIELD_ID>
+<!--3C5C84369EA4411797D244727CF0E389-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--3C5C84369EA4411797D244727CF0E389-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--3C5C84369EA4411797D244727CF0E389-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--3C5C84369EA4411797D244727CF0E389-->  <NAME><![CDATA[Sales Order Line]]></NAME>
+<!--3C5C84369EA4411797D244727CF0E389-->  <DESCRIPTION><![CDATA[A unique and often automatically generated identifier for a sales order line.]]></DESCRIPTION>
+<!--3C5C84369EA4411797D244727CF0E389-->  <HELP><![CDATA[A unique identifier and a reference to a sales order line (product).]]></HELP>
+<!--3C5C84369EA4411797D244727CF0E389-->  <ISCENTRALLYMAINTAINED><![CDATA[Y]]></ISCENTRALLYMAINTAINED>
+<!--3C5C84369EA4411797D244727CF0E389-->  <AD_TAB_ID><![CDATA[CC35784EF36B4732B40167CD5D2449F0]]></AD_TAB_ID>
+<!--3C5C84369EA4411797D244727CF0E389-->  <AD_COLUMN_ID><![CDATA[B7D8C41F83BC45BD87897C0E29ED53EF]]></AD_COLUMN_ID>
+<!--3C5C84369EA4411797D244727CF0E389-->  <IGNOREINWAD><![CDATA[N]]></IGNOREINWAD>
+<!--3C5C84369EA4411797D244727CF0E389-->  <ISDISPLAYED><![CDATA[N]]></ISDISPLAYED>
+<!--3C5C84369EA4411797D244727CF0E389-->  <DISPLAYLENGTH><![CDATA[32]]></DISPLAYLENGTH>
+<!--3C5C84369EA4411797D244727CF0E389-->  <ISREADONLY><![CDATA[N]]></ISREADONLY>
+<!--3C5C84369EA4411797D244727CF0E389-->  <SEQNO><![CDATA[130]]></SEQNO>
+<!--3C5C84369EA4411797D244727CF0E389-->  <ISSAMELINE><![CDATA[N]]></ISSAMELINE>
+<!--3C5C84369EA4411797D244727CF0E389-->  <ISFIELDONLY><![CDATA[N]]></ISFIELDONLY>
+<!--3C5C84369EA4411797D244727CF0E389-->  <ISENCRYPTED><![CDATA[N]]></ISENCRYPTED>
+<!--3C5C84369EA4411797D244727CF0E389-->  <SHOWINRELATION><![CDATA[N]]></SHOWINRELATION>
+<!--3C5C84369EA4411797D244727CF0E389-->  <ISFIRSTFOCUSEDFIELD><![CDATA[N]]></ISFIRSTFOCUSEDFIELD>
+<!--3C5C84369EA4411797D244727CF0E389-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--3C5C84369EA4411797D244727CF0E389-->  <GRID_SEQNO><![CDATA[0]]></GRID_SEQNO>
+<!--3C5C84369EA4411797D244727CF0E389-->  <STARTINODDCOLUMN><![CDATA[N]]></STARTINODDCOLUMN>
+<!--3C5C84369EA4411797D244727CF0E389-->  <STARTNEWLINE><![CDATA[N]]></STARTNEWLINE>
+<!--3C5C84369EA4411797D244727CF0E389-->  <ISSHOWNINSTATUSBAR><![CDATA[N]]></ISSHOWNINSTATUSBAR>
+<!--3C5C84369EA4411797D244727CF0E389-->  <EM_OBUIAPP_SHOWSUMMARY><![CDATA[N]]></EM_OBUIAPP_SHOWSUMMARY>
+<!--3C5C84369EA4411797D244727CF0E389--></AD_FIELD>
+
 <!--3CDA4C4528644DD9B42520391412D86D--><AD_FIELD>
 <!--3CDA4C4528644DD9B42520391412D86D-->  <AD_FIELD_ID><![CDATA[3CDA4C4528644DD9B42520391412D86D]]></AD_FIELD_ID>
 <!--3CDA4C4528644DD9B42520391412D86D-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src-db/database/sourcedata/AD_TAB.xml	Thu Sep 13 13:27:42 2012 +0200
+++ b/src-db/database/sourcedata/AD_TAB.xml	Tue Sep 11 18:23:54 2012 +0200
@@ -14245,7 +14245,7 @@
 AND CASE WHEN e.salesTransaction = true THEN 'true' ELSE 'false' END = @Order.salesTransaction@
 AND e.businessPartner.id = @Order.businessPartner@
 AND (e.orderNo IS NULL OR e.priceIncludesTax = (SELECT priceIncludesTax FROM PricingPriceList WHERE id = @Order.priceList@))]]></HQLWHERECLAUSE>
-<!--CC35784EF36B4732B40167CD5D2449F0-->  <HQLORDERBYCLAUSE><![CDATA[obSelected desc, movementDate desc, orderNo, product.name]]></HQLORDERBYCLAUSE>
+<!--CC35784EF36B4732B40167CD5D2449F0-->  <HQLORDERBYCLAUSE><![CDATA[obSelected desc, salesOrderLine.lineNo, movementDate desc, orderNo, product.name]]></HQLORDERBYCLAUSE>
 <!--CC35784EF36B4732B40167CD5D2449F0-->  <HQLFILTERCLAUSE><![CDATA[e.movementDate >= (now() - 90) OR e.obSelected=true]]></HQLFILTERCLAUSE>
 <!--CC35784EF36B4732B40167CD5D2449F0-->  <SHOWPARENTBUTTONS><![CDATA[Y]]></SHOWPARENTBUTTONS>
 <!--CC35784EF36B4732B40167CD5D2449F0-->  <EM_OBUIAPP_SELECTION><![CDATA[OB.RM.RMOrderSelectionChange]]></EM_OBUIAPP_SELECTION>
--- a/src/org/openbravo/common/actionhandler/SRMOPickEditLines.java	Thu Sep 13 13:27:42 2012 +0200
+++ b/src/org/openbravo/common/actionhandler/SRMOPickEditLines.java	Tue Sep 11 18:23:54 2012 +0200
@@ -28,15 +28,18 @@
 import org.codehaus.jettison.json.JSONArray;
 import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
+import org.hibernate.criterion.Projections;
+import org.hibernate.criterion.Restrictions;
 import org.openbravo.base.exception.OBException;
 import org.openbravo.base.provider.OBProvider;
 import org.openbravo.base.secureApp.VariablesSecureApp;
 import org.openbravo.client.application.process.BaseProcessActionHandler;
 import org.openbravo.client.kernel.RequestContext;
 import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.service.OBCriteria;
 import org.openbravo.dal.service.OBDal;
+import org.openbravo.dal.service.OBDao;
 import org.openbravo.erpCommon.utility.OBMessageUtils;
-import org.openbravo.erpCommon.utility.Utility;
 import org.openbravo.financial.FinancialUtils;
 import org.openbravo.model.common.order.Order;
 import org.openbravo.model.common.order.OrderLine;
@@ -49,6 +52,7 @@
 import org.openbravo.model.pricing.pricelist.ProductPrice;
 import org.openbravo.service.db.CallStoredProcedure;
 import org.openbravo.service.db.DalConnectionProvider;
+import org.openbravo.service.db.DbUtility;
 
 /**
  * 
@@ -71,8 +75,10 @@
       // Issue 20585: https://issues.openbravo.com/view.php?id=20585
       final String strOrderId = jsonRequest.getString("C_Order_ID");
       Order order = OBDal.getInstance().get(Order.class, strOrderId);
-      if (cleanOrderLines(order)) {
-        createOrderLines(jsonRequest);
+
+      if (order != null) {
+        List<String> idList = OBDao.getIDListFromOBObject(order.getOrderLineList());
+        createOrderLines(jsonRequest, idList);
       }
 
     } catch (Exception e) {
@@ -82,13 +88,14 @@
 
       try {
         jsonRequest = new JSONObject();
-
+        Throwable ex = DbUtility.getUnderlyingSQLException(e);
+        String message = OBMessageUtils.translateError(new DalConnectionProvider(), vars,
+            vars.getLanguage(), ex.getMessage()).getMessage();
         JSONObject errorMessage = new JSONObject();
         errorMessage.put("severity", "error");
-        errorMessage.put("text",
-            Utility.messageBD(new DalConnectionProvider(), e.getMessage(), vars.getLanguage()));
+        errorMessage.put("text", message);
+        jsonRequest.put("message", errorMessage);
 
-        jsonRequest.put("message", errorMessage);
       } catch (Exception e2) {
         log.error(e.getMessage(), e2);
         // do nothing, give up
@@ -99,46 +106,48 @@
     return jsonRequest;
   }
 
-  private boolean cleanOrderLines(Order order) {
-    if (order == null) {
-      return false;
-    } else if (order.getOrderLineList().isEmpty()) {
-      // nothing to delete.
-      return true;
-    }
-    try {
-      order.getOrderLineList().clear();
-      OBDal.getInstance().save(order);
-      OBDal.getInstance().flush();
-    } catch (Exception e) {
-      log.error(e.getMessage(), e);
-      return false;
-    }
-    return true;
-  }
-
-  private void createOrderLines(JSONObject jsonRequest) throws JSONException, OBException {
+  private void createOrderLines(JSONObject jsonRequest, List<String> idList) throws JSONException,
+      OBException {
     JSONArray selectedLines = jsonRequest.getJSONArray("_selection");
-    // if no lines selected don't do anything.
-    if (selectedLines.length() == 0) {
-      return;
-    }
     final String strOrderId = jsonRequest.getString("C_Order_ID");
     Order order = OBDal.getInstance().get(Order.class, strOrderId);
     boolean isSOTrx = order.isSalesTransaction();
+    // if no lines selected don't do anything.
+    if (selectedLines.length() == 0) {
+      removeNonSelectedLines(idList, order);
+      return;
+    }
+    OBCriteria<OrderLine> obc = OBDal.getInstance().createCriteria(OrderLine.class);
+    obc.add(Restrictions.eq(OrderLine.PROPERTY_SALESORDER, order));
+    obc.setProjection(Projections.max(OrderLine.PROPERTY_LINENO));
+    Long lineNo = 0L;
+    Object o = obc.list().get(0);
+    if (o != null) {
+      lineNo = (Long) o;
+    }
+
     for (long i = 0; i < selectedLines.length(); i++) {
       JSONObject selectedLine = selectedLines.getJSONObject((int) i);
       log.debug(selectedLine);
       if (selectedLine.get("returned").equals(null)) {
         continue;
       }
-      OrderLine newOrderLine = OBProvider.getInstance().get(OrderLine.class);
-      newOrderLine.setSalesOrder(order);
-      newOrderLine.setOrganization(order.getOrganization());
-      newOrderLine.setLineNo((i + 1L) * 10L);
-      newOrderLine.setOrderDate(order.getOrderDate());
-      newOrderLine.setWarehouse(order.getWarehouse());
-      newOrderLine.setCurrency(order.getCurrency());
+
+      OrderLine newOrderLine = null;
+      boolean notExistsOrderLine = selectedLine.get("salesOrderLine").equals(null);
+      if (notExistsOrderLine) {
+        newOrderLine = OBProvider.getInstance().get(OrderLine.class);
+        newOrderLine.setSalesOrder(order);
+        newOrderLine.setOrganization(order.getOrganization());
+        lineNo = lineNo + 10L;
+        newOrderLine.setLineNo(lineNo);
+        newOrderLine.setOrderDate(order.getOrderDate());
+        newOrderLine.setWarehouse(order.getWarehouse());
+        newOrderLine.setCurrency(order.getCurrency());
+      } else {
+        newOrderLine = OBDal.getInstance().get(OrderLine.class, selectedLine.get("salesOrderLine"));
+        idList.remove(selectedLine.get("salesOrderLine"));
+      }
 
       ShipmentInOutLine shipmentLine = OBDal.getInstance().get(ShipmentInOutLine.class,
           selectedLine.getString("goodsShipmentLine"));
@@ -255,13 +264,29 @@
         newOrderLine.setReturnReason(order.getReturnReason());
       }
 
-      List<OrderLine> orderLines = order.getOrderLineList();
-      orderLines.add(newOrderLine);
-      order.setOrderLineList(orderLines);
+      if (notExistsOrderLine) {
+        List<OrderLine> orderLines = order.getOrderLineList();
+        orderLines.add(newOrderLine);
+        order.setOrderLineList(orderLines);
+      }
 
       OBDal.getInstance().save(newOrderLine);
       OBDal.getInstance().save(order);
       OBDal.getInstance().flush();
     }
+
+    removeNonSelectedLines(idList, order);
+  }
+
+  private void removeNonSelectedLines(List<String> idList, Order order) {
+    if (idList.size() > 0) {
+      for (String id : idList) {
+        OrderLine ol = OBDal.getInstance().get(OrderLine.class, id);
+        order.getOrderLineList().remove(ol);
+        OBDal.getInstance().remove(ol);
+      }
+      OBDal.getInstance().save(order);
+      OBDal.getInstance().flush();
+    }
   }
 }
--- a/src/org/openbravo/dal/service/OBDao.java	Thu Sep 13 13:27:42 2012 +0200
+++ b/src/org/openbravo/dal/service/OBDao.java	Tue Sep 11 18:23:54 2012 +0200
@@ -104,4 +104,12 @@
     }
     return baseOBObjectList;
   }
+
+  public static <T extends BaseOBObject> List<String> getIDListFromOBObject(List<T> list) {
+    List<String> idList = new ArrayList<String>();
+    for (BaseOBObject o : list) {
+      idList.add(o.getId().toString());
+    }
+    return idList;
+  }
 }