| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968 |
- <?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\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\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 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'] ?? 1;
- $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', null, true);
- $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount');
- 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
- $newMember = intval($memberData['level']);
- $customMember = $c->member;
- if ($customMember != $newMember) {
- $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->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');
- }
- foreach ($levelDatas as $data) {
- if ($growth >= $data['amount']) {
- return $data;
- }
- }
- return ['level'=>0, 'name'=>''];
- }
- }
|