/// <summary>
        /// Generates the allocation view model.
        /// </summary>
        /// <param name="receiptAllocation">The receipt allocation.</param>
        /// <returns></returns>
        public static ReceiptAllocationViewModel GenerateAllocationViewModel(BLL.ReceiptAllocation receiptAllocation)
        {
            ReceiptAllocationViewModel receiptAllocationViewModel = new ReceiptAllocationViewModel();

            receiptAllocationViewModel.ProjectNumber = receiptAllocation.ProjectNumber;
            receiptAllocationViewModel.CommodityID   = receiptAllocation.CommodityID;
            receiptAllocationViewModel.SINumber      = receiptAllocation.SINumber;
            receiptAllocationViewModel.QuantityInMT  = receiptAllocation.QuantityInMT;
            if (receiptAllocation.QuantityInUnit == null)
            {
                receiptAllocationViewModel.QuantityInUnit = 0;
            }
            else
            {
                receiptAllocationViewModel.QuantityInUnit = receiptAllocation.QuantityInUnit.Value;
            }

            receiptAllocationViewModel.HubID   = receiptAllocation.HubID;
            receiptAllocationViewModel.ETA     = receiptAllocation.ETA;
            receiptAllocationViewModel.DonorID = receiptAllocation.DonorID;
            receiptAllocationViewModel.GiftCertificateDetailID = receiptAllocation.GiftCertificateDetailID;
            receiptAllocationViewModel.ReceiptAllocationID     = receiptAllocation.ReceiptAllocationID;
            receiptAllocationViewModel.IsCommited            = receiptAllocation.IsCommited;
            receiptAllocationViewModel.SourceHubID           = receiptAllocation.SourceHubID;
            receiptAllocationViewModel.PurchaseOrder         = receiptAllocation.PurchaseOrder;
            receiptAllocationViewModel.SupplierName          = receiptAllocation.SupplierName;
            receiptAllocationViewModel.OtherDocumentationRef = receiptAllocation.OtherDocumentationRef;
            receiptAllocationViewModel.Remark          = receiptAllocation.Remark;
            receiptAllocationViewModel.CommodityTypeID = receiptAllocation.Commodity.CommodityTypeID;

            receiptAllocationViewModel.ProgramID         = receiptAllocation.ProgramID;
            receiptAllocationViewModel.CommoditySourceID = receiptAllocation.CommoditySourceID;
            return(receiptAllocationViewModel);
        }
        /// <summary>
        /// Generates the allocation view model.
        /// </summary>
        /// <param name="receiptAllocation">The receipt allocation.</param>
        /// <returns></returns>
        public static ReceiptAllocationViewModel GenerateAllocationViewModel(BLL.ReceiptAllocation receiptAllocation)
        {
            ReceiptAllocationViewModel receiptAllocationViewModel = new ReceiptAllocationViewModel();

            receiptAllocationViewModel.ProjectNumber = receiptAllocation.ProjectNumber;
            receiptAllocationViewModel.CommodityID = receiptAllocation.CommodityID;
            receiptAllocationViewModel.SINumber = receiptAllocation.SINumber;
            receiptAllocationViewModel.QuantityInMT = receiptAllocation.QuantityInMT;
            if(receiptAllocation.QuantityInUnit == null)
                receiptAllocationViewModel.QuantityInUnit = 0;
            else
                receiptAllocationViewModel.QuantityInUnit = receiptAllocation.QuantityInUnit.Value;

            receiptAllocationViewModel.HubID = receiptAllocation.HubID;
            receiptAllocationViewModel.ETA = receiptAllocation.ETA;
            receiptAllocationViewModel.DonorID = receiptAllocation.DonorID;
            receiptAllocationViewModel.GiftCertificateDetailID = receiptAllocation.GiftCertificateDetailID;
            receiptAllocationViewModel.ReceiptAllocationID = receiptAllocation.ReceiptAllocationID;
            receiptAllocationViewModel.IsCommited = receiptAllocation.IsCommited;
            receiptAllocationViewModel.SourceHubID = receiptAllocation.SourceHubID;
            receiptAllocationViewModel.PurchaseOrder = receiptAllocation.PurchaseOrder;
            receiptAllocationViewModel.SupplierName = receiptAllocation.SupplierName;
            receiptAllocationViewModel.OtherDocumentationRef = receiptAllocation.OtherDocumentationRef;
            receiptAllocationViewModel.Remark = receiptAllocation.Remark;
            receiptAllocationViewModel.CommodityTypeID = receiptAllocation.Commodity.CommodityTypeID;

            receiptAllocationViewModel.ProgramID = receiptAllocation.ProgramID;
            receiptAllocationViewModel.CommoditySourceID = receiptAllocation.CommoditySourceID;
            return receiptAllocationViewModel;
        }
        private ReceiptAllocationViewModel BindReceiptAllocaitonViewModel()
        {
            var user = _userProfileService.GetUser(User.Identity.Name);
            var commodities = _commodityService.GetAllCommodity().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var donors = _donorService.GetAllDonor().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var hubs = new List<Hub>();
            if (user != null)
            {
                //Hubs = new List<Hub>() { user.DefaultHub };
                hubs = _hubService.GetAllWithoutId(user.DefaultHub.HubID).DefaultIfEmpty().OrderBy(o => o.Name)
                    .ToList();
            }
            else
            {

                hubs =
                    _hubService.GetAllHub().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            }
            var programs = _programService.GetAllProgram().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var commoditySources = _commoditySourceService.GetAllCommoditySource().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var commodityTypes = _commodityTypeService.GetAllCommodityType().DefaultIfEmpty().OrderBy(o => o.Name).ToList();
            var viewModel = new ReceiptAllocationViewModel(commodities, donors, hubs, programs, commoditySources, commodityTypes, user);
            viewModel.HubID = user.DefaultHub.HubID;
            return viewModel;
        }
        public ActionResult Edit(ReceiptAllocationViewModel receiptAllocationViewModel)
        {
            if (receiptAllocationViewModel.CommoditySourceID == BLL.CommoditySource.Constants.DONATION)
            {
                ModelState.Remove("SourceHubID");
                ModelState.Remove("SupplierName");
                ModelState.Remove("PurchaseOrder");

            }
            else if (receiptAllocationViewModel.CommoditySourceID == BLL.CommoditySource.Constants.LOCALPURCHASE)
            {
                ModelState.Remove("DonorID");
                ModelState.Remove("SourceHubID");
            }
            else
            {
                ModelState.Remove("DonorID");
                ModelState.Remove("SupplierName");
                ModelState.Remove("PurchaseOrder");
            }

            if (!(IsSIValid(receiptAllocationViewModel.SINumber, receiptAllocationViewModel.CommoditySourceID)))
            {
                ModelState.AddModelError("SINumber", "");
            }

            if (ModelState.IsValid)
            {
                BLL.ReceiptAllocation receiptAllocation = receiptAllocationViewModel.GenerateReceiptAllocation();
                int typeOfGridToReload = receiptAllocation.CommoditySourceID;
                int commType = _commodityService.FindById(receiptAllocation.CommodityID).CommodityTypeID;
                //override to default hub
                BLL.UserProfile user = _userProfileService.GetUser(User.Identity.Name);
                receiptAllocation.HubID = user.DefaultHub.HubID;
                if (typeOfGridToReload != DRMFSS.BLL.CommoditySource.Constants.DONATION &&
                    typeOfGridToReload != DRMFSS.BLL.CommoditySource.Constants.LOCALPURCHASE)
                {
                    typeOfGridToReload = DRMFSS.BLL.CommoditySource.Constants.LOAN;
                }
                //TODO:Check savechanges -> EditRecieptAllocation
                _receiptAllocationService.EditReceiptAllocation(receiptAllocation);
                return Json(new {gridId = typeOfGridToReload, CommodityTypeID = commType}, JsonRequestBehavior.AllowGet);
                //return RedirectToAction("Index");
            }
            //return this.Create(receiptAllocationViewModel.CommoditySourceID);
            //ModelState.Remove("SINumber");
            //TODO:Check if commenting out has any effect
            //================================================
               // receiptAllocationViewModel.InitalizeViewModel();
            //=============================================
            return PartialView(receiptAllocationViewModel);
        }
        public ActionResult Create(ReceiptAllocationViewModel receiptAllocationViewModel)
        {
            if (receiptAllocationViewModel.CommoditySourceID == BLL.CommoditySource.Constants.DONATION)
            {
                ModelState.Remove("SourceHubID");
                ModelState.Remove("SupplierName");
                ModelState.Remove("PurchaseOrder");

            }
            else if (receiptAllocationViewModel.CommoditySourceID == BLL.CommoditySource.Constants.LOCALPURCHASE)
            {
                ModelState.Remove("DonorID");
                ModelState.Remove("SourceHubID");
            }
            else
            {
                ModelState.Remove("DonorID");
                ModelState.Remove("SupplierName");
                ModelState.Remove("PurchaseOrder");
            }

            if (!(IsSIValid(receiptAllocationViewModel.SINumber, receiptAllocationViewModel.CommoditySourceID)))
            {
                ModelState.AddModelError("SINumber", "");
            }

            if (ModelState.IsValid)
            {
                BLL.ReceiptAllocation receiptAllocation = receiptAllocationViewModel.GenerateReceiptAllocation();
                //for creation make the giftCetificate null if it's from
                if (receiptAllocationViewModel.GiftCertificateDetailID == 0 ||
                    receiptAllocationViewModel.GiftCertificateDetailID == null
                    )
                {
                    var GC = _giftCertificateService.FindBySINumber(receiptAllocationViewModel.SINumber);

                    if (GC != null)
                    {
                        var GCD =
                            GC.GiftCertificateDetails.FirstOrDefault(
                                p => p.CommodityID == receiptAllocationViewModel.CommodityID);
                        if (GCD != null) //&& GCD.GiftCertificateDetailID;
                        {
                            receiptAllocation.GiftCertificateDetailID = GCD.GiftCertificateDetailID;
                        }
                    }
                    else
                    {
                        receiptAllocation.GiftCertificateDetailID = null;
                    }
                }
                int typeOfGridToReload = receiptAllocation.CommoditySourceID;
                int commType = _commodityService.FindById(receiptAllocation.CommodityID).CommodityTypeID;
                //override to default hub
                BLL.UserProfile user = _userProfileService.GetUser(User.Identity.Name);
                receiptAllocation.HubID = user.DefaultHub.HubID;

                if (typeOfGridToReload != DRMFSS.BLL.CommoditySource.Constants.DONATION &&
                    typeOfGridToReload != DRMFSS.BLL.CommoditySource.Constants.LOCALPURCHASE)
                {
                    typeOfGridToReload = DRMFSS.BLL.CommoditySource.Constants.LOAN;
                }

                _receiptAllocationService.AddReceiptAllocation(receiptAllocation);

                return Json(new {gridId = typeOfGridToReload, CommodityTypeID = commType}, JsonRequestBehavior.AllowGet);
                //return RedirectToAction("Index");
            }
            //check this out later
            //return this.Create(receiptAllocationViewModel.CommoditySourceID);
            //ModelState.Remove("SINumber");
            //TODO:Check if commenting out has any effect
            //=================================================
            //receiptAllocationViewModel.InitalizeViewModel();
            //================================================
            return PartialView(receiptAllocationViewModel);
        }
        public void ShouldCreateReceiptAllocation()
        {
            //Arrange
            var id = Guid.NewGuid();
            var receiptAllocationViewModel = new ReceiptAllocationViewModel()
                                                 {
                                                     CommodityID = 1,
                                                     CommodityTypeID = 1,
                                                     CommoditySourceID = 1,
                                                     DonorID = 1,
                                                     ETA = DateTime.Today,
                                                     GiftCertificateDetailID = 1,
                                                     HubID = 1,
                                                     IsCommited = true,
                                                     PartitionID = 1,
                                                     ProgramID = 1,
                                                     OtherDocumentationRef = "DOC-001",
                                                     PurchaseOrder = "PO-001",
                                                     ProjectNumber = "PRJ-001",
                                                     QuantityInMT = 10,
                                                     ReceiptAllocationID = id
                                                 };

            //Act
            _receiptAllocationController.Create(receiptAllocationViewModel);
            var result = (ViewResult)_receiptAllocationController.Index();

            //Assert

            Assert.AreEqual(2, ((List<ReceiptAllocation>)result.Model).Count);
        }
        public void ShouleEditReciptAllocation()
        {
            //Arrange
            var id = Guid.NewGuid();
            var receiptAllocationViewModel = new ReceiptAllocationViewModel()
                                                 {
                                                     CommodityID=1,
                                                     CommodityTypeID=1,
                                                     DonorID=1,
                                                     HubID=1,
                                                     PartitionID=1,
                                                     ProgramID=1,
                                                     QuantityInMT=20,
                                                     CommoditySourceID=2,
                                                     ETA=DateTime.Today,
                                                     GiftCertificateDetailID=1,
                                                     OtherDocumentationRef="REF-0012",
                                                     IsCommited=false,
                                                     ProjectNumber="PRJ-01",
                                                     ReceiptAllocationID=id,
                                                     SourceHubID=3,
                                                     SINumber="SI-001",
                                                     SupplierName="WF"

                                                 };

            //Act

            var result = _receiptAllocationController.Edit(receiptAllocationViewModel);

            //Assert
            Assert.IsNotInstanceOf<PartialViewResult>(result);
        }