ExcelToJson.php
[Pman.Core] / ExcelToJson.php
index 00d825a..27675cf 100644 (file)
@@ -30,7 +30,9 @@ class Pman_Core_ExcelToJson extends Pman_Roo
         require_once 'File/Convert.php';
         $fc = new File_Convert($img->getStoreName(), $img->mimetype );
         $csv = $fc->convert('text/csv');
-        $this->importCsv($csv);
+        $data = $this->importCsv($csv);
+        $this->jdata($data);
+        
     }
     
     function importCsv($csv)
@@ -42,10 +44,7 @@ class Pman_Core_ExcelToJson extends Pman_Roo
             $this->jerr("invalid file");
         }
         
-        $req = array(
-            'LINE', 'ITEM CODE', 'DESCRIPTION', 'QUANTITY'
-        );
-        
+         
         $cols = false;
         $header = false;
         $rows = array();
@@ -58,7 +57,7 @@ class Pman_Core_ExcelToJson extends Pman_Roo
             }
             
             if(!$header){
-               $rows[strtoupper($n[0])] = $n[1];
+               $ret[preg_replace(array('/\s/', '/\:/'), '', $n[0])] = $n[1];
                continue;
             }
             
@@ -72,29 +71,36 @@ class Pman_Core_ExcelToJson extends Pman_Roo
                     continue;
                 }
                
-                foreach($req as $r) {
-                    if (!in_array($r,$cols)) {
-                        $cols = false;
-                        break;
-                    }
-                }
+           
                 continue;
             }
             
             foreach($cols as $i=>$k) {
                 $row[$k] = $n[$i];
             }
-            $rows['TRANSFER ITEMS'][] = $row;
+            $rows[] = $row;
             
         }
         
-        if (empty($cols)) {
-            $this->jerr("could not find a row with " . implode(' / ', $req));
-        }
+        
         
         fclose($fh);
         
-        foreach ($rows['TRANSFER ITEMS'] as $r){
+        $lc = DB_DataObject::factory('location');
+        if(!$lc->get('location_name', $ret['From'])){
+            $this->jerr('error occur on getting location with reference ' . $ret['From']);
+        }
+        $ret['invhist_transfer_from'] = $lc->pid();
+        $ret['invhist_transfer_from_location_name'] = $lc->location_name;
+        
+        $lt = DB_DataObject::factory('location');
+        if(!$lt->get('location_name', $ret['To'])){
+            $this->jerr('error occur on getting location with reference ' . $ret['From']);
+        }
+        $ret['invhist_transfer_to'] = $lt->pid();
+        $ret['invhist_transfer_to_location_name'] = $lt->location_name;
+        
+        foreach ($rows as $r){
             $itemsite = DB_DataObject::factory('itemsite');
             $itemsite->autoJoin();
             $itemsite->whereAdd("join_itemsite_item_id_item_id.item_number = '{$itemsite->escape($r['ITEM CODE'])}'");
@@ -102,20 +108,16 @@ class Pman_Core_ExcelToJson extends Pman_Roo
                 $this->jerr("error occur on getting item with reference " . $r['ITEM CODE']);
             }
             
-            $ret[] = array(
+            $ret['data'][] = array(
                 'itemsite_item_id' => $itemsite->itemsite_item_id,
                 'itemsite_id' => $itemsite->pid(),
                 'itemsite_item_id_item_number' => $r['ITEM CODE'],
                 'itemsite_item_id_item_descrip1' => $r['DESCRIPTION'],
-                'itemsite_qty' => $r['QUANTITY'],
-                'avail_at_location' => ''
+                'itemsite_qty' => $r['QUANTITY']
             );
         }
-       
-        $this->jok($ret);
-        
+        return $ret;
         
-        exit;
     }
     
 }