yavsc/src/Api/Controllers/Business/BillingController.cs

185 lines
7.6 KiB
C#
Raw Normal View History

2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2019-08-21 17:23:58 +01:00
using Microsoft.Extensions.Localization;
using Newtonsoft.Json;
using System.Security.Claims;
using Yavsc.Helpers;
using Yavsc.ViewModels;
namespace Yavsc.ApiControllers
{
using Models;
using Services;
2016-11-09 14:03:57 +01:00
using Models.Messaging;
2023-03-19 17:57:55 +00:00
using Microsoft.Extensions.Options;
using Microsoft.EntityFrameworkCore;
2025-02-14 00:20:35 +00:00
using Yavsc.ViewModels.Auth;
2025-02-17 23:56:28 +00:00
using Yavsc.Server.Helpers;
2017-06-08 00:26:29 +02:00
[Route("api/bill"), Authorize]
public class BillingController : Controller
2016-11-07 19:34:56 +01:00
{
2020-10-09 19:35:39 +01:00
readonly ApplicationDbContext dbContext;
private readonly IStringLocalizer _localizer;
private readonly GoogleAuthSettings _googleSettings;
private readonly IYavscMessageSender _GCMSender;
private readonly IAuthorizationService authorizationService;
2016-11-09 14:03:57 +01:00
2017-06-08 00:26:29 +02:00
2020-10-09 19:35:39 +01:00
private readonly ILogger logger;
private readonly IBillingService billingService;
2016-11-09 14:03:57 +01:00
2017-06-08 00:26:29 +02:00
public BillingController(
IAuthorizationService authorizationService,
ILoggerFactory loggerFactory,
2024-02-25 18:05:10 +00:00
IStringLocalizer<Yavsc.YavscLocalization> SR,
2017-03-05 20:29:02 +01:00
ApplicationDbContext context,
IOptions<GoogleAuthSettings> googleSettings,
2019-05-08 01:35:10 +01:00
IYavscMessageSender GCMSender,
2017-06-08 00:26:29 +02:00
IBillingService billingService
2017-03-05 20:29:02 +01:00
)
2016-11-09 14:03:57 +01:00
{
2017-03-05 20:29:02 +01:00
_googleSettings=googleSettings.Value;
this.authorizationService = authorizationService;
2016-11-09 14:03:57 +01:00
dbContext = context;
2017-06-08 00:26:29 +02:00
logger = loggerFactory.CreateLogger<BillingController>();
2017-02-22 22:56:03 +01:00
this._localizer = SR;
2017-03-05 20:29:02 +01:00
_GCMSender=GCMSender;
2017-06-08 00:26:29 +02:00
this.billingService=billingService;
2016-11-09 14:03:57 +01:00
}
2017-06-08 11:05:57 +02:00
[HttpGet("facture-{billingCode}-{id}.pdf"), Authorize]
2017-06-08 00:26:29 +02:00
public async Task<IActionResult> GetPdf(string billingCode, long id)
{
var bill = await billingService.GetBillAsync(billingCode, id);
2025-02-23 20:23:23 +00:00
if ( authorizationService.AuthorizeAsync(User, bill, new ReadPermission()).IsFaulted)
2016-11-07 19:34:56 +01:00
{
return new ChallengeResult();
}
var fi = bill.GetBillInfo(billingService);
2016-11-09 14:03:57 +01:00
if (!fi.Exists) return Ok(new { Error = "Not generated" });
return File(fi.OpenRead(), "application/x-pdf", fi.Name);
2016-11-09 14:03:57 +01:00
}
2017-06-08 11:05:57 +02:00
[HttpGet("facture-{billingCode}-{id}.tex"), Authorize]
2017-06-08 00:26:29 +02:00
public async Task<IActionResult> GetTex(string billingCode, long id)
2016-11-09 14:03:57 +01:00
{
2017-06-08 00:26:29 +02:00
var bill = await billingService.GetBillAsync(billingCode, id);
2017-06-08 11:05:57 +02:00
if (bill==null) {
2017-06-08 17:09:06 +02:00
logger.LogCritical ( $"# not found !! {id} in {billingCode}");
2023-03-19 17:57:55 +00:00
return this.NotFound();
2017-06-08 11:05:57 +02:00
}
2023-03-19 17:57:55 +00:00
logger.LogTrace(JsonConvert.SerializeObject(bill));
2017-06-08 00:26:29 +02:00
2025-02-23 20:23:23 +00:00
if (!(await authorizationService.AuthorizeAsync(User, bill, new ReadPermission())).Succeeded)
{
return new ChallengeResult();
}
2016-11-09 14:03:57 +01:00
Response.ContentType = "text/x-tex";
return ViewComponent("Bill",new object[] {  billingCode, bill , OutputFormat.LaTeX, true });
2016-11-09 14:03:57 +01:00
}
2017-06-08 00:26:29 +02:00
[HttpPost("genpdf/{billingCode}/{id}")]
public async Task<IActionResult> GeneratePdf(string billingCode, long id)
2016-11-09 14:03:57 +01:00
{
2017-06-08 17:09:06 +02:00
var bill = await billingService.GetBillAsync(billingCode, id);
2017-06-08 17:09:06 +02:00
if (bill==null) {
logger.LogCritical ( $"# not found !! {id} in {billingCode}");
2023-03-19 17:57:55 +00:00
return this.NotFound();
}
logger.LogWarning("Got bill ack:"+bill.GetIsAcquitted().ToString());
return ViewComponent("Bill",new object[] { billingCode, bill, OutputFormat.Pdf, true } );
2016-11-07 19:34:56 +01:00
}
2017-06-08 00:26:29 +02:00
[HttpPost("prosign/{billingCode}/{id}")]
public async Task<IActionResult> ProSign(string billingCode, long id)
{
2017-02-22 22:56:03 +01:00
var estimate = dbContext.Estimates.
2019-05-24 20:17:24 +01:00
Include(e=>e.Client).Include(e=>e.Client.DeviceDeclaration)
2017-02-22 22:56:03 +01:00
.Include(e=>e.Bill).Include(e=>e.Owner).Include(e=>e.Owner.Performer)
.FirstOrDefault(e=>e.Id == id);
if (estimate == null)
return new BadRequestResult();
2025-02-23 20:23:23 +00:00
if (!(await authorizationService.AuthorizeAsync(User, estimate, new ReadPermission())).Succeeded)
2023-03-19 17:57:55 +00:00
{
return new ChallengeResult();
}
if (Request.Form.Files.Count!=1)
return new BadRequestResult();
2017-06-08 00:26:29 +02:00
User.ReceiveProSignature(billingCode,id,Request.Form.Files[0],"pro");
estimate.ProviderValidationDate = DateTime.Now;
2017-02-23 03:10:30 +01:00
dbContext.SaveChanges(User.GetUserId());
// Notify the client
2017-02-22 22:56:03 +01:00
var locstr = _localizer["EstimationMessageToClient"];
var yaev = new EstimationEvent(estimate,_localizer);
2017-02-22 22:56:03 +01:00
2019-05-24 20:17:24 +01:00
var regids = new [] { estimate.Client.Id };
bool gcmSent = false;
2018-05-04 10:45:45 +02:00
var grep = await _GCMSender.NotifyEstimateAsync(regids,yaev);
gcmSent = grep.success>0;
return Ok (new { ProviderValidationDate = estimate.ProviderValidationDate, GCMSent = gcmSent });
}
2017-06-08 00:26:29 +02:00
[HttpGet("prosign/{billingCode}/{id}")]
public async Task<IActionResult> GetProSign(string billingCode, long id)
{
// For authorization purpose
var estimate = dbContext.Estimates.FirstOrDefault(e=>e.Id == id);
2025-02-23 20:23:23 +00:00
if (!(await authorizationService.AuthorizeAsync(User, estimate, new ReadPermission())).Succeeded)
2023-03-19 17:57:55 +00:00
{
return new ChallengeResult();
}
2019-08-21 17:23:58 +01:00
var filename = AbstractFileSystemHelpers.SignFileNameFormat("pro", billingCode, id);
2018-03-26 19:27:29 +02:00
FileInfo fi = new FileInfo(Path.Combine(AbstractFileSystemHelpers.UserBillsDirName, filename));
2023-03-19 17:57:55 +00:00
if (!fi.Exists) return NotFound(new { Error = "Professional signature not found" });
return File(fi.OpenRead(), "application/x-pdf", filename); ;
}
2017-06-08 00:26:29 +02:00
[HttpPost("clisign/{billingCode}/{id}")]
public async Task<IActionResult> CliSign(string billingCode, long id)
{
2023-03-19 17:57:55 +00:00
var uid = User.FindFirstValue(ClaimTypes.NameIdentifier);
var estimate = dbContext.Estimates.Include( e=>e.Query
2017-02-22 22:56:03 +01:00
).Include(e=>e.Owner).Include(e=>e.Owner.Performer).Include(e=>e.Client)
.FirstOrDefault( e=> e.Id == id && e.Query.ClientId == uid );
2025-02-23 20:23:23 +00:00
if (!(await authorizationService.AuthorizeAsync(User, estimate, new ReadPermission())).Succeeded)
{
return new ChallengeResult();
}
if (Request.Form.Files.Count!=1)
return new BadRequestResult();
2017-06-08 00:26:29 +02:00
User.ReceiveProSignature(billingCode,id,Request.Form.Files[0],"cli");
estimate.ClientValidationDate = DateTime.Now;
2017-02-23 03:10:30 +01:00
dbContext.SaveChanges(User.GetUserId());
return Ok (new { ClientValidationDate = estimate.ClientValidationDate });
}
2017-06-08 00:26:29 +02:00
[HttpGet("clisign/{billingCode}/{id}")]
public async Task<IActionResult> GetCliSign(string billingCode, long id)
{
// For authorization purpose
var estimate = dbContext.Estimates.FirstOrDefault(e=>e.Id == id);
2025-02-23 20:23:23 +00:00
if (!(await authorizationService.AuthorizeAsync(User, estimate, new ReadPermission())).Succeeded)
{
return new ChallengeResult();
}
2019-08-21 17:23:58 +01:00
var filename = AbstractFileSystemHelpers.SignFileNameFormat("pro", billingCode, id);
2018-03-26 19:27:29 +02:00
FileInfo fi = new FileInfo(Path.Combine(AbstractFileSystemHelpers.UserBillsDirName, filename));
2023-03-19 17:57:55 +00:00
if (!fi.Exists) return NotFound(new { Error = "Professional signature not found" });
return File(fi.OpenRead(), "application/x-pdf", filename); ;
}
2016-11-07 19:34:56 +01:00
}
2018-05-04 10:45:45 +02:00
}