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         if($k == 'accnt'){
61             if(empty($opt['source'])){
62                 die("Missing Source directory for account json files! Try -f [JSON file path] \n");
63             }
64             
65             if (!file_exists($opt['source'])) {
66                 die("can not found account json file : {$opt['source']} \n");
67             }
68
69             $accounts = json_decode(file_get_contents($opt['source']),true);
70             
71             DB_DataObject::factory('accnt')->importFromArray($this, $accounts);
72             
73             die("DONE! \n");
74         }
75         
76         if($k == 'curr_symbol'){
77             if(!empty($opt['base'])){
78                 DB_DataObject::factory('curr_symbol')->setBase($this, $opt['base']);
79                 die("DONE! \n");
80             }
81             
82             if(!empty($opt['source'])){
83                 
84                 if (!file_exists($opt['source'])) {
85                     die("can not found currency json file : {$opt['source']} \n");
86                 }
87                 
88                 $currencies = json_decode(file_get_contents($opt['source']),true);
89                 
90                 DB_DataObject::factory('curr_symbol')->importFromArray($this, $currencies);
91                 die("DONE! \n");
92             }
93             
94             die("Missing Base Currency or Source directory for Currency json files! Try -b [base currency] or -f [JSON file path]\n");
95         }
96         
97         if($k == 'location'){
98             if(empty($opt['source'])){
99                 die("Missing Source directory for location json file! Try -f [JSON file path] \n");
100             }
101             
102             if (!file_exists($opt['source'])) {
103                 die("can not found location json file : {$opt['source']} \n");
104             }
105             
106             $locations = json_decode(file_get_contents($opt['source']),true);
107             
108             DB_DataObject::factory('location')->importFromArray($this, $locations);
109             
110             die("DONE! \n");
111         }
112         
113         if($k == 'terms'){
114             if(empty($opt['source'])){
115                 die("Missing Source directory for terms json file! Try -f [JSON file path] \n");
116             }
117             
118             if (!file_exists($opt['source'])) {
119                 die("can not found terms json file : {$opt['source']} \n");
120             }
121             
122             $terms = json_decode(file_get_contents($opt['source']),true);
123             
124             DB_DataObject::factory('terms')->importFromArray($this, $terms);
125             
126             die("DONE! \n");
127         }
128         
129         
130         $this->updateData($k);
131         
132     }
133     
134     
135     
136     
137     function updateData($k='') {
138         //DB_DataObject::debugLevel(1);
139         $tables = empty($k) ? array(
140                 
141 //                'curr_symbol', //?? fixme
142                 'custtype',
143                 'plancode',
144                 'prodcat',
145                 'classcode',
146                 'curr_rate',
147                 'char',
148                 'taxzone',
149                 'taxtype',
150                 'expcat',
151 //                'terms', //?? add
152                 
153 //                'location', //?? just check hat one exists.
154         ) : array($k);
155         
156         
157         
158         foreach($tables as $t) {
159             $cs = DB_DataObject::factory($t);
160             $cs->initDatabase($this);
161         
162         }
163         
164         
165     }
166     
167     
168     
169 }