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
207 // accounts - fixme - we need a better 'base set'
208 // 'costcat' -- fixme - we need to init this..
212 // 'location', //?? just check hat one exists.
216 $core_opts = HTML_FlexyFramework::get()->page->opts;
217 if (!empty($core_opts['init'])) {
218 DB_DataObject::factory('curr_symbol')->setBase($this, $core_opts['base']);
224 foreach($tables as $t) {
225 $cs = DB_DataObject::factory($t);
226 $cs->initDatabase($this);
230 // update data is called from main
232 $this->updateMetricValue();
236 function updateMetricValue()
239 $d = DB_DataObject::Factory('shipvia');
240 if($d->get('shipvia_code', 'UPS-Ground')){
241 $metric = DB_DataObject::Factory('metric');
242 $metric->query("SELECT setmetric('DefaultShipViaId', '{$d->pid()}')");
245 $d = DB_DataObject::Factory('accnt');
246 if($d->get('accnt_descrip', 'Currency Gain Loss')){
247 $metric = DB_DataObject::Factory('metric');
248 $metric->query("SELECT setmetric('CurrencyGainLossAccount', '{$d->pid()}')");
251 $d = DB_DataObject::Factory('salesrep');
255 salesrep_number IS NOT NULL
257 salesrep_number != ''
259 $d->orderBy('salesrep_id ASC');
261 $metric = DB_DataObject::Factory('metric');
262 $metric->query("SELECT setmetric('DefaultSalesRep', '{$d->pid()}')");
265 $d = DB_DataObject::Factory('shipform');
266 $d->orderBy('shipform_id ASC');
268 $metric = DB_DataObject::Factory('metric');
269 $metric->query("SELECT setmetric('DefaultShipFormId', '{$d->pid()}')");
289 function updateData($k='')
291 $uom = DB_DataObject::factory('uom');
293 'uom_name' => 'YEAR',
294 'uom_descrip' => 'YEAR'
297 if(!$uom->find(true)){
300 $this->addWarehouse();
301 $this->addExtraYears();
302 $this->addAccounts();
303 $this->addBankAccounts();
307 function addWarehouse()
309 $l = DB_DataObject::Factory('location');
311 echo "location exists - SKIP\n";
314 $cust = DB_DataObject::Factory('custinfo');
315 $cc =$cust->count() ;
317 echo "location create - needs 1 custinfo - got $cc\n";
323 'location_name' => 'Warehouse',
324 'location_descrip' => 'Warehouse',
325 'location_cust_id' => $cust->pid()
331 function addExtraYears()
333 $p = DB_DataObject::Factory('period');
335 echo "SKIP - already have periods\n";
338 $p->initPeriods($this);
339 $p->initPeriods($this);
340 $p->initPeriods($this);
341 $p->initPeriods($this);
344 function addAccounts()
346 require_once 'File/Convert.php';
347 $cp = $this->tempName('xls');
348 copy(dirname(__FILE__).'/../Xtuple/Setup/accounts_sample.xls', $cp);
349 $fc = new File_Convert($cp, 'application/vnd.ms-excel');
350 //var_Dump($img->getStoreName());
351 $csv = $fc->convert('text/csv');
354 require_once 'Pman/Xtuple/Import/Accounts.php';
355 $ia = new Pman_Xtuple_Import_Accounts();
356 $ia->importCsv($csv);
359 function addBankAccounts() {
361 $accnt = DB_DataObject::factory('subaccnt');
362 if (!$accnt->get('subaccnt_number', '01')) {
363 echo "SKIP Bank account creation - no subaccnt - 01\n";
367 $bankaccnt = DB_DataObject::factory('bankaccnt');
369 $bankaccnt->whereAdd("
373 if(!$bankaccnt->count()){
374 $bankaccnt = DB_DataObject::factory('bankaccnt');
376 if(!$bankaccnt->get('bankaccnt_name', 'Current Account')){
377 $accnt = DB_DataObject::factory('accnt');
378 $accnt->setFrom(array(
379 'accnt_number' => '131',
380 'accnt_descrip' => 'Current Account',
381 'accnt_comments' => '',
382 'accnt_profit' => '01',
385 'accnt_extref' => '',
386 'accnt_closedpost' => false,
387 'accnt_forwardupdate' => false,
388 'accnt_subaccnttype_code' => 'CA',
389 'accnt_curr_id' => $accnt->sqlValue("baseCurrId()"),
390 'accnt_active' => true
393 if(!$accnt->find(true)){
397 $bankaccnt->setFrom(array(
398 'bankaccnt_name' => 'Current Account',
399 'bankaccnt_descrip' => 'Current Account',
400 'bankaccnt_bankname' => 'Current Account',
401 'bankaccnt_accntnumber' => 'N/A',
402 'bankaccnt_ar' => true,
403 'bankaccnt_ap' => true,
404 'bankaccnt_nextchknum' => 5000,
405 'bankaccnt_type' => 'K',
406 'bankaccnt_accnt_id' => $accnt->pid(),
407 'bankaccnt_curr_id' => $bankaccnt->sqlValue("baseCurrId()"),
408 'bankaccnt_notes' => '',
409 'bankaccnt_routing' => '',
410 'bankaccnt_ach_enabled' => false,
411 'bankaccnt_ach_origintype' => 'I'
414 $bankaccnt->insert();
417 $bankaccnt = DB_DataObject::factory('bankaccnt');
419 if(!$bankaccnt->get('bankaccnt_name', 'Savings Account')){
420 $accnt = DB_DataObject::factory('accnt');
421 $accnt->setFrom(array(
422 'accnt_number' => '404',
423 'accnt_descrip' => 'Savings Account',
424 'accnt_comments' => '',
425 'accnt_profit' => '01',
428 'accnt_extref' => '',
429 'accnt_closedpost' => false,
430 'accnt_forwardupdate' => false,
431 'accnt_subaccnttype_code' => 'CA',
432 'accnt_curr_id' => $accnt->sqlValue("baseCurrId()"),
433 'accnt_active' => true
436 if(!$accnt->find(true)){
440 $bankaccnt->setFrom(array(
441 'bankaccnt_name' => 'Savings Account',
442 'bankaccnt_descrip' => 'Savings Account',
443 'bankaccnt_bankname' => 'Savings Account',
444 'bankaccnt_accntnumber' => 'N/A',
445 'bankaccnt_ar' => true,
446 'bankaccnt_ap' => true,
447 'bankaccnt_nextchknum' => 5000,
448 'bankaccnt_type' => 'K',
449 'bankaccnt_accnt_id' => $accnt->pid(),
450 'bankaccnt_curr_id' => $bankaccnt->sqlValue("baseCurrId()"),
451 'bankaccnt_notes' => '',
452 'bankaccnt_routing' => '',
453 'bankaccnt_ach_enabled' => false,
454 'bankaccnt_ach_origintype' => 'I'
457 $bankaccnt->insert();