cli) { return true; } return parent::getAuth(); } function post() { $this->transObj = DB_DataObject::Factory('poitem'); $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); require_once 'File/Convert.php'; $fc = new File_Convert($img->getStoreName(), $img->mimetype ); $csv = $fc->convert('text/csv'); $this->importCsv($csv); } function importCsv($csv) { ini_set("auto_detect_line_endings", true); $fh = fopen($csv, 'r'); if (!$fh) { $this->jerr("invalid file"); } $pohead_id = $_REQUEST['pohead_id']; $req = array('ITEM CODE', 'ITEM DESCRIPTION', 'DUE DATE', 'LINE NUMBER', 'ORDERED', 'UNIT PRICE'); $cols = false; $rows = array(); while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) { if (!$cols) { $cols = array(); foreach($n as $k) { $cols[] = strtoupper(trim($k)); } if (empty($cols)) { continue; } foreach($req as $r) { if (!in_array($r,$cols)) { $cols = false; break; } } continue; } foreach($cols as $i=>$k) { $row[$k] = $n[$i]; } $rows[] = $row; } if (empty($cols)) { $this->jerr("could not find a row with " . implode(' / ', $req)); } fclose($fh); // check pohead $pohead = DB_DataObject::factory('pohead'); if(!$pohead->get($pohead_id)){ $this->jerr('error occur on finding pohead'); } // check poitem linenumber $max_num = 0; $poitem = DB_DataObject::factory('poitem'); $poitem->selectAdd(); $poitem->selectAdd(" MAX(poitem_linenumber) as max_num "); $poitem->poitem_pohead_id = $pohead->pid(); $poitem->find(true); if(!empty($poitem->max_num)){ $max_num = $poitem->max_num; } foreach($rows as $i => $row){ /* * check item and itemsite and itemsrc, * throw exception while the item or itemsite or itemsrc is not exist for the time being * * we might need to new the item in the further */ $item = DB_DataObject::factory('item'); if(!$item->get('item_number', $row['ITEM CODE'])){ $this->jerr('item ' . $row['ITEM CODE'] . ' does not exist'); } $itemsite = DB_DataObject::factory('itemsite'); if(!$itemsite->get('itemsite_item_id', $item->pid())){ $this->jerr($row['ITEM CODE'] . ' does not exist in item site'); } $itemsrc = DB_DataObject::factory('itemsrc'); if(!$itemsrc->get('itemsrc_item_id', $item->pid())){ $this->jerr($row['ITEM CODE'] . ' does not exist in item src'); } // create poitem $poitem = DB_DataObject::factory('poitem'); $poitem->setFrom(array( 'poitem_status' => $pohead->pohead_status, 'poitem_pohead_id' => $pohead->pid(), 'poitem_linenumber' => $max_num + ($i + 1), 'poitem_duedate' => date('Y-m-d',strtotime($row['DUE DATE'])), 'poitem_itemsite_id' => $itemsite->pid(), 'poitem_qty_ordered' => $row['ORDERED'], 'poitem_unitprice' => $row['UNIT PRICE'], 'poitem_itemsrc_id' => $itemsrc->pid() )); foreach($poitem->defaults() as $k => $v){ if (!isset($poitem->$k)) { $poitem->$k = $v; } } $poitem->insert(); if(!$poitem->pid()){ $this->jerr('error occur on insert poitem ' . $row['ITEM CODE']); } } $this->jok('DONE'); exit; } }