yavsc/Yavsc/ViewComponents/EstimateViewComponent.cs

79 lines
2.9 KiB
C#
Raw Normal View History

2016-11-09 14:03:57 +01:00
using System;
using System.IO;
using System.Linq;
2016-11-30 11:01:50 +01:00
using System.Threading.Tasks;
2016-11-09 14:03:57 +01:00
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;
}
2016-11-30 11:01:50 +01:00
public async Task<IViewComponentResult> InvokeAsync(long id)
2016-11-09 14:03:57 +01:00
{
2016-11-30 11:01:50 +01:00
return await InvokeAsync(id,"Html");
}
2016-12-06 23:39:29 +01:00
public async Task<IViewComponentResult> InvokeAsync(long id, string outputFormat ="Html")
{
return await InvokeAsync(id,outputFormat,false,false);
}
public async Task<IViewComponentResult> InvokeAsync(long id, string outputFormat ="Html", bool asBill = false, bool acquitted = false)
2016-11-30 11:01:50 +01:00
{
return await Task.Run( ()=> {
Estimate estimate =
2016-11-09 14:03:57 +01:00
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");
2016-12-06 11:40:32 +01:00
var di = new DirectoryInfo(Startup.SiteSetup.UserFiles.Bills);
var dia = new DirectoryInfo(Startup.SiteSetup.UserFiles.Avatars);
ViewBag.BillsDir = di.FullName;
ViewBag.AvatarsDir = dia.FullName;
2016-12-06 23:39:29 +01:00
ViewBag.AsBill = asBill;
ViewBag.Acquitted = acquitted;
2016-11-30 11:01:50 +01:00
if (outputFormat == "LaTeX") {
return this.View("Estimate_tex", estimate);
}
else if (outputFormat == "Pdf")
2016-11-09 14:03:57 +01:00
{
2016-11-30 11:01:50 +01:00
// Sorry for this code
2016-11-09 14:03:57 +01:00
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{
2016-12-06 23:39:29 +01:00
Temp = Startup.Temp,
2016-11-09 14:03:57 +01:00
TeXSource = tex,
DestDir = Startup.UserBillsDirName,
BaseFileName = $"estimate-{id}"
} );
}
2016-11-30 11:01:50 +01:00
return View("Default",estimate);
}
);
2016-11-09 14:03:57 +01:00
}
}
2016-12-06 23:39:29 +01:00
}