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 // accounts - fixme - we need a better 'base set'
210 // 'costcat' -- fixme - we need to init this..
214 // 'location', //?? just check hat one exists.
218 $core_opts = HTML_FlexyFramework::get()->page->opts;
219 if (!empty($core_opts['init'])) {
220 DB_DataObject::factory('curr_symbol')->setBase($this, $core_opts['base']);
226 foreach($tables as $t) {
227 $cs = DB_DataObject::factory($t);
228 $cs->initDatabase($this);
232 // update data is called from main
234 $this->updateMetricValue();
236 $this->addWarehouse();
237 $this->addExtraYears();
238 $this->addAccounts();
239 $this->addBankAccounts();
245 function updateMetricValue()
248 $d = DB_DataObject::Factory('shipvia');
249 if($d->get('shipvia_code', 'UPS-Ground')){
250 $metric = DB_DataObject::Factory('metric');
251 $metric->query("SELECT setmetric('DefaultShipViaId', '{$d->pid()}')");
254 $d = DB_DataObject::Factory('accnt');
255 if($d->get('accnt_descrip', 'Currency Gain Loss')){
256 $metric = DB_DataObject::Factory('metric');
257 $metric->query("SELECT setmetric('CurrencyGainLossAccount', '{$d->pid()}')");
260 $d = DB_DataObject::Factory('salesrep');
264 salesrep_number IS NOT NULL
266 salesrep_number != ''
268 $d->orderBy('salesrep_id ASC');
270 $metric = DB_DataObject::Factory('metric');
271 $metric->query("SELECT setmetric('DefaultSalesRep', '{$d->pid()}')");
274 $d = DB_DataObject::Factory('shipform');
275 $d->orderBy('shipform_id ASC');
277 $metric = DB_DataObject::Factory('metric');
278 $metric->query("SELECT setmetric('DefaultShipFormId', '{$d->pid()}')");
298 function updateData($k='')
305 function addWarehouse()
307 $l = DB_DataObject::Factory('location');
309 echo "location exists - SKIP\n";
312 $cust = DB_DataObject::Factory('custinfo');
313 $cc =$cust->count() ;
315 echo "location create - needs 1 custinfo - got $cc\n";
321 'location_name' => 'Warehouse',
322 'location_descrip' => 'Warehouse',
323 'location_cust_id' => $cust->pid()
329 function addExtraYears()
331 $p = DB_DataObject::Factory('period');
333 echo "SKIP - already have periods\n";
336 $p->initPeriods($this);
337 $p->initPeriods($this);
338 $p->initPeriods($this);
339 $p->initPeriods($this);
342 function addAccounts()
344 $ac = DB_DataObject::factory('accnt');
349 require_once 'File/Convert.php';
350 $cp = $this->tempName('xls');
351 copy(dirname(__FILE__).'/Setup/accounts_sample.xls', $cp);
352 $fc = new File_Convert($cp, 'application/vnd.ms-excel');
353 //var_Dump($img->getStoreName());
354 $csv = $fc->convert('text/csv');
357 require_once 'Pman/Xtuple/Import/Accounts.php';
358 $ia = new Pman_Xtuple_Import_Accounts();
359 $ia->importCsv($csv);
362 function addBankAccounts() {
364 $accnt = DB_DataObject::factory('subaccnt');
365 if (!$accnt->get('subaccnt_number', '01')) {
366 echo "SKIP Bank account creation - no subaccnt - 01\n";
370 $bankaccnt = DB_DataObject::factory('bankaccnt');
372 $bankaccnt->whereAdd("
375 // do we have any bank accounts.. with names...
376 if($bankaccnt->count()){
379 $bankaccnt = DB_DataObject::factory('bankaccnt');
381 if(!$bankaccnt->get('bankaccnt_name', 'Current Account')){
382 // create a standard account.
383 $accnt = DB_DataObject::factory('accnt');
384 $accnt->setFrom(array(
385 'accnt_number' => '131',
386 'accnt_descrip' => 'Current Account',
387 'accnt_comments' => '',
388 'accnt_profit' => '01',
391 'accnt_extref' => '',
392 'accnt_closedpost' => false,
393 'accnt_forwardupdate' => false,
394 'accnt_subaccnttype_code' => 'CA',
395 'accnt_curr_id' => $accnt->sqlValue("baseCurrId()"),
396 'accnt_active' => true
399 if(!$accnt->find(true)){
403 $bankaccnt->setFrom(array(
404 'bankaccnt_name' => 'Current Account',
405 'bankaccnt_descrip' => 'Current Account',
406 'bankaccnt_bankname' => 'Current Account',
407 'bankaccnt_accntnumber' => 'N/A',
408 'bankaccnt_ar' => true,
409 'bankaccnt_ap' => true,
410 'bankaccnt_nextchknum' => 5000,
411 'bankaccnt_type' => 'K',
412 'bankaccnt_accnt_id' => $accnt->pid(),
413 'bankaccnt_curr_id' => $bankaccnt->sqlValue("baseCurrId()"),
414 'bankaccnt_notes' => '',
415 'bankaccnt_routing' => '',
416 'bankaccnt_ach_enabled' => false,
417 'bankaccnt_ach_origintype' => 'I'
420 $bankaccnt->insert();
423 $bankaccnt = DB_DataObject::factory('bankaccnt');
425 if(!$bankaccnt->get('bankaccnt_name', 'Savings Account')){
426 $accnt = DB_DataObject::factory('accnt');
427 $accnt->setFrom(array(
428 'accnt_number' => '404',
429 'accnt_descrip' => 'Savings Account',
430 'accnt_comments' => '',
431 'accnt_profit' => '01',
434 'accnt_extref' => '',
435 'accnt_closedpost' => false,
436 'accnt_forwardupdate' => false,
437 'accnt_subaccnttype_code' => 'CA',
438 'accnt_curr_id' => $accnt->sqlValue("baseCurrId()"),
439 'accnt_active' => true
442 if(!$accnt->find(true)){
446 $bankaccnt->setFrom(array(
447 'bankaccnt_name' => 'Savings Account',
448 'bankaccnt_descrip' => 'Savings Account',
449 'bankaccnt_bankname' => 'Savings Account',
450 'bankaccnt_accntnumber' => 'N/A',
451 'bankaccnt_ar' => true,
452 'bankaccnt_ap' => true,
453 'bankaccnt_nextchknum' => 5000,
454 'bankaccnt_type' => 'K',
455 'bankaccnt_accnt_id' => $accnt->pid(),
456 'bankaccnt_curr_id' => $bankaccnt->sqlValue("baseCurrId()"),
457 'bankaccnt_notes' => '',
458 'bankaccnt_routing' => '',
459 'bankaccnt_ach_enabled' => false,
460 'bankaccnt_ach_origintype' => 'I'
463 $bankaccnt->insert();