public static void MakeStockCalculations(int userID, int currentMonth, int currentYear, PriceSettings unpricedOrPricedOrBoth, OrderDetail ordDetail, int storeID, int itemID, Order order, Balance bal, int? unitid, int? preferredManufacturer, DateTime? preferedExpiry, int? preferredPhysicalStoreID, out decimal usableStock, out decimal approved, out decimal availableQuantity, bool markStockoutBit)
 {
     bal.GetItemsAvailableForApproval(unpricedOrPricedOrBoth, currentMonth, currentYear, storeID, itemID, unitid, preferedExpiry, preferredManufacturer, preferredPhysicalStoreID, userID, out  usableStock, out  approved, out  availableQuantity);
 }
 /// <summary>
 /// Gets the approved quantity.
 /// </summary>
 /// <param name="setting">The setting.</param>
 /// <param name="storeId">The store id.</param>
 /// <param name="itemID">The item ID.</param>
 /// <param name="unitid">The unitid.</param>
 /// <param name="preferedExpiry">The prefered expiry.</param>
 /// <param name="preferredManufacturer">The preferred manufacturer.</param>
 /// <param name="preferredPhysicalStoreID">The preferred physical store ID.</param>
 /// <returns></returns>
 internal static int GetApprovedQuantity(PriceSettings setting, int storeId, int itemID, int? unitid, DateTime? preferedExpiry, int? preferredManufacturer, int? preferredPhysicalStoreID)
 {
     var query = HCMIS.Repository.Queries.Order.SelectGetApprovedQuantity(storeId, itemID, unitid, preferedExpiry,
                                                                          preferredManufacturer,
                                                                          preferredPhysicalStoreID,
                                                                          setting ==
                                                                          PriceSettings.DELIVERY_NOTE_ONLY);
     Order ord = new Order();
     ord.LoadFromRawSql(query);
     if (ord.RowCount > 0 && !ord.IsColumnNull("Approved"))
     {
         return Convert.ToInt32(ord.GetColumn("Approved"));
     }
     return 0;
 }
 /// <summary>
 /// Gets the items available for approval.
 /// </summary>
 /// <param name="setting">The setting.</param>
 /// <param name="currentMonth">The current month.</param>
 /// <param name="currentYear">The current year.</param>
 /// <param name="storeID">The store ID.</param>
 /// <param name="itemID">The item ID.</param>
 /// <param name="unitid">The unitid.</param>
 /// <param name="preferedExpiry">The prefered expiry.</param>
 /// <param name="preferredManufacturer">The preferred manufacturer.</param>
 /// <param name="preferredPhysicalStoreID">The preferred physical store ID.</param>
 /// <param name="usableStock">The usable stock.</param>
 /// <param name="approved">The approved.</param>
 /// <param name="availableQuantity">The available quantity.</param>
 public void GetItemsAvailableForApproval(PriceSettings setting, int currentMonth, int currentYear, int storeID, int itemID, int? unitid, DateTime? preferedExpiry, int? preferredManufacturer, int? preferredPhysicalStoreID,int userID,out decimal usableStock, out decimal approved, out decimal availableQuantity)
 {
     var query = HCMIS.Repository.Queries.Balance.SelectGetItemsAvailableForApproval(userID, storeID, itemID, unitid,
                                                                                     setting ==
                                                                                     PriceSettings.DELIVERY_NOTE_ONLY,
                                                                                     BLL.Settings.
                                                                                         IssueUnPricedCommodities,
                                                                                     BLL.Settings.
                                                                                         DoNotIssueNearExpiryItems,
                                                                                     preferedExpiry,
                                                                                     preferredManufacturer,
                                                                                     preferredPhysicalStoreID);
     this.LoadFromRawSql(query);
     if (this.RowCount > 0 && !this.IsColumnNull("Usable"))
     {
         usableStock = Convert.ToDecimal(this.GetColumn("Usable"));
         approved = Order.GetApprovedQuantity(setting, storeID,itemID, unitid, preferedExpiry, preferredManufacturer, preferredPhysicalStoreID);
         availableQuantity = usableStock - approved;
     }
     else
     {
         usableStock = 0;
         approved = 0;
         availableQuantity = 0;
     }
 }
        /// <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;
        }
        /// <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);
        }