Abstract.php
7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
/**
* Created by PhpStorm.
* User: ziy
* Date: 14-7-22
* Time: 下午5:05
*/
abstract class YHMCart_Orders_Abstract extends YHMCart_Orders_Dispatch implements YHMCart_Orders_Interface
{
protected $orderType = 1;
protected $uid = 0;
protected $agreementData = array(
'agreement_key' => '',
'buyer_uid' => 0
);
/**
* agreement 协议价
* ordinary 普通、常规价格
* @var string
*/
protected $buyingPatterns = 'ordinary';
public function __construct($uid, $orderType = 1)
{
$this->uid = $uid;
$this->orderType = $orderType;
}
/**
* 设置默认的字典
*
* @var array
*/
private $orderDictionary = array(
'want_invoice' => 'N', #是否开发票
'payment_type' => 1,
'bank_code' => '',
'is_payed' => 'N',
'order_status' => 100,
'order_remark' => ''
);
/**
* 订单必选字典
* @var array
*/
private $orderMandatoryDictionary = array(
'buyer_uid',
'seller_uid',
'store_id',
'order_amount',
'last_order_amount',
'shipping_fee',
'last_shipping_fee',
'buying_way',
'payment_id'
);
/**
* 初始化订单数据
* @param array $data
* @return array
*/
public function initOrdersData(array $data)
{
$data['order_type'] = $this->orderType;
$_orderData = array(
'buying_way' => $data['buying_way']
);
$lastOrderData = array();
foreach ($this->orderMandatoryDictionary as $key => $val) {
$_orderData[$val] = '';
if (isset($data[$val])) {
$_orderData[$val] = $data[$val];
}
}
foreach ($this->orderDictionary as $odKey => $odVal) {
if (isset($data[$odKey])) {
$_orderData[$odKey] = $data[$odKey];
if (empty($_orderData[$odKey])) {
$_orderData[$odKey] = $odVal;
}
}
}
if (!empty($data['goods_list'])) {
$goodsData = $this->initOrdersGoods($data['goods_list']);
if (empty($goodsData)) {
throw new Exception('订单商品数据不能为空.');
}
foreach ($goodsData as $key => $storeGoods) {
$orderCode = YHMCart_Library_Number::getOrderCode($this->uid);
$_orderData['order_code'] = $orderCode;
$_orderData['goods_list'] = $storeGoods['goods_list'];
$_orderData['order_amount'] = $storeGoods['order_amount'];
$_orderData['last_order_amount'] = $storeGoods['last_order_amount'];
$_orderData['seller_uid'] = $storeGoods['seller_uid'];
$_orderData['store_id'] = $storeGoods['store_id'];
$lastOrderData[] = $_orderData;
}
unset($goodsData);
}
return $lastOrderData;
}
/**
* 初始化订单商品
* @param array $orderGoods
* @param $order_code
* @return array
*/
public function initOrdersGoods(array $orderGoods)
{
$_orderGoods = array();
/**
* $orderGoods[] = array(
* 'product_sku' => 2,
* 'buy_number' => 1
* );
* $orderGoods[] = array(
* 'product_sku' => 3,
* 'buy_number' => 1
* );
*/
foreach ($orderGoods as $key => $goods) {
if (empty($goods['product_sku'])) {
continue;
}
$stock = YHMProduct_Models_Stock_Client::getStockBySku($goods['product_sku']);
if (empty($stock)) {
continue;
}
$goodsInfo = YHMProduct_Models_Goods_Client::getOneByProductSkc($stock['product_skc']);
$lastPrice = empty($goodsInfo['last_price']) ? $goodsInfo['sale_price'] : $goodsInfo['last_price'];
if ($this->buyingPatterns == 'agreement') {
$agreementInfo = YHMOrders_Models_Shopping_Client::getShoppingChangePrice($this->agreementData['agreement_key'], $this->agreementData['buyer_uid']);
$lastPrice = empty($agreementInfo) ? $goodsInfo['sale_price'] : $agreementInfo['agreement_price'];
}
$storeGoods = array(
'store_id' => $goodsInfo['store_id'],
'seller_uid' => $goodsInfo['uid']
);
if (empty($_orderGoods[$goodsInfo['store_id']])) {
$orderAmount = ($lastPrice * (int)$goods['buy_number']);
$storeGoods['order_amount'] = $orderAmount;
$storeGoods['last_order_amount'] = $orderAmount;
} else {
$orderAmount = $_orderGoods[$goodsInfo['store_id']]['order_amount'] + ($lastPrice * (int)$goods['buy_number']);
$lastOrderAmount = $_orderGoods[$goodsInfo['store_id']]['last_order_amount'] + ($lastPrice * (int)$goods['buy_number']);
$storeGoods['order_amount'] = $orderAmount;
$storeGoods['last_order_amount'] = $lastOrderAmount;
}
$storeGoods['goods_list'][] = array(
'product_skc' => $stock['product_skc'],
'product_sku' => $stock['product_sku'],
'buy_number' => $goods['buy_number'],
'goods_type' => empty($goods['goods_type']) ? 1 : $goods['goods_type'],
'quality_level' => $goodsInfo['quality_level'],
'sale_price' => $goodsInfo['sale_price'],
'last_price' => $lastPrice,
'store_id' => $goodsInfo['store_id'],
'color_id' => $goodsInfo['color_id'],
'brand_id' => $goodsInfo['brand_id']
);
if (empty($_orderGoods[$goodsInfo['store_id']])) {
$_orderGoods[$goodsInfo['store_id']] = $storeGoods;
} else {
$_orderGoods[$goodsInfo['store_id']]['goods_list'] = array_merge($_orderGoods[$goodsInfo['store_id']]['goods_list'], $storeGoods['goods_list']);
unset($storeGoods['goods_list']);
$_orderGoods[$goodsInfo['store_id']] = array_merge($_orderGoods[$goodsInfo['store_id']], $storeGoods);
}
}
return $_orderGoods;
}
/**
* 是否需要支付
* @param array $orderData
* @return bool|mixed
*/
public function needPay(array $orderData)
{
if ((int)$orderData['last_order_amount'] != 0) {
return true;
}
return false;
}
/**
* 购买方式、形式
* @param $patterns
* @return $this
*/
public function buyingPatterns($patterns)
{
$this->buyingPatterns = $patterns;
return $this;
}
/**
* 设置协议KEY
* @param array $agreementData
* @return $this
*/
public function setAgreement(array $agreementData)
{
$this->agreementData = $agreementData;
return $this;
}
/**
* 支付URL
* @param $order_code
* @return string
*/
static function getPayUrl($order_code)
{
return YHMCart_Config::$payUrl . $order_code;
}
}