New Company Setup 'Barcodes On Stock' implemented. Rep303 shows barcodes if set.
[fa-stable.git] / inventory / manage / items.php
index 8ed29d2cb0bdaf7f484d86a3c7d8618f3c4fde71..06f27ea75fc98849f3e78626b70f3a14bf2de473 100644 (file)
@@ -316,8 +316,18 @@ function item_settings(&$stock_id, $new_item)
        //------------------------------------------------------------------------------------
        if ($new_item) 
        {
-               text_row(_("Item Code:"), 'NewStockID', null, 21, 20);
-
+               $tmpCodeID=null;
+               $post_label = null;
+               if (!empty($SysPrefs->prefs['barcodes_on_stock']))
+               {
+                       $post_label = '<button class="ajaxsubmit" type="submit" aspect=\'default\'  name="generateBarcode"  id="generateBarcode" value="Generate Barcode EAN8"> '._("Generate EAN-8 Barcode").' </button>';
+                       if (isset($_POST['generateBarcode']))
+                       {
+                               $tmpCodeID=generateBarcode();
+                               $_POST['NewStockID'] = $tmpCodeID;
+                       }
+               }       
+               text_row(_("Item Code:"), 'NewStockID', $tmpCodeID, 21, 20, null, "", $post_label);
                $_POST['inactive'] = 0;
        } 
        else 
@@ -652,3 +662,41 @@ end_form();
 //------------------------------------------------------------------------------------
 
 end_page();
+
+function generateBarcode() {
+       $tmpBarcodeID = "";
+       $tmpCountTrys = 0;
+       while ($tmpBarcodeID == "")     {
+               srand ((double) microtime( )*1000000);
+               $random_1  = rand(1,9);
+               $random_2  = rand(0,9);
+               $random_3  = rand(0,9);
+               $random_4  = rand(0,9);
+               $random_5  = rand(0,9);
+               $random_6  = rand(0,9);
+               $random_7  = rand(0,9);
+               //$random_8  = rand(0,9);
+
+                       // http://stackoverflow.com/questions/1136642/ean-8-how-to-calculate-checksum-digit
+               $sum1 = $random_2 + $random_4 + $random_6; 
+               $sum2 = 3 * ($random_1  + $random_3  + $random_5  + $random_7 );
+               $checksum_value = $sum1 + $sum2;
+
+               $checksum_digit = 10 - ($checksum_value % 10);
+               if ($checksum_digit == 10) 
+                       $checksum_digit = 0;
+
+               $random_8  = $checksum_digit;
+
+               $tmpBarcodeID = $random_1 . $random_2 . $random_3 . $random_4 . $random_5 . $random_6 . $random_7 . $random_8;
+
+               // LETS CHECK TO SEE IF THIS NUMBER HAS EVER BEEN USED
+               $query = "SELECT stock_id FROM ".TB_PREF."stock_master WHERE stock_id='" . $tmpBarcodeID . "'";
+               $arr_stock = db_fetch(db_query($query));
+  
+               if (  !$arr_stock['stock_id'] ) {
+                       return $tmpBarcodeID;
+               }
+               $tmpBarcodeID = "";      
+       }
+}