array( 'desc' => 'Source directory for json files.', 'short' => 'f', 'default' => '', 'min' => 1, 'max' => 1, ), 'base' => array( 'desc' => 'Base Currency', 'default' => '', 'short' => 'b', 'min' => 1, 'max' => 1, ), '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, ), ); var $cli = false; function getAuth() { $ff = HTML_FlexyFramework::get(); if (!empty($ff->cli)) { $this->cli = true; return true; } die("NOT ALLOWED"); } // called to ensure options are correct. // before any data is imported / started.. function checkOpts($opts) { $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'; } } function get($k = '',$opts) { $k = strtolower($k); $this->opts = $opts; 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 init has been called // look in pgsql.ini -- This creates all the tables.... 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"); // ?? use dbscripts/misc/postbooks_empty.backup ??? $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.init"); } /* // let's see if this works.. -- not very flexible... - hardcoding schema... $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/440_schema.sql"); 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 // 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.. $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}"); 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"); $this->{'fixSequences'. $dbtype}(); } } 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( // 'curr_symbol', //?? fixme 'custtype', 'plancode', 'prodcat', 'classcode', 'curr_rate', 'char', '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['init'])) { DB_DataObject::factory('curr_symbol')->setBase($this, $core_opts['base']); } foreach($tables as $t) { $cs = DB_DataObject::factory($t); $cs->initDatabase($this); } // update data is called from main $this->updateMetricValue(); $this->addAccounts(); $this->addBankAccounts(); } function updateMetricValue() { $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 != '' "); $d->orderBy('salesrep_id ASC'); if($d->find(true)){ $metric = DB_DataObject::Factory('metric'); $metric->query("SELECT setmetric('DefaultSalesRep', '{$d->pid()}')"); } $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 updateData($k='') { } */ function addAccounts() { $ac = DB_DataObject::factory('accnt'); if ($ac->count()) { return; } 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); } function addBankAccounts() { $accnt = DB_DataObject::factory('subaccnt'); if (!$accnt->get('subaccnt_number', '01')) { echo "SKIP Bank account creation - no subaccnt - 01\n"; return; } $bankaccnt = DB_DataObject::factory('bankaccnt'); $bankaccnt->whereAdd(" bankaccnt_name != '' "); // do we have any bank accounts.. with names... if($bankaccnt->count()){ return; } $bankaccnt = DB_DataObject::factory('bankaccnt'); if(!$bankaccnt->get('bankaccnt_name', 'Current Account')){ // create a standard account. $accnt = DB_DataObject::factory('accnt'); $accnt->setFrom(array( 'accnt_number' => '131', 'accnt_descrip' => 'Current Account', 'accnt_comments' => '', 'accnt_profit' => '01', 'accnt_sub' => '01', 'accnt_type' => 'A', 'accnt_extref' => '', 'accnt_closedpost' => false, 'accnt_forwardupdate' => false, 'accnt_subaccnttype_code' => 'CA', 'accnt_curr_id' => $accnt->sqlValue("baseCurrId()"), 'accnt_active' => true )); if(!$accnt->find(true)){ $accnt->insert(); } $bankaccnt->setFrom(array( 'bankaccnt_name' => 'Current Account', 'bankaccnt_descrip' => 'Current Account', 'bankaccnt_bankname' => 'Current Account', 'bankaccnt_accntnumber' => 'N/A', 'bankaccnt_ar' => true, 'bankaccnt_ap' => true, 'bankaccnt_nextchknum' => 5000, 'bankaccnt_type' => 'K', 'bankaccnt_accnt_id' => $accnt->pid(), 'bankaccnt_curr_id' => $bankaccnt->sqlValue("baseCurrId()"), 'bankaccnt_notes' => '', 'bankaccnt_routing' => '', 'bankaccnt_ach_enabled' => false, 'bankaccnt_ach_origintype' => 'I' )); $bankaccnt->insert(); } $bankaccnt = DB_DataObject::factory('bankaccnt'); if(!$bankaccnt->get('bankaccnt_name', 'Savings Account')){ $accnt = DB_DataObject::factory('accnt'); $accnt->setFrom(array( 'accnt_number' => '404', 'accnt_descrip' => 'Savings Account', 'accnt_comments' => '', 'accnt_profit' => '01', 'accnt_sub' => '01', 'accnt_type' => 'A', 'accnt_extref' => '', 'accnt_closedpost' => false, 'accnt_forwardupdate' => false, 'accnt_subaccnttype_code' => 'CA', 'accnt_curr_id' => $accnt->sqlValue("baseCurrId()"), 'accnt_active' => true )); if(!$accnt->find(true)){ $accnt->insert(); } $bankaccnt->setFrom(array( 'bankaccnt_name' => 'Savings Account', 'bankaccnt_descrip' => 'Savings Account', 'bankaccnt_bankname' => 'Savings Account', 'bankaccnt_accntnumber' => 'N/A', 'bankaccnt_ar' => true, 'bankaccnt_ap' => true, 'bankaccnt_nextchknum' => 5000, 'bankaccnt_type' => 'K', 'bankaccnt_accnt_id' => $accnt->pid(), 'bankaccnt_curr_id' => $bankaccnt->sqlValue("baseCurrId()"), 'bankaccnt_notes' => '', 'bankaccnt_routing' => '', 'bankaccnt_ach_enabled' => false, 'bankaccnt_ach_origintype' => 'I' )); $bankaccnt->insert(); } } } }