Ejemplo n.º 1
0
        public void SaveInsertWaletState(DBAccountstate toAdd)
        {
            try
            {
                var accState = accstates.Insert(toAdd);
                if (accState == null)
                {
                    return;
                }
                using (ISession Session = ConnectionHelper.CreateNewSession())
                {
                    using (ITransaction Transaction = Session.BeginTransaction())
                    {
                        if (accState.Account != null)
                        {
                            var account = Session.Get <DBAccount>(accState.Account.Id);
                            if (account != null)
                            {
                                account.Balance    = accState.Balance;
                                account.Lastupdate = DateTime.UtcNow;
                                Session.Update(account);
                            }
                        }

                        Transaction.Commit();
                    }
                }
            }
            catch (Exception e)
            {
                log.Error("Error: SaveInsertWaletState: " + e);
            }
        }
Ejemplo n.º 2
0
        public List <Wallet> GetWalletsState(DateTime forDate)
        {
            List <Wallet> results = new List <Wallet>();

            try
            {
                using (ISession Session = ConnectionHelper.CreateNewSession())
                {
                    // var rateList = Session.Query<DBRates>().Where(x => x.Retired == false).ToList();
                    IQueryable <DBWallet> wallets = null;
                    if (forDate == DateTime.MaxValue)
                    {
                        wallets = Session.Query <DBWallet>()
                                  .Where(x => x.Retired == false && !x.Name.Equals("test"));
                    }
                    else
                    {
                        wallets = Session.Query <DBWallet>().Where(x => !x.Name.Equals("test"));
                    }

                    foreach (var dbw in wallets)
                    {
                        Wallet  wallet  = toDTO(dbw);
                        decimal balance = 0;
                        IQueryable <DBAccount> accounts = null;
                        if (forDate == DateTime.MaxValue)
                        {
                            accounts = Session.Query <DBAccount>()
                                       .Where(x => x.Wallet.Id == wallet.Id && x.Retired == false);
                        }
                        else
                        {
                            accounts = Session.Query <DBAccount>().Where(x => x.Wallet.Id == wallet.Id);
                        }

                        foreach (var acc in accounts)
                        {
                            Account account = new Account();
                            if (DataService.toDTO(acc, ref account))
                            {
                                DBAccountstate accState = null;
                                IQueryable <DBAccountstate> accResults = null;
                                if (forDate.Equals(DateTime.MaxValue))
                                {
                                    accResults = Session.Query <DBAccountstate>()
                                                 .Where(x => x.Account.Id == acc.Id)
                                                 .OrderByDescending(x => x.Date);
                                }
                                else
                                {
                                    accResults = Session.Query <DBAccountstate>()
                                                 .Where(x => x.Account.Id == acc.Id && x.Date <= forDate)
                                                 .OrderByDescending(x => x.Date);
                                }

                                if (accResults == null || accResults.Count() == 0)
                                {
                                    continue;
                                }
                                // acc.Currency.Id
                                accState = accResults.FirstOrDefault();
                                if (accState != null)
                                {
                                    account.Balance = accState.Balance;
                                    decimal value = account.Balance;
                                    if (acc.Currency != null)
                                    {
                                        value = parent.ConvertToUSD(account.Balance, acc.Currency.Name);
                                    }
                                    balance += value;
                                }
                                wallet.Accounts.Add(account);
                            }
                        }
                        wallet.Balance = balance;
                        results.Add(wallet);
                    }
                }
            }
            catch (Exception e)
            {
                log.Error("GetWalletsState for Date: " + forDate.ToString(xtradeConstants.MTDATETIMEFORMAT) +
                          e);
            }

            return(results);
        }
Ejemplo n.º 3
0
        public void UpdateBalance(int AccountNumber, decimal Balance, decimal Equity)
        {
            lock (lockObject)
            {
                using (ISession Session = ConnectionHelper.CreateNewSession())
                {
                    var terms = Session.Query <DBTerminal>().Where(x => x.Accountnumber == AccountNumber);
                    if (terms == null || terms.Count() <= 0)
                    {
                        return;
                    }
                    DBTerminal terminal = terms.FirstOrDefault();
                    if (terminal == null)
                    {
                        return;
                    }
                    if (terminal.Account == null)
                    {
                        return;
                    }
                    using (ITransaction Transaction = Session.BeginTransaction())
                    {
                        terminal.Account.Balance    = Balance;
                        terminal.Account.Equity     = Equity;
                        terminal.Account.Lastupdate = DateTime.UtcNow;
                        Session.Update(terminal);
                        Transaction.Commit();
                    }

                    var acc = Session.Query <DBAccountstate>().Where(x => x.Account.Id == terminal.Account.Id)
                              .OrderByDescending(x => x.Date);
                    using (ITransaction Transaction = Session.BeginTransaction())
                    {
                        if (acc.Any())
                        {
                            DBAccountstate state = null;
                            state = acc.FirstOrDefault();
                            if (state == null || state.Date.DayOfYear != DateTime.Today.DayOfYear)
                            {
                                var newstate = new DBAccountstate();
                                if (state == null)
                                {
                                    newstate.Account = terminal.Account;
                                }
                                else
                                {
                                    newstate.Account = state.Account;
                                }
                                newstate.Balance = Balance;
                                newstate.Comment = "Autoupdate";
                                newstate.Date    = DateTime.UtcNow;
                                Session.Save(newstate);
                            }
                            else
                            {
                                state.Balance = Balance;
                                state.Comment = "Autoupdate";
                                state.Date    = DateTime.UtcNow;
                                Session.Update(state);
                            }

                            Transaction.Commit();
                        }
                    }
                }
            }
        }