private void BindMainOrderGrid()
        {
            // Given some items are already selected on the first step of the order process (CDR Request) // I doubt that
            // Bind the second grid with the selected items for the quantity to be filled by the HCMIS Operator

            if (OrderID == null)
            {
                BLL.OrderDetail or = new BLL.OrderDetail();
                or.LoadByPrimaryKey(-1);
                _dvItemTable = new DataView(new DataTable());

                _dvItemTable.Table.Columns.Add("LocationID");
                _dvItemTable.Table.Columns.Add("Packs");
                _dvItemTable.Table.Columns.Add("QtyPerPack");
                _dvItemTable.Table.Columns.Add("UnitPrice");
                _dvItemTable.Table.Columns.Add("ReceiveDocID");
                _dvItemTable.Table.Columns.Add("Cost");
                _dvItemTable.Table.Columns.Add("FullItemName");
                _dvItemTable.Table.Columns.Add("Manufacturer");
                _dvItemTable.Table.Columns.Add("ManufacturerID");
                _dvItemTable.Table.Columns.Add("Unit");
                _dvItemTable.Table.Columns.Add("Balance");
                _dvItemTable.Table.Columns.Add("Location");
                _dvItemTable.Table.Columns.Add("Supplier");
                _dvItemTable.Table.Columns.Add("account");
                _dvItemTable.Table.Columns.Add("BatchNo");
                _dvItemTable.Table.Columns.Add("ExpDate");
                _dvItemTable.Table.Columns.Add("ItemID");
                _dvItemTable.Table.Columns.Add("UnitID");
                _dvItemTable.Table.Columns.Add("ID");
                _dvItemTable.Table.Columns.Add("StockCode");
                _dvItemTable.Table.Columns.Add("StoreID");
                _dvItemTable.Table.Columns.Add("ApprovedPacks");
                _dvItemTable.Table.Columns.Add("ReceivingLocationID");

            }

            orderGrid.DataSource = _dvItemTable;

            dvPickList = new DataTable();
            dvPickList.Columns.Add("BatchNo");
            dvPickList.Columns.Add("CalculatedCost");
            dvPickList.Columns.Add("ExpDate");
            dvPickList.Columns.Add("AccountName");
            dvPickList.Columns.Add("FullItemName");
            dvPickList.Columns.Add("ManufacturerName");
            dvPickList.Columns.Add("Pack");
            dvPickList.Columns.Add("QtyInSKU");
            dvPickList.Columns.Add("PalletLocation");
            dvPickList.Columns.Add("PalletNo");
            dvPickList.Columns.Add("UnitPrice");
            dvPickList.Columns.Add("LineNum");
            dvPickList.Columns.Add("Unit");
            dvPickList.Columns.Add("WarehouseName");
            dvPickList.Columns.Add("PrintedSTVNumber");
            dvPickList.Columns.Add("PhysicalStoreName");
            dvPickList.Columns.Add("PhysicalStoreTypeName");
            dvPickList.Columns.Add("ActivityConcat");
            dvPickList.Columns.Add("StockCode");
        }
        private void BindMainOrderGrid()
        {
            // Given some items are already selected on the first step of the order process (CDR Request) // I doubt that
            // Bind the second grid with the selected items for the quantity to be filled by the HCMIS Operator

            if (OrderID == null)
            {
                BLL.OrderDetail or = new BLL.OrderDetail();
                or.LoadByPrimaryKey(-1);
                _dvItemTable = new DataView(new DataTable());

                _dvItemTable.Table.Columns.Add("LocationID");
                _dvItemTable.Table.Columns.Add("Packs");
                _dvItemTable.Table.Columns.Add("QtyPerPack");
                _dvItemTable.Table.Columns.Add("UnitPrice");
                _dvItemTable.Table.Columns.Add("ReceiveDocID");
                _dvItemTable.Table.Columns.Add("Cost");
                _dvItemTable.Table.Columns.Add("FullItemName");
                _dvItemTable.Table.Columns.Add("Manufacturer");
                _dvItemTable.Table.Columns.Add("ManufacturerID");
                _dvItemTable.Table.Columns.Add("Unit");
                _dvItemTable.Table.Columns.Add("Balance");
                _dvItemTable.Table.Columns.Add("Location");
                _dvItemTable.Table.Columns.Add("Supplier");
                _dvItemTable.Table.Columns.Add("account");
                _dvItemTable.Table.Columns.Add("BatchNo");
                _dvItemTable.Table.Columns.Add("ExpDate");
                _dvItemTable.Table.Columns.Add("ItemID");
                _dvItemTable.Table.Columns.Add("UnitID");
                _dvItemTable.Table.Columns.Add("ID");
                _dvItemTable.Table.Columns.Add("StockCode");
                _dvItemTable.Table.Columns.Add("StoreID");
                _dvItemTable.Table.Columns.Add("ApprovedPacks");
                _dvItemTable.Table.Columns.Add("ReceivingLocationID");
            }

            orderGrid.DataSource = _dvItemTable;

            dvPickList = new DataTable();
            dvPickList.Columns.Add("BatchNo");
            dvPickList.Columns.Add("CalculatedCost");
            dvPickList.Columns.Add("ExpDate");
            dvPickList.Columns.Add("AccountName");
            dvPickList.Columns.Add("FullItemName");
            dvPickList.Columns.Add("ManufacturerName");
            dvPickList.Columns.Add("Pack");
            dvPickList.Columns.Add("QtyInSKU");
            dvPickList.Columns.Add("PalletLocation");
            dvPickList.Columns.Add("PalletNo");
            dvPickList.Columns.Add("UnitPrice");
            dvPickList.Columns.Add("LineNum");
            dvPickList.Columns.Add("Unit");
            dvPickList.Columns.Add("WarehouseName");
            dvPickList.Columns.Add("PrintedSTVNumber");
            dvPickList.Columns.Add("PhysicalStoreName");
            dvPickList.Columns.Add("PhysicalStoreTypeName");
            dvPickList.Columns.Add("ActivityConcat");
            dvPickList.Columns.Add("StockCode");
        }
        /// <summary>
        /// The code supports manufacturer preference here too but we choose not to prefer manufacturers when looking into the stores.
        /// </summary>
        /// <param name="userID"></param>
        /// <param name="orderDetailID"></param>
        /// <param name="pricedUnpricedBoth"></param>
        /// <param name="bal"> </param>
        /// <param name="markStockoutBit"> </param>
        /// <param name="usableStock"> </param>
        /// <param name="approved"> </param>
        public decimal LoadOptionsForOrderDetail(int userID, int orderDetailID, PriceSettings pricedUnpricedBoth, Balance bal, bool markStockoutBit, out decimal usableStock, out decimal approved)
        {
            decimal avQuantity = 0;

            BLL.OrderDetail orderDetail = new OrderDetail();
            orderDetail.LoadByPrimaryKey(orderDetailID);
            BLL.Order order = new Order();
            order.LoadByPrimaryKey(orderDetail.OrderID);

            Institution ru = new Institution();

            ru.LoadByPrimaryKey(order.RequestedBy);

            int month = EthiopianDate.EthiopianDate.Now.Month;
            int year  = EthiopianDate.EthiopianDate.Now.Year;

            decimal availableQty = 0;

            usableStock = 0;
            approved    = 0;
            int?manufacturerPrefrence = null, unitID = null;

            if (!orderDetail.IsColumnNull("PreferredManufacturerID"))
            {
                manufacturerPrefrence = orderDetail.PreferredManufacturerID;
            }

            DateTime?expPreferrence = null;

            if (!orderDetail.IsColumnNull("PreferredExpiryDate"))
            {
                expPreferrence = orderDetail.PreferredExpiryDate;
            }

            int?preferredPhysicalStoreID = null;

            if (!orderDetail.IsColumnNull("PreferredPhysicalStoreID"))
            {
                preferredPhysicalStoreID = orderDetail.PreferredPhysicalStoreID;
            }

            //--------------------------------------------------------------------------
            //manufacturerPrefrence = null; //We are overriding the manufacturer preference.
            //expPreferrence = null;
            //--------------------------------------------------------------------------
            if (!orderDetail.IsColumnNull("UnitID"))
            {
                unitID = orderDetail.UnitID;
            }

            BLL.UserActivity userStore = new UserActivity();

            // Definitely a danger zone
            userStore.LoadByUserIDAndStoreType(userID, order.FromStore, orderDetail.ItemID, unitID.Value);


            while (!userStore.EOF)
            {
                var activity = new Activity();
                activity.LoadByPrimaryKey(userStore.ActivityID);

                if (order.FromStore == Mode.Constants.RDF && !BLL.Settings.IsCenter && !BLL.Settings.PrivateCanGetFromMDGAndPBS && ru.Ownership == OwnershipType.Constants.Private && activity.IsSubsidized)
                {
                    userStore.MoveNext();
                    continue;
                }



                // Load the Activity Selection that has PRiced Commodities.
                Balance balance = new Balance();
                if (pricedUnpricedBoth == PriceSettings.PRICED_ONLY || pricedUnpricedBoth == PriceSettings.BOTH)
                {
                    BLL.Order.MakeStockCalculations(userID, month, year, PriceSettings.PRICED_ONLY, orderDetail,
                                                    userStore.ActivityID, orderDetail.ItemID, order, balance, unitID,
                                                    manufacturerPrefrence, expPreferrence, preferredPhysicalStoreID, out usableStock, out approved,
                                                    out availableQty, markStockoutBit);
                    avQuantity += availableQty;

                    if (availableQty > 0)
                    {
                        this.AddNew();
                        this.ID   = userStore.ActivityID;
                        this.Name = string.Format("{0} ({1})", activity.FullActivityName, availableQty.ToString("#,##0"));
                        PrepareColumnsForApproval();
                        this.SetColumn("AvailableSKU", availableQty);
                        this.SetColumn("IsDeliveryNote", false);
                        this.SetColumn("TextID", string.Format("N{0}", this.ID));
                    }
                }



                // Load Activity Selections for Delivery Note Items.
                if (pricedUnpricedBoth == PriceSettings.DELIVERY_NOTE_ONLY || pricedUnpricedBoth == PriceSettings.BOTH)
                {
                    BLL.Order.MakeStockCalculations(userID, month, year, PriceSettings.DELIVERY_NOTE_ONLY, orderDetail,
                                                    userStore.ActivityID, orderDetail.ItemID, order, balance, unitID,
                                                    manufacturerPrefrence, expPreferrence, preferredPhysicalStoreID, out usableStock, out approved,
                                                    out availableQty, markStockoutBit);

                    avQuantity += availableQty;

                    if (availableQty > 0)
                    {
                        this.AddNew();
                        this.ID   = userStore.ActivityID; //Just to give it a different ID so we know there is a change only.
                        this.Name = string.Format("+-- {0} - Delivery Notes - ({1})", userStore.GetColumn("ActivityName"),
                                                  availableQty.ToString("#,##0"));
                        PrepareColumnsForApproval();
                        this.SetColumn("AvailableSKU", availableQty);
                        this.SetColumn("IsDeliveryNote", true);
                        this.SetColumn("TextID", string.Format("D{0}", this.ID));
                    }
                }
                userStore.MoveNext();
            }
            return(avQuantity);
        }