getStoreName()); $csv = $fc->convert('text/csv'); $this->importCsv($csv); } function post( ) { 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 ); //var_Dump($img->getStoreName()); $csv = $fc->convert('text/csv'); //print_R($fc);exit; //var_dump($csv);exit; $this->importCsv($csv); } function importCsv($csv) { ini_set("auto_detect_line_endings", true); $fh = fopen($csv, 'r'); $bom = "\xEF\xBB\xBF"; for ($i = 0; $i< 3;$i++) { if ($bom[$i] != fgetc($fh)) { fseek($fh,0); break; } } // we need to break this into cols.. $cols = array(); $names = fgetcsv($fh); $curr = fgetcsv($fh); $plist = array(); foreach($names as $i=>$n) { if (!strlen(trim($n))) { // skip the empty columsn continue; } $plist[$i] = array( 'curr' => $curr[$i], 'name' => $n, 'prices' => array() ); } $skucol = 0; if (!strlen(trim($names[1])) && $curr[1] == 'item_number') { $skucol = 1; } $allsku = array(); while (false !== ($row = fgetcsv($fh))) { $sku = $row[$skucol]; // if first column is brand.. $allsku[$sku] = 1; for ($i = $skucol+1; $i < count($row); $i++) { $plist[$i]['prices'][$sku] = $row[$i]; } } fclose($fh); // grab all the sku's... nice and emmeor //print_R($plist);exit; $it = DB_DataObject::Factory('item'); $imap = $it->fetchAll('item_number','item_id'); foreach($allsku as $sku => $n) { if (empty($imap[$sku])) { $this->jerr("invalid sku in upload:" . $sku); } } foreach($plist as $nn => $data) { $ih = DB_DataObject::Factory('ipshead'); $ih->selectAdd('(SELECT curr_name FROM curr_symbol where curr_id = ipshead_curr_id LIMIT 1) AS ipshead_curr'); $ih->ipshead_name = $data['name']; if (!$ih->find(true)) { $this->jerr('unknown pricelist column('. $nn .')'. @$data['name'] ); } if ($ih->ipshead_curr != $data['curr']) { // var_Dump($data['curr']); $this->jerr("currency on pricelist {$data['name']} can not be changed from {$ih->ipshead_curr} to {$data['curr']}"); } $plist[$nn]['ipshead'] = $ih; } $ret = array('updated'=>0, 'inserted'=>0,'deleted'=>0); foreach($plist as $nn => $data) { $res = $data['ipshead']->updatePrices($data['prices'],$imap); $ret['deleted'] += $res['deleted']; $ret['inserted'] += $res['inserted']; $ret['updated'] += $res['updated']; } $this->jok($ret); print_R($plist); exit; } }