public async Task <IActionResult> GenerateAgingReport(ReportFilterViewModel model) { try { if (!ModelState.IsValid) { throw new Exception("Form is not valid!"); } var userId = new Guid(User.FindFirstValue(ClaimTypes.NameIdentifier)); var result = await _reportBusinessManager.GetAgingReport(model.CompanyId, model.Date, 30, model.NumberOfPeriods, false); var plan = await _reportBusinessManager.GetSavedPlanReport(userId, model.CompanyId, model.Date); var viewDataDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { { "Plan", _mapper.Map <SavedReportPlanViewModel>(plan) } }; string html = await _viewRenderService.RenderToStringAsync("_AgingReportPartial", result, viewDataDictionary); return(Ok(html)); } catch (Exception er) { return(BadRequest(er.Message ?? er.StackTrace)); } }
public async Task <IActionResult> Export(long id, ReportFilterViewModel model) { try { if (!ModelState.IsValid) { throw new Exception("Form is not valid!"); } var settings = await _companyBusinessManager.GetExportSettings(id); if (settings == null) { return(NotFound()); } var result = await _reportBusinessManager.GetAgingReport(model.CompanyId, model.Date, _daysPerPeriod, model.NumberOfPeriods, settings.IncludeAllCustomers); var mem = new MemoryStream(); var writer = new StreamWriter(mem); var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture); //csvWriter.Configuration.Delimiter = ";"; //csvWriter.Configuration.HasHeaderRecord = true; //csvWriter.Configuration.AutoMap<ExpandoObject>(); var fields = settings.Fields.OrderBy(x => x.Sort);//.Concat(columns); foreach (var field in fields) { if (field.IsActive) { csvWriter.WriteField <string>(field.Value); } } csvWriter.NextRecord(); #region SORT BY ACCOUNT NUMBER/CUSTOMER BUSINESS NAME var sortedInvoices = result.Rows; if (settings.Sort == 0) { sortedInvoices = sortedInvoices.OrderBy(x => x.AccountNo).ToList(); } else { sortedInvoices = sortedInvoices.OrderBy(x => x.Customer.Name).ToList(); } #endregion foreach (var row in sortedInvoices) { foreach (var field in fields) { if (field.IsActive) { var matchSumField = Regex.Matches(field.Name, matchFieldSumExpression, RegexOptions.IgnoreCase); //check for regex. get values between curly braces {} if (matchSumField.Count > 0) { var fieldName = field.Name; foreach (var match in matchSumField.ToList()) { var key = match.Value.Trim(new char[] { '{', '}' }); var value = row.Data.ContainsKey(key) ? row.Data[key] : ObjectExtension.GetPropValue(row, key); fieldName = fieldName.Replace(match.Value, (value ?? 0).ToString(), StringComparison.OrdinalIgnoreCase); } var data = Convert.ToDecimal(new DataTable().Compute(fieldName, null)); csvWriter.WriteField(data == 0 ? settings.DefaultValueIfEmpty : data.ToString()); } else { var value = ObjectExtension.GetPropValue(row, field.Name); //TODO: Здесь нужно сделать проверку на массив и взять его значение //if(value.GetType() == typeof(Array)) { // Console.WriteLine("Array"); //} var data = row.Data.ContainsKey(field.Name) ? row.Data[field.Name].ToString() : value; csvWriter.WriteField(data == null || data.Equals("0") ? settings.DefaultValueIfEmpty : data); } } } csvWriter.NextRecord(); } writer.Flush(); mem.Position = 0; var fileName = settings.Title; var matchFileName = Regex.Match(fileName, matchFileNameExpression, RegexOptions.IgnoreCase); if (matchFileName.Success) { string template = matchFileName.Value.Trim(new char[] { '{', '}', '$' }); var date = model.Date.ToString(template, DateTimeFormatInfo.InvariantInfo); fileName = Regex.Replace(fileName, matchFileNameExpression, matchFileName.Value.Contains('$') ? date.ToUpper() : date, RegexOptions.IgnoreCase); } FileStreamResult fileStreamResult = new FileStreamResult(mem, "application/octet-stream"); fileStreamResult.FileDownloadName = fileName; return(fileStreamResult); } catch (Exception er) { return(BadRequest(er.Message ?? er.StackTrace)); } }