public bool DeleteDetailDistribution(WoredaStockDistributionDetail DetailDistribution)
 {
     if (DetailDistribution == null) return false;
     _unitOfWork.WoredaStockDistributionDetailRepository.Delete(DetailDistribution);
     _unitOfWork.Save();
     return true;
 }
 public bool EditDetailDistribution(WoredaStockDistributionDetail DetailDistribution)
 {
     _unitOfWork.WoredaStockDistributionDetailRepository.Edit(DetailDistribution);
     _unitOfWork.Save();
     return true;
 }
        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 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");
        }