public ActionResult InterAccountTransfer(int clientId, Guid? orderHeaderId)
        {
            var currentUserId = Guid.Parse(IdentityExtensions.GetUserId(User.Identity));
            var model = new InterAccountTransferViewModel()
            {
                Amount = 0,
                ClientId = clientId,
                FromEffectiveDate = DateTime.Now,
                ToEffectiveDate = DateTime.Now,
                MovementSource = "User generated transfer",
                UserID = currentUserId,
                FromAccountID = Generic.AccountControl
            };

            ViewBag.FromAccountID = new SelectList(db.Accounts.Where(m => m.Active), "AccountId", "AccountName",model.FromAccountID);

            if (orderHeaderId.HasValue)
            {
                var order = db.OrderHeaders.Find(orderHeaderId.Value);
                if (order.OnceOff)
                {
                    var client = db.Clients.Find(clientId);
                    var subscriptions = (from item in db.Subscriptions
                                         where item.ValidFromDate <= DateTime.Now
                                         && item.ClientTypeID == client.ClientTypeID
                                         && !item.InitialOnceOffFromAccountID.Equals(Guid.Empty)
                                         select item).ToArray();

                    var accountIds = subscriptions.Select(m => m.InitialOnceOffFromAccountID).Distinct().ToArray();
                    var accounts = db.Accounts.Where(m => m.Active && accountIds.Contains(m.AccountId)).ToArray();
                    if (accountIds.Length > 0)
                    {
                        model.FromAccountID = accountIds[0];
                        ViewBag.FromAccountID = new SelectList(accounts, "AccountId", "AccountName", accountIds[0]);
                    }
                }
                model.ToAccountID = order.SalesType.AccountId;
                ViewBag.ToAccountID = new SelectList(db.Accounts.Where(m => m.Active), "AccountId", "AccountName", model.ToAccountID);
                ViewBag.PaymentTypeId = new SelectList(db.PaymentTypes.Where(m => m.Active), "PaymentTypeId", "PaymentTypeName");
                model.OriginalAmount = order.Total + order.Shipping;
                model.AvailableAmount = CalaculateBalance(order.SalesType.AccountId, model.ClientId);
                model.Amount = model.OriginalAmount < model.AvailableAmount ? 0 : model.OriginalAmount - model.AvailableAmount;
                model.FromEffectiveDate = order.OrderDate;
                model.ToEffectiveDate = order.OrderDate;
                var inv = OrderHeadersController.GetInvoice(db, order.OrderHeaderId);
                if (inv != null)
                {
                    model.Comment = string.Format("Payment received for {0}", inv.Number);
                }
                    ViewBag.OrderHeaderId = orderHeaderId.Value;
                
            }
            else
            {
                ViewBag.ToAccountID = new SelectList(db.Accounts.Where(m => m.Active), "AccountId", "AccountName");
                ViewBag.PaymentTypeId = new SelectList(db.PaymentTypes.Where(m => m.Active), "PaymentTypeId", "PaymentTypeName");
            }
            return View(model);
        }
        public ActionResult InterAccountTransfer(InterAccountTransferViewModel model, int? paymentTypeId, Guid? orderHeaderId, bool splitPayment = false)
        {
            if (orderHeaderId.HasValue)
            {
                ViewBag.OrderHeaderId = orderHeaderId.Value;
                //check if payment already exists
                var orderHeader = db.OrderHeaders.Find(orderHeaderId);
                var payments = db.Journals.Where(m => m.MovementSource.Equals(orderHeaderId.Value) && m.Comment == model.Comment);
                var payment = false;
                if (payments.Count()>0) payment = payments.Sum(m=>m.Amount) >= orderHeader.Total + orderHeader.Shipping ;

                if (payment)
                {
                    ModelState.AddModelError(string.Empty, "A sufficient payment has already been made against this order");
                }
            }
                if (ModelState.IsValid)
            {
                try
                {
                    var paymentType = db.PaymentTypes.Find(paymentTypeId);
                    if (paymentType != null)
                    {
                        model.MovementSource = paymentType.PaymentTypeName;
                    }

                    if (!orderHeaderId.HasValue)
                    {

                        db.Database.ExecuteSqlCommand(string.Format(Generic.sqlCreateJournalSingle,
                            model.ClientId,
                            model.FromAccountID,
                            model.ToAccountID,
                            model.Amount.ToString().Replace(",", "."),
                            model.FromEffectiveDate.ToString(Generic.LongDate),
                            model.ToEffectiveDate.ToString(Generic.LongDate),
                            model.MovementSource,
                            model.Comment,
                            model.UserID
                            ));
                    }
                    else
                    {
                        if (model.Amount > 0)
                        {
                            db.Database.ExecuteSqlCommand(string.Format(Generic.sqlCreateJournalSingle_GS,
                                model.ClientId,
                                model.FromAccountID,
                                model.ToAccountID,
                                model.Amount.ToString().Replace(",", "."),
                                model.FromEffectiveDate.ToString(Generic.LongDate),
                                model.ToEffectiveDate.ToString(Generic.LongDate),
                                orderHeaderId.Value,
                                model.Comment,
                                model.UserID,
                                model.MovementSource
                                ));
                        }
                        if (splitPayment)
                        {
                            return RedirectToAction("InterAccountTransfer", new {clientId=model.ClientId, orderHeaderId = orderHeaderId });
                        }
                        db.Database.ExecuteSqlCommand("spCreateSalesWithAvailableFundsAll");
                        if ((User.IsInRole("Area Distributor")|| User.IsInRole("Distributor")))
                        {
                            if (orderHeaderId.HasValue)
                            {
                                return RedirectToAction("Invoice","OrderHeaders", new {id= orderHeaderId.Value });
                            }
                            else
                            {
                                return RedirectToAction("Index", "Clients");
                            }
                        }
                        else
                        {
                            return RedirectToAction("Details", "OrderHeaders", new { id = orderHeaderId.Value });
                        }
                    }

                    return RedirectToAction("Index", new { clientId = model.ClientId});
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("",ex);
                }
            }
            ViewBag.FromAccountID = new SelectList(db.Accounts.Where(m => m.Active), "AccountId", "AccountName");
            ViewBag.ToAccountID = new SelectList(db.Accounts.Where(m => m.Active), "AccountId", "AccountName");
            ViewBag.PaymentTypeId = new SelectList(db.PaymentTypes.Where(m => m.Active), "PaymentTypeId", "PaymentTypeName");

            return View(model);
        }