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