Beispiel #1
0
        public ActionResult Create(ReceiveNewViewModel viewModel)
        {
            //Todo: change to support multiple receive detail

            var user     = _userProfileService.GetUser(User.Identity.Name);
            var hubOwner = _hub.FindById(user.DefaultHub.Value);

            //var receiptAllocation = _receiptAllocationService.FindById(viewModel.ReceiptAllocationId);
            _receiptAllocationId = viewModel.ReceiptAllocationId;
            #region Fix to ModelState

            switch (viewModel.CommoditySourceTypeId)
            {
            case CommoditySource.Constants.DONATION:
                ModelState.Remove("SourceHub");
                ModelState.Remove("SupplierName");
                ModelState.Remove("PurchaseOrder");
                break;

            case CommoditySource.Constants.LOCALPURCHASE:
                ModelState.Remove("SourceHub");
                break;

            default:
                ModelState.Remove("DonorID");
                ModelState.Remove("ResponsibleDonorID");
                ModelState.Remove("SupplierName");
                ModelState.Remove("PurchaseOrder");
                break;
            }

            #endregion

            if (!ModelState.IsValid)
            {
                viewModel.AllocationStatusViewModel  = _receiveService.GetAllocationStatus(_receiptAllocationId);
                viewModel.IsTransporterDetailVisible = !hubOwner.HubOwner.Name.Contains("WFP");
                return(View(viewModel));
            }


            //check if the detail are not null
            if (viewModel.ReceiveDetailNewViewModel != null)
            {
                #region GRN validation

                if (!_receiveService.IsGrnUnique(viewModel.Grn))
                {
                    ModelState.AddModelError("GRN", @"GRN already existed");
                    viewModel.AllocationStatusViewModel  = _receiveService.GetAllocationStatus(_receiptAllocationId);
                    viewModel.IsTransporterDetailVisible = !hubOwner.HubOwner.Name.Contains("WFP");
                    return(View(viewModel));
                }

                #endregion

                #region Validate receive amount

                if (_receiveService.IsReceiveExcedeAllocation(viewModel.ReceiveDetailNewViewModel,
                                                              viewModel.ReceiptAllocationId))
                {
                    viewModel.AllocationStatusViewModel = _receiveService.GetAllocationStatus(_receiptAllocationId);
                    ModelState.AddModelError("ReceiveId", "Hey you are trying to receive more than allocated");
                    viewModel.IsTransporterDetailVisible = !hubOwner.HubOwner.Name.Contains("WFP");
                    return(View(viewModel));
                }

                #endregion

                #region Validate Receive Amount not excide Sent one

                if (_receiveService.IsReceiveGreaterThanSent(viewModel.ReceiveDetailNewViewModel))
                {
                    viewModel.AllocationStatusViewModel = _receiveService.GetAllocationStatus(_receiptAllocationId);
                    ModelState.AddModelError("ReceiveId", "You can't receive more than sent item");
                    viewModel.IsTransporterDetailVisible = !hubOwner.HubOwner.Name.Contains("WFP");
                    return(View(viewModel));
                }

                #endregion

                //Save transaction
                _transactionService.ReceiptTransaction(viewModel);

                return(RedirectToAction("Index", "Receive"));
            }
            else
            {
                viewModel.AllocationStatusViewModel  = _receiveService.GetAllocationStatus(_receiptAllocationId);
                viewModel.IsTransporterDetailVisible = !hubOwner.HubOwner.Name.Contains("WFP");
                ModelState.AddModelError("ReceiveDetails", "Please add at least one commodity");
            }
            viewModel.AllocationStatusViewModel  = _receiveService.GetAllocationStatus(_receiptAllocationId);
            viewModel.IsTransporterDetailVisible = !hubOwner.HubOwner.Name.Contains("WFP");
            return(View(viewModel));
        }