3 require_once 'Pman/Roo.php';
5 class Pman_Xtuple_Import_WayBill extends Pman_Roo
9 if (HTML_FlexyFramework::get()->cli) {
12 return parent::getAuth();
17 $this->transObj = DB_DataObject::Factory('cohead');
19 $this->transObj->query('BEGIN');
21 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
23 $img = DB_DataObject::Factory('images');
26 'ontable' => 'ipshead'
28 $img->onUpload(false);
29 // print_r($img->getStoreName());exit;
30 require_once 'File/Convert.php';
31 $fc = new File_Convert($img->getStoreName(), $img->mimetype );
32 $csv = $fc->convert('text/csv');
34 $this->importCsv($csv);
38 function importCsv($csv)
41 ini_set("auto_detect_line_endings", true);
43 $fh = fopen($csv, 'r');
45 $this->jerr("invalid file");
49 '訂單來源', '訂單編號', '運單號', '商品名稱', '商家編碼',
50 '商品數量', '買家暱稱', '發貨方式', '運單打印狀態', '運單打印時間',
51 '收件人姓名', '收件人地區', '收件人詳細地址', '收件人手機', '收件人電話',
52 '保價/聲明價值', '訂單類型', '業務類型', '付款方式', '重量',
53 '代收貨款金額', '成交時間', '發貨時間', '賣家備註', '買家留言',
54 '訂單自定義1', '訂單自定義2', '訂單自定義3'
60 while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
62 if (!strlen(implode('', $n))) {
69 $cols[] = strtoupper(trim($k));
77 if (!in_array($r,$cols)) {
84 foreach($cols as $i=>$k) {
85 $row[$k] = trim($n[$i]);
91 $this->jerr("could not find a row with " . implode(' / ', $req));
100 foreach ($rows as $row){
101 if(empty($row['賣家備註']) || empty($row['運單號'])){
102 $msg[] = ((!empty($row['賣家備註'])) ? $row['賣家備註'] : (!empty($row['運單號'])) ? $row['運單號'] : '' ) . 'missing infomations';
106 if(!isset($processed[$row['賣家備註']])){
107 $processed[$row['賣家備註']] = array(
108 'waybill' => $row['運單號'],
109 'qty' => (int)$row['商品數量']
114 $processed[$row['賣家備註']]['qty'] += (int)$row['商品數量'];
118 foreach ($processed as $k => $v){
120 $cohead = DB_DataObject::factory('cohead');
122 calcsalesorderqty(cohead_id::integer) AS qty
124 if(!$cohead->get('cohead_number', $k)){
125 $msg[] = $k . " - no matching order";
129 $shiphead = DB_DataObject::factory('shiphead');
130 $shiphead->whereAdd("
131 shiphead_order_id = {$cohead->pid()}
133 shiphead_shipdate IS NOT NULL
137 if(!$shiphead->find(true)){
138 $msg[] = $cohead->cohead_number . " - no matching shipment";
142 if((int)$cohead->qty != (int)$v['qty']){
143 $msg[] = $cohead->cohead_number . " - no matching qty";
147 $old = clone ($shiphead);
148 $shiphead->shiphead_delivery_note = $v['waybill'];
150 $shiphead->update($old);
152 $this->transObj->lockTables();
154 $res = $shiphead->confirm($this);
156 $msg[] = $cohead->cohead_number . " - confirm shipment error";
160 $cohead->shipment = clone ($shiphead);
161 $cohead->sendShipment($this);
163 $msg[] = $cohead->cohead_number . ' DONE';
168 $this->jok(implode("<br/>", $msg));