Fix #7123 - getting abra ready to test
[Pman.Xtuple] / UpdateDatabase.php
index f4f1afa..91b91e5 100644 (file)
@@ -7,8 +7,8 @@
  *
  */
 
-require_once 'Pman.php';
-class Pman_Xtuple_UpdateDatabase extends Pman
+require_once 'Pman/Core/UpdateDatabase.php';
+class Pman_Xtuple_UpdateDatabase extends Pman_Core_UpdateDatabase
 {
     
     static $cli_desc = "Update SQL - Beta";
@@ -32,16 +32,36 @@ class Pman_Xtuple_UpdateDatabase extends Pman
             'max' => 1,
             
         ),
-
-        'fix-sequences' => array(
-            'desc' => 'Run fix sequences -- for testing',
-            //'short' => 'f',
+        'json-accnt' => array(
+            'desc' => 'Accounts JSON file',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+            
+        ),
+        'json-curr-symbol' => array(
+            'desc' => 'Currency Symbols JSON file',
             'default' => '',
             'min' => 1,
             'max' => 1,
+            
         ),
 
-        
+        'json-location' => array(
+            'desc' => 'Location JSON file',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+            
+        ),
+        'json-terms' => array(
+            'desc' => 'Terms  JSON file',
+            'default' => '',
+            'min' => 1,
+            'max' => 1,
+            
+        ),
         
     );
     
@@ -56,99 +76,122 @@ class Pman_Xtuple_UpdateDatabase extends Pman
         }
         die("NOT ALLOWED");
     }
-    function get($k = '',$opt)
+    
+    // called to ensure options are correct.
+    // before any data is imported / started..
+    function checkOpts($opts)
     {
-        $k = strtolower($k);
+        $opts = HTML_FlexyFramework::get()->page->opts;
+        if (
+                (!empty($opts['init']) && empty($opts['base']) )
+                ||
+                (empty($opts['init']) && !empty($opts['base']) )
+           ) {
+                    
+            die("--base=XXX  and --init =1 must be used together.");
+        }
+        // default to import the terms.
+        if (
+                (!empty($opts['init']) && empty($opts['json-terms']) ))
+        {
+            HTML_FlexyFramework::get()->page->opts['json-terms'] = dirname(__FILE__).'/Setup/terms.json';
+            
+        }
+       
         
-        $this->opts = $opt;
+         
+    }
+    
+    
+    function get($k = '',$opts=array())
+    {
+        $k = strtolower($k);
         
+        $this->opts = $opts;
         
-        if(!empty($opt['fix-sequences'])){
-            $this->fixSequences();
-            exit;
-        }
+        require_once 'Pman/Core/UpdateDatabase.php';
         
+        Pman_Core_UpdateDatabase::jsonImportFromArray($opts);
+          
+        $this->updateData($k);
         
+    }
+    
+    function importModuleSQL($dburl)
+    {
+         
+            $dbtype = $dburl['scheme'];
+            $dirmethod = 'import' . $dburl['scheme'] . 'dir';
         
+            $m = 'Xtuple';
+            echo "Xtyple:Importing SQL\n";
         
-        if($k == 'accnt'){
-            if(empty($opt['source'])){
-                die("Missing Source directory for account json files! Try -f [JSON file path] \n");
-            }
-            
-            if (!file_exists($opt['source'])) {
-                die("can not found account json file : {$opt['source']} \n");
-            }
-
-            $accounts = json_decode(file_get_contents($opt['source']),true);
             
-            DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
+            // if init has been called
+            // look in pgsql.ini -- This creates all the tables....
             
-            die("DONE! \n");
-        }
-        
-        if($k == 'curr_symbol'){
-            if(!empty($opt['base'])){
-                DB_DataObject::factory('curr_symbol')->setBase($this, $opt['base']);
-                die("DONE! \n");
-            }
-            
-            if(!empty($opt['source'])){
+            if (!empty($this->opts['init'])) {
+                // it should really use xtuple... but it's not very well organized...
+                //$this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.init");
                 
-                if (!file_exists($opt['source'])) {
-                    die("can not found currency json file : {$opt['source']} \n");
-                }
+                // ?? use dbscripts/misc/postbooks_empty.backup ???
                 
-                $currencies = json_decode(file_get_contents($opt['source']),true);
+                $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.init");
                 
-                DB_DataObject::factory('curr_symbol')->importFromArray($this, $currencies);
-                die("DONE! \n");
             }
+            /*
+            // let's see if this works.. -- not very flexible... - hardcoding schema...
+            $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/440_schema.sql");
             
-            die("Missing Base Currency or Source directory for Currency json files! Try -b [base currency] or -f [JSON file path]\n");
-        }
-        
-        if($k == 'location'){
-            if(empty($opt['source'])){
-                die("Missing Source directory for location json file! Try -f [JSON file path] \n");
+            foreach(array(
+                'public/types',
+                'public/tables',
+                'public/views',
+                'public/functions', 
+                'public/trigger_functions',
+                'public/indexes',
+                'api/functions',
+                'api/views',
+                
+                
+            ) as $dir) {
+                $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/" . $dir);   
             }
+            */
+            // first try the standard from xtuple-database
             
-            if (!file_exists($opt['source'])) {
-                die("can not found location json file : {$opt['source']} \n");
-            }
             
-            $locations = json_decode(file_get_contents($opt['source']),true);
+            // new -- sql directory..
+            // new style will not support migrate ... they have to go into mysql-migrate.... directories..
+            // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
             
-            DB_DataObject::factory('location')->importFromArray($this, $locations);
             
-            die("DONE! \n");
-        }
-        
-        if($k == 'terms'){
-            if(empty($opt['source'])){
-                die("Missing Source directory for terms json file! Try -f [JSON file path] \n");
-            }
             
-            if (!file_exists($opt['source'])) {
-                die("can not found terms json file : {$opt['source']} \n");
-            }
+             
+            $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}");
             
-            $terms = json_decode(file_get_contents($opt['source']),true);
             
-            DB_DataObject::factory('terms')->importFromArray($this, $terms);
             
-            die("DONE! \n");
-        }
-        
-        
-        $this->updateData($k);
-        
+            if (!empty($this->opts['init']) && file_exists($this->rootDir. "/Pman/$m/{$dbtype}.initdata")) {
+                HTML_FlexyFramework::get()->generateDataobjectsCache(true);
+                
+                $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.initdata");
+                // fixSequences does nto appar to work on postgres anymore
+                //$this->{'fixSequences'. $dbtype}();
+                
+            }
+              
     }
     
     
-    
-    
-    function updateData($k='') {
+    function updateData($k='')
+    {
+        $ff = HTML_Flexyframework::get();
+        $dburl = parse_url($ff->DB_DataObject['database']);
+       
+        
+//        $this->importpgsqldir($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/empty_data.sql");
+        
         //DB_DataObject::debugLevel(1);
         $tables = empty($k) ? array(
                 
@@ -162,11 +205,28 @@ class Pman_Xtuple_UpdateDatabase extends Pman
                 'taxzone',
                 'taxtype',
                 'expcat',
+                
+                'custinfo', // create a base customer based on company.
+                'location', // default warehouse.
+                'period', // adds years and months -1 to +10 from now.
+                
+                // accounts - fixme - we need a better 'base set'
+                // 'costcat' -- fixme - we need to init this..
+            
 //                'terms', //?? add
                 
 //                'location', //?? just check hat one exists.
         ) : array($k);
         
+         $core_opts = HTML_FlexyFramework::get()->page->opts;
+         
+         if (!empty($core_opts['base'])) {
+            DB_DataObject::DebugLevel(1);
+            DB_DataObject::factory('curr_symbol')->importFromArray($this, array($core_opts['base']));
+            DB_DataObject::factory('curr_symbol')->setBase($this, $core_opts['base']);
+        }
+        
+        
         
         
         foreach($tables as $t) {
@@ -175,38 +235,80 @@ class Pman_Xtuple_UpdateDatabase extends Pman
         
         }
         
+        // update data is called from main
+        
+        $this->updateMetricValue();
+        
+        $this->addAccounts();
+         
+        
         
     }
     
-    function fixSequences()
+    function updateMetricValue()
     {
-        DB_DataObject::debugLevel(1);
-        $cs = DB_DataObject::factory('core_enum');
-         $cs->query("
-               SELECT  'SELECT SETVAL(' ||
-                        nspname || '.' || quote_literal(quote_ident(S.relname))||
-                        ', MAX(' || nspname || '.' || quote_ident(C.attname)|| ') )  FROM ' || nspname || '.' || quote_ident(T.relname)|| ';' as cmd 
-                FROM pg_class AS S,
-                    pg_depend AS D,
-                    pg_class AS T,
-                    pg_attribute AS C,
-                    pg_namespace AS NS
-                WHERE S.relkind = 'S'
-                    AND S.oid = D.objid
-                    AND D.refobjid = T.oid
-                    AND D.refobjid = C.attrelid
-                    AND D.refobjsubid = C.attnum
-                    AND NS.oid = T.relnamespace
-                ORDER BY S.relname;     
+        
+        $d = DB_DataObject::Factory('shipvia');
+        if($d->get('shipvia_code', 'UPS-Ground')){
+            $metric = DB_DataObject::Factory('metric');
+            $metric->query("SELECT setmetric('DefaultShipViaId', '{$d->pid()}')");
+        }
+        
+        $d = DB_DataObject::Factory('accnt');
+        if($d->get('accnt_descrip', 'Currency Gain Loss')){
+            $metric = DB_DataObject::Factory('metric');
+            $metric->query("SELECT setmetric('CurrencyGainLossAccount', '{$d->pid()}')");
+        }
+        
+        $d = DB_DataObject::Factory('salesrep');
+        $d->whereAdd("
+                salesrep_active
+            AND
+                salesrep_number IS NOT NULL
+            AND
+                salesrep_number != ''
         ");
-        while ($cs->fetch()) {
-            $cmds[] = $cs->cmd;
+        $d->orderBy('salesrep_id ASC');
+        if($d->find(true)){
+            $metric = DB_DataObject::Factory('metric');
+            $metric->query("SELECT setmetric('DefaultSalesRep', '{$d->pid()}')");
         }
         
-        print_R($cmds);
-         
+        $d = DB_DataObject::Factory('shipform');
+        $d->orderBy('shipform_id ASC');
+        if($d->find(true)){
+            $metric = DB_DataObject::Factory('metric');
+            $metric->query("SELECT setmetric('DefaultShipFormId', '{$d->pid()}')");
+        }
         
+    }
+    
+    
+    
+     
+    
+     
+    function addAccounts()
+    {
+        $ac = DB_DataObject::factory('accnt');
+        if ($ac->count()) {
+            return;
+        }
         
+        // this might want to be configurable -- so that we could use different 'base files' for the accounts..
+        require_once 'File/Convert.php';
+        $cp = $this->tempName('xls');
+        copy(dirname(__FILE__).'/Setup/accounts_sample.xls', $cp);
+        $fc = new File_Convert($cp, 'application/vnd.ms-excel');
+        //var_Dump($img->getStoreName());
+        $csv = $fc->convert('text/csv');
+        unlink($cp);
+        //var_dump($csv);
+        require_once 'Pman/Xtuple/Import/Accounts.php';
+        $ia = new Pman_Xtuple_Import_Accounts();
+        $ia->importCsv($csv);
     }
     
+   
+    
 }
\ No newline at end of file