public static Order GenerateOrder(int? orderID, int orderTypeID, int orderStatusID, int activityID, int paymentTypeID, string contactPerson, int? requestedBy, int userID, int? refNo = null)
        {
            var requisitionType = new RequisitionType();
            int requisitionTypeID = Convert.ToInt32(requisitionType.LoadIDByCode("DMN")["RequisitionTypeID"]);
            Order or = new Order();
            if (orderID == null)
            {
                or.AddNew();
            }
            else
            {
                or.LoadByPrimaryKey(orderID.Value);
            }

            or.RefNo = refNo == null ? GetNextOrderReference() : refNo.ToString();
            var oldOrderStatus = or.IsColumnNull("OrderStatusID") ? (int?)null : or.OrderStatusID;
            or.OrderStatusID = orderStatusID;
               // or.RequisitionTypeID = RequisitionType.CONSTANTS.DEMAND;
            or.RequisitionTypeID = requisitionTypeID;

            or.EurDate = or.Date = DateTimeHelper.ServerDateTime;

            if (requestedBy != null)
            {
                or.RequestedBy = requestedBy.Value;
            }

            or.FilledBy = userID;
            or.ContactPerson = contactPerson;
            var activity = new Activity();
            activity.LoadByPrimaryKey(activityID);
            or.FromStore = activity.ModeID;
            or.PaymentTypeID = paymentTypeID;
            or.FiscalYearID = FiscalYear.Current.ID;
            or.OrderTypeID = orderTypeID;
            or.Save();
            or.LogRequisitionStatus(or.ID, oldOrderStatus, orderStatusID, CurrentContext.UserId);
            return or;
        }
        private void lkRequisitionType_EditValueChanged(object sender, EventArgs e)
        {
            var type = (RequisitionType)lkRequisitionType.EditValue;
            Type = type;

            HideUnhideColumn(Type);
            ClearOrderGrid();
            if (type == RequisitionType.History && lkForFacility.EditValue != null && lkModes.EditValue != null && ! BLL.Settings.IsCenter) LoadCommodityHistoryForFacility();
        }
 /// <summary>
 /// there is diffrent reqtypeID across hubs
 /// so lets use the names as an identify
 /// </summary>
 /// <param name="requisitionTypeName"></param>
 /// <returns></returns>
 private int GetRealValueForRequisitionTypeByName(string requisitionTypeName)
 {
     int realReqTypeID = -1;
     BLL.RequisitionType requisitionType = new BLL.RequisitionType();
     requisitionType.LoadAll();
     DataTable _requisitionTypes = requisitionType.DefaultView.Table;
     DataRow[] dataRows = _requisitionTypes.Select(string.Format(@"Name = '{0}'", requisitionTypeName));
        if(dataRows.Any())
        {
       realReqTypeID = Convert.ToInt32(dataRows[0]["RequisitionTypeID"]);
        }
        return realReqTypeID;
 }
        private void HideUnhideColumn(RequisitionType? type)
        {
            //colDamaged.Visible = colExpired.Visible = colSOH.Visible = InstitutionIType.IsVaccine(GeneralInfo.Current);
            if (type != null)
            {
                colDamaged.Visible = colExpired.Visible = colSOH.Visible = (type == RequisitionType.Population);
                ColBeginningBalance.Visible = ColDOS.Visible = ColLoss.Visible = ColAdjustment.Visible = ColConsumption.Visible = ColMaxStockQuantity.Visible = ColQuantityNeedMax.Visible = ColQuantityReceived.Visible = (type == RequisitionType.Consumption);

                colSOH.Caption = type == RequisitionType.Population ? "rSOH" : "E.B";
                lkPeriod.Enabled = (type == RequisitionType.Consumption);
                NoRequisitionlayout.Visibility = LastVisitlayout.Visibility = LayoutVisibility.Always;
                progressBarlayoutControlItem.ContentVisible = (type == RequisitionType.History);
                lkRange.Enabled = ShowStockOutCheckBox.Enabled = (type == RequisitionType.History);
                // adjust colmuns Visibility  index
                switch (type)
                {
                    case RequisitionType.Demand:
                        ColStockCode.VisibleIndex = 0; ColItemNameOrderDetail.VisibleIndex = 1; ColUnit.VisibleIndex = 2; colPacks.VisibleIndex = 3;
                        break;
                    case RequisitionType.Population:
                        ColStockCode.VisibleIndex = 0; ColItemNameOrderDetail.VisibleIndex = 1; ColUnit.VisibleIndex = 2;
                        colDamaged.VisibleIndex = 3; colExpired.VisibleIndex = 4; colSOH.VisibleIndex = 5; colPacks.VisibleIndex = 6;
                        break;
                    case RequisitionType.Consumption:
                        ColStockCode.VisibleIndex = 0; ColItemNameOrderDetail.VisibleIndex = 1; ColUnit.VisibleIndex = 2;
                        ColBeginningBalance.VisibleIndex = 3; ColLoss.VisibleIndex = 4; ColAdjustment.VisibleIndex = 5; colSOH.VisibleIndex = 6; ColQuantityReceived.VisibleIndex = 7;
                        ColDOS.VisibleIndex = 8; ColQuantityNeedMax.VisibleIndex = 9; ColMaxStockQuantity.VisibleIndex = 10; ColConsumption.VisibleIndex = 11; colPacks.VisibleIndex = 12;
                        break;
                    case RequisitionType.History:
                        ColStockCode.VisibleIndex = 0; ColItemNameOrderDetail.VisibleIndex = 1; ColUnit.VisibleIndex = 2; colPacks.VisibleIndex = 3;
                        ShowStockOutCheckBox.CheckState = CheckState.Checked;
                        break;
                }
            }
            else
            {
                NoRequisitionlayout.Visibility = LastVisitlayout.Visibility = LayoutVisibility.Never;
                LastVisitlabel.Visible = NoOfVisitLabel.Visible = !BLL.Settings.IsCenter;
                colDamaged.Visible = colExpired.Visible = colSOH.Visible = (InstitutionIType.IsVaccine(GeneralInfo.Current)) && (!BLL.Settings.IsCenter);
                ColBeginningBalance.Visible = ColDOS.Visible = ColLoss.Visible = ColAdjustment.Visible = ColConsumption.Visible = ColMaxStockQuantity.Visible = ColQuantityNeedMax.Visible = ColQuantityReceived.Visible = !BLL.Settings.IsCenter;
                ColStockCode.VisibleIndex = 0; ColItemNameOrderDetail.VisibleIndex = 1; ColUnit.VisibleIndex = 2; colPacks.VisibleIndex = 3;
                lkPeriod.Enabled = !BLL.Settings.IsCenter;

            }
        }
 //fake one , local one not same accross hubs
 /// <summary>
 /// Get fake or Local enum RequisitionType By real reqTypeID
 /// </summary>
 /// <param name="reqTypeID"></param>
 /// <returns></returns>
 private RequisitionType GetfakeLocalenumRequisitionTypeByrealTypeID(int reqTypeID)
 {
     string fakeReqTypeName;
     RequisitionType localRequisitionType = 0;
     BLL.RequisitionType requisitionType = new BLL.RequisitionType();
     requisitionType.LoadAll();
     DataTable _requisitionTypes = requisitionType.DefaultView.Table;
     DataRow[] dataRows = _requisitionTypes.Select(string.Format(@"RequisitionTypeID ={0}", reqTypeID));
     if (dataRows.Any())
     {
         fakeReqTypeName = Convert.ToString(dataRows[0]["Name"]);
         localRequisitionType = (RequisitionType)Enum.GetValues(typeof(RequisitionType)).Cast<RequisitionType>()
             .Select(e => new { Value = (int)e, Description = e.ToString() })
             .ToList().Where(n => n.Description == fakeReqTypeName).Select(v => v.Value).FirstOrDefault();
     }
     return localRequisitionType;
 }