4 require_once 'Pman/Roo.php';
6 class Pman_Xtuple_Import_SalesOrder extends Pman_Roo
10 * get .. same as roo...
16 if (HTML_FlexyFramework::get()->cli) {
19 return parent::getAuth();
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);
39 $this->sessionState(0); // turn off the session..
40 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
42 if (empty($_REQUEST['onid'])) {
43 $this->jerr('no order found');
48 $img = DB_DataObject::Factory('images');
51 'ontable' => 'ipshead'
53 $img->onUpload(false);
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');
60 //var_dump($csv);exit;
61 $this->importCsv($csv);
63 function importCsv($csv)
66 ini_set("auto_detect_line_endings", true);
68 $req = array('ITEM CODE', 'FROM', 'QTY', 'SALE PRICE');
72 $fh = fopen($csv, 'r');
74 $this->jerr("invalid file");
76 $bom = "\xEF\xBB\xBF";
77 for ($i = 0; $i< 3;$i++) {
78 if ($bom[$i] != fgetc($fh)) {
87 // we need to break this into cols..
90 while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
96 $k = ($k == 'CUST PRICE') ? 'SALE PRICE' : $k;
97 $k = ($k == 'SALES PRICE') ? 'SALE PRICE' : $k;
100 $cols[] = strtoupper(trim($k));
106 foreach($req as $r) {
107 if (!in_array($r,$cols)) {
116 foreach($cols as $i=>$k) {
123 $this->jerr("could not find a row with Item code / from, qty and sale price");
128 $cohead = DB_DataObject::Factory('cohead');
129 if (!$cohead->get($_REQUEST['onid'])) {
130 $this->jerr("order is not valid");
133 // get the pricelist - just in case they did not fill in the prices..
135 $cust = $cohead->cust();
136 $ipshead = $cust->priceList();
140 //DB_DataObject::debugLevel(1);
143 foreach($rows as $row)
145 if (!strlen(trim(implode('', array_values($row))))) {
148 if (!strlen(trim($row['ITEM CODE']))) {
149 $missing[] = implode('', array_values($row));
153 $item = DB_DataObject::factory('item')->lookupSKU($row['ITEM CODE']);
155 $missing[] = $row['ITEM CODE'];
158 if (!empty($missing)) {
162 $itemsite = $item->itemsite();
164 if (empty($row['SALE PRICE']) && !strlen($row['SALE PRICE'])) {
166 $this-jerr("customer does not have a price list set and The SALE PRICE column is empty.");
168 $row['SALE PRICE'] = $ipshead->itemPrice($item);
171 $citem = DB_DataObject::Factory('coitem');
172 $citem->coitem_cohead_id = $cohead->pid();
173 $citem->setFrom($citem->defaults());
174 $citem->setFrom(array(
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'],
183 $citem->coitem_shipto_id = $cohead->cohead_shipto_id;
184 $citem->coitem_linenumber = $this->nextline($cohead);
188 if (!empty($missing)) {
189 $this->jerr("missing these codes : " . implode("\n", $missing));
192 $this->jok("IMPORTED");
197 function itemsite($code) {
198 $t = DB_DataObject::Factory('itemsite');
200 SELECT itemsite_id FROM itemsite LEFT JOIN item ON itemsite_item_id = item_id
201 WHERE item_number = '{$t->escape($code)}'
204 if (!$t->fetch() || !$t->itemsite_id) {
205 $this->jerr("item not found " . $code);
207 return $t->itemsite_id;
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");
217 function nextline($cohead)
219 static $line = false;
220 if ($line !== false) {
224 $t = DB_DataObject::Factory('coitem');
226 $t->coitem_cohead_id = $cohead->pid();
227 $t->selectAdd('MAX(coitem_linenumber) as coitem_linenumber');
228 if (!$t->find(true)) {
232 $line = $t->coitem_linenumber +1;