yavsc/Yavsc/ViewComponents/EstimateViewComponent.cs

70 lines
2.4 KiB
C#

8 years ago
using System;
using System.IO;
using System.Linq;
8 years ago
using System.Threading.Tasks;
8 years ago
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Yavsc.Models;
using Yavsc.Models.Billing;
using Yavsc.ViewModels.Gen;
namespace Yavsc.ViewComponents
{
public class EstimateViewComponent : ViewComponent
{
ApplicationDbContext dbContext;
public EstimateViewComponent(ApplicationDbContext dbContext)
{
this.dbContext = dbContext;
}
8 years ago
public async Task<IViewComponentResult> InvokeAsync(long id)
8 years ago
{
8 years ago
return await InvokeAsync(id,"Html");
}
public async Task<IViewComponentResult> InvokeAsync(long id, string outputFormat ="Html")
{
return await Task.Run( ()=> {
Estimate estimate =
8 years ago
dbContext.Estimates.Include(x => x.Query)
.Include(x => x.Query.Client)
.Include(x => x.Query.PerformerProfile)
.Include(x => x.Query.PerformerProfile.OrganizationAddress)
.Include(x => x.Query.PerformerProfile.Performer)
.Include(e => e.Bill).FirstOrDefault(x => x.Id == id);
if (estimate == null)
throw new Exception("No data");
8 years ago
if (outputFormat == "LaTeX") {
return this.View("Estimate_tex", estimate);
}
else if (outputFormat == "Pdf")
8 years ago
{
8 years ago
// Sorry for this code
8 years ago
string tex = null;
var oldWriter = ViewComponentContext.ViewContext.Writer;
using (var writer = new StringWriter())
{
this.ViewComponentContext.ViewContext.Writer = writer;
var resultTex = View("Estimate_tex", estimate);
resultTex.Execute(this.ViewComponentContext);
tex = writer.ToString();
}
ViewComponentContext.ViewContext.Writer = oldWriter;
return this.View("Estimate_pdf",
new PdfGenerationViewModel{
TeXSource = tex,
DestDir = Startup.UserBillsDirName,
BaseFileName = $"estimate-{id}"
} );
}
8 years ago
return View("Default",estimate);
}
);
8 years ago
}
}
}