cli) { return true; } return parent::getAuth(); } function post() { $this->transObj = DB_DataObject::Factory('vendinfo'); $this->transObj->query('BEGIN'); PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError')); $img = DB_DataObject::Factory('images'); $img->setFrom(array( 'onid' => 0, 'ontable' => 'ipshead' )); $img->onUpload(false); $this->importCsv($img->getStoreName()); } function importCsv($file) { ini_set("auto_detect_line_endings", true); $data = json_decode(file_get_contents($file), true); if (!$data) { $this->jerr("invalid file"); } foreach ($data as $d){ $custinfo = DB_DataObject::factory('custinfo'); if($custinfo->get('cust_number', $d['cust_number'])){ // exsit.. do update $cntct = $custinfo->cntct(); if(!$cntct->cntct_id){ $cntct = $this->addCntct($d, $custinfo->pid()); } if(!$cntct->cntct_id){ $this->jerr('error occur on insert a contact for ' . $d['cust_number']); } $oldcust = clone($custinfo); $custinfo->cust_cntct_id = $cntct->pid(); $custinfo->update($oldcust); $addr = $cntct->addr(); if(!$addr->addr_id){ $addr = $this->addAddr($d); } if(!$addr->addr_id){ $this->jerr('error occur on insert a address for ' . $d['cust_number']); } $oldcn = clone($cntct); $cntct->cntct_addr_id = $addr->pid(); $cntct->update($oldcn); $this->updateShipList($custinfo); continue; } $custinfo->setFrom(array( 'cust_active' => $d['cust_active'], 'cust_salesrep_id' => $this->salesrep($d['cust_salesrep_id_salesrep_number']), 'cust_name' => $d['cust_name'], 'cust_number' => $d['cust_number'], 'cust_terms_id' => $this->terms($d['cust_terms_id_terms_code']), 'cust_taxzone_id' => $this->taxzone($d['cust_taxzone_id_taxzone_code']), 'cust_curr_id' => $custinfo->sqlValue("getcurrid('{$d['cust_curr_id_curr_abbr']}'::text)"), 'cust_creditlmt_curr_id' => $custinfo->sqlValue("getcurrid('{$d['cust_creditlmt_curr_id_curr_abbr']}'::text)") )); foreach($custinfo->defaults() as $k=>$v) { if (!isset($custinfo->$k)) { $custinfo->$k = $v; } } $custinfo->insert(); if(!$custinfo->pid()){ $this->jerr("error occur on insert customer " . $d['cust_name']); } $cntct = $this->addCntct($d, $custinfo->pid()); if(!$cntct->pid()){ $this->jerr("error occur on insert contact for " . $d['cust_name']); } $addr = $this->addAddr($d); if(!$addr->pid()){ $this->jerr("error occur on insert address for " . $d['cust_name']); } $oldcn = clone($cntct); $cntct->cntct_addr_id = $addr->pid(); $cntct->update($oldcn); $oldcust = clone($custinfo); $custinfo->cust_cntct_id = $cntct->pid(); $custinfo->update($oldcust); $this->updateShipList($custinfo); } $this->jok("DONE"); exit; } function salesrep($salesrep_number) { $salesrep = DB_DataObject::factory('salesrep'); if(!$salesrep->get('salesrep_number', $salesrep_number)){ $salesrep->get('salesrep_number', 'ACCOUNTS'); } return $salesrep->pid(); } function terms($terms_code) { $terms = DB_DataObject::factory('terms'); if(!$terms->get('terms_code', $terms_code)){ $terms->get('terms_code', 'C.O.D.'); } return $terms->pid(); } function taxzone($taxzone_code) { $taxzone = DB_DataObject::factory('taxzone'); if(!$taxzone->get('taxzone_code', $taxzone_code)){ $taxzone->get('taxzone_code', 'NO TAX'); } return $taxzone->pid(); } function addCntct($d, $cid) { $cntct = DB_DataObject::factory('cntct'); $c = array( 'cntct_first_name' => $d['cust_cntct_id_cntct_first_name'], 'cntct_last_name' => $d['cust_cntct_id_cntct_last_name'], 'cntct_active' => $d['cust_cntct_id_cntct_active'], 'cntct_phone' => $d['cust_cntct_id_cntct_phone'], 'cntct_phone2' => $d['cust_cntct_id_cntct_phone2'], 'cntct_phone2' => $d['cust_cntct_id_cntct_phone2'], 'cntct_fax' => $d['cust_cntct_id_cntct_fax'], 'cntct_email' => $d['cust_cntct_id_cntct_email'], 'cntct_name' => $d['cust_cntct_id_cntct_name'], 'cntct_crmacct_id' => $cntct->sqlValue("(SELECT crmacct_id FROM crmacct WHERE crmacct_cust_id = {$cid})") ); $cntct->createFromArray($c); return $cntct; } function addAddr($d) { $addr = DB_DataObject::Factory('addr'); $addrs = array( 'addr_active' => $d['cntct_addr_active'], 'addr_line1' => $d['cntct_addr_line1'], 'addr_line2' => $d['cntct_addr_line2'], 'addr_line3' => $d['cntct_addr_line3'], 'addr_city' => $d['cntct_addr_city'], 'addr_state' => $d['cntct_addr_state'], 'addr_postalcode' => $d['cntct_addr_postalcode'], 'addr_country' => $d['cntct_addr_country'], 'addr_notes' => $d['cntct_addr_notes'], ); $addr->createFromArray($addrs); return $addr; } function updateShipList($custinfo) { $cn = DB_DataObject::factory('cntct'); $cn->whereAdd(" cntct_crmacct_id = (SELECT crmacct_id FROM crmacct WHERE crmacct_cust_id = {$custinfo->pid()}) "); $list = $cn->fetchAll('cntct_id'); $custinfo->updateShipList($list); } }