This commit is contained in:
2017-06-08 17:09:06 +02:00
parent a5286af1e5
commit e95de6a9c3
18 changed files with 193 additions and 114 deletions

View File

@ -3,6 +3,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Yavsc.Billing;
using Yavsc.Helpers;
using Yavsc.Models;
@ -15,12 +16,15 @@ namespace Yavsc.ViewComponents
{
ApplicationDbContext dbContext;
IStringLocalizer<Yavsc.Resources.YavscLocalisation> localizer;
ILogger logger ;
public BillViewComponent(ApplicationDbContext dbContext,
IStringLocalizer<Yavsc.Resources.YavscLocalisation> localizer)
IStringLocalizer<Yavsc.Resources.YavscLocalisation> localizer,
ILoggerFactory loggerFactory)
{
this.dbContext = dbContext;
this.localizer = localizer;
logger = loggerFactory.CreateLogger<BillViewComponent>();
}
public async Task<IViewComponentResult> InvokeAsync(string code, IBillable billable, OutputFormat format, bool asBill, bool acquitted)
@ -31,28 +35,34 @@ namespace Yavsc.ViewComponents
ViewBag.AvatarsDir = dia.FullName;
ViewBag.AsBill = asBill; // vrai pour une facture, sinon, c'est un devis
ViewBag.Acquitted = acquitted;
ViewBag.BillingCode = code;
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;
var proaddr = profile.OrganizationAddress.Address;
ViewBag.PerformerOrganizationAddress = proaddr.SplitAddressToTeX() ;
ViewBag.FooterPerformerOrganizationAddress = proaddr.SplitAddressToTeX(", ");
ViewBag.PerformerAddress = performer.PostalAddress?.Address.SplitAddressToTeX() ;
switch (format) {
case OutputFormat.LaTeX :
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;
ViewBag.ClientAddress = client.PostalAddress?.Address.SplitAddressToTeX();
var profile = await dbContext.Performers
.Include(p=>p.OrganizationAddress)
.SingleAsync(p=>p.PerformerId == billable.PerformerId);
ViewBag.PerformerProfile = profile;
ViewBag.ActivityLabel = localizer[billable.ActivityCode];
ViewBag.PerformerOrganizationAddress = profile.OrganizationAddress.Address.SplitAddressToTeX() ;
ViewBag.PerformerAddress = performer.PostalAddress?.Address.SplitAddressToTeX() ;
return this.View("Bill_tex", billable);
case OutputFormat.Pdf :
string tex = null;
@ -60,11 +70,9 @@ namespace Yavsc.ViewComponents
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;
@ -73,7 +81,7 @@ namespace Yavsc.ViewComponents
Temp = Startup.Temp,
TeXSource = tex,
DestDir = Startup.UserBillsDirName,
BaseFileName = $"bill-{code}-{billable.Id}"
BaseFileName = $"facture-{code}-{billable.Id}"
} );
}
return View("Default",billable);