public List<DispatchAllocationViewModelDto> GetCommitedAllocationsByHubDetached(int hubId, string PreferedWeightMeasurment)
        {
            List<DispatchAllocationViewModelDto> GetUncloDetacheced = new List<DispatchAllocationViewModelDto>();
            var unclosed =
                _unitOfWork.DispatchAllocationRepository.Get(
                    t => t.ShippingInstructionID.HasValue && t.ProjectCodeID.HasValue
                         && hubId == t.HubID);

            //if (AdminUnitId.HasValue)
            //{
            //    AdminUnit adminunit = _unitOfWork.AdminUnitRepository.FindById(AdminUnitId.Value);

            //    if (adminunit.AdminUnitType.AdminUnitTypeID == 2)//by region
            //        unclosed =
            //            unclosed.Where(p => p.FDP.AdminUnit.AdminUnit2.AdminUnit2.AdminUnitID == AdminUnitId.Value);
            //    else if (adminunit.AdminUnitType.AdminUnitTypeID == 3)//by zone
            //        unclosed =
            //                unclosed.Where(p => p.FDP.AdminUnit.AdminUnit2.AdminUnitID == AdminUnitId.Value);
            //    else if (adminunit.AdminUnitType.AdminUnitTypeID == 4)//by woreda
            //        unclosed =
            //                unclosed.Where(p => p.FDP.AdminUnit.AdminUnitID == AdminUnitId.Value);
            //    //DAVMD.Region = adminunit.FDP.AdminUnit.AdminUnit2.AdminUnit2.;
            //    //DAVMD.Zone = adminunit.FDP.AdminUnit.AdminUnit2.Name;
            //    //DAVMD.Woreda = adminunit.FDP.AdminUnit.Name;

            //    //unclosed = unclosed.Where(p => p.FDP.AdminUnitID == AdminUnitId.Value);
            //}
            //if (closedToo == null || closedToo == false)
            //{
            //    unclosed = unclosed.Where(p => p.IsClosed == false);
            //}
            //else
            //{
            //    unclosed = unclosed.Where(p => p.IsClosed == true);
            //}

            //if (CommodityType.HasValue)
            //{
            //    unclosed = unclosed.Where(p => p.Commodity.CommodityTypeID == CommodityType.Value);
            //}
            //else
            //{
            //    unclosed = unclosed.Where(p => p.Commodity.CommodityTypeID == 1);//by default
            //}

            foreach (var dispatchAllocation in unclosed)
            {
                var DAVMD = new DispatchAllocationViewModelDto();
                if (PreferedWeightMeasurment.ToUpperInvariant() == "MT" && dispatchAllocation.Commodity.CommodityTypeID == 1) //only for food
                {
                    DAVMD.Amount = dispatchAllocation.Amount / 10;
                    DAVMD.DispatchedAmount = dispatchAllocation.DispatchedAmount / 10;
                    DAVMD.RemainingQuantityInQuintals = dispatchAllocation.RemainingQuantityInQuintals / 10;
                }
                else
                {
                    DAVMD.Amount = dispatchAllocation.Amount;
                    DAVMD.DispatchedAmount = dispatchAllocation.DispatchedAmount;
                    DAVMD.RemainingQuantityInQuintals = dispatchAllocation.RemainingQuantityInQuintals;
                }
                DAVMD.DispatchAllocationID = dispatchAllocation.DispatchAllocationID;
                DAVMD.CommodityName = dispatchAllocation.Commodity.Name;
                DAVMD.RequisitionNo = dispatchAllocation.RequisitionNo;
                DAVMD.BidRefNo = dispatchAllocation.BidRefNo;

                DAVMD.Region = dispatchAllocation.FDP.AdminUnit.AdminUnit2.AdminUnit2.Name;
                DAVMD.Zone = dispatchAllocation.FDP.AdminUnit.AdminUnit2.Name;
                DAVMD.Woreda = dispatchAllocation.FDP.AdminUnit.Name;
                DAVMD.FDPName = dispatchAllocation.FDP.Name;
                DAVMD.IsClosed = dispatchAllocation.IsClosed;

                DAVMD.AmountInUnit = DAVMD.Amount;
                DAVMD.DispatchedAmountInUnit = dispatchAllocation.DispatchedAmountInUnit;
                DAVMD.RemainingQuantityInUnit = dispatchAllocation.RemainingQuantityInUnit;

                GetUncloDetacheced.Add(DAVMD);
                // db.Detach(dispatchAllocation);
            }
            return GetUncloDetacheced;
        }
        public ActionResult GetAllocationsGrid(string RquisitionNo, int? CommodityID, bool? Uncommited)
        {
            bool commitStatus = true;
            if (Uncommited.HasValue) commitStatus = Uncommited.Value;
            var list = new List<DispatchAllocation>();
            UserProfile user = _userProfileService.GetUser(User.Identity.Name);
            if (!string.IsNullOrEmpty(RquisitionNo) && CommodityID.HasValue)
            {
                list = _dispatchAllocationService.GetAllocations(RquisitionNo, CommodityID.Value, user.DefaultHub.HubID, commitStatus, user.PreferedWeightMeasurment);
            }
            else if (!string.IsNullOrEmpty(RquisitionNo))
            {
                list = _dispatchAllocationService.GetAllocations(RquisitionNo, user.DefaultHub.HubID, commitStatus);
            }
            List<DispatchAllocationViewModelDto> FDPAllocations = new List<DispatchAllocationViewModelDto>();
            foreach (var dispatchAllocation in list)
            {
                var DAVMD = new DispatchAllocationViewModelDto();
                string preferedWeightMeasurment = user.PreferedWeightMeasurment.ToUpperInvariant();
                //if (preferedWeightMeasurment == "MT" && dispatchAllocation.Commodity.CommodityTypeID == 1) //only for food
                //{
                //    DAVMD.Amount = dispatchAllocation.Amount / 10;
                //    DAVMD.DispatchedAmount = dispatchAllocation.DispatchedAmount / 10;
                //    DAVMD.RemainingQuantityInQuintals = dispatchAllocation.RemainingQuantityInQuintals / 10;
                //}
                //else
                {
                    DAVMD.Amount = dispatchAllocation.Amount;
                    DAVMD.DispatchedAmount = dispatchAllocation.DispatchedAmount;
                    DAVMD.RemainingQuantityInQuintals = dispatchAllocation.RemainingQuantityInQuintals;
                }
                DAVMD.DispatchAllocationID = dispatchAllocation.DispatchAllocationID;
                DAVMD.CommodityName = dispatchAllocation.Commodity.Name;
                DAVMD.CommodityID = dispatchAllocation.CommodityID;
                DAVMD.RequisitionNo = dispatchAllocation.RequisitionNo;
                DAVMD.BidRefNo = dispatchAllocation.BidRefNo;
                DAVMD.ProjectCodeID = dispatchAllocation.ProjectCodeID;
                DAVMD.ShippingInstructionID = dispatchAllocation.ShippingInstructionID;

                DAVMD.Region = dispatchAllocation.FDP.AdminUnit.AdminUnit2.AdminUnit2.Name;
                DAVMD.Zone = dispatchAllocation.FDP.AdminUnit.AdminUnit2.Name;
                DAVMD.Woreda = dispatchAllocation.FDP.AdminUnit.Name;
                DAVMD.FDPName = dispatchAllocation.FDP.Name;
                DAVMD.TransporterName = dispatchAllocation.Transporter.Name;
                DAVMD.IsClosed = dispatchAllocation.IsClosed;

                DAVMD.AmountInUnit = DAVMD.Amount;
                DAVMD.DispatchedAmountInUnit = dispatchAllocation.DispatchedAmountInUnit;
                DAVMD.RemainingQuantityInUnit = dispatchAllocation.RemainingQuantityInUnit;

                FDPAllocations.Add(DAVMD);

            }

            return View(new GridModel(FDPAllocations));
        }
 private DispatchAllocationViewModelDto BindDispatchAllocationViewModelDto(DispatchAllocation dispatchAllocation)
 {
     var target = new DispatchAllocationViewModelDto();
     target.Amount = dispatchAllocation.Amount;
     target.AmountInUnit = dispatchAllocation.AmountInUnit;
     target.BidRefNo = dispatchAllocation.BidRefNo;
     target.CommodityID = dispatchAllocation.CommodityID;
     target.CommodityName = dispatchAllocation.Commodity.Name;
     target.DispatchAllocationID = dispatchAllocation.DispatchAllocationID;
     target.DispatchedAmount = dispatchAllocation.DispatchedAmount;
     target.DispatchedAmountInUnit = dispatchAllocation.DispatchedAmountInUnit;
     target.FDPName = dispatchAllocation.FDP.Name;
     target.IsClosed = dispatchAllocation.IsClosed;
     target.ProjectCodeID = dispatchAllocation.ProjectCodeID;
     //TODO:Check Region,zone,woreda Name
     target.Region = dispatchAllocation.FDP.AdminUnit.AdminUnit2.Name;
     target.RemainingQuantityInQuintals = dispatchAllocation.RemainingQuantityInQuintals;
     target.RemainingQuantityInUnit = dispatchAllocation.RemainingQuantityInUnit;
     target.RequisitionNo = dispatchAllocation.RequisitionNo;
     target.ShippingInstructionID = dispatchAllocation.ShippingInstructionID;
     target.TransporterName = dispatchAllocation.Transporter.Name;
     target.Woreda = dispatchAllocation.FDP.AdminUnit.Name;
     target.Zone = dispatchAllocation.FDP.AdminUnit.AdminUnit2.Name;
     return target;
 }
        public List<DispatchAllocationViewModelDto> GetCommitedAllocationsByHubDetached(int hubId,
                                                                                        string PreferedWeightMeasurment,
                                                                                        bool? closedToo,
                                                                                        int? AdminUnitId,
                                                                                        int? CommodityType)
        {
            List<DispatchAllocationViewModelDto> GetUncloDetacheced = new List<DispatchAllocationViewModelDto>();
            //TODO:Check whether both si and pc is required for checking
            var unclosed =
                _unitOfWork.DispatchAllocationRepository.Get(
                    t => (t.ShippingInstructionID.HasValue || t.ProjectCodeID.HasValue)
                         && hubId == t.HubID
                    );

            if (AdminUnitId.HasValue)
            {
                AdminUnit adminunit = _unitOfWork.AdminUnitRepository.FindById(AdminUnitId.Value);

                if (adminunit.AdminUnitType.AdminUnitTypeID == 2) //by region
                    unclosed =
                        unclosed.Where(p => p.FDP.AdminUnit.AdminUnit2.AdminUnit2.AdminUnitID == AdminUnitId.Value);
                else if (adminunit.AdminUnitType.AdminUnitTypeID == 3) //by zone
                    unclosed =
                        unclosed.Where(p => p.FDP.AdminUnit.AdminUnit2.AdminUnitID == AdminUnitId.Value);
                else if (adminunit.AdminUnitType.AdminUnitTypeID == 4) //by woreda
                    unclosed =
                        unclosed.Where(p => p.FDP.AdminUnitID == AdminUnitId.Value);
                //DAVMD.Region = adminunit.FDP.AdminUnit.AdminUnit2.AdminUnit2.;
                //DAVMD.Zone = adminunit.FDP.AdminUnit.AdminUnit2.Name;
                //DAVMD.Woreda = adminunit.FDP.AdminUnit.Name;

                //unclosed = unclosed.Where(p => p.FDP.AdminUnitID == AdminUnitId.Value);
            }
            if (closedToo == null || closedToo == false)
            {
                unclosed = unclosed.Where(p => p.IsClosed == false);
            }
            else
            {
                unclosed = unclosed.Where(p => p.IsClosed == true);
            }

            if (CommodityType.HasValue)
            {
                unclosed = unclosed.Where(p => p.Commodity.CommodityTypeID == CommodityType.Value);
            }
            else
            {
                unclosed = unclosed.Where(p => p.Commodity.CommodityTypeID == 1); //by default
            }

            foreach (var dispatchAllocation in unclosed)
            {
                var DAVMD = new DispatchAllocationViewModelDto();
                if (PreferedWeightMeasurment.ToUpperInvariant() == "MT " &&
                    dispatchAllocation.Commodity.CommodityTypeID == 1) //only for food
                {
                    DAVMD.Amount = dispatchAllocation.Amount;
                    DAVMD.DispatchedAmount = dispatchAllocation.DispatchedAmount;
                    DAVMD.RemainingQuantityInQuintals = dispatchAllocation.RemainingQuantityInQuintals;
                }
                else
                {
                    DAVMD.Amount = dispatchAllocation.Amount*10;
                    DAVMD.DispatchedAmount = dispatchAllocation.DispatchedAmount*10;
                    DAVMD.RemainingQuantityInQuintals = dispatchAllocation.RemainingQuantityInQuintals*10;
                }
                DAVMD.DispatchAllocationID = dispatchAllocation.DispatchAllocationID;
                DAVMD.CommodityName = dispatchAllocation.Commodity.Name;
                DAVMD.RequisitionNo = dispatchAllocation.RequisitionNo;
                DAVMD.BidRefNo = dispatchAllocation.BidRefNo;

                DAVMD.Region = dispatchAllocation.FDP.AdminUnit.AdminUnit2.AdminUnit2.Name;
                DAVMD.Zone = dispatchAllocation.FDP.AdminUnit.AdminUnit2.Name;
                DAVMD.Woreda = dispatchAllocation.FDP.AdminUnit.Name;
                DAVMD.FDPName = dispatchAllocation.FDP.Name;
                DAVMD.IsClosed = dispatchAllocation.IsClosed;
                if (dispatchAllocation.Transporter != null) DAVMD.TransporterName = dispatchAllocation.Transporter.Name;
                DAVMD.Round = (dispatchAllocation.Round==null)?"-":dispatchAllocation.Round.ToString();

                DAVMD.AmountInUnit = DAVMD.Amount;
                DAVMD.DispatchedAmountInUnit = dispatchAllocation.DispatchedAmountInUnit;
                DAVMD.RemainingQuantityInUnit = dispatchAllocation.RemainingQuantityInUnit;

                GetUncloDetacheced.Add(DAVMD);
                // db.Detach(dispatchAllocation);
            }
            return GetUncloDetacheced;
        }