yavsc/src/Yavsc/ApiControllers/Business/BillingController.cs

190 lines
7.7 KiB
C#
Raw Normal View History

2016-11-07 19:34:56 +01:00
using System.IO;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
2016-11-09 14:03:57 +01:00
using System.Web.Routing;
using System.Linq;
using Microsoft.Data.Entity;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Localization;
2017-03-05 20:29:02 +01:00
using Microsoft.Extensions.OptionsModel;
using System;
using System.Security.Claims;
namespace Yavsc.ApiControllers
{
using Models;
using Helpers;
using Services;
2016-11-09 14:03:57 +01:00
using Models.Messaging;
using ViewModels.Auth;
2017-06-08 00:26:29 +02:00
using Newtonsoft.Json;
2017-06-08 11:05:57 +02:00
using Yavsc.ViewModels;
2018-03-26 19:27:29 +02:00
using Yavsc.Abstract.FileSystem;
2017-06-08 00:26:29 +02:00
[Route("api/bill"), Authorize]
public class BillingController : Controller
2016-11-07 19:34:56 +01:00
{
2016-11-09 14:03:57 +01:00
ApplicationDbContext dbContext;
private IStringLocalizer _localizer;
private GoogleAuthSettings _googleSettings;
2019-05-08 01:35:10 +01:00
private IYavscMessageSender _GCMSender;
private IAuthorizationService authorizationService;
2016-11-09 14:03:57 +01:00
2017-06-08 00:26:29 +02:00
private ILogger logger;
2017-06-08 00:26:29 +02:00
private IBillingService billingService;
2016-11-09 14:03:57 +01:00
2017-06-08 00:26:29 +02:00
public BillingController(
IAuthorizationService authorizationService,
ILoggerFactory loggerFactory,
2017-02-22 22:56:03 +01:00
IStringLocalizer<Yavsc.Resources.YavscLocalisation> 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);
if (!await authorizationService.AuthorizeAsync(User, bill, new ViewRequirement()))
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}");
2017-06-08 11:05:57 +02:00
return this.HttpNotFound();
}
2017-06-08 00:26:29 +02:00
logger.LogVerbose(JsonConvert.SerializeObject(bill));
if (!await authorizationService.AuthorizeAsync(User, bill, new ViewRequirement()))
{
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}");
return this.HttpNotFound();
}
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-08 01:35:10 +01:00
Include(e=>e.Client).Include(e=>e.Client.DeviceDeclarations)
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();
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
{
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-08 01:35:10 +01:00
var regids = estimate.Client.DeviceDeclarations.Select(d => d.DeviceId).ToArray();
bool gcmSent = false;
if (regids.Length>0) {
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);
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
{
return new ChallengeResult();
}
2017-06-08 00:26:29 +02:00
var filename = FileSystemHelpers.SignFileNameFormat("pro",billingCode,id);
2018-03-26 19:27:29 +02:00
FileInfo fi = new FileInfo(Path.Combine(AbstractFileSystemHelpers.UserBillsDirName, filename));
if (!fi.Exists) return HttpNotFound(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)
{
var uid = User.GetUserId();
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 );
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
{
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);
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
{
return new ChallengeResult();
}
2017-06-08 00:26:29 +02:00
var filename = FileSystemHelpers.SignFileNameFormat("pro",billingCode,id);
2018-03-26 19:27:29 +02:00
FileInfo fi = new FileInfo(Path.Combine(AbstractFileSystemHelpers.UserBillsDirName, filename));
if (!fi.Exists) return HttpNotFound(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
}