src-db/database/model/functions/M_INOUT_POST.xml
changeset 30926 a57c4aa5d672
parent 30916 97937c788c1c
parent 30782 50698b513147
child 31620 9c931e5263c1
child 32399 a031e45f7316
--- a/src-db/database/model/functions/M_INOUT_POST.xml	Fri Dec 02 11:00:27 2016 +0100
+++ b/src-db/database/model/functions/M_INOUT_POST.xml	Fri Dec 02 12:32:08 2016 +0100
@@ -80,8 +80,6 @@
     v_QtyPO NUMBER;
     v_QtySO NUMBER;
     v_QuantityOrder NUMBER;
-    v_QuantityOrderPO NUMBER;
-    v_QuantityOrderSO NUMBER;
     v_RDocumentNo VARCHAR2(40) ;
     v_RInOut_ID VARCHAR2(32);
     v_IsStocked NUMBER;
@@ -530,16 +528,12 @@
               v_Qty:=- Cur_InOutLine.MovementQty;
               v_QuantityOrder:=-Cur_InOutLine.QuantityOrder;
             END IF;
-            IF(Cur_InOut.IsSOTrx='N') THEN
+            IF (Cur_InOut.IsSOTrx='N' AND v_isreturndoctype='N') THEN
               v_QtySO:=0;
               v_QtyPO:=Cur_InOutLine.MovementQty;
-              v_QuantityOrderSO:=0;
-              v_QuantityOrderPO:=Cur_InOutLine.QuantityOrder;
             ELSE
               v_QtySO:=Cur_InOutLine.MovementQty;
               v_QtyPO:=0;
-              v_QuantityOrderSO:=Cur_InOutLine.QuantityOrder;
-              v_QuantityOrderPO:=0;
             END IF;
             -- UOM Conversion
             -- Is it a standard stocked product:3
@@ -747,7 +741,7 @@
             END IF;
             v_ResultStr:='UpdateOrderLine';
             IF(Cur_InOutLine.C_OrderLine_ID IS NOT NULL) THEN
-              if(Cur_InOut.ISSOTRX='Y') THEN
+              IF (Cur_InOut.ISSOTRX='Y' OR (Cur_InOut.ISSOTRX='N' AND v_isreturndoctype='Y')) THEN
                 -- Sets DateDelivered with the recent shipment date
                 -- of the shipment/s done for the orderline.
                 SELECT MAX(M.MOVEMENTDATE) INTO v_DateDelivered
@@ -808,6 +802,48 @@
               END IF;
             END IF;
           END LOOP; -- For all InOut Lines
+
+         -- Update the C_Order.IsDelivered flag only for Sales or RTV flows
+         -- This should be extended for other flows too in the future
+         IF (v_isSoTrx='Y' OR (v_isSoTrx='N' AND v_isreturndoctype='Y')) THEN
+           FOR Cur_Order IN
+           (SELECT o.c_order_id
+            FROM C_Order o
+            WHERE EXISTS (SELECT 1
+                          FROM C_ORDERLINE ol
+                          JOIN M_INOUTLINE iol ON iol.C_ORDERLINE_ID = ol.C_ORDERLINE_ID
+                          WHERE ol.C_Order_ID = o.C_Order_ID
+                          AND iol.m_inout_id = CUR_InOut.m_inout_id
+                          )
+           )
+           LOOP
+             IF (v_IsReversedDoc = 'N') THEN
+               -- Normal Scenario
+               UPDATE C_Order o
+               SET IsDelivered = 'Y'
+               WHERE o.c_order_id = Cur_Order.c_order_id
+               AND o.IsDelivered = 'N'
+               AND EXISTS (SELECT 1
+                           FROM C_ORDERLINE ol
+                           WHERE ol.C_Order_ID = o.C_Order_ID
+                           AND ol.c_order_discount_id IS NULL
+                           GROUP BY ol.C_Order_ID
+                           HAVING SUM(ol.QTYORDERED) = SUM(ol.QTYDELIVERED));
+             ELSIF (v_IsReversedDoc = 'Y') THEN
+               -- Void Scenario
+               UPDATE C_Order o
+               SET IsDelivered = 'N'
+               WHERE o.c_order_id = Cur_Order.c_order_id
+               AND o.IsDelivered = 'Y'
+               AND EXISTS (SELECT 1
+                           FROM C_ORDERLINE ol
+                           WHERE ol.C_Order_ID = o.C_Order_ID
+                           AND ol.c_order_discount_id IS NULL
+                           AND ol.QTYORDERED <> ol.QTYDELIVERED);
+             END IF;
+           END LOOP;
+         END IF;
+
           /*******************
           * PO Matching
           ******************/