3 require_once 'Pman/Roo.php';
5 class Pman_Xtuple_Import_MyCustomers extends Pman_Roo
9 if (HTML_FlexyFramework::get()->cli) {
12 return parent::getAuth();
17 $this->transObj = DB_DataObject::Factory('vendinfo');
19 $this->transObj->query('BEGIN');
21 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
23 $img = DB_DataObject::Factory('images');
26 'ontable' => 'ipshead'
28 $img->onUpload(false);
30 $this->importCsv($img->getStoreName());
33 function importCsv($file)
35 ini_set("auto_detect_line_endings", true);
37 $data = json_decode(file_get_contents($file), true);
39 $this->jerr("invalid file");
42 foreach ($data as $d){
43 $custinfo = DB_DataObject::factory('custinfo');
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());
51 if(!$cntct->cntct_id){
52 $this->jerr('error occur on insert a contact for ' . $d['cust_number']);
55 $oldcust = clone($custinfo);
56 $custinfo->cust_cntct_id = $cntct->pid();
57 $custinfo->update($oldcust);
59 $addr = $cntct->addr();
61 $addr = $this->addAddr($d);
65 $this->jerr('error occur on insert a address for ' . $d['cust_number']);
68 $oldcn = clone($cntct);
69 $cntct->cntct_addr_id = $addr->pid();
70 $cntct->update($oldcn);
72 $this->updateShipList($custinfo);
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)")
88 foreach($custinfo->defaults() as $k=>$v) {
89 if (!isset($custinfo->$k)) {
96 if(!$custinfo->pid()){
97 $this->jerr("error occur on insert customer " . $d['cust_name']);
100 $cntct = $this->addCntct($d, $custinfo->pid());
103 $this->jerr("error occur on insert contact for " . $d['cust_name']);
106 $addr = $this->addAddr($d);
109 $this->jerr("error occur on insert address for " . $d['cust_name']);
112 $oldcn = clone($cntct);
113 $cntct->cntct_addr_id = $addr->pid();
114 $cntct->update($oldcn);
116 $oldcust = clone($custinfo);
117 $custinfo->cust_cntct_id = $cntct->pid();
118 $custinfo->update($oldcust);
120 $this->updateShipList($custinfo);
129 function salesrep($salesrep_number)
131 $salesrep = DB_DataObject::factory('salesrep');
132 if(!$salesrep->get('salesrep_number', $salesrep_number)){
133 $salesrep->get('salesrep_number', 'ACCOUNTS');
135 return $salesrep->pid();
139 function terms($terms_code)
141 $terms = DB_DataObject::factory('terms');
142 if(!$terms->get('terms_code', $terms_code)){
143 $terms->get('terms_code', 'C.O.D.');
145 return $terms->pid();
148 function taxzone($taxzone_code)
150 $taxzone = DB_DataObject::factory('taxzone');
151 if(!$taxzone->get('taxzone_code', $taxzone_code)){
152 $taxzone->get('taxzone_code', 'NO TAX');
154 return $taxzone->pid();
157 function addCntct($d, $cid)
159 $cntct = DB_DataObject::factory('cntct');
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})")
172 $cntct->createFromArray($c);
179 $addr = DB_DataObject::Factory('addr');
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'],
192 $addr->createFromArray($addrs);
197 function updateShipList($custinfo)
199 $cn = DB_DataObject::factory('cntct');
201 cntct_crmacct_id = (SELECT crmacct_id FROM crmacct WHERE crmacct_cust_id = {$custinfo->pid()})
203 $list = $cn->fetchAll('cntct_id');
205 $custinfo->updateShipList($list);