From b3ea789b238d0176200ba361e8885aa06f9200ce Mon Sep 17 00:00:00 2001 From: Hugo Villeneuve Date: Mon, 20 Jan 2014 13:43:27 -0500 Subject: [PATCH] Add option to specify inventory file for Digikey BOM export If a part is present in inventory file, it won't appear in exported Digikey BOM. --- bomgen/Bomgen/digikey.inc.php | 62 +++++++++++++++++++++++++++++++++-- bomgen/bomgen.php | 15 +++++++-- 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/bomgen/Bomgen/digikey.inc.php b/bomgen/Bomgen/digikey.inc.php index 731dc1d..a6013d2 100644 --- a/bomgen/Bomgen/digikey.inc.php +++ b/bomgen/Bomgen/digikey.inc.php @@ -11,8 +11,64 @@ define("CUSTOMER_REFERENCE_COL_NAME", COMPANY_PN_COL_NAME); +function import_inventory($filename) +{ + global $debug; + + $data = array(); + + $handle = fopen($filename, "r"); + + if ($handle == false) { + echo "Cannot open file: " . $filename . "\n"; + exit(1); + } + + while (($row = fgetcsv($handle, 1024, ',')) !== false) { + $num = count($row); + + if ($num < 2) { + /* Ligne vide. */ + continue; + } + + $data[] = $row; + } + + fclose($handle); + + return $data; +} + +/* + * If a part is found in inventory file, exclude it from BOM. + * + * If the customer P/N is empty, try to match by manufacturer P/N instead. + */ +function find_part_in_inventory($inventory, $bom_customer_pn, $bom_manufacturer_pn) +{ + foreach ($inventory as $key => $row) { + $inv_customer_pn = $row[0]; + + if ($bom_customer_pn !== "") { + $match = $bom_customer_pn; + } else if ($bom_manufacturer_pn !== "") { + $match = $bom_manufacturer_pn; + } else { + echo "Error: Need at least one of customer or manufacturer P/N\n"; + exit(1); + } + + if ($inv_customer_pn == $match) { + return true; + } + } + + return false; +} + /* Exportation BOM digikey. */ -function export_bom_digikey(&$data, $num, $col_num_to_id, $col_id_to_num, $filename) +function export_bom_digikey(&$data, $num, $col_num_to_id, $col_id_to_num, $filename, $inventory) { global $debug, $kits; @@ -40,7 +96,9 @@ function export_bom_digikey(&$data, $num, $col_num_to_id, $col_id_to_num, $filen } } - if ($pn != "MISSING") { + $exclude_part = find_part_in_inventory($inventory, $customer_ref, $pn); + + if (($pn != "MISSING") && ($exclude_part == false)) { $line = $qty . CSV_DK_DELIM . $manuf . CSV_DK_DELIM . $pn . CSV_DK_DELIM . $customer_ref . "\r\n"; fwrite($handle, $line); } diff --git a/bomgen/bomgen.php b/bomgen/bomgen.php index 81c21d6..a1fc03b 100755 --- a/bomgen/bomgen.php +++ b/bomgen/bomgen.php @@ -22,6 +22,7 @@ $debug = 0; $digikey_bom = false; $kits = 1; +$use_inventory = false; define("DATA_ROW_START", 3); @@ -61,7 +62,7 @@ $csv_infos = "width" => 0), ); -$short_opts = "hckn:"; +$short_opts = "hci:kn:"; function usage() { @@ -71,6 +72,7 @@ function usage() echo "\n"; echo "Options:\n"; echo " -c Add cost information to the BOM\n"; + echo " -i Specifiy inventory CSV file\n"; echo " -k Create digikey BOM submission file (tab-separated fields)\n"; echo " -h Display this help and exit\n"; echo " -n Number of cards/kits for digikey BOM submission\n"; @@ -192,6 +194,10 @@ if (sizeof($opts) > 0) { case 'c': $add_cost = true; break; + case 'i': + $use_inventory = true; + $inventory_filename = $o[1]; + break; case 'k': $digikey_bom = true; break; @@ -322,8 +328,13 @@ array_multisort($sort_type, SORT_ASC, SORT_STRING, if ($digikey_bom) { /* Exportation BOM digikey. */ $dest .= ".txt"; + $inventory = array(); + + if ($use_inventory) { + $inventory = import_inventory($inventory_filename); + } - export_bom_digikey($data, $num, $col_num_to_id, $col_id_to_num, $dest); + export_bom_digikey($data, $num, $col_num_to_id, $col_id_to_num, $dest, $inventory); } else { /* Exportation BOM Excel */ -- 2.20.1