Пример #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));
        }
Пример #2
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);

            //when the combobox is disabled the commodity id is not submitted
            //var receiptAllocation = _receiptAllocationService.FindById(viewModel.ReceiptAllocationId);
            //viewModel.ReceiveDetailNewViewModel.CommodityId = receiptAllocation.CommodityID;


            if (viewModel.ReceiveId != Guid.Empty)
            {
                _receiptAllocationId =
                    _receiveService.FindById(viewModel.ReceiveId).ReceiptAllocationID.GetValueOrDefault();
                viewModel.ReceiptAllocationId = _receiptAllocationId;
            }
            else
            {
                _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))
                {
                    if (viewModel.ReceiveId == Guid.Empty || _receiveService.FindById(viewModel.ReceiveId).GRN != 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", "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


                //check if it is loan and not a false GRN
                //if (viewModel.CommoditySourceTypeId == CommoditySource.Constants.LOAN && !viewModel.IsFalseGRN && viewModel.SelectedGRN !=null)// this means it is the orginal GRN
                //{
                //    _transactionService.ReceiptTransactionForLoanFromNGOs(viewModel);
                //    return RedirectToAction("Index", "Receive");
                //}

                //Save transaction
                if (viewModel.ReceiveId != Guid.Empty)
                {
                    //reverse the transaction
                    Receive prevmodel = _receiveService.FindById((viewModel.ReceiveId));

                    _transactionService.ReceiptTransaction(ModeltoNewView(prevmodel), true);
                }
                _transactionService.ReceiptTransaction(viewModel);

                return(RedirectToAction("Index", "Receive"));
            }
            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));
        }