yavsc/Yavsc/ViewComponents/BillViewComponent.cs

102 lines
4.3 KiB
C#
Raw Normal View History

2017-06-08 00:26:29 +02:00
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Localization;
2017-06-08 17:09:06 +02:00
using Microsoft.Extensions.Logging;
2018-03-26 19:27:29 +02:00
using Yavsc.Abstract.FileSystem;
2017-06-08 00:26:29 +02:00
using Yavsc.Billing;
using Yavsc.Helpers;
using Yavsc.Models;
2017-06-08 11:08:33 +02:00
using Yavsc.ViewModels;
2017-06-08 00:26:29 +02:00
using Yavsc.ViewModels.Gen;
using Yavsc.Services;
2017-06-08 00:26:29 +02:00
namespace Yavsc.ViewComponents
{
public class BillViewComponent : ViewComponent
{
ApplicationDbContext dbContext;
IBillingService billing;
2017-06-08 00:26:29 +02:00
IStringLocalizer<Yavsc.Resources.YavscLocalisation> localizer;
2017-06-08 17:09:06 +02:00
ILogger logger ;
2017-06-08 00:26:29 +02:00
public BillViewComponent(ApplicationDbContext dbContext,
2017-06-08 17:09:06 +02:00
IStringLocalizer<Yavsc.Resources.YavscLocalisation> localizer,
IBillingService billing,
2017-06-08 17:09:06 +02:00
ILoggerFactory loggerFactory)
2017-06-08 00:26:29 +02:00
{
this.billing = billing;
2017-06-08 00:26:29 +02:00
this.dbContext = dbContext;
this.localizer = localizer;
2017-06-08 17:09:06 +02:00
logger = loggerFactory.CreateLogger<BillViewComponent>();
2017-06-08 00:26:29 +02:00
}
public async Task<IViewComponentResult> InvokeAsync(string code, IBillable billable, OutputFormat format, bool asBill)
2017-06-08 00:26:29 +02:00
{
2018-06-10 20:40:11 +02:00
var di = new DirectoryInfo(Startup.SiteSetup.Bills);
var dia = new DirectoryInfo(Startup.SiteSetup.Avatars);
2017-06-08 00:26:29 +02:00
ViewBag.BillsDir = di.FullName;
ViewBag.AvatarsDir = dia.FullName;
ViewBag.AsBill = asBill; // vrai pour une facture, sinon, c'est un devis
ViewBag.Acquitted = billable.GetIsAcquitted();
2017-06-08 17:09:06 +02:00
2017-06-08 00:26:29 +02:00
ViewBag.BillingCode = code;
2017-06-08 17:09:06 +02:00
var client = await dbContext.Users
.Include(u=>u.PostalAddress)
.SingleAsync(u=>u.Id == billable.ClientId);
ViewBag.Client = client;
var performer = await dbContext.Users
.Include(u=>u.BankInfo)
.Include(u=>u.PostalAddress)
.SingleAsync(u=>u.Id == billable.PerformerId);
ViewBag.Performer = performer;
string clientAddress = client.PostalAddress?.Address ?? null;
ViewBag.ClientAddress = clientAddress.SplitAddressToTeX();
var profile = await dbContext.Performers
.Include(p=>p.OrganizationAddress)
.SingleAsync(p=>p.PerformerId == billable.PerformerId);
ViewBag.PerformerProfile = profile;
ViewBag.ActivityLabel = (await dbContext.Activities.SingleAsync(a => a.Code == billable.ActivityCode)).Name;
2017-06-08 00:26:29 +02:00
2017-06-08 17:09:06 +02:00
var proaddr = profile.OrganizationAddress.Address;
ViewBag.PerformerOrganizationAddress = proaddr.SplitAddressToTeX() ;
ViewBag.FooterPerformerOrganizationAddress = proaddr.SplitAddressToTeX(", ");
2017-06-08 00:26:29 +02:00
2017-06-08 17:09:06 +02:00
ViewBag.PerformerAddress = performer.PostalAddress?.Address.SplitAddressToTeX() ;
switch (format) {
case OutputFormat.LaTeX :
2017-06-08 00:26:29 +02:00
return this.View("Bill_tex", billable);
case OutputFormat.Pdf :
string tex = null;
var oldWriter = ViewComponentContext.ViewContext.Writer;
using (var writer = new StringWriter())
{
this.ViewComponentContext.ViewContext.Writer = writer;
var resultTex = View("Bill_tex", billable);
await resultTex.ExecuteAsync(this.ViewComponentContext);
tex = writer.ToString();
}
ViewComponentContext.ViewContext.Writer = oldWriter;
var genrtrData = new PdfGenerationViewModel{
2017-06-08 00:26:29 +02:00
Temp = Startup.Temp,
TeXSource = tex,
2018-03-26 19:27:29 +02:00
DestDir = AbstractFileSystemHelpers.UserBillsDirName,
BaseFileName = billable.GetFileBaseName(billing)
};
if (genrtrData.GenerateEstimatePdf()) {
return Json(new { Generated = genrtrData.BaseFileName+".pdf" });
} else {
return Json(new { Error = genrtrData.GenerationErrorMessage } );
}
2017-06-08 00:26:29 +02:00
}
ViewBag.BillFileInfo = billable.GetBillInfo(billing);
2017-06-08 00:26:29 +02:00
return View("Default",billable);
}
}
}