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