Period.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/Core/UpdateDatabase.php';
11 class Pman_Xtuple_UpdateDatabase extends Pman_Core_UpdateDatabase
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     function importModuleSQL($dburl)
121     {
122          
123             $dbtype = $dburl['scheme'];
124             $dirmethod = 'import' . $dburl['scheme'] . 'dir';
125         
126             $m = 'Xtuple';
127             echo "Xtyple:Importing SQL\n";
128         
129             
130             // if init has been called
131             // look in pgsql.ini -- This creates all the tables....
132             
133             if (!empty($this->opts['init'])) {
134                 // it should really use xtuple... but it's not very well organized...
135                 //$this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.init");
136                 
137                 // ?? use dbscripts/misc/postbooks_empty.backup ???
138                 
139                 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.init");
140                 
141             }
142             /*
143             // let's see if this works.. -- not very flexible... - hardcoding schema...
144             $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/440_schema.sql");
145             
146             foreach(array(
147                 'public/types',
148                 'public/tables',
149                 'public/views',
150                 'public/functions', 
151                 'public/trigger_functions',
152                 'public/indexes',
153                 'api/functions',
154                 'api/views',
155                 
156                 
157             ) as $dir) {
158                 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/" . $dir);   
159             }
160             */
161             // first try the standard from xtuple-database
162             
163             
164             // new -- sql directory..
165             // new style will not support migrate ... they have to go into mysql-migrate.... directories..
166             // new style will not support pg.sql etc.. naming - that's what the direcotries are for..
167             
168             
169             
170              
171             $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}");
172             
173             
174             
175             if (!empty($this->opts['init']) && file_exists($this->rootDir. "/Pman/$m/{$dbtype}.initdata")) {
176                 HTML_FlexyFramework::get()->generateDataobjectsCache(true);
177                 
178                 $this->{$dirmethod}($dburl, $this->rootDir. "/Pman/$m/{$dbtype}.initdata");
179                 $this->{'fixSequences'. $dbtype}();
180                 
181             }
182               
183     }
184     
185     
186     function updateData($k='')
187     {
188         $ff = HTML_Flexyframework::get();
189         $dburl = parse_url($ff->DB_DataObject['database']);
190        
191         
192 //        $this->importpgsqldir($dburl, $this->rootDir. "/Pman/Xtuple/xtuple-database/empty_data.sql");
193         
194         //DB_DataObject::debugLevel(1);
195         $tables = empty($k) ? array(
196                 
197 //                'curr_symbol', //?? fixme
198                 'custtype',
199                 'plancode',
200                 'prodcat',
201                 'classcode',
202                 'curr_rate',
203                 'char',
204                 'taxzone',
205                 'taxtype',
206                 'expcat',
207                 
208                 'custinfo', // create a base customer based on company.
209                 'location', // default warehouse.
210                 
211                 // accounts - fixme - we need a better 'base set'
212                 // 'costcat' -- fixme - we need to init this..
213             
214 //                'terms', //?? add
215                 
216 //                'location', //?? just check hat one exists.
217         ) : array($k);
218         
219         
220         $core_opts = HTML_FlexyFramework::get()->page->opts;
221         if (!empty($core_opts['init'])) {
222             DB_DataObject::factory('curr_symbol')->setBase($this, $core_opts['base']);
223         }
224         
225         
226         
227         
228         foreach($tables as $t) {
229             $cs = DB_DataObject::factory($t);
230             $cs->initDatabase($this);
231         
232         }
233         
234         // update data is called from main
235         
236         $this->updateMetricValue();
237         
238          $this->addExtraYears();
239         $this->addAccounts();
240         $this->addBankAccounts();
241         
242         
243         
244     }
245     
246     function updateMetricValue()
247     {
248         
249         $d = DB_DataObject::Factory('shipvia');
250         if($d->get('shipvia_code', 'UPS-Ground')){
251             $metric = DB_DataObject::Factory('metric');
252             $metric->query("SELECT setmetric('DefaultShipViaId', '{$d->pid()}')");
253         }
254         
255         $d = DB_DataObject::Factory('accnt');
256         if($d->get('accnt_descrip', 'Currency Gain Loss')){
257             $metric = DB_DataObject::Factory('metric');
258             $metric->query("SELECT setmetric('CurrencyGainLossAccount', '{$d->pid()}')");
259         }
260         
261         $d = DB_DataObject::Factory('salesrep');
262         $d->whereAdd("
263                 salesrep_active
264             AND
265                 salesrep_number IS NOT NULL
266             AND
267                 salesrep_number != ''
268         ");
269         $d->orderBy('salesrep_id ASC');
270         if($d->find(true)){
271             $metric = DB_DataObject::Factory('metric');
272             $metric->query("SELECT setmetric('DefaultSalesRep', '{$d->pid()}')");
273         }
274         
275         $d = DB_DataObject::Factory('shipform');
276         $d->orderBy('shipform_id ASC');
277         if($d->find(true)){
278             $metric = DB_DataObject::Factory('metric');
279             $metric->query("SELECT setmetric('DefaultShipFormId', '{$d->pid()}')");
280         }
281         
282     }
283     
284     
285     
286     
287     
288     
289     
290     
291     
292     
293     
294     
295     
296     
297     /*
298     
299     function updateData($k='')
300     {
301         
302         
303     }
304     */
305      
306     
307     
308     function addExtraYears()
309     {
310         $p = DB_DataObject::Factory('period');
311         if ($p->count()) {
312             echo "SKIP - already have periods\n";
313             return;
314         }
315         $p->initPeriods($this);
316         $p->initPeriods($this);
317         $p->initPeriods($this);
318         $p->initPeriods($this);
319         
320     }
321     function addAccounts()
322     {
323         $ac = DB_DataObject::factory('accnt');
324         if ($ac->count()) {
325             return;
326         }
327         
328         require_once 'File/Convert.php';
329         $cp = $this->tempName('xls');
330         copy(dirname(__FILE__).'/Setup/accounts_sample.xls', $cp);
331         $fc = new File_Convert($cp, 'application/vnd.ms-excel');
332         //var_Dump($img->getStoreName());
333         $csv = $fc->convert('text/csv');
334         unlink($cp);
335         //var_dump($csv);
336         require_once 'Pman/Xtuple/Import/Accounts.php';
337         $ia = new Pman_Xtuple_Import_Accounts();
338         $ia->importCsv($csv);
339     }
340     
341     function addBankAccounts() {
342         
343         $accnt = DB_DataObject::factory('subaccnt');
344         if (!$accnt->get('subaccnt_number', '01')) {
345             echo "SKIP Bank account creation - no subaccnt - 01\n";
346             return;
347         }
348         
349         $bankaccnt = DB_DataObject::factory('bankaccnt');
350         
351         $bankaccnt->whereAdd("
352             bankaccnt_name != ''
353         ");
354         // do we have any bank accounts.. with names...
355         if($bankaccnt->count()){
356             return;
357         }
358         $bankaccnt = DB_DataObject::factory('bankaccnt');
359     
360         if(!$bankaccnt->get('bankaccnt_name', 'Current Account')){
361             // create a standard account.
362             $accnt = DB_DataObject::factory('accnt');
363             $accnt->setFrom(array(
364                 'accnt_number' => '131',
365                 'accnt_descrip' => 'Current Account',
366                 'accnt_comments' => '',
367                 'accnt_profit' => '01',
368                 'accnt_sub' => '01',
369                 'accnt_type' => 'A',
370                 'accnt_extref' => '',
371                 'accnt_closedpost' => false,
372                 'accnt_forwardupdate' => false,
373                 'accnt_subaccnttype_code' => 'CA',
374                 'accnt_curr_id' => $accnt->sqlValue("baseCurrId()"),
375                 'accnt_active' => true
376             ));
377
378             if(!$accnt->find(true)){
379                 $accnt->insert();
380             }
381
382             $bankaccnt->setFrom(array(
383                 'bankaccnt_name' => 'Current Account',
384                 'bankaccnt_descrip' => 'Current Account',
385                 'bankaccnt_bankname' => 'Current Account',
386                 'bankaccnt_accntnumber' => 'N/A',
387                 'bankaccnt_ar' => true,
388                 'bankaccnt_ap' => true,
389                 'bankaccnt_nextchknum' => 5000,
390                 'bankaccnt_type' => 'K',
391                 'bankaccnt_accnt_id' => $accnt->pid(),
392                 'bankaccnt_curr_id' => $bankaccnt->sqlValue("baseCurrId()"),
393                 'bankaccnt_notes' => '',
394                 'bankaccnt_routing' => '',
395                 'bankaccnt_ach_enabled' => false,
396                 'bankaccnt_ach_origintype' => 'I'
397             ));
398
399             $bankaccnt->insert();
400         }
401
402         $bankaccnt = DB_DataObject::factory('bankaccnt');
403
404         if(!$bankaccnt->get('bankaccnt_name', 'Savings Account')){
405             $accnt = DB_DataObject::factory('accnt');
406             $accnt->setFrom(array(
407                 'accnt_number' => '404',
408                 'accnt_descrip' => 'Savings Account',
409                 'accnt_comments' => '',
410                 'accnt_profit' => '01',
411                 'accnt_sub' => '01',
412                 'accnt_type' => 'A',
413                 'accnt_extref' => '',
414                 'accnt_closedpost' => false,
415                 'accnt_forwardupdate' => false,
416                 'accnt_subaccnttype_code' => 'CA',
417                 'accnt_curr_id' => $accnt->sqlValue("baseCurrId()"),
418                 'accnt_active' => true
419             ));
420
421             if(!$accnt->find(true)){
422                 $accnt->insert();
423             }
424
425             $bankaccnt->setFrom(array(
426                 'bankaccnt_name' => 'Savings Account',
427                 'bankaccnt_descrip' => 'Savings Account',
428                 'bankaccnt_bankname' => 'Savings Account',
429                 'bankaccnt_accntnumber' => 'N/A',
430                 'bankaccnt_ar' => true,
431                 'bankaccnt_ap' => true,
432                 'bankaccnt_nextchknum' => 5000,
433                 'bankaccnt_type' => 'K',
434                 'bankaccnt_accnt_id' => $accnt->pid(),
435                 'bankaccnt_curr_id' => $bankaccnt->sqlValue("baseCurrId()"),
436                 'bankaccnt_notes' => '',
437                 'bankaccnt_routing' => '',
438                 'bankaccnt_ach_enabled' => false,
439                 'bankaccnt_ach_origintype' => 'I'
440             ));
441
442             $bankaccnt->insert();
443         }
444     }
445     
446     }
447     
448 }