$val) { if (!isset($val['property'])) { util::fail('没有找到产品属性'); } $property = $val['property']; $unitType = $val['unitType'] ?? 0; $currentId = $val['productId'] ?? 0; $num = $val['num'] ?? 0; $unitPrice = 0; $currentWeight = 0; if ($property == dict::getDict('property', 'goods')) { $name = $goodsInfo[$currentId]['name'] ?? ''; $cover = $goodsInfo[$currentId]['cover'] ?? ''; $flower = $goodsInfo[$currentId]['flower'] ?? 0; $currentWeight = $goodsInfo[$currentId]['weight'] ?? 0; if (!empty($groupCover)) { $groupCover = $cover; } $currentPrice = $goodsInfo[$currentId]['price'] ?? 0; $currentMainId = $goodsInfo[$currentId]['mainId'] ?? 0; $unitPrice = $currentPrice; if ($currentMainId != $mainId) { util::fail('不是您的商品'); } $currentGoodsData = [ 'orderSn' => $orderSn, 'goodsId' => $currentId, 'flower' => $flower, 'customId' => $customId, 'hdId' => $hdId, 'sjId' => $sjId, 'shopId' => $shopId, 'name' => $name, 'cover' => $cover, 'unitPrice' => $unitPrice, 'num' => $num, 'price' => bcmul($unitPrice, $num, 2), 'mainId' => $mainId, ]; //库存变化在这里 OrderGoodsClass::addData($currentGoodsData); } if ($property == dict::getDict('property', 'item')) { $currentItemInfo = $itemInfo[$currentId] ?? []; $name = $itemInfo[$currentId]['name'] ?? ''; $cover = $itemInfo[$currentId]['cover'] ?? ''; if (!empty($groupCover)) { $groupCover = $cover; } $ratio = $itemInfo[$currentId]['ratio'] ?? 20; $currentMainId = $itemInfo[$currentId]['mainId'] ?? 0; $currentPrice = \bizGhs\product\classes\ProductClass::getFinalPrice($currentItemInfo, 0, $priceMap, $addPriceMap); $currentWeight = $itemInfo[$currentId]['weight'] ?? 0; if ($currentMainId != $mainId) { util::fail('不是您的花材' . $mainId . ' ' . $currentMainId); } if ($unitType == dict::getDict('unitType', 'big')) { $unitName = $itemInfo[$currentId]['bigUnit'] ?? ''; $unitId = $itemInfo[$currentId]['bigUnitId'] ?? 0; $totalNum = $num; $unitPrice = $currentPrice; } else { $unitName = $itemInfo[$currentId]['smallUnit'] ?? ''; $unitId = $itemInfo[$currentId]['smallUnitId'] ?? 0; $totalNum = bcdiv($num, $ratio, 2); $div = bcdiv($currentPrice, $ratio, 2); $smallRatio = $itemInfo[$currentId]['smallRatio'] ?? 0; $unitPrice = bcmul($div, $smallRatio, 2); } $ptItemId = $itemInfo[$currentId]['itemId'] ?? 0; $classId = $itemInfo[$currentId]['classId'] ?? 0; $belongCost = $itemInfo[$currentId]['belongCost'] ?? 0; $currentItemData = [ 'name' => $name, 'cover' => $cover, 'ratio' => $ratio, 'unitType' => $unitType, 'unitName' => $unitName, 'unitId' => $unitId, 'unitPrice' => $unitPrice, 'num' => $num, 'orderSn' => $orderSn, 'itemId' => $currentId, 'ptItemId' => $ptItemId, 'totalNum' => $totalNum, 'totalPrice' => 0, 'customId' => $customId, 'hdId' => $hdId, 'sjId' => $sjId, 'shopId' => $shopId, 'mainId' => $mainId, 'price' => bcmul($unitPrice, $num, 2), 'classId' => $classId, 'belongCost' => $belongCost, ]; //库存变化在这里 OrderItemClass::addData($currentItemData, $custom); } $weight = bcadd($weight, $currentWeight, 2); $goodsPrice = bcadd($goodsPrice, bcmul($unitPrice, $num, 2), 2); } //计算运费 $sendDistance = 0; $sendCost = 0; $sendType = $data['sendType'] ?? dict::getDict('sendType', 'thirdSend'); if ($sendType == dict::getDict('sendType', 'thirdSend')) { $lat = $data['lat'] ?? ''; $lng = $data['long'] ?? ''; if (empty($lat) || empty($lng)) { //util::fail('请填写收花地址...'); } $shopLat = $shop->lat ?? ''; $shopLong = $shop->long ?? ''; if (empty($shopLat) || empty($shopLong)) { //util::fail('请完成门店地址'); } $disRespond = OrderClass::getDistanceFee($lat, $lng, $shopLat, $shopLong, $weight); $sendCost = $disRespond['fee'] ?? 0; $sendDistance = $disRespond['distance'] ?? 0; } $data['sendDistance'] = $sendDistance; $data['sendCost'] = $sendCost; $totalPrice = bcadd($goodsPrice, $sendCost, 2); $modifyPrice = $totalPrice; if ($modifyPrice > $totalPrice) { //人工资材包装费 $data['labourCost'] = bcsub($modifyPrice, $totalPrice, 2); } if ($modifyPrice < $totalPrice) { //优惠 $data['discountType'] = dict::getDict('discountType', 'discount'); $discountAmount = bcsub($totalPrice, $modifyPrice, 2); $data['discountAmount'] = $discountAmount; } $data['goodsPrice'] = $goodsPrice; $data['prePrice'] = $totalPrice; $data['orderPrice'] = $modifyPrice; $data['actPrice'] = $modifyPrice; $data['realPrice'] = $modifyPrice; $data['mainPay'] = $modifyPrice; $data['cash'] = 0; $data['customId'] = $customId; $data['hdId'] = $hdId; $data['sjId'] = $sjId; $data['shopId'] = $shopId; $data['mainId'] = $mainId; $data['payWay'] = dict::getDict('payWay', 'unPay'); $data['orderSn'] = $orderSn; $data['status'] = OrderClass::ORDER_STATUS_UN_PAY; return OrderClass::addOrder($data); } //获取订单信息 TODO 重新实现,要限制查询字段与返回字段,性能第一???? -- 商品项(商品规格)、商品数量 public static function getOrderList($where) { $fields = 'id, shopId, hdId, orderSn, groupBuyId, orderType, actPrice, goodsNum, reachDate, reachPeriod, status, addTime'; $data = self::getList($fields, $where, 'addTime DESC'); if (empty($data['list'])) { return $data; } $list = $data['list']; $hdIds = array_values(array_filter(array_unique(array_column($list, 'hdId')))); $hdMap = []; if (!empty($hdIds)) { $hdMap = HdClass::getByIds($hdIds, null, 'id'); } $orderSns = array_values(array_filter(array_unique(array_column($list, 'orderSn')))); // 花束订单明细在 xhOrderGoods,花材订单明细在 xhOrderItem,orderType=3 时两者都有 $goodsListMap = OrderGoodsService::getGoodsListByOrderSns($orderSns); $itemListMap = OrderItemService::getItemListByOrderSns($orderSns); $periodData = [0 => '上午', 1 => '下午', 2 => '晚上']; foreach ($data['list'] as $key => $val) { $orderSn = $val['orderSn'] ?? ''; $orderType = intval($val['orderType'] ?? 1); $hdId = intval($val['hdId'] ?? 0); $orderStatus = intval($val['status'] ?? 0); $data['list'][$key]['hdName'] = $hdMap[$hdId]['name'] ?? ''; $data['list'][$key]['statusDesc'] = OrderStatusUtil::getSystemStatusDesc($orderStatus); $goodsInfoList = []; if (!empty($orderSn)) { if ($orderType == 1) { $goodsInfoList = $goodsListMap[$orderSn] ?? []; } elseif ($orderType == 2) { $goodsInfoList = $itemListMap[$orderSn] ?? []; } elseif ($orderType == 3) { $goodsInfoList = array_merge($goodsListMap[$orderSn] ?? [], $itemListMap[$orderSn] ?? []); } } if(isset($val['goodsNum']) && $val['goodsNum'] == 0) { $data['list'][$key]['goodsNum'] = count($goodsInfoList); } $data['list'][$key]['goodsInfoList'] = $goodsInfoList; $reachTime = OrderClass::getReachTime($val); $data['list'][$key]['reachTime'] = $reachTime; $requireTime = ''; if ($val['reachDate'] != '0000-00-00') { $reachPeriodId = $val['reachPeriod']; $reachPeriod = isset($periodData[$reachPeriodId]) ? $periodData[$reachPeriodId] : '上午'; $requireTime = date("m-d", strtotime($val['reachDate'])) . ' ' . $reachPeriod; } $data['list'][$key]['requireReachTime'] = $requireTime; $data['list'][$key]['smallHdAvatar'] = imgUtil::groupImg('') . "?x-oss-process=image/resize,m_fill,h_80,w_80"; } return $data; } //取客户最近若干条订单 ssh 2019.11.30 public static function getUserOrder($userId, $limit = 10, $order = null, $with = null) { $where = ['userId' => $userId, 'payStatus' => 1]; return self::getLimitList('*', $where, $limit, $order, $with); } //支付前验证订单 ssh 2019.12.6 public static function checkBeforePay($order) { $now = time(); if (empty($order)) { util::fail('订单无效'); } if ($order['status'] != 1) { util::fail('订单不是待付款状态'); } if ($order['deadline'] <= ($now - 60)) { util::fail('订单已过期'); } if (ceil($order['actPrice']) <= 0) { util::fail('订单金额有问题'); } if ($order['payStatus'] == 1) { util::fail('您已经付过了'); } } //根据编号查询 ssh 2019.12.14 public static function getByOrderSn($orderSn) { return OrderClass::getByOrderSn($orderSn); } //点评 ssh 2019.12.16 public static function comment($data) { return OrderClass::comment($data); } //订单归类 public static function classify($id, $categoryId, $usageId) { $connection = Yii::$app->db; $transaction = $connection->beginTransaction(); try { $order = OrderClass::getById($id, true); if ($order->payStatus != 1) { util::fail('订单还未付款'); } if ($order->classify == 1) { util::fail('已经归类过了'); } $order->categoryId = $categoryId; $order->usageId = $usageId; $order->classify = 1; $order->save(); //设置流水的分类和用途并分配资金 $setData = [ 'sjId' => $order->sjId, 'capitalId' => $order->capitalId, 'time' => $order->payTime, 'usageId' => $order->usageId, 'categoryId' => $order->categoryId, 'amount' => $order->actPrice, 'payWay' => $order->payWay, ]; MerchantCapitalService::orderClassify($setData); $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); util::fail('没有归类成功'); } } //更新配送单 ssh 2019.12.17 public static function updateSheet($order, $data) { $id = $order['id']; if ($order['status'] != OrderClass::ORDER_STATUS_UN_SEND) { util::fail('订单当前不是待配送状态'); } $data['deliver'] = 1; OrderClass::updateById($id, $data); } //订单详情 ssh 2020.1.2 public static function getOrderById($id) { return OrderClass::getOrderById($id); } //查看订单详情 ssh 2020.1.4 public static function getOrderBySn($orderSn) { return OrderClass::getOrderBySn($orderSn); } //计算优惠 ssh 2020.3.9 public static function getDiscountPrice($params) { $couponId = isset($params['couponId']) ? $params['couponId'] : 0; $userId = isset($params['userId']) ? $params['userId'] : 0; $price = isset($params['price']) ? $params['price'] : 0; $sourceType = $params['sourceType'] ?? 0; $discountAmount = 0; $discountType = 0; $discountPrice = $price; //使用优惠券 if (!empty($couponId)) { $discountAmount = CouponService::checkBeforeUse($couponId, $price, $userId); $discountPrice = stringUtil::calcSub($price, $discountAmount); $discountType = 1; } else { //使用会员 $asset = UserAssetClass::getByUserId($userId); if (isset($asset['member']) && $asset['member'] > 0) { $currentDiscount = $asset['discount']; $currentPrice = $price * $currentDiscount; $newPrice = substr(sprintf("%.3f", $currentPrice), 0, -1); $discountType = 2; $discountAmount = stringUtil::calcSub($price, $newPrice); $discountPrice = $newPrice; } } //如果没有优惠券和会员优惠,并且是朋友圈收款,则进行随机优惠 ssh 2019.9.12 if ($discountPrice == $price && $sourceType == 3 && 1 == 2) { $discountAmount = OrderClass::getRandDiscount($price); //开发和测试环境直接随机优惠 $env = getenv('YII_ENV'); if ($env == 'local' || $env == 'test') { $discountAmount = 0.01; } $discountPrice = stringUtil::calcSub($price, $discountAmount); $discountType = 3; } $discountType = $discountAmount <= 0 ? 0 : $discountType; $discountPrice = $discountPrice <= 0 ? 0.01 : $discountPrice; return ['price' => $discountPrice, 'discountType' => $discountType, 'discountAmount' => $discountAmount]; } }