Fix #7123 - getting abra ready to test
[Pman.Xtuple] / Import / WayBill.php
1 <?php
2
3 require_once 'Pman/Roo.php';
4
5 class Pman_Xtuple_Import_WayBill extends Pman_Roo
6 {
7     function getAuth()
8     {
9         if (HTML_FlexyFramework::get()->cli) {
10             return true;
11         }
12         return parent::getAuth();
13     }
14     
15     function post()
16     {
17         $this->transObj = DB_DataObject::Factory('cohead');
18         
19         $this->transObj->query('BEGIN');
20         
21         PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($this, 'onPearError'));
22         
23         $img = DB_DataObject::Factory('images');
24         $img->setFrom(array(
25             'onid' => 0,
26             'ontable' => 'ipshead'
27         ));
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');
33         
34         $this->importCsv($csv);
35         
36     }
37     
38     function importCsv($csv)
39     {
40         
41         ini_set("auto_detect_line_endings", true);
42         
43         $fh = fopen($csv, 'r');
44         if (!$fh) {
45             $this->jerr("invalid file");
46         }
47         
48         $req = array(
49             '訂單來源', '訂單編號', '運單號', '商品名稱', '商家編碼',
50             '商品數量', '買家暱稱', '發貨方式', '運單打印狀態', '運單打印時間',
51             '收件人姓名', '收件人地區', '收件人詳細地址', '收件人手機', '收件人電話',
52             '保價/聲明價值', '訂單類型', '業務類型', '付款方式', '重量',
53             '代收貨款金額', '成交時間', '發貨時間', '賣家備註', '買家留言',
54             '訂單自定義1', '訂單自定義2', '訂單自定義3'
55         );
56         
57         $cols = false;
58         $rows = array();
59         
60         while(false !== ($n = fgetcsv($fh,10000, ',', '"'))) {
61             
62             if (!strlen(implode('', $n))) {
63                 continue;
64             }
65             
66             if (!$cols) {
67                 $cols = array();
68                 foreach($n as $k) {
69                     $cols[] = strtoupper(trim($k));
70                 }
71                 
72                 if (empty($cols)) {
73                     continue;
74                 }
75                 
76                 foreach($req as $r) {
77                     if (!in_array($r,$cols)) {
78                         $cols = false;
79                         break;
80                     }
81                 }
82                 continue;
83             }
84             foreach($cols as $i=>$k) {
85                 $row[$k] = trim($n[$i]);
86             }
87             $rows[] = $row;
88         }
89         
90         if (empty($cols)) {
91             $this->jerr("could not find a row with " . implode(' / ', $req));
92         }
93         
94         fclose($fh);
95         
96         $msg = array();
97         
98         $processed = array();
99         
100         foreach ($rows as $row){
101             if(empty($row['賣家備註']) || empty($row['運單號'])){
102                 $msg[] = ((!empty($row['賣家備註'])) ? $row['賣家備註'] : (!empty($row['運單號'])) ? $row['運單號'] : '' ) . 'missing infomations';
103                 continue;
104             }
105             
106             if(!isset($processed[$row['賣家備註']])){
107                 $processed[$row['賣家備註']] = array(
108                     'waybill' => $row['運單號'],
109                     'qty' => (int)$row['商品數量']
110                 );
111                 continue;
112             }
113             
114             $processed[$row['賣家備註']]['qty'] += (int)$row['商品數量'];
115         }
116         
117         
118         foreach ($processed as $k => $v){
119             
120             $cohead = DB_DataObject::factory('cohead');
121             $cohead->selectAdd("
122                 calcsalesorderqty(cohead_id::integer) AS qty
123             ");
124             if(!$cohead->get('cohead_number', $k)){
125                 $msg[] = $k . " - no matching order";
126                 continue;
127             }
128             
129             $shiphead = DB_DataObject::factory('shiphead');
130             $shiphead->whereAdd("
131                     shiphead_order_id = {$cohead->pid()}
132                 AND
133                     shiphead_shipdate IS NOT NULL
134                 AND
135                     NOT shiphead_shipped
136             ");
137             if(!$shiphead->find(true)){
138                 $msg[] = $cohead->cohead_number . " - no matching shipment";
139                 continue;
140             }
141             
142             if((int)$cohead->qty != (int)$v['qty']){
143                 $msg[] = $cohead->cohead_number . " - no matching qty";
144                 continue;
145             }
146             
147             $old = clone ($shiphead);
148             $shiphead->shiphead_delivery_note = $v['waybill'];
149             
150             $shiphead->update($old);
151             
152             $this->transObj->lockTables();
153             
154             $res = $shiphead->confirm($this);
155             if (true !== $res) {
156                 $msg[] = $cohead->cohead_number . " - confirm shipment error";
157                 continue;
158             }
159             
160             $cohead->shipment = clone ($shiphead);
161             $cohead->sendShipment($this);
162             
163             $msg[] = $cohead->cohead_number . ' DONE';
164             
165         }
166         
167         if(!empty($msg)){
168             $this->jok(implode("<br/>", $msg));
169         }
170         
171         $this->jok('DONE');
172         
173     }
174     
175 }