From cdc8d89447c4b2af539c6e5be62c8b007156b9c6 Mon Sep 17 00:00:00 2001 From: Niels Kooiman Date: Wed, 23 Feb 2022 16:14:58 +0100 Subject: [PATCH] Add some existing examples --- dotnet/helpers/StringHelper.cs | 120 ++++++++++++++++++++++ dotnet/uniconta/CreateCustomReport.cs | 60 +++++++++++ dotnet/uniconta/CreateInvoiceReportPdf.cs | 29 ++++++ 3 files changed, 209 insertions(+) create mode 100644 dotnet/helpers/StringHelper.cs create mode 100644 dotnet/uniconta/CreateCustomReport.cs create mode 100644 dotnet/uniconta/CreateInvoiceReportPdf.cs diff --git a/dotnet/helpers/StringHelper.cs b/dotnet/helpers/StringHelper.cs new file mode 100644 index 0000000..47bcb22 --- /dev/null +++ b/dotnet/helpers/StringHelper.cs @@ -0,0 +1,120 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; + +namespace Default +{ + public class StringHelpers + { + // generics + public static IEnumerable> Split(IEnumerable src, int size) + => src.Where((x, i) => i % size == 0).Select((x, i) => src.Skip(i * size).Take(size)); + + public static IEnumerable Select(params T[] args) => args; + public static List List(params T[] args) => args.ToList(); + + + // string specific + public static bool StrEquals(string a, string b) => string.Equals((a ?? "").Trim(), (b ?? "").Trim(), StringComparison.OrdinalIgnoreCase); + + public static bool FirstNonEmpty(params string[] args) => args.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x)); + + public static bool AnyEmpty(params string[] args) => args.Any(x => string.IsNullOrWhiteSpace(x)); + + public static string LimitString(string str, int length) => str?.Substring(0, Math.Min(str.Length, length)); + + + public static IEnumerable SplitSizes(string str, params int[] sizes) + { + var start = 0; + return sizes.Select(s => { + if (start + s > str.Length) return ""; + var result = str.Substring(start, s); + start += s; + return result; + }); + } + + public static string JoinUrl(string base, params string[] parts) + { + var partList = new List(); + partList.Add(base.TrimEnd('/')); + foreach (string part in parts) + { + if (!String.IsNullOrEmpty(part)) partList.Add(part); + } + return String.Join("/", partList); + } + + private static readonly Regex nonAsciiRegex = new Regex("[^ -~]+", RegexOptions.Compiled); + public static string ReplaceNonAsciiChars(string input) + { + if (string.IsNullOrWhiteSpace(input)) + return input; + return nonAsciiRegex.Replace(input, ""); + } + + public static string ShortenText(string caption, int length) + { + if (string.IsNullOrEmpty(caption) || length <= 0) + return ""; + + if (caption.Length <= length) + return caption; + + int count = length - 5; + int lastIndex = caption.LastIndexOfAny(new char[] { ' ', ',', '.', '-' }, length - 1, count); + if (lastIndex >= 0) + return caption.Remove(lastIndex) + " ..."; + else + return caption.Remove(length - 4) + " ..."; + } + + public static string ShortenTextSimple(string caption, int length) + { + if (string.IsNullOrEmpty(caption) || length <= 0) + return ""; + if (caption.Length <= length) + return caption; + + return caption.Remove(length - 3) + "..."; + } + + public static List SplitString(string str, int chunkSize) + { + if (string.IsNullOrWhiteSpace(str)) + str = ""; + + var chunks = new List(); ; + int stringLength = str.Length; + for (int i = 0; i < stringLength; i += chunkSize) + { + if (i + chunkSize > stringLength) chunkSize = stringLength - i; + chunks.Add(str.Substring(i, chunkSize)); + } + if (chunks.Count == 0) + chunks.Add(""); + + return chunks; + } + + public static string Right(string value, int length) => value.Substring(value.Length - length); + + public static string UppercaseFirst(string str) + { + if (string.IsNullOrEmpty(str)) + return str; + return char.ToUpper(str[0]) + str.Substring(1); + } + + public static bool HasString(string src, string item) + { + return src?.IndexOf(item ?? "", StringComparison.OrdinalIgnoreCase) >= 0; + } + + public static string Repeat(string value, int count) + { + return new StringBuilder(value.Length * count).Insert(0, value, count).ToString(); + } + } +} diff --git a/dotnet/uniconta/CreateCustomReport.cs b/dotnet/uniconta/CreateCustomReport.cs new file mode 100644 index 0000000..2a6320e --- /dev/null +++ b/dotnet/uniconta/CreateCustomReport.cs @@ -0,0 +1,60 @@ +public async Task CreateCustomOfferReport(CrudAPI api, GiftProject giftProject) +{ + var reportName = "Offerte 3"; + var report = await LoadReport(api, reportName); + + var order = ConvertProjectToOrder(giftProject); + + report.DataSource = new List() + { + new UnicontaMasterDetail() + { + Master = order, + Details = order.OfferLines.ToArray() + } + }; + + var stream = new MemoryStream(); + var pdfOptions = new PdfExportOptions(); + await report.ExportToPdfAsync(stream, pdfOptions); + stream.Position = 0; + var data = stream.ToArray(); + var fileName = "Offerte 123"; + var attachment = new UserDocsClient() + { + _Data = data, + _Text = fileName, + _DocumentType = FileextensionsTypes.PDF + }; + return attachment; +} + +private static async Task LoadReport(CrudAPI api, string reportName) +{ + var apiWrapper = new ApiWrapper(api); + + var reports = await apiWrapper.Filter() + .WhereEqual(x => x.Name, reportName) + .Run(); + var reportRow = reports.FirstOrDefault(); + if (reportRow == null) + throw new Exception($"Kon rapport '{reportName}' niet vinden!"); + // Load Layout bytes in repord + _ = await api.Read(reportRow); + var reportLayout = reportRow.Layout; + if (reportLayout == null) + throw new Exception($"Kon rapport '{reportName}' layout niet laden!"); + + XtraReport report; + try + { + report = ReportUtil.GetXtraReportFromLayout(reportLayout); + } + catch (Exception) + { + report = null; + } + if (report == null) + throw new Exception($"Kon rapport '{reportName}' niet laden! (error while loading layout)"); + return report; +} \ No newline at end of file diff --git a/dotnet/uniconta/CreateInvoiceReportPdf.cs b/dotnet/uniconta/CreateInvoiceReportPdf.cs new file mode 100644 index 0000000..d5d93b0 --- /dev/null +++ b/dotnet/uniconta/CreateInvoiceReportPdf.cs @@ -0,0 +1,29 @@ +public async Task CreateOfferReport(CrudAPI api, GiftProject giftProject) +{ + var order = ConvertProjectToOrder(giftProject); + + var invoiceParams = new InvoiceParameters() + { + Date = DateTime.Now, + InvoiceNumber = order.Name, + order = order, + lines = order.OfferLines, + ReturnPDF = true, + SendByOutlook = false, + SendXML = false, + DocumentType = CompanyLayoutType.Offer, + ShowInvoice = false, + Simulate = false + }; + var invoice = await invoiceParams.PostInvoice(new InvoiceAPI(api)); + var data = invoice.pdf; + + var fileName = "Offerte 123"; + var attachment = new UserDocsClient() + { + _Data = data, + _Text = fileName, + _DocumentType = FileextensionsTypes.PDF + }; + return attachment; +} \ No newline at end of file