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 (
86                 (!empty($opts['init']) && empty($opts['base']) )
87                 ||
88                 (empty($opts['init']) && !empty($opts['base']) )
89            ) {
90                     
91             die("--base=XXX  and --init =1 must be used together.");
92         }
93         // default to import the terms.
94         if (
95                 (!empty($opts['init']) && empty($opts['json-terms']) ))
96         {
97             HTML_FlexyFramework::get()->page->opts['json-terms'] = dirname(__FILE__).'/Setup/terms.json';
98             
99         }
100        
101         
102          
103     }
104     
105     
106     function get($k = '',$opts)
107     {
108         $k = strtolower($k);
109         
110         $this->opts = $opts;
111         
112         require_once 'Pman/Core/UpdateDatabase.php';
113         
114         Pman_Core_UpdateDatabase::jsonImportFromArray($opts);
115           
116         $this->updateData($k);
117         
118     }
119     
120     
121     
122     
123     function updateData($k='') {
124         //DB_DataObject::debugLevel(1);
125         $tables = empty($k) ? array(
126                 
127 //                'curr_symbol', //?? fixme
128                 'custtype',
129                 'plancode',
130                 'prodcat',
131                 'classcode',
132                 'curr_rate',
133                 'char',
134                 'taxzone',
135                 'taxtype',
136                 'expcat',
137                 'salesrep',
138                 // accounts - fixme - we need a better 'base set'
139                 // 'costcat' -- fixme - we need to init this..
140             
141 //                'terms', //?? add
142                 
143 //                'location', //?? just check hat one exists.
144         ) : array($k);
145         
146         
147         $core_opts = HTML_FlexyFramework::get()->page->opts;
148         if (!empty($core_opts['init'])) {
149             DB_DataObject::factory('curr_symbol')->setBase($this, $core_opts['base']);
150         }
151         
152         
153         
154         
155         foreach($tables as $t) {
156             $cs = DB_DataObject::factory($t);
157             $cs->initDatabase($this);
158         
159         }
160         
161         // update data is called from main
162         
163         $this->updateMetricValue();
164         
165     }
166     
167     function updateMetricValue()
168     {
169         
170         $d = DB_DataObject::Factory('custtype');
171         if($d->get('custtype_code', 'NORMAL')){
172             $metric = DB_DataObject::Factory('metric');
173             $metric->query("SELECT setmetric('DefaultCustType', '{$d->pid()}')");
174         }
175         
176         $d = DB_DataObject::Factory('salesrep');
177         $d->whereAdd("
178                 salesrep_active
179             AND
180                 salesrep_number IS NOT NULL
181         ");
182         $d->orderBy('salesrep_id ASC');
183         
184         if($d->find(true)){
185             $metric = DB_DataObject::Factory('metric');
186             $metric->query("SELECT setmetric('DefaultSalesRep', '{$d->pid()}')");
187         }
188         
189         $d = DB_DataObject::Factory('shipform');
190         $d->orderBy('shipform_id ASC');
191         if($d->find(true)){
192             $metric = DB_DataObject::Factory('metric');
193             $metric->query("SELECT setmetric('DefaultShipFormId', '{$d->pid()}')");
194         }
195         
196     }
197     
198     
199 }