Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Import / SalesOrder.php
1 <?php
2
3
4 require_once 'Pman/Roo.php';
5
6 class Pman_Xtuple_Import_SalesOrder extends Pman_Roo
7 {
8     
9     /**
10      *  get .. same as roo... 
11      * 
12      */
13     
14     function getAuth()
15     {
16         if (HTML_FlexyFramework::get()->cli) {
17             return true;
18         }
19         return parent::getAuth();
20     }
21     
22     
23     function get()
24     {
25         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
26         $this->jerr("INVALID");
27         //DB_DataObject::DebugLevel(1);
28         require_once 'File/Convert.php';
29         $fc = new File_Convert('/tmp/sku.csv', 'text/csv');
30         //var_Dump($img->getStoreName());
31         $csv = $fc->convert('text/csv');
32         $this->importCsv($csv);
33     }
34     
35     
36     function post( )
37     {
38         
39         $this->sessionState(0); // turn off the session..
40         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
41
42         if (empty($_REQUEST['onid'])) {
43             $this->jerr('no order found');
44             
45         }
46         
47          
48         $img = DB_DataObject::Factory('images');
49         $img->setFrom(array(
50             'onid' => 0,
51             'ontable' => 'ipshead'
52         ));
53         $img->onUpload(false);
54         
55         require_once 'File/Convert.php';
56         $fc = new File_Convert($img->getStoreName(), $img->mimetype );
57         //var_Dump($img->getStoreName());
58         $csv = $fc->convert('text/csv');
59         //print_R($fc);exit;
60         //var_dump($csv);exit;
61         $this->importCsv($csv);
62     }
63     function importCsv($csv)
64     {
65         
66         ini_set("auto_detect_line_endings", true);
67
68         $req = array('ITEM CODE', 'FROM', 'QTY', 'SALE PRICE');
69         
70         
71         
72         $fh = fopen($csv, 'r');
73         if (!$fh) {
74             $this->jerr("invalid file");
75         }
76         $bom = "\xEF\xBB\xBF";
77         for ($i = 0; $i< 3;$i++) {
78             if ($bom[$i] != fgetc($fh)) {
79                 fseek($fh,0);
80                 break;
81             }
82             
83         }
84         
85         
86         
87         // we need to break this into cols..
88         $cols = false;
89         $rows = array();
90         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
91              
92             if (!$cols) {
93                 
94                 $cols = array();
95                 foreach($n as $k) {
96                     $k = ($k == 'CUST PRICE') ? 'SALE PRICE' : $k;
97                     $k = ($k == 'SALES PRICE') ? 'SALE PRICE' : $k;
98                     
99                     
100                     $cols[] = strtoupper(trim($k));
101                 }
102                 if (empty($cols)) {
103                     continue;
104                 }
105                 
106                 foreach($req as $r) {
107                     if (!in_array($r,$cols)) {
108                         $cols = false;
109                         break;
110                         
111                     }
112                 }
113                 continue;
114             }
115             $row = array();
116             foreach($cols as $i=>$k) {
117                 $row[$k] = $n[$i];
118             }
119             $rows[] = $row;
120         }
121          
122         if (empty($cols)) {
123             $this->jerr("could not find a row with Item code / from, qty and sale price");
124         }
125         fclose($fh);
126         
127         
128         $cohead = DB_DataObject::Factory('cohead');
129         if (!$cohead->get($_REQUEST['onid'])) {
130             $this->jerr("order is not valid");
131         }
132         
133         // get the pricelist - just in case they did not fill in the prices..
134         
135         $cust = $cohead->cust();
136         $ipshead = $cust->priceList();
137         
138          
139         
140         //DB_DataObject::debugLevel(1);
141         $missing = array();
142         
143         foreach($rows as $row)
144         {
145             if (!strlen(trim(implode('', array_values($row))))) {
146                 continue;
147             }
148             if (!strlen(trim($row['ITEM CODE']))) {
149                 $missing[]  = implode('', array_values($row));
150             }
151             
152             
153             $item = DB_DataObject::factory('item')->lookupSKU($row['ITEM CODE']);
154             if (!$item) {
155                 $missing[] = $row['ITEM CODE'];
156                 continue;
157             }
158             if (!empty($missing)) {
159                 continue;
160             }
161             
162             $itemsite = $item->itemsite();
163             
164             if (empty($row['SALE PRICE']) && !strlen($row['SALE PRICE'])) {
165                 if (!$ipshead) {
166                     $this-jerr("customer does not have a price list set and The SALE PRICE column is empty.");
167                 }
168                 $row['SALE PRICE'] = $ipshead->itemPrice($item);
169             }
170             
171             $citem = DB_DataObject::Factory('coitem');
172             $citem->coitem_cohead_id =  $cohead->pid();
173             $citem->setFrom($citem->defaults());
174             $citem->setFrom(array(
175                 
176                 'coitem_itemsite_id' => $itemsite->pid(),
177                 'coitem_location_src' => $this->location($row['FROM']),
178                 'coitem_qtyord' => $row['QTY'],
179                 'coitem_price' => $row['SALE PRICE'],
180                 'coitem_custprice' => $row['SALE PRICE'],
181                 // custprice????
182             ));
183             $citem->coitem_shipto_id = $cohead->cohead_shipto_id;
184             $citem->coitem_linenumber = $this->nextline($cohead);
185             $citem->insert();
186             
187         }
188         if (!empty($missing)) {
189             $this->jerr("missing these codes : " . implode("\n", $missing));
190         }
191         
192         $this->jok("IMPORTED");
193          
194     
195     }
196     
197     function itemsite($code) {
198         $t = DB_DataObject::Factory('itemsite');
199         $t->query("
200                   SELECT itemsite_id FROM itemsite LEFT JOIN item ON itemsite_item_id = item_id
201                   WHERE item_number = '{$t->escape($code)}'
202                   LIMIT 1;
203                   ");
204         if (!$t->fetch() || !$t->itemsite_id) {
205             $this->jerr("item not found " . $code);
206         }
207         return $t->itemsite_id;
208     }
209     function location($loc) {
210         $l = DB_DataObject::Factory('location');
211         if (!$l->get('location_name', $loc)) {
212             $this->jerr("could not find location $loc");
213         }
214         return $l->pid();
215     }
216     
217     function nextline($cohead)
218     {
219         static $line = false;
220         if ($line !== false) {
221             $line++;
222             return $line;
223         }
224         $t = DB_DataObject::Factory('coitem');
225         $t->selecTAdd();
226         $t->coitem_cohead_id = $cohead->pid();
227         $t->selectAdd('MAX(coitem_linenumber) as coitem_linenumber');
228         if (!$t->find(true)) {
229             $line = 1;
230             return $line;
231         }
232         $line = $t->coitem_linenumber +1;
233         return $line;
234     }
235         
236         
237         
238          
239 }