You are here

function barcode_ean_check_digit in Barcode 6.2

Same name and namespace in other branches
  1. 7.2 plugins/ean.inc \barcode_ean_check_digit()
1 call to barcode_ean_check_digit()
barcode_ean_barcode in plugins/ean.inc

File

plugins/ean.inc, line 20
Barcode plugin EAN-13: Used with consumer products internationally, 13 characters UPC-A: Used with consumer products in U.S., 12 characters ISBN: Used to mark books, 13 characters

Code

function barcode_ean_check_digit($barnumber) {
  $csum_total = 0;

  // The checksum working variable starts at zero
  // If the source message string is less than 12 characters long, we make
  // it 12 characters
  if (strlen($barnumber) <= 12) {
    $barnumber = str_pad($barnumber, 12, "0", STR_PAD_LEFT);
  }

  // Calculate the checksum value for the message
  for ($i = 0; $i < strlen($barnumber); $i++) {
    if ($i % 2 == 0) {
      $csum_total = $csum_total + intval($barnumber[$i]);
    }
    else {
      $csum_total = $csum_total + 3 * intval($barnumber[$i]);
    }
  }

  // Calculate the checksum digit
  if ($csum_total % 10 == 0) {
    $checksum_digit = '';
  }
  else {
    $checksum_digit = 10 - $csum_total % 10;
  }
  return $barnumber . $checksum_digit;
}