Prechádzať zdrojové kódy

product handler for product and services migration

szollosil 1 rok pred
rodič
commit
99ae9d4174
1 zmenil súbory, kde vykonal 80 pridanie a 14 odobranie
  1. 80 14
      custom/bbus/product_handler.php

+ 80 - 14
custom/bbus/product_handler.php

@@ -38,9 +38,6 @@ $llx_product_extrafields_schema = getSchema('llx_product_extrafields');
 $llx_product_price_schema = getSchema('llx_product_price');
 $llx_product_association_schema = getSchema('llx_product_association');
 
-//print_r($llx_product_schema);
-//var_dump($llx_product_schema['ref_ext']);
-
 function getSchema($table)
 {
     global $db;
@@ -57,25 +54,36 @@ function getSchema($table)
     return $array;
 }
 
-function productsCreator($csvFile, $table, $schema)
+function productsCreator($table, $schema)
 {
     $sql = '';
-    if (($handle = fopen($csvFile, 'r')) !== FALSE) {
+    $header_array = [];
+    if (($handle = fopen('csv/' . $table . '_excelia_staging_Excel.csv', 'r')) !== FALSE) {
         $header_InsertInto = '';
         $rowCounter = 0;
+        if ($table == 'llx_product_association') {
+            $sql .= "DELETE FROM llx_product_association;";
+            //$sql .= '<br><br>';
+        }
         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
             if ($rowCounter == 0) {
                 $header_array = explode(';', $data[0]);
                 $header_InsertInto = implode(',', $header_array);
             } else {
-                $sql .= "INSERT INTO {$table} ({$header_InsertInto}) VALUES (";
                 $fieldCounter = 0;
                 $row_array = explode(';', $data[0]);
                 $fieldsValues = [];
+                if ($table == 'llx_product_extrafields') {
+                    $sql .= "DELETE FROM llx_product_extrafields WHERE fk_object = {$row_array[2]};";
+                    //$sql .= '<br><br>';
+                }
+                if ($table == 'llx_product_price') {
+                    $sql .= "DELETE FROM llx_product_price WHERE fk_product = {$row_array[3]};";
+                    //$sql .= '<br><br>';
+                }
+                $sql .= "INSERT INTO {$table} ({$header_InsertInto}) VALUES (";
                 foreach ($header_array as $field) {
-                    //print $field . ' = ';
-                    //var_dump($row_array[$fieldCounter]);
-                    //print '<br>';
+
                     if ($schema[$field] == 'character varying' || $schema[$field] == 'timestamp without time zone' || $schema[$field] == 'text') {
                         if ($row_array[$fieldCounter] == "NULL") {
                             $fieldsValues[] = 'NULL';
@@ -85,7 +93,11 @@ function productsCreator($csvFile, $table, $schema)
                             $fieldsValues[] = '\'' . $row_array[$fieldCounter] . '\'';
                         }
                     } else {
-                        $fieldsValues[] = $row_array[$fieldCounter];
+                        if ($field == 'fk_user_author') {
+                            $fieldsValues[] = 1;
+                        } else {
+                            $fieldsValues[] = $row_array[$fieldCounter];
+                        }
                     }
                     $fieldCounter++;
                 }
@@ -99,7 +111,10 @@ function productsCreator($csvFile, $table, $schema)
                         $updatFieldsValues[] = $field . ' = EXCLUDED.' . $field;
                     }
                 }
-                $sql .= implode(', ', $updatFieldsValues) . ";" . '<br><br>';
+                $sql .= implode(', ', $updatFieldsValues) . ";";
+                //$sql .= "<br><br>";
+                //}
+
             }
             $rowCounter++;
         }
@@ -110,8 +125,59 @@ function productsCreator($csvFile, $table, $schema)
     return $sql;
 }
 
-$csvFile = 'csv/llx_product_excelia_staging_Excel.csv';
+function runQuery($sql, $table)
+{
+    global $db;
+    echo 'START query runnning: <b>' . $table . '</b><br>';
+    $sql_commands = explode(';', $sql);
+    foreach ($sql_commands as $sql_command) {
+        $sql_command = trim($sql_command); // Üres parancsok eltávolítása
+        if (!empty($sql_command)) {
+            $resql = $db->query($sql_command);
+            if (!$resql) {
+                echo $sql . '<br>';
+                echo "Hiba történt: " . $db->lasterror() . '<br>';
+                echo $sql_command . '<br><br>';
+            }
+        }
+    }
+    echo 'KÉSZ!<br><br>';
+}
+
+$files = ['llx_product_excelia_staging_Excel', 'llx_product_extrafields_excelia_staging_Excel', 'llx_product_price_excelia_staging_Excel', 'llx_product_association_excelia_staging_Excel'];
+print '<form method="post" action="' . $_SERVER["PHP_SELF"] . '">';
+print '<input type="hidden" name="token" value="' . newToken() . '">';
+if (isset($_REQUEST['submit_button'])) {
+    $debug = false;
 
-$sql = productsCreator($csvFile, 'llx_product_temp', $llx_product_schema);
+    $sql_llx_product = productsCreator('llx_product', $llx_product_schema);
+    $sql_llx_product_extrafields = productsCreator('llx_product_extrafields', $llx_product_extrafields_schema);
+    $sql_llx_product_price = productsCreator('llx_product_price', $llx_product_price_schema);
+    $sql_llx_product_association = productsCreator('llx_product_association', $llx_product_association_schema);
 
-print $sql;
+    if ($debug) {
+        print $sql_llx_product;
+        print $sql_llx_product_extrafields;
+        print $sql_llx_product_price;
+        print $sql_llx_product_association;
+    } else {
+        runQuery($sql_llx_product, 'llx_product');
+        runQuery($sql_llx_product_extrafields, 'llx_product_extrafields');
+        runQuery($sql_llx_product_price, 'llx_product_price');
+        runQuery($sql_llx_product_association, 'llx_product_association');
+
+        echo 'KÉSZ!';
+    }
+    print '<input type="submit" name="done" value="OK">';
+} elseif ($_REQUEST['done'] || !isset($_REQUEST['submit_button'])) {
+    foreach ($files as $file) {
+        if (file_exists('csv/' . $file . '.csv')) {
+            print $file . '.csv - <font style="color: green;"><b>OK</b></font><br>';
+        } else {
+            print $file . '.csv - <font style="color: RED;"><b>PROBLEM</b></font><br>';
+        }
+    }
+    print '<br><br>';
+    print '<input type="submit" name="submit_button" value="Termékek és szolgáltatások generálása!">';
+}
+print '</form>';