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
37         
38         
39     );
40     
41     var $cli = false; 
42     function getAuth() {
43         
44         
45         $ff = HTML_FlexyFramework::get();
46         if (!empty($ff->cli)) {
47             $this->cli = true;
48             return true;
49         }
50         die("NOT ALLOWED");
51     }
52     function get($k = '',$opt)
53     {
54         $k = strtolower($k);
55         
56         $this->opts = $opt;
57         
58         
59         
60         
61         
62         if($k == 'accnt'){
63             if(empty($opt['source'])){
64                 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
65             }
66             
67             if (!file_exists($opt['source'])) {
68                 die("can not found account json file : {$opt['source']} \n");
69             }
70
71             $accounts = json_decode(file_get_contents($opt['source']),true);
72             
73             DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
74             
75             die("DONE! \n");
76         }
77         
78         if($k == 'curr_symbol'){
79             if(!empty($opt['base'])){
80                 DB_DataObject::factory('curr_symbol')->setBase($this, $opt['base']);
81                 die("DONE! \n");
82             }
83             
84             if(!empty($opt['source'])){
85                 
86                 if (!file_exists($opt['source'])) {
87                     die("can not found currency json file : {$opt['source']} \n");
88                 }
89                 
90                 $currencies = json_decode(file_get_contents($opt['source']),true);
91                 
92                 DB_DataObject::factory('curr_symbol')->importFromArray($this, $currencies);
93                 die("DONE! \n");
94             }
95             
96             die("Missing Base Currency or Source directory for Currency json files! Try -b [base currency] or -f [JSON file path]\n");
97         }
98         
99         if($k == 'location'){
100             if(empty($opt['source'])){
101                 die("Missing Source directory for location json file! Try -f [JSON file path] \n");
102             }
103             
104             if (!file_exists($opt['source'])) {
105                 die("can not found location json file : {$opt['source']} \n");
106             }
107             
108             $locations = json_decode(file_get_contents($opt['source']),true);
109             
110             DB_DataObject::factory('location')->importFromArray($this, $locations);
111             
112             die("DONE! \n");
113         }
114         
115         if($k == 'terms'){
116             if(empty($opt['source'])){
117                 die("Missing Source directory for terms json file! Try -f [JSON file path] \n");
118             }
119             
120             if (!file_exists($opt['source'])) {
121                 die("can not found terms json file : {$opt['source']} \n");
122             }
123             
124             $terms = json_decode(file_get_contents($opt['source']),true);
125             
126             DB_DataObject::factory('terms')->importFromArray($this, $terms);
127             
128             die("DONE! \n");
129         }
130         
131         
132         $this->updateData($k);
133         
134     }
135     
136     
137     
138     
139     function updateData($k='') {
140         //DB_DataObject::debugLevel(1);
141         $tables = empty($k) ? array(
142                 
143 //                'curr_symbol', //?? fixme
144                 'custtype',
145                 'plancode',
146                 'prodcat',
147                 'classcode',
148                 'curr_rate',
149                 'char',
150                 'taxzone',
151                 'taxtype',
152                 'expcat',
153 //                'terms', //?? add
154                 
155 //                'location', //?? just check hat one exists.
156         ) : array($k);
157         
158         
159         $core_opts = HTML_FlexyFramework::get()->page->opts;
160         if (!empty($core_opts['init'])) {
161             
162         }
163         
164         
165         foreach($tables as $t) {
166             $cs = DB_DataObject::factory($t);
167             $cs->initDatabase($this);
168         
169         }
170         
171         // update data is called from main
172         
173         
174         
175         
176     }
177     
178     
179     
180 }