Add some existing examples

This commit is contained in:
Niels Kooiman
2022-02-23 16:14:58 +01:00
parent b7973a6e52
commit cdc8d89447
3 changed files with 209 additions and 0 deletions

View File

@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace Default
{
public class StringHelpers
{
// generics
public static IEnumerable<IEnumerable<T>> Split<T>(IEnumerable<T> src, int size)
=> src.Where((x, i) => i % size == 0).Select((x, i) => src.Skip(i * size).Take(size));
public static IEnumerable<T> Select<T>(params T[] args) => args;
public static List<T> List<T>(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<string> 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<string>();
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<string> SplitString(string str, int chunkSize)
{
if (string.IsNullOrWhiteSpace(str))
str = "";
var chunks = new List<string>(); ;
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();
}
}
}

View File

@@ -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;
}

View File

@@ -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;
}