You are here

function barcode_check_digit in Barcode 6.2

Same name and namespace in other branches
  1. 7.2 includes/barcode.plugins.inc \barcode_check_digit()
4 calls to barcode_check_digit()
barcode_ean8_barcode in plugins/ean8.inc
barcode_i25_encode in plugins/i25.inc
barcode_s2o5_encode in plugins/s2o5.inc
barcode_upce_barcode in plugins/upce.inc

File

includes/barcode.plugins.inc, line 98
Code to load barcode symbology plugins

Code

function barcode_check_digit($barnumber, $number) {
  $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) < $number) {
    $barnumber = str_pad($barnumber, $number, "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 + 3 * intval($barnumber[$i]);
    }
    else {
      $csum_total = $csum_total + 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;
}