UpdateDatabase.php
[Pman.Xtuple] / UpdateDatabase.php
1 <?php
2
3 /**
4  *
5  * Setup the core values in the database
6  *
7  *
8  */
9
10 require_once 'Pman/Core/UpdateDatabase.php';
11 class Pman_Xtuple_UpdateDatabase extends Pman_Core_UpdateDatabase
12 {
13     
14     static $cli_desc = "Update SQL - Beta";
15     
16     static $cli_opts = array(
17         'source' => array(
18             'desc' => 'Source directory for json files.',
19             'short' => 'f',
20             'default' => '',
21             'min' => 1,
22             'max' => 1,
23         ),
24         
25         
26         
27         'base' => array(
28             'desc' => 'Base Currency',
29             'default' => '',
30             'short' => 'b',
31             'min' => 1,
32             'max' => 1,
33             
34         ),
35  
36         'json-accnt' => array(
37             'desc' => 'Accounts JSON file',
38             'default' => '',
39             'min' => 1,
40             'max' => 1,
41             
42         ),
43         'json-curr-symbol' => array(
44             'desc' => 'Currency Symbols JSON file',
45             'default' => '',
46             'min' => 1,
47             'max' => 1,
48             
49         ),
50
51         'json-location' => array(
52             'desc' => 'Location JSON file',
53             'default' => '',
54             'min' => 1,
55             'max' => 1,
56             
57         ),
58         'json-terms' => array(
59             'desc' => 'Terms  JSON file',
60             'default' => '',
61             'min' => 1,
62             'max' => 1,
63             
64         ),
65         
66     );
67     
68     var $cli = false; 
69     function getAuth() {
70         
71         
72         $ff = HTML_FlexyFramework::get();
73         if (!empty($ff->cli)) {
74             $this->cli = true;
75             return true;
76         }
77         die("NOT ALLOWED");
78     }
79     
80     // called to ensure options are correct.
81     // before any data is imported / started..
82     function checkOpts($opts)
83     {
84         $opts = HTML_FlexyFramework::get()->page->opts;
85         if (
86                 (!empty($opts['init']) && empty($opts['base']) )
87                 ||
88                 (empty($opts['init']) && !empty($opts['base']) )
89            ) {
90                     
91             die("--base=XXX  and --init =1 must be used together.");
92         }
93         // default to import the terms.
94         if (
95                 (!empty($opts['init']) && empty($opts['json-terms']) ))
96         {
97             HTML_FlexyFramework::get()->page->opts['json-terms'] = dirname(__FILE__).'/Setup/terms.json';
98             
99         }
100        
101         
102          
103     }
104     
105     
106     function get($k = '',$opts)
107     {
108         $k = strtolower($k);
109         
110         $this->opts = $opts;
111         
112         require_once 'Pman/Core/UpdateDatabase.php';
113         
114         Pman_Core_UpdateDatabase::jsonImportFromArray($opts);
115           
116         $this->updateData($k);
117         
118     }
119     
120     function importModuleSQL($dburl)
121     {
122          
123             $dbtype = $dburl['scheme'];
124             $dirmethod = 'import' . $dburl['scheme'] . 'dir';
125         
126             $m = 'Xtuple';
127             echo "Xtyple:Importing SQL\n";
128         
129             
130             // if init has been called
131             // look in pgsql.ini -- This creates all the tables....
132             
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");
136                 
137                 // ?? use dbscripts/misc/postbooks_empty.backup ???
138                 
139                 //$this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.init");
140                 
141             }
142             // let's see if this works.. -- not very flexible... - hardcoding schema...
143             $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/404_schema.sql");
144             
145             foreach(array(
146                 'public/types',
147                 'public/tables',
148                 'public/views',
149                 'public/functions', 
150                 'public/trigger_functions',
151                 'public/indexes',
152                 'api/functions',
153                 'api/views',
154                 
155                 
156             ) as $dir) {
157                 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/" . $dir);   
158             }
159             
160             // first try the standard from xtuple-database
161             
162             
163             // new -- sql directory..
164             // new style will not support migrate ... they have to go into mysql-migrate.... directories..
165             // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
166             
167             
168             
169              
170             $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}");
171             
172             
173             
174             if (!empty($this->opts['init']) && file_exists($this->rootDir. "/Pman/$m/{$dbtype}.initdata")) {
175                 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
176                 
177                 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.initdata");
178                 $this->{'fixSequences'. $dbtype}();
179                 
180             }
181               
182     }
183     
184     
185     function updateData($k='')
186     {
187         $ff = HTML_Flexyframework::get();
188         $dburl = parse_url($ff->DB_DataObject['database']);
189        
190         
191         $this->importpgsqldir($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/empty_data.sql");
192         
193         //DB_DataObject::debugLevel(1);
194         $tables = empty($k) ? array(
195                 
196 //                'curr_symbol', //?? fixme
197                 'custtype',
198                 'plancode',
199                 'prodcat',
200                 'classcode',
201                 'curr_rate',
202                 'char',
203                 'taxzone',
204                 'taxtype',
205                 'expcat',
206                 // accounts - fixme - we need a better 'base set'
207                 // 'costcat' -- fixme - we need to init this..
208             
209 //                'terms', //?? add
210                 
211 //                'location', //?? just check hat one exists.
212         ) : array($k);
213         
214         
215         $core_opts = HTML_FlexyFramework::get()->page->opts;
216         if (!empty($core_opts['init'])) {
217             DB_DataObject::factory('curr_symbol')->setBase($this, $core_opts['base']);
218         }
219         
220         
221         
222         
223         foreach($tables as $t) {
224             $cs = DB_DataObject::factory($t);
225             $cs->initDatabase($this);
226         
227         }
228         
229         // update data is called from main
230         
231         $this->updateMetricValue();
232         
233     }
234     
235     function updateMetricValue()
236     {
237         
238         $d = DB_DataObject::Factory('shipvia');
239         if($d->get('shipvia_code', 'UPS-Ground')){
240             $metric = DB_DataObject::Factory('metric');
241             $metric->query("SELECT setmetric('DefaultShipViaId', '{$d->pid()}')");
242         }
243         
244         $d = DB_DataObject::Factory('accnt');
245         if($d->get('accnt_descrip', 'Currency Gain Loss')){
246             $metric = DB_DataObject::Factory('metric');
247             $metric->query("SELECT setmetric('CurrencyGainLossAccount', '{$d->pid()}')");
248         }
249         
250         $d = DB_DataObject::Factory('salesrep');
251         $d->whereAdd("
252                 salesrep_active
253             AND
254                 salesrep_number IS NOT NULL
255             AND
256                 salesrep_number != ''
257         ");
258         $d->orderBy('salesrep_id ASC');
259         if($d->find(true)){
260             $metric = DB_DataObject::Factory('metric');
261             $metric->query("SELECT setmetric('DefaultSalesRep', '{$d->pid()}')");
262         }
263         
264         $d = DB_DataObject::Factory('shipform');
265         $d->orderBy('shipform_id ASC');
266         if($d->find(true)){
267             $metric = DB_DataObject::Factory('metric');
268             $metric->query("SELECT setmetric('DefaultShipFormId', '{$d->pid()}')");
269         }
270         
271     }
272     
273     
274 }