Add mode to create Digikey BOM
[eda-utils.git] / bomgen / bomgen.php
index c240dea..4883b72 100755 (executable)
 require_once 'Spreadsheet/Excel/Writer.php';
 require_once 'Bomgen/functions.inc.php';
 require_once 'Bomgen/refdes.inc.php';
+require_once 'Bomgen/digikey.inc.php';
+require_once 'Bomgen/conf.inc.php';
 
 $debug = 0;
 
+$digikey_bom = false;
+
 define("DATA_ROW_START", 3);
 
 define("PAPER_US_LETTER", 1);
 
-define("QTY_COL_NAME", "Count");
-define("COMPANY_PN_COL_NAME", "P/N_LSI");
-define("DESIGNATOR_COL_NAME", "RefDes");
-
-
-define("ASSEMBLY_COL_NAME", "Asm");
-define("DO_NOT_POPULATE_KEYWORD", "DNP");
-
 /* width = 0 -> Ne pas afficher la colonne. */
 $csv_infos =
   array(QTY_COL_NAME     => array("nom"   => "Qty",
@@ -67,11 +63,12 @@ $csv_infos =
 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";
 }
 
@@ -93,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.
@@ -167,7 +166,7 @@ $add_cost = false;
 $comp_total = 0;
 $titre = "BOM";
 
-$short_opts = "hc";
+$short_opts = "hck";
 
 $cg = new Console_Getopt();
 
@@ -191,6 +190,9 @@ if (sizeof($opts) > 0) {
         case 'c':
           $add_cost = true;
           break;
+        case 'k':
+          $digikey_bom = true;
+          break;
         case 'h':
           usage();
           exit(1);
@@ -213,17 +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);
-
-if (PEAR::isError($worksheet)) {
-    die($worksheet->getMessage());
-}
+$dest = strip_ext($src);
 
 /* Importation du BOM CSV. */
 $data = array();
@@ -270,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 {
@@ -321,110 +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 */
+
+  $dest .= ".xls";
+
+  /* Création d'un workbook. */
+  $workbook = new Spreadsheet_Excel_Writer($dest);
 
-  /* Index de la prochaine colonne disponible pour affichage. */
+  /* Création du worksheet */
+  $worksheet =& $workbook->addWorksheet($titre);
+
+  if (PEAR::isError($worksheet)) {
+      die($worksheet->getMessage());
+  }
+
+  $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[COMPANY_PN_COL_NAME]]);
-        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[COMPANY_PN_COL_NAME]]);
-        }
+    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];
+          }
 
-        $k++;
-      } else if ($col_num_to_id[$c] == "Manufacturer") {
-        $manufacturer = $row[$c];
+          if ($price == 0) {
+            $price = "";
+          }
+
+          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[COMPANY_PN_COL_NAME]]);
-        //}
-
-        xls_write_cell($row_num, $k, $manufacturer, XLS_FORMAT_TEXT);
+          //}
 
-        $k++;
-      } else {
-        if ($width) {
-          $value = $row[$c];
+          xls_write_cell($row_num, $k, $manufacturer, XLS_FORMAT_TEXT);
 
-          /* 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";
-}
+  /* 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);
+  if (!file_exists($dest)) {
+    echo "Cannot create destination file: " . $dest . "\n";
+    exit(1);
+  }
 }
 
 ?>