refactorise la génération de la facture au format Pdf, presente la facture aquittée.

This commit is contained in:
2017-07-08 03:23:21 +02:00
parent 4268b6d7af
commit 6bab04c143
9 changed files with 41 additions and 30 deletions

View File

@ -19,5 +19,9 @@ namespace Yavsc.Billing
DateTime? ValidationDate { get; } DateTime? ValidationDate { get; }
bool GetIsAcquitted ();
string GetFileBaseName ();
} }
} }

View File

@ -64,7 +64,8 @@ namespace Yavsc.ApiControllers
return new ChallengeResult(); return new ChallengeResult();
} }
var fi = BillingHelpers.GetBillInfo(billingCode,id); var fi = bill.GetBillInfo();
if (!fi.Exists) return Ok(new { Error = "Not generated" }); if (!fi.Exists) return Ok(new { Error = "Not generated" });
return File(fi.OpenRead(), "application/x-pdf", fi.Name); return File(fi.OpenRead(), "application/x-pdf", fi.Name);
} }
@ -85,19 +86,20 @@ namespace Yavsc.ApiControllers
return new ChallengeResult(); return new ChallengeResult();
} }
Response.ContentType = "text/x-tex"; Response.ContentType = "text/x-tex";
return ViewComponent("Bill",new object[] {  billingCode, bill , OutputFormat.LaTeX, true, false }); return ViewComponent("Bill",new object[] {  billingCode, bill , OutputFormat.LaTeX, true });
} }
[HttpPost("genpdf/{billingCode}/{id}")] [HttpPost("genpdf/{billingCode}/{id}")]
public async Task<IActionResult> GeneratePdf(string billingCode, long id) public async Task<IActionResult> GeneratePdf(string billingCode, long id)
{ {
var bill = await billingService.GetBillAsync(billingCode, id); var bill = await billingService.GetBillAsync(billingCode, id);
if (bill==null) { if (bill==null) {
logger.LogCritical ( $"# not found !! {id} in {billingCode}"); logger.LogCritical ( $"# not found !! {id} in {billingCode}");
return this.HttpNotFound(); return this.HttpNotFound();
} }
return ViewComponent("Bill",new object[] { billingCode, bill, OutputFormat.Pdf, true, false } ); logger.LogWarning("Got bill ack:"+bill.GetIsAcquitted().ToString());
return ViewComponent("Bill",new object[] { billingCode, bill, OutputFormat.Pdf, true } );
} }

View File

@ -20,10 +20,10 @@ namespace Yavsc.Helpers
return bill; return bill;
} }
public static FileInfo GetBillInfo(string billingcode, long id, bool acquitted = false) public static FileInfo GetBillInfo(this IBillable bill)
{ {
var suffix = acquitted ? "-ack":null; var suffix = bill.GetIsAcquitted() ? "-ack":null;
var filename = $"facture-{billingcode}-{id}{suffix}.pdf"; var filename = bill.GetFileBaseName()+".pdf";
return new FileInfo(Path.Combine(Startup.UserBillsDirName, filename)); return new FileInfo(Path.Combine(Startup.UserBillsDirName, filename));
} }
} }

View File

@ -78,6 +78,19 @@ namespace Yavsc.Models.Billing
public virtual Activity Context  { get; set ; } public virtual Activity Context  { get; set ; }
public abstract System.Collections.Generic.List<IBillItem> GetBillItems(); public abstract System.Collections.Generic.List<IBillItem> GetBillItems();
public bool GetIsAcquitted()
{
return Regularisation?.IsOk() ?? false;
}
public string GetFileBaseName()
{
string type = GetType().Name;
string ack = GetIsAcquitted() ? "-ack" : null;
return $"facture-{ActivityCode}-{type}-{Id}{ack}";
}
public string PaymentId { get; set; } public string PaymentId { get; set; }
[ForeignKey("PaymentId"), Display(Name = "Acquittement de la facture")] [ForeignKey("PaymentId"), Display(Name = "Acquittement de la facture")]

View File

@ -18,6 +18,7 @@ namespace Yavsc
/// </summary> /// </summary>
public static List<Type> ProfileTypes = new List<Type>(); public static List<Type> ProfileTypes = new List<Type>();
public static List<PropertyInfo> UserSettings = new List<PropertyInfo>(); public static List<PropertyInfo> UserSettings = new List<PropertyInfo>();
public static Dictionary<string,Func<ApplicationDbContext,long,IBillable>> Billing = public static Dictionary<string,Func<ApplicationDbContext,long,IBillable>> Billing =
new Dictionary<string,Func<ApplicationDbContext,long,IBillable>> (); new Dictionary<string,Func<ApplicationDbContext,long,IBillable>> ();
@ -73,15 +74,15 @@ mais n'implemente pas l'interface IQueryable<ISpecializationSettings>
Billing.Add("Brush", new Func<ApplicationDbContext,long,IBillable> Billing.Add("Brush", new Func<ApplicationDbContext,long,IBillable>
( ( db, id) => ( ( db, id) =>
{ {
var query = db.HairCutQueries.Include(q=>q.Prestation).Single(q=>q.Id == id) ; var query = db.HairCutQueries.Include(q=>q.Prestation).Include(q=>q.Regularisation).Single(q=>q.Id == id) ;
query.SelectedProfile = db.BrusherProfile.Single(b=>b.UserId == query.PerformerId); query.SelectedProfile = db.BrusherProfile.Single(b=>b.UserId == query.PerformerId);
return query; return query;
})) ; })) ;
Billing.Add("MBrush",new Func<ApplicationDbContext,long,IBillable> Billing.Add("MBrush",new Func<ApplicationDbContext,long,IBillable>
( (db, id) => db.HairMultiCutQueries.Single(q=>q.Id == id))); ( (db, id) => db.HairMultiCutQueries.Include(q=>q.Regularisation).Single(q=>q.Id == id)));
Billing.Add("Rdv", new Func<ApplicationDbContext,long,IBillable> Billing.Add("Rdv", new Func<ApplicationDbContext,long,IBillable>
( (db, id) => db.RdvQueries.Single(q=>q.Id == id))); ( (db, id) => db.RdvQueries.Include(q=>q.Regularisation).Single(q=>q.Id == id)));
} }
public static System.Reflection.Assembly OnYavscResourceResolve(object sender, ResolveEventArgs ev) public static System.Reflection.Assembly OnYavscResourceResolve(object sender, ResolveEventArgs ev)
{ {

View File

@ -27,14 +27,14 @@ namespace Yavsc.ViewComponents
logger = loggerFactory.CreateLogger<BillViewComponent>(); logger = loggerFactory.CreateLogger<BillViewComponent>();
} }
public async Task<IViewComponentResult> InvokeAsync(string code, IBillable billable, OutputFormat format, bool asBill, bool acquitted) public async Task<IViewComponentResult> InvokeAsync(string code, IBillable billable, OutputFormat format, bool asBill)
{ {
var di = new DirectoryInfo(Startup.SiteSetup.UserFiles.Bills); var di = new DirectoryInfo(Startup.SiteSetup.UserFiles.Bills);
var dia = new DirectoryInfo(Startup.SiteSetup.UserFiles.Avatars); var dia = new DirectoryInfo(Startup.SiteSetup.UserFiles.Avatars);
ViewBag.BillsDir = di.FullName; ViewBag.BillsDir = di.FullName;
ViewBag.AvatarsDir = dia.FullName; ViewBag.AvatarsDir = dia.FullName;
ViewBag.AsBill = asBill; // vrai pour une facture, sinon, c'est un devis ViewBag.AsBill = asBill; // vrai pour une facture, sinon, c'est un devis
ViewBag.Acquitted = acquitted; ViewBag.Acquitted = billable.GetIsAcquitted();
ViewBag.BillingCode = code; ViewBag.BillingCode = code;
var client = await dbContext.Users var client = await dbContext.Users
@ -81,9 +81,10 @@ namespace Yavsc.ViewComponents
Temp = Startup.Temp, Temp = Startup.Temp,
TeXSource = tex, TeXSource = tex,
DestDir = Startup.UserBillsDirName, DestDir = Startup.UserBillsDirName,
BaseFileName = $"facture-{code}-{billable.Id}" BaseFileName = billable.GetFileBaseName()
} ); } );
} }
ViewBag.BillFileInfo = billable.GetBillInfo();
return View("Default",billable); return View("Default",billable);
} }

View File

@ -1,8 +1,7 @@
@model IBillable @model IBillable
<div class="bill"> <div class="bill">
@{ var fi = BillingHelpers.GetBillInfo(ViewBag.BillingCode,Model.Id); } @if (ViewBag.BillFileInfo.Exists) {
@if (fi.Exists) {
<a class="btn btn-link" href="~/api/bill/facture-@(ViewBag.BillingCode)-@(Model.Id).pdf" >La facture au format Pdf</a> <a class="btn btn-link" href="~/api/bill/facture-@(ViewBag.BillingCode)-@(Model.Id).pdf" >La facture au format Pdf</a>
} else { } else {
<form action="~/api/bill/genpdf/@ViewBag.BillingCode/@Model.Id" method="POST"> <form action="~/api/bill/genpdf/@ViewBag.BillingCode/@Model.Id" method="POST">

View File

@ -15,12 +15,14 @@
<text> : <text> :
</text> </text>
<label> <label>@Model.GetIsAcquitted()
<input type="checkbox" checked="@Model.Regularisation.IsOk()" disabled/> <input type="checkbox" checked="@Model.GetIsAcquitted()" disabled/>
<a asp-controller="Manage" asp-action="PaymentInfo" asp-route-id="@Model.Regularisation.CreationToken">@Model.Regularisation.CreationToken</a> <a asp-controller="Manage" asp-action="PaymentInfo" asp-route-id="@Model.Regularisation.CreationToken">@Model.Regularisation.CreationToken</a>
</label> </label>
} else { }
@if (!Model.GetIsAcquitted()) {
<div id="paypalzone"></div> <div id="paypalzone"></div>
<script> <script>
var CREATE_PAYMENT_URL = '@ViewBag.CreatePaymentUrl'; var CREATE_PAYMENT_URL = '@ViewBag.CreatePaymentUrl';

View File

@ -1,15 +1,4 @@
@model HairCutQuery @model HairCutQuery
@{
var paid = false;
if (Model.PaymentId!=null) {
if (Model.Regularisation!=null) {
if (Model.Regularisation.IsOk()) {
paid=true;
}
}
}
}
<dl class="dl-horizontal"> <dl class="dl-horizontal">
<dt>@Html.DisplayNameFor(m=>m.Prestation) <dt>@Html.DisplayNameFor(m=>m.Prestation)
</dt> </dt>
@ -33,7 +22,7 @@
</dd> </dd>
<dt>@SR["La facture"] <dt>@SR["La facture"]
</dt> </dt>
<dd>@await Component.InvokeAsync("Bill", "Brush", Model, OutputFormat.Html, true, paid ) <dd>@await Component.InvokeAsync("Bill", "Brush", Model, OutputFormat.Html, true)
</dd> </dd>
<dt>@Html.DisplayNameFor(m=>m.Regularisation)</dt> <dt>@Html.DisplayNameFor(m=>m.Regularisation)</dt>
<dd> <dd>