actPrice ?? 0; $hdId = $order->hdId ?? 0; $hd = HdClass::getLockById($hdId); $balance = $hd->balance ?? 0; if ($actPrice > $balance) { util::fail('余额不足'); } $remain = bcsub($balance, $actPrice, 2); $hd->balance = $remain; $hd->save(); $hdName = $hd->name; $customId = $order->customId ?? 0; $custom = self::getLockById($customId); $balance = $custom->balance ?? 0; if ($actPrice > $balance) { util::fail('余额不足哦'); } $remain = bcsub($balance, $actPrice, 2); $custom->balance = $remain; $custom->save(); $customName = $custom->name; if ($custom->balance != $hd->balance) { util::fail('余额有问题哦'); } $relateId = $order->id ?? 0; //0 花店操作 1 客户操作 $payWay = $order->payWay; $side = 1; $shopId = $order->shopId; $mainId = $order->mainId; $staffId = $order->shopAdminId; $staffName = $order->shopAdminName; $orderSn = $order->orderSn; $event = '买花,单号:' . $orderSn; $capitalType = dict::getDict('capitalType', 'xhOrder', 'id'); $change = [ 'hdId' => $hdId, 'hdName' => $hdName, 'customId' => $customId, 'customName' => $customName, 'relateId' => $relateId, 'onlinePay' => 1, 'capitalType' => $capitalType, 'amount' => $actPrice, 'balance' => $hd->balance, 'io' => 0, 'side' => $side, 'payWay' => $payWay, 'event' => $event, 'staffId' => $staffId, 'staffName' => $staffName, 'shopId' => $shopId, 'mainId' => $mainId, 'remark' => '', ]; return BalanceChangeClass::add($change, true); } //会员等级变化,包括手动修改、自动降会员和充值升会员等 ssh 20250324 public static function levelChange($oldLevel, $newLevel, $shop, $custom, $hd, $params) { $staffId = $params['staffId'] ?? 0; $staffName = $params['staffName'] ?? ''; $style = $params['style'] ?? 0; $target = $params['target'] ?? []; $onlinePay = $params['onlinePay'] ?? 1; $rechargeAmount = $params['rechargeAmount'] ?? 0; $targetId = $target->id ?? 0; $mainId = $shop->mainId; $shopId = $shop->id; $weal = MemberWealClass::getAllByCondition(['mainId' => $mainId], null, '*', 'level'); $current = $weal[$newLevel] ?? []; $before = $weal[$oldLevel] ?? []; $beforeName = $before['name'] ?? '普通客户'; $discount = $current['discount'] ?? 1; $memberName = $current['name'] ?? ''; $custom->member = $newLevel; $custom->discount = $discount; $custom->memberName = $memberName; $custom->save(); $hd->member = $newLevel; $hd->discount = $discount; $hd->memberName = $memberName; $hd->save(); $event = '手动修改'; if ($style == 0) { if ($onlinePay == 2) { $event = '线上充值' . $rechargeAmount . '元'; } else { $event = '线下充值' . $rechargeAmount . '元'; } } $change = [ 'customId' => $custom->id, 'customName' => $custom->name, 'hdId' => $hd->id, 'hdName' => $hd->name, 'mainId' => $mainId, 'shopId' => $shopId, 'targetId' => $targetId, 'rechargeAmount' => $rechargeAmount, 'days' => 0, 'daysBefore' => '0000-00-00 00:00:00', 'daysAfter' => '0000-00-00 00:00:00', 'beforeLevel' => $oldLevel, 'afterLevel' => $newLevel, 'beforeName' => $beforeName, 'afterName' => $memberName, 'style' => $style, 'event' => $event, 'staffId' => $staffId, 'staffName' => $staffName, ]; MemberChangeClass::add($change); } //客户采购欠款金额增加,如果有余额时,要进行欠款销账 ssh 20250715 public static function cgDebtAmountAdd($custom, $order) { $clearAmount = 0; $amount = $order->remainDebtPrice ?? 0; $staffId = $order->shopAdminId ?? 0; $staffName = $order->shopAdminName ?? ''; $orderId = $order->id; $orderSn = $order->orderSn ?? ''; $shopId = $order->shopId; $settleNo = ''; $settleId = 0; $settleAmount = 0; $hdId = $custom->hdId ?? 0; $hd = HdClass::getLockById($hdId); if (empty($hd)) { util::fail('花店信息缺失,编号6331'); } $hdName = $hd->name ?? ''; if ($custom->balance > 0) { if ($amount >= $custom->balance) { $clearAmount = $custom->balance; } else { $clearAmount = $amount; } } if ($clearAmount > 0) { $shop = ShopClass::getById($shopId, true); $needClearIdMap[] = ['orderId' => $orderId, 'clearAmount' => $clearAmount, 'orderSn' => $orderSn]; $setParams = ['staffId' => $staffId, 'staffName' => $staffName]; $set = SettleClass::addSettle($needClearIdMap, $shop, $custom, $hd, $setParams); $ret = SettleClass::clearSettle($set); $settleAmount = $ret['totalClearAmount'] ?? 0; $settleNo = $set->orderSn ?? ''; $settleId = $set->id; } $customBalance = bcsub($custom->balance, $amount, 2); $custom->balance = $customBalance; if ($custom->balance < 0) { $custom->isDebt = 1; } $custom->save(); $hdBalance = bcsub($hd->balance, $amount, 2); $hd->balance = $hdBalance; if ($hd->balance < 0) { $hd->debt = 2; } $hd->save(); if (floatval($customBalance) != floatval($hdBalance)) { util::fail('余额有错,请联系管理员,编号' . $hdId); } $relateId = $order->id ?? 0; $customId = $custom->id ?? 0; $orderSn = $order->orderSn ?? ''; $capitalType = dict::getDict('capitalType', 'xhOrder', 'id'); $event = '购买花材,单号:' . $orderSn; $customName = $custom->name ?? ''; $onlinePay = 1; $side = 0; $payWay = 0; $change = [ 'hdId' => $hdId, 'hdName' => $hdName, 'customId' => $customId, 'customName' => $customName, 'relateId' => $relateId, 'onlinePay' => $onlinePay, 'capitalType' => $capitalType, 'amount' => $amount, 'balance' => $customBalance, 'io' => 0, 'side' => $side, 'payWay' => $payWay, 'event' => $event, 'staffId' => $staffId, 'staffName' => $staffName, 'shopId' => $shopId, 'mainId' => $order->mainId, 'settleId' => $settleId, 'settleNo' => $settleNo, 'settleAmount' => $settleAmount, 'remark' => '', ]; BalanceChangeClass::add($change, true); } //欠款和余额支付的售后 ssh 20250803 public static function refundDebtAmountReduce($custom, $amount, $refund) { $hdId = $custom->hdId ?? 0; $hd = HdClass::getLockById($hdId); if (empty($hd)) { util::fail('花店信息缺失,编号520'); } $customBalance = bcadd($custom->balance, $amount, 2); $custom->balance = $customBalance; if ($custom->balance < 0) { $custom->isDebt = 1; } $custom->save(); $hdBalance = bcadd($hd->balance, $amount, 2); $hd->balance = $hdBalance; if ($hd->balance < 0) { $hd->debt = 2; } $hd->save(); if (floatval($customBalance) !== floatval($hdBalance)) { util::fail('余额错误,请联系管理员,编号' . $hdId); } $relateId = $refund->id ?? 0; $customId = $custom->id ?? 0; $refundSn = $refund->refundSn ?? ''; $orderSn = $refund->orderSn ?? ''; $orderId = $refund->orderId ?? 0; $staffId = $refund->shopAdminId ?? 0; $staffName = $refund->shopAdminName ?? ''; $capitalType = dict::getDict('capitalType', 'hdRefund', 'id'); $event = "订单售后 " . $refundSn; $customName = $custom->name ?? ''; $onlinePay = 1; $side = 0; $payWay = 2;//余额支付 $change = [ 'customId' => $customId, 'customName' => $customName, 'relateId' => $relateId, 'relateSn' => $refundSn, 'onlinePay' => $onlinePay, 'capitalType' => $capitalType, 'amount' => $amount, 'balance' => $customBalance, 'io' => 1, 'side' => $side, 'payWay' => $payWay, 'event' => $event, 'staffId' => $staffId, 'staffName' => $staffName, 'shopId' => $refund->shopId, 'mainId' => $refund->mainId, 'refundOrderId' => $orderId, 'refundOrderSn' => $orderSn, 'remark' => '', ]; BalanceChangeClass::add($change, true); } //添加客户 ssh 2021.2.28 public static function addCustom($data) { return self::add($data, true); } //创建或获取客户信息 ssh 2021.3.19 public static function buildRelation($shop, $user) { if (empty($user)) { return false; } if (empty($shop)) { return false; } if ($shop->ptStyle == 2) { util::fail('有问题,批发店状态'); } $salt = stringUtil::charsShuffleLowerCase(8); $isNewCustom = 0; $userId = $user->id; $name = $user->name ?? ''; $mobile = $user->mobile ?? ''; $shopId = $shop->id ?? 0; $hdAvatar = $shop->avatar ?? ''; $sjId = $shop->sjId ?? 0; $sjName = $shop->merchantName ?? ''; $shopName = $shop->shopName ?? ''; $hdMobile = $shop->mobile ?? ''; $hdName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName; $hdPy = stringUtil::py($hdName); $custom = self::getByCondition(['shopId' => $shopId, 'userId' => $userId], true); if (empty($custom)) { $py = stringUtil::py($name); $visitTime = date("Y-m-d H:i:s"); $data = [ 'userId' => $userId, 'sjId' => $sjId, 'shopId' => $shopId, 'name' => $name, 'originName' => $name, 'py' => $py, 'mobile' => $mobile, 'visitTime' => $visitTime, 'salt' => $salt, ]; $custom = self::addCustom($data); $isNewCustom = 1; } $customId = $custom->id ?? 0; $hd = HdClass::getByCondition(['shopId' => $shopId, 'userId' => $userId], true); if (empty($hd)) { $hdData = [ 'shopId' => $shopId, 'userId' => $userId, 'name' => $hdName, 'sjId' => $sjId, 'customId' => $customId, 'avatar' => $hdAvatar, 'py' => $hdPy, 'mobile' => $hdMobile, 'province' => $shop->province ?? '', 'city' => $shop->city ?? '', 'dist' => $shop->dist ?? '', 'address' => $shop->address ?? '', 'floor' => $shop->floor ?? '', 'fullAddress' => $shop->fullAddress ?? '', 'showAddress' => $shop->showAddress ?? '', 'lat' => $shop->lat ?? '', 'long' => $shop->long ?? '', 'salt' => $salt, ]; $hd = HdClass::addData($hdData); $isNewCustom = 1; } $hdId = $hd->id ?? 0; $custom->hdId = $hdId; $custom->save(); if ($isNewCustom == 1) { WxMessageClass::newHdCustomInform($shop, $custom); } return ['custom' => $custom, 'hd' => $hd]; } //获取客户信息 public static function getCustom($data) { $custom = self::getByCondition($data); if (empty($custom)) { $custom = self::add($data); } return $custom; } //获取客户信息 ssh 2021.2.1 public static function getCustomInfo($id) { $custom = self::getById($id); if (empty($custom)) { return $custom; } $list = self::groupBaseInfo([$custom]); return current($list); } //获取客户信息 ssh 2021.3.19 public static function getCustomByIds($ids) { $list = self::getByIds($ids); $list = self::groupBaseInfo($list); return $list; } public static function valid($info, $shopId) { if (isset($info['shopId']) && $info['shopId'] == $shopId) { return true; } util::fail('客户信息无效'); } //整合基本信息 ssh 2021.1.31 public static function groupBaseInfo($list) { if (empty($list)) { return $list; } foreach ($list as $key => $val) { $list[$key]['avatar'] = imgUtil::groupImg($val['avatar']); //超时未下单 $recentExpend = $val['recentExpend'] ?? ''; $list[$key]['overTimeUnExpend'] = 0; if ($recentExpend == '0000-00-00 00:00:00') { $list[$key]['overTimeUnExpend'] = 10000; } else { $recentExpendTime = strtotime($recentExpend); $seven = bcmul(86400, 7); $sevenTime = bcadd($recentExpendTime, $seven); if ($sevenTime < time()) { $list[$key]['overTimeUnExpend'] = 7; } $month = bcmul(86400, 30); $monthTime = bcadd($recentExpendTime, $month); if ($monthTime < time()) { $list[$key]['overTimeUnExpend'] = 30; } $halfYear = bcmul(86400, 180); $halfYearTime = bcadd($recentExpendTime, $halfYear); if ($halfYearTime < time()) { $list[$key]['overTimeUnExpend'] = 180; } $year = bcmul(86400, 365); $yearTime = bcmul($recentExpendTime, $year); if ($yearTime < time()) { $list[$key]['overTimeUnExpend'] = 365; } } } return $list; } }