2023-03-19 17:57:55 +00:00
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2017-06-08 00:26:29 +02:00
|
|
|
using Microsoft.Extensions.Localization;
|
|
|
|
|
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;
|
2018-05-15 12:13:38 +02:00
|
|
|
using Yavsc.Services;
|
2023-03-19 17:57:55 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-09-17 22:22:18 +01:00
|
|
|
using Microsoft.Extensions.Options;
|
2017-06-08 00:26:29 +02:00
|
|
|
|
|
|
|
|
namespace Yavsc.ViewComponents
|
|
|
|
|
{
|
|
|
|
|
public class BillViewComponent : ViewComponent
|
|
|
|
|
{
|
2020-09-12 01:11:30 +01:00
|
|
|
readonly ApplicationDbContext dbContext;
|
|
|
|
|
readonly IBillingService billing;
|
2025-08-31 18:27:53 +01:00
|
|
|
readonly IStringLocalizer<BillViewComponent> localizer;
|
2026-09-17 22:22:18 +01:00
|
|
|
private readonly SiteSettings siteSettings;
|
2017-06-08 00:26:29 +02:00
|
|
|
|
2026-09-17 22:22:18 +01:00
|
|
|
public BillViewComponent(ApplicationDbContext dbContext,
|
2025-08-31 18:27:53 +01:00
|
|
|
IStringLocalizer<BillViewComponent> localizer,
|
2026-09-17 22:22:18 +01:00
|
|
|
IBillingService billing,
|
|
|
|
|
IOptions<SiteSettings> siteSettings)
|
2017-06-08 00:26:29 +02:00
|
|
|
{
|
2018-05-15 12:13:38 +02:00
|
|
|
this.billing = billing;
|
2017-06-08 00:26:29 +02:00
|
|
|
this.dbContext = dbContext;
|
|
|
|
|
this.localizer = localizer;
|
2026-09-17 22:22:18 +01:00
|
|
|
this.siteSettings = siteSettings.Value;
|
2017-06-08 00:26:29 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-17 22:22:18 +01:00
|
|
|
public async Task<IViewComponentResult> InvokeAsync(
|
|
|
|
|
string code,
|
|
|
|
|
IBillable billable,
|
|
|
|
|
OutputFormat format,
|
|
|
|
|
bool asBill,
|
|
|
|
|
SiteSettings settings
|
|
|
|
|
)
|
2017-06-08 00:26:29 +02:00
|
|
|
{
|
2026-09-17 22:22:18 +01:00
|
|
|
var di = new DirectoryInfo(settings.Bills);
|
|
|
|
|
var dia = new DirectoryInfo(settings.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
|
2017-07-08 03:23:21 +02:00
|
|
|
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
|
|
|
|
2026-09-17 22:22:18 +01: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;
|
2026-09-17 22:22:18 +01:00
|
|
|
|
|
|
|
|
var pdfGenerationViewModel = new PdfGenerationViewModel
|
|
|
|
|
{
|
2024-02-25 18:05:10 +00:00
|
|
|
Temp = Config.Temp,
|
2026-09-17 22:22:18 +01:00
|
|
|
TeXSource = tex,
|
|
|
|
|
DestDir = settings.Blog,
|
2018-05-15 12:13:38 +02:00
|
|
|
BaseFileName = billable.GetFileBaseName(billing)
|
2017-07-24 17:45:50 +02:00
|
|
|
};
|
2026-09-17 22:22:18 +01:00
|
|
|
if (settings.GenerateEstimatePdf(pdfGenerationViewModel)) {
|
|
|
|
|
return this.View(new { Generated = pdfGenerationViewModel.BaseFileName+".pdf" });
|
2017-07-24 17:45:50 +02:00
|
|
|
} else {
|
2026-09-17 22:22:18 +01:00
|
|
|
return View(new { Error = pdfGenerationViewModel.GenerationErrorMessage } );
|
2017-07-24 17:45:50 +02:00
|
|
|
}
|
2017-06-08 00:26:29 +02:00
|
|
|
}
|
2026-09-17 22:22:18 +01:00
|
|
|
ViewBag.BillFileInfo = billable.GetBillInfo(billing, siteSettings);
|
|
|
|
|
ViewBag.Settings = settings;
|
2017-06-08 00:26:29 +02:00
|
|
|
return View("Default",billable);
|
2026-09-17 22:22:18 +01:00
|
|
|
|
2017-06-08 00:26:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|