# Jarvis Pizzeria: EAN / GTIN code validation in DMN Service In this blog we describe the implementation of an EAN / GTIN code validator in een Decision Model Service.

As a Pizzeria that wants to treat the customer like a king, we think it is very important that we can always serve the customer with our full range of pizzas.

For this it is necessary that we have a good stock management policy. Every pizza we make must be processed accurately in the system. To simplify this processing, we provide each pizza box with an EAN barcode that needs to be scanned for stock management.

In this blog we describe a Decision Model that can be used to validate the scanned / entered EAN codes.

The purpose of this blog is to demonstrate that it is fairly easy to implement such an algorithm in a Decision Model.

# What is EAN / GTIN?

From wikipedia: EAN also known as European Article Number or EAN) is a standard describing a barcode symbology and numbering system used in global trade to identify a specific retail product type, in a specific packaging configuration, from a specific manufacturer. The standard has been subsumed in the Global Trade Item Number standard from the GS1 organization; the same numbers can be referred to as GTINs and can be encoded in other barcode symbologies defined by GS1.

The most commonly used EAN standard is the thirteen-digit EAN-13.

And from the https://www.gs1.org/ website.

The last digit of all fixed-length, numeric GS1 Identification Keys is a check digit that ensures the integrity of the key. The check digit is calculated using a simple algorithm, based on the other numbers in the key.

The image below shows the algorithm in brief (with 13-digit code) The following table gives an example to illustrate how a TIN-13 Check Digit is calculated # The OIC Implementation

Now we have come to the point of putting things into practice. How can this algorithm be implemented in an OIC Decision Model?

The step-by-step explanation below provides the answer.

First I will explain the Multiplier function that is needed per digit. Next, the MultiplierList function is discussed that is needed to be able to use the Multiplier function for all digit. To sum up this list, we have the MultiplierSum function created. Once we have determined the sum of all digits, the CheckSum can be calculated. For this we have defined the CheckSum expression of the same name. Finally we have the IsValid expression to determine if the EAN is valid.

## Multiplier function

The focus of this function is on the red block in the image below For a number at a certain position, the multiplication factor is determined (even positions = 1 and odd positions = 3) and the multiplication is performed. The return value is the calculated sum. In the “if” condition it is determined whether the position is odd or even, and the value of the digit is returned multiplied depending on this.

## MultiplierList function

This multiplier function must be called for each digit in the EAN code, the results must be stored in an array. For this we have created the MutiplierList function.

The various digits of the EAN code are processed in a for loop. With every loop the variable pos is filled with a value from the “in” array (this array extends to 18 because the implementation supports EAN13 as well as EAN18). The where clause ensures that the correct number of loops is executed. With EAN13 the highest permitted value for pos is 12. The multiplier function is therefore called 12 times in that case (with the pos value 1 … 12).
note: it is quite unusual to start an array at position 1 instead of at 0, but this is how it works.

The return clause contains the call to the multiplier function with the pos and digit parameters (pos is hereby explicitly converted from a string type into a number).

The result of this MutiplierList function is an array with all sums. ## MultiplierSum function

The next step that we can now take is to determine the total sum of all numbers in the result array of multiplierList. This is also easy to achieve with a function. By offering the array to the sum operation, all numbers in the array are added together.

## CheckSum expression

We are already starting to make good progress with the implementation of the algorithm. The next thing we have to do is determine the check digit according to the algorithm. Here we must determine in one way or another what the “nearest equal or higher multiple of ten” is. This can be done in the following steps:

• Divide the sum by 10. In the example: 57/10 = 5.7
• Round up the result. ceiling(5,7) = 6
• Then multiply this again by 10 = 60

Check Digit is this result – the sum = 60 – 57.

That is how we get the following expression in our Decision Model ## isValid expression

The last operation to be performed is a check whether the calculated checkSum corresponds to the last digit of the EAN code and a check on the length of the EAN code.

In our case we use EAN13 codes, so the length must be 13. ## Complete algorithme

For completeness, the complete algorithm as implemented in a Decision Model is shown here. We see here an EANCode function with within it the 3 functions and the 2 expressions that we have defined. The EAN code to be validated is the input.

## Test

But does the EANCode function work? The Decision Modeler offers an easy-to-use test functionality for this. For this test I added a context to the model to make the test result visible. In the “if” condition, the isValid expression of the EANCode function is called. This returns a boolean. In the “then” and “else” expression, the result is presented in a readable form.

### Test results

By clicking on the test ( ) button, the popup below appears. Enter an EAN13 code and then click on the “Start Test” button. With a valid EAN, a message will appear that will look something like the one below.

And with an invalid EAN In this blog we have shown that it is quite possible to implement a transparent representation of a Check Digit calculation algorithm as applied to EAN / GTIN codes.

note: also check Rick Keller’s blog for the latest Jarvis Pizzeria news.