[Reservations]Implement reservations in MRP Purchasing.
authorGorka Ion Damián <gorkaion.damian@openbravo.com>
Mon, 12 Nov 2012 09:56:06 +0100
changeset 18689 a17b382229ce
parent 18688 9181cfa32cd7
child 18690 c56e0ec7fabf
[Reservations]Implement reservations in MRP Purchasing.
src-db/database/model/functions/MRP_PURCHASEPLAN.xml
src-db/database/model/functions/MRP_RUN_INITIALIZE.xml
src-db/database/model/functions/M_GET_STOCK.xml
src-db/database/model/functions/M_INOUT_CREATE.xml
src-db/database/model/functions/M_RESERVATION_CONSUMPTION.xml
src-db/database/model/functions/M_RESERVATION_POST.xml
src-db/database/model/tables/MRP_RUN_PURCHASE.xml
src-db/database/model/tables/M_PRODUCT_ORG.xml
src-db/database/sourcedata/AD_COLUMN.xml
src-db/database/sourcedata/AD_ELEMENT.xml
src-db/database/sourcedata/AD_FIELD.xml
src-db/database/sourcedata/AD_MODEL_OBJECT.xml
src-db/database/sourcedata/AD_PROCESS.xml
src-db/database/sourcedata/AD_PROCESS_PARA.xml
src-db/database/sourcedata/AD_TAB.xml
src-db/database/sourcedata/AD_VAL_RULE.xml
src/org/openbravo/erpCommon/ad_process/MRPPurchaseCreateReservations.java
--- a/src-db/database/model/functions/MRP_PURCHASEPLAN.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/model/functions/MRP_PURCHASEPLAN.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -72,34 +72,32 @@
     v_ResultStr := 'Purchase mrp';
 
     FOR Cur_PlanProduct IN (
-        SELECT MRP_RUN_PURCHASELINE.M_PRODUCT_ID, M_PRODUCT.NAME AS PRODUCTNAME,
-          COALESCE(M_PRODUCT_PO.CAPACITY, M_PRODUCT_ORG.CAPACITY, M_PRODUCT.CAPACITY) AS CAPACITY,
-          COALESCE(M_PRODUCT_PO.DELIVERYTIME_PROMISED, M_PRODUCT_ORG.DELAYMIN, M_PRODUCT.DELAYMIN, 0) AS DELAYMIN,
-          COALESCE(M_PRODUCT_PO.QTYTYPE, M_PRODUCT_ORG.QTYTYPE, M_PRODUCT_ORG.QTYTYPE, 'E') AS qtytype,
-          COALESCE(M_PRODUCT_PO.ORDER_MIN, M_PRODUCT_ORG.QTYMIN, M_PRODUCT_ORG.QTYMIN, 0) AS qtymin,
-          COALESCE(M_PRODUCT_PO.QTYSTD, M_PRODUCT_ORG.QTYSTD, M_PRODUCT_ORG.QTYSTD, 1) AS qtystd,
-          M_PRODUCT_PO.C_BPARTNER_ID AS vendor_ID
-        FROM MRP_RUN_PURCHASELINE, M_PRODUCT_PO,
-             M_PRODUCT LEFT JOIN M_PRODUCT_ORG ON M_PRODUCT.M_PRODUCT_ID = M_PRODUCT_ORG.M_PRODUCT_ID,
-             (SELECT MAX(C_BPARTNER_ID) AS C_BPARTNER_ID, M_PRODUCT_ID
-              FROM M_PRODUCT_PO
-              WHERE ISCURRENTVENDOR = 'Y'
-                AND (p_Vendor_ID IS NULL OR M_PRODUCT_PO.C_BPARTNER_ID = p_Vendor_ID)
-                AND M_PRODUCT_PO.DISCONTINUED = 'N'
-              GROUP BY M_PRODUCT_ID) PRODUCT_PO --CASE WITH MORE THAN ONE DEFAULT VENDOR
-        WHERE MRP_RUN_PURCHASE_ID = p_Run_ID
-          AND M_PRODUCT.M_PRODUCT_ID = M_PRODUCT_PO.M_PRODUCT_ID
-          AND M_PRODUCT.M_PRODUCT_ID = MRP_RUN_PURCHASELINE.M_PRODUCT_ID
-          AND MRP_RUN_PURCHASELINE.AD_ORG_ID = M_PRODUCT_ORG.AD_ORG_ID
-          AND PRODUCT_PO.C_BPARTNER_ID = M_PRODUCT_PO.C_BPARTNER_ID
-          AND PRODUCT_PO.M_PRODUCT_ID = M_PRODUCT_PO.M_PRODUCT_ID
-          AND M_PRODUCT.ISPURCHASED = 'Y'
-      GROUP BY MRP_RUN_PURCHASELINE.M_PRODUCT_ID, M_PRODUCT.NAME, M_PRODUCT_PO.CAPACITY, M_PRODUCT_ORG.CAPACITY, M_PRODUCT.CAPACITY,
-        M_PRODUCT_PO.DELIVERYTIME_PROMISED, M_PRODUCT_ORG.DELAYMIN, M_PRODUCT.DELAYMIN,
-        M_PRODUCT_PO.QTYTYPE, M_PRODUCT_ORG.QTYTYPE, M_PRODUCT_ORG.QTYTYPE,
-        M_PRODUCT_PO.ORDER_MIN, M_PRODUCT_ORG.QTYMIN, M_PRODUCT_ORG.QTYMIN,
-        M_PRODUCT_PO.QTYSTD, M_PRODUCT_ORG.QTYSTD, M_PRODUCT_ORG.QTYSTD, M_PRODUCT_PO.C_BPARTNER_ID --CASE WITH MORE THAN ONE DEFAULT VENDOR
-      ) LOOP
+        SELECT rpl.m_product_id, p.name AS productname,
+            COALESCE(ppo.capacity, porg.capacity, p.capacity) AS capacity,
+            COALESCE(ppo.deliverytime_promised, porg.delaymin, p.delaymin, 0) AS delaymin,
+            COALESCE(ppo.qtytype, porg.qtytype, 'E') AS qtytype,
+            COALESCE(ppo.order_min, porg.qtymin,0) AS qtymin,
+            COALESCE(ppo.qtystd, porg.qtystd, 1) AS qtystd,
+            ppo.c_bpartner_id AS vendor_id
+        FROM mrp_run_purchaseline rpl
+              JOIN m_product p ON p.m_product_id = rpl.m_product_id AND p.ispurchased = 'Y'
+              JOIN m_product_org porg ON p.m_product_id = porg.m_product_id AND rpl.ad_org_id = porg.ad_org_id
+              JOIN (SELECT MAX(m_product_po_id) AS m_product_po_id, m_product_id
+                    FROM m_product_po
+                    WHERE iscurrentvendor = 'Y'
+                      AND (p_vendor_id IS NULL OR m_product_po.c_bpartner_id = p_vendor_id)
+                      AND m_product_po.discontinued = 'N'
+                    GROUP BY m_product_id
+                  ) maxppo ON maxppo.m_product_id = p.m_product_id --CASE WITH MORE THAN ONE DEFAULT VENDOR
+              LEFT JOIN m_product_po prefppo ON prefppo.discontinued = 'N' AND porg.m_product_po_id = prefppo.m_product_po_id
+              JOIN m_product_po ppo ON COALESCE(prefppo.m_product_po_id, maxppo.m_product_po_id) = ppo.m_product_po_id
+        WHERE mrp_run_purchase_id = p_run_id
+        GROUP BY rpl.m_product_id, p.name, ppo.capacity, porg.capacity, p.capacity,
+            ppo.deliverytime_promised, porg.delaymin, p.delaymin,
+            ppo.qtytype, porg.qtytype, 
+            ppo.order_min, porg.qtymin,
+            ppo.qtystd, porg.qtystd, ppo.c_bpartner_id --CASE WITH MORE THAN ONE DEFAULT VENDOR
+    ) LOOP
       v_stock := 0;
       v_neededqty_old := NULL;
       v_planneddate_old := NULL;
--- a/src-db/database/model/functions/MRP_RUN_INITIALIZE.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/model/functions/MRP_RUN_INITIALIZE.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -82,11 +82,12 @@
   FOR Cur_Product IN (SELECT p.m_product_id,
                       COALESCE(po.stockmin, p.stockmin, 0) AS stockmin,
                       COALESCE(po.mrp_planningmethod_id, p.mrp_planningmethod_id) AS mrp_planningmethod_id
-                      FROM m_product p LEFT JOIN m_product_org po ON p.m_product_id = po.m_product_id
-                                                                     AND po.ad_org_id = p_org_id
+                      FROM m_product p 
+                            JOIN m_product_org po ON p.m_product_id = po.m_product_id AND po.ad_org_id = p_org_id
                       WHERE (p_product_id IS NULL OR p.m_product_id = p_product_id)
                         AND (p_product_category_id IS NULL OR p.m_product_category_id = p_product_category_id)
                         AND (p_planner_id IS NULL OR COALESCE(po.mrp_planner_id, p.mrp_planner_id) = p_planner_id)
+                        AND po.mrp_planningmethod_id IS NOT NULL
                         AND AD_ISORGINCLUDED(p_org_id, p.ad_org_id, p_client_id) > -1
                         AND p.ad_client_id = p_client_id
                         AND ((p_production = 'Y' AND p.ispurchased = 'N') OR (p_production = 'N' AND p.ispurchased = 'Y'))
@@ -97,10 +98,12 @@
                                         FROM m_product_po
                                         WHERE m_product_po.m_product_id = p.m_product_id
                                           AND m_product_po.c_bpartner_id = p_vendor_id
-                                          AND m_product_po.iscurrentvendor = 'Y'
                                           AND m_product_po.isactive = 'Y'
-                                          AND m_product_po.discontinued = 'N'
-                                        )))
+                                          AND (m_product_po.iscurrentvendor = 'Y'
+                                               OR COALESCE(po.m_product_po_id, m_product_po_id) = m_product_po_id
+                                              )
+                                          AND m_product_po.discontinued = 'N')
+                             ))
                         AND (p_bpartner_id IS NULL
                              OR EXISTS (SELECT 1
                                         FROM c_order o, c_orderline ol
@@ -213,14 +216,9 @@
       END IF;
     END; -- End Call M_GET_STOCK
 
-    SELECT COALESCE(SUM(quantity),0) INTO v_QtyOnHand
+    SELECT COALESCE(SUM(sp.quantity),0) INTO v_QtyOnHand
     FROM m_stock_proposed sp
-        JOIN m_storage_detail sd ON sp.m_storage_detail_id = sd.m_storage_detail_id
-        JOIN m_locator l ON sd.m_locator_id = l.m_locator_id
-        JOIN m_warehouse w ON l.m_warehouse_id = w.m_warehouse_id
-    WHERE ad_pinstance_id = v_ad_pinstance_id
-      -- Only stock of warehouses in p_org_id and its childs.
-      AND AD_ISORGINCLUDED(w.ad_org_id, p_org_id, w.ad_client_id) <> -1;
+    WHERE sp.aD_pinstance_id = v_ad_pinstance_id;
 
     v_ResultStr := 'Inserting stock lines product: ' || Cur_Product.M_Product_ID;
     MRP_RUN_INSERTLINES(p_client_id, p_org_id, p_user_id, p_run, cur_product.m_product_id, (-1 * cur_product.stockmin), 0, NULL, 'MS', 'Y', NULL, NULL, NULL, NULL, NULL, p_planningdate, NULL, p_production, (CASE WHEN cur_product.stockmin > 0 THEN 'N' ELSE 'Y' END), NULL, v_aux_id);
@@ -231,7 +229,7 @@
         SELECT MRP_CHECK_PLANNINGMETHOD(cur_product.mrp_planningmethod_id,
                                         (CASE o.issotrx WHEN 'Y' THEN 'SO' ELSE 'PO' END),
                                         TO_NUMBER(COALESCE(ol.datepromised, ol.dateordered, o.dateordered) - p_planningdate), p_timehorizon) AS weighting,
-              (ol.qtyordered - COALESCE(ol.qtydelivered, 0) - COALESCE(matchpo.qty,0)) AS qty,
+              (ol.qtyordered - COALESCE(ol.qtydelivered, 0) - COALESCE(matchpo.qty,0) - COALESCE(preres.reservedqty, 0) - (COALESCE(res.reservedqty,0) - COALESCE(res.releasedqty, 0))) AS qty,
               ol.c_orderline_id, GREATEST(COALESCE(ol.datepromised, ol.dateordered, o.dateordered), p_planningdate) AS planningdate,
               COALESCE(ol.dateordered, o.dateordered) AS dateordered,
               o.issotrx
@@ -242,12 +240,25 @@
                         WHERE m_inoutline_id IS NOT NULL
                         GROUP BY c_orderline_id) matchpo ON ol.c_orderline_id = matchpo.c_orderline_id
               JOIN m_warehouse w ON ol.m_warehouse_id = w.m_warehouse_id
+              -- Sales order reservation
+              LEFT JOIN m_reservation res ON res.c_orderline_id = ol.c_orderline_id AND res.res_status <> 'CL'
+              -- Prereserved purchase orders
+              LEFT JOIN (
+                  SELECT rs.c_orderline_id, SUM(rs.quantity - COALESCE(rs.releasedqty, 0)) AS reservedqty
+                  FROM m_reservation r
+                      JOIN m_reservation_stock rs ON r.m_reservation_id = rs.m_reservation_id
+                  WHERE rs.c_orderline_id IS NOT NULL
+                    AND r.res_status <> 'CL'
+                  GROUP BY rs.c_orderline_id 
+              ) preres ON preres.c_orderline_id = ol.c_orderline_id
          WHERE o.processed = 'Y'
            AND ol.qtyordered <> (COALESCE(ol.qtydelivered,0) + COALESCE(matchpo.qty,0))
            AND ol.m_product_id = cur_product.m_product_id
            AND MRP_CHECK_PLANNINGMETHOD(cur_product.mrp_planningmethod_id,
                                        (CASE o.issotrx WHEN 'Y' THEN 'SO' ELSE 'PO' END),
                                        to_number(COALESCE(ol.datepromised, ol.dateordered, o.dateordered) - p_planningdate), p_timehorizon) <> -1
+           AND ((o.issotrx = 'N' AND ol.qtyordered - COALESCE(matchpo.qty,0) > COALESCE(preres.reservedqty, 0))
+               OR (o.issotrx = 'Y' AND ol.qtyordered - COALESCE(ol.qtydelivered, 0) > COALESCE(res.reservedqty,0) - COALESCE(res.releasedqty, 0)))
            AND AD_ISORGINCLUDED(o.ad_org_id, p_org_id, p_client_id) > -1
            -- Only orders of warehouses in p_org_id and its childs
            AND AD_ISORGINCLUDED(w.ad_org_id, p_org_id, p_client_id) > -1
--- a/src-db/database/model/functions/M_GET_STOCK.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/model/functions/M_GET_STOCK.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -35,6 +35,7 @@
   Cur_Stock RECORD;
   Cur_MStock RECORD;
   cur_warehouse RECORD;
+  cur_allocated RECORD;
 
   -- Parameter Variables
   v_Quantity NUMBER(10);
@@ -54,6 +55,7 @@
   v_LineNo NUMBER;
   v_Count NUMBER:=1;
   v_ProcessID VARCHAR2(32) ;
+  v_Reservation_ID VARCHAR2(32);
   
 BEGIN
   --  Update AD_PInstance
@@ -119,12 +121,39 @@
     ELSIF (Cur_Parameter.ParameterName = 'ProcessID') THEN
       v_ProcessID:=Cur_Parameter.P_String;
       DBMS_OUTPUT.PUT_LINE(' ProcessID=' || v_ProcessID);
+    ELSIF (Cur_Parameter.ParameterName = 'M_Reservation_ID') THEN
+      v_reservation_id:=Cur_Parameter.P_String;
+      DBMS_OUTPUT.PUT_LINE(' M_Reservation_ID=' || v_reservation_id);
     ELSE
       DBMS_OUTPUT.PUT_LINE('*** Unknown Parameter=' || Cur_Parameter.ParameterName) ;
     END IF;
   END LOOP; -- Get Parameter
   DBMS_OUTPUT.PUT_LINE('  Record_ID=' || v_Record_ID) ;
 
+  IF (v_reservation_id IS NOT NULL) THEN
+    -- If a reservation is given first consume allocated stock.
+    FOR cur_allocated IN (
+        SELECT *
+        FROM m_reservation_stock
+        WHERE m_reservation_id = v_reservation_id
+          AND isallocated = 'Y'
+          AND quantity > 0
+          AND quantity <> COALESCE(releasedqty, 0)
+          AND m_locator_id IS NOT NULL
+    ) LOOP
+      INSERT INTO m_stock_proposed (
+          m_stock_proposed_id, ad_client_id, ad_org_id, isactive, 
+          created , createdby, updated, updatedby,
+          ad_pinstance_id, priority, m_storage_detail_id, quantity, qtyorder
+      ) VALUES (
+          get_uuid(), v_ClientID, v_OrgID, 'Y',
+          now(), v_AD_User_ID, now(), v_AD_User_ID,
+          pinstance_id, v_Count, cur_allocated.m_storage_detail_id, cur_allocated.quantity - COALESCE(cur_allocated.releasedqty, 0), null
+      );
+      v_Count:=v_Count+1;
+    END LOOP;
+  END IF;
+
   -- Insert Values from Auxiliar STOCK
   FOR Cur_Stock IN (
       SELECT ms.m_storage_detail_id, COALESCE(ms.quantity, 0) AS qty, ms.qtyorder AS qtyorder, ms.UPDATED AS lastupdated
@@ -165,7 +194,8 @@
 
   -- Insert Values from Storage Detail
   FOR Cur_Stock  IN (
-      SELECT t.m_storage_detail_id, COALESCE(t.qtyonhand, 0) AS qty, t.qtyorderonhand AS qtyorder, l.priorityno, t.UPDATED AS lastupdated
+      SELECT t.m_storage_detail_id, COALESCE(t.qtyonhand - COALESCE(res.reservedqty, 0), 0) AS qty, t.qtyorderonhand AS qtyorder, l.priorityno, t.UPDATED AS lastupdated,
+      t.qtyonhand, COALESCE(res.reservedqty, 0) as reservedqty
       FROM m_storage_detail t 
           JOIN m_locator l ON t.m_locator_id=l.m_locator_id
           JOIN m_warehouse w ON l.m_warehouse_id = w.m_warehouse_id
@@ -173,6 +203,24 @@
           LEFT JOIN m_attributesetinstance asi ON t.m_attributesetinstance_id = asi.m_attributesetinstance_id
           LEFT JOIN m_product_uom pu ON t.m_product_uom_id = pu.m_product_uom_id
           LEFT JOIN c_uom u ON pu.c_uom_id = u.c_uom_id
+          LEFT JOIN (
+              SELECT r.m_product_id, r.c_uom_id, rs.m_locator_id, COALESCE(rs.m_attributesetinstance_id, '0') as m_attributesetinstance_id,
+                  SUM(rs.quantity - COALESCE(rs.releasedqty, 0)) AS reservedqty
+              FROM m_reservation r
+                  JOIN m_reservation_stock rs ON r.m_reservation_id = rs.m_reservation_id
+              WHERE r.m_product_id = v_productid
+                AND r.c_uom_id = COALESCE(v_uomid, r.c_uom_id)
+                AND rs.m_locator_id IS NOT NULL
+                AND rs.m_locator_id = COALESCE(v_locatorid, rs.m_locator_id)
+                AND COALESCE(rs.m_attributesetinstance_id, '0') = COALESCE(v_attributesetinstanceid, rs.m_attributesetinstance_id, '0')
+                AND r.res_status <> 'CL'
+                AND ((v_reservation_id IS NOT NULL AND rs.isallocated = 'Y')
+                    OR v_reservation_id IS NULL)
+              GROUP BY r.m_product_id, r.c_uom_id, rs.m_locator_id, COALESCE(rs.m_attributesetinstance_id, '0')
+            ) res ON t.m_product_id = res.m_product_id
+                      AND t.c_uom_id = res.c_uom_id
+                      AND COALESCE(t.m_attributesetinstance_id, '0') = res.m_attributesetinstance_id
+                      AND t.m_locator_id = res.m_locator_id
       WHERE t.m_product_id=v_productid
         AND COALESCE(t.c_uom_id, '-1') = COALESCE(v_uomid, t.c_uom_id, '-1')
         AND COALESCE(t.m_product_uom_id, '-1') = COALESCE(v_productuomid, t.m_product_uom_id, '-1')
@@ -181,20 +229,23 @@
         AND COALESCE(w.m_warehouse_id, '-1') = COALESCE(v_warehouseid, w.m_warehouse_id, '-1')
         AND ow.ad_org_id = v_orgid
         AND COALESCE(t.qtyonhand, 0)>0
+        AND t.qtyonhand - COALESCE(res.reservedqty, 0) > 0
       ORDER BY CASE w.m_warehouse_id WHEN COALESCE(v_PriorityWarehouseID, '-1') THEN 0 ELSE 1 END, ow.priority, l.priorityno, asi.created, t.created
   ) LOOP
-    INSERT INTO m_stock_proposed (
-        m_stock_proposed_id, ad_client_id, ad_org_id, isactive, 
-        created , createdby, updated, updatedby,
-        ad_pinstance_id, priority, m_storage_detail_id, quantity, qtyorder,
-        isfinal
-    ) VALUES (
-        get_uuid(), v_ClientID, v_OrgID, 'Y',
-        now(), v_AD_User_ID, now(), v_AD_User_ID,
-        pinstance_id, v_Count, Cur_Stock.M_STORAGE_DETAIL_ID, Cur_Stock.Qty, Cur_Stock.QtyOrder,
-        'Y'
-    );
-    v_Count:=v_Count+1;
+    IF (cur_stock.qtyonhand > cur_stock.reservedqty) THEN
+      INSERT INTO m_stock_proposed (
+          m_stock_proposed_id, ad_client_id, ad_org_id, isactive, 
+          created , createdby, updated, updatedby,
+          ad_pinstance_id, priority, m_storage_detail_id, quantity, qtyorder,
+          isfinal
+      ) VALUES (
+          get_uuid(), v_ClientID, v_OrgID, 'Y',
+          now(), v_AD_User_ID, now(), v_AD_User_ID,
+          pinstance_id, v_Count, Cur_Stock.M_STORAGE_DETAIL_ID, Cur_Stock.Qty, Cur_Stock.QtyOrder,
+          'Y'
+      );
+      v_Count:=v_Count+1;
+    END IF;
   END LOOP;
 
   --M_Get_Stock - Finish Process Extension Point
--- a/src-db/database/model/functions/M_INOUT_CREATE.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/model/functions/M_INOUT_CREATE.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -403,6 +403,7 @@
                   v_pinstance_result AD_PInstance.result%TYPE;
                   v_pinstance_msg AD_PInstance.errormsg%TYPE;
                   v_warehouse_rule_id VARCHAR2(32);
+                  v_reservation_id VARCHAR2(32);
                 BEGIN
                   v_pinstance_id := get_uuid();
                   INSERT INTO ad_pinstance (
@@ -430,6 +431,21 @@
                   WHERE c_orderline_id = ol.c_orderline_id;
                   AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '100', 'M_Warehouse_Rule_ID', v_warehouse_rule_id, null, null, null, null, null);
 
+                  SELECT count(*) INTO v_count
+                  FROM m_reservation
+                  WHERE res_status != 'CL'
+                    AND c_orderline_id = ol.c_orderline_id;
+                  IF (v_count > 0) THEN
+                    IF (v_count > 1) THEN
+                      RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
+                    END IF;
+                    SELECT MAX(m_reservation_id) INTO v_reservation_id
+                    FROM m_reservation
+                    WHERE res_status != 'CL'
+                      AND c_orderline_id = ol.c_orderline_id;
+                    AD_PINSTANCE_PARA_INSERT(v_pinstance_id, '110', 'M_Reservation_ID', v_reservation_id, null, null, null, null, null);
+                  END IF;
+
                   M_GET_STOCK(v_pinstance_ID, 'N');
                   -- Check result
                   SELECT result, errormsg
--- a/src-db/database/model/functions/M_RESERVATION_CONSUMPTION.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/model/functions/M_RESERVATION_CONSUMPTION.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -69,7 +69,7 @@
 BEGIN
   SELECT r.m_product_id, r.c_uom_id, COALESCE(r.reservedqty, 0), COALESCE(r.releasedqty, 0), r.quantity - COALESCE(r.reservedqty, 0),
       COALESCE(SUM(alloc.quantity - COALESCE(alloc.releasedqty, 0)), 0),
-      r.m_warehouse_id, r.m_locator_id, r.m_attributesetinstance_id, res_status
+      r.m_warehouse_id, r.m_locator_id, r.m_attributesetinstance_id, r.res_status
     INTO v_product_id, v_uom_id, v_this_reservedqty, v_this_releasedqty, v_this_notreserved,
       v_this_allocated,
       v_dim_warehouse_id, v_dim_locator_id, v_dim_asi_id, v_res_status
@@ -77,7 +77,7 @@
       LEFT JOIN m_reservation_stock alloc ON r.m_reservation_id = alloc.m_reservation_id AND alloc.isallocated = 'Y'
   WHERE r.m_reservation_id = p_reservation_id
   GROUP BY r.m_product_id, r.c_uom_id, r.reservedqty, r.releasedqty, r.quantity,
-      r.m_warehouse_id, r.m_locator_id, r.m_attributesetinstance_id;
+      r.m_warehouse_id, r.m_locator_id, r.m_attributesetinstance_id, r.res_status;
   v_this_noalloc := v_this_reservedqty - v_this_allocated;
 
   IF (v_res_status = 'HO') THEN
--- a/src-db/database/model/functions/M_RESERVATION_POST.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/model/functions/M_RESERVATION_POST.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -38,6 +38,8 @@
   v_resstatus       VARCHAR2(60);
   v_newstatus       VARCHAR2(60);
   v_newaction       VARCHAR2(60);
+  v_orderline_id    VARCHAR2(32);
+  v_count           NUMBER;
 
 TYPE RECORD IS REF CURSOR;
   Cur_Parameter RECORD;
@@ -71,8 +73,8 @@
   END IF;
 
 BEGIN
-  SELECT res_status
-    INTO v_resstatus
+  SELECT res_status, c_orderline_id
+    INTO v_resstatus, v_orderline_id
   FROM m_reservation
   WHERE m_reservation_id = v_reservation_id;
   
@@ -83,9 +85,19 @@
     IF (v_resstatus != 'DR') THEN
       RAISE_APPLICATION_ERROR(-20000, '@ActionNotSupported@');
     END IF;
+    IF (v_orderline_id IS NOT NULL) THEN
+      SELECT count(*) INTO v_count
+      FROM m_reservation
+      WHERE c_orderline_id = v_orderline_id
+        AND res_status != 'CL'
+        AND m_reservation_id != v_reservation_id;
+      IF (v_count > 0) THEN
+        RAISE_APPLICATION_ERROR(-20000, '@SOLineWithMoreThanOneOpenReservation@');
+      END IF;
+    END IF;
+
     v_newstatus := 'CO';
     v_newaction := 'HO';
-    
     -- ADD RESERVED STOCK
     M_RESERVE_STOCK_AUTO(v_reservation_id, v_user_id, v_message);
 
--- a/src-db/database/model/tables/MRP_RUN_PURCHASE.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/model/tables/MRP_RUN_PURCHASE.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -85,6 +85,10 @@
         <default><![CDATA[N]]></default>
         <onCreateDefault/>
       </column>
+      <column name="CREATE_RESERVATIONS" primaryKey="false" required="false" type="CHAR" size="1" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
       <foreign-key foreignTable="AD_CLIENT" name="AD_CLIENT_RUN_PURCHASE">
         <reference local="AD_CLIENT_ID" foreign="AD_CLIENT_ID"/>
       </foreign-key>
@@ -109,6 +113,7 @@
       <foreign-key foreignTable="C_BPARTNER" name="MRP_RUN_PURCHASE_VENDOR">
         <reference local="VENDOR_ID" foreign="C_BPARTNER_ID"/>
       </foreign-key>
+      <check name="MRP_RUN_PURCHASE"><![CDATA[CREATE_RESERVATIONS IN ('Y', 'N')]]></check>
       <check name="MRP_RUN_PURCHASE_ISACTIVE_CHK"><![CDATA[ISACTIVE IN ('Y', 'N')]]></check>
       <check name="MRP_RUN_PURCHASE_LAUNCHPO_CHK"><![CDATA[LAUNCHPO IN ('Y', 'N')]]></check>
       <check name="MRP_RUN_PURCHASE_SIMULATE_CHK"><![CDATA[SIMULATE IN ('Y', 'N')]]></check>
--- a/src-db/database/model/tables/M_PRODUCT_ORG.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/model/tables/M_PRODUCT_ORG.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -81,6 +81,10 @@
         <default/>
         <onCreateDefault/>
       </column>
+      <column name="M_PRODUCT_PO_ID" primaryKey="false" required="false" type="VARCHAR" size="32" autoIncrement="false">
+        <default/>
+        <onCreateDefault/>
+      </column>
       <foreign-key foreignTable="MRP_PLANNINGMETHOD" name="M_PRDCT_RG_MRP_PLNNNGMTHD">
         <reference local="MRP_PLANNINGMETHOD_ID" foreign="MRP_PLANNINGMETHOD_ID"/>
       </foreign-key>
@@ -99,6 +103,9 @@
       <foreign-key foreignTable="MRP_PLANNER" name="M_PRODUCT_ORG_MRP_PLANNER">
         <reference local="MRP_PLANNER_ID" foreign="MRP_PLANNER_ID"/>
       </foreign-key>
+      <foreign-key foreignTable="M_PRODUCT_PO" name="M_PRODUCT_ORG_PRODUCTPO">
+        <reference local="M_PRODUCT_PO_ID" foreign="M_PRODUCT_PO_ID"/>
+      </foreign-key>
       <unique name="M_PRODUCT_ORG_ORG_PRODUCT_UN">
         <unique-column name="AD_ORG_ID"/>
         <unique-column name="M_PRODUCT_ID"/>
--- a/src-db/database/sourcedata/AD_COLUMN.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/sourcedata/AD_COLUMN.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -293230,6 +293230,75 @@
 <!--CDE4B52D8B583968E040007F01000910-->  <ISUSEDSEQUENCE><![CDATA[N]]></ISUSEDSEQUENCE>
 <!--CDE4B52D8B583968E040007F01000910--></AD_COLUMN>
 
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F--><AD_COLUMN>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <AD_COLUMN_ID><![CDATA[CDFFE9CA0CA4A5D4E040007F01001A9F]]></AD_COLUMN_ID>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <NAME><![CDATA[Create Reservations]]></NAME>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <COLUMNNAME><![CDATA[Create_Reservations]]></COLUMNNAME>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <AD_TABLE_ID><![CDATA[800220]]></AD_TABLE_ID>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <AD_REFERENCE_ID><![CDATA[28]]></AD_REFERENCE_ID>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <FIELDLENGTH><![CDATA[1]]></FIELDLENGTH>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISKEY><![CDATA[N]]></ISKEY>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISPARENT><![CDATA[N]]></ISPARENT>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISMANDATORY><![CDATA[N]]></ISMANDATORY>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISUPDATEABLE><![CDATA[Y]]></ISUPDATEABLE>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISIDENTIFIER><![CDATA[N]]></ISIDENTIFIER>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <SEQNO><![CDATA[220]]></SEQNO>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISTRANSLATED><![CDATA[N]]></ISTRANSLATED>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISENCRYPTED><![CDATA[N]]></ISENCRYPTED>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISSELECTIONCOLUMN><![CDATA[N]]></ISSELECTIONCOLUMN>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <AD_ELEMENT_ID><![CDATA[CE0CE91E4BFD17FDE040007F0100600C]]></AD_ELEMENT_ID>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <AD_PROCESS_ID><![CDATA[7CB6B4D1ECCF4036B3F111D2CF11AADE]]></AD_PROCESS_ID>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISSESSIONATTR><![CDATA[N]]></ISSESSIONATTR>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISSECONDARYKEY><![CDATA[N]]></ISSECONDARYKEY>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISDESENCRYPTABLE><![CDATA[N]]></ISDESENCRYPTABLE>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <DEVELOPMENTSTATUS><![CDATA[RE]]></DEVELOPMENTSTATUS>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <POSITION><![CDATA[22]]></POSITION>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISTRANSIENT><![CDATA[N]]></ISTRANSIENT>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISAUTOSAVE><![CDATA[Y]]></ISAUTOSAVE>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <VALIDATEONNEW><![CDATA[Y]]></VALIDATEONNEW>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <IMAGESIZEVALUESACTION><![CDATA[N]]></IMAGESIZEVALUESACTION>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F-->  <ISUSEDSEQUENCE><![CDATA[N]]></ISUSEDSEQUENCE>
+<!--CDFFE9CA0CA4A5D4E040007F01001A9F--></AD_COLUMN>
+
+<!--CE0F0D775D8D6C8FE040007F01007137--><AD_COLUMN>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <AD_COLUMN_ID><![CDATA[CE0F0D775D8D6C8FE040007F01007137]]></AD_COLUMN_ID>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <NAME><![CDATA[Preferred Vendor]]></NAME>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <HELP><![CDATA[Identifies a product preferred vendor.]]></HELP>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <COLUMNNAME><![CDATA[M_Product_Po_ID]]></COLUMNNAME>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <AD_TABLE_ID><![CDATA[800205]]></AD_TABLE_ID>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <AD_REFERENCE_ID><![CDATA[19]]></AD_REFERENCE_ID>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <AD_VAL_RULE_ID><![CDATA[197EFF9A92394AD183B4034E2038F61D]]></AD_VAL_RULE_ID>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <FIELDLENGTH><![CDATA[32]]></FIELDLENGTH>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISKEY><![CDATA[N]]></ISKEY>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISPARENT><![CDATA[N]]></ISPARENT>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISMANDATORY><![CDATA[N]]></ISMANDATORY>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISUPDATEABLE><![CDATA[Y]]></ISUPDATEABLE>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISIDENTIFIER><![CDATA[N]]></ISIDENTIFIER>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <SEQNO><![CDATA[210]]></SEQNO>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISTRANSLATED><![CDATA[N]]></ISTRANSLATED>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISENCRYPTED><![CDATA[N]]></ISENCRYPTED>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISSELECTIONCOLUMN><![CDATA[N]]></ISSELECTIONCOLUMN>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <AD_ELEMENT_ID><![CDATA[20D54DC47FFA4A80AFA6063D303A7715]]></AD_ELEMENT_ID>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISSESSIONATTR><![CDATA[N]]></ISSESSIONATTR>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISSECONDARYKEY><![CDATA[N]]></ISSECONDARYKEY>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISDESENCRYPTABLE><![CDATA[N]]></ISDESENCRYPTABLE>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <DEVELOPMENTSTATUS><![CDATA[RE]]></DEVELOPMENTSTATUS>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <POSITION><![CDATA[21]]></POSITION>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISTRANSIENT><![CDATA[N]]></ISTRANSIENT>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISAUTOSAVE><![CDATA[Y]]></ISAUTOSAVE>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <VALIDATEONNEW><![CDATA[Y]]></VALIDATEONNEW>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <IMAGESIZEVALUESACTION><![CDATA[N]]></IMAGESIZEVALUESACTION>
+<!--CE0F0D775D8D6C8FE040007F01007137-->  <ISUSEDSEQUENCE><![CDATA[N]]></ISUSEDSEQUENCE>
+<!--CE0F0D775D8D6C8FE040007F01007137--></AD_COLUMN>
+
 <!--CEC2AA7C93F4423A97CE96BDD1C4B49F--><AD_COLUMN>
 <!--CEC2AA7C93F4423A97CE96BDD1C4B49F-->  <AD_COLUMN_ID><![CDATA[CEC2AA7C93F4423A97CE96BDD1C4B49F]]></AD_COLUMN_ID>
 <!--CEC2AA7C93F4423A97CE96BDD1C4B49F-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src-db/database/sourcedata/AD_ELEMENT.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/sourcedata/AD_ELEMENT.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -20961,6 +20961,19 @@
 <!--204EED0BC11646F083B7E226CA5E5667-->  <ISGLOSSARY><![CDATA[N]]></ISGLOSSARY>
 <!--204EED0BC11646F083B7E226CA5E5667--></AD_ELEMENT>
 
+<!--20D54DC47FFA4A80AFA6063D303A7715--><AD_ELEMENT>
+<!--20D54DC47FFA4A80AFA6063D303A7715-->  <AD_ELEMENT_ID><![CDATA[20D54DC47FFA4A80AFA6063D303A7715]]></AD_ELEMENT_ID>
+<!--20D54DC47FFA4A80AFA6063D303A7715-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--20D54DC47FFA4A80AFA6063D303A7715-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--20D54DC47FFA4A80AFA6063D303A7715-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--20D54DC47FFA4A80AFA6063D303A7715-->  <COLUMNNAME><![CDATA[M_Product_PO_ID]]></COLUMNNAME>
+<!--20D54DC47FFA4A80AFA6063D303A7715-->  <NAME><![CDATA[Preferred Vendor]]></NAME>
+<!--20D54DC47FFA4A80AFA6063D303A7715-->  <PRINTNAME><![CDATA[Preferred Vendor]]></PRINTNAME>
+<!--20D54DC47FFA4A80AFA6063D303A7715-->  <HELP><![CDATA[Identifies a product preferred vendor.]]></HELP>
+<!--20D54DC47FFA4A80AFA6063D303A7715-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--20D54DC47FFA4A80AFA6063D303A7715-->  <ISGLOSSARY><![CDATA[N]]></ISGLOSSARY>
+<!--20D54DC47FFA4A80AFA6063D303A7715--></AD_ELEMENT>
+
 <!--227C67C05861481E803697C2153404AA--><AD_ELEMENT>
 <!--227C67C05861481E803697C2153404AA-->  <AD_ELEMENT_ID><![CDATA[227C67C05861481E803697C2153404AA]]></AD_ELEMENT_ID>
 <!--227C67C05861481E803697C2153404AA-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
@@ -28830,6 +28843,18 @@
 <!--CDE4B52D8B593968E040007F01000910-->  <ISGLOSSARY><![CDATA[N]]></ISGLOSSARY>
 <!--CDE4B52D8B593968E040007F01000910--></AD_ELEMENT>
 
+<!--CE0CE91E4BFD17FDE040007F0100600C--><AD_ELEMENT>
+<!--CE0CE91E4BFD17FDE040007F0100600C-->  <AD_ELEMENT_ID><![CDATA[CE0CE91E4BFD17FDE040007F0100600C]]></AD_ELEMENT_ID>
+<!--CE0CE91E4BFD17FDE040007F0100600C-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--CE0CE91E4BFD17FDE040007F0100600C-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--CE0CE91E4BFD17FDE040007F0100600C-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--CE0CE91E4BFD17FDE040007F0100600C-->  <COLUMNNAME><![CDATA[Create_Reservations]]></COLUMNNAME>
+<!--CE0CE91E4BFD17FDE040007F0100600C-->  <NAME><![CDATA[Create Reservations]]></NAME>
+<!--CE0CE91E4BFD17FDE040007F0100600C-->  <PRINTNAME><![CDATA[Create Reservations]]></PRINTNAME>
+<!--CE0CE91E4BFD17FDE040007F0100600C-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--CE0CE91E4BFD17FDE040007F0100600C-->  <ISGLOSSARY><![CDATA[N]]></ISGLOSSARY>
+<!--CE0CE91E4BFD17FDE040007F0100600C--></AD_ELEMENT>
+
 <!--CEFF12045E10490496BA6CE8AC1AD27A--><AD_ELEMENT>
 <!--CEFF12045E10490496BA6CE8AC1AD27A-->  <AD_ELEMENT_ID><![CDATA[CEFF12045E10490496BA6CE8AC1AD27A]]></AD_ELEMENT_ID>
 <!--CEFF12045E10490496BA6CE8AC1AD27A-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src-db/database/sourcedata/AD_FIELD.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/sourcedata/AD_FIELD.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -233102,6 +233102,60 @@
 <!--CDCEB8A436794783BE180739C34F49FE-->  <EM_OBUIAPP_SHOWSUMMARY><![CDATA[N]]></EM_OBUIAPP_SHOWSUMMARY>
 <!--CDCEB8A436794783BE180739C34F49FE--></AD_FIELD>
 
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F--><AD_FIELD>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <AD_FIELD_ID><![CDATA[CDFFE9CA0CA8A5D4E040007F01001A9F]]></AD_FIELD_ID>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <NAME><![CDATA[Create Reservations]]></NAME>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <ISCENTRALLYMAINTAINED><![CDATA[Y]]></ISCENTRALLYMAINTAINED>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <AD_TAB_ID><![CDATA[800258]]></AD_TAB_ID>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <AD_COLUMN_ID><![CDATA[CDFFE9CA0CA4A5D4E040007F01001A9F]]></AD_COLUMN_ID>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <IGNOREINWAD><![CDATA[N]]></IGNOREINWAD>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <ISDISPLAYED><![CDATA[Y]]></ISDISPLAYED>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <DISPLAYLENGTH><![CDATA[1]]></DISPLAYLENGTH>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <ISREADONLY><![CDATA[N]]></ISREADONLY>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <SEQNO><![CDATA[160]]></SEQNO>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <ISSAMELINE><![CDATA[N]]></ISSAMELINE>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <ISFIELDONLY><![CDATA[N]]></ISFIELDONLY>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <ISENCRYPTED><![CDATA[N]]></ISENCRYPTED>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <SHOWINRELATION><![CDATA[N]]></SHOWINRELATION>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <ISFIRSTFOCUSEDFIELD><![CDATA[N]]></ISFIRSTFOCUSEDFIELD>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <STARTINODDCOLUMN><![CDATA[N]]></STARTINODDCOLUMN>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <STARTNEWLINE><![CDATA[N]]></STARTNEWLINE>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <ISSHOWNINSTATUSBAR><![CDATA[N]]></ISSHOWNINSTATUSBAR>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F-->  <EM_OBUIAPP_SHOWSUMMARY><![CDATA[N]]></EM_OBUIAPP_SHOWSUMMARY>
+<!--CDFFE9CA0CA8A5D4E040007F01001A9F--></AD_FIELD>
+
+<!--CE0F0D775DAF6C8FE040007F01007137--><AD_FIELD>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <AD_FIELD_ID><![CDATA[CE0F0D775DAF6C8FE040007F01007137]]></AD_FIELD_ID>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <NAME><![CDATA[Preferred Vendor]]></NAME>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <HELP><![CDATA[Identifies a product preferred vendor.]]></HELP>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <ISCENTRALLYMAINTAINED><![CDATA[Y]]></ISCENTRALLYMAINTAINED>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <AD_TAB_ID><![CDATA[800241]]></AD_TAB_ID>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <AD_COLUMN_ID><![CDATA[CE0F0D775D8D6C8FE040007F01007137]]></AD_COLUMN_ID>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <IGNOREINWAD><![CDATA[N]]></IGNOREINWAD>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <ISDISPLAYED><![CDATA[Y]]></ISDISPLAYED>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <DISPLAYLENGTH><![CDATA[32]]></DISPLAYLENGTH>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <ISREADONLY><![CDATA[N]]></ISREADONLY>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <SEQNO><![CDATA[115]]></SEQNO>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <ISSAMELINE><![CDATA[N]]></ISSAMELINE>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <ISFIELDONLY><![CDATA[N]]></ISFIELDONLY>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <ISENCRYPTED><![CDATA[N]]></ISENCRYPTED>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <SHOWINRELATION><![CDATA[Y]]></SHOWINRELATION>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <ISFIRSTFOCUSEDFIELD><![CDATA[N]]></ISFIRSTFOCUSEDFIELD>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <GRID_SEQNO><![CDATA[115]]></GRID_SEQNO>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <STARTINODDCOLUMN><![CDATA[N]]></STARTINODDCOLUMN>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <STARTNEWLINE><![CDATA[N]]></STARTNEWLINE>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <ISSHOWNINSTATUSBAR><![CDATA[N]]></ISSHOWNINSTATUSBAR>
+<!--CE0F0D775DAF6C8FE040007F01007137-->  <EM_OBUIAPP_SHOWSUMMARY><![CDATA[N]]></EM_OBUIAPP_SHOWSUMMARY>
+<!--CE0F0D775DAF6C8FE040007F01007137--></AD_FIELD>
+
 <!--CE12B62FCF03457CBF7C23202FBF1B34--><AD_FIELD>
 <!--CE12B62FCF03457CBF7C23202FBF1B34-->  <AD_FIELD_ID><![CDATA[CE12B62FCF03457CBF7C23202FBF1B34]]></AD_FIELD_ID>
 <!--CE12B62FCF03457CBF7C23202FBF1B34-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src-db/database/sourcedata/AD_MODEL_OBJECT.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/sourcedata/AD_MODEL_OBJECT.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -3113,6 +3113,18 @@
 <!--18DA249486964F65818793F51AF50058-->  <LOADONSTARTUP><![CDATA[1]]></LOADONSTARTUP>
 <!--18DA249486964F65818793F51AF50058--></AD_MODEL_OBJECT>
 
+<!--1E2308B9F7794479A58F6F20A3522067--><AD_MODEL_OBJECT>
+<!--1E2308B9F7794479A58F6F20A3522067-->  <AD_MODEL_OBJECT_ID><![CDATA[1E2308B9F7794479A58F6F20A3522067]]></AD_MODEL_OBJECT_ID>
+<!--1E2308B9F7794479A58F6F20A3522067-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--1E2308B9F7794479A58F6F20A3522067-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--1E2308B9F7794479A58F6F20A3522067-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--1E2308B9F7794479A58F6F20A3522067-->  <ACTION><![CDATA[P]]></ACTION>
+<!--1E2308B9F7794479A58F6F20A3522067-->  <CLASSNAME><![CDATA[org.openbravo.erpCommon.ad_process.MRPPurchaseCreateReservations]]></CLASSNAME>
+<!--1E2308B9F7794479A58F6F20A3522067-->  <ISDEFAULT><![CDATA[Y]]></ISDEFAULT>
+<!--1E2308B9F7794479A58F6F20A3522067-->  <AD_PROCESS_ID><![CDATA[7CB6B4D1ECCF4036B3F111D2CF11AADE]]></AD_PROCESS_ID>
+<!--1E2308B9F7794479A58F6F20A3522067-->  <OBJECT_TYPE><![CDATA[S]]></OBJECT_TYPE>
+<!--1E2308B9F7794479A58F6F20A3522067--></AD_MODEL_OBJECT>
+
 <!--1EC03C207CB741AE8C94DBF0340E2429--><AD_MODEL_OBJECT>
 <!--1EC03C207CB741AE8C94DBF0340E2429-->  <AD_MODEL_OBJECT_ID><![CDATA[1EC03C207CB741AE8C94DBF0340E2429]]></AD_MODEL_OBJECT_ID>
 <!--1EC03C207CB741AE8C94DBF0340E2429-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src-db/database/sourcedata/AD_PROCESS.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/sourcedata/AD_PROCESS.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -5668,6 +5668,25 @@
 <!--75F83D534E764C7C8781FFA6C08E87ED-->  <ISADVANCEDFEATURE><![CDATA[N]]></ISADVANCEDFEATURE>
 <!--75F83D534E764C7C8781FFA6C08E87ED--></AD_PROCESS>
 
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE--><AD_PROCESS>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <AD_PROCESS_ID><![CDATA[7CB6B4D1ECCF4036B3F111D2CF11AADE]]></AD_PROCESS_ID>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <VALUE><![CDATA[MRPPurchaseCreateReservations]]></VALUE>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <NAME><![CDATA[Create Reservations]]></NAME>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <ACCESSLEVEL><![CDATA[1]]></ACCESSLEVEL>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <ISUSERSTARTABLE><![CDATA[N]]></ISUSERSTARTABLE>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <ISREPORT><![CDATA[N]]></ISREPORT>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <ISDIRECTPRINT><![CDATA[N]]></ISDIRECTPRINT>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <ISBACKGROUND><![CDATA[N]]></ISBACKGROUND>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <ISJASPER><![CDATA[N]]></ISJASPER>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <ISEXTERNALSERVICE><![CDATA[N]]></ISEXTERNALSERVICE>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <UIPATTERN><![CDATA[S]]></UIPATTERN>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE-->  <ISADVANCEDFEATURE><![CDATA[N]]></ISADVANCEDFEATURE>
+<!--7CB6B4D1ECCF4036B3F111D2CF11AADE--></AD_PROCESS>
+
 <!--7E9E0E62BF1341B796A57F3AC4A2D861--><AD_PROCESS>
 <!--7E9E0E62BF1341B796A57F3AC4A2D861-->  <AD_PROCESS_ID><![CDATA[7E9E0E62BF1341B796A57F3AC4A2D861]]></AD_PROCESS_ID>
 <!--7E9E0E62BF1341B796A57F3AC4A2D861-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src-db/database/sourcedata/AD_PROCESS_PARA.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/sourcedata/AD_PROCESS_PARA.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -4411,6 +4411,26 @@
 <!--B8F7FEBD957D4215A414BF284D739C83-->  <AD_ELEMENT_ID><![CDATA[193]]></AD_ELEMENT_ID>
 <!--B8F7FEBD957D4215A414BF284D739C83--></AD_PROCESS_PARA>
 
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D--><AD_PROCESS_PARA>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <AD_PROCESS_PARA_ID><![CDATA[C4B24CB8FFC84E5FB5E7AE6BC15B913D]]></AD_PROCESS_PARA_ID>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <NAME><![CDATA[Warehouse]]></NAME>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <DESCRIPTION><![CDATA[The location where products arrive to or are sent from.]]></DESCRIPTION>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <HELP><![CDATA[The Warehouse identifies a unique Warehouse where products are stored or Services are provided.]]></HELP>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <AD_PROCESS_ID><![CDATA[7CB6B4D1ECCF4036B3F111D2CF11AADE]]></AD_PROCESS_ID>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <SEQNO><![CDATA[10]]></SEQNO>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <AD_REFERENCE_ID><![CDATA[19]]></AD_REFERENCE_ID>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <COLUMNNAME><![CDATA[M_Warehouse_ID]]></COLUMNNAME>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <ISCENTRALLYMAINTAINED><![CDATA[Y]]></ISCENTRALLYMAINTAINED>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <FIELDLENGTH><![CDATA[0]]></FIELDLENGTH>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <ISMANDATORY><![CDATA[Y]]></ISMANDATORY>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <ISRANGE><![CDATA[N]]></ISRANGE>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <DEFAULTVALUE><![CDATA[@#M_Warehouse_ID@]]></DEFAULTVALUE>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D-->  <AD_ELEMENT_ID><![CDATA[459]]></AD_ELEMENT_ID>
+<!--C4B24CB8FFC84E5FB5E7AE6BC15B913D--></AD_PROCESS_PARA>
+
 <!--CB8D3FBA02B84AEFB90CB2AD3D5D6FD5--><AD_PROCESS_PARA>
 <!--CB8D3FBA02B84AEFB90CB2AD3D5D6FD5-->  <AD_PROCESS_PARA_ID><![CDATA[CB8D3FBA02B84AEFB90CB2AD3D5D6FD5]]></AD_PROCESS_PARA_ID>
 <!--CB8D3FBA02B84AEFB90CB2AD3D5D6FD5-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- a/src-db/database/sourcedata/AD_TAB.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/sourcedata/AD_TAB.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -11072,6 +11072,7 @@
 <!--800259-->  <ISSORTTAB><![CDATA[N]]></ISSORTTAB>
 <!--800259-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--800259-->  <UIPATTERN><![CDATA[STD]]></UIPATTERN>
+<!--800259-->  <HQLORDERBYCLAUSE><![CDATA[product, -quantity]]></HQLORDERBYCLAUSE>
 <!--800259-->  <SHOWPARENTBUTTONS><![CDATA[Y]]></SHOWPARENTBUTTONS>
 <!--800259-->  <EM_OBUIAPP_CAN_ADD><![CDATA[N]]></EM_OBUIAPP_CAN_ADD>
 <!--800259-->  <EM_OBUIAPP_CAN_DELETE><![CDATA[N]]></EM_OBUIAPP_CAN_DELETE>
--- a/src-db/database/sourcedata/AD_VAL_RULE.xml	Wed Nov 07 18:34:23 2012 +0100
+++ b/src-db/database/sourcedata/AD_VAL_RULE.xml	Mon Nov 12 09:56:06 2012 +0100
@@ -1283,6 +1283,17 @@
 <!--16C9E72407D94B93968D1E81F32D537D-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
 <!--16C9E72407D94B93968D1E81F32D537D--></AD_VAL_RULE>
 
+<!--197EFF9A92394AD183B4034E2038F61D--><AD_VAL_RULE>
+<!--197EFF9A92394AD183B4034E2038F61D-->  <AD_VAL_RULE_ID><![CDATA[197EFF9A92394AD183B4034E2038F61D]]></AD_VAL_RULE_ID>
+<!--197EFF9A92394AD183B4034E2038F61D-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--197EFF9A92394AD183B4034E2038F61D-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--197EFF9A92394AD183B4034E2038F61D-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--197EFF9A92394AD183B4034E2038F61D-->  <NAME><![CDATA[M_Product_PO of the m_product_id]]></NAME>
+<!--197EFF9A92394AD183B4034E2038F61D-->  <TYPE><![CDATA[S]]></TYPE>
+<!--197EFF9A92394AD183B4034E2038F61D-->  <CODE><![CDATA[m_product_po.discontinued = 'N' and m_product_po.m_product_id = @M_Product_ID@]]></CODE>
+<!--197EFF9A92394AD183B4034E2038F61D-->  <AD_MODULE_ID><![CDATA[0]]></AD_MODULE_ID>
+<!--197EFF9A92394AD183B4034E2038F61D--></AD_VAL_RULE>
+
 <!--299FA667CF374AC5ACC74739C3251134--><AD_VAL_RULE>
 <!--299FA667CF374AC5ACC74739C3251134-->  <AD_VAL_RULE_ID><![CDATA[299FA667CF374AC5ACC74739C3251134]]></AD_VAL_RULE_ID>
 <!--299FA667CF374AC5ACC74739C3251134-->  <AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/openbravo/erpCommon/ad_process/MRPPurchaseCreateReservations.java	Mon Nov 12 09:56:06 2012 +0100
@@ -0,0 +1,224 @@
+/*
+ *************************************************************************
+ * The contents of this file are subject to the Openbravo  Public  License
+ * Version  1.1  (the  "License"),  being   the  Mozilla   Public  License
+ * Version 1.1  with a permitted attribution clause; you may not  use this
+ * file except in compliance with the License. You  may  obtain  a copy of
+ * the License at http://www.openbravo.com/legal/license.html 
+ * Software distributed under the License  is  distributed  on  an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
+ * License for the specific  language  governing  rights  and  limitations
+ * under the License. 
+ * The Original Code is Openbravo ERP. 
+ * The Initial Developer of the Original Code is Openbravo SLU 
+ * All portions are Copyright (C) 2012 Openbravo SLU 
+ * All Rights Reserved. 
+ * Contributor(s):  ______________________________________.
+ ************************************************************************
+ */
+package org.openbravo.erpCommon.ad_process;
+
+import java.math.BigDecimal;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.hibernate.ScrollMode;
+import org.hibernate.ScrollableResults;
+import org.openbravo.base.exception.OBException;
+import org.openbravo.dal.core.DalUtil;
+import org.openbravo.dal.core.OBContext;
+import org.openbravo.dal.core.SessionHandler;
+import org.openbravo.dal.service.OBDal;
+import org.openbravo.dal.service.OBQuery;
+import org.openbravo.erpCommon.utility.OBError;
+import org.openbravo.erpCommon.utility.OBMessageUtils;
+import org.openbravo.materialmgmt.ReservationUtils;
+import org.openbravo.model.ad.process.ProcessInstance;
+import org.openbravo.model.ad.ui.Process;
+import org.openbravo.model.common.order.Order;
+import org.openbravo.model.common.order.OrderLine;
+import org.openbravo.model.materialmgmt.onhandquantity.Reservation;
+import org.openbravo.model.mrp.PurchasingRun;
+import org.openbravo.model.mrp.PurchasingRunLine;
+import org.openbravo.scheduling.ProcessBundle;
+import org.openbravo.scheduling.ProcessLogger;
+import org.openbravo.service.db.CallProcess;
+import org.openbravo.service.db.DalBaseProcess;
+import org.openbravo.service.db.DbUtility;
+
+public class MRPPurchaseCreateReservations extends DalBaseProcess {
+
+  private ProcessLogger logger;
+
+  public void doExecute(ProcessBundle bundle) throws Exception {
+    logger = bundle.getLogger();
+    Map<String, Object> params = bundle.getParams();
+
+    String strMRPRunId = (String) params.get("MRP_Run_Purchase_ID");
+    PurchasingRun mrpPurchaseRun = OBDal.getInstance().get(PurchasingRun.class, strMRPRunId);
+
+    String strMWarehosueID = (String) params.get("mWarehouseId");
+
+    // Execute Create Orders process.
+    OBContext.setAdminMode(true);
+    Process process = null;
+    try {
+      process = OBDal.getInstance().get(Process.class, "800163");
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+    Map<String, String> createOrderParams = new HashMap<String, String>();
+    createOrderParams.put("M_Warehouse_ID", strMWarehosueID);
+    try {
+      final ProcessInstance pinstance = CallProcess.getInstance().call(process, strMRPRunId,
+          createOrderParams);
+
+      if (pinstance.getResult() == 0L) {
+        OBDal.getInstance().rollbackAndClose();
+        OBError oberror = OBMessageUtils.getProcessInstanceMessage(pinstance);
+        bundle.setResult(oberror);
+        return;
+      }
+    } catch (Exception e) {
+      OBDal.getInstance().rollbackAndClose();
+      OBError messsage = OBMessageUtils.translateError(DbUtility.getUnderlyingSQLException(e)
+          .getMessage());
+      bundle.setResult(messsage);
+      return;
+    }
+
+    // Create reservations
+    ScrollableResults outgoingRLs = getPRLinesOutgoing(mrpPurchaseRun);
+    ScrollableResults incomingRLs = getPRLinesIncoming(mrpPurchaseRun);
+    int i = 1;
+    BigDecimal currentStock = BigDecimal.ZERO;
+
+    PurchasingRunLine incomingLine = null;
+    String productID = "";
+    while (outgoingRLs.next()) {
+      PurchasingRunLine outgoingLine = (PurchasingRunLine) outgoingRLs.get(0);
+      if (!productID.equals((String) DalUtil.getId(outgoingLine.getProduct()))) {
+        productID = (String) DalUtil.getId(outgoingLine.getProduct());
+        currentStock = BigDecimal.ZERO;
+      }
+      BigDecimal quantity = outgoingLine.getQuantity().negate();
+      boolean isSalesOrderLine = outgoingLine.getSalesOrderLine() != null
+          && outgoingLine.getSalesOrderLine().getSalesOrder().isSalesTransaction();
+      while (quantity.signum() == 1) {
+        if (currentStock.signum() < 1 && incomingRLs.next()) {
+          incomingLine = (PurchasingRunLine) incomingRLs.get(0);
+          if (!productID.equals((String) DalUtil.getId(outgoingLine.getProduct()))
+              && incomingRLs.next()) {
+            incomingLine = (PurchasingRunLine) incomingRLs.get(0);
+          }
+          currentStock = currentStock.add(incomingLine.getQuantity());
+          if (incomingLine.getTransactionType().equals("PP")
+              && incomingLine.getSalesOrderLine() != null) {
+            OBDal.getInstance().refresh(incomingLine.getSalesOrderLine().getSalesOrder());
+            if (!incomingLine.getSalesOrderLine().getSalesOrder().isProcessed()) {
+              try {
+                processOrder(incomingLine.getSalesOrderLine().getSalesOrder());
+              } catch (OBException e) {
+                OBDal.getInstance().rollbackAndClose();
+                OBError error = OBMessageUtils.translateError(e.getMessage());
+                bundle.setResult(error);
+                return;
+              }
+            }
+          }
+        }
+        BigDecimal consumedQuantity = currentStock.min(quantity);
+        currentStock = currentStock.subtract(consumedQuantity);
+        quantity = quantity.subtract(consumedQuantity);
+        if (isSalesOrderLine) {
+          Reservation reservation = getReservation(outgoingLine.getSalesOrderLine());
+          if (reservation.getReservedQty().compareTo(reservation.getQuantity()) == -1) {
+            if (incomingLine.getTransactionType().equals("PP")
+                && incomingLine.getSalesOrderLine() != null) {
+              ReservationUtils.reserveStockManual(reservation, incomingLine.getSalesOrderLine(),
+                  consumedQuantity);
+            }
+
+            if (quantity.signum() < 1 && reservation.getRESStatus().equals("DR")) {
+              ReservationUtils.processReserve(reservation, "PR");
+            }
+          }
+          OBDal.getInstance().save(reservation);
+          OBDal.getInstance().flush();
+        }
+      }
+      if ((i % 100) == 0) {
+        SessionHandler.getInstance().commitAndStart();
+        OBDal.getInstance().getSession().clear();
+      }
+    }
+    OBError message = new OBError();
+    message.setType("Success");
+    message.setTitle(OBMessageUtils.messageBD("Success"));
+    bundle.setResult(message);
+  }
+
+  private ScrollableResults getPRLinesIncoming(PurchasingRun mrpPurchaseRun) {
+    StringBuffer where = new StringBuffer();
+    where.append(" where " + PurchasingRunLine.PROPERTY_PURCHASINGPLAN + ".id = :purchaserun");
+    where.append("   and " + PurchasingRunLine.PROPERTY_QUANTITY + " > 0");
+    where.append(" order by " + PurchasingRunLine.PROPERTY_PRODUCT + ","
+        + PurchasingRunLine.PROPERTY_PLANNEDDATE + ", CASE "
+        + PurchasingRunLine.PROPERTY_TRANSACTIONTYPE
+        + " WHEN 'ST' THEN 0 WHEN 'MS' THEN 2 ELSE 1 END");
+
+    OBQuery<PurchasingRunLine> soQry = OBDal.getInstance().createQuery(PurchasingRunLine.class,
+        where.toString());
+    soQry.setNamedParameter("purchaserun", mrpPurchaseRun.getId());
+    soQry.setFetchSize(1000);
+    return soQry.scroll(ScrollMode.FORWARD_ONLY);
+  }
+
+  private ScrollableResults getPRLinesOutgoing(PurchasingRun mrpPurchaseRun) {
+    StringBuffer where = new StringBuffer();
+    where.append(" where " + PurchasingRunLine.PROPERTY_PURCHASINGPLAN + ".id = :purchaserun");
+    where.append("   and " + PurchasingRunLine.PROPERTY_QUANTITY + " < 0");
+    where.append(" order by " + PurchasingRunLine.PROPERTY_PRODUCT + ","
+        + PurchasingRunLine.PROPERTY_PLANNEDDATE + ", CASE "
+        + PurchasingRunLine.PROPERTY_TRANSACTIONTYPE
+        + " WHEN 'ST' THEN 0 WHEN 'MS' THEN 2 ELSE 3 END");
+
+    OBQuery<PurchasingRunLine> soQry = OBDal.getInstance().createQuery(PurchasingRunLine.class,
+        where.toString());
+    soQry.setNamedParameter("purchaserun", mrpPurchaseRun.getId());
+    soQry.setFetchSize(1000);
+    return soQry.scroll(ScrollMode.FORWARD_ONLY);
+  }
+
+  private Reservation getReservation(OrderLine salesOrderLine) {
+    OBDal.getInstance().refresh(salesOrderLine);
+    for (Reservation res : salesOrderLine.getMaterialMgmtReservationList()) {
+      if (res.getRESStatus() != "CL") {
+        return res;
+      }
+    }
+    return ReservationUtils.createReserveFromSalesOrderLine(salesOrderLine, false);
+  }
+
+  private void processOrder(Order salesOrder) throws OBException {
+    OBContext.setAdminMode(true);
+    Process process = null;
+    try {
+      process = OBDal.getInstance().get(Process.class, "104");
+    } finally {
+      OBContext.restorePreviousMode();
+    }
+    try {
+      final ProcessInstance pinstance = CallProcess.getInstance().call(process, salesOrder.getId(),
+          null);
+
+      if (pinstance.getResult() == 0L) {
+        OBError oberror = OBMessageUtils.getProcessInstanceMessage(pinstance);
+        throw new OBException(oberror.getMessage());
+      }
+    } catch (Exception e) {
+      Throwable t = DbUtility.getUnderlyingSQLException(e);
+      throw new OBException(OBMessageUtils.parseTranslation(t.getMessage()), t);
+    }
+  }
+}
\ No newline at end of file