Exemple #1
0
 public async Task <ActionResult <bool> > CreateAsync([FromBody] ImportVM value)
 {
     if (value == null)
     {
         return(this.BadRequest(this.ModelState));
     }
     return(await this.GetService <ImportService>().CreateAsync(value));
 }
Exemple #2
0
        private async Task <ActionResult <bool> > ValidateAsync(ImportVM value)
        {
            try
            {
                // VALIDATE TYPE
                if (value.Entries.Any(x => x.Type != Categories.enCategoryType.Income && x.Type != Categories.enCategoryType.Expense))
                {
                    return(this.WarningResponse(this.GetTranslation("IMPORT_INVALID_TYPE_WARNING")));
                }

                // VALIDATE TEXT
                if (value.Entries.Any(x => string.IsNullOrEmpty(x.Text)))
                {
                    return(this.WarningResponse(this.GetTranslation("IMPORT_INVALID_TEXT_WARNING")));
                }

                // VALIDATE DATES
                if (value.Entries.Any(x => x.DueDate == DateTime.MinValue))
                {
                    return(this.WarningResponse(this.GetTranslation("IMPORT_INVALID_DUEDATE_WARNING")));
                }
                if (value.Entries.Any(x => x.Paid && (!x.PayDate.HasValue || x.PayDate.Value == null || x.PayDate.Value == DateTime.MinValue)))
                {
                    return(this.WarningResponse(this.GetTranslation("IMPORT_INVALID_PAYDATE_WARNING")));
                }

                // VALIDATE VALUE
                if (value.Entries.Any(x => Math.Round(x.Value, 2) == 0))
                {
                    return(this.WarningResponse(this.GetTranslation("IMPORT_INVALID_VALUE_WARNING")));
                }

                // VALIDATE ACCOUNT
                if (value.Entries.Any(x => string.IsNullOrEmpty(x.Account)))
                {
                    return(this.WarningResponse(this.GetTranslation("IMPORT_INVALID_ACCOUNT_WARNING")));
                }

                // VALIDATE CATEGORY
                if (value.Entries.Any(x => string.IsNullOrEmpty(x.Category)))
                {
                    return(this.WarningResponse(this.GetTranslation("IMPORT_INVALID_CATEGORY_WARNING")));
                }

                // RESULT
                return(this.OkResponse(await Task.FromResult(true)));
            }
            catch (Exception ex) { return(this.ExceptionResponse(ex)); }
        }
Exemple #3
0
        internal async Task <ActionResult <bool> > CreateTransfersAsync(ImportVM value)
        {
            try
            {
                var transfersService = this.GetService <Transfers.TransfersService>();
                if (value.Transfers == null || value.Transfers.Count == 0)
                {
                    return(this.OkResponse(true));
                }
                value.Transfers = value.Transfers.OrderBy(x => x.IncomeAccount).ThenBy(x => x.Date).ToList();

                // NUMBER OF ROWS TO LOG AT EACH 10 PERCENTE
                var eachNthRows = Math.Floor((double)(value.Transfers.Count / 10));

                // LOOP THROUGH ENTRIES
                for (int i = 0; i < value.Transfers.Count; i++)
                {
                    var transfer = value.Transfers[i];

                    if ((i % eachNthRows) == 0)
                    {
                        this.TrackEvent("Import Data - Importing Transfers", $"UserID:{value.UserID}", $"Percent:{i / eachNthRows * 10}%");
                    }

                    var createParam = new Transfers.TransferVM
                    {
                        ExpenseAccountID = transfer.ExpenseAccountID.Value,
                        IncomeAccountID  = transfer.IncomeAccountID.Value,
                        TransferDate     = transfer.Date,
                        TransferValue    = transfer.Value
                    };

                    var createMessage = await transfersService.CreateAsync(createParam);

                    var createResult = this.GetValue(createMessage);
                    if (!createResult)
                    {
                        return(createMessage.Result);
                    }
                }

                // RESULT
                return(this.OkResponse(true));
            }
            catch (Exception ex) { return(this.ExceptionResponse(ex)); }
        }
Exemple #4
0
        private async Task <ActionResult <bool> > ClearAsync(ImportVM value)
        {
            try
            {
                var queryPath    = "FriendlyCashFlow.ServerApi.Import.QUERY.Clear.sql";
                var queryContent = await Helpers.EmbededResource.GetResourceContent(queryPath);

                using (var queryReader = this.GetService <Helpers.DataReaderService>().GetDataReader(queryContent))
                {
                    queryReader.AddParameter("@paramResourceID", value.ResourceID);
                    if (!await queryReader.ExecuteReaderAsync())
                    {
                        return(this.WarningResponse("data query error"));
                    }

                    var queryResult = await queryReader.GetDataResultAsync <bool>();

                    return(queryResult?[0] ?? false);
                }
            }
            catch (Exception ex) { return(this.ExceptionResponse(ex)); }
        }
        internal async Task <ActionResult <bool> > CreateCategoriesAsync(ImportVM value)
        {
            try
            {
                var categoriesService = this.GetService <Categories.CategoriesService>();
                if (value.Entries == null)
                {
                    return(this.OkResponse(true));
                }

                // LOAD DATA
                value.Categories = new List <Categories.CategoryVM>();
                var categoryTypes = new Categories.enCategoryType[] { Categories.enCategoryType.Income, Categories.enCategoryType.Expense };
                foreach (var categoryType in categoryTypes)
                {
                    var loadMessage = await categoriesService.GetDataAsync(categoryType);

                    var loadResult = this.GetValue(loadMessage);
                    if (loadResult == null)
                    {
                        return(loadMessage.Result);
                    }
                    value.Categories.AddRange(loadResult);
                }

                // GROUP ALL CATEGORY TEXTS
                value.Entries.ForEach(x => x.Category = x.Category.Replace("\\", "/"));
                var incomeCategoryTexts = value.Entries
                                          .Where(x => x.Type == Categories.enCategoryType.Income).Select(x => x.Category)
                                          .GroupBy(x => x).Select(x => x.Key).OrderBy(x => x)
                                          .ToList();
                incomeCategoryTexts.RemoveAll(x => value.Categories.Select(a => a.HierarchyText).Contains(x));
                var expenseCategoryTexts = value.Entries
                                           .Where(x => x.Type == Categories.enCategoryType.Expense).Select(x => x.Category)
                                           .GroupBy(x => x).Select(x => x.Key).OrderBy(x => x)
                                           .ToList();
                expenseCategoryTexts.RemoveAll(x => value.Categories.Select(a => a.HierarchyText).Contains(x));

                // GET CATEGORY FUNCTION
                var getCategoryID = new Func <Categories.enCategoryType, long?, string, long?>((categoryType, parentID, categoryText) => value.Categories
                                                                                               .Where(a => a.Type == categoryType && a.ParentID == parentID && a.Text == categoryText)
                                                                                               .Select(a => a.CategoryID)
                                                                                               .FirstOrDefault());
                var getHierarchyCategoryID = new Func <Categories.enCategoryType, string, long?>((categoryType, categoryText) => value.Categories
                                                                                                 .Where(a => a.Type == categoryType && a.HierarchyText == categoryText)
                                                                                                 .Select(a => a.CategoryID)
                                                                                                 .FirstOrDefault());

                // NEW CATEGORY FUNCTION
                var newCategory = new Func <Categories.enCategoryType, long?, string, Task>(async(categoryType, parentID, hierarchyText) =>
                {
                    var categoryTexts = hierarchyText.Split(" / ", StringSplitOptions.RemoveEmptyEntries);
                    foreach (var categoryText in categoryTexts)
                    {
                        // TRY TO LOCATE
                        var categoryID = getCategoryID(categoryType, parentID, categoryText);
                        if (!categoryID.HasValue || categoryID.Value == 0)
                        {
                            // ADD A NEW ONE
                            var createParam = new Categories.CategoryVM
                            {
                                ParentID = parentID,
                                Text     = categoryText,
                                Type     = categoryType
                            };
                            var createMessage = await categoriesService.CreateAsync(createParam);
                            var createResult  = this.GetValue(createMessage);
                            if (createResult != null)
                            {
                                value.Categories.Add(createResult);
                                categoryID = createResult.CategoryID;
                            }
                        }

                        parentID = categoryID;
                    }
                });

                // CHECK FOR NEW CATEGORIES
                foreach (var categoryText in incomeCategoryTexts)
                {
                    await newCategory(Categories.enCategoryType.Income, 0, categoryText);
                }
                foreach (var categoryText in expenseCategoryTexts)
                {
                    await newCategory(Categories.enCategoryType.Expense, 0, categoryText);
                }

                // MARK CATEGORIES ON ENTRIES
                if (value.Entries != null)
                {
                    value.Entries.ForEach(x => x.CategoryID = getHierarchyCategoryID(x.Type, x.Category));
                }
                if (value.Entries.Any(x => !x.CategoryID.HasValue || x.CategoryID.Value == 0))
                {
                    return(this.WarningResponse("IMPORT_SOME_CATEGORIES_COULD_NOT_BE_DEFINED"));
                }

                // RESULT
                return(this.OkResponse(true));
            }
            catch (Exception ex) { return(this.ExceptionResponse(ex)); }
        }
        internal async Task <ActionResult <bool> > CreateAccountsAsync(ImportVM value)
        {
            try
            {
                var accountsService = this.GetService <Accounts.AccountsService>();

                // LOAD DATA
                var loadMessage = await accountsService.GetDataAsync();

                var loadResult = this.GetValue(loadMessage);
                if (loadResult == null)
                {
                    return(loadMessage.Result);
                }
                value.Accounts = loadResult;

                // GROUP ALL ACCOUNT TEXTS
                var accountTexts = new List <string>();
                if (value.Entries != null)
                {
                    accountTexts.AddRange(value.Entries?.Select(x => x.Account).ToArray());
                }
                if (value.Transfers != null)
                {
                    accountTexts.AddRange(value.Transfers?.Select(x => x.IncomeAccount).ToArray());
                    accountTexts.AddRange(value.Transfers?.Select(x => x.ExpenseAccount).ToArray());
                }
                accountTexts = accountTexts.GroupBy(x => x).Select(x => x.Key).ToList();

                // CHECK FOR NEW ACCOUNTS
                accountTexts.RemoveAll(x => value.Accounts.Select(a => a.Text).Contains(x));
                foreach (var accountText in accountTexts)
                {
                    var createParam = new Accounts.AccountVM
                    {
                        Text   = accountText,
                        Type   = Accounts.enAccountType.General,
                        Active = true
                    };
                    var createMessage = await accountsService.CreateAsync(createParam);

                    var createResult = this.GetValue(createMessage);
                    if (createResult != null)
                    {
                        value.Accounts.Add(createResult);
                    }
                }

                // GET ACCOUNT FUNCTION
                var getAccountID = new Func <string, long?>(accountText => value.Accounts
                                                            .Where(a => a.Text == accountText)
                                                            .Select(a => a.AccountID)
                                                            .FirstOrDefault());

                // MARK ACCOUNTS ON ENTRIES
                if (value.Entries != null)
                {
                    value.Entries.ForEach(x => x.AccountID = getAccountID(x.Account));
                }
                if (value.Entries.Any(x => !x.AccountID.HasValue || x.AccountID.Value == 0))
                {
                    return(this.WarningResponse("IMPORT_SOME_ACCOUNTS_COULD_NOT_BE_DEFINED"));
                }

                // MARK ACCOUNTS ON TRANSFERS
                if (value.Transfers != null)
                {
                    value.Transfers.ForEach(x =>
                    {
                        x.IncomeAccountID  = getAccountID(x.IncomeAccount);
                        x.ExpenseAccountID = getAccountID(x.ExpenseAccount);
                    });
                    if (value.Transfers.Any(x =>
                                            !x.IncomeAccountID.HasValue || x.IncomeAccountID.Value == 0 ||
                                            !x.ExpenseAccountID.HasValue || x.ExpenseAccountID.Value == 0))
                    {
                        return(this.WarningResponse("IMPORT_SOME_ACCOUNTS_COULD_NOT_BE_DEFINED"));
                    }
                }

                // RESULT
                return(this.OkResponse(true));
            }
            catch (Exception ex) { return(this.ExceptionResponse(ex)); }
        }
Exemple #7
0
        internal async Task <ActionResult <bool> > CreateAsync(ImportVM value)
        {
            try
            {
                // VALIDATE
                var validateMessage = await this.ValidateAsync(value);

                var validateResult = this.GetValue(validateMessage);
                if (!validateResult)
                {
                    return(validateMessage.Result);
                }

                // INITIALIZE
                var user = this.GetService <Helpers.User>();
                value.UserID     = user.UserID;
                value.ResourceID = user.ResourceID;
                if (value.ClearDataBefore)
                {
                    await this.ClearAsync(value);
                }
                this.TrackEvent("Import Data - Start",
                                $"UserID:{value.UserID}",
                                $"ClearDataBefore:{value.ClearDataBefore}",
                                $"Entries:{value.Entries?.Count ?? 0}",
                                $"Transfers:{value.Transfers?.Count ?? 0}");

                // ACCOUNTS
                this.TrackEvent("Import Data - Importing Accounts", $"UserID:{value.UserID}");
                var accountsMessage = await this.CreateAccountsAsync(value);

                var accountsResult = this.GetValue(accountsMessage);
                if (!accountsResult)
                {
                    return(accountsMessage.Result);
                }
                this.TrackEvent("Import Data - Accounts Imported", $"UserID:{value.UserID}");

                // CATEGORIES
                this.TrackEvent("Import Data - Importing Categories", $"UserID:{value.UserID}");
                var categoriesMessage = await this.CreateCategoriesAsync(value);

                var categoriesResult = this.GetValue(categoriesMessage);
                if (!categoriesResult)
                {
                    return(categoriesMessage.Result);
                }
                this.TrackEvent("Import Data - Categories Imported", $"UserID:{value.UserID}");

                // ENTRIES
                this.TrackEvent("Import Data - Importing Entries", $"UserID:{value.UserID}");
                var entriesMessage = await this.CreateEntriesAsync(value);

                var entriesResult = this.GetValue(entriesMessage);
                if (!entriesResult)
                {
                    return(entriesMessage.Result);
                }
                this.TrackEvent("Import Data - Entries Imported", $"UserID:{value.UserID}");

                // TRANSFERS
                this.TrackEvent("Import Data - Importing Transfers", $"UserID:{value.UserID}");
                var transfersMessage = await this.CreateTransfersAsync(value);

                var transfersResult = this.GetValue(transfersMessage);
                if (!transfersResult)
                {
                    return(transfersMessage.Result);
                }
                this.TrackEvent("Import Data - Transfers Imported", $"UserID:{value.UserID}");

                // RESULT
                this.TrackEvent("Import Data - Finish", $"UserID:{value.UserID}");
                return(this.OkResponse(true));
            }
            catch (Exception ex) { return(this.ExceptionResponse(ex)); }
        }
Exemple #8
0
        internal async Task <ActionResult <bool> > CreateEntriesAsync(ImportVM value)
        {
            try
            {
                var entriesService = this.GetService <Entries.EntriesService>();
                if (value.Entries == null || value.Entries.Count == 0)
                {
                    return(this.OkResponse(true));
                }
                value.Entries = value.Entries.OrderBy(x => x.Account).ThenBy(x => x.DueDate).ToList();

                // NUMBER OF ROWS TO LOG AT EACH 10 PERCENTE
                var eachNthRows = Math.Floor((double)(value.Entries.Count / 10));

                // LOOP THROUGH ENTRIES
                for (int i = 0; i < value.Entries.Count; i++)
                {
                    var entry = value.Entries[i];

                    if ((i % eachNthRows) == 0)
                    {
                        this.TrackEvent("Import Data - Importing Entries", $"UserID:{value.UserID}", $"Percent:{i / eachNthRows * 10}%");
                    }

                    var createParam = new Entries.EntryVM
                    {
                        Text         = entry.Text,
                        Type         = entry.Type,
                        AccountID    = entry.AccountID,
                        CategoryID   = entry.CategoryID,
                        DueDate      = entry.DueDate,
                        EntryValue   = Math.Round(Math.Abs(entry.Value), 2),
                        Paid         = entry.Paid,
                        PayDate      = entry.PayDate,
                        RecurrencyID = entry.RecurrencyID,
                        EntryID      = 0
                    };
                    if (!string.IsNullOrEmpty(entry.Recurrency) && !createParam.RecurrencyID.HasValue)
                    {
                        createParam.Recurrency = new Recurrencies.RecurrencyVM
                        {
                            Type  = Recurrencies.enRecurrencyType.Monthly,
                            Count = 1
                        };
                    }

                    var createMessage = await entriesService.CreateAsync(createParam);

                    var createResult = this.GetValue(createMessage);
                    if (createResult == null)
                    {
                        return(createMessage.Result);
                    }

                    if (createParam.Recurrency != null)
                    {
                        value.Entries
                        .Where(x => x.Recurrency == entry.Recurrency && !x.RecurrencyID.HasValue)
                        .ToList()
                        .ForEach(x => x.RecurrencyID = createResult.RecurrencyID);
                    }
                }

                // RESULT
                return(this.OkResponse(true));
            }
            catch (Exception ex) { return(this.ExceptionResponse(ex)); }
        }