5 * Setup the core values in the database
10 require_once 'Pman/Core/UpdateDatabase.php';
11 class Pman_Xtuple_UpdateDatabase extends Pman_Core_UpdateDatabase
14 static $cli_desc = "Update SQL - Beta";
16 static $cli_opts = array(
18 'desc' => 'Source directory for json files.',
28 'desc' => 'Base Currency',
36 'json-accnt' => array(
37 'desc' => 'Accounts JSON file',
43 'json-curr-symbol' => array(
44 'desc' => 'Currency Symbols JSON file',
51 'json-location' => array(
52 'desc' => 'Location JSON file',
58 'json-terms' => array(
59 'desc' => 'Terms JSON file',
72 $ff = HTML_FlexyFramework::get();
73 if (!empty($ff->cli)) {
80 // called to ensure options are correct.
81 // before any data is imported / started..
82 function checkOpts($opts)
84 $opts = HTML_FlexyFramework::get()->page->opts;
86 (!empty($opts['init']) && empty($opts['base']) )
88 (empty($opts['init']) && !empty($opts['base']) )
91 die("--base=XXX and --init =1 must be used together.");
93 // default to import the terms.
95 (!empty($opts['init']) && empty($opts['json-terms']) ))
97 HTML_FlexyFramework::get()->page->opts['json-terms'] = dirname(__FILE__).'/Setup/terms.json';
106 function get($k = '',$opts=array())
112 require_once 'Pman/Core/UpdateDatabase.php';
114 Pman_Core_UpdateDatabase::jsonImportFromArray($opts);
116 $this->updateData($k);
120 function importModuleSQL($dburl)
123 $dbtype = $dburl['scheme'];
124 $dirmethod = 'import' . $dburl['scheme'] . 'dir';
127 echo "Xtyple:Importing SQL\n";
130 // if init has been called
131 // look in pgsql.ini -- This creates all the tables....
133 if (!empty($this->opts['init'])) {
134 // it should really use xtuple... but it's not very well organized...
135 //$this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.init");
137 // ?? use dbscripts/misc/postbooks_empty.backup ???
139 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.init");
143 // let's see if this works.. -- not very flexible... - hardcoding schema...
144 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/440_schema.sql");
151 'public/trigger_functions',
158 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/" . $dir);
161 // first try the standard from xtuple-database
164 // new -- sql directory..
165 // new style will not support migrate ... they have to go into mysql-migrate.... directories..
166 // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
171 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}");
175 if (!empty($this->opts['init']) && file_exists($this->rootDir. "/Pman/$m/{$dbtype}.initdata")) {
176 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
178 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.initdata");
179 // fixSequences does nto appar to work on postgres anymore
180 //$this->{'fixSequences'. $dbtype}();
187 function updateData($k='')
189 $ff = HTML_Flexyframework::get();
190 $dburl = parse_url($ff->DB_DataObject['database']);
193 // $this->importpgsqldir($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/empty_data.sql");
195 //DB_DataObject::debugLevel(1);
196 $tables = empty($k) ? array(
198 // 'curr_symbol', //?? fixme
209 'custinfo', // create a base customer based on company.
210 'location', // default warehouse.
211 'period', // adds years and months -1 to +10 from now.
213 // accounts - fixme - we need a better 'base set'
214 // 'costcat' -- fixme - we need to init this..
218 // 'location', //?? just check hat one exists.
221 $core_opts = HTML_FlexyFramework::get()->page->opts;
223 if (!empty($core_opts['base'])) {
224 DB_DataObject::DebugLevel(1);
225 DB_DataObject::factory('curr_symbol')->importFromArray($this, array($core_opts['base']));
226 DB_DataObject::factory('curr_symbol')->setBase($this, $core_opts['base']);
232 foreach($tables as $t) {
233 $cs = DB_DataObject::factory($t);
234 $cs->initDatabase($this);
238 // update data is called from main
240 $this->updateMetricValue();
242 $this->addAccounts();
248 function updateMetricValue()
251 $d = DB_DataObject::Factory('shipvia');
252 if($d->get('shipvia_code', 'UPS-Ground')){
253 $metric = DB_DataObject::Factory('metric');
254 $metric->query("SELECT setmetric('DefaultShipViaId', '{$d->pid()}')");
257 $d = DB_DataObject::Factory('accnt');
258 if($d->get('accnt_descrip', 'Currency Gain Loss')){
259 $metric = DB_DataObject::Factory('metric');
260 $metric->query("SELECT setmetric('CurrencyGainLossAccount', '{$d->pid()}')");
263 $d = DB_DataObject::Factory('salesrep');
267 salesrep_number IS NOT NULL
269 salesrep_number != ''
271 $d->orderBy('salesrep_id ASC');
273 $metric = DB_DataObject::Factory('metric');
274 $metric->query("SELECT setmetric('DefaultSalesRep', '{$d->pid()}')");
277 $d = DB_DataObject::Factory('shipform');
278 $d->orderBy('shipform_id ASC');
280 $metric = DB_DataObject::Factory('metric');
281 $metric->query("SELECT setmetric('DefaultShipFormId', '{$d->pid()}')");
291 function addAccounts()
293 $ac = DB_DataObject::factory('accnt');
298 // this might want to be configurable -- so that we could use different 'base files' for the accounts..
299 require_once 'File/Convert.php';
300 $cp = $this->tempName('xls');
301 copy(dirname(__FILE__).'/Setup/accounts_sample.xls', $cp);
302 $fc = new File_Convert($cp, 'application/vnd.ms-excel');
303 //var_Dump($img->getStoreName());
304 $csv = $fc->convert('text/csv');
307 require_once 'Pman/Xtuple/Import/Accounts.php';
308 $ia = new Pman_Xtuple_Import_Accounts();
309 $ia->importCsv($csv);