This commit is contained in:
Paul Schneider 2017-06-08 00:26:29 +02:00
commit a120dae977
144 changed files with 10574 additions and 1527 deletions

View file

@ -19,8 +19,11 @@ namespace Yavsc.ApiControllers
using Models.Messaging;
using ViewModels.Auth;
[Route("api/pdfestimate"), Authorize]
public class PdfEstimateController : Controller
using Yavsc.ViewComponents;
using Newtonsoft.Json;
[Route("api/bill"), Authorize]
public class BillingController : Controller
{
ApplicationDbContext dbContext;
private IStringLocalizer _localizer;
@ -28,59 +31,64 @@ namespace Yavsc.ApiControllers
private IGoogleCloudMessageSender _GCMSender;
private IAuthorizationService authorizationService;
private ILogger logger;
public PdfEstimateController(
private ILogger logger;
private IBillingService billingService;
public BillingController(
IAuthorizationService authorizationService,
ILoggerFactory loggerFactory,
IStringLocalizer<Yavsc.Resources.YavscLocalisation> SR,
ApplicationDbContext context,
IOptions<GoogleAuthSettings> googleSettings,
IGoogleCloudMessageSender GCMSender
IGoogleCloudMessageSender GCMSender,
IBillingService billingService
)
{
_googleSettings=googleSettings.Value;
this.authorizationService = authorizationService;
dbContext = context;
logger = loggerFactory.CreateLogger<PdfEstimateController>();
logger = loggerFactory.CreateLogger<BillingController>();
this._localizer = SR;
_GCMSender=GCMSender;
this.billingService=billingService;
}
[HttpGet("get/{id}", Name = "Get"), Authorize]
public async Task<IActionResult> Get(long id)
{
var estimate = dbContext.Estimates.Include(
e=>e.Query
).FirstOrDefault(e=>e.Id == id);
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
[HttpGet("pdf/facture-{billingCode}-{id}.pdf"), Authorize]
public async Task<IActionResult> GetPdf(string billingCode, long id)
{
var bill = await billingService.GetBillAsync(billingCode, id);
if (!await authorizationService.AuthorizeAsync(User, bill, new ViewRequirement()))
{
return new ChallengeResult();
}
var filename = $"estimate-{id}.pdf";
var filename = $"facture-{billingCode}-{id}.pdf";
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); ;
}
[HttpGet("estimate-{id}.tex", Name = "GetTex"), Authorize]
public async Task<IActionResult> GetTex(long id)
[HttpGet("tex/{billingCode}-{id}.tex"), Authorize]
public async Task<IActionResult> GetTex(string billingCode, long id)
{
var estimate = dbContext.Estimates.Include(
e=>e.Query
).FirstOrDefault(e=>e.Id == id);
if (!await authorizationService.AuthorizeAsync(User, estimate, new ViewRequirement()))
var bill = await billingService.GetBillAsync(billingCode, id);
if (bill==null) return this.HttpNotFound();
logger.LogVerbose(JsonConvert.SerializeObject(bill));
if (!await authorizationService.AuthorizeAsync(User, bill, new ViewRequirement()))
{
return new ChallengeResult();
}
Response.ContentType = "text/x-tex";
return ViewComponent("Estimate",new object[] { id, "LaTeX" });
return ViewComponent("Bill",new object[] {  billingCode, bill , OutputFormat.LaTeX, true, false });
}
[HttpPost("gen/{id}")]
public async Task<IActionResult> GeneratePdf(long id)
[HttpPost("genpdf/{billingCode}/{id}")]
public async Task<IActionResult> GeneratePdf(string billingCode, long id)
{
var estimate = dbContext.Estimates.Include(
e=>e.Query
@ -89,12 +97,12 @@ namespace Yavsc.ApiControllers
{
return new ChallengeResult();
}
return ViewComponent("Estimate",new object[] { id, "Pdf" } );
return ViewComponent("Bill",new object[] { billingCode, id, OutputFormat.Pdf } );
}
[HttpPost("prosign/{id}")]
public async Task<IActionResult> ProSign(long id)
[HttpPost("prosign/{billingCode}/{id}")]
public async Task<IActionResult> ProSign(string billingCode, long id)
{
var estimate = dbContext.Estimates.
Include(e=>e.Client).Include(e=>e.Client.Devices)
@ -108,7 +116,7 @@ namespace Yavsc.ApiControllers
}
if (Request.Form.Files.Count!=1)
return new BadRequestResult();
User.ReceiveProSignature(id,Request.Form.Files[0],"pro");
User.ReceiveProSignature(billingCode,id,Request.Form.Files[0],"pro");
estimate.ProviderValidationDate = DateTime.Now;
dbContext.SaveChanges(User.GetUserId());
// Notify the client
@ -125,8 +133,8 @@ namespace Yavsc.ApiControllers
return Ok (new { ProviderValidationDate = estimate.ProviderValidationDate, GCMSent = gcmSent });
}
[HttpGet("prosign/{id}")]
public async Task<IActionResult> GetProSign(long id)
[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);
@ -135,14 +143,14 @@ namespace Yavsc.ApiControllers
return new ChallengeResult();
}
var filename = FileSystemHelpers.SignFileNameFormat("pro",id);
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); ;
}
[HttpPost("clisign/{id}")]
public async Task<IActionResult> CliSign(long id)
[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
@ -154,14 +162,14 @@ namespace Yavsc.ApiControllers
}
if (Request.Form.Files.Count!=1)
return new BadRequestResult();
User.ReceiveProSignature(id,Request.Form.Files[0],"cli");
User.ReceiveProSignature(billingCode,id,Request.Form.Files[0],"cli");
estimate.ClientValidationDate = DateTime.Now;
dbContext.SaveChanges(User.GetUserId());
return Ok (new { ClientValidationDate = estimate.ClientValidationDate });
}
[HttpGet("clisign/{id}")]
public async Task<IActionResult> GetCliSign(long id)
[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);
@ -170,7 +178,7 @@ namespace Yavsc.ApiControllers
return new ChallengeResult();
}
var filename = FileSystemHelpers.SignFileNameFormat("pro",id);
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); ;

View file

@ -39,7 +39,7 @@ namespace Yavsc.Controllers
var uid = User.GetUserId();
var now = DateTime.Now;
var result = _context.Commands.Include(c => c.Location).
var result = _context.RdvQueries.Include(c => c.Location).
Include(c => c.Client).Where(c => c.PerformerId == uid && c.Id < maxId && c.EventDate > now
&& c.ValidationDate == null).
Select(c => new RdvQueryProviderInfo
@ -71,7 +71,7 @@ namespace Yavsc.Controllers
}
var uid = User.GetUserId();
RdvQuery bookQuery = _context.Commands.Where(c => c.ClientId == uid || c.PerformerId == uid).Single(m => m.Id == id);
RdvQuery bookQuery = _context.RdvQueries.Where(c => c.ClientId == uid || c.PerformerId == uid).Single(m => m.Id == id);
if (bookQuery == null)
{
@ -133,7 +133,7 @@ namespace Yavsc.Controllers
ModelState.AddModelError("ClientId", "You must be the client at creating a book query");
return new BadRequestObjectResult(ModelState);
}
_context.Commands.Add(bookQuery);
_context.RdvQueries.Add(bookQuery);
try
{
_context.SaveChanges(User.GetUserId());
@ -162,7 +162,7 @@ namespace Yavsc.Controllers
return HttpBadRequest(ModelState);
}
var uid = User.GetUserId();
RdvQuery bookQuery = _context.Commands.Single(m => m.Id == id);
RdvQuery bookQuery = _context.RdvQueries.Single(m => m.Id == id);
if (bookQuery == null)
{
@ -170,7 +170,7 @@ namespace Yavsc.Controllers
}
if (bookQuery.ClientId != uid) return HttpNotFound();
_context.Commands.Remove(bookQuery);
_context.RdvQueries.Remove(bookQuery);
_context.SaveChanges(User.GetUserId());
return Ok(bookQuery);
@ -187,7 +187,7 @@ namespace Yavsc.Controllers
private bool BookQueryExists(long id)
{
return _context.Commands.Count(e => e.Id == id) > 0;
return _context.RdvQueries.Count(e => e.Id == id) > 0;
}
}
}