Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Import / Flrpt.php
1 <?php
2
3 require_once 'Pman/Roo.php';
4
5 class Pman_Xtuple_Import_Flrpt extends Pman_Roo
6 {
7     
8     static $cli_desc = "Copy reports from hk office to other offices...";
9    
10     
11    
12     function getAuth()
13     {
14         if (HTML_FlexyFramework::get()->cli) {
15             return true;
16         }
17         return parent::getAuth();
18     }
19     
20     function get($flhead_id)
21     {
22         if (empty($flhead_id)) {
23             print_r(json_decode( file_get_contents('http://localhost/dragon/hk.php/Roo/flhead?_columns=flhead_id,flhead_name')));
24             
25             $this->jerr("pick an available report");
26         }
27         
28         $this->transObj = DB_DataObject::Factory('gltrans');
29         
30         $this->transObj->query('BEGIN');
31         
32         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
33         
34         // sandobx or live???
35         $url = 'http://localhost/dragon/hk.php/Roo/Flhead?flhead_id='.$flhead_id.'&_to_json=1';
36         $res = json_decode(file_get_contents($url),true);
37         
38         if(count($res['data']) < 1) {
39             $this->jerr('no data found');
40         }
41         //$res['data']['flhead_name'] = 'TESTING';
42         $flhead = DB_DataObject::factory('flhead');
43         if($flhead->get('flhead_name' , $res['data']['flhead_name'])){
44             $this->jerr('flhead already exists');
45         }
46         
47         $flhead->setFrom($res['data']);
48         $flhead->insert();
49         
50         if(!$flhead->pid()){
51             $this->jerr('error occur on insert flhead');
52         }
53         if(!count($res['data']['groups'])){
54             $this->jerr('no groups found');
55         }
56         $grp = $res['data']['groups'];
57         $this->walkgrp($grp, $flhead->pid());
58         
59         if(count($res['data']['col'])){
60             $this->walkcol($res['data']['col'], $flhead->pid());
61         }
62         
63          $this->transObj->query('COMMIT');
64         
65         exit;
66     }
67     
68     function walkgrp($grp, $hid, $pargrp = '-1')
69     {
70         foreach($grp as $g){
71             $g['flgrp_flhead_id'] = $hid;
72             if($g['flgrp_flgrp_id'] > 0){ // get and parent
73                 $g['flgrp_flgrp_id'] = $pargrp;    
74             }
75             $flgrp = DB_DataObject::factory('flgrp');
76             $flgrp->setFrom($g);
77             $flgrp->insert();
78             
79             if(!$flgrp->pid()){
80                 $this->jerr('error occur on insert group : ' . $g['flgrp_name']);
81             }
82             
83             if(count($g['items'])){
84                 $this->walkitem($g['items'], $hid, $flgrp->pid());
85             }
86             
87             if(count($g['groups'])){
88                 $this->walkgrp($g['groups'], $hid, $flgrp->pid());
89             }
90         }
91         
92     }
93     
94     function walkitem($item, $hid, $gid)
95     {
96         foreach($item as $i){
97             $i['flitem_flhead_id'] = $hid;
98             $i['flitem_flgrp_id'] = $gid;
99             $flitem = DB_DataObject::factory('flitem');
100             $flitem->setFrom($i);
101             $flitem->insert();
102         }
103     }
104     
105     function walkcol($col, $hid)
106     {
107         foreach($col as $c){
108             if(!count($c['report'])){
109                 $this->jerr('Missing report details');
110             }
111             $report = DB_DataObject::factory('report');
112             
113             if(!$report->get('report_name', $c['report']['report_name'])){
114                 $report->setFrom($c['report']);
115                 $report->insert();
116             }
117             
118             if(!$report->pid()){
119                 $this->jerr('error occur on insert report : ' . $c['report']['report_name']);
120             }
121             
122             $c['flcol_flhead_id'] = $hid;
123             $c['flcol_report_id'] = $report->pid();
124             $flcol = DB_DataObject::factory('flcol');
125             $flcol->setFrom($c);
126             $flcol->insert();
127         }
128     }
129 }