Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Import / MyCustomers.php
1 <?php
2
3 require_once 'Pman/Roo.php';
4
5 class Pman_Xtuple_Import_MyCustomers extends Pman_Roo
6 {
7     function getAuth()
8     {
9         if (HTML_FlexyFramework::get()->cli) {
10             return true;
11         }
12         return parent::getAuth();
13     }
14     
15     function post()
16     {
17         $this->transObj = DB_DataObject::Factory('vendinfo');
18         
19         $this->transObj->query('BEGIN');
20         
21         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
22         
23         $img = DB_DataObject::Factory('images');
24         $img->setFrom(array(
25             'onid' => 0,
26             'ontable' => 'ipshead'
27         ));
28         $img->onUpload(false);
29         
30         $this->importCsv($img->getStoreName());
31     }
32     
33     function importCsv($file)
34     {
35         ini_set("auto_detect_line_endings", true);
36         
37         $data = json_decode(file_get_contents($file), true);
38         if (!$data) {
39             $this->jerr("invalid file");
40         }
41         
42         foreach ($data as $d){
43             $custinfo = DB_DataObject::factory('custinfo');
44             
45             if($custinfo->get('cust_number', $d['cust_number'])){ // exsit.. do update
46                 $cntct = $custinfo->cntct();
47                 if(!$cntct->cntct_id){
48                     $cntct = $this->addCntct($d, $custinfo->pid());
49                 }
50                 
51                 if(!$cntct->cntct_id){
52                     $this->jerr('error occur on insert a contact for ' . $d['cust_number']);
53                 }
54                 
55                 $oldcust = clone($custinfo);
56                 $custinfo->cust_cntct_id = $cntct->pid();
57                 $custinfo->update($oldcust);
58                 
59                 $addr = $cntct->addr();
60                 if(!$addr->addr_id){
61                    $addr =  $this->addAddr($d);
62                 }
63                 
64                 if(!$addr->addr_id){
65                     $this->jerr('error occur on insert a address for ' . $d['cust_number']);
66                 }
67                 
68                 $oldcn = clone($cntct);
69                 $cntct->cntct_addr_id = $addr->pid();
70                 $cntct->update($oldcn);
71                 
72                 $this->updateShipList($custinfo);
73                 
74                 continue; 
75             }
76               
77             $custinfo->setFrom(array(
78                 'cust_active'            => $d['cust_active'],
79                 'cust_salesrep_id'       => $this->salesrep($d['cust_salesrep_id_salesrep_number']),
80                 'cust_name'              => $d['cust_name'],
81                 'cust_number'            => $d['cust_number'],
82                 'cust_terms_id'          => $this->terms($d['cust_terms_id_terms_code']),
83                 'cust_taxzone_id'        => $this->taxzone($d['cust_taxzone_id_taxzone_code']),
84                 'cust_curr_id'           => $custinfo->sqlValue("getcurrid('{$d['cust_curr_id_curr_abbr']}'::text)"),
85                 'cust_creditlmt_curr_id' => $custinfo->sqlValue("getcurrid('{$d['cust_creditlmt_curr_id_curr_abbr']}'::text)")
86             ));
87                 
88             foreach($custinfo->defaults() as $k=>$v) {
89                 if (!isset($custinfo->$k)) {
90                     $custinfo->$k = $v;
91                 }
92             }
93             
94             $custinfo->insert();
95             
96             if(!$custinfo->pid()){
97                 $this->jerr("error occur on insert customer " . $d['cust_name']);
98             }
99             
100             $cntct = $this->addCntct($d, $custinfo->pid());
101             
102             if(!$cntct->pid()){
103                 $this->jerr("error occur on insert contact for " . $d['cust_name']);
104             }
105             
106             $addr = $this->addAddr($d);
107             
108             if(!$addr->pid()){
109                 $this->jerr("error occur on insert address for " . $d['cust_name']);
110             }
111             
112             $oldcn = clone($cntct);
113             $cntct->cntct_addr_id = $addr->pid();
114             $cntct->update($oldcn);
115             
116             $oldcust = clone($custinfo);
117             $custinfo->cust_cntct_id = $cntct->pid();
118             $custinfo->update($oldcust);
119             
120             $this->updateShipList($custinfo);
121             
122         }
123        
124         $this->jok("DONE");
125         
126         exit;
127     }
128     
129     function salesrep($salesrep_number)
130     {
131         $salesrep = DB_DataObject::factory('salesrep');
132         if(!$salesrep->get('salesrep_number', $salesrep_number)){
133             $salesrep->get('salesrep_number', 'ACCOUNTS');
134         }
135         return $salesrep->pid();
136         
137     }
138     
139     function terms($terms_code)
140     {
141         $terms = DB_DataObject::factory('terms');
142         if(!$terms->get('terms_code', $terms_code)){
143             $terms->get('terms_code', 'C.O.D.');
144         }
145         return $terms->pid();
146     }
147     
148     function taxzone($taxzone_code)
149     {
150         $taxzone = DB_DataObject::factory('taxzone');
151         if(!$taxzone->get('taxzone_code', $taxzone_code)){
152             $taxzone->get('taxzone_code', 'NO TAX');
153         }
154         return $taxzone->pid();
155     }
156     
157     function addCntct($d, $cid)
158     {
159         $cntct = DB_DataObject::factory('cntct');
160         $c = array(
161             'cntct_first_name'  => $d['cust_cntct_id_cntct_first_name'],
162             'cntct_last_name'   => $d['cust_cntct_id_cntct_last_name'],
163             'cntct_active'      => $d['cust_cntct_id_cntct_active'],
164             'cntct_phone'       => $d['cust_cntct_id_cntct_phone'],
165             'cntct_phone2'       => $d['cust_cntct_id_cntct_phone2'],
166             'cntct_phone2'       => $d['cust_cntct_id_cntct_phone2'],
167             'cntct_fax'         => $d['cust_cntct_id_cntct_fax'],
168             'cntct_email'       => $d['cust_cntct_id_cntct_email'],
169             'cntct_name'        => $d['cust_cntct_id_cntct_name'],
170             'cntct_crmacct_id'  => $cntct->sqlValue("(SELECT crmacct_id FROM crmacct WHERE crmacct_cust_id = {$cid})")
171         );
172         $cntct->createFromArray($c);
173         
174         return $cntct;
175     }
176     
177     function addAddr($d)
178     {
179         $addr = DB_DataObject::Factory('addr');
180         $addrs = array(
181             'addr_active'       => $d['cntct_addr_active'],
182             'addr_line1'        => $d['cntct_addr_line1'],
183             'addr_line2'        => $d['cntct_addr_line2'],
184             'addr_line3'        => $d['cntct_addr_line3'],
185             'addr_city'         => $d['cntct_addr_city'],
186             'addr_state'        => $d['cntct_addr_state'],
187             'addr_postalcode'   => $d['cntct_addr_postalcode'],
188             'addr_country'      => $d['cntct_addr_country'],
189             'addr_notes'        => $d['cntct_addr_notes'],
190         );
191
192         $addr->createFromArray($addrs);
193         
194         return $addr;
195     }
196     
197     function updateShipList($custinfo)
198     {
199         $cn = DB_DataObject::factory('cntct');
200         $cn->whereAdd("
201             cntct_crmacct_id = (SELECT crmacct_id FROM crmacct WHERE crmacct_cust_id = {$custinfo->pid()})
202         ");
203         $list = $cn->fetchAll('cntct_id');
204
205         $custinfo->updateShipList($list);
206     }
207         
208 }