| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?php
- namespace bizHd\custom\classes;
- use biz\wx\classes\WxMessageClass;
- use bizGhs\shop\classes\TotalDebtChangeClass;
- use bizHd\member\classes\MemberChangeClass;
- use bizHd\member\classes\MemberWealClass;
- use bizHd\shop\classes\MainClass;
- use bizMall\user\classes\UserClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\stringUtil;
- use common\components\util;
- use Yii;
- use bizHd\base\classes\BaseClass;
- class CustomClass extends BaseClass
- {
- public static $baseFile = '\bizHd\custom\models\Custom';
- //用余额支付,客户余额减少 ssh 20250410
- public static function payByChangeBalance($order)
- {
- $hdId = $order->hdId ?? 0;
- $hd = HdClass::getLockById($hdId);
- $customId = $order->customId ?? 0;
- $custom = self::getLockById($customId);
- }
- //会员等级变化,包括手动修改、自动降会员和充值升会员等 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 20220908
- public static function cgDebtAmountAdd($custom, $order)
- {
- $amount = $order->remainDebtPrice ?? 0;
- $custom->isDebt = 1;
- $lastDebt = bcadd($custom->debtAmount, $amount, 2);
- $custom->debtAmount = $lastDebt;
- $custom->save();
- $relateId = $order->id ?? 0;
- $customId = $custom->id ?? 0;
- $orderSn = $order->orderSn ?? '';
- $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
- $event = '购买花材,订单号:' . $orderSn;
- $change = [
- 'relateId' => $relateId,
- 'customId' => $customId,
- 'ptStyle' => dict::getDict('ptStyle', 'hd'),
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $lastDebt,
- 'io' => 1,
- 'payWay' => $order->payWay,
- 'event' => $event,
- 'sjId' => $order->sjId,
- 'shopId' => $order->id,
- 'mainId' => $order->mainId,
- ];
- CustomDebtRecordClass::addChange($change);
- $mainId = $order->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息38');
- }
- $debt = $main->debt ?? 0;
- $remain = bcadd($debt, $amount, 2);
- $main->debt = $remain;
- $main->save();
- $event = $custom->name . ' 购买花材,订单号:' . $orderSn;
- $change['balance'] = $remain;
- $change['event'] = $event;
- TotalDebtChangeClass::addChange($change);
- }
- //散客申请退款,零售门店欠款金额减少
- 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} 超出了", '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 20220908
- public static function clearDebtAmountReduce($custom, $amount, $clear)
- {
- $debtAmount = $custom->debtAmount ?? 0.00;
- $remain = bcsub($debtAmount, $amount, 2);
- if ($remain < 0) {
- noticeUtil::push("零售客户欠款总金额:{$custom->debtAmount} 本次结账金额:{$amount} 客户ID:{$custom->id} 超出了", '15280215347');
- util::fail('操作失败,结账后欠款金额会出现负数');
- }
- $custom->isDebt = $remain <= 0 ? 0 : 1;
- $custom->debtAmount = $remain;
- $custom->save();
- $relateId = $clear->id ?? 0;
- $customId = $custom->id ?? 0;
- $capitalType = dict::getDict('capitalType', 'hdXsClear', 'id');
- $orderSn = $clear->orderSn ?? '';
- $change = [
- 'relateId' => $relateId,
- 'customId' => $customId,
- 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
- 'capitalType' => $capitalType,
- 'amount' => $amount,
- 'balance' => $remain,
- 'io' => 0,
- 'payWay' => 0,
- 'event' => "结账,单号:{$orderSn}",
- 'sjId' => $clear->sjId,
- 'shopId' => $clear->id,
- 'mainId' => $clear->mainId,
- ];
- CustomDebtRecordClass::addChange($change);
- $mainId = $clear->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main信息40');
- }
- $debt = $main->debt ?? 0;
- $totalRemain = bcsub($debt, $amount, 2);
- $main->debt = $totalRemain;
- $main->save();
- $event = $custom->name . " 结账,单号:{$orderSn}";
- $change['balance'] = $totalRemain;
- $change['event'] = $event;
- TotalDebtChangeClass::addChange($change);
- }
- 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;
- }
- $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 (isset($data['mobile']) && !empty($data['mobile'])) {
- $userData['mobile'] = $data['mobile'];
- }
- if (!empty($userData)) {
- UserClass::updateById($userId, $userData);
- }
- $id = $custom['id'];
- self::updateById($id, $data);
- }
- }
|