|
|
@@ -245,20 +245,18 @@ class OrderController extends BaseController
|
|
|
public function actionCreateOrder()
|
|
|
{
|
|
|
$post = Yii::$app->request->post();
|
|
|
- $goodsId = isset($post['goodsId']) ? $post['goodsId'] : 0;
|
|
|
- $goodsStyleId = isset($post['goodsStyleId']) ? $post['goodsStyleId'] : 0;
|
|
|
+ $goodsId = $post['goodsId'] ?? 0;
|
|
|
$goodsNum = isset($post['goodsNum']) && $post['goodsNum'] > 0 ? $post['goodsNum'] : 1;
|
|
|
$sendType = $post['sendType'] ?? dict::getDict('sendType', 'thirdSend');
|
|
|
if (empty($goodsId)) {
|
|
|
util::fail('请选择商品');
|
|
|
}
|
|
|
- $goodsInfo = GoodsClass::getGoodsInfo($goodsId);
|
|
|
+ $goodsInfo = GoodsClass::getById($goodsId, true);
|
|
|
if (empty($goodsInfo)) {
|
|
|
util::fail('没有找到商品');
|
|
|
}
|
|
|
- $stock = $goodsInfo['stock'] && is_numeric($goodsInfo['stock']) ? $goodsInfo['stock'] : 0;
|
|
|
- $stockSet = $goodsInfo['stockSet'] && is_numeric($goodsInfo['stockSet']) ? $goodsInfo['stockSet'] : 0;
|
|
|
- if ($stock <= 0 && $stockSet == 0) {
|
|
|
+ $stock = $goodsInfo->stock ?? 0;
|
|
|
+ if ($stock <= 0) {
|
|
|
util::fail('库存不足');
|
|
|
}
|
|
|
//事务处理
|
|
|
@@ -266,27 +264,25 @@ class OrderController extends BaseController
|
|
|
$transaction = $connection->beginTransaction();
|
|
|
try {
|
|
|
|
|
|
- $custom = CustomClass::getByCondition(['shopId' => $this->shopId, 'userId' => $this->userId], true);
|
|
|
- $customId = $custom->id ?? 0;
|
|
|
- if (!empty($custom)) {
|
|
|
- $post['customId'] = $customId;
|
|
|
- $customName = $custom->name ?? '';
|
|
|
- $post['customName'] = $customName;
|
|
|
- $post['customNamePy'] = stringUtil::py($customName);
|
|
|
- $hdId = $custom->hdId ?? 0;
|
|
|
- $hd = HdClass::getById($hdId, true);
|
|
|
- if (!empty($hd)) {
|
|
|
- $post['hdId'] = $hd->id ?? 0;
|
|
|
- $post['hdName'] = $hd->name ?? '';
|
|
|
- }
|
|
|
+ $custom = $this->custom;
|
|
|
+ if (empty($custom)) {
|
|
|
+ util::fail('个人信息缺失');
|
|
|
}
|
|
|
+ $customId = $custom->id ?? 0;
|
|
|
+ $hd = $this->hd;
|
|
|
+ $post['customId'] = $customId;
|
|
|
+ $customName = $custom->name ?? '';
|
|
|
+ $post['customName'] = $customName;
|
|
|
+ $post['customNamePy'] = stringUtil::py($customName);
|
|
|
+ $post['hdId'] = $hd->id ?? 0;
|
|
|
+ $post['hdName'] = $hd->name ?? '';
|
|
|
|
|
|
- $user = isset($this->user->attributes) ? $this->user->attributes : [];
|
|
|
- $post['bookName'] = isset($user['userName']) ? $user['userName'] : '';
|
|
|
- $bookMobile = isset($post['bookMobile']) && !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
|
|
|
- $bookMobile = empty($bookMobile) && isset($user['mobile']) && !empty($user['mobile']) ? $user['mobile'] : $bookMobile;
|
|
|
+ $user = $this->user;
|
|
|
+ $post['bookName'] = $user->name ?? '';
|
|
|
+ $bookMobile = !empty($post['bookMobile']) && stringUtil::isMobile($post['bookMobile']) ? $post['bookMobile'] : '';
|
|
|
+ $bookMobile = empty($bookMobile) && !empty($user->mobile) ? $user->mobile : $bookMobile;
|
|
|
$post['bookMobile'] = $bookMobile;
|
|
|
- $post['payWay'] = $this->isWx == true ? 0 : 1;
|
|
|
+ $post['payWay'] = 0;
|
|
|
|
|
|
//计算运费
|
|
|
$sendDistance = 0;
|
|
|
@@ -322,38 +318,27 @@ class OrderController extends BaseController
|
|
|
$post['mainId'] = $mainId;
|
|
|
$post['userId'] = $this->userId;
|
|
|
//计算总金额
|
|
|
- $unitPrice = $goodsInfo['price'];
|
|
|
- $goodsStyleList = isset($goodsInfo['goodsStyleList']) ? $goodsInfo['goodsStyleList'] : [];
|
|
|
- if (!empty($goodsStyleId)) {
|
|
|
- if (empty($goodsStyleList)) {
|
|
|
- util::fail('商品款式没有找到');
|
|
|
- }
|
|
|
- $styleIdList = array_keys($goodsStyleList);
|
|
|
- if (in_array($goodsStyleId, $styleIdList) == false) {
|
|
|
- util::fail('商品款式没有找到!');
|
|
|
- }
|
|
|
- $unitPrice = isset($goodsStyleList[$goodsStyleId]['price']) ? $goodsStyleList[$goodsStyleId]['price'] : 9999;
|
|
|
+ $unitPrice = $goodsInfo->price ?? 0;
|
|
|
+ if ($unitPrice <= 0) {
|
|
|
+ util::fail('没有价格,无法下单');
|
|
|
}
|
|
|
$goodsPrice = $unitPrice * $goodsNum;
|
|
|
$prePrice = stringUtil::calcAdd($sendCost, $goodsPrice);
|
|
|
//非门店订单
|
|
|
$post['store'] = 0;
|
|
|
- //来源 0微信 1支付宝 2小程序 3朋友圈 4美团
|
|
|
- $sourceType = $this->isWx ? 0 : 1;
|
|
|
- if (httpUtil::isMiniProgram()) {
|
|
|
- $sourceType = 2;
|
|
|
- }
|
|
|
- $couponId = isset($post['couponId']) && !empty($post['couponId']) ? $post['couponId'] : 0;
|
|
|
+ $sourceType = 2;
|
|
|
+ $couponId = !empty($post['couponId']) ? $post['couponId'] : 0;
|
|
|
$discountData = OrderService::getDiscountPrice(['price' => $prePrice, 'sourceType' => $sourceType, 'couponId' => $couponId, 'userId' => $this->userId]);
|
|
|
$actPrice = $discountData['price'];
|
|
|
+ $post['modifyPrice'] = $actPrice;
|
|
|
$post['discountType'] = $discountData['discountType'];
|
|
|
$post['discountAmount'] = $discountData['discountAmount'];
|
|
|
|
|
|
//非自取订单考虑配送时间
|
|
|
- $post['reachDate'] = isset($post['reachDate']) && !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
|
|
|
+ $post['reachDate'] = !empty($post['reachDate']) ? $post['reachDate'] : date("Y-m-d");
|
|
|
$sendType = $post['sendType'] ?? 0;
|
|
|
if ($sendType != 1) {
|
|
|
- if (isset($post['reachPeriod']) == false || empty($post['reachPeriod'])) {
|
|
|
+ if (empty($post['reachPeriod'])) {
|
|
|
util::fail('请选择配送时间');
|
|
|
}
|
|
|
$post['reachTime'] = strtotime($post['reachDate'] . ' ' . $post['reachPeriod']);
|
|
|
@@ -373,7 +358,10 @@ class OrderController extends BaseController
|
|
|
$post['cash'] = 0;
|
|
|
$post['mainId'] = $mainId;
|
|
|
$post['needPrint'] = dict::getDict('needPrint', 'need');
|
|
|
- $return = OrderClass::addOrder($post);
|
|
|
+ $hasPay = 0;
|
|
|
+ $product = [['productId' => $goodsId, 'unitType' => 0, 'property' => 0, 'unitPrice' => $unitPrice, 'num' => $goodsNum]];
|
|
|
+ $post['product'] = $product;
|
|
|
+ $return = \bizHd\order\services\OrderService::createHdOrder($post, $custom, $hasPay);
|
|
|
$orderId = $return->id;
|
|
|
$orderSn = $return->orderSn;
|
|
|
$transaction->commit();
|