view model for Internal Movements Internal Movements are the movements of commdity from one store to another on the same Hub
 public ActionResult Create()
 {
     var user = _userProfileService.GetUser(User.Identity.Name);
     var fromStore = _hubService.GetAllStoreByUser(user); ;
     var commodities = _commodityService.GetAllParents(); ;
     var programs = _programService.GetAllProgramsForReport();
     var units = _unitService.GetAllUnit(); ;
     var toStore = _hubService.GetAllStoreByUser(user); ;
     var reasons = _detailService.GetReasonByMaster(Master.Constants.REASON_FOR_INTERNAL_MOVMENT);
     var viewModel = new InternalMovementViewModel(fromStore, commodities, programs, units, toStore, reasons);
     return View(viewModel);
 }
        public ActionResult Create(InternalMovementViewModel viewModel)
        {
            var user = _userProfileService.GetUser(User.Identity.Name);
            var fromStore = _hubService.GetAllStoreByUser(user); ;
            var commodities = _commodityService.GetAllParents(); ;
            var programs = _programService.GetAllProgramsForReport();
            var units = _unitService.GetAllUnit(); ;
            var toStore = _hubService.GetAllStoreByUser(user); ;
            var reasons = _detailService.GetReasonByMaster(Master.Constants.REASON_FOR_INTERNAL_MOVMENT);
            var newViewModel = new InternalMovementViewModel(fromStore, commodities, programs, units, toStore, reasons);
            if (viewModel.QuantityInMt > _transactionService.GetCommodityBalanceForStack(viewModel.FromStoreId, viewModel.FromStackId, viewModel.CommodityId, viewModel.ShippingInstructionId, viewModel.ProjectCodeId))
            {
                ModelState.AddModelError("QuantityInMt", "you dont have sufficent ammout to transfer");
                return View(newViewModel);
            }
            if (viewModel.QuantityInMt <= 0)
            {
                ModelState.AddModelError("QuantityInMt", "You have nothing to transfer");
                return View(newViewModel);
            }

            _internalMovementService.AddNewInternalMovement(viewModel, user);
            return RedirectToAction("Index", "InternalMovement");
        }
        /// <summary>
        /// </summary>
        /// <param name="viewModel"></param>
        /// <param name="user"></param>
        /// <exception cref="System.Exception"></exception>
        public void SaveInternalMovementTrasnsaction(InternalMovementViewModel viewModel, UserProfile user)
        {
            InternalMovement internalMovement = new InternalMovement();
            TransactionGroup transactionGroup = new TransactionGroup();
            Transaction transactionFromStore = new Transaction();

            Commodity commodity = _unitOfWork.CommodityRepository.FindById(viewModel.CommodityId);

            //transaction.TransactionGroupID = transactionGroupId;
            transactionFromStore.LedgerID = 2;
            transactionFromStore.HubOwnerID = user.DefaultHub.HubOwner.HubOwnerID;
            //trasaction.AccountID
            transactionFromStore.AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.HUB, user.DefaultHub.HubID); //
            transactionFromStore.HubID = user.DefaultHub.HubID;
            transactionFromStore.StoreID = viewModel.FromStoreId;  //
            transactionFromStore.Stack = viewModel.FromStackId; //
            transactionFromStore.ProjectCodeID = viewModel.ProjectCodeId;
            transactionFromStore.ShippingInstructionID = viewModel.ShippingInstructionId;
            transactionFromStore.ProgramID = viewModel.ProgramId;
            transactionFromStore.ParentCommodityID = (commodity.ParentID == null)
                                                       ? commodity.CommodityID
                                                       : commodity.ParentID.Value;
            transactionFromStore.CommodityID = viewModel.CommodityId;
            transactionFromStore.CommodityGradeID = null; // How did I get this value ?
            transactionFromStore.QuantityInMT = 0 - viewModel.QuantityInMt;
            transactionFromStore.QuantityInUnit = 0 - viewModel.QuantityInUnit;
            transactionFromStore.UnitID = viewModel.UnitId;
            transactionFromStore.TransactionDate = DateTime.Now;

            Transaction transactionToStore = new Transaction();

            //transactionToStore.TransactionGroupID = transactionGroupId;
            transactionToStore.LedgerID = 2;
            transactionToStore.HubOwnerID = user.DefaultHub.HubOwner.HubOwnerID;
            //transactionToStore.AccountID
            transactionToStore.AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.HUB, user.DefaultHub.HubID); //
            transactionToStore.HubID = user.DefaultHub.HubID;
            transactionToStore.StoreID = viewModel.ToStoreId;  //
            transactionToStore.Stack = viewModel.ToStackId; //
            transactionToStore.ProjectCodeID = viewModel.ProjectCodeId;
            transactionToStore.ShippingInstructionID = viewModel.ShippingInstructionId;
            transactionToStore.ProgramID = viewModel.ProgramId;

            transactionToStore.ParentCommodityID = (commodity.ParentID == null)
                                                       ? commodity.CommodityID
                                                       : commodity.ParentID.Value;
            transactionToStore.CommodityID = viewModel.CommodityId;
            transactionToStore.CommodityGradeID = null; // How did I get this value ?
            transactionToStore.QuantityInMT = viewModel.QuantityInMt;
            transactionToStore.QuantityInUnit = viewModel.QuantityInUnit;
            transactionToStore.UnitID = viewModel.UnitId;
            transactionToStore.TransactionDate = DateTime.Now;

            transactionGroup.Transactions.Add(transactionFromStore);
            transactionGroup.Transactions.Add(transactionToStore);
            transactionGroup.PartitionID = 0;

            internalMovement.PartitionID = 0;
            internalMovement.TransactionGroup = transactionGroup;
            internalMovement.TransferDate = viewModel.SelectedDate;
            internalMovement.DReason = viewModel.ReasonId;
            internalMovement.Notes = viewModel.Note;
            internalMovement.ApprovedBy = viewModel.ApprovedBy;
            internalMovement.ReferenceNumber = viewModel.ReferenceNumber;

            // Try to save this transaction
            //db.Database.Connection.Open();
            //DbTransaction dbTransaction = db.Database.Connection.BeginTransaction();
            try
            {
                _unitOfWork.InternalMovementRepository.Add(internalMovement);
                _unitOfWork.Save();
             //   repository.InternalMovement.Add(internalMovement);
               // dbTransaction.Commit();
            }
            catch (Exception exp)
            {
                //dbTransaction.Rollback();
                //TODO: Save the detail of this exception somewhere
                throw new Exception("The Internal Movement Transaction Cannot be saved. <br />Detail Message :" + exp.Message);
            }
        }
 public void Should_Create_InternalMovementViewModel_Object_ForCurentUser_and_Context()
 {
     var viewModel = new InternalMovementViewModel(repository, user);
     Assert.IsNotNull(viewModel);
 }
        public void Should_Add_InternalMovement()
        {
            InternalMovementViewModel actualViewModel = new InternalMovementViewModel();
            actualViewModel.FromStoreId = 3;
            actualViewModel.SelectedDate = DateTime.Now;
            actualViewModel.FromStackId = 4;
            actualViewModel.ReferenceNumber = "Test/001/";
            actualViewModel.CommodityId = 1;
            actualViewModel.ProgramId = 2;
            actualViewModel.ProjectCodeId = 33;
            actualViewModel.ShippingInstructionId = 46;
            actualViewModel.UnitId = 5;
            actualViewModel.QuantityInUnit = 100;
            actualViewModel.QuantityInMt = 200;
            actualViewModel.ToStoreId = 4;
            actualViewModel.ToStackId = 2;
            actualViewModel.ReasonId = 17;
            actualViewModel.Note = "Test transfer";
            actualViewModel.ApprovedBy = "Mr Test";

            ///expected values
            InternalMovement expectdInternalMovementModel = new InternalMovement();
            expectdInternalMovementModel.PartitionID = 0;
            //expectdInternalMovementModel.TransactionGroupID
            expectdInternalMovementModel.TransferDate = actualViewModel.SelectedDate;
            expectdInternalMovementModel.ReferenceNumber = actualViewModel.ReferenceNumber;
            expectdInternalMovementModel.DReason = actualViewModel.ReasonId;
            expectdInternalMovementModel.Notes = actualViewModel.Note;
            expectdInternalMovementModel.ApprovedBy = actualViewModel.ApprovedBy;

            BLL.UserProfile actualUser = repository.UserProfile.GetUser("admin");

            Transaction expectedFromStoreTrasactionModel = new Transaction();
            expectedFromStoreTrasactionModel.PartitionID = 0;
            //expectedTrasactionModel.TransactionGroupID
            expectedFromStoreTrasactionModel.LedgerID = 2;
            expectedFromStoreTrasactionModel.HubOwnerID = actualUser.DefaultHub.HubOwner.HubOwnerID;
            //expectedTrasactionModel.AccountID =
            expectedFromStoreTrasactionModel.HubID = actualUser.DefaultHub.HubID;
            expectedFromStoreTrasactionModel.StoreID = actualViewModel.FromStoreId;
            expectedFromStoreTrasactionModel.Stack = actualViewModel.FromStackId;
            expectedFromStoreTrasactionModel.ProjectCodeID = actualViewModel.ProjectCodeId;
            expectedFromStoreTrasactionModel.ShippingInstructionID = actualViewModel.ShippingInstructionId;
            expectedFromStoreTrasactionModel.ProgramID = actualViewModel.ProgramId;
            //expectedFromStoreTrasactionModel.ParentCommodityID
            //expectedFromStoreTrasactionModel.CommodityID
            expectedFromStoreTrasactionModel.QuantityInMT = actualViewModel.QuantityInMt;
            expectedFromStoreTrasactionModel.QuantityInUnit  = actualViewModel.QuantityInUnit;
            expectedFromStoreTrasactionModel.UnitID = actualViewModel.UnitId;

            Transaction expectedToStoreTrasactionModel = new Transaction();
            expectedToStoreTrasactionModel.PartitionID = 0;
            //expectedTrasactionModel.TransactionGroupID
            expectedToStoreTrasactionModel.LedgerID = 2;
            expectedToStoreTrasactionModel.HubOwnerID = actualUser.DefaultHub.HubOwner.HubOwnerID;
            //expectedTrasactionModel.AccountID =
            expectedToStoreTrasactionModel.HubID = actualUser.DefaultHub.HubID;
            expectedToStoreTrasactionModel.StoreID = actualViewModel.FromStoreId;
            expectedToStoreTrasactionModel.Stack = actualViewModel.FromStackId;
            expectedToStoreTrasactionModel.ProjectCodeID = actualViewModel.ProjectCodeId;
            expectedToStoreTrasactionModel.ShippingInstructionID = actualViewModel.ShippingInstructionId;
            expectedToStoreTrasactionModel.ProgramID = actualViewModel.ProgramId;
            //expectedFromStoreTrasactionModel.ParentCommodityID
            //expectedFromStoreTrasactionModel.CommodityID
            expectedToStoreTrasactionModel.QuantityInMT = actualViewModel.QuantityInMt;
            expectedToStoreTrasactionModel.QuantityInUnit  = actualViewModel.QuantityInUnit;
            expectedToStoreTrasactionModel.UnitID = actualViewModel.UnitId;

            //var redirectToRouteResult = (RedirectToRouteResult)  new InternalMovementController().Create(actualViewModel);

            //repository.InternalMovement.AddNewInternalMovement(actualViewModel, actualUser);
            repository.Transaction.SaveInternalMovementTrasnsaction(actualViewModel, actualUser);

            //CollectionAssert.Contains(repository.Transaction.GetAll(), expectedFromStoreTrasactionModel);
            //CollectionAssert.Contains(repository.InternalMovement.GetAll(), expectdInternalMovementModel);
        }
 public void CanDoCreatePostback()
 {
     //ACT
     var internalMovementViewModel = new InternalMovementViewModel
         {
             //TODO: Seed data before tesing
         };
     var viewResult = _internalMovementController.Create(internalMovementViewModel) as ViewResult;
     
     //ASSERT
     Assert.NotNull(viewResult);
     var model = viewResult.Model;
     Assert.IsInstanceOf<InternalMovementViewModel>(model);
 }