public async Task <List <SavedReportListViewModel> > GetSavedReportFactList([FromQuery] ReportFilterViewModel model)
        {
            var companies = await _companyBusinessManager.GetCompanies();

            var userId = new Guid(User.FindFirstValue(ClaimTypes.NameIdentifier));

            var list = await _reportBusinessManager.GetSavedFactReport(userId);

            var result = list.GroupBy(x => x.CompanyId, x => x.Name, (companyId, names) => new {
                Name  = names.First(),
                Key   = companyId ?? 0,
                Count = names.Count()
            }).Select(x => new SavedReportListViewModel()
            {
                Name      = x.Name,
                CompanyId = x.Key,
                Count     = x.Count
            }).ToList();

            var factCompanies = companies.Where(x => !result.Any(y => y.CompanyId == x.Id)).ToList();

            if (factCompanies.Count > 0)
            {
                result.AddRange(factCompanies.Select(x => new SavedReportListViewModel()
                {
                    Name      = x.Name,
                    CompanyId = x.Id,
                    Count     = 0
                }));
            }

            return(result);
        }
Beispiel #2
0
        public async Task <IActionResult> CreateSavedReportView([FromQuery] ReportFilterViewModel model)
        {
            try {
                if (!ModelState.IsValid)
                {
                    throw new Exception("Form is not valid!");
                }

                var company = await _companyBusinessManager.GetCompany(model.CompanyId);

                if (company == null)
                {
                    return(NotFound());
                }

                var userId = new Guid(User.FindFirstValue(ClaimTypes.NameIdentifier));

                var savedItem = await _reportBusinessManager.GetSavedFactReport(userId, model.CompanyId, model.Date);

                if (savedItem != null && savedItem.IsPublished)
                {
                    string html = await _viewRenderService.RenderToStringAsync("_SaveAgingReportPartial", _mapper.Map <SavedReportViewModel>(savedItem));

                    return(Ok(html));
                }
                else
                {
                    var result = _mapper.Map <SavedReportViewModel>(savedItem) ?? new SavedReportViewModel()
                    {
                        CompanyId       = model.CompanyId,
                        Name            = company.Name,
                        Date            = model.Date,
                        NumberOfPeriods = model.NumberOfPeriods
                    };

                    var settings = await _companyBusinessManager.GetAllExportSettings(model.CompanyId);

                    var checkingCustomerAccountNumber = await _reportBusinessManager.CheckingCustomerAccountNumber(model.CompanyId, model.Date, model.NumberOfPeriods);

                    var viewDataDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
                    {
                        { "Settings", _mapper.Map <List <CompanyExportSettingsViewModel> >(settings) },
                        { "CheckingCustomerAccountNumber", _mapper.Map <ReportStatusViewModel>(checkingCustomerAccountNumber) }
                    };

                    string html = await _viewRenderService.RenderToStringAsync("_SaveAgingReportPartial", result, viewDataDictionary);

                    return(Ok(html));
                }
            } catch (Exception er) {
                return(BadRequest(er.Message ?? er.StackTrace));
            }
        }