Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Import / Products.php
1 <?php
2
3
4 require_once 'Pman/Roo.php';
5
6 class Pman_Xtuple_Import_Products extends Pman_Roo
7 {
8     
9     /**
10      *  get .. same as roo... 
11      * 
12      */
13     
14     function getAuth()
15     {
16         if (HTML_FlexyFramework::get()->cli) {
17             return true;
18         }
19         return parent::getAuth();
20     }
21     
22     
23     function get()
24     {
25         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
26
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);
33     }
34     
35     
36     function post( )
37     {
38         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
39
40         $this->sessionState(0); // turn off the session..
41         
42         $img = DB_DataObject::Factory('images');
43         $img->setFrom(array(
44             'onid' => 0,
45             'ontable' => 'ipshead'
46         ));
47         $img->onUpload(false);
48         
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');
53         //print_R($fc);exit;
54         //var_dump($csv);exit;
55         $this->importCsv($csv);
56     }
57     function importCsv($csv)
58     {
59        // $this->jerr("this is disabled at present - please email alan the file");
60         
61         ini_set('memory_limit', '1024M');
62         sleep(10); // sleep so the that progress dialog can close..
63         
64         ini_set("auto_detect_line_endings", true);
65
66          
67         $fh = fopen($csv, 'r');
68         if (!$fh) {
69             $this->jerr("invalid file");
70         }
71         $bom = "\xEF\xBB\xBF";
72         for ($i = 0; $i< 3;$i++) {
73             if ($bom[$i] != fgetc($fh)) {
74                 fseek($fh,0);
75                 break;
76             }
77             
78         }
79         
80         
81         
82         // we need to break this into cols..
83         $cols = false;
84         $rows = array();
85         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
86             
87             
88             
89             if (!$cols) {
90                 
91                 $cols = array();
92                 foreach($n as $k) {
93                     $cols[] = strtoupper(trim($k));
94                 }
95                 
96                 if (!in_array('ITEM CODE',$cols)) {
97                     $this->jerr('missing ITEM CODE : ' . implode(' , ' , $cols));
98                 
99                 }
100                 continue;
101             }
102             $row = array();
103             foreach($cols as $i=>$k) {
104                 
105                 if (($k == 'PALLET LOCATION') && !empty($row[$k])) {
106                     if (!strlen(trim($n[$i]))) {
107                         continue;
108                     }
109                     $row[$k] .= ','.trim($n[$i]);
110                     continue;
111                 }
112                 
113                 $row[$k] = trim($n[$i]);
114             }
115             // ignore empty item codes
116             if (empty($row['ITEM CODE']) || !strlen(trim($row['ITEM CODE']))) {
117                 continue;
118             }
119             
120             
121             
122             
123             //$row['ITEM CODE'] = strtoupper($row['ITEM CODE']);
124             $rows[] = $row;
125         }
126         
127         
128         
129         fclose($fh);
130         $dupes = array();
131         
132        // print_r($rows);exit;
133         $skipped = array();
134         $missing = array();
135         $nrows = array();
136         foreach($rows as $i => $row) {
137             
138             if (in_array($row['ITEM CODE'], $dupes)) {
139                 continue;
140             }
141             
142             $i = DB_DataObject::factory('item')->lookupSKU($row['ITEM CODE']);
143             
144             if (!$i) {
145                 
146                 
147                 
148                 
149                 if (empty($row['NEW DESCRIPTION'])) {
150                     $skipped[] = strtoupper($row['ITEM CODE']). '(missing NEW DESCRIPTION)';
151                     continue;
152                     
153                 }
154                 if (empty($row['NONPRODUCT']) &&
155                     
156             
157                   ( empty($row['CUR'])
158                   || !isset($row['NEW UNIT PRICE'])
159                   || !strlen(trim($row['NEW UNIT PRICE']))
160                 ))  {
161                     
162                     $skipped[] = strtoupper($row['ITEM CODE']). '(missing price)';
163                     continue;
164                     
165                     //$this->jerr('for new items, NEW DESCRIPTION, CUR (currency) and NEW UNIT PRICE are needed.'. print_R($row,true));
166                 }
167               
168             }
169             $dupes[]  = $row['ITEM CODE'];
170             $nrows[] = $row;
171         }
172         
173         
174         $rows = $nrows;
175        //print_r($rows);exit;
176         
177         $pc= DB_DataObject::factory('prodcat');
178         if (!$pc->get('prodcat_code', 'NONPRODUCT')) {
179             $this->jerr("product category NONPRODUCT missing");
180         }
181         $nonproduct = $pc; 
182         $pc= DB_DataObject::factory('prodcat');
183         if (!$pc->get('prodcat_code', 'PRODUCT')) {
184             $this->jerr("product category PRODUCT missing");
185         }       
186         $product = $pc;
187         //print_r($rows); exit;
188         $added = 0;
189         $updated = 0;
190         
191         foreach($rows as $i => $row) {
192             // verify data...
193             
194             $row['ITEM CODE'] = strtoupper($row['ITEM CODE']);
195             $skip = false;
196             
197             $opts = array();
198             $row['item_type'] = 'P';
199             $row['item_prodcat_id'] = $product->pid();
200             if (!empty($row['NONPRODUCT'])) {
201                 
202                 
203                 $opts = array(
204                         'item_type'=> 'R', // reference item..
205                         'item_prodcat_id' => $nonproduct->pid()
206                 );
207                 foreach($opts as $k=>$v) {
208                     $row[$k] = $v;
209                 }
210                 
211             }
212             
213             $i = DB_DataObject::factory('item')->lookupSKU($row['ITEM CODE']);
214             if (!$i) {
215                 $i = DB_Dataobject::Factory('item');
216                 $i = $i->createNew($this,$row['ITEM CODE'],$row['NEW DESCRIPTION'] , $opts);
217                 $added++;
218                 
219             } else {
220                 
221                 $updated++;
222             }
223             $doupdate = false;
224             
225             $ii = clone($i);
226             if (!empty($row['NEW DESCRIPTION']) ) {
227                 $i->item_descrip1 = $row['NEW DESCRIPTION'];
228                 $doupdate = true;
229             }
230             if (!empty($row['NEW DESCRIPTION2']) ) {
231                 $i->item_descrip2 = $row['NEW DESCRIPTION2'];
232                 $doupdate = true;
233             }
234               if (!empty($row['NEW BARCODE']) ) {
235                 $i->item_upccode= $row['NEW BARCODE'];
236                 $doupdate = true;
237             }
238             
239             if ($i->item_type != $row['item_type']) {
240                 $i->item_type = $row['item_type'];
241                 $doupdate = true;
242             }
243             
244             if ($i->item_prodcat_id != $row['item_prodcat_id']) {
245                 $i->item_prodcat_id = $row['item_prodcat_id'];
246                 $doupdate = true;
247             }
248             //print_r($row);exit;
249             if ($doupdate) {
250                 $i->update($ii);
251             }
252         
253             
254             
255             $is = DB_DataObject::Factory('itemsite');
256             $is = $is->createOrFetchFromItem($this, $i);
257             $is->fixItemsite($i);
258             
259             
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'));
266                 }
267             }    
268             if (!empty($row['ITEM_CHAR_PRODUCTGROUP'])) {
269                 $i->updateCharAss('PRODUCTGROUP', $row['ITEM_CHAR_PRODUCTGROUP']);
270             }
271             
272             if (!empty($row['ITEM_CHAR_BRAND'])) {
273                 $i->updateCharAss('BRAND', $row['ITEM_CHAR_BRAND']);
274             }
275             if (!empty($row['ITEM_CHAR_PICKFACE_LOCATION'])) {
276                 $i->updateCharAss('PICKFACE_LOCATION', $row['ITEM_CHAR_PICKFACE_LOCATION']);
277             }
278             if (!empty($row['PALLET LOCATION'])) {
279                 $i->updateCharAss('PALLET_LOCATION', $row['PALLET LOCATION']);
280             }
281         }
282         $this->addEvent('PRODUCTIMPORT', false, 'Import succeeded');
283         
284         $this->jok( array(
285                 'inserted' => $added,
286                 'updated'  => $updated,
287                 'skipped' => $skipped ? implode('\n', $skipped) : false,
288             )); 
289         
290          
291          
292     
293     }
294     
295 }