Exemple #1
0
        public async Task <int> UpsertWorstCaseBudgetCashflowUnit(WorstCaseBudgetCashflowFormDto form)
        {
            var month = form.DueDate.AddHours(_identityService.TimezoneOffset).AddMonths(1).Month;
            var year  = form.DueDate.AddHours(_identityService.TimezoneOffset).AddMonths(1).Year;
            var model = _dbContext.BudgetCashflowWorstCases.FirstOrDefault(entity => entity.UnitId == form.UnitId && entity.Month == month && entity.Year == year);

            if (model == null)
            {
                model = new BudgetCashflowWorstCase(form.DueDate.AddHours(_identityService.TimezoneOffset), form.UnitId);
                EntityExtension.FlagForCreate(model, _identityService.Username, UserAgent);
                _dbContext.BudgetCashflowWorstCases.Add(model);
                await _dbContext.SaveChangesAsync();
            }

            var items = _dbContext.BudgetCashflowWorstCaseItems.Where(entity => entity.BudgetCashflowWorstCaseId == model.Id);

            foreach (var formItem in form.Items)
            {
                var currencyId   = formItem.Currency != null ? formItem.Currency.Id : 0;
                var existingItem = items.FirstOrDefault(item => item.CurrencyId == currencyId && item.LayoutOrder == formItem.LayoutOrder && item.BudgetCashflowWorstCaseId == model.Id);

                if (existingItem != null)
                {
                    existingItem.UpdateNominal(formItem.CurrencyNominal, formItem.Nominal, formItem.ActualNominal);
                    EntityExtension.FlagForUpdate(existingItem, _identityService.Username, UserAgent);
                    _dbContext.BudgetCashflowWorstCaseItems.Update(existingItem);
                }
                else
                {
                    var item = new BudgetCashflowWorstCaseItem(formItem.LayoutOrder, currencyId, formItem.CurrencyNominal, formItem.Nominal, formItem.ActualNominal, model.Id, form.UnitId);
                    EntityExtension.FlagForCreate(item, _identityService.Username, UserAgent);
                    _dbContext.BudgetCashflowWorstCaseItems.Add(item);
                }
            }
            await _dbContext.SaveChangesAsync();

            //var model = new BudgetCashflowWorstCase(form.Date.AddHours(_identityService.TimezoneOffset), form.UnitId);
            //EntityExtension.FlagForCreate(model, _identityService.Username, UserAgent);
            //_dbContext.BudgetCashflowWorstCases.Add(model);
            //await _dbContext.SaveChangesAsync();

            //var items = form.Items.Select(item => new BudgetCashflowWorstCaseItem(item.LayoutOrder, item.Currency.Id, item.CurrencyNominal, item.Nominal, model.Id));
            //_dbContext.BudgetCashflowWorstCaseItems.AddRange(items);
            //await _dbContext.SaveChangesAsync();
            return(model.Id);
        }
Exemple #2
0
 public Task <int> UpdateWorstCaseBudgetCashflowUnit(int year, int month, int unitId, WorstCaseBudgetCashflowFormDto form)
 {
     throw new NotImplementedException();
 }