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)
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 $this->{'fixSequences'. $dbtype}();
186 function updateData($k='')
188 $ff = HTML_Flexyframework::get();
189 $dburl = parse_url($ff->DB_DataObject['database']);
192 // $this->importpgsqldir($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/empty_data.sql");
194 //DB_DataObject::debugLevel(1);
195 $tables = empty($k) ? array(
197 // 'curr_symbol', //?? fixme
208 'custinfo', // create a base customer based on company.
209 'location', // default warehouse.
210 'period', // adds years and months -1 to +10 from now.
212 // accounts - fixme - we need a better 'base set'
213 // 'costcat' -- fixme - we need to init this..
217 // 'location', //?? just check hat one exists.
221 $core_opts = HTML_FlexyFramework::get()->page->opts;
222 if (!empty($core_opts['init'])) {
223 DB_DataObject::factory('curr_symbol')->setBase($this, $core_opts['base']);
229 foreach($tables as $t) {
230 $cs = DB_DataObject::factory($t);
231 $cs->initDatabase($this);
235 // update data is called from main
237 $this->updateMetricValue();
239 $this->addExtraYears();
240 $this->addAccounts();
241 $this->addBankAccounts();
247 function updateMetricValue()
250 $d = DB_DataObject::Factory('shipvia');
251 if($d->get('shipvia_code', 'UPS-Ground')){
252 $metric = DB_DataObject::Factory('metric');
253 $metric->query("SELECT setmetric('DefaultShipViaId', '{$d->pid()}')");
256 $d = DB_DataObject::Factory('accnt');
257 if($d->get('accnt_descrip', 'Currency Gain Loss')){
258 $metric = DB_DataObject::Factory('metric');
259 $metric->query("SELECT setmetric('CurrencyGainLossAccount', '{$d->pid()}')");
262 $d = DB_DataObject::Factory('salesrep');
266 salesrep_number IS NOT NULL
268 salesrep_number != ''
270 $d->orderBy('salesrep_id ASC');
272 $metric = DB_DataObject::Factory('metric');
273 $metric->query("SELECT setmetric('DefaultSalesRep', '{$d->pid()}')");
276 $d = DB_DataObject::Factory('shipform');
277 $d->orderBy('shipform_id ASC');
279 $metric = DB_DataObject::Factory('metric');
280 $metric->query("SELECT setmetric('DefaultShipFormId', '{$d->pid()}')");
300 function updateData($k='')
309 function addAccounts()
311 $ac = DB_DataObject::factory('accnt');
316 require_once 'File/Convert.php';
317 $cp = $this->tempName('xls');
318 copy(dirname(__FILE__).'/Setup/accounts_sample.xls', $cp);
319 $fc = new File_Convert($cp, 'application/vnd.ms-excel');
320 //var_Dump($img->getStoreName());
321 $csv = $fc->convert('text/csv');
324 require_once 'Pman/Xtuple/Import/Accounts.php';
325 $ia = new Pman_Xtuple_Import_Accounts();
326 $ia->importCsv($csv);
329 function addBankAccounts() {
331 $accnt = DB_DataObject::factory('subaccnt');
332 if (!$accnt->get('subaccnt_number', '01')) {
333 echo "SKIP Bank account creation - no subaccnt - 01\n";
337 $bankaccnt = DB_DataObject::factory('bankaccnt');
339 $bankaccnt->whereAdd("
342 // do we have any bank accounts.. with names...
343 if($bankaccnt->count()){
346 $bankaccnt = DB_DataObject::factory('bankaccnt');
348 if(!$bankaccnt->get('bankaccnt_name', 'Current Account')){
349 // create a standard account.
350 $accnt = DB_DataObject::factory('accnt');
351 $accnt->setFrom(array(
352 'accnt_number' => '131',
353 'accnt_descrip' => 'Current Account',
354 'accnt_comments' => '',
355 'accnt_profit' => '01',
358 'accnt_extref' => '',
359 'accnt_closedpost' => false,
360 'accnt_forwardupdate' => false,
361 'accnt_subaccnttype_code' => 'CA',
362 'accnt_curr_id' => $accnt->sqlValue("baseCurrId()"),
363 'accnt_active' => true
366 if(!$accnt->find(true)){
370 $bankaccnt->setFrom(array(
371 'bankaccnt_name' => 'Current Account',
372 'bankaccnt_descrip' => 'Current Account',
373 'bankaccnt_bankname' => 'Current Account',
374 'bankaccnt_accntnumber' => 'N/A',
375 'bankaccnt_ar' => true,
376 'bankaccnt_ap' => true,
377 'bankaccnt_nextchknum' => 5000,
378 'bankaccnt_type' => 'K',
379 'bankaccnt_accnt_id' => $accnt->pid(),
380 'bankaccnt_curr_id' => $bankaccnt->sqlValue("baseCurrId()"),
381 'bankaccnt_notes' => '',
382 'bankaccnt_routing' => '',
383 'bankaccnt_ach_enabled' => false,
384 'bankaccnt_ach_origintype' => 'I'
387 $bankaccnt->insert();
390 $bankaccnt = DB_DataObject::factory('bankaccnt');
392 if(!$bankaccnt->get('bankaccnt_name', 'Savings Account')){
393 $accnt = DB_DataObject::factory('accnt');
394 $accnt->setFrom(array(
395 'accnt_number' => '404',
396 'accnt_descrip' => 'Savings Account',
397 'accnt_comments' => '',
398 'accnt_profit' => '01',
401 'accnt_extref' => '',
402 'accnt_closedpost' => false,
403 'accnt_forwardupdate' => false,
404 'accnt_subaccnttype_code' => 'CA',
405 'accnt_curr_id' => $accnt->sqlValue("baseCurrId()"),
406 'accnt_active' => true
409 if(!$accnt->find(true)){
413 $bankaccnt->setFrom(array(
414 'bankaccnt_name' => 'Savings Account',
415 'bankaccnt_descrip' => 'Savings Account',
416 'bankaccnt_bankname' => 'Savings Account',
417 'bankaccnt_accntnumber' => 'N/A',
418 'bankaccnt_ar' => true,
419 'bankaccnt_ap' => true,
420 'bankaccnt_nextchknum' => 5000,
421 'bankaccnt_type' => 'K',
422 'bankaccnt_accnt_id' => $accnt->pid(),
423 'bankaccnt_curr_id' => $bankaccnt->sqlValue("baseCurrId()"),
424 'bankaccnt_notes' => '',
425 'bankaccnt_routing' => '',
426 'bankaccnt_ach_enabled' => false,
427 'bankaccnt_ach_origintype' => 'I'
430 $bankaccnt->insert();