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; $shopId = $order->shopId; $settleNo = ''; $settleId = 0; $settleAmount = 0; $hdId = $custom->hdId ?? 0; $hd = HdClass::getLockById($hdId); if (empty($hd)) { util::fail('花店信息缺失,编号6331'); } 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]; $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; } $lastBalance = bcsub($custom->balance, $amount, 2); $custom->balance = $lastBalance; if ($custom->balance < 0) { $custom->isDebt = 1; } $custom->save(); $remain = bcsub($hd->balance, $amount, 2); $hd->balance = $remain; if ($hd->balance < 0) { $hd->debt = 2; } $hd->save(); $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 = [ 'customId' => $customId, 'customName' => $customName, 'relateId' => $relateId, 'onlinePay' => $onlinePay, 'capitalType' => $capitalType, 'amount' => $amount, 'balance' => $lastBalance, 'io' => 0, 'side' => $side, 'payWay' => $payWay, 'event' => $event, 'staffId' => $staffId, 'staffName' => $staffName, 'shopId' => $order->id, 'mainId' => $order->mainId, 'settleId' => $settleId, 'settleNo' => $settleNo, 'settleAmount' => $settleAmount, 'remark' => '', ]; BalanceChangeClass::add($change, true); } //散客申请退款,零售门店欠款金额减少 public static function refundDebtAmountReduce($custom, $amount, $refund) { $debtAmount = $custom->debtAmount ?? 0.00; $remain = bcsub($debtAmount, $amount, 2); if ($remain < 0) { noticeUtil::push("零售客户欠款总金额:{$custom->debtAmount} 本次退款金额:{$amount} 客户ID:{$custom->id} 超出了,编号66379", '15280215347'); util::fail('操作失败,欠款金额会出现负数'); } $custom->isDebt = $remain <= 0 ? 0 : 1; $custom->debtAmount = $remain; $custom->save(); $relateId = $refund->id ?? 0; $customId = $custom->id ?? 0; $capitalType = dict::getDict('capitalType', 'hdRefund', 'id'); $refundSn = $refund->refundSn ?? ''; $change = [ 'relateId' => $relateId, 'customId' => $customId, 'ptStyle' => dict::getDict('ptStyle', 'hd'), 'capitalType' => $capitalType, 'amount' => $amount, 'balance' => $remain, 'io' => 0, 'payWay' => 0, 'event' => "客户申请退款,退款单号:{$refundSn}", 'sjId' => $refund->sjId ?? 0, 'shopId' => $refund->shopId ?? 0, 'mainId' => $refund->mainId ?? 0, ]; CustomDebtRecordClass::addChange($change); $mainId = $refund->mainId ?? 0; $main = MainClass::getLockById($mainId); if (empty($main)) { util::fail('没有main信息39'); } $debt = $main->debt ?? 0; $totalRemain = bcsub($debt, $amount, 2); $main->debt = $totalRemain; $main->save(); $event = $custom->name . "申请退款,退款单号:{$refundSn}"; $change['balance'] = $totalRemain; $change['event'] = $event; TotalDebtChangeClass::addChange($change); } //客户结账欠款金额减少 ssh 20250627 public static function clearDebtAmountReduce($custom, $amount, $clear) { $balance = $custom->balance ?? 0.00; $remainBalance = bcsub($balance, $amount, 2); if ($remainBalance < 0) { noticeUtil::push("零售客户欠款总金额:{$balance} 本次结账金额:{$amount} 客户ID:{$custom->id} 超出了,编号788965", '15280215347'); util::fail('操作失败,结账后欠款金额会出现负数'); } $custom->balance = $remainBalance; $custom->save(); $hdId = $custom->hdId ?? 0; $hd = HdClass::getLockById($hdId); if (empty($hd)) { util::fail('店铺信息异常'); } $hdName = $hd->name ?? ''; $hdBalance = $hd->balance ?? 0; $hdRemainBalance = bcsub($hdBalance, $amount, 2); $hd->balance = $hdRemainBalance; $hd->save(); if (floatval($hdRemainBalance) != floatval($remainBalance)) { util::fail('余额有问题'); } $relateId = $clear->id ?? 0; $customId = $custom->id ?? 0; $capitalType = dict::getDict('capitalType', 'hdXsClear', 'id'); $orderSn = $clear->orderSn ?? ''; $customName = $custom->name ?? ''; $event = "结账,单号:{$orderSn}"; $staffId = $clear->hdShopAdminId ?? 0; $staffName = $clear->hdShopAdminName ?? ''; $shopId = $clear->hdShopId ?? 0; $mainId = $clear->mainId ?? 0; $remark = ''; $payWay = $clear->payWay ?? 0; $change = [ 'customId' => $customId, 'customName' => $customName, 'hdId' => $hdId, 'hdName' => $hdName, 'relateId' => $relateId, 'onlinePay' => 1, 'capitalType' => $capitalType, 'amount' => $amount, 'balance' => $hdRemainBalance, 'io' => 0, 'side' => 0, 'payWay' => $payWay, 'event' => $event, 'staffId' => $staffId, 'staffName' => $staffName, 'shopId' => $shopId, 'mainId' => $mainId, 'remark' => $remark, ]; BalanceChangeClass::add($change, true); } public static function getCustomCacheKey($sjId, $shopId, $userId) { return 'hd_custom_key_' . $sjId . '_' . $shopId . '_' . $userId; } //添加客户 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']); } return $list; } //更新客户信息 ssh 2021.3.23 public static function updateCustomInfo($custom, $data) { $userId = $custom['userId']; $userData = []; if (!empty($data['mobile'])) { $userData['mobile'] = $data['mobile']; } if (!empty($userData)) { UserClass::updateById($userId, $userData); } $id = $custom['id']; self::updateById($id, $data); } }