cli) { return true; } return parent::getAuth(); } function get() { PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError')); //DB_DataObject::DebugLevel(1); require_once 'File/Convert.php'; $fc = new File_Convert('/tmp/sku.csv', 'text/csv'); //var_Dump($img->getStoreName()); $csv = $fc->convert('text/csv'); $this->importCsv($csv); } function post( ) { PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError')); $this->sessionState(0); // turn off the session.. $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) { // $this->jerr("this is disabled at present - please email alan the file"); ini_set('memory_limit', '1024M'); sleep(10); // sleep so the that progress dialog can close.. ini_set("auto_detect_line_endings", true); $fh = fopen($csv, 'r'); if (!$fh) { $this->jerr("invalid file"); } $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 = false; $rows = array(); while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) { if (!$cols) { $cols = array(); foreach($n as $k) { $cols[] = strtoupper(trim($k)); } if (!in_array('ITEM CODE',$cols)) { $this->jerr('missing ITEM CODE : ' . implode(' , ' , $cols)); } continue; } $row = array(); foreach($cols as $i=>$k) { if (($k == 'PALLET LOCATION') && !empty($row[$k])) { if (!strlen(trim($n[$i]))) { continue; } $row[$k] .= ','.trim($n[$i]); continue; } $row[$k] = trim($n[$i]); } // ignore empty item codes if (empty($row['ITEM CODE']) || !strlen(trim($row['ITEM CODE']))) { continue; } //$row['ITEM CODE'] = strtoupper($row['ITEM CODE']); $rows[] = $row; } fclose($fh); $dupes = array(); // print_r($rows);exit; $skipped = array(); $missing = array(); $nrows = array(); foreach($rows as $i => $row) { if (in_array($row['ITEM CODE'], $dupes)) { continue; } $i = DB_DataObject::factory('item')->lookupSKU($row['ITEM CODE']); if (!$i) { if (empty($row['NEW DESCRIPTION'])) { $skipped[] = strtoupper($row['ITEM CODE']). '(missing NEW DESCRIPTION)'; continue; } if (empty($row['NONPRODUCT']) && ( empty($row['CUR']) || !isset($row['NEW UNIT PRICE']) || !strlen(trim($row['NEW UNIT PRICE'])) )) { $skipped[] = strtoupper($row['ITEM CODE']). '(missing price)'; continue; //$this->jerr('for new items, NEW DESCRIPTION, CUR (currency) and NEW UNIT PRICE are needed.'. print_R($row,true)); } } $dupes[] = $row['ITEM CODE']; $nrows[] = $row; } $rows = $nrows; //print_r($rows);exit; $pc= DB_DataObject::factory('prodcat'); if (!$pc->get('prodcat_code', 'NONPRODUCT')) { $this->jerr("product category NONPRODUCT missing"); } $nonproduct = $pc; $pc= DB_DataObject::factory('prodcat'); if (!$pc->get('prodcat_code', 'PRODUCT')) { $this->jerr("product category PRODUCT missing"); } $product = $pc; //print_r($rows); exit; $added = 0; $updated = 0; foreach($rows as $i => $row) { // verify data... $row['ITEM CODE'] = strtoupper($row['ITEM CODE']); $skip = false; $opts = array(); $row['item_type'] = 'P'; $row['item_prodcat_id'] = $product->pid(); if (!empty($row['NONPRODUCT'])) { $opts = array( 'item_type'=> 'R', // reference item.. 'item_prodcat_id' => $nonproduct->pid() ); foreach($opts as $k=>$v) { $row[$k] = $v; } } $i = DB_DataObject::factory('item')->lookupSKU($row['ITEM CODE']); if (!$i) { $i = DB_Dataobject::Factory('item'); $i = $i->createNew($this,$row['ITEM CODE'],$row['NEW DESCRIPTION'] , $opts); $added++; } else { $updated++; } $doupdate = false; $ii = clone($i); if (!empty($row['NEW DESCRIPTION']) ) { $i->item_descrip1 = $row['NEW DESCRIPTION']; $doupdate = true; } if (!empty($row['NEW DESCRIPTION2']) ) { $i->item_descrip2 = $row['NEW DESCRIPTION2']; $doupdate = true; } if (!empty($row['NEW BARCODE']) ) { $i->item_upccode= $row['NEW BARCODE']; $doupdate = true; } if ($i->item_type != $row['item_type']) { $i->item_type = $row['item_type']; $doupdate = true; } if ($i->item_prodcat_id != $row['item_prodcat_id']) { $i->item_prodcat_id = $row['item_prodcat_id']; $doupdate = true; } //print_r($row);exit; if ($doupdate) { $i->update($ii); } $is = DB_DataObject::Factory('itemsite'); $is = $is->createOrFetchFromItem($this, $i); $is->fixItemsite($i); if ($row['item_type'] == 'P') { // update prices if found. -- and if a product if ( !empty($row['CUR']) && isset($row['NEW UNIT PRICE']) && strlen(trim($row['NEW UNIT PRICE']))) { $i->updateItemCost($this, $row['CUR'], $row['NEW UNIT PRICE'], date('Y-m-d')); } } if (!empty($row['ITEM_CHAR_PRODUCTGROUP'])) { $i->updateCharAss('PRODUCTGROUP', $row['ITEM_CHAR_PRODUCTGROUP']); } if (!empty($row['ITEM_CHAR_BRAND'])) { $i->updateCharAss('BRAND', $row['ITEM_CHAR_BRAND']); } if (!empty($row['ITEM_CHAR_PICKFACE_LOCATION'])) { $i->updateCharAss('PICKFACE_LOCATION', $row['ITEM_CHAR_PICKFACE_LOCATION']); } if (!empty($row['PALLET LOCATION'])) { $i->updateCharAss('PALLET_LOCATION', $row['PALLET LOCATION']); } } $this->addEvent('PRODUCTIMPORT', false, 'Import succeeded'); $this->jok( array( 'inserted' => $added, 'updated' => $updated, 'skipped' => $skipped ? implode('\n', $skipped) : false, )); } }