Blame view

site/jpgraph/Examples/barcode_usps_example.php 2.15 KB
8ec98c9f   Guillaume   MAJ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  <?php
  // =======================================================
  // Example of how to format US Postal shipping information
  // =======================================================
  require_once ('jpgraph/jpgraph_barcode.php');
  
  // The Full barcode standard is described in
  // http://www.usps.com/cpim/ftp/pubs/pub91/91c4.html#508hdr1
  //
  // The data start with AI=420 which means
  // "Ship to/Deliver To Postal Code (within single authority)
  //
  class USPS_Confirmation {
      function __construct() {
      }
  
      // Private utility function
      function _USPS_chkd($aData) {
  	$n = strlen($aData);
  
  	// Add all even numbers starting from position 1 from the end
  	$et = 0 ;
  	for( $i=1; $i <= $n; $i+=2 ) {
  	    $d = intval(substr($aData,-$i,1));
  	    $et += $d;
  	}
  
  	// Add all odd numbers starting from position 2 from the end
  	$ot = 0 ;
  	for( $i=2; $i <= $n; $i+=2 ) {
  	    $d = intval(substr($aData,-$i,1));
  	    $ot += $d;
  	}
  	$tot = 3*$et + $ot;
  	$chkdigit = (10 - ($tot % 10))%10;;
  	return $chkdigit;
      }
  
      // Get type 1 of confirmation code (with ZIP)
      function GetPICwithZIP($aZIP,$aServiceType,$aDUNS,$aSeqNbr) {
  	// Convert to USPS format with AI=420 and extension starting with AI=91
  	$data = '420'. $aZIP . '91' . $aServiceType . $aDUNS . $aSeqNbr;
  	// Only calculate the checkdigit from the AI=91 and forward
  	// and do not include the ~1 (FUNC1) in the calculation
  	$cd = $this->_USPS_chkd(substr($data,8));
  	$data = '420'. $aZIP . '~191' . $aServiceType . $aDUNS . $aSeqNbr;
  	return $data . $cd;
      }
  
      // Get type 2 of confirmation code (without ZIP)
      function GetPIC($aServiceType,$aDUNS,$aSeqNbr) {
  	// Convert to USPS format with AI=91
  	$data = '91' . $aServiceType . $aDUNS . $aSeqNbr;
  	$cd = $this->_USPS_chkd($data);
  	return $data . $cd;
      }
  
  }
  
  $usps = new USPS_Confirmation();
  $zip     = '92663';
  $service = '21';
  $DUNS    = '805213907';
  $seqnr   = '04508735';
  $data = $usps->GetPICwithZIP($zip,$service,$DUNS,$seqnr);
  //$data = $usps->GetPIC('01','123456789','00000001');
  
  $encoder = BarcodeFactory::Create(ENCODING_EAN128);
  $e = BackendFactory::Create(BACKEND_IMAGE,$encoder);
  $e->SetModuleWidth(2);
  $e->SetFont(FF_ARIAL,FS_NORMAL,14);
  $e->Stroke($data);
  
  ?>