private WoredaStockDistributionWithDetailViewModel GetWoredaStockDistributionFormDB(WoredaStockDistribution woredaStockDistribution)
        {
            var woredaStockDistributionWithDetailViewModel = new WoredaStockDistributionWithDetailViewModel()
            {
                WoredaStockDistributionID = woredaStockDistribution.WoredaStockDistributionID,
                WoredaID = woredaStockDistribution.WoredaID,
                ProgramID = woredaStockDistribution.ProgramID,
                PlanID = woredaStockDistribution.PlanID,
                Month = woredaStockDistribution.Month,
                SupportTypeID = woredaStockDistribution.SupportTypeID,
                ActualBeneficairies = woredaStockDistribution.ActualBeneficairies,
                MaleBetween5And18Years = woredaStockDistribution.MaleBetween5And18Years,
                MaleAbove18Years = woredaStockDistribution.MaleAbove18Years,
                MaleLessThan5Years = woredaStockDistribution.MaleLessThan5Years,
                FemaleAbove18Years = woredaStockDistribution.FemaleAbove18Years,
                FemaleBetween5And18Years = woredaStockDistribution.FemaleBetween5And18Years,

            };

            return woredaStockDistributionWithDetailViewModel;
        }
        public WoredaStockDistributionWithDetailViewModel CheckWoredaDistribution(int woredaID = -1, int planID = -1, int month = -1)
        {
            if (woredaID == -1 || planID == -1 || month == -1)
                return null;
            //var woredaStockDistribution = _utilizationService.FindBy(m => m.WoredaID == woredaID && m.Month == month && m.PlanID == planID).FirstOrDefault();
            var zoneID = _commonService.GetZoneID(woredaID);
            var regionalRequest =
                _regionalRequestService.FindBy(m => m.PlanID == planID && m.Month == month).FirstOrDefault();
            if (regionalRequest!=null)
            {
                var requisition =
                    _reliefRequisitionService.FindBy(
                        m => m.RegionalRequestID == regionalRequest.RegionalRequestID && m.ZoneID == zoneID).
                        FirstOrDefault();
                if (requisition != null)
                {
                    var woredaStockDistribution = _utilizationService.FindBy(m => m.WoredaID == woredaID && m.Month == month && m.PlanID == planID).FirstOrDefault();
                    if (woredaStockDistribution==null)
                    {
                       //TODO:populateWOredas with detail for the grid
                        var fdpStockDistribution = _commonService.GetFDPs(woredaID);
                        var listOfFdps = new WoredaStockDistributionWithDetailViewModel
                            {
                                WoredaDistributionDetailViewModels = GetWoredaStockDistribution(fdpStockDistribution,requisition),

                            };

                        return listOfFdps;
                    }

                    var woredaStockDistributionWithDetailViewModel = new WoredaStockDistributionWithDetailViewModel()
                    {
                    WoredaStockDistributionID = woredaStockDistribution.WoredaStockDistributionID,
                    WoredaID = woredaStockDistribution.WoredaID,
                    ProgramID = woredaStockDistribution.ProgramID,
                    PlanID = woredaStockDistribution.PlanID,
                    Month = woredaStockDistribution.WoredaStockDistributionID,
                    SupportTypeID = woredaStockDistribution.SupportTypeID,
                    ActualBeneficairies = woredaStockDistribution.ActualBeneficairies,
                    MaleBetween5And18Years = woredaStockDistribution.MaleBetween5And18Years,
                    FemaleLessThan5Years = woredaStockDistribution.FemaleLessThan5Years,
                    MaleAbove18Years = woredaStockDistribution.MaleAbove18Years,
                    MaleLessThan5Years = woredaStockDistribution.MaleLessThan5Years,
                    FemaleAbove18Years = woredaStockDistribution.FemaleAbove18Years,
                    FemaleBetween5And18Years = woredaStockDistribution.FemaleBetween5And18Years,
                    WoredaDistributionDetailViewModels = (from woredaDistributionDetail in woredaStockDistribution.WoredaStockDistributionDetails
                                                          select new WoredaDistributionDetailViewModel()
                                                              {
                                                                  FdpId = woredaDistributionDetail.FdpId,
                                                                  FDP = woredaDistributionDetail.FDP.Name,
                                                                  RequisitionDetailViewModel = GetRequisionInfo(requisition.RequisitionID, woredaDistributionDetail.FdpId),
                                                                  RequistionNo = requisition.RequisitionNo,
                                                                  Round = requisition.Round,
                                                                  Month = RequestHelper.MonthName(requisition.Month),

                                                                  //CommodityName = ,
                                                                  //Month = ,
                                                                  //AllocatedAmount = ,
                                                                  //RequistionNo = ,
                                                                  //BeginingBalance = 0,
                                                                  //EndingBalance = 0,
                                                                  DistributedAmount = woredaDistributionDetail.DistributedAmount,

                                                              }
                         )

                };

                    return woredaStockDistributionWithDetailViewModel;
                }
                return null;

            }
            return null;
        }
 public void LookUps(WoredaStockDistributionWithDetailViewModel distributionInfo)
 {
     ViewBag.Region = new SelectList(_commonService.GetAminUnits(m => m.AdminUnitTypeID == 2), "AdminUnitID", "Name");
     ViewBag.Zone = new SelectList(_commonService.FindBy(m => m.AdminUnitTypeID == 3 && m.ParentID == 3), "AdminUnitID", "Name");
     ViewBag.Woreda = new SelectList(_commonService.FindBy(m => m.AdminUnitTypeID == 4 && m.ParentID == 19), "AdminUnitID", "Name",distributionInfo.WoredaID);
     ViewBag.ProgramID = new SelectList(_commonService.GetPrograms(), "ProgramID", "Name",distributionInfo.ProgramID);
     //ViewBag.Month = new SelectList(RequestHelper.GetMonthList(), "Id", "Name");
     ViewBag.SupportTypeID = new SelectList(_commonService.GetAllSupportType(), "SupportTypeID", "Description");
 }
        private WoredaStockDistribution GetWoredaDetailMOdel(WoredaStockDistributionWithDetailViewModel distributionViewModel)
        {
            if (distributionViewModel!=null)
            {
                var distributionModel = new WoredaStockDistribution()
                    {
                        WoredaStockDistributionID = distributionViewModel.WoredaStockDistributionID,
                        ProgramID = distributionViewModel.ProgramID,
                        PlanID = distributionViewModel.PlanID,
                        WoredaID = distributionViewModel.WoredaID,
                        Month = distributionViewModel.Month,
                        ActualBeneficairies = distributionViewModel.ActualBeneficairies,
                        MaleBetween5And18Years = distributionViewModel.MaleBetween5And18Years,
                        MaleLessThan5Years = distributionViewModel.MaleLessThan5Years,
                        MaleAbove18Years = distributionViewModel.MaleAbove18Years,
                        FemaleAbove18Years = distributionViewModel.FemaleAbove18Years,
                        FemaleBetween5And18Years = distributionViewModel.FemaleBetween5And18Years,
                        FemaleLessThan5Years = distributionViewModel.FemaleLessThan5Years,
                        SupportTypeID = distributionViewModel.SupportTypeID

                    };
                return distributionModel;
            }
            return null;
        }
        public ActionResult Create(int Woreda = -1, int planID = -1,int programID=-1, int month = -1)
        {
            // var woredaStockDistribution = new WoredaStockDistribution();
            var woredaStockDistributionViewModel = new WoredaStockDistributionWithDetailViewModel();
            //woredaStockDistributionViewModel.WoredaDistributionDetailViewModels
            var woredaStockDistribution = CheckWoredaDistribution(Woreda, planID, month);
            if (woredaStockDistribution!=null)
            {
                LookUps(woredaStockDistribution);
                woredaStockDistributionViewModel = woredaStockDistribution;
                woredaStockDistributionViewModel.PlanID = planID;
                woredaStockDistributionViewModel.WoredaID = Woreda;
                woredaStockDistributionViewModel.Month = month;
                woredaStockDistributionViewModel.ProgramID = programID;
                return View(woredaStockDistributionViewModel);
            }
            //ModelState.AddModelError("Errora",@"Request is Not Created for this plan");
            LookUps();
            var distributionDetail=_commonService.GetFDPs(Woreda);
            //var listOfFdps = GetWoredaStockDistribution(distributionDetail);
            //woredaStockDistributionViewModel.WoredaDistributionDetailViewModels = listOfFdps;
            woredaStockDistributionViewModel.PlanID = planID;
            woredaStockDistributionViewModel.WoredaID = Woreda;
            woredaStockDistributionViewModel.Month = month;
            woredaStockDistributionViewModel.ProgramID = programID;

            return View(woredaStockDistributionViewModel);
        }
        public ActionResult Create(WoredaStockDistributionWithDetailViewModel woredaStockDistribution)
        {
            if (woredaStockDistribution!=null && ModelState.IsValid)
            {
                var utilization = _utilizationService.FindBy(m => m.WoredaID == woredaStockDistribution.WoredaID &&
                                                             m.PlanID == woredaStockDistribution.PlanID &&
                                                             m.Month==woredaStockDistribution.Month).FirstOrDefault();
                var BindToModel = GetWoredaDetailMOdel(woredaStockDistribution);
                if (utilization==null)
                {
                    var userProfileId = _userAccountService.GetUserInfo(HttpContext.User.Identity.Name).UserProfileID;
                    BindToModel.DistributedBy = userProfileId;
                    BindToModel.DistributionDate = DateTime.Now;
                    var saved=_utilizationService.AddHeaderDistribution(BindToModel);
                    if (saved)
                    {
                        var fdpStockDistribution = _commonService.GetFDPs(woredaStockDistribution.WoredaID);

                        var woredaDistributionDetailViewModels = GetWoredaStockDistribution(fdpStockDistribution,null);

                        var distributionHeader =
                            _utilizationService.FindBy(m => m.WoredaID == woredaStockDistribution.WoredaID &&
                                                            m.PlanID == woredaStockDistribution.PlanID &&
                                                            m.Month == woredaStockDistribution.Month).FirstOrDefault();

                        foreach (var woredaDistributionDetailViewModel in woredaDistributionDetailViewModels)
                        {
                            var distributionDetailModel = new WoredaStockDistributionDetail()
                                {
                                    WoredaStockDistributionID = distributionHeader.WoredaStockDistributionID,
                                    FdpId = woredaDistributionDetailViewModel.FdpId,
                                    DistributedAmount = woredaDistributionDetailViewModel.DistributedAmount

                                };
                            _utilizationDetailSerivce.AddDetailDistribution(distributionDetailModel);
                        }

                        ModelState.AddModelError("Success", @"Distribution Information Successfully Saved");
                        LookUps(woredaStockDistribution);

                        if (distributionHeader != null)
                        {
                            _transactionService.PostDistribution(distributionHeader.WoredaStockDistributionID);
                        }
                        WoredaStockDistributionWithDetailViewModel woredaStockDistributionViewModel = GetWoredaStockDistributionFormDB(distributionHeader);

                        return View(woredaStockDistributionViewModel);
                    }
                }
                utilization.ActualBeneficairies = woredaStockDistribution.ActualBeneficairies;
                utilization.FemaleLessThan5Years = woredaStockDistribution.FemaleLessThan5Years;
                utilization.FemaleBetween5And18Years = woredaStockDistribution.FemaleLessThan5Years;
                utilization.FemaleAbove18Years = woredaStockDistribution.FemaleAbove18Years;
                utilization.MaleLessThan5Years = woredaStockDistribution.MaleLessThan5Years;
                utilization.MaleBetween5And18Years = woredaStockDistribution.MaleBetween5And18Years;
                utilization.MaleAbove18Years = woredaStockDistribution.MaleAbove18Years;
                utilization.SupportTypeID = woredaStockDistribution.SupportTypeID;
                _utilizationService.EditHeaderDistribution(utilization);

                var woredaDistributionHeader = _utilizationService.FindById(utilization.WoredaStockDistributionID);
                WoredaStockDistributionWithDetailViewModel woredaStockDistributionViewModel2 = GetWoredaStockDistributionFormDB(woredaDistributionHeader);
                LookUps();
                return View(woredaStockDistributionViewModel2);
            }
            ModelState.AddModelError("Errors",@"Unable to Save Distribution Information");
            return View();
        }
        public WoredaStockDistributionWithDetailViewModel CheckWoredaDistribution(int woredaID = -1, int planID = -1, int month = -1,int programId=-1)
        {
            if (woredaID == -1 || planID == -1 || month == -1 || programId == -1)
                return null;
            //var woredaStockDistribution = _utilizationService.FindBy(m => m.WoredaID == woredaID && m.Month == month && m.PlanID == planID).FirstOrDefault();
            var zoneID = _commonService.GetZoneID(woredaID);
            var regionID = _commonService.GetRegion(zoneID);
            RegionalRequest regionalRequest = null;
            if (programId == (int)Programs.Releif)
            {
                 regionalRequest =
                    _regionalRequestService.FindBy(m => m.PlanID == planID && m.Round == month && m.RegionID == regionID)
                        .FirstOrDefault();
            }
            else if (programId == (int) Programs.PSNP)
            {
                regionalRequest = _regionalRequestService.FindBy(m => m.PlanID == planID && m.Month == month && m.RegionID == regionID)
                    .FirstOrDefault();
            }
            if (regionalRequest != null)
            {
                var requisition =
                    _reliefRequisitionService.FindBy(
                 m => m.RegionalRequestID == regionalRequest.RegionalRequestID && m.ZoneID == zoneID);

                if (requisition != null)
                {

                    var woredaStockDistribution = _utilizationService.FindBy(m => m.WoredaID == woredaID && m.Month == month && m.PlanID == planID).FirstOrDefault();
                    if (woredaStockDistribution==null)
                    {

                        var fdpStockDistribution = _commonService.GetFDPs(woredaID);

                        var woredaDistributionDetailViewModels = new List<WoredaDistributionDetailViewModel>();
                        foreach (var reliefRequisition in requisition)
                        {
                            var detail = GetWoredaStockDistribution(fdpStockDistribution, reliefRequisition);
                            if (detail!=null)
                            {
                                woredaDistributionDetailViewModels.AddRange(detail);
                            }

                        }
                        var listOfFdps = new WoredaStockDistributionWithDetailViewModel
                            {
                                WoredaDistributionDetailViewModels = woredaDistributionDetailViewModels,

                            };

                        return listOfFdps;
                    }

                    var woredaStockDistributionWithDetailViewModel = new WoredaStockDistributionWithDetailViewModel()
                    {
                    WoredaStockDistributionID = woredaStockDistribution.WoredaStockDistributionID,
                    WoredaID = woredaStockDistribution.WoredaID,
                    ProgramID = woredaStockDistribution.ProgramID,
                    PlanID = woredaStockDistribution.PlanID,
                    Month = woredaStockDistribution.WoredaStockDistributionID,
                    DirectSupport = woredaStockDistribution.DirectSupport,
                    PublicSupport = woredaStockDistribution.PublicSupport,
                    ActualBeneficairies = woredaStockDistribution.ActualBeneficairies,
                    MaleBetween5And18Years = woredaStockDistribution.MaleBetween5And18Years,
                    FemaleLessThan5Years = woredaStockDistribution.FemaleLessThan5Years,
                    MaleAbove18Years = woredaStockDistribution.MaleAbove18Years,
                    MaleLessThan5Years = woredaStockDistribution.MaleLessThan5Years,
                    FemaleAbove18Years = woredaStockDistribution.FemaleAbove18Years,
                    FemaleBetween5And18Years = woredaStockDistribution.FemaleBetween5And18Years,
                    WoredaDistributionDetailViewModels = (from woredaDistributionDetail in woredaStockDistribution.WoredaStockDistributionDetails
                                                          from reliefRequisition in requisition
                                                          where woredaDistributionDetail.CommodityID==reliefRequisition.CommodityID
                                                          let lossReason = woredaDistributionDetail.LossReason
                                                          where lossReason != null
                                                          select new WoredaDistributionDetailViewModel()
                                                              {
                                                                  WoredaStockDistributionDetailID = woredaDistributionDetail.WoredaStockDistributionDetailID,
                                                                  FdpId = woredaDistributionDetail.FdpId,
                                                                  FDP = woredaDistributionDetail.FDP.Name,
                                                                  CommodityID = GetRequisionInfo(reliefRequisition.RequisitionID, woredaDistributionDetail.FdpId).CommodityID,
                                                                  CommodityName = GetRequisionInfo(reliefRequisition.RequisitionID, woredaDistributionDetail.FdpId).CommodityName,
                                                                  AllocatedAmount = GetRequisionInfo(reliefRequisition.RequisitionID, woredaDistributionDetail.FdpId).AllocatedAmount,
                                                                  NumberOfBeneficiaries = GetRequisionInfo(reliefRequisition.RequisitionID, woredaDistributionDetail.FdpId).BeneficaryNumber,
                                                                  //RequisitionDetailViewModel = GetRequisionInfo(reliefRequisition.RequisitionID, woredaDistributionDetail.FdpId),
                                                                  dispatched = GetDispatchAllocation(reliefRequisition.RequisitionNo, woredaDistributionDetail.FDP.FDPID),
                                                                  delivered = GetDelivered(reliefRequisition.RequisitionNo, woredaDistributionDetail.FDP.FDPID),
                                                                  RequistionNo = reliefRequisition.RequisitionNo,
                                                                  Round = reliefRequisition.Round,
                                                                  Month = RequestHelper.MonthName(reliefRequisition.Month),
                                                                  BeginingBalance = woredaDistributionDetail.StartingBalance,
                                                                  EndingBalance = woredaDistributionDetail.EndingBalance,
                                                                  DistributedAmount = woredaDistributionDetail.DistributedAmount,
                                                                  TotalIn =  woredaDistributionDetail.TotalIn,
                                                                  TotalOut = woredaDistributionDetail.TotoalOut,
                                                                  LossAmount = woredaDistributionDetail.LossAmount,
                                                                  LossReasonId = (int) lossReason,
                                                                  RequisitionId = woredaDistributionDetail.RequisitionId,
                                                                  DistributionStartDate = woredaDistributionDetail.DistributionStartDate,
                                                                  DistributionEndDate = woredaDistributionDetail.DistributionEndDate

                                                              }
                         ).ToList()

                };

                    return woredaStockDistributionWithDetailViewModel;
                }
                return null;

            }
            return null;
        }
        public void LookUps(WoredaStockDistributionWithDetailViewModel distributionInfo)
        {
            var userRegionID = _userAccountService.GetUserInfo(HttpContext.User.Identity.Name).RegionID;
            var regions = _commonService.GetRegions();
            if (userRegionID != null)
            {
                regions = _commonService.FindBy(m => m.AdminUnitID == userRegionID);

            }
            var lossReasons = _lossReasonService.GetAllLossReason().Select(t => new
            {
                name =
            t.LossReasonCodeEg + "-" +
            t.LossReasonEg,
                Id = t.LossReasonId
            });

            ViewData["LossReasons"] = lossReasons;
            ViewBag.Region = new SelectList(regions, "AdminUnitID", "Name", "--Select Region--");
            ViewBag.Zone = new SelectList(_commonService.FindBy(m => m.AdminUnitTypeID == 3 && m.ParentID == 3), "AdminUnitID", "Name");
            ViewBag.Woreda = new SelectList(_commonService.FindBy(m => m.AdminUnitTypeID == 4 && m.ParentID == 19), "AdminUnitID", "Name",distributionInfo.WoredaID);
            ViewBag.ProgramID = new SelectList(_commonService.GetPrograms(), "ProgramID", "Name",distributionInfo.ProgramID);
            //ViewBag.Month = new SelectList(RequestHelper.GetMonthList(), "Id", "Name");
            ViewBag.SupportTypeID = new SelectList(_commonService.GetAllSupportType(), "SupportTypeID", "Description");
        }
        public ActionResult Create(WoredaStockDistributionWithDetailViewModel woredaStockDistribution)
        {
            if (woredaStockDistribution!=null)
            {
                var utilization = _utilizationService.FindBy(m => m.WoredaID == woredaStockDistribution.WoredaID &&
                                                             m.PlanID == woredaStockDistribution.PlanID &&
                                                             m.Month==woredaStockDistribution.Month).FirstOrDefault();

                if (utilization==null)
                {
                    var bindToModel = GetWoredaDetailMOdel(woredaStockDistribution);
                    var userProfileId = _userAccountService.GetUserInfo(HttpContext.User.Identity.Name).UserProfileID;
                    bindToModel.DistributedBy = userProfileId;
                    bindToModel.DistributionDate = DateTime.Now;
                    var saved = _utilizationService.AddHeaderDistribution(bindToModel);
                    if (saved)
                    {
                        //var fdpStockDistribution = _commonService.GetFDPs(woredaStockDistribution.WoredaID);

                        //var woredaDistributionDetailViewModels = GetWoredaStockDistribution(fdpStockDistribution,null);

                        var distributionHeader =_utilizationService.FindBy(m => m.WoredaID == woredaStockDistribution.WoredaID &&
                                                            m.PlanID == woredaStockDistribution.PlanID &&
                                                            m.Month == woredaStockDistribution.Month).FirstOrDefault();

                        foreach (var woredaDistributionDetailViewModel in woredaStockDistribution.WoredaDistributionDetailViewModels)
                        {
                            var distributionDetailModel = new WoredaStockDistributionDetail()
                                {
                                    WoredaStockDistributionID = distributionHeader.WoredaStockDistributionID,
                                    FdpId = woredaDistributionDetailViewModel.FdpId,
                                    CommodityID = woredaDistributionDetailViewModel.CommodityID,
                                    StartingBalance = woredaDistributionDetailViewModel.BeginingBalance,
                                    EndingBalance = woredaDistributionDetailViewModel.EndingBalance,
                                    TotalIn = woredaDistributionDetailViewModel.TotalIn,
                                    TotoalOut = woredaDistributionDetailViewModel.TotalOut,
                                    LossAmount = woredaDistributionDetailViewModel.LossAmount,
                                    LossReason = woredaDistributionDetailViewModel.LossReasonId,
                                    DistributedAmount = woredaDistributionDetailViewModel.DistributedAmount,
                                    RequisitionId = woredaDistributionDetailViewModel.RequisitionId,
                                    DistributionStartDate  = woredaDistributionDetailViewModel.DistributionStartDate,
                                    DistributionEndDate = woredaDistributionDetailViewModel.DistributionEndDate

                                };
                            _utilizationDetailSerivce.AddDetailDistribution(distributionDetailModel);
                        }

                       // ModelState.AddModelError("Success", @"Distribution Information Successfully Saved");
                        LookUps(woredaStockDistribution);

                        if (distributionHeader != null)
                        {
                            _transactionService.PostDistribution(distributionHeader.WoredaStockDistributionID);
                        }
                        WoredaStockDistributionWithDetailViewModel woredaStockDistributionViewModel = GetWoredaStockDistributionFormDB(distributionHeader);

                        //ModelState.AddModelError("Success", @"Distribution Information Successfully Saved!");
                        TempData["CustomError"] = "Distribution Information Successfully Saved!";
                        return RedirectToAction("Create",
                                                new { Woreda = woredaStockDistributionViewModel.WoredaID,
                                                      planID = woredaStockDistributionViewModel.PlanID,
                                                      programID = woredaStockDistributionViewModel.ProgramID,
                                                      month = woredaStockDistributionViewModel.Month
                                                });
                        // return View(woredaStockDistributionViewModel);
                    }
                }
                else
                {
                    utilization.ActualBeneficairies = woredaStockDistribution.ActualBeneficairies;
                    utilization.FemaleLessThan5Years = woredaStockDistribution.FemaleLessThan5Years;
                    utilization.FemaleBetween5And18Years = woredaStockDistribution.FemaleLessThan5Years;
                    utilization.FemaleAbove18Years = woredaStockDistribution.FemaleAbove18Years;
                    utilization.MaleLessThan5Years = woredaStockDistribution.MaleLessThan5Years;
                    utilization.MaleBetween5And18Years = woredaStockDistribution.MaleBetween5And18Years;
                    utilization.MaleAbove18Years = woredaStockDistribution.MaleAbove18Years;
                    utilization.DirectSupport = woredaStockDistribution.DirectSupport;
                    utilization.PublicSupport = woredaStockDistribution.PublicSupport;

                    _utilizationService.EditHeaderDistribution(utilization);

                    var woredaDistributionDetails = _utilizationDetailSerivce.FindBy(m => m.WoredaStockDistributionID == utilization.WoredaStockDistributionID);
                    if (woredaDistributionDetails != null)
                    {
                        foreach (var woredaDistributionDetailViewModel in woredaStockDistribution.WoredaDistributionDetailViewModels)
                        {
                            var woredaDistributionDetail =_utilizationDetailSerivce.FindById(woredaDistributionDetailViewModel.WoredaStockDistributionDetailID);
                            if (woredaDistributionDetail!=null)
                            {
                                woredaDistributionDetail.DistributionStartDate =
                                    woredaDistributionDetailViewModel.DistributionStartDate != null
                                        ? (DateTime?)
                                          Convert.ToDateTime(
                                              (woredaDistributionDetailViewModel.DistributionStartDate.ToString()))
                                        : null;
                                woredaDistributionDetail.DistributionEndDate =
                                    woredaDistributionDetailViewModel.DistributionEndDate != null
                                        ? (DateTime?)
                                          Convert.ToDateTime(
                                              woredaDistributionDetailViewModel.DistributionEndDate.ToString())
                                        : null;

                                woredaDistributionDetail.CommodityID = woredaDistributionDetailViewModel.CommodityID;
                                woredaDistributionDetail.StartingBalance =woredaDistributionDetailViewModel.BeginingBalance;
                                woredaDistributionDetail.EndingBalance = woredaDistributionDetailViewModel.EndingBalance;
                                woredaDistributionDetail.TotalIn = woredaDistributionDetailViewModel.TotalIn;
                                woredaDistributionDetail.TotoalOut = woredaDistributionDetailViewModel.TotalOut;
                                woredaDistributionDetail.LossAmount = woredaDistributionDetailViewModel.LossAmount;
                                woredaDistributionDetail.LossReason = woredaDistributionDetailViewModel.LossReasonId;
                                woredaDistributionDetail.DistributedAmount =woredaDistributionDetailViewModel.DistributedAmount;
                                woredaDistributionDetail.RequisitionId = woredaDistributionDetailViewModel.RequisitionId;
                                _utilizationDetailSerivce.EditDetailDistribution(woredaDistributionDetail);

                            }
                        }

                    }
                    LookUps();
                    //ModelState.AddModelError("Success", @"Distribution Information Successfully Saved!");
                    TempData["CustomError"] = "Distribution Information Successfully Saved!";
                    return RedirectToAction("Create",
                                                    new
                                                    {
                                                        Woreda = utilization.WoredaID,
                                                        planID = utilization.PlanID,
                                                        programID = utilization.ProgramID,
                                                        month = utilization.Month
                                                    });
                }

                //WoredaStockDistributionWithDetailViewModel woredaStockDistributionViewModel2 = GetWoredaStockDistributionFormDB(woredaDistributionHeader);

            }
            //ModelState.AddModelError("Errors",@"Unable to Save Distribution Information");
            TempData["CustomError2"] = "Unable to Save Distribution Information";

            LookUps();
            ViewBag.Errors = "errors";
            if (woredaStockDistribution != null)
                return RedirectToAction("Create",
                                        new
                                            {
                                                Woreda = woredaStockDistribution.WoredaID,
                                                planID = woredaStockDistribution.PlanID,
                                                programID = woredaStockDistribution.ProgramID,
                                                month = woredaStockDistribution.Month
                                            });
             return RedirectToAction("Create");
        }
        public ActionResult Create(int Woreda = -1, int planID = -1,int programID=-1, int month = -1)
        {
            if (TempData["CustomError"] != null)
            {
                ModelState.AddModelError("Success", TempData["CustomError"].ToString());
            }

            else if (TempData["CustomError2"] != null)
            {
                ModelState.AddModelError("Errors", TempData["CustomError2"].ToString());
            }

            if(Woreda==-1||planID==-1 || programID==-1 || month==-1)
               {

               LookUps();
               ViewBag.Errors = "errors";

               return RedirectToAction("Index");

               }

            var woredaStockDistributionViewModel = new WoredaStockDistributionWithDetailViewModel();
            //woredaStockDistributionViewModel.WoredaDistributionDetailViewModels
            var woredaStockDistribution = CheckWoredaDistribution(Woreda, planID, month,programID);
            if (woredaStockDistribution!=null)
            {
                selectedLookUps(programID, planID , Woreda,month);
                LookUps(woredaStockDistribution);
                woredaStockDistributionViewModel = woredaStockDistribution;
                woredaStockDistributionViewModel.PlanID = planID;
                woredaStockDistributionViewModel.WoredaID = Woreda;
                woredaStockDistributionViewModel.Month = month;
                woredaStockDistributionViewModel.ProgramID = programID;
                woredaStockDistributionViewModel.ActualBeneficairies = getWoredaBeneficiaryNoFromHRD(planID, Woreda, month, programID);

                return View(woredaStockDistributionViewModel);
            }
            //ModelState.AddModelError("Errora",@"Request is Not Created for this plan");
            LookUps();

            //ViewBag.WoredaName =_commonService.GetAminUnits(m => m.AdminUnitID == woredaStockDistribution.WoredaID).FirstOrDefault().Name;
            var distributionDetail = _commonService.GetFDPs(Woreda);
            //var listOfFdps = GetWoredaStockDistribution(distributionDetail);
            //woredaStockDistributionViewModel.WoredaDistributionDetailViewModels = listOfFdps;
            woredaStockDistributionViewModel.PlanID = planID;
            woredaStockDistributionViewModel.WoredaID = Woreda;
            woredaStockDistributionViewModel.Month = month;
            woredaStockDistributionViewModel.ProgramID = programID;
            woredaStockDistributionViewModel.ActualBeneficairies = getWoredaBeneficiaryNoFromHRD(planID, Woreda,month,programID);

            return View(woredaStockDistributionViewModel);
        }
        public ActionResult Create(int Woreda = -1, int planID = -1,int programID=-1, int month = -1)
        {
            if(Woreda==-1||planID==-1 || programID==-1 || month==-1)
               {

               LookUps();
               ViewBag.Errors = "errors";

               return RedirectToAction("Index");

               }
            var woredaStockDistributionViewModel = new WoredaStockDistributionWithDetailViewModel();
            //woredaStockDistributionViewModel.WoredaDistributionDetailViewModels
            var woredaStockDistribution = CheckWoredaDistribution(Woreda, planID, month);
            if (woredaStockDistribution!=null)
            {
                LookUps(woredaStockDistribution);
                woredaStockDistributionViewModel = woredaStockDistribution;
                woredaStockDistributionViewModel.PlanID = planID;
                woredaStockDistributionViewModel.WoredaID = Woreda;
                woredaStockDistributionViewModel.Month = month;
                woredaStockDistributionViewModel.ProgramID = programID;
                return View(woredaStockDistributionViewModel);
            }
            //ModelState.AddModelError("Errora",@"Request is Not Created for this plan");
            LookUps();
            ViewBag.WoredaName =_commonService.GetAminUnits(m => m.AdminUnitID == woredaStockDistribution.WoredaID).FirstOrDefault().Name;
            var distributionDetail = _commonService.GetFDPs(Woreda);
            //var listOfFdps = GetWoredaStockDistribution(distributionDetail);
            //woredaStockDistributionViewModel.WoredaDistributionDetailViewModels = listOfFdps;
            woredaStockDistributionViewModel.PlanID = planID;
            woredaStockDistributionViewModel.WoredaID = Woreda;
            woredaStockDistributionViewModel.Month = month;
            woredaStockDistributionViewModel.ProgramID = programID;

            return View(woredaStockDistributionViewModel);
        }