Change digikey customer reference column to P/N_LSI
[eda-utils.git] / bomgen / bomgen.php
index e61d73e..b5b8c23 100755 (executable)
  * (at your option) any later version.
  */
 
+require_once 'Bomgen/conf.inc.php';
 require_once 'Spreadsheet/Excel/Writer.php';
 require_once 'Bomgen/functions.inc.php';
+require_once 'Bomgen/refdes.inc.php';
+require_once 'Bomgen/digikey.inc.php';
+
+$debug = 0;
+
+$digikey_bom = false;
 
 define("DATA_ROW_START", 3);
 
 define("PAPER_US_LETTER", 1);
-define("PAPER_US_LEGAL", 5);
-define("PAPER_US_A3", 8);
-define("PAPER_US_A4", 9);
-define("PAPER_US_A5", 11);
 
 /* width = 0 -> Ne pas afficher la colonne. */
 $csv_infos =
-  array("Count"          => array("nom"   => "Qty",
+  array(QTY_COL_NAME     => array("nom"   => "Qty",
                                   "width" => 5),
-        "RefDes"         => array("nom"   => "Designator",
+        DESIGNATOR_COL_NAME => array("nom"   => "Designator",
                                   "width" => 30),
         "Description"    => array("nom"   => "Description",
                                   "width" => 45),
@@ -35,23 +38,19 @@ $csv_infos =
                                   "width" => 15),
         "Voltage"        => array("nom"   => "Voltage",
                                   "width" => 10),
-        "CURRENT"        => array("nom"   => "Current",
+        "Current"        => array("nom"   => "Current",
                                   "width" => 10),
-        "POWER"          => array("nom"   => "Power",
+        "Power"          => array("nom"   => "Power",
                                   "width" => 10),
-        "MANUFACTURER"   => array("nom"   => "Manufacturer",
+        "Manufacturer"   => array("nom"   => "Manufacturer",
                                   "width" => 30),
         "Part Number"    => array("nom"   => "Manuf. P/N",
                                   "width" => 30),
-        "P/N_LSI"        => array("nom"   => "LSI P/N",
+        COMPANY_PN_COL_NAME => array("nom"   => "LSI P/N",
                                   "width" => 15),
         "PatternName"    => array("nom"   => "Footprint",
                                   "width" => 20),
-        "Revision"       => array("nom"   => "Revision",
-                                  "width" => 0),
-        "DocumentNumber" => array("nom"   => "DocumentNumber",
-                                  "width" => 0),
-        "NOPOP"          => array("nom"   => "NOPOP",
+        ASSEMBLY_COL_NAME => array("nom"   => "Assembly",
                                   "width" => 0),
         "UnitCost"       => array("nom"   => "Unit Cost",
                                   "width" => 7),
@@ -61,14 +60,15 @@ $csv_infos =
                                   "width" => 0),
         );
 
-function usage() 
-{ 
+function usage()
+{
   echo "Usage: bomgen.php [OPTIONS] [INPUT FILE]\n";
-  echo "Generate an Excel BOM from an Altium CSV-exported BOM.\n";
-  echo "Output file has same name as input file, but with a .xls extension.\n";
+  echo "Generate an Excel BOM from a PCAD CSV-exported BOM.\n";
+  echo "Output file has same name as input file, but with a .xls or .txt extension.\n";
   echo "\n";
   echo "Options:\n";
   echo "  -c  Add cost information to the BOM\n";
+  echo "  -k  Create digiley BOM submission file (tab-separated fields)\n";
   echo "  -h  Display this help and exit\n";
 }
 
@@ -90,6 +90,8 @@ $col_id_to_num = array();
  */
 $col_num_to_id = array();
 
+$entete = array();
+
 /*
  * value: clé
  * c:     Numéro de colonne dans le fichier CSV.
@@ -164,7 +166,7 @@ $add_cost = false;
 $comp_total = 0;
 $titre = "BOM";
 
-$short_opts = "hc";
+$short_opts = "hck";
 
 $cg = new Console_Getopt();
 
@@ -188,6 +190,9 @@ if (sizeof($opts) > 0) {
         case 'c':
           $add_cost = true;
           break;
+        case 'k':
+          $digikey_bom = true;
+          break;
         case 'h':
           usage();
           exit(1);
@@ -210,13 +215,7 @@ if (sizeof($args) == 0) {
   exit(1);
 }
 
-$dest = strip_ext($src) . ".xls";
-
-/* Création d'un workbook. */
-$workbook = new Spreadsheet_Excel_Writer($dest);
-
-/* Création du worksheet */
-$worksheet =& $workbook->addWorksheet($titre);
+$dest = strip_ext($src);
 
 /* Importation du BOM CSV. */
 $data = array();
@@ -263,7 +262,8 @@ if (($handle = fopen($src, "r")) !== FALSE) {
           if ($csv_infos[$id]['width'] != 0) {
             $nom = $csv_infos[$id]['nom'];
 
-            xls_write_col_header(DATA_ROW_START, $k, $nom);
+            $entete[$k] = $nom;
+
             $k++;
           }
         } else {
@@ -271,13 +271,13 @@ if (($handle = fopen($src, "r")) !== FALSE) {
         }
       }
     } else {
-      $ref_prefix2 = substr($row[$col_id_to_num['Designator']], 0, 2);
-      $company_pn = substr($row[$col_id_to_num['Comment']], 0, 1); /* Pour les vieux BOMs qui n'ont pas
+      $ref_prefix2 = substr($row[$col_id_to_num[DESIGNATOR_COL_NAME]], 0, 2);
+      $company_pn = substr($row[$col_id_to_num[COMPANY_PN_COL_NAME]], 0, 1); /* Pour les vieux BOMs qui n'ont pas
                                                                  * l'attribut NOPOP. */
-
       if (($ref_prefix2 == "MH") ||
           ($ref_prefix2 == "TP") ||
-          (array_key_exists('NOPOP', $col_id_to_num) && ($row[$col_id_to_num['NOPOP']] == "NOPOP")) ||
+          ($ref_prefix2 == "FI") ||
+          (array_key_exists(ASSEMBLY_COL_NAME, $col_id_to_num) && ($row[$col_id_to_num[ASSEMBLY_COL_NAME]] == DO_NOT_POPULATE_KEYWORD)) ||
           ($company_pn == "*")) {
         /* Enlève les composants non désirés. */
         continue;
@@ -293,6 +293,9 @@ if (($handle = fopen($src, "r")) !== FALSE) {
   fclose($handle);
 }
 
+/* Combinaison de plusieurs lignes en une seule pour PCAD. */
+refdes_combine($data, $num, $col_num_to_id, $col_id_to_num);
+
 /* Génère une liste des colonnes pour le tri. */
 foreach ($data as $key => $row) {
   $sort_type[$key]  = $row[$col_id_to_num['Type']];
@@ -302,7 +305,7 @@ foreach ($data as $key => $row) {
 }
 
 /*
- * Triage des lignes selon la référence (colonne "Designator"),
+ * Triage des lignes selon la référence (colonne DESIGNATOR_COL_NAME),
  * et ensuite selon la description, la valeur et enfin le voltage.
  */
 array_multisort($sort_type, SORT_ASC, SORT_STRING,
@@ -311,104 +314,143 @@ array_multisort($sort_type, SORT_ASC, SORT_STRING,
                 $sort_volt, SORT_ASC, SORT_NUMERIC,
                 $data);
 
-$row_num = DATA_ROW_START + 1;
+if ($digikey_bom) {
+  /* Exportation BOM digikey. */
+  $dest .= ".txt";
 
-/* Insertion des données dans le tableau. */
-foreach ($data as $key => $row) {
-  $comp_total++; /* Compte le nombre de composants total. */
-  $num = count($row);
+  export_bom_digikey($data, $num, $col_num_to_id, $col_id_to_num, $dest);
+} else {
+  /* Exportation BOM Excel */
 
-  if (empty($project_name)) {
-    $project_name = $row[$col_id_to_num['DocumentNumber']];
-  }
-  if (empty($revision)) {
-    $revision = $row[$col_id_to_num['Revision']];
+  $dest .= ".xls";
+
+  /* Création d'un workbook. */
+  $workbook = new Spreadsheet_Excel_Writer($dest);
+
+  /* Création du worksheet */
+  $worksheet =& $workbook->addWorksheet($titre);
+
+  if (PEAR::isError($worksheet)) {
+      die($worksheet->getMessage());
   }
 
-  /* Index de la prochaine colonne disponible pour affichage. */
+  $row_num = DATA_ROW_START;
+
+  /* Écriture des colonnes d'entête. */
   $k = 0;
+  foreach ($entete as $key => $value) {
+    xls_write_col_header($row_num, $k, $value);
+    $k++;
+  }
 
-  for ($c = 0; $c < $num; $c++) {
-    if (isset($csv_infos[$col_num_to_id[$c]])) {
-      $width = $csv_infos[$col_num_to_id[$c]]['width'];
+  $row_num++;
 
-      if (($add_cost == true) && ($col_num_to_id[$c] == "UnitCost")) {
-        /* Lecture du prix dans Syteline. */
-        $price = get_price($row[$col_id_to_num['Comment']]);
-        if ($price == 0) {
-          /* Si pas de prix Syteline, on utilise le prix dans Altium. */
-          $price = $row[$c];
-        }
+  /* Insertion des données dans le tableau. */
+  foreach ($data as $key => $row) {
+    $comp_total++; /* Compte le nombre de composants total. */
+    $num = count($row);
 
-        if ($price == 0) {
-          $price = "";
-        }
+    /* Index de la prochaine colonne disponible pour affichage. */
+    $k = 0;
 
-        xls_write_price($row_num, $k, $price);
-        $k++;
-      } else if (($add_cost == true) && ($col_num_to_id[$c] == "TotalCost")) {
-        /* Formule prix total = qté * unit_cost */
-        xls_write_price($row_num, $k, "=" . col_to_letter($col_id_to_xls_num["Quantity"]) .
-                        ($row_num + 1) . "*" . col_to_letter($col_id_to_xls_num["UnitCost"]) .
-                        ($row_num + 1) . ")");
-        $k++;
-      } else if ($col_num_to_id[$c] == "MPN") {
-        $mpn = $row[$c];
-
-        if (empty($mpn)) {
-          /* Lecture du "MPN" dans Syteline si celui dans Altium est absent. */
-          $mpn = get_mpn($row[$col_id_to_num['Comment']]);
-        }
+    for ($c = 0; $c < $num; $c++) {
+      if (isset($csv_infos[$col_num_to_id[$c]])) {
+        $width = $csv_infos[$col_num_to_id[$c]]['width'];
 
-        xls_write_cell($row_num, $k, $mpn, XLS_FORMAT_TEXT);
+        if (($add_cost == true) && ($col_num_to_id[$c] == "UnitCost")) {
+          /* Lecture du prix dans Syteline. */
+          $price = get_price($row[$col_id_to_num[COMPANY_PN_COL_NAME]]);
+          if ($price == 0) {
+            /* Si pas de prix Syteline, on utilise le prix dans Altium. */
+            $price = $row[$c];
+          }
+
+          if ($price == 0) {
+            $price = "";
+          }
 
-        $k++;
-      } else if ($col_num_to_id[$c] == "Manufacturer") {
-        $manufacturer = $row[$c];
+          xls_write_price($row_num, $k, $price);
 
-        if (empty($manufacturer)) {
+          $k++;
+        } else if (($add_cost == true) && ($col_num_to_id[$c] == "TotalCost")) {
+          /* Formule prix total = qté * unit_cost */
+          xls_write_price($row_num, $k, "=" . col_to_letter($col_id_to_xls_num["Quantity"]) .
+                          ($row_num + 1) . "*" . col_to_letter($col_id_to_xls_num["UnitCost"]) .
+                          ($row_num + 1) . ")");
+
+          $k++;
+        } else if ($col_num_to_id[$c] == "MPN") {
+          $mpn = $row[$c];
+
+          if (empty($mpn)) {
+            /* Lecture du "MPN" dans Syteline si celui dans Altium est absent. */
+            $mpn = get_mpn($row[$col_id_to_num[COMPANY_PN_COL_NAME]]);
+          }
+
+          xls_write_cell($row_num, $k, $mpn, XLS_FORMAT_TEXT);
+
+          $k++;
+        } else if ($col_num_to_id[$c] == "Manufacturer") {
+          $manufacturer = $row[$c];
+
+          //if (empty($manufacturer)) {
           /* Lecture du "Manufacturer" dans Syteline si celui dans Altium est absent. */
-          $manufacturer = get_manufacturer($row[$col_id_to_num['Comment']]);
-        }
+          //$manufacturer = get_manufacturer($row[$col_id_to_num[COMPANY_PN_COL_NAME]]);
+          //}
 
-        xls_write_cell($row_num, $k, $manufacturer, XLS_FORMAT_TEXT);
+          xls_write_cell($row_num, $k, $manufacturer, XLS_FORMAT_TEXT);
 
-        $k++;
-      } else {
-        if ($width) {
-          $value = $row[$c];
-          
-          /* Ligne de données. */
-          xls_write_cell($row_num, $k, $value, XLS_FORMAT_TEXT);
           $k++;
+        } else {
+          if ($width) {
+            $value = $row[$c];
+
+            /* Ligne de données. */
+            xls_write_cell($row_num, $k, $value, XLS_FORMAT_TEXT);
+
+            $k++;
+          }
         }
       }
     }
+
+    $row_num++;
   }
 
-  $row_num++;
-}
+  if ($add_cost == true) {
+    /* Ajout cellule prix total. */
+    xls_write_price($row_num, $col_id_to_xls_num["TotalCost"],
+                    "=SUM(" . col_to_letter($col_id_to_xls_num["TotalCost"]) .
+                    (DATA_ROW_START + 2) . ":" . col_to_letter($col_id_to_xls_num["TotalCost"]) .
+                    $row_num . ")");
+  }
 
-if ($add_cost == true) {
-  /* Ajout cellule prix total. */
-  xls_write_price($row_num, $col_id_to_xls_num["TotalCost"],
-                  "=SUM(" . col_to_letter($col_id_to_xls_num["TotalCost"]) .
-                  (DATA_ROW_START + 2) . ":" . col_to_letter($col_id_to_xls_num["TotalCost"]) .
-                  $row_num . ")");
-}
+  /* TODO... */
+  if (empty($project_name)) {
+    $project_name = "PROJECT NAME";
+  }
+  if (empty($revision)) {
+    $revision = "REV";
+  }
 
-/* Écriture des informations globales relatives au BOM. */
-xls_write_cell_gen(0, 0, "BOM " . $project_name,
-                   XLS_FORMAT_TEXT, false, 18, FALSE, FALSE);
+  /* Écriture des informations globales relatives au BOM. */
+  xls_write_cell_gen(0, 0, "BOM " . $project_name,
+                     XLS_FORMAT_TEXT, false, 18, FALSE, FALSE);
 
-xls_write_cell_gen(1, 0, "Révision (schéma): " . $revision,
-                   XLS_FORMAT_TEXT, false, 16, FALSE, FALSE);
+  xls_write_cell_gen(1, 0, "Révision (schéma): " . $revision,
+                     XLS_FORMAT_TEXT, false, 16, FALSE, FALSE);
 
-xls_write_cell_gen(1, 2, "Composants: " . $comp_total,
-                   XLS_FORMAT_TEXT, false, 16, FALSE, FALSE);
+  xls_write_cell_gen(1, 2, "Composants: " . $comp_total,
+                     XLS_FORMAT_TEXT, false, 16, FALSE, FALSE);
 
-xls_configure($workbook, $worksheet);
+  xls_configure($workbook, $worksheet);
 
-$workbook->close();
+  $workbook->close();
+
+  if (!file_exists($dest)) {
+    echo "Cannot create destination file: " . $dest . "\n";
+    exit(1);
+  }
+}
 
 ?>