Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Pricing.php
1 <?php
2
3
4 require_once 'Pman/Roo.php';
5
6 class Pman_Xtuple_Pricing extends Pman_Roo
7 {
8     
9     /**
10      *  get .. same as roo... 
11      * 
12      */
13     
14     function get()
15     {
16         
17          PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
18
19         //DB_DataObject::DebugLevel(1);
20         require_once 'File/Convert.php';
21         $fc = new File_Convert('/tmp/pricelist2012-07-20.xls', 'application/vnd.ms-excel');
22         //var_Dump($img->getStoreName());
23         $csv = $fc->convert('text/csv');
24         $this->importCsv($csv);
25     }
26     
27     
28     function post( )
29     {
30         
31                 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
32
33         
34         $img = DB_DataObject::Factory('images');
35         $img->setFrom(array(
36             'onid' => 0,
37             'ontable' => 'ipshead'
38         ));
39         $img->onUpload(false);
40         
41         require_once 'File/Convert.php';
42         $fc = new File_Convert($img->getStoreName(), $img->mimetype );
43         //var_Dump($img->getStoreName());
44         $csv = $fc->convert('text/csv');
45         //print_R($fc);exit;
46         //var_dump($csv);exit;
47         $this->importCsv($csv);
48     }
49     function importCsv($csv)
50     {
51         ini_set("auto_detect_line_endings", true);
52
53         $fh = fopen($csv, 'r');
54         
55         $bom = "\xEF\xBB\xBF";
56         for ($i = 0; $i< 3;$i++) {
57             if ($bom[$i] != fgetc($fh)) {
58                 fseek($fh,0);
59                 break;
60             }
61             
62         }
63         
64         // we need to break this into cols..
65         $cols = array();
66         $names = fgetcsv($fh);
67         $curr = fgetcsv($fh);
68         
69        
70         
71         $plist = array();
72         foreach($names as $i=>$n) {
73             if (!strlen(trim($n))) { // skip the empty columsn
74                 continue;
75             }
76             
77             
78             $plist[$i] = array(
79                 'curr' => $curr[$i],
80                 'name' => $n,
81                 'prices' => array()
82             );
83             
84             
85         }
86         
87         $skucol = 0;
88         
89         
90         if (!strlen(trim($names[1])) && $curr[1] == 'item_number') {
91             $skucol = 1;
92         }
93     
94         $allsku = array();
95         
96         while (false !== ($row = fgetcsv($fh))) {
97             $sku = $row[$skucol]; // if first column is brand..
98             
99             
100             $allsku[$sku] = 1;
101             for ($i = $skucol+1; $i  < count($row); $i++) {
102                 $plist[$i]['prices'][$sku] = $row[$i];
103             }
104             
105         }
106         fclose($fh);
107         // grab all the sku's... nice and emmeor
108         
109         //print_R($plist);exit;
110         
111         $it = DB_DataObject::Factory('item');
112         $imap = $it->fetchAll('item_number','item_id');
113         
114         foreach($allsku as $sku => $n) {
115             if (empty($imap[$sku])) {
116                 $this->jerr("invalid sku in upload:" . $sku);
117             }
118             
119         }
120         
121         foreach($plist as $nn => $data) {
122             $ih = DB_DataObject::Factory('ipshead');
123             $ih->selectAdd('(SELECT curr_name   FROM curr_symbol where curr_id = ipshead_curr_id LIMIT 1) AS ipshead_curr');
124             $ih->ipshead_name = $data['name'];
125             
126             if (!$ih->find(true)) {
127                 $this->jerr('unknown pricelist column('. $nn .')'. @$data['name'] );
128             }
129             
130             if ($ih->ipshead_curr != $data['curr']) {
131                //  var_Dump($data['curr']);
132                 $this->jerr("currency on pricelist {$data['name']} can not be changed from {$ih->ipshead_curr}  to {$data['curr']}");
133             }
134             $plist[$nn]['ipshead'] = $ih;
135             
136         }
137         $ret = array('updated'=>0, 'inserted'=>0,'deleted'=>0);
138         foreach($plist as $nn => $data) {
139             $res = $data['ipshead']->updatePrices($data['prices'],$imap);
140             $ret['deleted'] += $res['deleted'];
141             $ret['inserted'] += $res['inserted'];
142             $ret['updated'] += $res['updated'];
143         }
144         $this->jok($ret);
145         
146         
147         
148         
149         
150         
151         
152         
153         print_R($plist);
154         
155         
156         
157         
158         exit;
159         
160         
161         
162         
163     
164     }
165     
166 }