| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- <?php
- namespace bizHd\order\services;
- use bizGhs\item\classes\ItemClass;
- use bizHd\base\services\BaseService;
- use biz\sj\services\SjCapitalService;
- use biz\sj\services\MerchantService;
- use bizHd\custom\classes\CustomClass;
- use bizHd\goods\classes\GoodsClass;
- use bizHd\hb\classes\HbClass;
- use bizHd\order\classes\OrderClass;
- use bizHd\order\classes\OrderGoodsClass;
- use bizHd\order\classes\OrderItemClass;
- use bizHd\promote\services\CouponService;
- use bizHd\user\classes\UserAssetClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\orderSn;
- use common\components\stringUtil;
- use common\components\util;
- use Yii;
- class OrderService extends BaseService
- {
- public static $baseFile = '\bizHd\order\classes\OrderClass';
- public static function createHdOrder($data, $custom, $hasPay = 1)
- {
- $payWay = $data['payWay'] ?? 0;
- $orderSn = orderSn::getOrderSn();
- $customId = $custom->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;
- $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);
- $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);
- //如果 $data['hbId'] 不为空,则把红包消费掉
- if(isset($data['hbId']) && $data['hbId'] != 0){
- $hbId = $data['hbId'];
- HbClass::hbBack($hbId);
- }
- //欠款、已付款、余额支付的直接完成,另外客服下的单要走配送流程,所以是待配送状态
- 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];
- }
- }
|