Пример #1
0
        public static void ImportBankTransactions(DefaultContext db, out string bankToken, out string failedUrl)
        {
            bankToken = "Unknown";
            failedUrl = "Unknown";
            string bankTransactionUrlTemplate = Properties.Settings.Default.BankTransactionUrl;
            string bankTransactionSetStartDateUrlTemplate = Properties.Settings.Default.BankTransactionSetStartDateUrl;
            string bankTransactionSetStartIdUrlTemplate = Properties.Settings.Default.BankTransactionSetStartIdUrl;

            BankAccount[] bankAccounts = GetValidAccounts(db);
            foreach (BankAccount bankAccount in bankAccounts)
            {
                bankToken = bankAccount.Token;

                InternetResponse internetResponse;
                if (!bankAccount.LastDownloadId.HasValue)
                {
                    string bankTransactionSetStartDateUrl = String.Format(bankTransactionSetStartDateUrlTemplate, bankAccount.Token, bankAccount.TransactionStartDate.ToString("yyyy-MM-dd"));
                    failedUrl = bankTransactionSetStartDateUrl;
                    internetResponse = InternetRequest.SendRequest(bankTransactionSetStartDateUrl, InternetRequestType.OnlyStatusCode);
                }
                else
                {
                    string bankTransactionSetStartIdUrl = String.Format(bankTransactionSetStartIdUrlTemplate, bankAccount.Token, bankAccount.LastDownloadId.GetValueOrDefault());
                    failedUrl = bankTransactionSetStartIdUrl;
                    internetResponse = InternetRequest.SendRequest(bankTransactionSetStartIdUrl, InternetRequestType.OnlyStatusCode);
                }

                if (internetResponse.HttpStatusCode != HttpStatusCode.OK)
                    continue;

                string bankTransactionUrl = String.Format(bankTransactionUrlTemplate, bankAccount.Token);
                failedUrl = bankTransactionUrl;
                BankAccountHistory[] bankAccountHistories;
                Int64? lastDownloadId;
                HttpStatusCode httpStatusCode = BankAccountHistoryCache.ProcessTransactionRequest(bankTransactionUrl, bankAccount.BankAccountId, out bankAccountHistories, out lastDownloadId);

                if (httpStatusCode != HttpStatusCode.OK || bankAccountHistories.Length == 0)
                    continue;

                foreach (BankAccountHistory bankAccountHistory in bankAccountHistories.Where(bah => bah.Ammount > 0))
                {
                    db.BankAccountHistories.Add(bankAccountHistory);

                    string logMessage = String.Format("Imported bank transaction: account: {0}, date: {1}, ammount: {2}, exchange: {3}, ks: {4}, vs: {5}, ss: {6}", bankAccount.AccountId,bankAccountHistory.Date, bankAccountHistory.Ammount,bankAccountHistory.Exchange, bankAccountHistory.Ks,bankAccountHistory.Vs, bankAccountHistory.Ss);
                    Logger.SetLog(logMessage);
                }

                bankAccount.LastDownloadId = lastDownloadId;

                db.Entry(bankAccount).State = EntityState.Modified;
            }

            db.SaveChanges();
        }