Ejemplo n.º 1
0
 void BindTransactions(ref XTransaction SourceTransaction, ref XTransaction DestTransaction)
 {
     lock (TransactionJournal.XmlJournal) {
         SourceTransaction.BankAccountTransactionFK = DestTransaction.BankAccountTransactionK;
         DestTransaction.BankAccountTransactionFK   = SourceTransaction.BankAccountTransactionK;
     }
 }
        void BindTransactions(ref XTransaction SourceTransaction, ref XTransaction DestTransaction) {

            lock (TransactionJournal.XmlJournal) {
                SourceTransaction.BankAccountTransactionFK = DestTransaction.BankAccountTransactionK;
                DestTransaction.BankAccountTransactionFK = SourceTransaction.BankAccountTransactionK;
            }

        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds an XTransaction to the transactions collection.
        /// </summary>
        public static XTransaction AddTransaction(XTransaction Transaction)
        {
            lock (__staticLock) {
                Transaction.BankAccountTransactionK = RandomString(8);

                XmlJournal.XPathSelectElement("/Journal/Transactions").Add((XElement)Transaction);

                return(Transaction);
            }
        }
        XTransaction BeginSourceTransaction(Money Amount, string Message) {
            XTransaction sourceTran = new XTransaction(Amount);

            sourceTran.BankAccountFK = this.BankAccountK;
            sourceTran.Flags = Journal.BankAccountTransactionFlags.FundsAvailable;
            sourceTran.TransactionDateUtc = DateTime.UtcNow;
            sourceTran.Amount = (Amount * (-1));

            if (!string.IsNullOrEmpty(Message)) {
                sourceTran.Message = Message;
            }

            lock (TransactionJournal.XmlJournal) {
                return Journal.TransactionJournal.AddTransaction(sourceTran);
            }
        }
        XTransaction FinishEndTransaction(string SourceBankTransactionKey, XBankAccount ToAccount, Money Amount, string Message) {
            XTransaction destTran = new XTransaction(Amount);

            destTran.BankAccountFK = ToAccount.BankAccountK;
            destTran.Flags = Journal.BankAccountTransactionFlags.FundsAvailable;
            destTran.TransactionDateUtc = DateTime.UtcNow;
            destTran.Amount = Amount;
            destTran.BankAccountTransactionFK = SourceBankTransactionKey;

            if (!string.IsNullOrEmpty(Message)) {
                destTran.Message = Message;
            }

            lock (TransactionJournal.XmlJournal) {
                return Journal.TransactionJournal.AddTransaction(destTran);
            }
        }
Ejemplo n.º 6
0
        XTransaction BeginSourceTransaction(Money Amount, string Message)
        {
            XTransaction sourceTran = new XTransaction(Amount);

            sourceTran.BankAccountFK      = this.BankAccountK;
            sourceTran.Flags              = Journal.BankAccountTransactionFlags.FundsAvailable;
            sourceTran.TransactionDateUtc = DateTime.UtcNow;
            sourceTran.Amount             = (Amount * (-1));

            if (!string.IsNullOrEmpty(Message))
            {
                sourceTran.Message = Message;
            }

            lock (TransactionJournal.XmlJournal) {
                return(Journal.TransactionJournal.AddTransaction(sourceTran));
            }
        }
Ejemplo n.º 7
0
        XTransaction FinishEndTransaction(string SourceBankTransactionKey, XBankAccount ToAccount, Money Amount, string Message)
        {
            XTransaction destTran = new XTransaction(Amount);

            destTran.BankAccountFK            = ToAccount.BankAccountK;
            destTran.Flags                    = Journal.BankAccountTransactionFlags.FundsAvailable;
            destTran.TransactionDateUtc       = DateTime.UtcNow;
            destTran.Amount                   = Amount;
            destTran.BankAccountTransactionFK = SourceBankTransactionKey;

            if (!string.IsNullOrEmpty(Message))
            {
                destTran.Message = Message;
            }

            lock (TransactionJournal.XmlJournal) {
                return(Journal.TransactionJournal.AddTransaction(destTran));
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Adds an XTransaction to the transactions collection.
        /// </summary>
        public static XTransaction AddTransaction(XTransaction Transaction) {
            lock (__staticLock) {
                Transaction.BankAccountTransactionK = RandomString(8);

                XmlJournal.XPathSelectElement("/Journal/Transactions").Add((XElement)Transaction);

                return Transaction;
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Compresses all transactions into one line.  Doing this is going to remove all transaction history but you gain space and processing speed
        /// </summary>
        public static void SquashJournal() {
            int bankAccountCount = BankAccounts.Count();
            XDocument newJournal = NewJournal();

            bool responsibleForTurningBackupsBackOn = false;

            Console.WriteLine("seconomy xml: beginning Squash");

            if (SEconomyPlugin.BackupCanRun) {
                SEconomyPlugin.BackupCanRun = false;
                responsibleForTurningBackupsBackOn = true;
            }

            for (int i = 0; i < bankAccountCount; i++) {
                XBankAccount account = BankAccounts.ElementAtOrDefault(i);
                if (account != null) {
                    //update account balance
                    account.SyncBalance();

                    string line = string.Format("\r [squash] {0:p} {1}", (double)i / (double)bankAccountCount, account.UserAccountName);
                    SEconomyPlugin.FillWithSpaces(ref line);

                    Console.Write(line);

                    //copy the bank account from the old journal into the new one
                    newJournal.Element("Journal").Element("BankAccounts").Add((XElement)account);

                    //Add the squished summary
                    XTransaction transSummary = new XTransaction(account.Balance);
                    transSummary.BankAccountTransactionK = RandomString(13);
                    transSummary.BankAccountFK = account.BankAccountK;
                    transSummary.Flags = BankAccountTransactionFlags.FundsAvailable | BankAccountTransactionFlags.Squashed;
                    transSummary.Message = "Transaction squash";
                    transSummary.TransactionDateUtc = DateTime.UtcNow;

                    newJournal.Element("Journal").Element("Transactions").Add((XElement)transSummary);
                }
            }

            //abandon the old journal and assign the squashed one
            XmlJournal = newJournal;

            Console.WriteLine("re-syncing online accounts.");

            foreach (XBankAccount account in BankAccounts) {
                XBankAccount runtimeAccount = GetBankAccount(account.BankAccountK);
                if (runtimeAccount != null && runtimeAccount.Owner != null) {
                    Console.WriteLine("re-syncing {0}", runtimeAccount.Owner.TSPlayer.Name);
                    runtimeAccount.SyncBalance();
                }
            }

            SaveXml(Config.JournalPath);

            //the backups could already have been disabled by something else.  We don't want to be the ones turning it back on
            if (responsibleForTurningBackupsBackOn) {
                SEconomyPlugin.BackupCanRun = true;
            }


        }
Ejemplo n.º 10
0
        /// <summary>
        /// Transfers money from this account to the destination account, if negative, takes money from the destination account into this account.
        /// </summary>
        public BankTransferEventArgs TransferTo(XBankAccount ToAccount, Money Amount, BankAccountTransferOptions Options, bool UseProfiler = true, string Message = "")
        {
            lock (__tranlock) {
                BankTransferEventArgs args = new BankTransferEventArgs();
                Guid profile = Guid.Empty;

                if (UseProfiler)
                {
                    profile = SEconomyPlugin.Profiler.Enter(string.Format("transfer: {0} to {1}", this.UserAccountName, ToAccount.UserAccountName));
                }

                if (ToAccount != null && TransferMaySucceed(this, ToAccount, Amount, Options))
                {
                    args.Amount            = Amount;
                    args.SenderAccount     = this;
                    args.ReceiverAccount   = ToAccount;
                    args.TransferOptions   = Options;
                    args.TransferSucceeded = false;

                    //insert the source negative transaction
                    XTransaction sourceTran = BeginSourceTransaction(Amount, Message);
                    if (sourceTran != null && !string.IsNullOrEmpty(sourceTran.BankAccountTransactionK))
                    {
                        //insert the destination inverse transaction
                        XTransaction destTran = FinishEndTransaction(sourceTran.BankAccountTransactionK, ToAccount, Amount, Message);

                        if (destTran != null && !string.IsNullOrEmpty(destTran.BankAccountTransactionK))
                        {
                            //perform the double-entry binding
                            BindTransactions(ref sourceTran, ref destTran);

                            args.TransactionID = sourceTran.BankAccountTransactionK;

                            //update balances
                            this.Balance      += (Amount * (-1));
                            ToAccount.Balance += Amount;

                            //transaction complete
                            args.TransferSucceeded = true;
                        }
                    }
                }
                else
                {
                    args.TransferSucceeded = false;

                    if (!ToAccount.IsSystemAccount && !ToAccount.IsPluginAccount)
                    {
                        if (Amount < 0)
                        {
                            this.Owner.TSPlayer.SendErrorMessageFormat("Invalid amount.");
                        }
                        else
                        {
                            this.Owner.TSPlayer.SendErrorMessageFormat("You need {0} more money to make this payment.", ((Money)(this.Balance - Amount)).ToLongString());
                        }
                    }
                }

                //raise the transfer event
                OnBankTransferComplete(args);

                if (UseProfiler)
                {
                    SEconomyPlugin.Profiler.ExitLog(profile);
                }

                return(args);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Compresses all transactions into one line.  Doing this is going to remove all transaction history but you gain space and processing speed
        /// </summary>
        public static void SquashJournal()
        {
            int       bankAccountCount = BankAccounts.Count();
            int       tranCount        = Transactions.Count();
            XDocument newJournal       = NewJournal();

            bool responsibleForTurningBackupsBackOn = false;

            Console.WriteLine("seconomy xml: beginning Squash");

            if (SEconomyPlugin.BackupCanRun == true)
            {
                SEconomyPlugin.BackupCanRun        = false;
                responsibleForTurningBackupsBackOn = true;
            }

            for (int i = 0; i < bankAccountCount; i++)
            {
                XBankAccount account = BankAccounts.ElementAtOrDefault(i);
                if (account != null)
                {
                    //update account balance
                    account.SyncBalance();

                    string line = string.Format("\r [squash] {0:p} {1}", (double)i / (double)bankAccountCount, account.UserAccountName);
                    SEconomyPlugin.FillWithSpaces(ref line);

                    Console.Write(line);

                    //copy the bank account from the old journal into the new one
                    newJournal.Element("Journal").Element("BankAccounts").Add((XElement)account);

                    //Add the squished summary
                    XTransaction transSummary = new XTransaction(account.Balance);
                    transSummary.BankAccountTransactionK = RandomString(13);
                    transSummary.BankAccountFK           = account.BankAccountK;
                    transSummary.Flags              = BankAccountTransactionFlags.FundsAvailable | BankAccountTransactionFlags.Squashed;
                    transSummary.Message            = "Transaction squash";
                    transSummary.TransactionDateUtc = DateTime.UtcNow;

                    newJournal.Element("Journal").Element("Transactions").Add((XElement)transSummary);
                }
            }

            //abandon the old journal and assign the squashed one
            XmlJournal = newJournal;

            Console.WriteLine("re-syncing online accounts.");

            foreach (XBankAccount account in BankAccounts)
            {
                XBankAccount runtimeAccount = GetBankAccount(account.BankAccountK);
                if (runtimeAccount != null && runtimeAccount.Owner != null)
                {
                    Console.WriteLine("re-syncing {0}", runtimeAccount.Owner.TSPlayer.Name);
                    runtimeAccount.SyncBalance();
                }
            }

            SaveXml(Configuration.JournalPath);

            //the backups could already have been disabled by something else.  We don't want to be the ones turning it back on
            if (responsibleForTurningBackupsBackOn)
            {
                SEconomyPlugin.BackupCanRun = true;
            }
        }