Merge branch 'vnext' of https://github.com/pazof/yavsc.git
This commit is contained in:
commit
aa92e4a145
18 changed files with 787 additions and 473 deletions
27
.gitignore
vendored
27
.gitignore
vendored
|
|
@ -1,27 +0,0 @@
|
||||||
|
|
||||||
.gitignore
|
|
||||||
appsettings.*.json
|
|
||||||
.idea/
|
|
||||||
.vscode/
|
|
||||||
.vs/
|
|
||||||
YavscWeb.userprefs
|
|
||||||
bin/
|
|
||||||
obj/
|
|
||||||
bower_components/
|
|
||||||
node_modules/
|
|
||||||
docfx_project/
|
|
||||||
debugcmd
|
|
||||||
undefined/
|
|
||||||
debugcode/
|
|
||||||
kestrel*.pid
|
|
||||||
kestrel*.log
|
|
||||||
scaffold.cmds
|
|
||||||
DataProtection-Keys
|
|
||||||
RSA-Params.json
|
|
||||||
Components/
|
|
||||||
packages/
|
|
||||||
build/
|
|
||||||
*.user
|
|
||||||
*.bak
|
|
||||||
*~
|
|
||||||
|
|
||||||
92
Yavsc/ApiControllers/PdfEstimateController.cs
Normal file
92
Yavsc/ApiControllers/PdfEstimateController.cs
Normal file
|
|
@ -0,0 +1,92 @@
|
||||||
|
using System.IO;
|
||||||
|
using Microsoft.AspNet.Authorization;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Web.Routing;
|
||||||
|
using Microsoft.AspNet.Mvc.ViewComponents;
|
||||||
|
using Microsoft.AspNet.Razor;
|
||||||
|
|
||||||
|
namespace Yavsc.ApiControllers
|
||||||
|
{
|
||||||
|
using Models;
|
||||||
|
|
||||||
|
[Route("api/pdfestimate"), Authorize]
|
||||||
|
public class PdfEstimateController : Controller
|
||||||
|
{
|
||||||
|
ApplicationDbContext dbContext;
|
||||||
|
DefaultViewComponentHelper helper;
|
||||||
|
IViewComponentDescriptorCollectionProvider provider;
|
||||||
|
IViewComponentInvokerFactory factory;
|
||||||
|
RazorEngineHost host;
|
||||||
|
RazorTemplateEngine engine;
|
||||||
|
IViewComponentSelector selector;
|
||||||
|
|
||||||
|
|
||||||
|
public PdfEstimateController(
|
||||||
|
IViewComponentDescriptorCollectionProvider provider,
|
||||||
|
IViewComponentSelector selector,
|
||||||
|
IViewComponentInvokerFactory factory,
|
||||||
|
ApplicationDbContext context)
|
||||||
|
{
|
||||||
|
|
||||||
|
this.selector = selector;
|
||||||
|
this.provider = provider;
|
||||||
|
this.factory = factory;
|
||||||
|
helper = new DefaultViewComponentHelper(provider, selector, factory);
|
||||||
|
dbContext = context;
|
||||||
|
|
||||||
|
var language = new CSharpRazorCodeLanguage();
|
||||||
|
host = new RazorEngineHost(language)
|
||||||
|
{
|
||||||
|
DefaultBaseClass = "RazorPage",
|
||||||
|
DefaultClassName = "Estimate",
|
||||||
|
DefaultNamespace = "Yavsc",
|
||||||
|
};
|
||||||
|
|
||||||
|
// Everyone needs the System namespace, right?
|
||||||
|
host.NamespaceImports.Add("System");
|
||||||
|
engine = new RazorTemplateEngine(host);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
GeneratorResults razorResult =
|
||||||
|
engine.GenerateCode(
|
||||||
|
|
||||||
|
) */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[HttpGet("get/{id}", Name = "Get"), Authorize]
|
||||||
|
public IActionResult Get(long id)
|
||||||
|
{
|
||||||
|
var filename = $"estimate-{id}.pdf";
|
||||||
|
|
||||||
|
var cd = new System.Net.Mime.ContentDisposition
|
||||||
|
{
|
||||||
|
// for example foo.bak
|
||||||
|
FileName = filename,
|
||||||
|
|
||||||
|
// always prompt the user for downloading, set to true if you want
|
||||||
|
// the browser to try to show the file inline
|
||||||
|
Inline = false,
|
||||||
|
};
|
||||||
|
|
||||||
|
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 IActionResult GetTex(long id)
|
||||||
|
{
|
||||||
|
Response.ContentType = "text/x-tex";
|
||||||
|
return ViewComponent("Estimate",new object[] { id, false });
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("gen/{id}")]
|
||||||
|
public async Task<IActionResult> GeneratePdf(long id)
|
||||||
|
{
|
||||||
|
return ViewComponent("Estimate",new object[] { id, true } );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -11,7 +11,7 @@ using System;
|
||||||
|
|
||||||
namespace Yavsc.Controllers
|
namespace Yavsc.Controllers
|
||||||
{
|
{
|
||||||
[ServiceFilter(typeof(LanguageActionFilter)),
|
[ServiceFilter(typeof(LanguageActionFilter)),
|
||||||
Route("do")]
|
Route("do")]
|
||||||
public class FrontOfficeController : Controller
|
public class FrontOfficeController : Controller
|
||||||
{
|
{
|
||||||
|
|
@ -35,44 +35,45 @@ namespace Yavsc.Controllers
|
||||||
return View(latestPosts);
|
return View(latestPosts);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Route("Book/{id?}"),HttpGet]
|
[Route("Book/{id?}"), HttpGet]
|
||||||
|
|
||||||
public ActionResult Book(string id)
|
public ActionResult Book(string id)
|
||||||
{
|
{
|
||||||
if (id == null) {
|
if (id == null)
|
||||||
|
{
|
||||||
throw new NotImplementedException("No Activity code");
|
throw new NotImplementedException("No Activity code");
|
||||||
}
|
}
|
||||||
|
|
||||||
ViewBag.Activities = _context.ActivityItems(id);
|
ViewBag.Activities = _context.ActivityItems(id);
|
||||||
ViewBag.Activity = _context.Activities.FirstOrDefault(
|
ViewBag.Activity = _context.Activities.FirstOrDefault(
|
||||||
a => a.Code == id );
|
a => a.Code == id);
|
||||||
|
|
||||||
return View(
|
return View(
|
||||||
_context.Performers.Include(p=>p.Performer).Where
|
_context.Performers.Include(p => p.Performer).Where
|
||||||
(p => p.ActivityCode == id && p.Active).OrderBy(
|
(p => p.ActivityCode == id && p.Active).OrderBy(
|
||||||
x=>x.MinDailyCost
|
x => x.MinDailyCost
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Route("Book/{id}"),HttpPost]
|
[Route("Book/{id}"), HttpPost]
|
||||||
|
|
||||||
public ActionResult Book(BookQuery bookQuery)
|
public ActionResult Book(BookQuery bookQuery)
|
||||||
{
|
{
|
||||||
if (ModelState.IsValid) {
|
if (ModelState.IsValid)
|
||||||
|
{
|
||||||
var pro = _context.Performers.Include(
|
var pro = _context.Performers.Include(
|
||||||
pr => pr.Performer
|
pr => pr.Performer
|
||||||
).FirstOrDefault(
|
).FirstOrDefault(
|
||||||
x=>x.PerformerId == bookQuery.PerformerId
|
x => x.PerformerId == bookQuery.PerformerId
|
||||||
);
|
);
|
||||||
if (pro==null)
|
if (pro == null)
|
||||||
return HttpNotFound();
|
return HttpNotFound();
|
||||||
// Let's create a command
|
// Let's create a command
|
||||||
if (bookQuery.Id==0)
|
if (bookQuery.Id == 0)
|
||||||
{
|
{
|
||||||
_context.BookQueries.Add(bookQuery);
|
_context.BookQueries.Add(bookQuery);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
_context.BookQueries.Update(bookQuery);
|
_context.BookQueries.Update(bookQuery);
|
||||||
}
|
}
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
|
|
@ -81,36 +82,39 @@ namespace Yavsc.Controllers
|
||||||
return View("Index");
|
return View("Index");
|
||||||
}
|
}
|
||||||
ViewBag.Activities = _context.ActivityItems(null);
|
ViewBag.Activities = _context.ActivityItems(null);
|
||||||
return View( _context.Performers.Include(p=>p.Performer).Where
|
return View(_context.Performers.Include(p => p.Performer).Where
|
||||||
(p => p.Active).OrderBy(
|
(p => p.Active).OrderBy(
|
||||||
x=>x.MinDailyCost
|
x => x.MinDailyCost
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Produces("text/x-tex"), Authorize, Route("estimate-{id}.tex")]
|
[Produces("text/x-tex"), Authorize, Route("estimate-{id}.tex")]
|
||||||
public ViewResult EstimateTex(long id)
|
public ViewResult EstimateTex(long id)
|
||||||
{
|
{
|
||||||
var estimate = _context.Estimates.Include(x=>x.Query)
|
var estimate = _context.Estimates.Include(x => x.Query)
|
||||||
.Include(x=>x.Query.Client)
|
.Include(x => x.Query.Client)
|
||||||
.Include(x=>x.Query.PerformerProfile)
|
.Include(x => x.Query.PerformerProfile)
|
||||||
.Include(x=>x.Query.PerformerProfile.OrganizationAddress)
|
.Include(x => x.Query.PerformerProfile.OrganizationAddress)
|
||||||
.Include(x=>x.Query.PerformerProfile.Performer)
|
.Include(x => x.Query.PerformerProfile.Performer)
|
||||||
.Include(e=>e.Bill).FirstOrDefault(x=>x.Id==id);
|
.Include(e => e.Bill).FirstOrDefault(x => x.Id == id);
|
||||||
Response.ContentType = "text/x-tex";
|
Response.ContentType = "text/x-tex";
|
||||||
return View("Estimate.tex", estimate);
|
return View("Estimate.tex", estimate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Authorize,Route("Estimate-{id}.pdf")]
|
||||||
[Produces("application/x-pdf"), Authorize, Route("estimate-{id}.pdf")]
|
public IActionResult EstimatePdf(long id)
|
||||||
public ViewResult EstimatePdf(long id)
|
|
||||||
{
|
{
|
||||||
var estimate = _context.Estimates.Include(x=>x.Query)
|
ViewBag.TempDir = Startup.SiteSetup.TempDir;
|
||||||
.Include(x=>x.Query.Client)
|
ViewBag.BillsDir = Startup.UserBillsDirName;
|
||||||
.Include(x=>x.Query.PerformerProfile)
|
var estimate = _context.Estimates.Include(x => x.Query)
|
||||||
.Include(x=>x.Query.PerformerProfile.OrganizationAddress)
|
.Include(x => x.Query.Client)
|
||||||
.Include(x=>x.Query.PerformerProfile.Performer)
|
.Include(x => x.Query.PerformerProfile)
|
||||||
.Include(e=>e.Bill).FirstOrDefault(x=>x.Id==id);
|
.Include(x => x.Query.PerformerProfile.OrganizationAddress)
|
||||||
return View("Estimate.pdf", estimate);
|
.Include(x => x.Query.PerformerProfile.Performer)
|
||||||
|
.Include(e => e.Bill).FirstOrDefault(x => x.Id == id);
|
||||||
|
if (estimate==null)
|
||||||
|
throw new Exception("No data");
|
||||||
|
return View("Estimate.pdf",estimate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,7 +68,6 @@ namespace Yavsc.Helpers
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Transforms a string of Markdown into HTML.
|
/// Transforms a string of Markdown into HTML.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="helper">HtmlHelper - Not used, but required to make this an extension method.</param>
|
|
||||||
/// <param name="text">The Markdown that should be transformed.</param>
|
/// <param name="text">The Markdown that should be transformed.</param>
|
||||||
/// <param name="urlBaseLocation">The url Base Location.</param>
|
/// <param name="urlBaseLocation">The url Base Location.</param>
|
||||||
/// <returns>The HTML representation of the supplied Markdown.</returns>
|
/// <returns>The HTML representation of the supplied Markdown.</returns>
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,68 @@
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.AspNet.Http;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||||
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
|
using Microsoft.AspNet.Mvc.ViewEngines;
|
||||||
|
using Microsoft.AspNet.Mvc.ViewFeatures;
|
||||||
|
|
||||||
namespace Yavsc.Helpers
|
namespace Yavsc.Helpers
|
||||||
{
|
{
|
||||||
|
public class TeXString {
|
||||||
|
|
||||||
|
public class Replacement {
|
||||||
|
string target;
|
||||||
|
string replacement;
|
||||||
|
public Replacement(string target, string replacement){
|
||||||
|
this.target=target;
|
||||||
|
this.replacement=replacement;
|
||||||
|
}
|
||||||
|
public string Execute(string source)
|
||||||
|
{
|
||||||
|
return source.Replace(target,replacement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly static Replacement[] SpecialCharsToCommands =
|
||||||
|
{
|
||||||
|
new Replacement("<","\\textless"),
|
||||||
|
new Replacement(">","\\textgreater"),
|
||||||
|
new Replacement("©","\\copyright"),
|
||||||
|
new Replacement("®","\\textregistered"),
|
||||||
|
new Replacement("\\","\\textbackslash"),
|
||||||
|
new Replacement("™","\\texttrademark"),
|
||||||
|
new Replacement("¶","\\P"),
|
||||||
|
new Replacement("|","\\textbar"),
|
||||||
|
new Replacement("%","\\%"),
|
||||||
|
new Replacement("{","\\{"),
|
||||||
|
new Replacement("}","\\}"),
|
||||||
|
new Replacement("_","\\_"),
|
||||||
|
new Replacement("#","\\#"),
|
||||||
|
new Replacement("$","\\$"),
|
||||||
|
new Replacement("_","\\_"),
|
||||||
|
new Replacement("¿","\\textquestiondown"),
|
||||||
|
new Replacement("§","\\S"),
|
||||||
|
new Replacement("£","\\pounds"),
|
||||||
|
new Replacement("&","\\&"),
|
||||||
|
new Replacement("¡","\\textexclamdown"),
|
||||||
|
new Replacement("†","\\dag"),
|
||||||
|
new Replacement("–","\\textendash")
|
||||||
|
};
|
||||||
|
string data;
|
||||||
|
public TeXString(string str) {
|
||||||
|
data = str;
|
||||||
|
foreach (var r in SpecialCharsToCommands) {
|
||||||
|
data = r.Execute(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override public string ToString()
|
||||||
|
{
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
public static class TeXHelpers
|
public static class TeXHelpers
|
||||||
{
|
{
|
||||||
public static string NewLinesWith(this string target, string separator)
|
public static string NewLinesWith(this string target, string separator)
|
||||||
|
|
@ -11,5 +72,39 @@ namespace Yavsc.Helpers
|
||||||
|
|
||||||
return string.Join(separator, items);
|
return string.Join(separator, items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static TeXString ToTeX(string target, string lineSeparator="\n\\\\")
|
||||||
|
{
|
||||||
|
if (target==null) return null;
|
||||||
|
return new TeXString(target.NewLinesWith(lineSeparator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string RenderViewToString(
|
||||||
|
this Controller controller, IViewEngine engine,
|
||||||
|
IHttpContextAccessor httpContextAccessor,
|
||||||
|
string viewName, object model)
|
||||||
|
{
|
||||||
|
using (var sw = new StringWriter())
|
||||||
|
{
|
||||||
|
if (engine == null)
|
||||||
|
throw new InvalidOperationException("no engine");
|
||||||
|
|
||||||
|
// try to find the specified view
|
||||||
|
controller.TryValidateModel(model);
|
||||||
|
ViewEngineResult viewResult = engine.FindPartialView(controller.ActionContext, viewName);
|
||||||
|
// create the associated context
|
||||||
|
ViewContext viewContext = new ViewContext();
|
||||||
|
viewContext.ActionDescriptor = controller.ActionContext.ActionDescriptor;
|
||||||
|
viewContext.HttpContext = controller.ActionContext.HttpContext;
|
||||||
|
viewContext.TempData = controller.TempData;
|
||||||
|
viewContext.View = viewResult.View;
|
||||||
|
viewContext.Writer = sw;
|
||||||
|
|
||||||
|
// write the render view with the given context to the stringwriter
|
||||||
|
viewResult.View.RenderAsync(viewContext);
|
||||||
|
viewResult.EnsureSuccessful();
|
||||||
|
return sw.GetStringBuilder().ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -16,15 +16,32 @@ namespace Yavsc
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public string Authority { get; set; }
|
public string Authority { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Owner's email
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
public EmailEntry Owner { get; set; }
|
public EmailEntry Owner { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Administrator's email
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
public EmailEntry Admin { get; set; }
|
public EmailEntry Admin { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// User's files directory
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
public ThirdPartyFiles UserFiles { get; set; }
|
public ThirdPartyFiles UserFiles { get; set; }
|
||||||
|
|
||||||
public string BusinessName { get; set; }
|
public string BusinessName { get; set; }
|
||||||
public string Street { get; set; }
|
public string Street { get; set; }
|
||||||
public string PostalCode { get; set; }
|
public string PostalCode { get; set; }
|
||||||
public string CountryCode { get; set; }
|
public string CountryCode { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies the directory where should be
|
||||||
|
/// generated pdf files using pandoc
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The temporary directory to use</returns>
|
||||||
|
public string TempDir { get; set; } = "Temp";
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ namespace Yavsc
|
||||||
{
|
{
|
||||||
public static string UserFilesDirName { get; private set; }
|
public static string UserFilesDirName { get; private set; }
|
||||||
public static FileServerOptions UserFilesOptions { get; private set; }
|
public static FileServerOptions UserFilesOptions { get; private set; }
|
||||||
|
|
||||||
public void ConfigureFileServerApp(IApplicationBuilder app,
|
public void ConfigureFileServerApp(IApplicationBuilder app,
|
||||||
SiteSettings siteSettings, IHostingEnvironment env)
|
SiteSettings siteSettings, IHostingEnvironment env)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web.Optimization;
|
using System.Web.Optimization;
|
||||||
|
|
@ -32,8 +33,11 @@ namespace Yavsc
|
||||||
public partial class Startup
|
public partial class Startup
|
||||||
{
|
{
|
||||||
public static string ConnectionString { get; private set; }
|
public static string ConnectionString { get; private set; }
|
||||||
|
public static string UserBillsDirName { private set; get; }
|
||||||
public static string Authority { get; private set; }
|
public static string Authority { get; private set; }
|
||||||
public static string Audience { get; private set; }
|
public static string Audience { get; private set; }
|
||||||
|
public static SiteSettings SiteSetup { get; private set; }
|
||||||
|
|
||||||
private static ILogger logger;
|
private static ILogger logger;
|
||||||
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
|
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
|
||||||
{
|
{
|
||||||
|
|
@ -225,7 +229,18 @@ namespace Yavsc
|
||||||
RoleManager<IdentityRole> roleManager,
|
RoleManager<IdentityRole> roleManager,
|
||||||
ILoggerFactory loggerFactory)
|
ILoggerFactory loggerFactory)
|
||||||
{
|
{
|
||||||
|
SiteSetup = siteSettings.Value;
|
||||||
Startup.UserFilesDirName = siteSettings.Value.UserFiles.DirName;
|
Startup.UserFilesDirName = siteSettings.Value.UserFiles.DirName;
|
||||||
|
Startup.UserBillsDirName = siteSettings.Value.UserFiles.Bills;
|
||||||
|
|
||||||
|
// TODO implement an installation & upgrade procedure
|
||||||
|
// Create required directories
|
||||||
|
foreach (string dir in new string[] { Startup.UserFilesDirName, Startup.UserBillsDirName, SiteSetup.TempDir })
|
||||||
|
{
|
||||||
|
DirectoryInfo di = new DirectoryInfo(dir);
|
||||||
|
if (!di.Exists) di.Create();
|
||||||
|
}
|
||||||
|
|
||||||
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
|
||||||
loggerFactory.AddDebug();
|
loggerFactory.AddDebug();
|
||||||
logger = loggerFactory.CreateLogger<Startup>();
|
logger = loggerFactory.CreateLogger<Startup>();
|
||||||
|
|
|
||||||
58
Yavsc/ViewComponents/EstimateViewComponent.cs
Normal file
58
Yavsc/ViewComponents/EstimateViewComponent.cs
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.Data.Entity;
|
||||||
|
using Yavsc.Models;
|
||||||
|
using Yavsc.Models.Billing;
|
||||||
|
using Yavsc.ViewModels.Gen;
|
||||||
|
|
||||||
|
namespace Yavsc.ViewComponents
|
||||||
|
{
|
||||||
|
public class EstimateViewComponent : ViewComponent
|
||||||
|
{
|
||||||
|
ApplicationDbContext dbContext;
|
||||||
|
public EstimateViewComponent(ApplicationDbContext dbContext)
|
||||||
|
{
|
||||||
|
this.dbContext = dbContext;
|
||||||
|
}
|
||||||
|
public async Task<IViewComponentResult> InvokeAsync(long id, bool toPdf = false)
|
||||||
|
{
|
||||||
|
Estimate estimate =
|
||||||
|
dbContext.Estimates.Include(x => x.Query)
|
||||||
|
.Include(x => x.Query.Client)
|
||||||
|
.Include(x => x.Query.PerformerProfile)
|
||||||
|
.Include(x => x.Query.PerformerProfile.OrganizationAddress)
|
||||||
|
.Include(x => x.Query.PerformerProfile.Performer)
|
||||||
|
.Include(e => e.Bill).FirstOrDefault(x => x.Id == id);
|
||||||
|
if (estimate == null)
|
||||||
|
throw new Exception("No data");
|
||||||
|
if (toPdf)
|
||||||
|
{
|
||||||
|
string tex = null;
|
||||||
|
var oldWriter = ViewComponentContext.ViewContext.Writer;
|
||||||
|
|
||||||
|
using (var writer = new StringWriter())
|
||||||
|
{
|
||||||
|
this.ViewComponentContext.ViewContext.Writer = writer;
|
||||||
|
|
||||||
|
var resultTex = View("Estimate_tex", estimate);
|
||||||
|
resultTex.Execute(this.ViewComponentContext);
|
||||||
|
tex = writer.ToString();
|
||||||
|
|
||||||
|
}
|
||||||
|
ViewComponentContext.ViewContext.Writer = oldWriter;
|
||||||
|
|
||||||
|
return this.View("Estimate_pdf",
|
||||||
|
new PdfGenerationViewModel{
|
||||||
|
TeXSource = tex,
|
||||||
|
DestDir = Startup.UserBillsDirName,
|
||||||
|
BaseFileName = $"estimate-{id}"
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
return this.View("Estimate_tex", estimate);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
14
Yavsc/ViewModels/Gen/PdfGenerationViewModel.cs
Normal file
14
Yavsc/ViewModels/Gen/PdfGenerationViewModel.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
namespace Yavsc.ViewModels.Gen
|
||||||
|
{
|
||||||
|
public class PdfGenerationViewModel
|
||||||
|
{
|
||||||
|
[Required]
|
||||||
|
public string TeXSource { get; set; }
|
||||||
|
[Required]
|
||||||
|
public string BaseFileName { get; set; }
|
||||||
|
[Required]
|
||||||
|
public string DestDir { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -39,6 +39,12 @@
|
||||||
<a asp-action="Index">@SR["Back to List"]</a> |
|
<a asp-action="Index">@SR["Back to List"]</a> |
|
||||||
@{ var filenametex = $"estimate-{Model.Id}.tex" ;
|
@{ var filenametex = $"estimate-{Model.Id}.tex" ;
|
||||||
var filenamepdf = $"estimate-{Model.Id}.pdf" ;}
|
var filenamepdf = $"estimate-{Model.Id}.pdf" ;}
|
||||||
<a href="~/do/@filenametex" >Export au format LaTeX</a>
|
<a href="~/api/pdfestimate/estimate-@(Model.Id).tex" >Export au format LaTeX</a>
|
||||||
<a href="~/do/@filenamepdf" >Export au format LaTeX</a>
|
|
||||||
|
|
||||||
|
<form action="~/api/pdfestimate/gen/@Model.Id" method="POST">
|
||||||
|
<input type="submit" value="Générer le Pdf"/>
|
||||||
|
</form>
|
||||||
|
<a href="~/api/pdfestimate/get/@(Model.Id)" >Télécharger le document</a>
|
||||||
|
|
||||||
</p>
|
</p>
|
||||||
|
|
|
||||||
|
|
@ -1,55 +0,0 @@
|
||||||
|
|
||||||
@using System.Reflection
|
|
||||||
@using System.IO
|
|
||||||
@using Microsoft.Extensions.WebEncoders
|
|
||||||
@using System.Diagnostics
|
|
||||||
@using System.Text
|
|
||||||
@using Yavsc.Formatters
|
|
||||||
|
|
||||||
@model Estimate
|
|
||||||
@{
|
|
||||||
Layout = null;
|
|
||||||
ViewBag.Pdf = "";
|
|
||||||
ViewBag.TeX = "";
|
|
||||||
var writer = new System.IO.StringWriter();
|
|
||||||
var content = await Html.PartialAsync("Estimate.tex", Model );
|
|
||||||
content.WriteTo(writer, new TexEncoder());
|
|
||||||
var contentStr = writer.ToString();
|
|
||||||
string name = $"tmpestimtex-{Model.Id}";
|
|
||||||
string fullname = new FileInfo(
|
|
||||||
System.IO.Path.Combine(ViewBag.TempDir,name)).FullName;
|
|
||||||
|
|
||||||
FileInfo fi = new FileInfo(fullname + ".tex");
|
|
||||||
FileInfo fo = new FileInfo(fullname + ".pdf");
|
|
||||||
using (StreamWriter sw = new StreamWriter (fi.FullName))
|
|
||||||
{
|
|
||||||
sw.Write (contentStr);
|
|
||||||
}
|
|
||||||
if (!fi.Exists)
|
|
||||||
{
|
|
||||||
throw new Exception ("Source write failed");
|
|
||||||
}
|
|
||||||
using (Process p = new Process ()) {
|
|
||||||
p.StartInfo.WorkingDirectory = ViewBag.TempDir;
|
|
||||||
p.StartInfo = new ProcessStartInfo ();
|
|
||||||
p.StartInfo.UseShellExecute = false;
|
|
||||||
p.StartInfo.FileName = "/usr/bin/texi2pdf";
|
|
||||||
p.StartInfo.Arguments = $"--batch --build-dir=. -o {fo.FullName} {fi.FullName}";
|
|
||||||
p.Start ();
|
|
||||||
p.WaitForExit ();
|
|
||||||
if (p.ExitCode != 0) {
|
|
||||||
throw new Exception ("Pdf generation failed with exit code:" + p.ExitCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (fo.Exists) {
|
|
||||||
UTF8Encoding utf8 = new UTF8Encoding();
|
|
||||||
|
|
||||||
using (StreamReader sr = new StreamReader (fo.FullName)) {
|
|
||||||
byte[] buffer = File.ReadAllBytes (fo.FullName);
|
|
||||||
ViewBag.Pdf = utf8.GetString(buffer,0,buffer.Length);
|
|
||||||
}
|
|
||||||
fo.Delete();
|
|
||||||
}
|
|
||||||
fi.Delete();
|
|
||||||
}
|
|
||||||
@ViewBag.Pdf
|
|
||||||
50
Yavsc/Views/Shared/Components/Estimate/Estimate_pdf.cshtml
Normal file
50
Yavsc/Views/Shared/Components/Estimate/Estimate_pdf.cshtml
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
@using System.Reflection
|
||||||
|
@using System.IO
|
||||||
|
@using Microsoft.Extensions.WebEncoders
|
||||||
|
@using System.Diagnostics
|
||||||
|
@using System.Text
|
||||||
|
@using Yavsc.Formatters
|
||||||
|
@model Yavsc.ViewModels.Gen.PdfGenerationViewModel
|
||||||
|
@{
|
||||||
|
string errorMsg = null;
|
||||||
|
var billdir = Model.DestDir;
|
||||||
|
var tempdir = Startup.SiteSetup.TempDir;
|
||||||
|
string name = Model.BaseFileName;
|
||||||
|
string fullname = new FileInfo(
|
||||||
|
System.IO.Path.Combine(tempdir,name)).FullName;
|
||||||
|
string ofullname = new FileInfo(
|
||||||
|
System.IO.Path.Combine(billdir,name)).FullName;
|
||||||
|
|
||||||
|
FileInfo fi = new FileInfo(fullname + ".tex");
|
||||||
|
FileInfo fo = new FileInfo(ofullname + ".pdf");
|
||||||
|
using (StreamWriter sw = new StreamWriter (fi.FullName))
|
||||||
|
{
|
||||||
|
sw.Write (Model.TeXSource);
|
||||||
|
}
|
||||||
|
if (!fi.Exists)
|
||||||
|
{
|
||||||
|
errorMsg = "Source write failed";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
using (Process p = new Process ()) {
|
||||||
|
p.StartInfo.WorkingDirectory = tempdir;
|
||||||
|
p.StartInfo = new ProcessStartInfo ();
|
||||||
|
p.StartInfo.UseShellExecute = false;
|
||||||
|
p.StartInfo.FileName = "/usr/bin/texi2pdf";
|
||||||
|
p.StartInfo.Arguments = $"--batch --build-dir=. -o {fo.FullName} {fi.FullName}";
|
||||||
|
p.Start ();
|
||||||
|
p.WaitForExit ();
|
||||||
|
if (p.ExitCode != 0) {
|
||||||
|
errorMsg = $"Pdf generation failed with exit code: {p.ExitCode}";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fi.Delete();
|
||||||
|
}
|
||||||
|
ViewBag.GenSuccess = fo.Exists;
|
||||||
|
}
|
||||||
|
@if (ViewBag.GenSuccess) {
|
||||||
|
@($"{name}.pdf")
|
||||||
|
} else {
|
||||||
|
@errorMsg
|
||||||
|
<text>Something went wrong ...</text>
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
@model Estimate
|
|
||||||
@using Yavsc.Helpers
|
@using Yavsc.Helpers
|
||||||
|
@using System.Globalization
|
||||||
|
@model Estimate
|
||||||
@{
|
@{
|
||||||
Layout = null;
|
Layout = null;
|
||||||
var pro = Model.Query.PerformerProfile;
|
var pro = Model.Query.PerformerProfile;
|
||||||
|
|
@ -9,8 +10,8 @@
|
||||||
var proaddr = Model.Query?.PerformerProfile.OrganizationAddress.Address;
|
var proaddr = Model.Query?.PerformerProfile.OrganizationAddress.Address;
|
||||||
var proaddrn = (proaddr!=null) ? proaddr.NewLinesWith("\\\\\n") : null ;
|
var proaddrn = (proaddr!=null) ? proaddr.NewLinesWith("\\\\\n") : null ;
|
||||||
var proaddrm = (proaddr!=null) ? proaddr.NewLinesWith(" - ") : null ;
|
var proaddrm = (proaddr!=null) ? proaddr.NewLinesWith(" - ") : null ;
|
||||||
}
|
}\documentclass[french,11pt]{article}
|
||||||
\documentclass[french,11pt]{article}
|
\usepackage{eurosym}
|
||||||
\usepackage{babel}
|
\usepackage{babel}
|
||||||
\usepackage[T1]{fontenc}
|
\usepackage[T1]{fontenc}
|
||||||
\usepackage[utf8]{inputenc}
|
\usepackage[utf8]{inputenc}
|
||||||
|
|
@ -68,33 +69,29 @@
|
||||||
\def\FactureNum {@Model.Id.ToString()} % Numéro de facture
|
\def\FactureNum {@Model.Id.ToString()} % Numéro de facture
|
||||||
\def\FactureAcquittee {non} % Facture acquittée : oui/non
|
\def\FactureAcquittee {non} % Facture acquittée : oui/non
|
||||||
\def\FactureLieu {@proaddrn} % Lieu de l'édition de la facture
|
\def\FactureLieu {@proaddrn} % Lieu de l'édition de la facture
|
||||||
\def\FactureObjet {Facture : @Model.Title} % Objet du document
|
\def\FactureObjet {Facture : @TeXHelpers.ToTeX(Model.Title)} % Objet du document
|
||||||
% Description de la facture
|
% Description de la facture
|
||||||
\def\FactureDescr {
|
\def\FactureDescr {
|
||||||
@Model.Description
|
@TeXHelpers.ToTeX(Model.Description)
|
||||||
}
|
}
|
||||||
|
|
||||||
% Infos Client
|
% Infos Client
|
||||||
\def\ClientNom{@to.UserName} % Nom du client
|
\def\ClientNom{@TeXHelpers.ToTeX(to.UserName)} % Nom du client
|
||||||
|
|
||||||
\def\ClientAdresse{
|
\def\ClientAdresse{
|
||||||
% Adresse du client
|
% Adresse du client
|
||||||
@if (!string.IsNullOrWhiteSpace(PostalAddress)) {
|
@if (!string.IsNullOrWhiteSpace(PostalAddress)) {
|
||||||
<text> @PostalAddress\\</text> }
|
<text> @TeXHelpers.ToTeX(PostalAddress)\\</text> }
|
||||||
@if (!string.IsNullOrWhiteSpace(to.PhoneNumber)) {<text>
|
@if (!string.IsNullOrWhiteSpace(to.PhoneNumber)) {<text>
|
||||||
@to.PhoneNumber <text>\\</text>
|
@TeXHelpers.ToTeX(to.PhoneNumber)<text>\\</text>
|
||||||
</text>}
|
</text>}
|
||||||
E-mail: @to.Email
|
E-mail: @TeXHelpers.ToTeX(to.Email)
|
||||||
}
|
}
|
||||||
|
|
||||||
% Liste des produits facturés : Désignation, prix
|
% Liste des produits facturés : Désignation, prix
|
||||||
|
@if (Model.Bill!=null) { foreach (CommandLine line in Model.Bill) {
|
||||||
@if (Model.Bill!=null) {
|
<text>\AjouterService{@TeXHelpers.ToTeX(line.Description)}{@line.Count}{@line.UnitaryCost.ToString("F2",CultureInfo.InvariantCulture)}
|
||||||
foreach (CommandLine line in Model.Bill) {
|
</text>} }
|
||||||
<text>
|
|
||||||
\AjouterService {@line.Description} {@line.Count} {@line.UnitaryCost}
|
|
||||||
</text>
|
|
||||||
} }
|
|
||||||
|
|
||||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||||
|
|
||||||
|
|
@ -109,24 +106,24 @@
|
||||||
\setlength{\parindent}{0pt}
|
\setlength{\parindent}{0pt}
|
||||||
|
|
||||||
\renewcommand{\headrulewidth}{0pt}
|
\renewcommand{\headrulewidth}{0pt}
|
||||||
\cfoot{ @if (!string.IsNullOrWhiteSpace(from.UserName)) { @from.UserName }
|
\cfoot{ @TeXHelpers.ToTeX(from.UserName) @if (!string.IsNullOrWhiteSpace(proaddrm)) { <text> - @TeXHelpers.ToTeX(proaddrm) </text> } \newline
|
||||||
@if (!string.IsNullOrWhiteSpace(proaddrm)) { <text> - @proaddrm </text> } \newline
|
\small{ E-mail: @TeXHelpers.ToTeX(from.Email) @if (!string.IsNullOrWhiteSpace(from.PhoneNumber)) { <text> - Téléphone fixe: @TeXHelpers.ToTeX(from.PhoneNumber) </text> }
|
||||||
\small{ E-mail: @from.Email
|
|
||||||
@if (!string.IsNullOrWhiteSpace(from.PhoneNumber)) { <text> - Téléphone fixe: @from.PhoneNumber </text> }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
\begin{document}
|
\begin{document}
|
||||||
|
|
||||||
% Logo de la société
|
% Logo de la société
|
||||||
@if (from.Avatar != null) {<text>
|
@if (from.Avatar != null) {
|
||||||
\includegraphics{@from.Avatar}</text>
|
<text>\includegraphics{@from.Avatar}
|
||||||
} else { <text>
|
</text>
|
||||||
%\includegraphics{logo.png}</text>
|
} else {
|
||||||
|
<text>%\includegraphics{logo.png}
|
||||||
|
</text>
|
||||||
}
|
}
|
||||||
% Nom et adresse de la société
|
% Nom et adresse de la société
|
||||||
@from.UserName \\
|
@TeXHelpers.ToTeX(from.UserName) \\
|
||||||
@proaddrn
|
@TeXHelpers.ToTeX(proaddrn)
|
||||||
|
|
||||||
Facture n°\FactureNum
|
Facture n°\FactureNum
|
||||||
|
|
||||||
|
|
@ -151,7 +148,7 @@ Facture n°\FactureNum
|
||||||
|
|
||||||
\begin{center}
|
\begin{center}
|
||||||
\begin{tabular}{lrrr}
|
\begin{tabular}{lrrr}
|
||||||
\textbf{Désignation ~~~~~~} & \textbf{Prix unitaire} & \textbf{Quantité} & \textbf{Montant (EUR)} \\
|
\textbf{Désignation ~~~~~~} & \textbf{Prix unitaire} & \textbf{Quantité} & \textbf{Montant (\euro)} \\
|
||||||
\hline
|
\hline
|
||||||
\AfficheResultat{}
|
\AfficheResultat{}
|
||||||
\end{tabular}
|
\end{tabular}
|
||||||
9
Yavsc/estimate-3.t2d/pdf/bak/estimate-3.aux
Normal file
9
Yavsc/estimate-3.t2d/pdf/bak/estimate-3.aux
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
\relax
|
||||||
|
\catcode `:\active
|
||||||
|
\catcode `;\active
|
||||||
|
\catcode `!\active
|
||||||
|
\catcode `?\active
|
||||||
|
\select@language{french}
|
||||||
|
\@writefile{toc}{\select@language{french}}
|
||||||
|
\@writefile{lof}{\select@language{french}}
|
||||||
|
\@writefile{lot}{\select@language{french}}
|
||||||
|
|
@ -140,5 +140,6 @@
|
||||||
"postrestore": "echo after restoring packages",
|
"postrestore": "echo after restoring packages",
|
||||||
"prepublish": "gulp min",
|
"prepublish": "gulp min",
|
||||||
"postpublish": "echo \" . ./contrib/postPublish.sh # to push in prod.\""
|
"postpublish": "echo \" . ./contrib/postPublish.sh # to push in prod.\""
|
||||||
}
|
},
|
||||||
|
"embed": "Views/**/*.cshtml"
|
||||||
}
|
}
|
||||||
9
Yavsc/tmpestimtex-3.t2d/pdf/bak/tmpestimtex-3.aux
Normal file
9
Yavsc/tmpestimtex-3.t2d/pdf/bak/tmpestimtex-3.aux
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
\relax
|
||||||
|
\catcode `:\active
|
||||||
|
\catcode `;\active
|
||||||
|
\catcode `!\active
|
||||||
|
\catcode `?\active
|
||||||
|
\select@language{french}
|
||||||
|
\@writefile{toc}{\select@language{french}}
|
||||||
|
\@writefile{lof}{\select@language{french}}
|
||||||
|
\@writefile{lot}{\select@language{french}}
|
||||||
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue