public ActionResult CreateAdjustment()
        {
            List<Commodity> commodity;
            List<StoreViewModel> stores;
            List<AdjustmentReason> adjustmentReasonMinus;
            List<AdjustmentReason> adjustmentReasonPlus;
            List<Unit> units;
            List<ProgramViewModel> programs;

            BLL.UserProfile user = _userProfileService.GetUser(User.Identity.Name);

            commodity = _commodityService.GetAllParents();
            stores = _hubService.GetAllStoreByUser(user);
            adjustmentReasonMinus = _adjustmentReasonService.GetAllAdjustmentReason().Where(c => c.Direction == "-").ToList();
            adjustmentReasonPlus = _adjustmentReasonService.GetAllAdjustmentReason().Where(c => c.Direction == "+").ToList();
            units = _unitService.GetAllUnit().ToList();
            programs = _programService.GetAllProgramsForReport();

            LossesAndAdjustmentsViewModel viewModel = new LossesAndAdjustmentsViewModel(commodity, stores, adjustmentReasonMinus, adjustmentReasonPlus, units, programs, user, 2);

            return View(viewModel);
        }
        public ActionResult CreateAdjustment(LossesAndAdjustmentsViewModel viewModel)
        {
            LossesAndAdjustmentsViewModel newViewModel = new LossesAndAdjustmentsViewModel();
            BLL.UserProfile user = _userProfileService.GetUser(User.Identity.Name);

            viewModel.IsLoss = false;
            _adjustmentService.AddNewLossAndAdjustment(viewModel, user);
            return RedirectToAction("Index");
        }
        /// <summary>
        /// </summary>
        /// <param name="viewModel"></param>
        /// <param name="user"></param>
        /// <exception cref="System.Exception"></exception>
        public void SaveLossTrasnsaction(LossesAndAdjustmentsViewModel viewModel, UserProfile user)
        {
            Commodity commodity = _unitOfWork.CommodityRepository.FindById(viewModel.CommodityId);

            Adjustment lossAndAdjustment = new Adjustment();
            TransactionGroup transactionGroup = new TransactionGroup();
            Transaction transactionOne = new Transaction();

            //transaction.TransactionGroupID = transactionGroupId;
            transactionOne.LedgerID = 2;
            transactionOne.HubOwnerID = user.DefaultHub.HubOwner.HubOwnerID;
            transactionOne.AccountID = _accountService.GetAccountIdWithCreate(Account.Constants.HUB, user.DefaultHub.HubID); //
            transactionOne.HubID = user.DefaultHub.HubID;
            transactionOne.StoreID = viewModel.StoreId;  //
            transactionOne.ProjectCodeID = viewModel.ProjectCodeId;
            transactionOne.ShippingInstructionID = viewModel.ShippingInstructionId;

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

            Transaction transactionTwo = new Transaction();

            //transactionToStore.TransactionGroupID = transactionGroupId;
            transactionTwo.LedgerID = 14;
            transactionTwo.HubOwnerID = user.DefaultHub.HubOwnerID;
            transactionTwo.AccountID =_accountService.GetAccountIdWithCreate(Account.Constants.HUB, user.DefaultHub.HubID); //
            transactionTwo.HubID = user.DefaultHub.HubID;
            transactionTwo.StoreID = viewModel.StoreId;  //
            transactionTwo.ProjectCodeID = viewModel.ProjectCodeId;
            transactionTwo.ShippingInstructionID = viewModel.ShippingInstructionId;
            transactionTwo.ParentCommodityID = (commodity.ParentID == null)
                                                       ? commodity.CommodityID
                                                       : commodity.ParentID.Value;
            transactionTwo.CommodityID = viewModel.CommodityId;
            transactionTwo.ProgramID = viewModel.ProgramId;
            transactionTwo.CommodityGradeID = null; // How did I get this value ?
            transactionTwo.QuantityInMT = viewModel.QuantityInMt;
            transactionTwo.QuantityInUnit = viewModel.QuantityInUint;
            transactionTwo.UnitID = viewModel.UnitId;
            transactionTwo.TransactionDate = DateTime.Now;

            transactionGroup.Transactions.Add(transactionOne);
            transactionGroup.Transactions.Add(transactionTwo);

            lossAndAdjustment.PartitionID = 0;
            lossAndAdjustment.TransactionGroup = transactionGroup;
            lossAndAdjustment.HubID = user.DefaultHub.HubID;
            lossAndAdjustment.AdjustmentReasonID = viewModel.ReasonId;
            lossAndAdjustment.AdjustmentDirection = "L";
            lossAndAdjustment.AdjustmentDate = viewModel.SelectedDate;
            lossAndAdjustment.ApprovedBy = viewModel.ApprovedBy;
            lossAndAdjustment.Remarks = viewModel.Description;
            lossAndAdjustment.UserProfileID = user.UserProfileID;
            lossAndAdjustment.ReferenceNumber = viewModel.MemoNumber;
            lossAndAdjustment.StoreManName = viewModel.StoreMan;

            // Try to save this transaction
            //db.Database.Connection.Open();
            //DbTransaction dbTransaction = db.Database.Connection.BeginTransaction();
            try
            {
                _unitOfWork.AdjustmentRepository.Add(lossAndAdjustment);
                _unitOfWork.Save();
                //repository.Adjustment.Add(lossAndAdjustment);
                //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 ActionResult CreateLoss(LossesAndAdjustmentsViewModel viewModel)
        {
            List<Commodity> commodity;
            List<StoreViewModel> stores;
            List<AdjustmentReason> adjustmentReasonMinus;
            List<AdjustmentReason> adjustmentReasonPlus;
            List<Unit> units;
            List<ProgramViewModel> programs;

            BLL.UserProfile user = _userProfileService.GetUser(User.Identity.Name);

            commodity = _commodityService.GetAllParents();
            stores = _hubService.GetAllStoreByUser(user);
            adjustmentReasonMinus = _adjustmentReasonService.GetAllAdjustmentReason().Where(c => c.Direction == "-").ToList();
            adjustmentReasonPlus = _adjustmentReasonService.GetAllAdjustmentReason().Where(c => c.Direction == "+").ToList();
            units = _unitService.GetAllUnit().ToList();
            programs = _programService.GetAllProgramsForReport();

            LossesAndAdjustmentsViewModel newViewModel = new LossesAndAdjustmentsViewModel(commodity, stores, adjustmentReasonMinus, adjustmentReasonPlus, units, programs, user, 1);

            if (viewModel.QuantityInMt > _TransactionService.GetCommodityBalanceForStore(viewModel.StoreId, viewModel.CommodityId, viewModel.ShippingInstructionId, viewModel.ProjectCodeId))
            {
                ModelState.AddModelError("QuantityInMT", "You have nothing to loss");
                return View(newViewModel);
            }

            if (viewModel.QuantityInMt <= 0)
            {
                ModelState.AddModelError("QuantityInMT", "You have nothing to loss");

                return View(newViewModel);
            }
            viewModel.IsLoss = true;
            _adjustmentService.AddNewLossAndAdjustment(viewModel, user);
            return RedirectToAction("Index");
        }
 /// <summary>
 /// Saves the loss adjustment transaction.
 /// </summary>
 /// <param name="viewModel">The view model.</param>
 /// <param name="user">The user.</param>
 public void SaveLossAdjustmentTransaction(LossesAndAdjustmentsViewModel viewModel, UserProfile user)
 {
     if (viewModel.IsLoss == true)
     {
         SaveLossTrasnsaction(viewModel, user);
     }
     else
     {
         SaveAdjustmentTrasnsaction(viewModel, user);
     }
 }