ExcelToJson.php
[Pman.Core] / ExcelToJson.php
1 <?php
2
3 require_once 'Pman/Roo.php';
4
5 class Pman_Core_ExcelToJson extends Pman_Roo
6 {
7     function getAuth()
8     {
9         if (HTML_FlexyFramework::get()->cli) {
10             return true;
11         }
12         return parent::getAuth();
13     }
14     
15     function post()
16     {
17         $this->transObj = DB_DataObject::Factory('invhist_transfer');
18         
19         $this->transObj->query('BEGIN');
20         
21         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
22         
23         $img = DB_DataObject::Factory('images');
24         $img->setFrom(array(
25             'onid' => 0,
26             'ontable' => 'ipshead'
27         ));
28         $img->onUpload(false);
29         
30         require_once 'File/Convert.php';
31         $fc = new File_Convert($img->getStoreName(), $img->mimetype );
32         $csv = $fc->convert('text/csv');
33         $this->importCsv($csv);
34     }
35     
36     function importCsv($csv)
37     {
38         ini_set("auto_detect_line_endings", true);
39         
40         $fh = fopen($csv, 'r');
41         if (!$fh) {
42             $this->jerr("invalid file");
43         }
44         
45         $req = array(
46             'LINE', 'ITEM CODE', 'DESCRIPTION', 'QUANTITY'
47         );
48         
49         $cols = false;
50         $header = false;
51         $rows = array();
52         $ret = array();
53         
54         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
55             if(!array_filter($n)){
56                 $header = true;
57                 continue;
58             }
59             
60             if(!$header){
61                $rows[strtoupper($n[0])] = $n[1];
62                continue;
63             }
64             
65             if(!$cols){
66                 $cols = array();
67                 foreach($n as $k) {
68                     $cols[] = strtoupper(trim($k));
69                 }
70                 
71                 if (empty($cols)) {
72                     continue;
73                 }
74                
75                 foreach($req as $r) {
76                     if (!in_array($r,$cols)) {
77                         $cols = false;
78                         break;
79                     }
80                 }
81                 continue;
82             }
83             
84             foreach($cols as $i=>$k) {
85                 $row[$k] = $n[$i];
86             }
87             $rows['TRANSFER ITEMS'][] = $row;
88             
89         }
90         
91         if (empty($cols)) {
92             $this->jerr("could not find a row with " . implode(' / ', $req));
93         }
94         
95         fclose($fh);
96         
97         foreach ($rows['TRANSFER ITEMS'] as $r){
98             $itemsite = DB_DataObject::factory('itemsite');
99 //            $itemsite->autoJoin();
100             if(!$itemsite->get('itemsite_id', 5583)){
101                 $this->jerr("error occur on getting item with reference " . $r['ITEM CODE']);
102             }
103             print_r($itemsite);exit;
104             $ret[] = array(
105                 'itemsite_item_id' => $itemsite->itemsite_item_id,
106                 'itemsite_id' => $itemsite->pid(),
107                 'itemsite_item_id_item_number' => $r['itemsite_item_id_item_number']
108             );
109         }
110         
111         
112         
113         
114         exit;
115     }
116     
117 }