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);
        }
Example #2
0
        public bool ReceiptTransactionForLoanFromNGOs(ReceiveNewViewModel viewModel, Boolean reverse = false)
        {
            //Todo: Construct Receive from the viewModel .... refactor
            int transactionsign = reverse ? -1 : 1;

            #region BindReceiveFromViewModel

            Receive receive;

            if (viewModel.ReceiveId == Guid.Empty)
            {
                receive = new Receive();
                receive.ReceiveID = Guid.NewGuid();

            }
            else
            {
                receive = _unitOfWork.ReceiveRepository.FindById(viewModel.ReceiveId);
            }

            receive.GRN = viewModel.Grn;
            receive.CommodityTypeID = viewModel.CommodityTypeId;

            receive.SourceDonorID = viewModel.SourceDonorId;
            receive.ResponsibleDonorID = viewModel.ResponsibleDonorId;

            receive.TransporterID = viewModel.TransporterId > 0 ? viewModel.TransporterId : 1;
            receive.PlateNo_Prime = viewModel.PlateNoPrime;
            receive.PlateNo_Trailer = viewModel.PlateNoTrailer;
            receive.DriverName = viewModel.DriverName;
            receive.WeightBridgeTicketNumber = viewModel.WeightBridgeTicketNumber;
            receive.WeightBeforeUnloading = viewModel.WeightBeforeUnloading;
            receive.WeightAfterUnloading = viewModel.WeightAfterUnloading;

            receive.VesselName = viewModel.VesselName;
            receive.PortName = viewModel.PortName;

            receive.ReceiptDate = viewModel.ReceiptDate;
            receive.CreatedDate = DateTime.Now;
            receive.WayBillNo = viewModel.WayBillNo;
            receive.CommoditySourceID = viewModel.CommoditySourceTypeId;
            receive.ReceivedByStoreMan = viewModel.ReceivedByStoreMan;

            receive.PurchaseOrder = viewModel.PurchaseOrder;
            receive.SupplierName = viewModel.SupplierName;

            receive.Remark = viewModel.Remark;

            receive.ReceiptAllocationID = viewModel.ReceiptAllocationId;
            receive.HubID = viewModel.CurrentHub;
            receive.UserProfileID = viewModel.UserProfileId;
            receive.StoreId = viewModel.StoreId;
            receive.StackNumber = viewModel.StackNumber;
            receive.SourceDonorID = viewModel.SourceDonorId;
            receive.ResponsibleDonorID = viewModel.ResponsibleDonorId;

            #endregion

            //Todo: Construct ReceiveDetail from the viewModel Transaction

            var transactionGroup = new TransactionGroup { TransactionGroupID = Guid.NewGuid() };

            #region transaction for receiveDetail

            //foreach (var receiveDetailNewViewModel in viewModel.ReceiveDetailNewViewModels)
            //{
            //    ReceiveSingleTransaction(viewModel, receiveDetailNewViewModel, receive, transactionGroup);
            //}

            //Tem implantation for one Receive

            //check for non food

            #region

            if (viewModel.CommodityTypeId == 2)
            {
                viewModel.ReceiveDetailNewViewModel.ReceivedQuantityInMt = 0;
                viewModel.ReceiveDetailNewViewModel.SentQuantityInMt = 0;
            }

            #endregion

            //Construct receive detail from viewModel

            #region

            var receiveDetail = new ReceiveDetail
            {
                ReceiveDetailID = Guid.NewGuid(), //Todo: if there is existing id dont give new one

                CommodityID = viewModel.ReceiveDetailNewViewModel.CommodityId,
                CommodityChildID = viewModel.ReceiveDetailNewViewModel.CommodityChildID,
                Description = viewModel.ReceiveDetailNewViewModel.Description,
                SentQuantityInMT = viewModel.ReceiveDetailNewViewModel.SentQuantityInMt,
                SentQuantityInUnit = viewModel.ReceiveDetailNewViewModel.SentQuantityInUnit,
                UnitID = viewModel.ReceiveDetailNewViewModel.UnitId,
                ReceiveID = receive.ReceiveID,
                TransactionGroupID = transactionGroup.TransactionGroupID,
                TransactionGroup = transactionGroup,

            };

            #endregion

            //add to receive
            receive.ReceiveDetails.Clear();
            receive.ReceiveDetails.Add(receiveDetail);

            var parentCommodityId =
                _unitOfWork.CommodityRepository.FindById(viewModel.ReceiveDetailNewViewModel.CommodityId).ParentID ??
                viewModel.ReceiveDetailNewViewModel.CommodityId;

            //get transactionGroup from a a loaned commodity so that we can deduct commodity amount using this transactionGroup.

             Guid? transactionGroupIdForLoan  = _unitOfWork.ReceiveDetailRepository.FindBy(r => r.ReceiveID == viewModel.SelectedGRN).Select(
                    t => t.TransactionGroupID).FirstOrDefault();

            //physical stock movement

            #region

            //transaction for goods on hand

            #region On Positive Side

            var transactionOne = new Transaction
            {
                TransactionID = Guid.NewGuid(),
                TransactionGroupID = transactionGroup.TransactionGroupID,
                TransactionDate = DateTime.Now,
                ParentCommodityID = null,
                CommodityID = receiveDetail.CommodityID,
                CommodityChildID = receiveDetail.CommodityChildID,
                LedgerID = Ledger.Constants.GOODS_ON_HAND,
                //HubOwnerID =
                DonorID = receive.SourceDonorID,
                AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.HUB, receive.HubID),
                ShippingInstructionID =
                    _shippingInstructionService.GetSINumberIdWithCreate(viewModel.SiNumber).ShippingInstructionID,
                ProjectCodeID = _projectCodeService.GetProjectCodeIdWIthCreate(viewModel.ProjectCode).ProjectCodeID,
                HubID = viewModel.CurrentHub,
                UnitID = viewModel.ReceiveDetailNewViewModel.UnitId,
                QuantityInMT = transactionsign * viewModel.ReceiveDetailNewViewModel.ReceivedQuantityInMt,
                QuantityInUnit = transactionsign * viewModel.ReceiveDetailNewViewModel.ReceivedQuantityInUnit,

                //CommodityGradeID =
                ProgramID = viewModel.ProgramId,
                StoreID = viewModel.StoreId,
                Stack = viewModel.StackNumber,
                IsFalseGRN = viewModel.IsFalseGRN
            };
            transactionGroup.Transactions.Add(transactionOne);

            #endregion

            // transaction for goods under care, receivable, liabilities

            #region Negative Side

            var transactionTwo = new Transaction
            {
                TransactionID = Guid.NewGuid(),
                TransactionGroupID = transactionGroupIdForLoan,// transactionGroup.TransactionGroupID,
                TransactionDate = DateTime.Now,
                ParentCommodityID = null,
                CommodityID = receiveDetail.CommodityID,
                CommodityChildID = receiveDetail.CommodityChildID,
                LedgerID = Ledger.Constants.GOODS_ON_HAND,//Ledger.Constants.GOODS_UNDER_CARE,
                AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.HUB,
                        viewModel.SourceHubId.GetValueOrDefault(0)),
                //HubOwnerID =
                DonorID = receive.SourceDonorID, //

                ShippingInstructionID =
                    _shippingInstructionService.GetSINumberIdWithCreate(viewModel.SiNumber).ShippingInstructionID,
                ProjectCodeID = _projectCodeService.GetProjectCodeIdWIthCreate(viewModel.ProjectCode).ProjectCodeID,
                HubID = viewModel.CurrentHub,
                UnitID = viewModel.ReceiveDetailNewViewModel.UnitId,
                QuantityInMT = transactionsign * (-viewModel.ReceiveDetailNewViewModel.ReceivedQuantityInMt),
                QuantityInUnit = transactionsign * (-viewModel.ReceiveDetailNewViewModel.ReceivedQuantityInUnit),

                //CommodityGradeID =
                ProgramID = viewModel.ProgramId,
                StoreID = viewModel.StoreId,
                Stack = viewModel.StackNumber,
                IsFalseGRN = true
            };

            //switch (viewModel.CommoditySourceTypeId)
            //{
            //    case CommoditySource.Constants.LOCALPURCHASE:
            //    case CommoditySource.Constants.DONATION:
            //        transactionTwo.LedgerID = Ledger.Constants.GOODS_UNDER_CARE;
            //        transactionTwo.AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.DONOR,
            //            receive.ResponsibleDonorID.GetValueOrDefault(0));
            //        break;
            //    case CommoditySource.Constants.REPAYMENT:
            //        transactionTwo.LedgerID = Ledger.Constants.GOODS_RECIEVABLE;
            //        transactionTwo.AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.HUB,
            //            viewModel.SourceHubId.GetValueOrDefault(0));
            //        break;
            //    default:
            //        transactionTwo.LedgerID = Ledger.Constants.LIABILITIES;
            //        transactionTwo.AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.HUB,
            //            viewModel.SourceHubId.GetValueOrDefault(0));
            //        break;
            //}

            transactionGroup.Transactions.Add(transactionTwo);

            #endregion

            #endregion

            // plan side

            #region

            #region Positive Side

            //statstics free

            var transactionThree = new Transaction
            {
                TransactionID = Guid.NewGuid(),
                TransactionGroupID = transactionGroup.TransactionGroupID,
                TransactionDate = DateTime.Now,
                ParentCommodityID = null,
                CommodityID = receiveDetail.CommodityID,
                CommodityChildID = receiveDetail.CommodityChildID,
                LedgerID = Ledger.Constants.STATISTICS_FREE_STOCK,
                //HubOwnerID =
                DonorID = receive.SourceDonorID,
                AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.HUB, receive.HubID),
                ShippingInstructionID =
                    _shippingInstructionService.GetSINumberIdWithCreate(viewModel.SiNumber).ShippingInstructionID,
                ProjectCodeID = _projectCodeService.GetProjectCodeIdWIthCreate(viewModel.ProjectCode).ProjectCodeID,
                HubID = viewModel.CurrentHub,
                UnitID = viewModel.ReceiveDetailNewViewModel.UnitId,
                QuantityInMT = transactionsign * viewModel.ReceiveDetailNewViewModel.ReceivedQuantityInMt,
                QuantityInUnit = transactionsign * viewModel.ReceiveDetailNewViewModel.ReceivedQuantityInUnit,

                //CommodityGradeID =
                ProgramID = viewModel.ProgramId,
                StoreID = viewModel.StoreId,
                Stack = viewModel.StackNumber,
                IsFalseGRN = viewModel.IsFalseGRN
            };

            transactionGroup.Transactions.Add(transactionThree);

            #endregion

            #region Negative Side

            var transactionFour = new Transaction
            {
                TransactionID = Guid.NewGuid(),
                TransactionGroupID =transactionGroupIdForLoan,
                TransactionDate = DateTime.Now,
                ParentCommodityID = null,
                CommodityID = receiveDetail.CommodityID,
                CommodityChildID = receiveDetail.CommodityChildID,
                 LedgerID = Ledger.Constants.STATISTICS_FREE_STOCK,
                 AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.HUB,
                    viewModel.SourceHubId.GetValueOrDefault(0)),
                //HubOwnerID =
                DonorID = receive.SourceDonorID,
                ShippingInstructionID =
                    _shippingInstructionService.GetSINumberIdWithCreate(viewModel.SiNumber).ShippingInstructionID,
                ProjectCodeID = _projectCodeService.GetProjectCodeIdWIthCreate(viewModel.ProjectCode).ProjectCodeID,
                HubID = viewModel.CurrentHub,
                UnitID = viewModel.ReceiveDetailNewViewModel.UnitId,
                QuantityInMT = transactionsign * (-viewModel.ReceiveDetailNewViewModel.ReceivedQuantityInMt),
                QuantityInUnit = transactionsign * (-viewModel.ReceiveDetailNewViewModel.ReceivedQuantityInUnit),

                //CommodityGradeID =
                ProgramID = viewModel.ProgramId,
                StoreID = viewModel.StoreId,
                Stack = viewModel.StackNumber,
                IsFalseGRN =true// viewModel.IsFalseGRN
            };

            //if (transactionFour.CommoditySourceID == CommoditySource.Constants.DONATION ||
            //    viewModel.CommoditySourceTypeId == CommoditySource.Constants.LOCALPURCHASE)
            //{
            //    transactionFour.LedgerID = Ledger.Constants.GOODS_UNDER_CARE;
            //    transactionFour.AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.DONOR,
            //        receive.ResponsibleDonorID.GetValueOrDefault(0));
            //}
            //else if (transactionFour.CommoditySourceID == CommoditySource.Constants.REPAYMENT)
            //{
            //    transactionFour.LedgerID = Ledger.Constants.GOODS_RECIEVABLE;
            //    transactionFour.AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.HUB,
            //        viewModel.SourceHubId.GetValueOrDefault(0));
            //}
            //else
            //{
            //    transactionFour.LedgerID = Ledger.Constants.LIABILITIES;
            //    transactionFour.AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.HUB,
            //        viewModel.SourceHubId.GetValueOrDefault(0));
            //}

            transactionGroup.Transactions.Add(transactionFour);

            #endregion

            #endregion

            #endregion

            //Todo: Save Receive

            try
            {
                if (!reverse)
                {
                    if (viewModel.ReceiveId == Guid.Empty)
                    {
                        _unitOfWork.ReceiveRepository.Add(receive);
                    }
                    else
                    {
                        _unitOfWork.ReceiveRepository.Edit(receive);
                    }

                }

                _unitOfWork.Save();
                return true;
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
Example #3
0
        public ReceiveNewViewModel ReceiptAllocationToReceive(ReceiptAllocation receiptAllocation)
        {
            var viewModel = new ReceiveNewViewModel
            {
                ReceiptDate = DateTime.Now,
                ReceiptAllocationId = receiptAllocation.ReceiptAllocationID,
                SiNumber = receiptAllocation.SINumber,
                ProjectCode = receiptAllocation.ProjectNumber,
                Program = _unitOfWork.ProgramRepository.FindById(receiptAllocation.ProgramID).Name,
                ProgramId = receiptAllocation.ProgramID,
                CommodityType = _unitOfWork.CommodityTypeRepository.FindById(receiptAllocation.Commodity.CommodityTypeID).Name,
                CommodityTypeId = receiptAllocation.Commodity.CommodityTypeID,
            };

            if (CommoditySource.Constants.LOAN == receiptAllocation.CommoditySourceID
                || CommoditySource.Constants.SWAP == receiptAllocation.CommoditySourceID
                || CommoditySource.Constants.TRANSFER == receiptAllocation.CommoditySourceID
                || CommoditySource.Constants.REPAYMENT == receiptAllocation.CommoditySourceID)
            {
                if (receiptAllocation.SourceHubID.HasValue)
                {
                    viewModel.SourceHub = _unitOfWork.HubRepository.FindById(receiptAllocation.SourceHubID.GetValueOrDefault(0)).Name;
                }
                viewModel.IsFalseGRN = receiptAllocation.IsFalseGRN;
            }

            if (CommoditySource.Constants.LOCALPURCHASE == receiptAllocation.CommoditySourceID)
            {
                viewModel.SupplierName = receiptAllocation.SupplierName;
                viewModel.PurchaseOrder = receiptAllocation.PurchaseOrder;
            }

            viewModel.CommoditySource = receiptAllocation.CommoditySource.Name;
            viewModel.CommoditySourceTypeId = receiptAllocation.CommoditySourceID;
            viewModel.ReceiveDetailNewViewModels = new List<ReceiveDetailNewViewModel> { };
            return viewModel;
        }
Example #4
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);
        }