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         
52         
53     );
54     
55     var $cli = false; 
56     function getAuth() {
57         
58         
59         $ff = HTML_FlexyFramework::get();
60         if (!empty($ff->cli)) {
61             $this->cli = true;
62             return true;
63         }
64         die("NOT ALLOWED");
65     }
66     
67     // called to ensure options are correct.
68     // before any data is imported / started..
69     function checkOpts()
70     {
71         $core_opts = HTML_FlexyFramework::get()->page->opts;
72         if (!empty($core_opts['init']) && empty($core_opts['base'])) {
73             die("--base=XXX  must be set when using --init ");
74         }
75         
76         
77         
78     }
79     
80     
81     function get($k = '',$opt)
82     {
83         $k = strtolower($k);
84         
85         $this->opts = $opt;
86         
87         
88         
89         
90         
91         if($k == 'accnt'){
92             if(empty($opt['source'])){
93                 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
94             }
95             
96             if (!file_exists($opt['source'])) {
97                 die("can not found account json file : {$opt['source']} \n");
98             }
99
100             $accounts = json_decode(file_get_contents($opt['source']),true);
101             
102             DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
103             
104             die("DONE! \n");
105         }
106         
107         if($k == 'curr_symbol'){
108             if(!empty($opt['base'])){
109                 DB_DataObject::factory('curr_symbol')->setBase($this, $opt['base']);
110                 die("DONE! \n");
111             }
112             
113             if(!empty($opt['source'])){
114                 
115                 if (!file_exists($opt['source'])) {
116                     die("can not found currency json file : {$opt['source']} \n");
117                 }
118                 
119                 $currencies = json_decode(file_get_contents($opt['source']),true);
120                 
121                 DB_DataObject::factory('curr_symbol')->importFromArray($this, $currencies);
122                 die("DONE! \n");
123             }
124             
125             die("Missing Base Currency or Source directory for Currency json files! Try -b [base currency] or -f [JSON file path]\n");
126         }
127         
128         if($k == 'location'){
129             if(empty($opt['source'])){
130                 die("Missing Source directory for location json file! Try -f [JSON file path] \n");
131             }
132             
133             if (!file_exists($opt['source'])) {
134                 die("can not found location json file : {$opt['source']} \n");
135             }
136             
137             $locations = json_decode(file_get_contents($opt['source']),true);
138             
139             DB_DataObject::factory('location')->importFromArray($this, $locations);
140             
141             die("DONE! \n");
142         }
143         
144         if($k == 'terms'){
145             if(empty($opt['source'])){
146                 die("Missing Source directory for terms json file! Try -f [JSON file path] \n");
147             }
148             
149             if (!file_exists($opt['source'])) {
150                 die("can not found terms json file : {$opt['source']} \n");
151             }
152             
153             $terms = json_decode(file_get_contents($opt['source']),true);
154             
155             DB_DataObject::factory('terms')->importFromArray($this, $terms);
156             
157             die("DONE! \n");
158         }
159         
160         
161         $this->updateData($k);
162         
163     }
164     
165     
166     
167     
168     function updateData($k='') {
169         //DB_DataObject::debugLevel(1);
170         $tables = empty($k) ? array(
171                 
172 //                'curr_symbol', //?? fixme
173                 'custtype',
174                 'plancode',
175                 'prodcat',
176                 'classcode',
177                 'curr_rate',
178                 'char',
179                 'taxzone',
180                 'taxtype',
181                 'expcat',
182 //                'terms', //?? add
183                 
184 //                'location', //?? just check hat one exists.
185         ) : array($k);
186         
187         
188         $core_opts = HTML_FlexyFramework::get()->page->opts;
189         if (!empty($core_opts['init'])) {
190             DB_DataObject::factory('curr_symbol')->setBase($this, $core_opt['base']);
191         }
192         
193         
194         
195         
196         foreach($tables as $t) {
197             $cs = DB_DataObject::factory($t);
198             $cs->initDatabase($this);
199         
200         }
201         
202         // update data is called from main
203         
204         
205         
206         
207     }
208     
209     
210     
211 }