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         '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 (!empty($opts['init']) && empty($opts['base'])) {
86             die("--base=XXX  must be set when using --init ");
87         }
88         
89         foreach($opts as $o=>$v) {
90             if (!empty(preg_match('/^json-/', $o)) {
91                 continue;
92             }
93             if (!file_exists($v)) {
94                 die("File does not exist : OPTION --{$o} = {$v} \n");
95             }
96         }
97         
98         
99     }
100     function loadFromFile($db, $file)
101     {
102         if (!file_exists($file)) {
103             die("can not found account json file :$file \n");
104         }
105
106         $accounts = json_decode(file_get_contents($opt['source']),true);
107         
108         DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
109         
110         
111         
112         die("DONE! \n");
113     }
114     }
115     
116     function get($k = '',$opt)
117     {
118         $k = strtolower($k);
119         
120         $this->opts = $opt;
121         
122         
123         foreach($this->opts as $o=>$v) {
124             if (!empty(preg_match('/^json-/', $o)) {
125                 continue;
126             }
127             $type = str_replace('_', '-', substr($o,6));
128             $this->loadFromFile($type, $v);
129         }
130         
131         
132         if($k == 'accnt'){
133             if(empty($opt['source'])){
134                 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
135             }
136             
137             if (!file_exists($opt['source'])) {
138                 die("can not found account json file : {$opt['source']} \n");
139             }
140
141             $accounts = json_decode(file_get_contents($opt['source']),true);
142             
143             DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
144             
145             die("DONE! \n");
146         }
147         
148         if($k == 'curr_symbol'){
149             if(!empty($opt['base'])){
150                 DB_DataObject::factory('curr_symbol')->setBase($this, $opt['base']);
151                 die("DONE! \n");
152             }
153             
154             if(!empty($opt['source'])){
155                 
156                 if (!file_exists($opt['source'])) {
157                     die("can not found currency json file : {$opt['source']} \n");
158                 }
159                 
160                 $currencies = json_decode(file_get_contents($opt['source']),true);
161                 
162                 DB_DataObject::factory('curr_symbol')->importFromArray($this, $currencies);
163                 die("DONE! \n");
164             }
165             
166             die("Missing Base Currency or Source directory for Currency json files! Try -b [base currency] or -f [JSON file path]\n");
167         }
168         
169         if($k == 'location'){
170             if(empty($opt['source'])){
171                 die("Missing Source directory for location json file! Try -f [JSON file path] \n");
172             }
173             
174             if (!file_exists($opt['source'])) {
175                 die("can not found location json file : {$opt['source']} \n");
176             }
177             
178             $locations = json_decode(file_get_contents($opt['source']),true);
179             
180             DB_DataObject::factory('location')->importFromArray($this, $locations);
181             
182             die("DONE! \n");
183         }
184         
185         if($k == 'terms'){
186             if(empty($opt['source'])){
187                 die("Missing Source directory for terms json file! Try -f [JSON file path] \n");
188             }
189             
190             if (!file_exists($opt['source'])) {
191                 die("can not found terms json file : {$opt['source']} \n");
192             }
193             
194             $terms = json_decode(file_get_contents($opt['source']),true);
195             
196             DB_DataObject::factory('terms')->importFromArray($this, $terms);
197             
198             die("DONE! \n");
199         }
200         
201         
202         $this->updateData($k);
203         
204     }
205     
206     
207     
208     
209     function updateData($k='') {
210         //DB_DataObject::debugLevel(1);
211         $tables = empty($k) ? array(
212                 
213 //                'curr_symbol', //?? fixme
214                 'custtype',
215                 'plancode',
216                 'prodcat',
217                 'classcode',
218                 'curr_rate',
219                 'char',
220                 'taxzone',
221                 'taxtype',
222                 'expcat',
223 //                'terms', //?? add
224                 
225 //                'location', //?? just check hat one exists.
226         ) : array($k);
227         
228         
229         $core_opts = HTML_FlexyFramework::get()->page->opts;
230         if (!empty($core_opts['init'])) {
231             DB_DataObject::factory('curr_symbol')->setBase($this, $core_opt['base']);
232         }
233         
234         
235         
236         
237         foreach($tables as $t) {
238             $cs = DB_DataObject::factory($t);
239             $cs->initDatabase($this);
240         
241         }
242         
243         // update data is called from main
244         
245         
246         
247         
248     }
249     
250     
251     
252 }