id ?? 0; $hdId = $custom->hdId ?? 0; $userId = $custom->userId ?? 0; $sjId = $data['sjId'] ?? 0; $shopId = $data['shopId'] ?? 0; $mainId = $data['mainId'] ?? 0; $forward = $data['forward'] ?? 0; $dj = $data['dj'] ?? 0; if ($dj > 0) { util::fail('请去掉订金,不再支持订金方式'); } $forwardStock = $data['forwardStock'] ?? 0; $product = $data['product'] ?? []; //组合 $groupId = $data['groupId'] ?? 0; if (!empty($groupId)) { util::fail('暂不支持使用组合功能'); } if (empty($product)) { util::fail('没有商品'); } //如果开单没有给预订人和预订人手机号,则给设置 if (empty($data['bookMobile'])) { $data['bookMobile'] = $custom->mobile; } if (empty($data['bookName'])) { $data['bookName'] = $custom->name; } //如果开单没有传收花人信息,则使用订花人的信息 if (empty($data['receiveUserName'])) { $data['receiveUserName'] = $custom->name; } if (empty($data['receiveMobile'])) { $data['receiveMobile'] = $custom->mobile; } if (empty($data['address'])) { $data['address'] = $custom->address; $data['province'] = $custom->province; $data['city'] = $custom->city; $data['floor'] = $custom->floor; $data['fullAddress'] = $custom->fullAddress; $data['showAddress'] = $custom->showAddress; $data['long'] = $custom->long; $data['lat'] = $custom->lat; } $currentGoodsIds = []; $currentItemIds = []; foreach ($product as $key => $val) { $currentId = $val['productId'] ?? 0; $property = $val['property']; if ($property == dict::getDict('property', 'goods')) { $currentGoodsIds[] = $currentId; } if ($property == dict::getDict('property', 'item')) { $currentItemIds[] = $currentId; } } $goodsInfo = []; $currentGoodsIds = array_unique(array_filter($currentGoodsIds)); if (!empty($currentGoodsIds)) { $goodsInfo = GoodsClass::getByIds($currentGoodsIds, null, 'id'); } $itemInfo = []; $currentItemIds = array_unique(array_filter($currentItemIds)); if (!empty($currentItemIds)) { $itemInfo = ItemClass::getByIds($currentItemIds, null, 'id'); } $goodsPrice = 0; $manyType = count($product) > 1 ? 1 : 0; $totalWeight = 0; $orderCover = ''; foreach ($product as $key => $val) { if (!isset($val['property'])) { util::fail('没有找到产品属性'); } $property = $val['property']; $unitPrice = $val['unitPrice'] ?? 0; $unitType = $val['unitType'] ?? 0; $currentId = $val['productId'] ?? 0; $num = $val['num'] ?? 0; //修改订单会出现数量为0的情况 if ($num <= 0) { continue; } $currentPrice = bcmul($unitPrice, $num, 2); $goodsPrice = bcadd($goodsPrice, $currentPrice, 2); if ($property == dict::getDict('property', 'goods')) { $currentGoodsInfo = $goodsInfo[$currentId] ?? []; if (empty($currentGoodsInfo)) { util::fail("花束商品没有找到,编号{$currentId}"); } $name = $currentGoodsInfo['name'] ?? ''; $cover = $currentGoodsInfo['cover'] ?? ''; $flower = $currentGoodsInfo['flower'] ?? 0; $kindId = $currentGoodsInfo['kindId'] ?? 0; $kindName = $currentGoodsInfo['kindName'] ?? 0; $sn = $currentGoodsInfo['sn'] ?? ''; if (empty($orderCover)) { $orderCover = $cover; } $currentMainId = $currentGoodsInfo['mainId'] ?? 0; if ($currentMainId != $mainId) { util::fail('不是您的商品'); } $weight = $currentGoodsInfo['weight'] ?? 0; $currentWeight = bcmul($weight, $num, 2); $totalWeight = bcadd($totalWeight, $currentWeight, 2); $currentGoodsData = [ 'sn' => $sn, '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, 'kindId' => $kindId, 'kindName' => $kindName, 'forward' => $forward, 'forwardStock' => $forwardStock, ]; //占用库存在这里 OrderGoodsClass::addData($currentGoodsData, $custom, $goodsInfo); } if ($property == dict::getDict('property', 'item')) { $currentInfo = $itemInfo[$currentId] ?? []; $name = $currentInfo['name'] ?? ''; $cover = $currentInfo['cover'] ?? ''; if (empty($orderCover)) { $orderCover = $cover; } $ratio = $currentInfo['ratio'] ?? 20; $currentMainId = $currentInfo['mainId'] ?? 0; if ($currentMainId != $mainId) { util::fail('不是您的花材' . $mainId . ' ' . $currentMainId); } $weight = $currentInfo['weight'] ?? 0; $currentWeight = bcmul($weight, $num, 2); $totalWeight = bcadd($totalWeight, $currentWeight, 2); if ($unitType == dict::getDict('unitType', 'big')) { $unitName = $currentInfo['bigUnit'] ?? ''; $unitId = $currentInfo['bigUnitId'] ?? 0; $totalNum = $num; } else { $unitName = $currentInfo['smallUnit'] ?? ''; $unitId = $currentInfo['smallUnitId'] ?? 0; $totalNum = bcdiv($num, $ratio, 2); } $ptItemId = $currentInfo['itemId'] ?? 0; $classId = $currentInfo['classId'] ?? 0; $belongCost = $currentInfo['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, 'classId' => $classId, 'price' => bcmul($unitPrice, $num, 2), 'belongCost' => $belongCost, 'forward' => $forward, 'forwardStock' => $forwardStock, ]; //库存变化在这里 OrderItemClass::addData($currentItemData, $custom); } } //运费 $data['sendCost'] = isset($data['sendCost']) && $data['sendCost'] > 0 ? $data['sendCost'] : 0; $totalPrice = bcadd($goodsPrice, $data['sendCost'], 2); //人工资材包装费 $labourCost = isset($data['labourCost']) && $data['labourCost'] > 0 ? $data['labourCost'] : 0; $totalPrice = bcadd($totalPrice, $labourCost, 2); //打包费 $packingFee = isset($data['packingFee']) && $data['packingFee'] > 0 ? $data['packingFee'] : 0; $totalPrice = bcadd($totalPrice, $packingFee, 2); //美团服务费和手续费 $serviceFee = isset($data['serviceFee']) && $data['serviceFee'] > 0 ? $data['serviceFee'] : 0; $totalPrice = bcadd($totalPrice, $serviceFee, 2); //扣除红包的减免 $hbAmount = isset($data['hbAmount']) && $data['hbAmount'] > 0 ? $data['hbAmount'] : 0; $totalPrice = bcsub($totalPrice, $hbAmount, 2); $modifyPrice = isset($data['modifyPrice']) && $data['modifyPrice'] > 0 ? $data['modifyPrice'] : 0; //优惠 if ($modifyPrice < $totalPrice) { $data['discountType'] = dict::getDict('discountType', 'discount'); $discountAmount = bcsub($totalPrice, $modifyPrice, 2); $data['discountAmount'] = $discountAmount; } //保存组合 $groupName = $data['groupName'] ?? ''; if (empty($groupId) && !empty($groupName)) { util::fail('组合功能不可用'); } $data['weight'] = $totalWeight; $data['goodsPrice'] = $goodsPrice; $data['prePrice'] = $totalPrice; $data['orderPrice'] = $modifyPrice; if (isset($data['cash']) && is_numeric($data['cash']) && $data['cash'] > 0) { $cashAmount = $data['cash'] ?? 0; //现金付款如果大于订单金额,则切换成现金付款模式 if ($cashAmount >= $modifyPrice) { $payWay = dict::getDict('payWay', 'cash'); $hasPay = dict::getDict('hasPay', 'payed'); $data['cash'] = 0; $data['mainPay'] = $modifyPrice; } else { //微信+现金 支付宝+现金 组合付款 $data['cash'] = $cashAmount; $data['mainPay'] = bcsub($modifyPrice, $cashAmount, 2); $hasPay = dict::getDict('hasPay', 'unPay'); } } else { $data['cash'] = 0; $data['mainPay'] = $modifyPrice; } if ($hasPay == dict::getDict('hasPay', 'debt')) { $data['debtPrice'] = $modifyPrice; $data['remainDebtPrice'] = $modifyPrice; $data['debt'] = 1; $payWay = dict::getDict('payWay', 'debtPay'); } //hasPay balance 转化成余额支付方式 if ($hasPay == dict::getDict('hasPay', 'balance')) { $payWay = dict::getDict('payWay', 'balancePay'); } $data['cover'] = $orderCover; $data['actPrice'] = $modifyPrice; $data['realPrice'] = $modifyPrice; $data['customId'] = $customId; $data['hdId'] = $hdId; $data['sjId'] = $sjId; $data['userId'] = $userId; $data['shopId'] = $shopId; $data['mainId'] = $mainId; $data['manyType'] = $manyType; $data['payWay'] = $payWay; $data['orderSn'] = $orderSn; $data['status'] = OrderClass::ORDER_STATUS_UN_PAY; if (in_array($hasPay, [dict::getDict('hasPay', 'payed'), dict::getDict('hasPay', 'debt')])) { $data['onlinePay'] = dict::getDict('onlinePay', 'not'); } //保存订单 $returnOrder = OrderClass::addOrder($data); //欠款、已付款、余额支付的直接完成,另外客服下的单要走配送流程,所以是待配送状态 if (in_array($hasPay, [dict::getDict('hasPay', 'payed'), dict::getDict('hasPay', 'debt'), dict::getDict('hasPay', 'balance')])) { $params = []; if (!empty($data['historyDate'])) { //后台开单传账单日期 $params['historyDate'] = $data['historyDate']; } OrderClass::payAfter($returnOrder, $payWay, $params); } return $returnOrder; } //添加订单 public static function addOrder($data) { return OrderClass::addOrder($data); } //获取订单信息 ssh 2019.11.28 public static function getOrderList($where) { $order = 'addTime DESC'; if (isset($where['status']) && $where['status'] == 2) { $order = 'reachDate ASC,reachPeriod ASC,addTime ASC'; } $data = self::getList('*', $where, $order); if (empty($data['list'])) { return $data; } $orderSns = array_column($data['list'], 'orderSn'); //获取订单的商品信息 $goodsList = OrderGoodsService::getGoodsListByOrderSns($orderSns); //获取订花人的用户信息 $orderUserIds = array_column($data['list'], 'customId'); $orderUserIds = array_unique(array_filter($orderUserIds)); $customList = CustomClass::getCustomByIds($orderUserIds); $sjIdList = array_column($data['list'], 'sjId'); $sjIdList = array_filter(array_unique($sjIdList)); $merchantList = MerchantService::getByIds($sjIdList, null, 'id'); $defaultImg = imgUtil::groupImg(''); foreach ($data['list'] as $key => $val) { $id = $val['id']; $customId = $val['customId']; $currentActPrice = $val['actPrice'] ?? 0; $currentSendCost = $val['sendCost'] ?? 0; $currentPrice = bcsub($currentActPrice, $currentSendCost, 2); //没有商品显示的样式 $goodsInfoList = [['title' => '', 'coverUrl' => $defaultImg, 'unitPrice' => $currentPrice, 'num' => 1]]; if (isset($goodsList[$id])) { $goodsInfoList = $goodsList[$id]; } $data['list'][$key]['goodsInfoList'] = $goodsInfoList; $data['list'][$key]['bookAvatar'] = $customList[$customId]['smallAvatarUrl'] ?? ''; $sjId = $val['sjId']; $data['list'][$key]['name'] = $merchantList[$sjId]['name'] ?? ''; $data['list'][$key]['reachTime'] = OrderClass::getReachTime($val); $shortCover = $val['cover'] ?? ''; $data['list'][$key]['cover'] = imgUtil::groupImg($shortCover); //同个人同地址当天订单数 $sameTimeIds = $val['sameTimeIds'] ?? ''; $sameNum = 0; if (!empty($sameTimeIds)) { $sameIds = explode(',', $sameTimeIds); $sameNum = count($sameIds); } $data['list'][$key]['sameTimeIdsNum'] = $sameNum; } return $data; } //取客户最近若干条订单 ssh 2019.11.30 public static function getUserOrder($customId, $limit = 10, $order = null, $with = null) { $where = ['adminId' => $customId, '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 (($now - 60) > $order['deadline']) { util::fail('订单已过期'); } if (ceil($order['actPrice']) <= 0) { util::fail('订单金额有问题'); } if ($order['payStatus'] == 1) { util::fail('您已经付过了'); } } //商家验证是否有效 ssh 2019.12.15 public static function valid($order, $mainId) { if (empty($order)) { util::fail('订单无效'); } if ($order['mainId'] != $mainId) { 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, ]; SjCapitalService::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; $customId = isset($params['userId']) ? $params['userId'] : 0; $price = isset($params['price']) ? $params['price'] : 0; $sourceType = isset($params['sourceType']) ? $params['sourceType'] : 0; $discountAmount = 0; $discountType = 0; $discountPrice = $price; //使用优惠券 if (!empty($couponId)) { $discountAmount = CouponService::checkBeforeUse($couponId, $price, $customId); $discountPrice = stringUtil::calcSub($price, $discountAmount); $discountType = 1; } else { //使用会员 $asset = UserAssetClass::getByUserId($customId); 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) { $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]; } }