public async Task<ActionResult> Pay(Guid id, decimal amount, int clientId)
        {
            var userId = Guid.Parse(User.Identity.GetUserId());

            var order = db.OrderHeaders.Find(id);
            var model = new PayOrderViewModel();

            var toAccount = await db.ClientAccounts.Where(m => m.AccountId.Equals(order.SalesType.AccountId) && m.ClientID == clientId).Select(m => m.ClientAccountId).FirstOrDefaultAsync();

            if (toAccount.Equals(Guid.Empty))
            {
                toAccount = Guid.NewGuid();
                db.ClientAccounts.Add(new finClientAccount()
                {
                    AccountId = order.SalesType.AccountId,
                    Active = true,
                    ClientAccountId = toAccount,
                    ClientID = order.ClientID
                });
                await db.SaveChangesAsync();
            }

            var accounts = await db.ClientAccounts.Where(m => m.ClientID == clientId).ToListAsync();
            model.OrderAmount = order.Total;
            model.OutstandingAmount = order.Total - order.Balance(db, toAccount);
            model.OrderHeaderId = id;
            model.AccountInfo = (from item in accounts
                                 select new PayOrderAccountInfo()
                                 {
                                     AccountId = item.AccountId,
                                     ClientAccountId = item.ClientAccountId,
                                     DestinationAccount = (item.AccountId.Equals(order.SalesType.AccountId)),
                                     EffectiveDate = DateTime.Now,
                                     Balance = Generic.Balance(item.ClientAccountId, db),
                                     AccountName = item.Account.AccountName
                                 }).ToList();

            return View(model);

            /*db.Database.ExecuteSqlCommand("spPayOrder {0}, {1}, {2}, {3}, {4} ", Generic.AccountTelemarked, id, amount, userId, clientId);*/
            /*return RedirectToAction("UnpaidOrders");*/
        }
        public async Task<ActionResult> Pay(PayOrderViewModel model)
        {
            if (ModelState.IsValid)
            {
                var userId = Guid.Parse(User.Identity.GetUserId());
                var toAccount = model.AccountInfo.Where(m => m.DestinationAccount).Select(m => m.ClientAccountId).FirstOrDefault();
                var order = db.OrderHeaders.Find(model.OrderHeaderId);
                if (toAccount.Equals(Guid.Empty))
                {
                    toAccount = await db.ClientAccounts.Where(m => m.AccountId.Equals(order.SalesType.AccountId) && m.ClientID == order.ClientID).Select(m => m.ClientAccountId).FirstOrDefaultAsync();
                }
                if (toAccount.Equals(Guid.Empty))
                {
                    toAccount = Guid.NewGuid();
                    db.ClientAccounts.Add(new finClientAccount()
                    {
                        AccountId = order.SalesType.AccountId,
                        Active = true,
                        ClientAccountId = toAccount,
                        ClientID = order.ClientID
                    });
                    await db.SaveChangesAsync();
                }

                /////////////////////////////////////////////////////
                DataTable dt = new DataTable();
                dt.Columns.Add("FromAccountID");
                dt.Columns.Add("ToAccountID");
                dt.Columns.Add("Amount", typeof(decimal));
                dt.Columns.Add("FromEffectiveDate");
                dt.Columns.Add("ToEffectiveDate");
                dt.Columns.Add("JournalDateDate");
                dt.Columns.Add("MovementSource");
                dt.Columns.Add("Comment");
                dt.Columns.Add("UserID");

                foreach (var acc in model.AccountInfo.Where(m => !m.DestinationAccount))
                {
                    DataRow dr = dt.NewRow();
                    dr["FromAccountID"] = acc.ClientAccountId;
                    dr["ToAccountID"] = toAccount;
                    dr["Amount"] = acc.TransferAmount;
                    dr["FromEffectiveDate"] = DateTime.Now.ToString(Generic.LongDate);
                    dr["ToEffectiveDate"] = acc.EffectiveDate.ToString(Generic.LongDate);
                    dr["JournalDateDate"] = DateTime.Now.ToString(Generic.LongDate);
                    dr["MovementSource"] = model.OrderHeaderId;
                    dr["Comment"] = "Order payment";
                    dr["UserID"] = userId;
                    dt.Rows.Add(dr);
                }
                ////Use DbType.Structured for TVP
                var userdetails = new SqlParameter("JournalInput", SqlDbType.Structured);
                userdetails.Value = dt;
                userdetails.TypeName = "tpfinJournal_Base";


                db.Database.ExecuteSqlCommand("EXEC spfinCreateJournal_Base @JournalInput", userdetails);

                /////////////////////////////////////////////////////
            }
            return View(model);
        }