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.php';
11 class Pman_Xtuple_UpdateDatabase extends Pman
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         'fix-sequences' => array(
37             'desc' => 'Run fix sequences -- for testing',
38             //'short' => 'f',
39             'default' => '',
40             'min' => 1,
41             'max' => 1,
42         ),
43
44         
45         
46     );
47     
48     var $cli = false; 
49     function getAuth() {
50         
51         
52         $ff = HTML_FlexyFramework::get();
53         if (!empty($ff->cli)) {
54             $this->cli = true;
55             return true;
56         }
57         die("NOT ALLOWED");
58     }
59     function get($k = '',$opt)
60     {
61         $k = strtolower($k);
62         
63         $this->opts = $opt;
64         
65         
66         if(!empty($opt['fix-sequences'])){
67             $this->fixSequences();
68             exit;
69         }
70         
71         
72         
73         
74         if($k == 'accnt'){
75             if(empty($opt['source'])){
76                 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
77             }
78             
79             if (!file_exists($opt['source'])) {
80                 die("can not found account json file : {$opt['source']} \n");
81             }
82
83             $accounts = json_decode(file_get_contents($opt['source']),true);
84             
85             DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
86             
87             die("DONE! \n");
88         }
89         
90         if($k == 'curr_symbol'){
91             if(!empty($opt['base'])){
92                 DB_DataObject::factory('curr_symbol')->setBase($this, $opt['base']);
93                 die("DONE! \n");
94             }
95             
96             if(!empty($opt['source'])){
97                 
98                 if (!file_exists($opt['source'])) {
99                     die("can not found currency json file : {$opt['source']} \n");
100                 }
101                 
102                 $currencies = json_decode(file_get_contents($opt['source']),true);
103                 
104                 DB_DataObject::factory('curr_symbol')->importFromArray($this, $currencies);
105                 die("DONE! \n");
106             }
107             
108             die("Missing Base Currency or Source directory for Currency json files! Try -b [base currency] or -f [JSON file path]\n");
109         }
110         
111         if($k == 'location'){
112             if(empty($opt['source'])){
113                 die("Missing Source directory for location json file! Try -f [JSON file path] \n");
114             }
115             
116             if (!file_exists($opt['source'])) {
117                 die("can not found location json file : {$opt['source']} \n");
118             }
119             
120             $locations = json_decode(file_get_contents($opt['source']),true);
121             
122             DB_DataObject::factory('location')->importFromArray($this, $locations);
123             
124             die("DONE! \n");
125         }
126         
127         if($k == 'terms'){
128             if(empty($opt['source'])){
129                 die("Missing Source directory for terms json file! Try -f [JSON file path] \n");
130             }
131             
132             if (!file_exists($opt['source'])) {
133                 die("can not found terms json file : {$opt['source']} \n");
134             }
135             
136             $terms = json_decode(file_get_contents($opt['source']),true);
137             
138             DB_DataObject::factory('terms')->importFromArray($this, $terms);
139             
140             die("DONE! \n");
141         }
142         
143         
144         $this->updateData($k);
145         
146     }
147     
148     
149     
150     
151     function updateData($k='') {
152         //DB_DataObject::debugLevel(1);
153         $tables = empty($k) ? array(
154                 
155 //                'curr_symbol', //?? fixme
156                 'custtype',
157                 'plancode',
158                 'prodcat',
159                 'classcode',
160                 'curr_rate',
161                 'char',
162                 'taxzone',
163                 'taxtype',
164                 'expcat',
165 //                'terms', //?? add
166                 
167 //                'location', //?? just check hat one exists.
168         ) : array($k);
169         
170         
171         
172         foreach($tables as $t) {
173             $cs = DB_DataObject::factory($t);
174             $cs->initDatabase($this);
175         
176         }
177         
178         
179     }
180     
181     function fixSequences()
182     {
183         DB_DataObject::debugLevel(1);
184         $cs = DB_DataObject::factory('core_enum');
185          $cs->query("
186                SELECT  'SELECT SETVAL(' ||
187                          quote_literal(quote_ident(nspname) || '.' || quote_ident(S.relname)))||
188                         ', MAX(' || quote_ident(C.attname)|| ') )  FROM ' || nspname || '.' || quote_ident(T.relname)|| ';' as cmd 
189                 FROM pg_class AS S,
190                     pg_depend AS D,
191                     pg_class AS T,
192                     pg_attribute AS C,
193                     pg_namespace AS NS
194                 WHERE S.relkind = 'S'
195                     AND S.oid = D.objid
196                     AND D.refobjid = T.oid
197                     AND D.refobjid = C.attrelid
198                     AND D.refobjsubid = C.attnum
199                     AND NS.oid = T.relnamespace
200                 ORDER BY S.relname;     
201         ");
202         while ($cs->fetch()) {
203             $cmds[] = $cs->cmd;
204         }
205         
206         print_R($cmds);
207          
208         
209         
210     }
211     
212 }