| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- <?php
- namespace bizMall\order\services;
- use bizMall\base\services\BaseService;
- use bizMall\custom\classes\CustomClass;
- use bizMall\goods\classes\GoodsClass;
- use bizMall\item\classes\ItemClass;
- use bizMall\merchant\services\MerchantCapitalService;
- use bizMall\merchant\services\MerchantService;
- use bizMall\order\classes\OrderClass;
- use bizMall\order\classes\OrderGoodsClass;
- use bizMall\order\classes\OrderItemClass;
- use bizMall\promote\services\CouponService;
- use bizMall\shop\classes\ShopClass;
- use bizMall\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 = '\bizMall\order\classes\OrderClass';
- public static function createOrder($data, $custom)
- {
- $customId = $data['customId'] ?? 0;
- $hdId = $data['hdId'] ?? 0;
- $orderSn = orderSn::getOrderSn();
- $sjId = $data['sjId'] ?? 0;
- $shopId = $data['shopId'] ?? 0;
- $mainId = $data['mainId'] ?? 0;
- $shop = ShopClass::getById($shopId, true);
- if (empty($shop)) {
- util::fail('没有找到门店86');
- }
- $product = $data['product'] ?? [];
- if (empty($product)) {
- util::fail('没有花材');
- }
- $gIds = [];
- $itIds = [];
- foreach ($product as $val) {
- $currentId = $val['productId'] ?? 0;
- $property = $val['property'];
- if ($property == dict::getDict('property', 'goods')) {
- $gIds[] = $currentId;
- }
- if ($property == dict::getDict('property', 'item')) {
- $itIds[] = $currentId;
- }
- }
- $goodsInfo = [];
- $gIds = array_unique(array_filter($gIds));
- if (!empty($gIds)) {
- $goodsInfo = GoodsClass::getByIds($gIds, null, 'id');
- }
- $itemInfo = [];
- $itIds = array_unique(array_filter($itIds));
- if (!empty($itIds)) {
- $itemInfo = ItemClass::getByIds($itIds, null, 'id');
- }
- $weight = 0;
- $goodsPrice = 0;
- $priceMap = \bizGhs\custom\classes\CustomClass::$levelPriceKeyMap;
- $addPriceMap = \bizGhs\custom\classes\CustomClass::$levelAddPriceKeyMap;
- foreach ($product as $key => $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);
- }
- //获取订单信息 ssh 2019.11.28
- public static function getOrderList($where)
- {
- $data = self::getList('*', $where, 'addTime DESC');
- if (empty($data['list'])) {
- return $data;
- }
- $list = $data['list'];
- $orderSns = array_column($list, 'orderSn');
- $goodsList = OrderGoodsService::getGoodsListByOrderSns($orderSns);
- $periodData = [0 => '上午', 1 => '下午', 2 => '晚上'];
- foreach ($data['list'] as $key => $val) {
- $id = $val['id'];
- $goodsInfoList = isset($goodsList[$id]) ? $goodsList[$id] : [];
- $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];
- }
- }
|