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         $data = $this->importCsv($csv);
34         $this->jdata($data);
35         
36     }
37     
38     function importCsv($csv)
39     {
40         ini_set("auto_detect_line_endings", true);
41         
42         $fh = fopen($csv, 'r');
43         if (!$fh) {
44             $this->jerr("invalid file");
45         }
46         
47          
48         $cols = false;
49         $header = false;
50         $rows = array();
51         $ret = array();
52         
53         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
54             if(!array_filter($n)){
55                 $header = true;
56                 continue;
57             }
58             
59             if(!$header){
60                $ret[preg_replace(array('/\s/', '/\:/'), '', $n[0])] = $n[1];
61                continue;
62             }
63             
64             if(!$cols){
65                 $cols = array();
66                 foreach($n as $k) {
67                     $cols[] = strtoupper(trim($k));
68                 }
69                 
70                 if (empty($cols)) {
71                     continue;
72                 }
73                
74            
75                 continue;
76             }
77             
78             foreach($cols as $i=>$k) {
79                 $row[$k] = $n[$i];
80             }
81             $rows[] = $row;
82             
83         }
84         
85         
86         
87         fclose($fh);
88         
89         $lc = DB_DataObject::factory('location');
90         if(!$lc->get('location_name', $ret['From'])){
91             $this->jerr('error occur on getting location with reference ' . $ret['From']);
92         }
93         $ret['invhist_transfer_from'] = $lc->pid();
94         $ret['invhist_transfer_from_location_name'] = $lc->location_name;
95         
96         $lt = DB_DataObject::factory('location');
97         if(!$lt->get('location_name', $ret['To'])){
98             $this->jerr('error occur on getting location with reference ' . $ret['From']);
99         }
100         $ret['invhist_transfer_to'] = $lt->pid();
101         $ret['invhist_transfer_to_location_name'] = $lt->location_name;
102         
103         foreach ($rows as $r){
104             $itemsite = DB_DataObject::factory('itemsite');
105             $itemsite->autoJoin();
106             $itemsite->whereAdd("join_itemsite_item_id_item_id.item_number = '{$itemsite->escape($r['ITEM CODE'])}'");
107             if(!$itemsite->find(true)){
108                 $this->jerr("error occur on getting item with reference " . $r['ITEM CODE']);
109             }
110             
111             $ret['data'][] = array(
112                 'itemsite_item_id' => $itemsite->itemsite_item_id,
113                 'itemsite_id' => $itemsite->pid(),
114                 'itemsite_item_id_item_number' => $r['ITEM CODE'],
115                 'itemsite_item_id_item_descrip1' => $r['DESCRIPTION'],
116                 'itemsite_qty' => $r['QUANTITY']
117             );
118         }
119         return $ret;
120         
121     }
122     
123 }