cli) { return true; } return parent::getAuth(); } function post() { $this->transObj = DB_DataObject::Factory('cohead'); $this->transObj->query('BEGIN'); PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError')); $img = DB_DataObject::Factory('images'); $img->setFrom(array( 'onid' => 0, 'ontable' => 'ipshead' )); $img->onUpload(false); // print_r($img->getStoreName());exit; require_once 'File/Convert.php'; $fc = new File_Convert($img->getStoreName(), $img->mimetype ); $csv = $fc->convert('text/csv'); $this->importCsv($csv); } function importCsv($csv) { ini_set("auto_detect_line_endings", true); $fh = fopen($csv, 'r'); if (!$fh) { $this->jerr("invalid file"); } $req = array( '訂單來源', '訂單編號', '運單號', '商品名稱', '商家編碼', '商品數量', '買家暱稱', '發貨方式', '運單打印狀態', '運單打印時間', '收件人姓名', '收件人地區', '收件人詳細地址', '收件人手機', '收件人電話', '保價/聲明價值', '訂單類型', '業務類型', '付款方式', '重量', '代收貨款金額', '成交時間', '發貨時間', '賣家備註', '買家留言', '訂單自定義1', '訂單自定義2', '訂單自定義3' ); $cols = false; $rows = array(); while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) { if (!strlen(implode('', $n))) { continue; } if (!$cols) { $cols = array(); foreach($n as $k) { $cols[] = strtoupper(trim($k)); } if (empty($cols)) { continue; } foreach($req as $r) { if (!in_array($r,$cols)) { $cols = false; break; } } continue; } foreach($cols as $i=>$k) { $row[$k] = trim($n[$i]); } $rows[] = $row; } if (empty($cols)) { $this->jerr("could not find a row with " . implode(' / ', $req)); } fclose($fh); $msg = array(); $processed = array(); foreach ($rows as $row){ if(empty($row['賣家備註']) || empty($row['運單號'])){ $msg[] = ((!empty($row['賣家備註'])) ? $row['賣家備註'] : (!empty($row['運單號'])) ? $row['運單號'] : '' ) . 'missing infomations'; continue; } if(!isset($processed[$row['賣家備註']])){ $processed[$row['賣家備註']] = array( 'waybill' => $row['運單號'], 'qty' => (int)$row['商品數量'] ); continue; } $processed[$row['賣家備註']]['qty'] += (int)$row['商品數量']; } foreach ($processed as $k => $v){ $cohead = DB_DataObject::factory('cohead'); $cohead->selectAdd(" calcsalesorderqty(cohead_id::integer) AS qty "); if(!$cohead->get('cohead_number', $k)){ $msg[] = $k . " - no matching order"; continue; } $shiphead = DB_DataObject::factory('shiphead'); $shiphead->whereAdd(" shiphead_order_id = {$cohead->pid()} AND shiphead_shipdate IS NOT NULL AND NOT shiphead_shipped "); if(!$shiphead->find(true)){ $msg[] = $cohead->cohead_number . " - no matching shipment"; continue; } if((int)$cohead->qty != (int)$v['qty']){ $msg[] = $cohead->cohead_number . " - no matching qty"; continue; } $old = clone ($shiphead); $shiphead->shiphead_delivery_note = $v['waybill']; $shiphead->update($old); $this->transObj->lockTables(); $res = $shiphead->confirm($this); if (true !== $res) { $msg[] = $cohead->cohead_number . " - confirm shipment error"; continue; } $cohead->shipment = clone ($shiphead); $cohead->sendShipment($this); $msg[] = $cohead->cohead_number . ' DONE'; } if(!empty($msg)){ $this->jok(implode("
", $msg)); } $this->jok('DONE'); } }