4 require_once 'Pman/Roo.php';
6 class Pman_Xtuple_Import_Products extends Pman_Roo
10 * get .. same as roo...
16 if (HTML_FlexyFramework::get()->cli) {
19 return parent::getAuth();
25 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
27 //DB_DataObject::DebugLevel(1);
28 require_once 'File/Convert.php';
29 $fc = new File_Convert('/tmp/sku.csv', 'text/csv');
30 //var_Dump($img->getStoreName());
31 $csv = $fc->convert('text/csv');
32 $this->importCsv($csv);
38 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
40 $this->sessionState(0); // turn off the session..
42 $img = DB_DataObject::Factory('images');
45 'ontable' => 'ipshead'
47 $img->onUpload(false);
49 require_once 'File/Convert.php';
50 $fc = new File_Convert($img->getStoreName(), $img->mimetype );
51 //var_Dump($img->getStoreName());
52 $csv = $fc->convert('text/csv');
54 //var_dump($csv);exit;
55 $this->importCsv($csv);
57 function importCsv($csv)
59 // $this->jerr("this is disabled at present - please email alan the file");
61 ini_set('memory_limit', '1024M');
62 sleep(10); // sleep so the that progress dialog can close..
64 ini_set("auto_detect_line_endings", true);
67 $fh = fopen($csv, 'r');
69 $this->jerr("invalid file");
71 $bom = "\xEF\xBB\xBF";
72 for ($i = 0; $i< 3;$i++) {
73 if ($bom[$i] != fgetc($fh)) {
82 // we need to break this into cols..
85 while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
93 $cols[] = strtoupper(trim($k));
96 if (!in_array('ITEM CODE',$cols)) {
97 $this->jerr('missing ITEM CODE : ' . implode(' , ' , $cols));
103 foreach($cols as $i=>$k) {
105 if (($k == 'PALLET LOCATION') && !empty($row[$k])) {
106 if (!strlen(trim($n[$i]))) {
109 $row[$k] .= ','.trim($n[$i]);
113 $row[$k] = trim($n[$i]);
115 // ignore empty item codes
116 if (empty($row['ITEM CODE']) || !strlen(trim($row['ITEM CODE']))) {
123 //$row['ITEM CODE'] = strtoupper($row['ITEM CODE']);
132 // print_r($rows);exit;
136 foreach($rows as $i => $row) {
138 if (in_array($row['ITEM CODE'], $dupes)) {
142 $i = DB_DataObject::factory('item')->lookupSKU($row['ITEM CODE']);
149 if (empty($row['NEW DESCRIPTION'])) {
150 $skipped[] = strtoupper($row['ITEM CODE']). '(missing NEW DESCRIPTION)';
154 if (empty($row['NONPRODUCT']) &&
158 || !isset($row['NEW UNIT PRICE'])
159 || !strlen(trim($row['NEW UNIT PRICE']))
162 $skipped[] = strtoupper($row['ITEM CODE']). '(missing price)';
165 //$this->jerr('for new items, NEW DESCRIPTION, CUR (currency) and NEW UNIT PRICE are needed.'. print_R($row,true));
169 $dupes[] = $row['ITEM CODE'];
175 //print_r($rows);exit;
177 $pc= DB_DataObject::factory('prodcat');
178 if (!$pc->get('prodcat_code', 'NONPRODUCT')) {
179 $this->jerr("product category NONPRODUCT missing");
182 $pc= DB_DataObject::factory('prodcat');
183 if (!$pc->get('prodcat_code', 'PRODUCT')) {
184 $this->jerr("product category PRODUCT missing");
187 //print_r($rows); exit;
191 foreach($rows as $i => $row) {
194 $row['ITEM CODE'] = strtoupper($row['ITEM CODE']);
198 $row['item_type'] = 'P';
199 $row['item_prodcat_id'] = $product->pid();
200 if (!empty($row['NONPRODUCT'])) {
204 'item_type'=> 'R', // reference item..
205 'item_prodcat_id' => $nonproduct->pid()
207 foreach($opts as $k=>$v) {
213 $i = DB_DataObject::factory('item')->lookupSKU($row['ITEM CODE']);
215 $i = DB_Dataobject::Factory('item');
216 $i = $i->createNew($this,$row['ITEM CODE'],$row['NEW DESCRIPTION'] , $opts);
226 if (!empty($row['NEW DESCRIPTION']) ) {
227 $i->item_descrip1 = $row['NEW DESCRIPTION'];
230 if (!empty($row['NEW DESCRIPTION2']) ) {
231 $i->item_descrip2 = $row['NEW DESCRIPTION2'];
234 if (!empty($row['NEW BARCODE']) ) {
235 $i->item_upccode= $row['NEW BARCODE'];
239 if ($i->item_type != $row['item_type']) {
240 $i->item_type = $row['item_type'];
244 if ($i->item_prodcat_id != $row['item_prodcat_id']) {
245 $i->item_prodcat_id = $row['item_prodcat_id'];
248 //print_r($row);exit;
255 $is = DB_DataObject::Factory('itemsite');
256 $is = $is->createOrFetchFromItem($this, $i);
257 $is->fixItemsite($i);
260 if ($row['item_type'] == 'P') {
261 // update prices if found. -- and if a product
262 if ( !empty($row['CUR'])
263 && isset($row['NEW UNIT PRICE'])
264 && strlen(trim($row['NEW UNIT PRICE']))) {
265 $i->updateItemCost($this, $row['CUR'], $row['NEW UNIT PRICE'], date('Y-m-d'));
268 if (!empty($row['ITEM_CHAR_PRODUCTGROUP'])) {
269 $i->updateCharAss('PRODUCTGROUP', $row['ITEM_CHAR_PRODUCTGROUP']);
272 if (!empty($row['ITEM_CHAR_BRAND'])) {
273 $i->updateCharAss('BRAND', $row['ITEM_CHAR_BRAND']);
275 if (!empty($row['ITEM_CHAR_PICKFACE_LOCATION'])) {
276 $i->updateCharAss('PICKFACE_LOCATION', $row['ITEM_CHAR_PICKFACE_LOCATION']);
278 if (!empty($row['PALLET LOCATION'])) {
279 $i->updateCharAss('PALLET_LOCATION', $row['PALLET LOCATION']);
282 $this->addEvent('PRODUCTIMPORT', false, 'Import succeeded');
285 'inserted' => $added,
286 'updated' => $updated,
287 'skipped' => $skipped ? implode('\n', $skipped) : false,