| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063 |
- <?php
- namespace bizHd\custom\classes;
- use biz\wx\classes\WxMessageClass;
- use bizHd\balance\classes\BalanceChangeClass;
- use bizHd\balance\classes\BalanceGiveChangeClass;
- use bizHd\balance\classes\BalancePayChangeClass;
- use bizHd\birthday\classes\BirthdayGiftClass;
- use bizHd\member\classes\MemberChangeClass;
- use bizHd\member\classes\MemberLevelClass;
- use bizHd\order\classes\OrderClass;
- use bizHd\order\classes\SettleClass;
- use bizHd\shop\classes\ShopClass;
- use bizHd\user\classes\UserClass;
- use bizHd\user\classes\UserGrowthClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\stringUtil;
- use common\components\util;
- use Overtrue\ChineseCalendar\Calendar;
- use bizHd\base\classes\BaseClass;
- class CustomClass extends BaseClass
- {
- public static $baseFile = '\bizHd\custom\models\Custom';
- //修改客户生日 ssh 20250814
- public static function modifyBirthday($customId, $shopId, $params)
- {
- date_default_timezone_set('PRC');
- $custom = self::getById($customId, true);
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- if ($custom->shopId != $shopId) {
- util::fail('不是你的客户');
- }
- $lunar = $params['lunar'] ?? 0;
- $birthdayMonth = $params['birthdayMonth'] ?? 1;
- if ($birthdayMonth > 12 || $birthdayMonth < 1) {
- util::fail('月份必须1到12月');
- }
- $birthdayDate = $params['birthdayDate'] ?? 1;
- if ($birthdayDate > 31 || $birthdayDate < 1) {
- util::fail('日期必须1到31号');
- }
- $userId = $custom->userId;
- $user = UserClass::getById($userId, true);
- if (empty($user)) {
- util::fail('客户信息缺失');
- }
- if ($lunar == 1 && $birthdayDate == 31) {
- util::fail('农历没有31号');
- }
- $calendar = new Calendar();
- if ($lunar == 1) {
- try {
- $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
- } catch (\InvalidArgumentException $e) {
- $msg = $e->getMessage();
- util::fail($msg);
- }
- //农历转阳历,有多处使用,需要同步修改,关键词 change_my_birthday
- $month = $result['gregorian_month'] ?? 1;
- $date = $result['gregorian_day'] ?? 1;
- $birthday = date("Y") . '-' . $month . '-' . $date;
- } else {
- $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
- }
- $birthdayTime = strtotime($birthday);
- if (getenv('YII_ENV') != 'dev' && $user->birthdaySet == 1) {
- util::fail('客户已填写,不能修改');
- }
- $custom->lunar = $lunar;
- $custom->birthdayDate = $birthdayDate;
- $custom->birthdayMonth = $birthdayMonth;
- $custom->birthday = $birthday;
- $custom->birthdayTime = $birthdayTime;
- $custom->save();
- $user->lunar = $lunar;
- $user->birthdayDate = $birthdayDate;
- $user->birthdayMonth = $birthdayMonth;
- $user->birthday = $birthday;
- $user->birthdayTime = $birthdayTime;
- $user->save();
- // 同步变更生日赠礼记录表(xhBirthdayGift)中的数据(只更新当年年份的,往年的不去修改)
- $birthdayGift = BirthdayGiftClass::getByCondition(['userId'=>$user->id, 'shopId'=>$shopId, 'year'=>date("Y")], true);
- if (!empty($birthdayGift)) {
- list($giftBirthdayMonth, $giftBirthdayDate) = BirthdayGiftClass::convertBirthdayMonthDay(
- $lunar,
- $birthdayMonth,
- $birthdayDate
- );
- $birthdayGift->lunar = $lunar;
- $birthdayGift->birthdayMonth = $giftBirthdayMonth;
- $birthdayGift->birthdayDate = $giftBirthdayDate;
- $birthdayGift->birthdayDisplay = BirthdayGiftClass::formatBirthdayDisplay($custom, $giftBirthdayMonth, $giftBirthdayDate);
- $birthdayGift->status = 1;
- $birthdayGift->save();
- }
- return $custom;
- }
- //用余额支付,客户余额减少 ssh 20250410
- public static function payToChangeBalance($order)
- {
- $actPrice = $order->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' => '',
- ];
- BalanceChangeClass::add($change, true);
- $order->balance = $actPrice;
- $order->save();
- // 多处需要同步修改 balance_pay_give
- if ($hd->balancePay >= $actPrice) {
- $payAmount = $actPrice;
- $mustUseGiveAmount = 0;
- } else {
- $payAmount = $hd->balancePay;
- $mustUseGiveAmount = bcsub($actPrice, $payAmount, 2);
- if ($mustUseGiveAmount > $hd->balanceGive) {
- util::fail('赠送余额异常,不够扣');
- }
- }
- /************************* 使用充值余额 *****************************/
- $customBalancePay = bcsub($custom->balancePay, $payAmount, 2);
- $custom->balancePay = $customBalancePay;
- $custom->save();
- $hdBalancePay = bcsub($hd->balancePay, $payAmount, 2);
- $hd->balancePay = $hdBalancePay;
- $hd->save();
- $payChange = [
- 'hdId' => $hdId,
- 'hdName' => $hdName,
- 'customId' => $customId,
- 'customName' => $customName,
- 'relateId' => $relateId,
- 'onlinePay' => 1,
- 'capitalType' => $capitalType,
- 'amount' => $payAmount,
- 'balance' => $customBalancePay,
- 'io' => 0,
- 'side' => $side,
- 'payWay' => $payWay,
- 'event' => $event,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'shopId' => $shopId,
- 'mainId' => $mainId,
- 'remark' => '',
- ];
- BalancePayChangeClass::add($payChange, true);
- $order->payBalance = $payAmount;
- $order->save();
- /***************************** 使用赠送余额 ******************************/
- //应用掉的赠送金额
- if ($mustUseGiveAmount > 0) {
- $customBalanceGive = bcsub($custom->balanceGive, $mustUseGiveAmount, 2);
- if ($customBalanceGive < 0) {
- util::fail('余额异常哈!请联系管理员,编号' . $hdId . ' ' . $custom->balanceGive);
- }
- $custom->balanceGive = $customBalanceGive;
- $custom->save();
- $hdBalanceGive = bcsub($hd->balanceGive, $mustUseGiveAmount, 2);
- $hd->balanceGive = $hdBalanceGive;
- $hd->save();
- if (floatval($customBalanceGive) != floatval($hdBalanceGive)) {
- util::fail('余额有问题呢!请联系管理员,编号' . $hdId);
- }
- $giveChange = [
- 'hdId' => $hdId,
- 'hdName' => $hdName,
- 'customId' => $customId,
- 'customName' => $customName,
- 'relateId' => $relateId,
- 'onlinePay' => 1,
- 'capitalType' => $capitalType,
- 'amount' => $mustUseGiveAmount,
- 'balance' => $customBalanceGive,
- 'io' => 0,
- 'side' => $side,
- 'payWay' => $payWay,
- 'event' => $event,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'shopId' => $shopId,
- 'mainId' => $mainId,
- 'remark' => '',
- ];
- BalanceGiveChangeClass::add($giveChange, true);
- $order->giveBalance = $mustUseGiveAmount;
- $order->save();
- }
- $addBalance = bcadd($hd->balancePay, $hd->balanceGive, 2);
- if (floatval($addBalance) != floatval($custom->balance)) {
- util::fail('账户余额有问题!请检查,编号' . $customId);
- }
- }
- //会员等级变化,包括手动修改、自动降会员和充值升会员等 ssh 20250324
- public static function levelChange($oldLevel, $newLevel, $shop, $custom, $hd, $params)
- {
- $adminId = $params['adminId'];
- $staffId = $params['staffId'] ?? 0;
- $staffName = $params['staffName'] ?? '';
- $style = $params['style'] ?? 0;
- $target = $params['target'] ?? [];
- $rechargeAmount = $params['rechargeAmount'] ?? 0;
- $targetId = $target->id ?? 0;
- $mainId = $shop->mainId;
- $shopId = $shop->id;
- $levelData = MemberLevelClass::getAllByCondition(['mainId' => $mainId], null, '*', 'level');
- $current = $levelData[$newLevel] ?? [];
- $before = $levelData[$oldLevel] ?? [];
- $beforeName = $before['name'] ?? '普通客户';
- $discount = $current['discount'] ?? 10;
- $memberName = $current['name'] ?? '';
- $oldGrowth = $custom->growth;
- $hd->member = $newLevel;
- $hd->discount = $discount;
- $hd->memberName = $memberName;
- $hd->save();
- $event = '手动修改';
- if ($style == 0) {
- util::fail('充值不支持升级会员');
- }
- $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);
- $custom->member = $newLevel;
- $custom->discount = $discount;
- $custom->memberName = $memberName;
- //成长值变更
- $growth = $current['amount'] ?? 0;
- $custom->growth = $growth;
- $custom->save();
- //成长值变动记录
- $growthData = [
- 'shopId' => $shopId,
- 'userId' => $custom->userId,
- 'userName' => $custom->name,
- 'event' => "管理员变更{$memberName}级,成长值从{$oldGrowth}变更为{$growth}",
- 'relateId' => 0,
- 'relateType' => 0,
- 'num' => $custom->growth - $oldGrowth,
- 'growth' => $growth,
- //'operatorId' => $adminId,
- 'staffId' => $staffId, //操作人(员工id)
- 'staffName' => $staffName, //员工名称
- 'createTime' => date('Y-m-d H:i:s')
- ];
- UserGrowthClass::add($growthData);
- }
- //客户采购欠款金额增加,如果有余额时,要进行欠款销账 ssh 20250715
- //这边开欠款单时,如果还有余额,会产生结账动作,结账动作也可以用赠送余额来结账,所以要分出赠送余额的占比
- //结账消余额,不是下单时消余额,不涉及售后时余额回滚问题,所以不需要设置 xhOrder 的 giveBalance payBalance balance等值
- public static function skCgDebtAmountAdd($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('花店信息缺失,编号6325');
- }
- $hdName = $hd->name ?? '';
- //结账时用了多少赠送的余额
- $settleUseGiveAmount = 0;
- if ($custom->balance > 0) {
- if ($amount > $custom->balance) {
- $clearAmount = $custom->balance;
- $settleUseGiveAmount = $custom->balanceGive;
- } else {
- util::fail('余额充足,请用余额支付');
- }
- }
- 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;
- $newSettle = $ret['settle'];
- //结账时用了多少赠送的余额
- $newSettle->giveBalance = bcadd($newSettle->giveBalance, $settleUseGiveAmount, 2);
- $newSettle->save();
- }
- /************************ 总余额 *********************************/
- $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);
- $mustUseGiveAmount = 0;
- // 多处需要同步修改 balance_pay_give
- if ($hd->balancePay >= $amount) {
- util::fail('请用余额支付哈');
- } else {
- // 如果【充值金额】小于订单欠款金额,则用全部【充值金额】消单,同时必须用上【赠送金额】,够抵,则【充值金额】还是正数,不够抵,则是负数
- if ($hd->balanceGive <= 0) {
- //没有【赠送金额】,则用【充值金额】来消全部的欠款金额,不够则变成负数
- $payAmount = $amount;
- } else {
- //这个是【总余额】,包括【充值金额】和【赠送金额】
- $currentAdd = bcadd($hd->balancePay, $hd->balanceGive, 2);
- if ($currentAdd >= $amount) {
- util::fail('请使用余额支付哦');
- } else {
- //【总余额】不够抵欠款金额,先用掉全部的【赠送金额】,其他的全部算在【充值金额】名下,因为充值金额也不够扣,会出现负数
- $mustUseGiveAmount = $hd->balanceGive;
- $payAmount = bcsub($amount, $mustUseGiveAmount, 2);
- }
- }
- }
- /************************* 充值余额 *****************************/
- $customBalancePay = bcsub($custom->balancePay, $payAmount, 2);
- $custom->balancePay = $customBalancePay;
- $custom->save();
- $hdBalancePay = bcsub($hd->balancePay, $payAmount, 2);
- $hd->balancePay = $hdBalancePay;
- $hd->save();
- if (floatval($customBalancePay) != floatval($hdBalancePay)) {
- 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;
- $payChange = [
- 'hdId' => $hdId,
- 'hdName' => $hdName,
- 'customId' => $customId,
- 'customName' => $customName,
- 'relateId' => $relateId,
- 'onlinePay' => $onlinePay,
- 'capitalType' => $capitalType,
- 'amount' => $payAmount,
- 'balance' => $customBalancePay,
- 'io' => 0,
- 'side' => $side,
- 'payWay' => $payWay,
- 'event' => $event,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'shopId' => $shopId,
- 'mainId' => $order->mainId,
- 'settleId' => $settleId,
- 'settleNo' => $settleNo,
- 'settleAmount' => $settleAmount,
- 'remark' => '',
- ];
- BalancePayChangeClass::add($payChange, true);
- /***************************** 赠送余额 ******************************/
- //应用掉的赠送金额
- if ($mustUseGiveAmount > 0) {
- $customBalanceGive = bcsub($custom->balanceGive, $mustUseGiveAmount, 2);
- if ($customBalanceGive < 0) {
- util::fail('余额异常哈,请联系管理员,编号' . $hdId . ' ' . $custom->balanceGive);
- }
- $custom->balanceGive = $customBalanceGive;
- $custom->save();
- $hdBalanceGive = bcsub($hd->balanceGive, $mustUseGiveAmount, 2);
- $hd->balanceGive = $hdBalanceGive;
- $hd->save();
- if (floatval($customBalanceGive) != floatval($hdBalanceGive)) {
- 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;
- $giveChange = [
- 'hdId' => $hdId,
- 'hdName' => $hdName,
- 'customId' => $customId,
- 'customName' => $customName,
- 'relateId' => $relateId,
- 'onlinePay' => $onlinePay,
- 'capitalType' => $capitalType,
- 'amount' => $mustUseGiveAmount,
- 'balance' => $customBalanceGive,
- 'io' => 0,
- 'side' => $side,
- 'payWay' => $payWay,
- 'event' => $event,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'shopId' => $shopId,
- 'mainId' => $order->mainId,
- 'settleId' => $settleId,
- 'settleNo' => $settleNo,
- 'settleAmount' => $settleAmount,
- 'remark' => '',
- ];
- BalanceGiveChangeClass::add($giveChange, true);
- }
- $addBalance = bcadd($hd->balancePay, $hd->balanceGive, 2);
- if (floatval($addBalance) != floatval($custom->balance)) {
- util::fail('账户余额有问题,请检查,编号' . $customId);
- }
- }
- //欠款和余额支付的售后 ssh 20250803
- public static function skRefundDebtAmountReduce($custom, $amount, $refund, $payWay)
- {
- $hdId = $custom->hdId ?? 0;
- $hd = HdClass::getLockById($hdId);
- if (empty($hd)) {
- util::fail('花店信息缺失,编号5362');
- }
- $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;
- $order = OrderClass::getLockById($orderId);
- if (empty($order)) {
- util::fail('没有找到订单哈!');
- }
- // 多处需要同步修改 balance_pay_give
- $mustUseGiveAmount = 0;
- $buyAmount = 0;
- if ($payWay == 3) {
- //如果是欠款单售后,没有退款到赠送余额的情况
- $buyAmount = $amount;
- } elseif ($payWay == 2) {
- //这个是总消费余额中充值余额部分
- $payBalance = $order->payBalance ?? 0;
- //这个是累计已经退还的余额
- $balanceTk = $order->balanceTk ?? 0;
- $currentBalanceTk = bcadd($balanceTk, $amount, 2);
- //当前累计退还的余额如果超过了充值余额的话,说明要退还赠送的余额了
- if ($currentBalanceTk > $payBalance) {
- $pastAmount = bcsub($currentBalanceTk, $payBalance, 2);
- if ($pastAmount > $amount) {
- $mustUseGiveAmount = $amount;
- } else {
- $buyAmount = bcsub($amount, $pastAmount, 2);
- $mustUseGiveAmount = bcsub($amount, $buyAmount, 2);
- }
- } else {
- //累计退还的余额,还没超过充值余额,还是退充值余额部分
- $buyAmount = $amount;
- }
- } else {
- util::fail('无效的支付方式!编号69880');
- }
- $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);
- $order->balanceTk = bcadd($order->balanceTk, $amount, 2);
- if ($order->balanceTk < 0) {
- util::fail('不能小于0,编号88672');
- }
- $order->save();
- if ($buyAmount > 0) {
- $customBalancePay = bcadd($custom->balancePay, $buyAmount, 2);
- $custom->balancePay = $customBalancePay;
- $custom->save();
- $hdBalancePay = bcadd($hd->balancePay, $buyAmount, 2);
- $hd->balancePay = $hdBalancePay;
- $hd->save();
- $payChange = [
- 'customId' => $customId,
- 'customName' => $customName,
- 'relateId' => $relateId,
- 'relateSn' => $refundSn,
- 'onlinePay' => $onlinePay,
- 'capitalType' => $capitalType,
- 'amount' => $buyAmount,
- 'balance' => $hdBalancePay,
- 'io' => 1,
- 'side' => $side,
- 'payWay' => $payWay,
- 'event' => $event,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'shopId' => $refund->shopId,
- 'mainId' => $refund->mainId,
- 'refundOrderId' => $orderId,
- 'refundOrderSn' => $orderSn,
- 'remark' => '',
- ];
- BalancePayChangeClass::add($payChange, true);
- $order->payBalanceTk = bcadd($order->payBalanceTk, $buyAmount, 2);
- if ($order->payBalanceTk < 0) {
- util::fail('不能小于0,编号88673');
- }
- $order->save();
- }
- if ($mustUseGiveAmount > 0) {
- $customBalanceGive = bcadd($custom->balanceGive, $mustUseGiveAmount, 2);
- $custom->balanceGive = $customBalanceGive;
- $custom->save();
- $hdBalanceGive = bcadd($hd->balanceGive, $mustUseGiveAmount, 2);
- $hd->balanceGive = $hdBalanceGive;
- $hd->save();
- $giveChange = [
- 'customId' => $customId,
- 'customName' => $customName,
- 'relateId' => $relateId,
- 'relateSn' => $refundSn,
- 'onlinePay' => $onlinePay,
- 'capitalType' => $capitalType,
- 'amount' => $mustUseGiveAmount,
- 'balance' => $hdBalanceGive,
- 'io' => 1,
- 'side' => $side,
- 'payWay' => $payWay,
- 'event' => $event,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'shopId' => $refund->shopId,
- 'mainId' => $refund->mainId,
- 'refundOrderId' => $orderId,
- 'refundOrderSn' => $orderSn,
- 'remark' => '',
- ];
- BalanceGiveChangeClass::add($giveChange, true);
- $order->giveBalanceTk = bcadd($order->giveBalanceTk, $mustUseGiveAmount, 2);
- if ($order->giveBalanceTk < 0) {
- util::fail('不能小于0,编号88675');
- }
- $order->save();
- }
- }
- //添加客户 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,
- ];
- $birthdaySet = $user->birthdaySet ?? 0;
- if ($birthdaySet == 1) {
- //如果用户自己设置过了生日,则生日给传过去
- $lunar = $user->lunar ?? 0;
- $birthday = $user->birthday ?? '';
- $birthdayTime = $user->birthdayTime ?? 0;
- $birthdayMonth = $user->birthdayMonth ?? 0;
- $birthdayDate = $user->birthdayDate ?? 0;
- //如果当年的生日时间没有更新,则需要给更新
- $year = date("Y", strtotime($birthday));
- $thisYear = date("Y");
- if ($year != $thisYear) {
- if ($lunar == 1) {
- try {
- //农历转阳历,有多处使用,需要同步修改,关键词change_my_birthday
- $calendar = new Calendar();
- $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
- $month = $result['gregorian_month'] ?? 1;
- $date = $result['gregorian_day'] ?? 1;
- $birthday = date("Y") . '-' . $month . '-' . $date;
- $user->birthday = $birthday;
- $user->save();
- CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
- } catch (\InvalidArgumentException $e) {
- $msg = $e->getMessage();
- noticeUtil::push("同步生日出错了,需要检查确认,userId:" . $userId . ' 月份:' . $birthdayMonth . ' 日期:' . $birthdayDate . ' ' . $msg, '15280215347');
- $birthday = '0000-00-00 00:00:00';
- }
- } else {
- $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
- $user->birthday = $birthday;
- $user->save();
- CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
- }
- }
- $data['birthday'] = $birthday;
- $data['birthdayTime'] = $birthdayTime;
- $data['birthdayMonth'] = $birthdayMonth;
- $data['birthdayDate'] = $birthdayDate;
- $data['lunar'] = $lunar;
- }
- // 处理并发插入:尝试插入,如果遇到唯一键冲突则重新查询
- try {
- $custom = self::addCustom($data);
- $isNewCustom = 1;
- } catch (\yii\db\IntegrityException $e) {
- // 唯一键冲突,说明其他并发请求已经插入,重新查询
- if (strpos($e->getMessage(), 'Duplicate entry') !== false) {
- $custom = self::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
- if (empty($custom)) {
- // 如果仍然查不到,则抛出异常
- throw $e;
- }
- $isNewCustom = 0;
- } else {
- throw $e;
- }
- }
- }
- $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,
- ];
- // 处理并发插入:尝试插入,如果遇到唯一键冲突则重新查询
- try {
- $hd = HdClass::addData($hdData);
- } catch (\yii\db\IntegrityException $e) {
- // 唯一键冲突,说明其他并发请求已经插入,重新查询
- if (strpos($e->getMessage(), 'Duplicate entry') !== false) {
- $hd = HdClass::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
- if (empty($hd)) {
- // 如果仍然查不到,则抛出异常
- throw $e;
- }
- } else {
- throw $e;
- }
- }
- $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) {
- $shortAvatar = $val['avatar'] ?? '';
- $avatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_800,w_800";
- $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $bigAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
- $list[$key]['shortAvatar'] = $shortAvatar;
- $list[$key]['avatar'] = $avatar;
- $list[$key]['smallAvatar'] = $smallAvatar;
- $list[$key]['bigAvatar'] = $bigAvatar;
- //超时未下单
- $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;
- }
- // 客户消费等级批量变更
- public static function updateCustomExpenseLevel($data)
- {
- $shopId = $data['shopId'];
- $mainId = $data['mainId'];
- $customs = self::getAllByCondition(['shopId' => $shopId], null, 'id, name, hdId, userId, buyAmount, growth, member, memberName, discount', null, true);
- $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount, discount');
- if (!empty($customs) && !empty($levelDatas)) {
- foreach ($customs as $c) {
- // $buyAmount = 0;
- // // 获取所有零售订单,计算消费金额
- // $orderList = \bizHd\order\classes\OrderClass::getAllByCondition(['customId' => $c->id], null, 'id, actPrice');
- // if (!empty($orderList)) {
- // foreach ($orderList as $order) {
- // $buyAmount = bcadd($buyAmount, $order['actPrice'], 2);
- // }
- // }
- // $c->buyAmount = $buyAmount;
- // 根据 growth 设置等级
- $memberData = self::getCustomExpenseLevel($c->growth, $mainId, $levelDatas); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel
- if ($c->member != $memberData['level'] || $c->memberName != $memberData['name']) {
- $hdId = $c->hdId;
- $hd = HdClass::getById($hdId, true);
- $change = [
- 'customId' => $c->id,
- 'customName' => $c->name,
- 'hdId' => $hd->id,
- 'hdName' => $hd->name,
- 'mainId' => $mainId,
- 'shopId' => $shopId,
- 'targetId' => 0,
- 'rechargeAmount' => 0,
- 'days' => 0,
- 'daysBefore' => '0000-00-00 00:00:00',
- 'daysAfter' => '0000-00-00 00:00:00',
- 'beforeLevel' => $c->member,
- 'afterLevel' => $memberData['level'],
- 'beforeName' => $c->memberName,
- 'afterName' => $memberData['name'],
- 'style' => 1, //手动修改会员,脚本执行
- 'event' => '系统脚本修改',
- 'staffId' => 0,
- 'staffName' => '',
- ];
- MemberChangeClass::add($change);
- }
- $c->member = $memberData['level'];
- $c->memberName = $memberData['name'];
- $c->discount = bcdiv(floatval($memberData['discount']), 10, 2);
- $c->save();
- if (!empty($c->hdId)) {
- $hd = HdClass::getById($c->hdId, true);
- if (!empty($hd)) {
- $hd->member = $memberData['level'];
- $hd->memberName = $memberData['name'];
- $hd->discount = $c->discount;
- $hd->save();
- }
- }
- // 更新客户所对应花店的消费金额
- // HdClass::updateByCondition(['shopId'=>$shopId, 'userId'=>$c->userId], ['expendAmount'=>$buyAmount]);
- }
- }
- return true;
- }
- // 获取客户消费等级
- public static function getCustomExpenseLevel($growth, $mainId, $levelDatas=[])
- {
- if (empty($levelDatas)) {
- $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount, discount');
- }
- foreach ($levelDatas as $data) {
- if ($growth >= $data['amount']) {
- return $data;
- }
- }
- return ['level'=>0, 'name'=>'', 'discount'=>10];
- }
- }
|