Skip to content

Commit

Permalink
more elegant profile checks
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanstapel committed Jan 9, 2025
1 parent 12d58a8 commit 9f6a5dd
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 155 deletions.
Binary file added ZUGFeRD.PDF.Test/output.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion ZUGFeRD.Render.Demo/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal class Application
internal async Task RunAsync()
{
InvoiceDescriptor desc = InvoiceDescriptor.Load("../../../../demodata/zugferd22/zugferd_2p2_EXTENDED_Fremdwaehrung-factur-x.xml");
string html = InvoiceDescriptorHtmlRenderer.render(desc);
string html = InvoiceDescriptorHtmlRenderer.Render(desc);
System.IO.File.WriteAllText("output.html", html);
} // !RunAsync()
}
Expand Down
111 changes: 19 additions & 92 deletions ZUGFeRD.Render/InvoiceDescriptorHtmlRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
using RazorEngine;
using RazorEngine.Templating;
using RazorLight;
using s2industries.ZUGFeRD;
using System;
using System.Collections.Generic;
Expand All @@ -28,118 +27,46 @@
using System.Security.Permissions;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace s2industries.ZUGFeRD.Render
{
public class InvoiceDescriptorHtmlRenderer
{
public static string render(InvoiceDescriptor desc)
public static string Render(InvoiceDescriptor desc)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = typeof(InvoiceDescriptorHtmlRenderer).Namespace + ".test.cshtml";

string template = "";
string templateText = "";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
template = reader.ReadToEnd();
templateText = reader.ReadToEnd();
}

var engine = new RazorLightEngineBuilder()
.UseOptions(new RazorLightOptions()
{
EnableDebugMode = true
})
.SetOperatingAssembly(typeof(InvoiceDescriptorHtmlRenderer).GetTypeInfo().Assembly)
.Build();
Task<string> resultTask = engine.CompileRenderAsync("test", desc);
resultTask.Wait();

ITemplateKey key = new FullPathTemplateKey("Invoice-Template", "Invoice-Template", ResolveType.Global, null);
Engine.Razor.AddTemplate(key,
new LoadedTemplateSource(template));

string result = Engine.Razor.RunCompile(key, typeof(InvoiceDescriptor), desc);
return resultTask.Result;
} // !Render()




/*
MemoryStream ms = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteDocType("html", "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", null);
writer.WriteStartElement("html");
writer.WriteStartElement("head");
writer.WriteRaw("<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css\" integrity=\"sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7\" crossorigin=\"anonymous\">");
writer.WriteEndElement(); // !head
writer.WriteStartElement("body");
// Absender
writer.WriteRaw("<div class=\"row\">");
writer.WriteRaw("<div class=\"col-md-3\">");
writer.WriteRaw("<table class=\"table table-bordered\">");
writer.WriteStartElement("tr");
writer.WriteStartElement("td");
writer.WriteRaw("<small>Rechnungsersteller</small><br />");
_writeParty(writer, desc.Seller);
writer.WriteEndElement(); // !td
writer.WriteEndElement(); // !tr
writer.WriteStartElement("tr");
writer.WriteStartElement("td");
writer.WriteRaw("<small>Rechnungsempfänger</small><br />");
_writeParty(writer, desc.Buyer);
writer.WriteEndElement(); // !td
writer.WriteEndElement(); // !tr
writer.WriteRaw("</table>");
writer.WriteRaw("</div>");
writer.WriteRaw("</div>");
// Empfänger
writer.WriteRaw("<div class=\"row\">");
writer.WriteRaw("<div class=\"col-md-3\">");
writer.WriteRaw("</div>");
writer.WriteRaw("</div>");
// Kopfdaten
writer.WriteRaw("<div class=\"row\">");
writer.WriteRaw("<div class=\"col-md-6\">");
writer.WriteRaw(String.Format("Rechnungsnummer: {0}", desc.InvoiceNo));
writer.WriteElementString("br", "");
writer.WriteRaw(String.Format("Rechnungsdatum: {0}", desc.InvoiceDate));
writer.WriteRaw("</div>");
writer.WriteRaw("</div>");
writer.WriteEndElement(); // !body
writer.WriteEndElement(); // !html
writer.WriteEndDocument();
writer.Flush();
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string retval = sr.ReadToEnd();
return retval;
*/
return result;
} // !render()


public static void render(InvoiceDescriptor desc, string filename)
public static void Render(InvoiceDescriptor desc, string filename)
{
string output = render(desc);
string output = Render(desc);
StreamWriter writer = File.CreateText(filename);
writer.WriteLine(output);
writer.Close();
} // !render()


private static void _writeParty(XmlTextWriter writer, Party p)
{
writer.WriteRaw(p.Name);
writer.WriteElementString("br", "");
writer.WriteRaw(p.Street);
writer.WriteElementString("br", "");
writer.WriteRaw(String.Format("{0} {1} {2}", p.Country.EnumToString(), p.Postcode, p.City));
writer.WriteElementString("br", "");
writer.WriteRaw(String.Format("ID: ", p.ID));
writer.WriteElementString("br", "");
writer.WriteRaw(String.Format("GlobalID: ", p.GlobalID.ID));
} // !_writeParty()
} // !Render()
}
}
57 changes: 0 additions & 57 deletions ZUGFeRD.Render/RazorTemplateManager.cs

This file was deleted.

72 changes: 71 additions & 1 deletion ZUGFeRD.Render/test.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<div class="col-6">
<div class="card">
<div class="card-body">
<h5 class="card-title">Buyer</h5>
<h5 class="card-title">Buyer</h5>
@Model.Buyer?.Name<br />
@Model.Buyer?.Street<br />
@Model.Buyer?.Postcode @Model.Buyer?.City<br />
Expand All @@ -31,6 +31,76 @@
</div>
</div>
</div>

<div class="row">
<div class="col-12">
<table class="table">
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Price</th>
</tr>
</thead>
<tbody>
@foreach (s2industries.ZUGFeRD.TradeLineItem item in Model.TradeLineItems)
{
<tr>
<td>@item.Name
@if (!String.IsNullOrWhiteSpace(item.Description))
{
<br />
<small>@item.Description</small>
}
</td>
<td>
@item.BilledQuantity

@if (item.UnitQuantity.HasValue)
{
<br />
<small>Unit quantity: @item.UnitQuantity.Value</small>
}
</td>
<td>
@if (item.NetUnitPrice.HasValue)
{
@Html.Raw(item.NetUnitPrice.Value.ToString("N2"))
}
</td>
<td>@(item.BilledQuantity * item.NetUnitPrice.Value).ToString("N2")</td>
</tr>
}
</tbody>
</table>
</div>
</div>

<div class="row">
<div class="offset-8 col-4">
<dl>
<dt>Line Total</dt>
<dd>@Model.LineTotalAmount.Value.ToString("N2")</dd>
<dt>Tax Total</dt>
<dd>@Model.TaxTotalAmount.Value.ToString("N2")</dd>
<dt>Grand Total</dt>
<dd>@Model.GrandTotalAmount.Value.ToString("N2")</dd>

@if (Model.TotalPrepaidAmount.HasValue)
{
<dt>Prepaid Amount</dt>
<dd>@Model.TotalPrepaidAmount.Value.ToString("N2")</dd>
}

@if (Model.DuePayableAmount.HasValue)
{
<dt>Due Payable</dt>
<dd>@Model.DuePayableAmount.Value.ToString("N2")</dd>
}
</dl>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</body>
Expand Down
24 changes: 22 additions & 2 deletions ZUGFeRD.Test/ZUGFeRD22Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2300,14 +2300,18 @@ public void TestWriteAndReadDespatchAdviceDocumentReferenceXRechnung()


[TestMethod]
public void TestSpecifiedTradeAllowanceCharge()
[DataRow(Profile.Basic)]
[DataRow(Profile.Comfort)]
[DataRow(Profile.Extended)]
[DataRow(Profile.XRechnung)]
public void TestSpecifiedTradeAllowanceCharge(Profile profile)
{
InvoiceDescriptor invoice = _InvoiceProvider.CreateInvoice();

invoice.TradeLineItems[0].AddSpecifiedTradeAllowanceCharge(true, CurrencyCodes.EUR, 198m, 19.8m, 10m, "Discount 10%");

MemoryStream ms = new MemoryStream();
invoice.Save(ms, ZUGFeRDVersion.Version23, Profile.Basic);
invoice.Save(ms, ZUGFeRDVersion.Version23, profile);
ms.Position = 0;

InvoiceDescriptor loadedInvoice = InvoiceDescriptor.Load(ms);
Expand All @@ -2323,6 +2327,22 @@ public void TestSpecifiedTradeAllowanceCharge()
} // !SpecifiedTradeAllowanceCharge()


[TestMethod]
public void TestSpecifiedTradeAllowanceChargeNotWrittenInMinimum()
{
InvoiceDescriptor invoice = _InvoiceProvider.CreateInvoice();

invoice.TradeLineItems[0].AddSpecifiedTradeAllowanceCharge(true, CurrencyCodes.EUR, 198m, 19.8m, 10m, "Discount 10%");

MemoryStream ms = new MemoryStream();
invoice.Save(ms, ZUGFeRDVersion.Version23, Profile.Minimum);
ms.Position = 0;

InvoiceDescriptor loadedInvoice = InvoiceDescriptor.Load(ms);
Assert.AreEqual(0, loadedInvoice.TradeLineItems[0].GetSpecifiedTradeAllowanceCharges().Count);
} // !TestSpecifiedTradeAllowanceChargeNotWrittenInMinimum()


[TestMethod]
public void TestSellerDescription()
{
Expand Down
4 changes: 2 additions & 2 deletions ZUGFeRD/InvoiceDescriptor23CIIWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public override void Save(InvoiceDescriptor descriptor, Stream stream, ZUGFeRDFo
#region SpecifiedLineTradeAgreement (Basic, Comfort, Extended, XRechnung)
//Eine Gruppe von betriebswirtschaftlichen Begriffen, die Informationen über den Preis für die in der betreffenden Rechnungsposition in Rechnung gestellten Waren und Dienstleistungen enthält

if (new Profile[] { Profile.Basic, Profile.Comfort, Profile.Extended, Profile.XRechnung, Profile.XRechnung1 }.Contains(descriptor.Profile))
if (descriptor.Profile.In(Profile.Basic, Profile.Comfort, Profile.Extended, Profile.XRechnung, Profile.XRechnung1))
{
Writer.WriteStartElement("ram", "SpecifiedLineTradeAgreement", Profile.Basic | Profile.Comfort | Profile.Extended | Profile.XRechnung1 | Profile.XRechnung);

Expand Down Expand Up @@ -492,7 +492,7 @@ public override void Save(InvoiceDescriptor descriptor, Stream stream, ZUGFeRDFo

#region SpecifiedTradeAllowanceCharge (Basic, Comfort, Extended, XRechnung)
//Abschläge auf Ebene der Rechnungsposition (Basic, Comfort, Extended, XRechnung)
if (new Profile[] { Profile.Basic, Profile.Comfort, Profile.Extended, Profile.XRechnung1, Profile.XRechnung }.Contains(descriptor.Profile))
if (descriptor.Profile.In(Profile.Basic, Profile.Comfort, Profile.Extended, Profile.XRechnung1, Profile.XRechnung))
{
if (tradeLineItem.GetSpecifiedTradeAllowanceCharges().Count > 0)
{
Expand Down

0 comments on commit 9f6a5dd

Please sign in to comment.