private bool CheckDurationOfAssisstance(HRDPSNPPlan plan)
        {
            if (plan.ProgramID == (int)Programs.Releif)
            {
                var hrd = _unitOfWork.HRDRepository.FindBy(m => m.PlanID == plan.PlanID).LastOrDefault();
                //var woredas=new List<>();
                if (hrd != null)
                {
                    var requests = _unitOfWork.RegionalRequestRepository.FindBy(r => r.RegionID == plan.RegionID && r.ProgramId == (int)Programs.Releif && r.PlanID == plan.PlanID);

                    var hrdDetails = (from region in hrd.HRDDetails
                                      where region.AdminUnit.AdminUnit2.AdminUnit2.AdminUnitID == plan.RegionID
                                      select new
                    {
                        region.WoredaID,
                        region.DurationOfAssistance
                    }).ToList();

                    if (requests.Count >= hrdDetails.Max(m => m.DurationOfAssistance))
                    {
                        return(false);
                    }
                    return(true);
                }
            }
            else if (plan.ProgramID == (int)Programs.PSNP)
            {
                var psnpPlan = _unitOfWork.RegionalPSNPPlanRepository.FindBy(m => m.PlanId == plan.PSNPPlanID).LastOrDefault();
                if (psnpPlan != null)
                {
                    var psnpRequests    = _unitOfWork.RegionalRequestRepository.FindBy(r => r.RegionID == plan.RegionID && r.ProgramId == (int)Programs.PSNP && r.PlanID == plan.PSNPPlanID);
                    var psnpPlanDetails = (from detail in psnpPlan.RegionalPSNPPlanDetails
                                           where detail.PlanedWoreda.AdminUnit2.AdminUnit2.AdminUnitID == plan.RegionID
                                           select new
                    {
                        detail.PlanedWoredaID,
                        detail.FoodRatio
                    }).ToList();
                    if (psnpPlanDetails.Count != 0)
                    {
                        if (psnpRequests.Count >= psnpPlanDetails.Max(m => m.FoodRatio))
                        {
                            return(false);
                        }
                    }

                    return(true);
                }
            }
            return(true);
        }
        public HRDPSNPPlanInfo PlanToRequest(HRDPSNPPlan plan)
        {
            //if (CheckDurationOfAssisstance(plan))
            //{
            HRDPSNPPlanInfo        result           = new HRDPSNPPlanInfo();
            List <BeneficiaryInfo> beneficiaryInfos = new List <BeneficiaryInfo>();

            result.HRDPSNPPlan = plan;
            if (plan.ProgramID == (int)Programs.PSNP)
            {
                RegionalPSNPPlan psnpplan =
                    _unitOfWork.RegionalPSNPPlanRepository.FindBy(r => r.PlanId == plan.PSNPPlanID).FirstOrDefault();

                if (psnpplan != null)
                {
                    //TODO:Uncomment this (if PSNP case team Users change their mind and want to get previous request information instead of planned information)

                    var lastPsnpRequest = _unitOfWork.RegionalRequestRepository.FindBy(r => r.RegionID == plan.RegionID && r.ProgramId == (int)Programs.PSNP && r.PlanID == plan.PSNPPlanID).LastOrDefault();
                    if (lastPsnpRequest != null)
                    {
                        result.HRDPSNPPlan.RationID    = psnpplan.RationID;
                        result.HRDPSNPPlan.Contingency = lastPsnpRequest.Contingency;
                        var noOfPsnprequests       = _unitOfWork.RegionalRequestRepository.FindBy(r => r.RegionID == plan.RegionID && r.ProgramId == (int)Programs.PSNP && r.PlanID == plan.PSNPPlanID).Count;
                        var psnpApplicationWoredas = (from psnpDetail in psnpplan.RegionalPSNPPlanDetails
                                                      where
                                                      psnpDetail.PlanedWoreda.AdminUnit2.AdminUnit2.AdminUnitID ==
                                                      plan.RegionID &&
                                                      psnpDetail.FoodRatio > noOfPsnprequests
                                                      select psnpDetail.PlanedWoredaID).ToList();
                        beneficiaryInfos = LastReliefRequest(lastPsnpRequest, psnpApplicationWoredas);
                    }
                    else
                    {
                        result.HRDPSNPPlan.RationID    = psnpplan.RationID;
                        result.HRDPSNPPlan.Contingency =
                            psnpplan.RegionalPSNPPlanDetails.Any(
                                t => t.StartingMonth == result.HRDPSNPPlan.Month && t.Contingency);
                        beneficiaryInfos = PSNPToRequest(psnpplan, plan.RegionID, plan.Month);
                    }
                }
            }
            else if (plan.ProgramID == (int)Programs.Releif)
            {
                HRD hrd = _unitOfWork.HRDRepository.FindBy(r => r.PlanID == plan.PlanID).LastOrDefault();

                if (hrd != null)
                {
                    var lastRequest =
                        _unitOfWork.RegionalRequestRepository.FindBy(
                            r => r.RegionID == plan.RegionID && r.ProgramId == (int)Programs.Releif && r.PlanID == plan.PlanID).
                        LastOrDefault();
                    if (lastRequest != null)
                    {
                        result.HRDPSNPPlan.RationID = hrd.RationID;
                        var requests =
                            _unitOfWork.RegionalRequestRepository.FindBy(
                                r => r.RegionID == plan.RegionID && r.ProgramId == 1 && r.PlanID == plan.PlanID);
                        var numberOfRequestsPerRegion = requests.Count;
                        var applicableWoredas         = (from detail in hrd.HRDDetails
                                                         where detail.AdminUnit.AdminUnit2.AdminUnit2.AdminUnitID == plan.RegionID //&& detail.DurationOfAssistance > numberOfRequestsPerRegion
                                                         select detail.WoredaID).ToList();
                        beneficiaryInfos = LastReliefRequest(lastRequest, applicableWoredas);
                        // var lastRequestDetail = LastReliefRequest(lastRequest);
                    }
                    else
                    {
                        result.HRDPSNPPlan.RationID = hrd.RationID;
                        List <HRDDetail> hrddetail =
                            (from woreda in hrd.HRDDetails
                             where
                             woreda.AdminUnit.AdminUnit2.AdminUnit2.AdminUnitID == plan.RegionID &&
                             woreda.DurationOfAssistance > 0
                             select woreda).ToList();
                        beneficiaryInfos = HRDToRequest(hrddetail);
                    }
                }
            }
            else
            {
                //if program is IDPS
                List <BeneficiaryInfo> benficiaries = new List <BeneficiaryInfo>();
                List <AdminUnit>       woredas      = new List <AdminUnit>();
                var zones =
                    _unitOfWork.AdminUnitRepository.FindBy(
                        w => w.AdminUnitTypeID == 3 && w.ParentID == plan.RegionID);
                foreach (var zone in zones)
                {
                    AdminUnit zone1 = zone;
                    woredas.AddRange(_unitOfWork.AdminUnitRepository.FindBy(w => w.ParentID == zone1.AdminUnitID));
                }
                //var
                foreach (var woreda in woredas)
                {
                    AdminUnit  woreda1    = woreda;
                    List <FDP> WoredaFDPs =
                        _unitOfWork.FDPRepository.FindBy(w => w.AdminUnitID == woreda1.AdminUnitID);
                    ICollection <BeneficiaryInfo> woredabeneficiaries =
                        (from FDP fdp in WoredaFDPs
                         select new BeneficiaryInfo {
                        FDPID = fdp.FDPID, FDPName = fdp.Name, Beneficiaries = 0
                    }).
                        ToList();
                    benficiaries.AddRange(woredabeneficiaries);
                }

                //beneficiaryInfos = benficiaries;
                //beneficiaryInfos = null;
            }
            result.BeneficiaryInfos = beneficiaryInfos;
            return(result);
            //}
            //return null;
        }