common/Logic/ProductionOrderCore/OrderData/ScanToSerialNr.cs
2026-04-23 17:50:07 +02:00

97 lines
2.8 KiB
C#

using System;
using System.Linq;
namespace Xylem.Common.Logic.ProductionOrderCore.OrderData
{
/// <summary>
/// Conversion of scanned number to serial number
/// </summary>
public static class ScanToSerialNr
{
public static Boolean ConvertScanToSerialNr(String scan ,out Int32 internalSerialNr,out String customerSr)
{
customerSr = "";
internalSerialNr = -1;
try
{
const String prefix = "8SEN20";
const Int32 length = 9;
if (scan.Contains(","))
{
customerSr = scan.Substring(0, scan.IndexOf(",", StringComparison.Ordinal));
}
if (scan.StartsWith(prefix))
{
scan = scan.Substring(prefix.Length, length);
if (!scan.EndsWith(","))
{
throw new ApplicationException("Could not parse serial number");
}
scan = scan.TrimEnd(',');
}
if (int.TryParse(scan, out var ret))
{
internalSerialNr = ret;
return true;
}
if (scan.Length == 11 && scan.StartsWith("10"))
{
customerSr = scan.Substring(2, 9);
return true;
}
if (scan.Count( a => a == ',') == 2)
{
customerSr = scan.Split(',')[1] + ',';// 0, scan.IndexOf(",", StringComparison.Ordinal));
}
if (customerSr.StartsWith(prefix))
{
customerSr = customerSr.Substring(prefix.Length, length);
if (!customerSr.EndsWith(","))
{
throw new ApplicationException("Could not parse serial number");
}
customerSr = customerSr.TrimEnd(',');
}
if (int.TryParse(customerSr, out var retCustom))
{
if (retCustom.ToString().Length == "18795584".Length)
{
internalSerialNr = retCustom;
}
return true;
}
if (scan.Replace(" ", "").Length == 12)
{
customerSr = scan.Replace(" ", "");
}
throw new ApplicationException("Could not parse serial number");
}
catch (Exception )
{
return false;
}
}
}
}