eBiss 3

Hilfe & Dokumentation

User Tools

Site Tools


en:transformation:mappings:funktionen:custom:validategln

Code samples: ValidateGLN, ValidateBarcode, Abs

using System;
using eBiss.Api;
 
namespace Map
{
 
    /// <summary>
    /// Sample class containing mapping function(s)
    /// 
    /// This class must inherit from eBiss.Api.IMapFunctionExtender and should use logging
    /// via eBiss.Api.ILoggingObject
    /// </summary>
    public class fun : IMapFunctionExtender, ILoggingObject
    {
 
        /// <summary>
        /// Validate GLN checks if a given value has 13 digits and is a valid Barcode number
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>//TRUE/FALSE
        /// <remarks>//Usage: Map.fun.ValidateGLN(41235746687)</remarks>
        [eBiss.Api.MappingFunction]
        public Boolean ValidateGLN(string value)
        {
            bool check;
            check = ((value.Length == 13) & (isValidBarcodeWithCheckDigit(value))); //check if value is a valid Barcode and has 13 digits
 
            //if (value.Length != 13) //check if it has the required length
            //    //throw new ApplicationException("GLNs must have 13 digits!");
            //    check = false;
 
            Log.Info("ValidateGLN('{0}') returns {1}", value, check);
            if (!check)
                throw new ApplicationException("GLN('" + value + "') is " + (check ? "valid" : "invalid"));
 
            return check;
        }
 
 
        /// <summary>
        /// Validate Barcode
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>//TRUE/FALSE
        /// <remarks>//Usage: Map.fun.ValidateBarCode(41235746687)</remarks>
        [eBiss.Api.MappingFunction]
        public Boolean ValidateBarcode(string value)
        {
            Log.Info("ValidateBarcode('{0}') returns {1}", value, isValidBarcodeWithCheckDigit(value));
 
            return isValidBarcodeWithCheckDigit(value);
        }
 
        /// <summary>
        /// Absolute Value
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        /// <remarks>//Usage: Map.fun.Abs(-1)</remarks>
        [eBiss.Api.MappingFunction]
        public NullableNumeric Abs(NullableNumeric value)
        {
            Log.Info("Abs(" + value + ")");
            return Math.Abs(value);
        }
 
 
        private eBiss.Api.ILogContext logContext;
        public eBiss.Api.ILogContext Log
        {
            get { return logContext;  }
            set { logContext = value; }
        }
 
 
        private static char GetBarcodeChecksumWithLegacyCode(string barcodeWithoutCheckSum)
        {
            if (barcodeWithoutCheckSum.Length > 6)
            {
                int a = 0;
                int b = 0;
                int j = barcodeWithoutCheckSum.Length - 1;
                int i = j;
                while (i >= 0)
                {
                    a = a + barcodeWithoutCheckSum[i] - 48;
                    i = i - 2;
                }
 
                j = barcodeWithoutCheckSum.Length - 2;
                i = j;
                while (i >= 0)
                {
                    b = b + barcodeWithoutCheckSum[i] - 48;
                    i = i - 2;
                }
                a = 3 * a + b;
                b = a % 10;
                if (b != 0) b = 10 - b;
 
                var ch = (char)(48 + b);
                return ch;
            }
            return ' ';
        }
 
        public static string GetBarcodeChecksum(string barcode)
        {
            int oddTotal;
            int oddTotalTripled;
            int evenTotal;
            // Which positions are odd or even depend on the length of the barcode, 
            // or more specifically, whether its length is odd or even, so:
            if (isStringOfEvenLen(barcode))
            {
                oddTotal = sumInsideOrdinals(barcode);
                oddTotalTripled = oddTotal * 3;
                evenTotal = sumOutsideOrdinals(barcode);
            }
            else
            {
                oddTotal = sumOutsideOrdinals(barcode);
                oddTotalTripled = oddTotal * 3;
                evenTotal = sumInsideOrdinals(barcode);
            }
            int finalTotal = oddTotalTripled + evenTotal;
            int modVal = finalTotal % 10;
            int czechSum = 10 - modVal;
            if (czechSum == 10)
            {
                return "0";
            }
            return czechSum.ToString();
        }
 
        private static bool isStringOfEvenLen(string barcode)
        {
            return (barcode.Length % 2 == 0);
        }
 
        // "EvenOrdinals" instead of "EvenVals" because values at index 0,2,4,etc. are seen by the 
        // checkdigitmeisters as First, Third, Fifth, ... (etc.), not Zeroeth, Second, Fourth
        private static int sumInsideOrdinals(string barcode)
        {
            int cumulativeVal = 0;
            for (int i = barcode.Length - 1; i > -1; i--)
            {
                if (i % 2 != 0)
                {
                    cumulativeVal += Convert.ToInt16(barcode[i] - '0');
                }
            }
            return cumulativeVal;
        }
 
        // "OddOrdinals" instead of "OddVals" because values at index 1,3,5,etc. are seen by the 
        // checkdigitmeisters as Second, Fourth, Sixth, ..., not First, Third, Fifth, ...
        private static int sumOutsideOrdinals(string barcode)
        {
            int cumulativeVal = 0;
            for (int i = barcode.Length - 1; i > -1; i--)
            {
                if (i % 2 == 0)
                {
                    cumulativeVal += Convert.ToInt16(barcode[i] - '0');
                }
            }
            return cumulativeVal;
        }
 
 
 
        private static bool isValidBarcodeWithCheckDigit(string barcodeWithCheckDigit)
        {
            string barcodeSansCheckDigit = barcodeWithCheckDigit.Substring(0, barcodeWithCheckDigit.Length - 1);
            string czechDigit = barcodeWithCheckDigit.Substring(barcodeWithCheckDigit.Length - 1, 1);
            //MessageBox.Show(string.Format("raw barcode portion is {0}", barcodeSansCheckDigit));
            //MessageBox.Show(string.Format("czech portion is {0}", czechDigit));
            return GetBarcodeChecksum(barcodeSansCheckDigit) == czechDigit;
        }
 
 
    }
}
en/transformation/mappings/funktionen/custom/validategln.txt · Last modified: 2024/02/20 08:15 by 127.0.0.1