public ActionResult Create(LocalPurchaseWithDetailViewModel localPurchaseWithDetailViewModel)
        {
            if (localPurchaseWithDetailViewModel != null && localPurchaseWithDetailViewModel.Quantity >= localPurchaseWithDetailViewModel.LocalPurchaseDetailViewModels.Sum(m => m.AllocatedAmonut))
            {
                var shippingInstractionID = CheckAvilabilityOfSiNumber(localPurchaseWithDetailViewModel.SINumber);
                if (shippingInstractionID != 0)
                {

                    if (!CheckAvailabilityOfSiInLocalPurchase(localPurchaseWithDetailViewModel.SINumber))
                        {

                            SaveNewLocalPurchase(localPurchaseWithDetailViewModel, shippingInstractionID);
                        }

                }
                else
                {
                    var si = AddSiNumber(localPurchaseWithDetailViewModel.SINumber);
                    if (si != -1)
                        SaveNewLocalPurchase(localPurchaseWithDetailViewModel, si);// second in doation table
                }
                TempData["success"] = "Local Purchase Sucessfully Saved";
                return RedirectToAction("Index");
            }
            ModelState.AddModelError("Errors", @"Total Allocated Amount Can't Exceed Planned Quantity");
            PopulateLookUps();
            return View(localPurchaseWithDetailViewModel);
        }
 public ActionResult Create()
 {
     PopulateLookUps();
     var localpurchase = new LocalPurchaseWithDetailViewModel
         {
             CommoditySource=_commonService.GetCommditySourceName(3),//commodity source for local purchase
             LocalPurchaseDetailViewModels = GetNewLocalPurchaseDetail()
         };
     return View(localpurchase);
 }
 public ActionResult Create()
 {
     ViewBag.ProgramID = new SelectList(_commonService.GetPrograms(),"ProgramID","Name");
     ViewBag.CommodityID =new SelectList( _commonService.GetCommodities(),"CommodityID","Name");
     ViewBag.CommodityTypeID =new SelectList( _commonService.GetCommodityTypes(),"CommodityTypeID","Name");
     ViewBag.DonorID = new SelectList( _commonService.GetDonors(),"DonorID","Name");
     var localpurchase = new LocalPurchaseWithDetailViewModel
         {
             CommoditySource=_commonService.GetCommditySourceName(3),//commodity source for local purchase
             LocalPurchaseDetailViewModels = GetNewLocalPurchaseDetail()
         };
     return View(localpurchase);
 }
        public ActionResult Details(int id)
        {
            var localPurchase = _localPurchaseService.FindById(id);
            ViewBag.ProgramID = new SelectList(_commonService.GetPrograms(), "ProgramID", "Name",localPurchase.ProgramID);
            ViewBag.CommodityID = new SelectList(_commonService.GetCommodities(), "CommodityID", "Name",localPurchase.CommodityID);
            ViewBag.CommodityTypeID = new SelectList(_commonService.GetCommodityTypes(), "CommodityTypeID", "Name");
            ViewBag.DonorID = new SelectList(_commonService.GetDonors(), "DonorID", "Name",localPurchase.DonorID);
            if (localPurchase!=null)
            {
                var localPurchaseWithDetailViewModel = new LocalPurchaseWithDetailViewModel()
                    {
                        LocalPurchaseID = localPurchase.LocalPurchaseID,
                        ProgramID = localPurchase.ProgramID,
                        DonorID = localPurchase.DonorID,
                        CommodityID = localPurchase.DonorID,
                        ProjectCode = localPurchase.ProjectCode,
                        SINumber = localPurchase.ShippingInstruction.Value,
                        ReferenceNumber = localPurchase.ReferenceNumber,
                        SupplierName = localPurchase.SupplierName,
                        PurchaseOrder = localPurchase.PurchaseOrder,
                        Quantity = localPurchase.Quantity,
                        StatusID = localPurchase.StatusID,
                        CommoditySource = _commonService.GetCommditySourceName(3),//commodity source for local purchase
                        LocalPurchaseDetailViewModels = GetLocalPurchaseDetail(localPurchase.LocalPurchaseDetails)

                    };
                return View(localPurchaseWithDetailViewModel);
            }
            return RedirectToAction("Index");
        }
        private bool SaveNewLocalPurchase(LocalPurchaseWithDetailViewModel localPurchaseWithDetailViewModel, int sippingInstractionID)
        {
            try
            {

                var localPurchase = new LocalPurchase()
                {
                    DateCreated = DateTime.Now,
                    CommodityID = localPurchaseWithDetailViewModel.CommodityID,
                    DonorID = localPurchaseWithDetailViewModel.DonorID,
                    ProgramID = localPurchaseWithDetailViewModel.ProgramID,
                    ShippingInstructionID = sippingInstractionID,
                    PurchaseOrder = localPurchaseWithDetailViewModel.PurchaseOrder,
                    SupplierName = localPurchaseWithDetailViewModel.SupplierName,
                    Quantity = localPurchaseWithDetailViewModel.Quantity,
                    ReferenceNumber = localPurchaseWithDetailViewModel.ReferenceNumber,
                    ProjectCode = localPurchaseWithDetailViewModel.ProjectCode,
                    StatusID = (int)LocalPurchaseStatus.Draft,

                };

                foreach (var localPurchaseDetail in localPurchaseWithDetailViewModel.LocalPurchaseDetailViewModels
                    .Select(localPurchaseDetail => new LocalPurchaseDetail()
                {
                    HubID = localPurchaseDetail.HubID,
                    AllocatedAmount = localPurchaseDetail.AllocatedAmonut,
                    RecievedAmount = localPurchaseDetail.RecievedAmonut,
                    LocalPurchase = localPurchase
                }))
                {
                    _localPurchaseDetailService.AddLocalPurchaseDetail(localPurchaseDetail);
                }
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
        public ActionResult UpdateLocalPurchase(LocalPurchaseWithDetailViewModel localPurchaseDetailViewModel)
        {
            var localPurchase = _localPurchaseService.FindById(localPurchaseDetailViewModel.LocalPurchaseID);
            if (localPurchase!=null)
            {
                localPurchase.CommodityID = localPurchaseDetailViewModel.CommodityID;
                localPurchase.DonorID = localPurchaseDetailViewModel.DonorID;
                localPurchase.ProgramID = localPurchaseDetailViewModel.ProgramID;
                localPurchase.PurchaseOrder = localPurchaseDetailViewModel.PurchaseOrder;
                localPurchase.SupplierName = localPurchaseDetailViewModel.SupplierName;
                localPurchase.Quantity = localPurchaseDetailViewModel.Quantity;
                localPurchase.ReferenceNumber = localPurchaseDetailViewModel.ReferenceNumber;
                localPurchase.ProjectCode = localPurchaseDetailViewModel.ProjectCode;
                _localPurchaseService.EditLocalPurchase(localPurchase);

                foreach (var localPurchaseDetail in localPurchaseDetailViewModel.LocalPurchaseDetailViewModels)
                {
                    var detail = _localPurchaseDetailService.FindById(localPurchaseDetail.LocalPurchaseDetailID);
                    if (detail!=null)
                    {
                        detail.AllocatedAmount = localPurchaseDetail.AllocatedAmonut;
                        _localPurchaseDetailService.EditLocalPurchaseDetail(detail);
                    }

                }
                ModelState.AddModelError("Success", @"Local Purchase Sucessfully Updated");
                return RedirectToAction("Details", new {id = localPurchase.LocalPurchaseID});
            }
            return RedirectToAction("Index");
        }
        public ActionResult SaveLocalPurchase(LocalPurchaseWithDetailViewModel localPurchaseWithDetailViewModel)
        {
            if(localPurchaseWithDetailViewModel!=null)
            {
                var shippingInstractionID = CheckAvilabilityOfSiNumber(localPurchaseWithDetailViewModel.SINumber);
                if (shippingInstractionID != 0)
                {
                    if (!CheckAvailabilityOfSiInLocalPurchase(localPurchaseWithDetailViewModel.SINumber))
                    {

                        SaveNewLocalPurchase(localPurchaseWithDetailViewModel, shippingInstractionID);
                    }

                }
                else
                {
                    var si = AddSiNumber(localPurchaseWithDetailViewModel.SINumber);
                    if (si != -1)
                        SaveNewLocalPurchase(localPurchaseWithDetailViewModel, si);// second in doation table
                }
            }
            return RedirectToAction("Index");
        }
        public ActionResult UpdateLocalPurchase(LocalPurchaseWithDetailViewModel localPurchaseDetailViewModel)
        {
            var localPurchase = _localPurchaseService.FindById(localPurchaseDetailViewModel.LocalPurchaseID);
            if (localPurchase != null && localPurchaseDetailViewModel.LocalPurchaseDetailViewModels.Sum(m=>m.AllocatedAmonut) <= localPurchase.Quantity)
            {
                localPurchase.CommodityID = localPurchaseDetailViewModel.CommodityID;
                localPurchase.DonorID = localPurchaseDetailViewModel.DonorID;
                localPurchase.ProgramID = localPurchaseDetailViewModel.ProgramID;
                localPurchase.PurchaseOrder = localPurchaseDetailViewModel.PurchaseOrder;
                localPurchase.SupplierName = localPurchaseDetailViewModel.SupplierName;
                localPurchase.Quantity = localPurchaseDetailViewModel.Quantity;
                localPurchase.ReferenceNumber = localPurchaseDetailViewModel.ReferenceNumber;
                localPurchase.ProjectCode = localPurchaseDetailViewModel.ProjectCode;
                _localPurchaseService.EditLocalPurchase(localPurchase);

                foreach (var localPurchaseDetail in localPurchaseDetailViewModel.LocalPurchaseDetailViewModels)
                {
                    var detail = _localPurchaseDetailService.FindById(localPurchaseDetail.LocalPurchaseDetailID);
                    if (detail!=null)
                    {
                        detail.AllocatedAmount = localPurchaseDetail.AllocatedAmonut;
                        _localPurchaseDetailService.EditLocalPurchaseDetail(detail);
                    }

                }
                TempData["success"] = "Local Purchase Sucessfully Updated";
                //ModelState.AddModelError("Success", @"Local Purchase Sucessfully Updated");
                return RedirectToAction("Details", new {id = localPurchase.LocalPurchaseID});
            }
            TempData["CustomError"] = "Total Allocated Amount Can't Exceed Planned Quantity";
            return RedirectToAction("Details", new { id = localPurchaseDetailViewModel.LocalPurchaseID });
        }
        public ActionResult Details(int id)
        {
            var localPurchase = _localPurchaseService.FindById(id);
            var parentCommodityID = _commodityService.Get(m => m.CommodityID ==localPurchase.CommodityID).FirstOrDefault().ParentID;
            ViewBag.ProgramID = new SelectList(_commonService.GetPrograms(), "ProgramID", "Name",localPurchase.ProgramID);
            ViewBag.CommodityID = new SelectList(_commodityService.FindBy(m => m.ParentID == parentCommodityID), "CommodityID", "Name", localPurchase.CommodityID);
            ViewBag.CommodityTypeID = new SelectList(_commonService.GetCommodityTypes(), "CommodityTypeID", "Name");
            ViewBag.DonorID = new SelectList(_commonService.GetDonors(), "DonorID", "Name",localPurchase.DonorID);
            if (localPurchase!=null)
            {
                var localPurchaseWithDetailViewModel = new LocalPurchaseWithDetailViewModel()
                    {
                        LocalPurchaseID = localPurchase.LocalPurchaseID,
                        ProgramID = localPurchase.ProgramID,
                        DonorID = localPurchase.DonorID,
                        CommodityID = localPurchase.DonorID,
                        ProjectCode = localPurchase.ProjectCode,
                        SINumber = localPurchase.ShippingInstruction.Value,
                        ReferenceNumber = localPurchase.ReferenceNumber,
                        SupplierName = localPurchase.SupplierName,
                        PurchaseOrder = localPurchase.PurchaseOrder,
                        Quantity = localPurchase.Quantity,
                        StatusID = localPurchase.StatusID,
                        CommoditySource = _commonService.GetCommditySourceName(3),//commodity source for local purchase
                        LocalPurchaseDetailViewModels = GetLocalPurchaseDetail(localPurchase.LocalPurchaseDetails)

                    };
                if (TempData["CustomError"] != null)
                {
                    ModelState.AddModelError("Errors", TempData["CustomError"].ToString());
                }
                if (TempData["success"] != null)
                    ModelState.AddModelError("Success", TempData["success"].ToString());
                return View(localPurchaseWithDetailViewModel);

            }
            return RedirectToAction("Index");
        }