yavsc/Yavsc/ApiControllers/BillingController.cs

189 lines
7.7 KiB
C#

8 years ago
using System.IO;
using Microsoft.AspNet.Authorization;
using Microsoft.AspNet.Mvc;
8 years ago
using System.Web.Routing;
using System.Linq;
using Microsoft.Data.Entity;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Localization;
8 years ago
using Microsoft.Extensions.OptionsModel;
using System;
using System.Security.Claims;
namespace Yavsc.ApiControllers
{
using Models;
using Helpers;
using Services;
8 years ago
using Models.Messaging;
using ViewModels.Auth;
7 years ago
using Newtonsoft.Json;
7 years ago
using Yavsc.ViewModels;
7 years ago
[Route("api/bill"), Authorize]
public class BillingController : Controller
8 years ago
{
8 years ago
ApplicationDbContext dbContext;
private IStringLocalizer _localizer;
private GoogleAuthSettings _googleSettings;
private IGoogleCloudMessageSender _GCMSender;
private IAuthorizationService authorizationService;
8 years ago
7 years ago
private ILogger logger;
7 years ago
private IBillingService billingService;
8 years ago
7 years ago
public BillingController(
IAuthorizationService authorizationService,
ILoggerFactory loggerFactory,
8 years ago
IStringLocalizer<Yavsc.Resources.YavscLocalisation> SR,
8 years ago
ApplicationDbContext context,
IOptions<GoogleAuthSettings> googleSettings,
7 years ago
IGoogleCloudMessageSender GCMSender,
IBillingService billingService
8 years ago
)
8 years ago
{
8 years ago
_googleSettings=googleSettings.Value;
this.authorizationService = authorizationService;
8 years ago
dbContext = context;
7 years ago
logger = loggerFactory.CreateLogger<BillingController>();
8 years ago
this._localizer = SR;
8 years ago
_GCMSender=GCMSender;
7 years ago
this.billingService=billingService;
8 years ago
}
7 years ago
[HttpGet("facture-{billingCode}-{id}.pdf"), Authorize]
7 years ago
public async Task<IActionResult> GetPdf(string billingCode, long id)
{
var bill = await billingService.GetBillAsync(billingCode, id);
if (!await authorizationService.AuthorizeAsync(User, bill, new ViewRequirement()))
8 years ago
{
return new ChallengeResult();
}
7 years ago
var filename = $"facture-{billingCode}-{id}.pdf";
8 years ago
FileInfo fi = new FileInfo(Path.Combine(Startup.UserBillsDirName, filename));
if (!fi.Exists) return Ok(new { Error = "Not generated" });
return File(fi.OpenRead(), "application/x-pdf", filename); ;
}
7 years ago
[HttpGet("facture-{billingCode}-{id}.tex"), Authorize]
7 years ago
public async Task<IActionResult> GetTex(string billingCode, long id)
8 years ago
{
7 years ago
var bill = await billingService.GetBillAsync(billingCode, id);
7 years ago
if (bill==null) {
7 years ago
logger.LogCritical ( $"# not found !! {id} in {billingCode}");
7 years ago
return this.HttpNotFound();
}
7 years ago
logger.LogVerbose(JsonConvert.SerializeObject(bill));
if (!await authorizationService.AuthorizeAsync(User, bill, new ViewRequirement()))
{
return new ChallengeResult();
}
8 years ago
Response.ContentType = "text/x-tex";
7 years ago
return ViewComponent("Bill",new object[] {  billingCode, bill , OutputFormat.LaTeX, true, false });
8 years ago
}
7 years ago
[HttpPost("genpdf/{billingCode}/{id}")]
public async Task<IActionResult> GeneratePdf(string billingCode, long id)
8 years ago
{
7 years ago
var bill = await billingService.GetBillAsync(billingCode, id);
if (bill==null) {
logger.LogCritical ( $"# not found !! {id} in {billingCode}");
return this.HttpNotFound();
}
7 years ago
return ViewComponent("Bill",new object[] { billingCode, bill, OutputFormat.Pdf, true, false } );
8 years ago
}
7 years ago
[HttpPost("prosign/{billingCode}/{id}")]
public async Task<IActionResult> ProSign(string billingCode, long id)
{
8 years ago
var estimate = dbContext.Estimates.
Include(e=>e.Client).Include(e=>e.Client.Devices)
.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();
7 years ago
User.ReceiveProSignature(billingCode,id,Request.Form.Files[0],"pro");
estimate.ProviderValidationDate = DateTime.Now;
dbContext.SaveChanges(User.GetUserId());
// Notify the client
8 years ago
var locstr = _localizer["EstimationMessageToClient"];
var yaev = new EstimationEvent(dbContext,estimate,_localizer);
8 years ago
var regids = estimate.Client.Devices.Select(d => d.GCMRegistrationId).ToArray();
bool gcmSent = false;
if (regids.Length>0) {
var grep = await _GCMSender.NotifyEstimateAsync(_googleSettings,regids,yaev);
gcmSent = grep.success>0;
}
return Ok (new { ProviderValidationDate = estimate.ProviderValidationDate, GCMSent = gcmSent });
}
7 years ago
[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();
}
7 years ago
var filename = FileSystemHelpers.SignFileNameFormat("pro",billingCode,id);
FileInfo fi = new FileInfo(Path.Combine(Startup.UserBillsDirName, filename));
if (!fi.Exists) return HttpNotFound(new { Error = "Professional signature not found" });
return File(fi.OpenRead(), "application/x-pdf", filename); ;
}
7 years ago
[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
8 years ago
).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();
7 years ago
User.ReceiveProSignature(billingCode,id,Request.Form.Files[0],"cli");
estimate.ClientValidationDate = DateTime.Now;
dbContext.SaveChanges(User.GetUserId());
return Ok (new { ClientValidationDate = estimate.ClientValidationDate });
}
7 years ago
[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();
}
7 years ago
var filename = FileSystemHelpers.SignFileNameFormat("pro",billingCode,id);
FileInfo fi = new FileInfo(Path.Combine(Startup.UserBillsDirName, filename));
if (!fi.Exists) return HttpNotFound(new { Error = "Professional signature not found" });
return File(fi.OpenRead(), "application/x-pdf", filename); ;
}
8 years ago
}
}