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', 'ITEM DESCRIPTION', 'ON HAND', 'AVG COST'); $cols = false; $rows = array(); while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) { if(!array_filter($n)){ // empty row continue; } 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); //print_R($rows);exit; // check pohead $pohead = DB_DataObject::factory('pohead'); if(!$pohead->get($pohead_id)){ $this->jerr('error occur on finding pohead'); } $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; } $missing = array(); foreach ($rows as $i => $row){ if (empty($row['ON HAND']) || $row['ON HAND'] < 1) { continue; } if(empty($row['ITEM'])){ unset($rows[$i]); continue; } $item = DB_DataObject::factory('item')->lookupSKU($row['ITEM']); if(!$item){ $missing[] = $row['ITEM'] . ' does not exist'; continue; } $itemsite = DB_DataObject::factory('itemsite'); if(!$itemsite->get('itemsite_item_id', $item->pid())){ $missing[] = $row['ITEM'] . ' does not exist in item site'; continue; } $rows[$i]['ITEMSITE ID'] = $itemsite->pid(); } if(count($missing)){ $this->jerr(implode("\n", $missing)); } foreach($rows as $i => $row){ // create poitem if (empty($row['ON HAND']) || $row['ON HAND'] < 1) { continue; } $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'), 'poitem_itemsite_id' => $row['ITEMSITE ID'], 'poitem_qty_ordered' => $row['ON HAND'], 'poitem_unitprice' => $row['AVG COST'], 'poitem_itemsrc_id' => $poitem->sqlValue('NULL') )); 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('IMPORTED'); exit; } }