/// <summary>
        /// Generates the allocation view model.
        /// </summary>
        /// <param name="receiptAllocation">The receipt allocation.</param>
        /// <returns></returns>
        public static ReceiptAllocationViewModel GenerateAllocationViewModel(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.Receives        = receiptAllocation.Receives.ToList();
            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(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.Receives = receiptAllocation.Receives.ToList();
            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<Cats.Models.Hubs.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 ==CommoditySource.Constants.DONATION)
            {
                ModelState.Remove("SourceHubID");
                ModelState.Remove("SupplierName");
                ModelState.Remove("PurchaseOrder");

            }
            else if (receiptAllocationViewModel.CommoditySourceID ==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)
            {
               ReceiptAllocation receiptAllocation = receiptAllocationViewModel.GenerateReceiptAllocation();
                int typeOfGridToReload = receiptAllocation.CommoditySourceID;
                int commType = _commodityService.FindById(receiptAllocation.CommodityID).CommodityTypeID;
                //override to default hub
               UserProfile user = _userProfileService.GetUser(User.Identity.Name);
                receiptAllocation.HubID = user.DefaultHub.HubID;
                if (typeOfGridToReload != Cats.Models.Hubs.CommoditySource.Constants.DONATION &&
                    typeOfGridToReload != Cats.Models.Hubs.CommoditySource.Constants.LOCALPURCHASE)
                {
                    typeOfGridToReload = Cats.Models.Hubs.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 ==CommoditySource.Constants.DONATION)
            {
                ModelState.Remove("SourceHubID");
                ModelState.Remove("SupplierName");
                ModelState.Remove("PurchaseOrder");

            }
            else if (receiptAllocationViewModel.CommoditySourceID ==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)
            {
               ReceiptAllocation receiptAllocation = receiptAllocationViewModel.GenerateReceiptAllocation();
                //for creation make the giftCetificate null if it's from
                if (receiptAllocationViewModel.GiftCertificateDetailID == 0 ||
                    receiptAllocationViewModel.GiftCertificateDetailID == null
                    )
                {
                    var shippingInstruction =
                        _shippingInstructionService.FindBy(t => t.Value == receiptAllocationViewModel.SINumber).
                            FirstOrDefault();
                    var GC = new Cats.Models.Hubs.GiftCertificate();
                    if(shippingInstruction!=null)
                        GC = _giftCertificateService.FindBySINumber(shippingInstruction.ShippingInstructionID);

                    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
               UserProfile user = _userProfileService.GetUser(User.Identity.Name);
                receiptAllocation.HubID = user.DefaultHub.HubID;

                if (typeOfGridToReload != Cats.Models.Hubs.CommoditySource.Constants.DONATION &&
                    typeOfGridToReload != Cats.Models.Hubs.CommoditySource.Constants.LOCALPURCHASE)
                {
                    typeOfGridToReload = Cats.Models.Hubs.CommoditySource.Constants.LOAN;
                }
                receiptAllocation.ReceiptAllocationID = Guid.NewGuid();
                _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);
        }
Beispiel #8
0
 private IEnumerable<ReceiptAllocationViewModel> BindReceiptAllocationViewModels(IEnumerable<ReceiptAllocation> receiptAllocations)
 {
     var receiptAllocationViewModels = new List<ReceiptAllocationViewModel>();
     foreach (var receiptAllocation in receiptAllocations)
     {
         var receiptAllocationViewModel = new ReceiptAllocationViewModel
             {
                 ReceiptAllocationID = receiptAllocation.ReceiptAllocationID,
                 PartitionID = receiptAllocation.PartitionID,
                 IsCommited = receiptAllocation.IsCommited,
                 ETA = receiptAllocation.ETA,
                 ProjectNumber = receiptAllocation.ProjectNumber,
                 GiftCertificateDetailID = receiptAllocation.GiftCertificateDetailID,
                 CommodityID = receiptAllocation.CommodityID,
                 SINumber = receiptAllocation.SINumber,
                 UnitID = receiptAllocation.UnitID,
                 QuantityInUnit = receiptAllocation.QuantityInUnit ?? 0,
                 QuantityInMT = receiptAllocation.QuantityInMT,
                 DonorID = receiptAllocation.DonorID,
                 ProgramID = receiptAllocation.ProgramID,
                 CommoditySourceID = receiptAllocation.CommoditySourceID,
                 IsClosed = receiptAllocation.IsClosed,
                 PurchaseOrder = receiptAllocation.PurchaseOrder,
                 SupplierName = receiptAllocation.SupplierName,
                 SourceHubID = receiptAllocation.SourceHubID,
                 OtherDocumentationRef = receiptAllocation.OtherDocumentationRef,
                 Remark = receiptAllocation.Remark
             };
         receiptAllocationViewModels.Add(receiptAllocationViewModel);
     }
     return receiptAllocationViewModels;
 }
        private ReceiptAllocation FromReceiptAllocationViewModel(ReceiptAllocationViewModel viewModel, ReceiptAllocation entityModel)
        {
            // ReceiptAllocationID = receiptAllocation.ReceiptAllocationID,
                    entityModel.PartitionId = viewModel.PartitionId;
                    entityModel.IsCommited = viewModel.IsCommited;
                    entityModel.ETA = viewModel.ETA;
                    entityModel.ProjectNumber = viewModel.ProjectNumber;
                    entityModel.GiftCertificateDetailID = viewModel.GiftCertificateDetailID;
                    entityModel.CommodityID = viewModel.CommodityID;
                   // CommodityName = receiptAllocation.Commodity.Name,
                    entityModel.SINumber = viewModel.SINumber;
                    entityModel.UnitID = viewModel.UnitID;
                    entityModel.QuantityInUnit = viewModel.QuantityInUnit;
                    entityModel.QuantityInMT = viewModel.QuantityInMT;
                    entityModel.RemainingBalanceInMT = viewModel.RemainingBalanceInMT;
                    entityModel.ReceivedQuantityInMT = viewModel.ReceivedQuantityInMT;
                    entityModel.DonorID = viewModel.DonorID;
                    entityModel.ProgramID = viewModel.ProgramID;
                    entityModel.CommoditySourceID = viewModel.CommoditySourceID;
                    entityModel.IsClosed = viewModel.IsClosed;
                    entityModel.PurchaseOrder = viewModel.PurchaseOrder;
                    entityModel.SupplierName = viewModel.SupplierName;
                    entityModel.SourceHubID = viewModel.SourceHubID;
                    entityModel.OtherDocumentationRef = viewModel.OtherDocumentationRef;
                    entityModel.Remark = viewModel.Remark;

            return entityModel;
        }