public void SaveInterChange(InterChangeFormViewModel viewModel, string action)
        {
            try
            {
                CheckSplitAmountsEntry.CheckInterChange(viewModel.TerminalId, viewModel.Id, viewModel.SplitAmount);

                if (viewModel.StartDate > viewModel.StopDate)
                {
                    throw new Exception("Stop Date must be after Start Date. ");
                }

                var interChange = Table.SingleOrDefault(x => x.BankAccountId == viewModel.BankAccountId);
                if (action == "Edit")
                {
                    var interChangeToEdit = Table.SingleOrDefault(x => x.Id == viewModel.Id && !x.Deleted);
                    if (interChangeToEdit == null)
                    {
                        throw new Exception("InterChange not found. ");
                    }



                    if (interChange != null)
                    {
                        if (interChange.Id != interChangeToEdit.Id && !interChange.Deleted)
                        {
                            throw new Exception("This Terminal already has this InterChange account. ");
                        }
                        if (interChange.Deleted)
                        {
                            Table.Remove(interChange);
                        }
                    }


                    Mapper.Map(viewModel, interChangeToEdit);

                    Edit(interChangeToEdit);
                }
                else
                {
                    if (interChange != null)
                    {
                        if (!interChange.Deleted)
                        {
                            throw new Exception("This Terminal already has this InterChange account. ");
                        }
                        Table.Remove(interChange);
                    }


                    var result = Mapper.Map <InterChangeFormViewModel, InterChange>(viewModel);
                    Add(result);
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + " Could not add InterChange split amount, please check the entered values. ");
            }
        }
        public ActionResult Save(InterChangeFormViewModel viewModel)
        {
            try
            {
                if (!ModelState.IsValid && viewModel.Id > 0)
                {
                    ViewBag.Error = "Please check the entered values. ";
                    return(View("InterChangeForm", _repository.InterChangeToEdit(viewModel.Id)));
                }
                if (!ModelState.IsValid && viewModel.Id == 0)
                {
                    ViewBag.Error = "Please check the entered values. ";
                    return(View("InterChangeForm",
                                _repository.InitializeNewInterChangeFormViewModel(viewModel)));
                }


                _repository.SaveInterChange(viewModel, viewModel.Id == 0 ? "Create" : "Edit");

                return(RedirectToAction("Details", "Terminals", new { id = viewModel.TerminalId }));
            }
            catch (Exception ex)
            {
                ViewBag.Error = ex.Message + ", Please check the entered values. ";
                return(RedirectToAction("Details", "Terminals", new { id = viewModel.Id }));
            }
        }
        public InterChangeFormViewModel InitializeNewInterChangeFormViewModel(InterChangeFormViewModel viewModel)
        {
            try
            {
                if (viewModel == null)
                {
                    throw new Exception("Model not found. ");
                }

                viewModel.Terminal     = Context.Terminals.SingleOrDefault(x => x.Id == viewModel.TerminalId && !x.Deleted);
                viewModel.BankAccounts = Context.BankAccounts.Where(x => !x.Deleted && x.PartnerId == viewModel.Terminal.PartnerId).ToList();
                viewModel.StartDate    = new DateTime(DateTime.UtcNow.Year, DateTime.UtcNow.Month,
                                                      DateTime.UtcNow.Day + 1);
                viewModel.StopDate          = new DateTime(DateTime.Now.Year + 1, DateTime.Now.Month, DateTime.Now.Day);
                viewModel.CalculationMethod = CalculationMethod.Method.PerTransaction;
                return(viewModel);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + "InterChange not found. ");
            }
        }