From: Joe Hunt Date: Wed, 22 Nov 2017 23:12:27 +0000 (+0100) Subject: New Company Setup 'Barcodes On Stock' implemented. Rep303 shows barcodes if set. X-Git-Tag: v2.4.4~55 X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=commitdiff_plain;ds=sidebyside;h=0388050f6d4c6c7c9a9db06d2f31aa8879da69a8;p=fa-stable.git New Company Setup 'Barcodes On Stock' implemented. Rep303 shows barcodes if set. --- diff --git a/admin/company_preferences.php b/admin/company_preferences.php index 34af247c..b23c92c7 100644 --- a/admin/company_preferences.php +++ b/admin/company_preferences.php @@ -114,8 +114,8 @@ if (isset($_POST['update']) && $_POST['update'] != "") 'use_dimension', 'curr_default', 'f_year', 'shortname_name_in_list', 'no_item_list' => 0, 'no_customer_list' => 0, 'no_supplier_list' =>0, 'base_sales', - 'time_zone' => 0, 'company_logo_report' => 0, 'add_pct', 'round_to', 'login_tout', 'auto_curr_reval', - 'bcc_email', 'alternative_tax_include_on_docs', 'suppress_tax_rates', + 'time_zone' => 0, 'company_logo_report' => 0, 'barcodes_on_stock' => 0, 'add_pct', 'round_to', 'login_tout', + 'auto_curr_reval', 'bcc_email', 'alternative_tax_include_on_docs', 'suppress_tax_rates', 'use_manufacturing', 'use_fixed_assets')) ); @@ -161,6 +161,12 @@ if (!isset($myrow["company_logo_report"])) $myrow["company_logo_report"] = get_company_pref("company_logo_report"); } $_POST['company_logo_report'] = $myrow["company_logo_report"]; +if (!isset($myrow["barcodes_on_stock"])) +{ + set_company_pref("barcodes_on_stock", "setup.company", "tinyint", 1, '0'); + $myrow["barcodes_on_stock"] = get_company_pref("barcodes_on_stock"); +} +$_POST['barcodes_on_stock'] = $myrow["barcodes_on_stock"]; $_POST['version_id'] = $myrow["version_id"]; $_POST['add_pct'] = $myrow['add_pct']; $_POST['login_tout'] = $myrow['login_tout']; @@ -201,6 +207,7 @@ check_row(_("Delete Company Logo:"), 'del_coy_logo', $_POST['del_coy_logo']); check_row(_("Automatic Revaluation Currency Accounts"), 'auto_curr_reval', $_POST['auto_curr_reval']); check_row(_("Time Zone on Reports"), 'time_zone', $_POST['time_zone']); check_row(_("Company Logo on Reports"), 'company_logo_report', $_POST['company_logo_report']); +check_row(_("Use Barcodes on Stocks"), 'barcodes_on_stock', $_POST['barcodes_on_stock']); label_row(_("Database Scheme Version"), $_POST['version_id']); table_section(2); diff --git a/inventory/manage/items.php b/inventory/manage/items.php index 8ed29d2c..06f27ea7 100644 --- a/inventory/manage/items.php +++ b/inventory/manage/items.php @@ -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 = ''; + 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 = ""; + } +} diff --git a/reporting/includes/barcodes.php b/reporting/includes/barcodes.php index 03ee468e..bfe7f4a8 100644 --- a/reporting/includes/barcodes.php +++ b/reporting/includes/barcodes.php @@ -79,7 +79,7 @@ class TCPDFBarcode { *
  • $arrcode["bcode"][$k]["h"] bar height in units.
  • *
  • $arrcode["bcode"][$k]["p"] bar top position (0 = top, 1 = middle)
  • * @param string $code code to print - * @param string $type type of barcode: + * @param string $type type of barcode: */ function __construct($code, $type) { $this->setBarcode($code, $type); @@ -96,7 +96,7 @@ class TCPDFBarcode { /** * Set the barcode. * @param string $code code to print - * @param string $type type of barcode: + * @param string $type type of barcode: * @return array */ function setBarcode($code, $type) { @@ -133,12 +133,28 @@ class TCPDFBarcode { $arrcode = $this->barcode_c128($code, "C"); break; } - case "EAN13": { // EAN 13 - $arrcode = $this->barcode_ean13($code, 13); + case 'EAN2': { // 2-Digits UPC-Based Extension + $arrcode = $this->barcode_eanext($code, 2); break; } - case "UPCA": { // UPC-A - $arrcode = $this->barcode_ean13($code, 12); + case 'EAN5': { // 5-Digits UPC-Based Extension + $arrcode = $this->barcode_eanext($code, 5); + break; + } + case 'EAN8': { // EAN 8 + $arrcode = $this->barcode_eanupc($code, 8); + break; + } + case 'EAN13': { // EAN 13 + $arrcode = $this->barcode_eanupc($code, 13); + break; + } + case 'UPCA': { // UPC-A + $arrcode = $this->barcode_eanupc($code, 12); + break; + } + case 'UPCE': { // UPC-E + $arrcode = $this->barcode_eanupc($code, 6); break; } case "POSTNET": { // POSTNET @@ -570,49 +586,84 @@ class TCPDFBarcode { } return $bararray; } - + /** * EAN13 and UPC-A barcodes. - * @param string $code code to represent. - * @param string $len barcode type: 13 = EAN13, 12 = UPC-A + * EAN13: European Article Numbering international retail product code + * UPC-A: Universal product code seen on almost all retail products in the USA and Canada + * UPC-E: Short version of UPC symbol + * @param $code (string) code to represent. + * @param $len (string) barcode type: 6 = UPC-E, 8 = EAN8, 13 = EAN13, 12 = UPC-A * @return array barcode representation. - * @access protected + * @protected */ - function barcode_ean13($code, $len=13) { + protected function barcode_eanupc($code, $len=13) { + $upce = false; + if ($len == 6) { + $len = 12; // UPC-A + $upce = true; // UPC-E mode + } + $data_len = $len - 1; //Padding - $code = str_pad($code, $len-1, '0', STR_PAD_LEFT); - if($len == 12) { - $code = '0'.$code; + $code = str_pad($code, $data_len, '0', STR_PAD_LEFT); + $code_len = strlen($code); + // calculate check digit + $sum_a = 0; + for ($i = 1; $i < $data_len; $i+=2) { + $sum_a += $code{$i}; } - // add check digit - if(strlen($code) == 12) { - $sum=0; - for($i=1;$i<=11;$i+=2) { - $sum += (3 * $code{$i}); - } - for($i=0; $i <= 10; $i+=2) { - $sum += ($code{$i}); - } - $r = $sum % 10; - if($r > 0) { - $r = (10 - $r); - } + if ($len > 12) { + $sum_a *= 3; + } + $sum_b = 0; + for ($i = 0; $i < $data_len; $i+=2) { + $sum_b += ($code{$i}); + } + if ($len < 13) { + $sum_b *= 3; + } + $r = ($sum_a + $sum_b) % 10; + if($r > 0) { + $r = (10 - $r); + } + if ($code_len == $data_len) { + // add check digit $code .= $r; - } else { // test checkdigit - $sum = 0; - for($i=1; $i <= 11; $i+=2) { - $sum += (3 * $code{$i}); - } - for($i=0; $i <= 10; $i+=2) { - $sum += $code{$i}; - } - if ((($sum + $code{12}) % 10) != 0) { - return false; + } elseif ($r !== intval($code{$data_len})) { + // wrong checkdigit + return false; + } + if ($len == 12) { + // UPC-A + $code = '0'.$code; + ++$len; + } + if ($upce) { + // convert UPC-A to UPC-E + $tmp = substr($code, 4, 3); + if (($tmp == '000') OR ($tmp == '100') OR ($tmp == '200')) { + // manufacturer code ends in 000, 100, or 200 + $upce_code = substr($code, 2, 2).substr($code, 9, 3).substr($code, 4, 1); + } else { + $tmp = substr($code, 5, 2); + if ($tmp == '00') { + // manufacturer code ends in 00 + $upce_code = substr($code, 2, 3).substr($code, 10, 2).'3'; + } else { + $tmp = substr($code, 6, 1); + if ($tmp == '0') { + // manufacturer code ends in 0 + $upce_code = substr($code, 2, 4).substr($code, 11, 1).'4'; + } else { + // manufacturer code does not end in zero + $upce_code = substr($code, 2, 5).substr($code, 11, 1); + } + } } } //Convert digits to bars $codes = array( - 'A'=>array( + 'A'=>array( // left odd parity '0'=>'0001101', '1'=>'0011001', '2'=>'0010011', @@ -623,7 +674,7 @@ class TCPDFBarcode { '7'=>'0111011', '8'=>'0110111', '9'=>'0001011'), - 'B'=>array( + 'B'=>array( // left even parity '0'=>'0100111', '1'=>'0110011', '2'=>'0011011', @@ -634,7 +685,7 @@ class TCPDFBarcode { '7'=>'0010001', '8'=>'0001001', '9'=>'0010111'), - 'C'=>array( + 'C'=>array( // right '0'=>'1110010', '1'=>'1100110', '2'=>'1101100', @@ -658,38 +709,78 @@ class TCPDFBarcode { '8'=>array('A','B','A','B','B','A'), '9'=>array('A','B','B','A','B','A') ); - - $bararray = array("code" => $code, "maxw" => 0, "maxh" => 1, "bcode" => array()); + $upce_parities = array(); + $upce_parities[0] = array( + '0'=>array('B','B','B','A','A','A'), + '1'=>array('B','B','A','B','A','A'), + '2'=>array('B','B','A','A','B','A'), + '3'=>array('B','B','A','A','A','B'), + '4'=>array('B','A','B','B','A','A'), + '5'=>array('B','A','A','B','B','A'), + '6'=>array('B','A','A','A','B','B'), + '7'=>array('B','A','B','A','B','A'), + '8'=>array('B','A','B','A','A','B'), + '9'=>array('B','A','A','B','A','B') + ); + $upce_parities[1] = array( + '0'=>array('A','A','A','B','B','B'), + '1'=>array('A','A','B','A','B','B'), + '2'=>array('A','A','B','B','A','B'), + '3'=>array('A','A','B','B','B','A'), + '4'=>array('A','B','A','A','B','B'), + '5'=>array('A','B','B','A','A','B'), + '6'=>array('A','B','B','B','A','A'), + '7'=>array('A','B','A','B','A','B'), + '8'=>array('A','B','A','B','B','A'), + '9'=>array('A','B','B','A','B','A') + ); $k = 0; - $seq = '101'; - $p = $parities[$code{0}]; - for($i=1; $i < 7; $i++) { - $seq .= $codes[$p[$i-1]][$code{$i}]; - } - $seq .= '01010'; - for($i=7; $i < 13; $i++) { - $seq .= $codes['C'][$code{$i}]; + $seq = '101'; // left guard bar + if ($upce) { + $bararray = array('code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + $p = $upce_parities[$code[1]][$r]; + for ($i = 0; $i < 6; ++$i) { + $seq .= $codes[$p[$i]][$upce_code{$i}]; + } + $seq .= '010101'; // right guard bar + } else { + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + $half_len = intval(ceil($len / 2)); + if ($len == 8) { + for ($i = 0; $i < $half_len; ++$i) { + $seq .= $codes['A'][$code{$i}]; + } + } else { + $p = $parities[$code[0]]; + for ($i = 1; $i < $half_len; ++$i) { + $seq .= $codes[$p[$i-1]][$code{$i}]; + } + } + $seq .= '01010'; // center guard bar + for ($i = $half_len; $i < $len; ++$i) { + $seq .= $codes['C'][$code{$i}]; + } + $seq .= '101'; // right guard bar } - $seq .= '101'; - $len = strlen($seq); + $clen = strlen($seq); $w = 0; - for($i=0; $i < $len; $i++) { + for ($i = 0; $i < $clen; ++$i) { $w += 1; - if (($i == ($len - 1)) OR (($i < ($len - 1)) AND ($seq{$i} != $seq{($i+1)}))) { + if (($i == ($clen - 1)) OR (($i < ($clen - 1)) AND ($seq{$i} != $seq{($i+1)}))) { if ($seq{$i} == '1') { $t = true; // bar } else { $t = false; // space } - $bararray["bcode"][$k] = array("t" => $t, "w" => $w, "h" => 1, "p" => 0); - $bararray["maxw"] += $w; - $k++; + $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); + $bararray['maxw'] += $w; + ++$k; $w = 0; } } return $bararray; } - + /** * POSTNET barcodes. * @param string $code zip code to represent. Must be a string containing a zip code of the form DDDDD or DDDDD-DDDD. diff --git a/reporting/rep303.php b/reporting/rep303.php index 7d8984db..8851ca7e 100644 --- a/reporting/rep303.php +++ b/reporting/rep303.php @@ -112,6 +112,7 @@ function print_stock_check() $short = _('No'); $available = _('Available'); } + $barcodes = !empty($SysPrefs->prefs['barcodes_on_stock']); if ($no_zeros) $nozeros = _('Yes'); else $nozeros = _('No'); if ($check) @@ -127,12 +128,33 @@ function print_stock_check() $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right'); } + $params = array( + 0 => $comments, + 1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''), + 2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''), + 3 => array('text' => _('Only Shortage'), 'from' => $short, 'to' => ''), + 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => '') + ); - $params = array( 0 => $comments, - 1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''), - 2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''), - 3 => array('text' => _('Only Shortage'), 'from' => $short, 'to' => ''), - 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => '')); + if ($barcodes) + { + // define barcode style + $style = array( + 'position' => 'L', // If blank string, barcode starts on left edge of page + 'stretch' => false, + 'fitwidth' => true, + 'cellfitalign' => '', + 'border' => false, + 'padding' => 3, + 'fgcolor' => array(0,0,0), + 'bgcolor' => false, //array(255,255,255), + 'text' => true, + 'font' => 'helvetica', + 'fontsize' => 8, + 'stretchtext' => 4 + ); + // write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres=0.4, $style='', $align='') + } $rep = new FrontReport(_('Stock Check Sheets'), "StockCheckSheet", user_pagesize(), 9, $orientation); if ($orientation == 'L') @@ -191,19 +213,29 @@ function print_stock_check() $rep->AmountCol(5, 6, $trans['QtyOnHand'] - $demandqty, $dec); $rep->AmountCol(6, 7, $onorder, $dec); } - if ($pictures) + if ($pictures || $barcodes) { - $image = company_path() . '/images/' - . item_img_name($trans['stock_id']) . '.jpg'; - if (file_exists($image)) + $rep->NewLine(); + if ($rep->row - $SysPrefs->pic_height < $rep->bottomMargin) + $rep->NewPage(); + $firstcol = 1; + if ($barcodes) { - $rep->NewLine(); - if ($rep->row - $SysPrefs->pic_height < $rep->bottomMargin) - $rep->NewPage(); - $rep->AddImage($image, $rep->cols[1], $rep->row - $SysPrefs->pic_height, 0, $SysPrefs->pic_height); - $rep->row -= $SysPrefs->pic_height; - $rep->NewLine(); - } + $bar_y = $rep->GetY(); + $barcode = str_pad($trans['stock_id'], 7, '0', STR_PAD_LEFT); + $barcode = substr($barcode, 0, 8); // EAN 8 Check digit is auto computed and barcode printed + $rep->write1DBarcode($barcode, 'EAN8', $rep->cols[$firstcol++], $bar_y + 22, 22, $SysPrefs->pic_height, 1.2, $style, 'N'); + } + if ($pictures) + { + $image = company_path() . '/images/' . item_img_name($trans['stock_id']) . '.jpg'; + if (file_exists($image)) + { + $rep->AddImage($image, $rep->cols[$firstcol], $rep->row - $SysPrefs->pic_height, 0, $SysPrefs->pic_height); + } + } + $rep->row -= $SysPrefs->pic_height; + $rep->NewLine(); } } $rep->Line($rep->row - 4); diff --git a/sql/alter2.4.php b/sql/alter2.4.php index b3ec864d..51136eac 100644 --- a/sql/alter2.4.php +++ b/sql/alter2.4.php @@ -74,6 +74,9 @@ class fa2_4 extends fa_patch { if (get_company_pref('company_logo_report') === null) { // available from 2.4.2, during updates set_company_pref('company_logo_report', 'setup.company', 'tinyint', 1, '0'); } + if (get_company_pref('barcodes_on_stock') === null) { // available from 2.4.3, during updates + set_company_pref('barcodes_on_stock', 'setup.company', 'tinyint', 1, '0'); + } if (get_company_pref('shortname_name_in_list') === null) { // available from 2.4.2, during updates set_company_pref('shortname_name_in_list', 'setup.company', 'tinyint', 1, '0'); } @@ -114,7 +117,8 @@ class fa2_4 extends fa_patch { 'tax_algorithm', 'grn_clearing_act', 'default_receival_required', 'default_quote_valid_days', 'no_zero_lines_amount', 'show_po_item_codes', 'accounts_alpha', 'loc_notification', 'print_invoice_no', 'allow_negative_prices', 'print_item_images_on_quote', - 'bcc_email', 'alternative_tax_include_on_docs', 'suppress_tax_rates', 'company_logo_report')"); + 'bcc_email', 'alternative_tax_include_on_docs', 'suppress_tax_rates', 'company_logo_report', + 'barcodes_on_report')"); } function update_workorders()