CustomClass.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. <?php
  2. namespace bizHd\custom\classes;
  3. use biz\wx\classes\WxMessageClass;
  4. use bizHd\balance\classes\BalanceChangeClass;
  5. use bizHd\balance\classes\BalanceGiveChangeClass;
  6. use bizHd\balance\classes\BalancePayChangeClass;
  7. use bizHd\birthday\classes\BirthdayGiftClass;
  8. use bizHd\member\classes\MemberChangeClass;
  9. use bizHd\member\classes\MemberLevelClass;
  10. use bizHd\order\classes\OrderClass;
  11. use bizHd\order\classes\SettleClass;
  12. use bizHd\shop\classes\ShopClass;
  13. use bizHd\user\classes\UserClass;
  14. use bizHd\user\classes\UserGrowthClass;
  15. use common\components\dict;
  16. use common\components\imgUtil;
  17. use common\components\noticeUtil;
  18. use common\components\stringUtil;
  19. use common\components\util;
  20. use Overtrue\ChineseCalendar\Calendar;
  21. use bizHd\base\classes\BaseClass;
  22. class CustomClass extends BaseClass
  23. {
  24. public static $baseFile = '\bizHd\custom\models\Custom';
  25. public static function getBalanceStat($where)
  26. {
  27. $cacheKey = self::getBalanceStatCacheKey($where);
  28. $cached = \Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  29. if ($cached !== false && $cached !== null && $cached !== '') {
  30. $data = json_decode($cached, true);
  31. if (is_array($data)) {
  32. return $data;
  33. }
  34. }
  35. $countWhere = array_merge($where, ['balance>' => 0]);
  36. $count = self::getCount($countWhere);
  37. $totalBalance = self::sum($where, 'balance');
  38. $data = [
  39. 'count' => intval($count),
  40. 'totalBalance' => bcadd($totalBalance ?: '0', '0', 2),
  41. ];
  42. \Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 60, json_encode($data)]);
  43. return $data;
  44. }
  45. public static function getBalanceStatCacheKey($where)
  46. {
  47. return 'hd_custom_balance_stat:' . md5(json_encode($where, JSON_UNESCAPED_UNICODE));
  48. }
  49. /**
  50. * 修改客户生日
  51. * @param $customId
  52. * @param $shopId
  53. * @param $params
  54. * @return array [custom, bool] custom表示客户数据,bool表示是否变动了生日
  55. * @throws \Exception
  56. */
  57. public static function modifyBirthday($customId, $shopId, $params)
  58. {
  59. date_default_timezone_set('PRC');
  60. $custom = self::getById($customId, true);
  61. if (empty($custom)) {
  62. util::fail('没有找到客户');
  63. }
  64. if ($custom->shopId != $shopId) {
  65. util::fail('不是你的客户');
  66. }
  67. $lunar = $params['lunar'] ?? 0;
  68. $birthdayMonth = $params['birthdayMonth'] ?? 1;
  69. if ($birthdayMonth > 12 || $birthdayMonth < 1) {
  70. util::fail('月份必须1到12月');
  71. }
  72. $birthdayDate = $params['birthdayDate'] ?? 1;
  73. if ($birthdayDate > 31 || $birthdayDate < 1) {
  74. util::fail('日期必须1到31号');
  75. }
  76. $userId = $custom->userId;
  77. $user = UserClass::getById($userId, true);
  78. if (empty($user)) {
  79. util::fail('客户信息缺失');
  80. }
  81. if ($lunar == 1 && $birthdayDate == 31) {
  82. util::fail('农历没有31号');
  83. }
  84. $isUpdateBirthday = $user->birthdayTime == 0;
  85. // 如果 xhUser 中已经设置了生日,则直接取用
  86. if ($user->birthdayTime != 0) {
  87. $custom->lunar = $user->lunar;
  88. $custom->birthdayDate = $user->birthdayDate;
  89. $custom->birthdayMonth = $user->birthdayMonth;
  90. $custom->birthday = $user->birthday;
  91. $custom->birthdayTime = $user->birthdayTime;
  92. $custom->save();
  93. } else {
  94. $calendar = new Calendar();
  95. if ($lunar == 1) {
  96. try {
  97. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  98. } catch (\InvalidArgumentException $e) {
  99. $msg = $e->getMessage();
  100. util::fail($msg);
  101. }
  102. //农历转阳历,有多处使用,需要同步修改,关键词 change_my_birthday
  103. $month = $result['gregorian_month'] ?? 1;
  104. $date = $result['gregorian_day'] ?? 1;
  105. $birthday = date("Y") . '-' . $month . '-' . $date;
  106. } else {
  107. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  108. }
  109. $birthdayTime = strtotime($birthday);
  110. if (getenv('YII_ENV') != 'dev' && $user->birthdaySet == 1) {
  111. util::fail('客户已填写,不能修改');
  112. }
  113. $custom->lunar = $lunar;
  114. $custom->birthdayDate = $birthdayDate;
  115. $custom->birthdayMonth = $birthdayMonth;
  116. $custom->birthday = $birthday;
  117. $custom->birthdayTime = $birthdayTime;
  118. $custom->save();
  119. $user->lunar = $lunar;
  120. $user->birthdayDate = $birthdayDate;
  121. $user->birthdayMonth = $birthdayMonth;
  122. $user->birthday = $birthday;
  123. $user->birthdayTime = $birthdayTime;
  124. $user->save();
  125. }
  126. // 同步变更生日赠礼记录表(xhBirthdayGift)中的数据(只更新当年年份的,往年的不去修改)
  127. $birthdayGift = BirthdayGiftClass::getByCondition(['userId'=>$user->id, 'shopId'=>$shopId, 'year'=>date("Y")], true);
  128. if (!empty($birthdayGift)) {
  129. $lunar = intval($custom->lunar);
  130. $birthdayMonth = intval($custom->birthdayMonth);
  131. $birthdayDate = intval($custom->birthdayDate);
  132. list($giftBirthdayMonth, $giftBirthdayDate) = BirthdayGiftClass::convertBirthdayMonthDay(
  133. $lunar,
  134. $birthdayMonth,
  135. $birthdayDate
  136. );
  137. $birthdayGift->lunar = $lunar;
  138. $birthdayGift->birthdayMonth = $giftBirthdayMonth;
  139. $birthdayGift->birthdayDate = $giftBirthdayDate;
  140. $birthdayGift->birthdayDisplay = BirthdayGiftClass::formatBirthdayDisplay($custom, $giftBirthdayMonth, $giftBirthdayDate);
  141. $birthdayGift->status = 1;
  142. $birthdayGift->save();
  143. }
  144. return [$custom, $isUpdateBirthday];
  145. }
  146. //用余额支付,客户余额减少 ssh 20250410
  147. public static function payToChangeBalance($order)
  148. {
  149. $actPrice = $order->actPrice ?? 0;
  150. $hdId = $order->hdId ?? 0;
  151. $hd = HdClass::getLockById($hdId);
  152. $balance = $hd->balance ?? 0;
  153. if ($actPrice > $balance) {
  154. util::fail('余额不足');
  155. }
  156. $remain = bcsub($balance, $actPrice, 2);
  157. $hd->balance = $remain;
  158. $hd->save();
  159. $hdName = $hd->name;
  160. $customId = $order->customId ?? 0;
  161. $custom = self::getLockById($customId);
  162. $balance = $custom->balance ?? 0;
  163. if ($actPrice > $balance) {
  164. util::fail('余额不足哦');
  165. }
  166. $remain = bcsub($balance, $actPrice, 2);
  167. $custom->balance = $remain;
  168. $custom->save();
  169. $customName = $custom->name;
  170. if ($custom->balance != $hd->balance) {
  171. util::fail('余额有问题哦');
  172. }
  173. $relateId = $order->id ?? 0;
  174. //0 花店操作 1 客户操作
  175. $payWay = $order->payWay;
  176. $side = 1;
  177. $shopId = $order->shopId;
  178. $mainId = $order->mainId;
  179. $staffId = $order->shopAdminId;
  180. $staffName = $order->shopAdminName;
  181. $orderSn = $order->orderSn;
  182. $event = '买花,单号:' . $orderSn;
  183. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  184. $change = [
  185. 'hdId' => $hdId,
  186. 'hdName' => $hdName,
  187. 'customId' => $customId,
  188. 'customName' => $customName,
  189. 'relateId' => $relateId,
  190. 'onlinePay' => 1,
  191. 'capitalType' => $capitalType,
  192. 'amount' => $actPrice,
  193. 'balance' => $hd->balance,
  194. 'io' => 0,
  195. 'side' => $side,
  196. 'payWay' => $payWay,
  197. 'event' => $event,
  198. 'staffId' => $staffId,
  199. 'staffName' => $staffName,
  200. 'shopId' => $shopId,
  201. 'mainId' => $mainId,
  202. 'remark' => '',
  203. ];
  204. BalanceChangeClass::add($change, true);
  205. $order->balance = $actPrice;
  206. $order->save();
  207. // 多处需要同步修改 balance_pay_give
  208. if ($hd->balancePay >= $actPrice) {
  209. $payAmount = $actPrice;
  210. $mustUseGiveAmount = 0;
  211. } else {
  212. $payAmount = $hd->balancePay;
  213. $mustUseGiveAmount = bcsub($actPrice, $payAmount, 2);
  214. if ($mustUseGiveAmount > $hd->balanceGive) {
  215. util::fail('赠送余额异常,不够扣');
  216. }
  217. }
  218. /************************* 使用充值余额 *****************************/
  219. $customBalancePay = bcsub($custom->balancePay, $payAmount, 2);
  220. $custom->balancePay = $customBalancePay;
  221. $custom->save();
  222. $hdBalancePay = bcsub($hd->balancePay, $payAmount, 2);
  223. $hd->balancePay = $hdBalancePay;
  224. $hd->save();
  225. $payChange = [
  226. 'hdId' => $hdId,
  227. 'hdName' => $hdName,
  228. 'customId' => $customId,
  229. 'customName' => $customName,
  230. 'relateId' => $relateId,
  231. 'onlinePay' => 1,
  232. 'capitalType' => $capitalType,
  233. 'amount' => $payAmount,
  234. 'balance' => $customBalancePay,
  235. 'io' => 0,
  236. 'side' => $side,
  237. 'payWay' => $payWay,
  238. 'event' => $event,
  239. 'staffId' => $staffId,
  240. 'staffName' => $staffName,
  241. 'shopId' => $shopId,
  242. 'mainId' => $mainId,
  243. 'remark' => '',
  244. ];
  245. BalancePayChangeClass::add($payChange, true);
  246. $order->payBalance = $payAmount;
  247. $order->save();
  248. /***************************** 使用赠送余额 ******************************/
  249. //应用掉的赠送金额
  250. if ($mustUseGiveAmount > 0) {
  251. $customBalanceGive = bcsub($custom->balanceGive, $mustUseGiveAmount, 2);
  252. if ($customBalanceGive < 0) {
  253. util::fail('余额异常哈!请联系管理员,编号' . $hdId . ' ' . $custom->balanceGive);
  254. }
  255. $custom->balanceGive = $customBalanceGive;
  256. $custom->save();
  257. $hdBalanceGive = bcsub($hd->balanceGive, $mustUseGiveAmount, 2);
  258. $hd->balanceGive = $hdBalanceGive;
  259. $hd->save();
  260. if (floatval($customBalanceGive) != floatval($hdBalanceGive)) {
  261. util::fail('余额有问题呢!请联系管理员,编号' . $hdId);
  262. }
  263. $giveChange = [
  264. 'hdId' => $hdId,
  265. 'hdName' => $hdName,
  266. 'customId' => $customId,
  267. 'customName' => $customName,
  268. 'relateId' => $relateId,
  269. 'onlinePay' => 1,
  270. 'capitalType' => $capitalType,
  271. 'amount' => $mustUseGiveAmount,
  272. 'balance' => $customBalanceGive,
  273. 'io' => 0,
  274. 'side' => $side,
  275. 'payWay' => $payWay,
  276. 'event' => $event,
  277. 'staffId' => $staffId,
  278. 'staffName' => $staffName,
  279. 'shopId' => $shopId,
  280. 'mainId' => $mainId,
  281. 'remark' => '',
  282. ];
  283. BalanceGiveChangeClass::add($giveChange, true);
  284. $order->giveBalance = $mustUseGiveAmount;
  285. $order->save();
  286. }
  287. $addBalance = bcadd($hd->balancePay, $hd->balanceGive, 2);
  288. if (floatval($addBalance) != floatval($custom->balance)) {
  289. util::fail('账户余额有问题!请检查,编号' . $customId);
  290. }
  291. }
  292. //会员等级变化,包括手动修改、自动降会员和充值升会员等 ssh 20250324
  293. public static function levelChange($oldLevel, $newLevel, $shop, $custom, $hd, $params)
  294. {
  295. $adminId = $params['adminId'];
  296. $staffId = $params['staffId'] ?? 0;
  297. $staffName = $params['staffName'] ?? '';
  298. $style = $params['style'] ?? 0;
  299. $target = $params['target'] ?? [];
  300. $rechargeAmount = $params['rechargeAmount'] ?? 0;
  301. $targetId = $target->id ?? 0;
  302. $mainId = $shop->mainId;
  303. $shopId = $shop->id;
  304. $levelData = MemberLevelClass::getAllByCondition(['mainId' => $mainId], null, '*', 'level');
  305. $current = $levelData[$newLevel] ?? [];
  306. $before = $levelData[$oldLevel] ?? [];
  307. $beforeName = $before['name'] ?? '普通客户';
  308. $discount = $current['discount'] ?? 10;
  309. $memberName = $current['name'] ?? '';
  310. $oldGrowth = $custom->growth;
  311. $hd->member = $newLevel;
  312. $hd->discount = $discount;
  313. $hd->memberName = $memberName;
  314. $hd->save();
  315. $event = '手动修改';
  316. if ($style == 0) {
  317. util::fail('充值不支持升级会员');
  318. }
  319. $change = [
  320. 'customId' => $custom->id,
  321. 'customName' => $custom->name,
  322. 'hdId' => $hd->id,
  323. 'hdName' => $hd->name,
  324. 'mainId' => $mainId,
  325. 'shopId' => $shopId,
  326. 'targetId' => $targetId,
  327. 'rechargeAmount' => $rechargeAmount,
  328. 'days' => 0,
  329. 'daysBefore' => '0000-00-00 00:00:00',
  330. 'daysAfter' => '0000-00-00 00:00:00',
  331. 'beforeLevel' => $oldLevel,
  332. 'afterLevel' => $newLevel,
  333. 'beforeName' => $beforeName,
  334. 'afterName' => $memberName,
  335. 'style' => $style,
  336. 'event' => $event,
  337. 'staffId' => $staffId,
  338. 'staffName' => $staffName,
  339. ];
  340. MemberChangeClass::add($change);
  341. $custom->member = $newLevel;
  342. $custom->discount = $discount;
  343. $custom->memberName = $memberName;
  344. //成长值变更
  345. $growth = $current['amount'] ?? 0;
  346. $custom->growth = $growth;
  347. $custom->save();
  348. //成长值变动记录
  349. $growthData = [
  350. 'shopId' => $shopId,
  351. 'userId' => $custom->userId,
  352. 'userName' => $custom->name,
  353. 'event' => "管理员变更{$memberName}级,成长值从{$oldGrowth}变更为{$growth}",
  354. 'relateId' => 0,
  355. 'relateType' => 0,
  356. 'num' => $custom->growth - $oldGrowth,
  357. 'growth' => $growth,
  358. //'operatorId' => $adminId,
  359. 'staffId' => $staffId, //操作人(员工id)
  360. 'staffName' => $staffName, //员工名称
  361. 'createTime' => date('Y-m-d H:i:s')
  362. ];
  363. UserGrowthClass::add($growthData);
  364. }
  365. //客户采购欠款金额增加,如果有余额时,要进行欠款销账 ssh 20250715
  366. //这边开欠款单时,如果还有余额,会产生结账动作,结账动作也可以用赠送余额来结账,所以要分出赠送余额的占比
  367. //结账消余额,不是下单时消余额,不涉及售后时余额回滚问题,所以不需要设置 xhOrder 的 giveBalance payBalance balance等值
  368. public static function skCgDebtAmountAdd($custom, $order)
  369. {
  370. $clearAmount = 0;
  371. $amount = $order->remainDebtPrice ?? 0;
  372. $staffId = $order->shopAdminId ?? 0;
  373. $staffName = $order->shopAdminName ?? '';
  374. $orderId = $order->id;
  375. $orderSn = $order->orderSn ?? '';
  376. $shopId = $order->shopId;
  377. $settleNo = '';
  378. $settleId = 0;
  379. $settleAmount = 0;
  380. $hdId = $custom->hdId ?? 0;
  381. $hd = HdClass::getLockById($hdId);
  382. if (empty($hd)) {
  383. util::fail('花店信息缺失,编号6325');
  384. }
  385. $hdName = $hd->name ?? '';
  386. //结账时用了多少赠送的余额
  387. $settleUseGiveAmount = 0;
  388. if ($custom->balance > 0) {
  389. if ($amount > $custom->balance) {
  390. $clearAmount = $custom->balance;
  391. $settleUseGiveAmount = $custom->balanceGive;
  392. } else {
  393. util::fail('余额充足,请用余额支付');
  394. }
  395. }
  396. if ($clearAmount > 0) {
  397. $shop = ShopClass::getById($shopId, true);
  398. $needClearIdMap[] = ['orderId' => $orderId, 'clearAmount' => $clearAmount, 'orderSn' => $orderSn];
  399. $setParams = ['staffId' => $staffId, 'staffName' => $staffName];
  400. $set = SettleClass::addSettle($needClearIdMap, $shop, $custom, $hd, $setParams);
  401. $ret = SettleClass::clearSettle($set);
  402. $settleAmount = $ret['totalClearAmount'] ?? 0;
  403. $settleNo = $set->orderSn ?? '';
  404. $settleId = $set->id;
  405. $newSettle = $ret['settle'];
  406. //结账时用了多少赠送的余额
  407. $newSettle->giveBalance = bcadd($newSettle->giveBalance, $settleUseGiveAmount, 2);
  408. $newSettle->save();
  409. }
  410. /************************ 总余额 *********************************/
  411. $customBalance = bcsub($custom->balance, $amount, 2);
  412. $custom->balance = $customBalance;
  413. if ($custom->balance < 0) {
  414. $custom->isDebt = 1;
  415. }
  416. $custom->save();
  417. $hdBalance = bcsub($hd->balance, $amount, 2);
  418. $hd->balance = $hdBalance;
  419. if ($hd->balance < 0) {
  420. $hd->debt = 2;
  421. }
  422. $hd->save();
  423. if (floatval($customBalance) != floatval($hdBalance)) {
  424. util::fail('余额有错,请联系管理员,编号' . $hdId);
  425. }
  426. $relateId = $order->id ?? 0;
  427. $customId = $custom->id ?? 0;
  428. $orderSn = $order->orderSn ?? '';
  429. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  430. $event = '购买花材,单号:' . $orderSn;
  431. $customName = $custom->name ?? '';
  432. $onlinePay = 1;
  433. $side = 0;
  434. $payWay = 0;
  435. $change = [
  436. 'hdId' => $hdId,
  437. 'hdName' => $hdName,
  438. 'customId' => $customId,
  439. 'customName' => $customName,
  440. 'relateId' => $relateId,
  441. 'onlinePay' => $onlinePay,
  442. 'capitalType' => $capitalType,
  443. 'amount' => $amount,
  444. 'balance' => $customBalance,
  445. 'io' => 0,
  446. 'side' => $side,
  447. 'payWay' => $payWay,
  448. 'event' => $event,
  449. 'staffId' => $staffId,
  450. 'staffName' => $staffName,
  451. 'shopId' => $shopId,
  452. 'mainId' => $order->mainId,
  453. 'settleId' => $settleId,
  454. 'settleNo' => $settleNo,
  455. 'settleAmount' => $settleAmount,
  456. 'remark' => '',
  457. ];
  458. BalanceChangeClass::add($change, true);
  459. $mustUseGiveAmount = 0;
  460. // 多处需要同步修改 balance_pay_give
  461. if ($hd->balancePay >= $amount) {
  462. util::fail('请用余额支付哈');
  463. } else {
  464. // 如果【充值金额】小于订单欠款金额,则用全部【充值金额】消单,同时必须用上【赠送金额】,够抵,则【充值金额】还是正数,不够抵,则是负数
  465. if ($hd->balanceGive <= 0) {
  466. //没有【赠送金额】,则用【充值金额】来消全部的欠款金额,不够则变成负数
  467. $payAmount = $amount;
  468. } else {
  469. //这个是【总余额】,包括【充值金额】和【赠送金额】
  470. $currentAdd = bcadd($hd->balancePay, $hd->balanceGive, 2);
  471. if ($currentAdd >= $amount) {
  472. util::fail('请使用余额支付哦');
  473. } else {
  474. //【总余额】不够抵欠款金额,先用掉全部的【赠送金额】,其他的全部算在【充值金额】名下,因为充值金额也不够扣,会出现负数
  475. $mustUseGiveAmount = $hd->balanceGive;
  476. $payAmount = bcsub($amount, $mustUseGiveAmount, 2);
  477. }
  478. }
  479. }
  480. /************************* 充值余额 *****************************/
  481. $customBalancePay = bcsub($custom->balancePay, $payAmount, 2);
  482. $custom->balancePay = $customBalancePay;
  483. $custom->save();
  484. $hdBalancePay = bcsub($hd->balancePay, $payAmount, 2);
  485. $hd->balancePay = $hdBalancePay;
  486. $hd->save();
  487. if (floatval($customBalancePay) != floatval($hdBalancePay)) {
  488. util::fail('余额有错呢,请联系管理员,编号' . $hdId);
  489. }
  490. $relateId = $order->id ?? 0;
  491. $customId = $custom->id ?? 0;
  492. $orderSn = $order->orderSn ?? '';
  493. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  494. $event = '购买花材,单号:' . $orderSn;
  495. $customName = $custom->name ?? '';
  496. $onlinePay = 1;
  497. $side = 0;
  498. $payWay = 0;
  499. $payChange = [
  500. 'hdId' => $hdId,
  501. 'hdName' => $hdName,
  502. 'customId' => $customId,
  503. 'customName' => $customName,
  504. 'relateId' => $relateId,
  505. 'onlinePay' => $onlinePay,
  506. 'capitalType' => $capitalType,
  507. 'amount' => $payAmount,
  508. 'balance' => $customBalancePay,
  509. 'io' => 0,
  510. 'side' => $side,
  511. 'payWay' => $payWay,
  512. 'event' => $event,
  513. 'staffId' => $staffId,
  514. 'staffName' => $staffName,
  515. 'shopId' => $shopId,
  516. 'mainId' => $order->mainId,
  517. 'settleId' => $settleId,
  518. 'settleNo' => $settleNo,
  519. 'settleAmount' => $settleAmount,
  520. 'remark' => '',
  521. ];
  522. BalancePayChangeClass::add($payChange, true);
  523. /***************************** 赠送余额 ******************************/
  524. //应用掉的赠送金额
  525. if ($mustUseGiveAmount > 0) {
  526. $customBalanceGive = bcsub($custom->balanceGive, $mustUseGiveAmount, 2);
  527. if ($customBalanceGive < 0) {
  528. util::fail('余额异常哈,请联系管理员,编号' . $hdId . ' ' . $custom->balanceGive);
  529. }
  530. $custom->balanceGive = $customBalanceGive;
  531. $custom->save();
  532. $hdBalanceGive = bcsub($hd->balanceGive, $mustUseGiveAmount, 2);
  533. $hd->balanceGive = $hdBalanceGive;
  534. $hd->save();
  535. if (floatval($customBalanceGive) != floatval($hdBalanceGive)) {
  536. util::fail('余额有问题呢,请联系管理员,编号' . $hdId);
  537. }
  538. $relateId = $order->id ?? 0;
  539. $customId = $custom->id ?? 0;
  540. $orderSn = $order->orderSn ?? '';
  541. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  542. $event = '购买花材,单号:' . $orderSn;
  543. $customName = $custom->name ?? '';
  544. $onlinePay = 1;
  545. $side = 0;
  546. $payWay = 0;
  547. $giveChange = [
  548. 'hdId' => $hdId,
  549. 'hdName' => $hdName,
  550. 'customId' => $customId,
  551. 'customName' => $customName,
  552. 'relateId' => $relateId,
  553. 'onlinePay' => $onlinePay,
  554. 'capitalType' => $capitalType,
  555. 'amount' => $mustUseGiveAmount,
  556. 'balance' => $customBalanceGive,
  557. 'io' => 0,
  558. 'side' => $side,
  559. 'payWay' => $payWay,
  560. 'event' => $event,
  561. 'staffId' => $staffId,
  562. 'staffName' => $staffName,
  563. 'shopId' => $shopId,
  564. 'mainId' => $order->mainId,
  565. 'settleId' => $settleId,
  566. 'settleNo' => $settleNo,
  567. 'settleAmount' => $settleAmount,
  568. 'remark' => '',
  569. ];
  570. BalanceGiveChangeClass::add($giveChange, true);
  571. }
  572. $addBalance = bcadd($hd->balancePay, $hd->balanceGive, 2);
  573. if (floatval($addBalance) != floatval($custom->balance)) {
  574. util::fail('账户余额有问题,请检查,编号' . $customId);
  575. }
  576. }
  577. //欠款和余额支付的售后 ssh 20250803
  578. public static function skRefundDebtAmountReduce($custom, $amount, $refund, $payWay)
  579. {
  580. $hdId = $custom->hdId ?? 0;
  581. $hd = HdClass::getLockById($hdId);
  582. if (empty($hd)) {
  583. util::fail('花店信息缺失,编号5362');
  584. }
  585. $customBalance = bcadd($custom->balance, $amount, 2);
  586. $custom->balance = $customBalance;
  587. if ($custom->balance < 0) {
  588. $custom->isDebt = 1;
  589. }
  590. $custom->save();
  591. $hdBalance = bcadd($hd->balance, $amount, 2);
  592. $hd->balance = $hdBalance;
  593. if ($hd->balance < 0) {
  594. $hd->debt = 2;
  595. }
  596. $hd->save();
  597. if (floatval($customBalance) !== floatval($hdBalance)) {
  598. util::fail('余额错误,请联系管理员,编号' . $hdId);
  599. }
  600. $relateId = $refund->id ?? 0;
  601. $customId = $custom->id ?? 0;
  602. $refundSn = $refund->refundSn ?? '';
  603. $orderSn = $refund->orderSn ?? '';
  604. $orderId = $refund->orderId ?? 0;
  605. $order = OrderClass::getLockById($orderId);
  606. if (empty($order)) {
  607. util::fail('没有找到订单哈!');
  608. }
  609. // 多处需要同步修改 balance_pay_give
  610. $mustUseGiveAmount = 0;
  611. $buyAmount = 0;
  612. if ($payWay == 3) {
  613. //如果是欠款单售后,没有退款到赠送余额的情况
  614. $buyAmount = $amount;
  615. } elseif ($payWay == 2) {
  616. //这个是总消费余额中充值余额部分
  617. $payBalance = $order->payBalance ?? 0;
  618. //这个是累计已经退还的余额
  619. $balanceTk = $order->balanceTk ?? 0;
  620. $currentBalanceTk = bcadd($balanceTk, $amount, 2);
  621. //当前累计退还的余额如果超过了充值余额的话,说明要退还赠送的余额了
  622. if ($currentBalanceTk > $payBalance) {
  623. $pastAmount = bcsub($currentBalanceTk, $payBalance, 2);
  624. if ($pastAmount > $amount) {
  625. $mustUseGiveAmount = $amount;
  626. } else {
  627. $buyAmount = bcsub($amount, $pastAmount, 2);
  628. $mustUseGiveAmount = bcsub($amount, $buyAmount, 2);
  629. }
  630. } else {
  631. //累计退还的余额,还没超过充值余额,还是退充值余额部分
  632. $buyAmount = $amount;
  633. }
  634. } else {
  635. util::fail('无效的支付方式!编号69880');
  636. }
  637. $staffId = $refund->shopAdminId ?? 0;
  638. $staffName = $refund->shopAdminName ?? '';
  639. $capitalType = dict::getDict('capitalType', 'hdRefund', 'id');
  640. $event = "订单售后 " . $refundSn;
  641. $customName = $custom->name ?? '';
  642. $onlinePay = 1;
  643. $side = 0;
  644. $payWay = 2;//余额支付
  645. $change = [
  646. 'customId' => $customId,
  647. 'customName' => $customName,
  648. 'relateId' => $relateId,
  649. 'relateSn' => $refundSn,
  650. 'onlinePay' => $onlinePay,
  651. 'capitalType' => $capitalType,
  652. 'amount' => $amount,
  653. 'balance' => $customBalance,
  654. 'io' => 1,
  655. 'side' => $side,
  656. 'payWay' => $payWay,
  657. 'event' => $event,
  658. 'staffId' => $staffId,
  659. 'staffName' => $staffName,
  660. 'shopId' => $refund->shopId,
  661. 'mainId' => $refund->mainId,
  662. 'refundOrderId' => $orderId,
  663. 'refundOrderSn' => $orderSn,
  664. 'remark' => '',
  665. ];
  666. BalanceChangeClass::add($change, true);
  667. $order->balanceTk = bcadd($order->balanceTk, $amount, 2);
  668. if ($order->balanceTk < 0) {
  669. util::fail('不能小于0,编号88672');
  670. }
  671. $order->save();
  672. if ($buyAmount > 0) {
  673. $customBalancePay = bcadd($custom->balancePay, $buyAmount, 2);
  674. $custom->balancePay = $customBalancePay;
  675. $custom->save();
  676. $hdBalancePay = bcadd($hd->balancePay, $buyAmount, 2);
  677. $hd->balancePay = $hdBalancePay;
  678. $hd->save();
  679. $payChange = [
  680. 'customId' => $customId,
  681. 'customName' => $customName,
  682. 'relateId' => $relateId,
  683. 'relateSn' => $refundSn,
  684. 'onlinePay' => $onlinePay,
  685. 'capitalType' => $capitalType,
  686. 'amount' => $buyAmount,
  687. 'balance' => $hdBalancePay,
  688. 'io' => 1,
  689. 'side' => $side,
  690. 'payWay' => $payWay,
  691. 'event' => $event,
  692. 'staffId' => $staffId,
  693. 'staffName' => $staffName,
  694. 'shopId' => $refund->shopId,
  695. 'mainId' => $refund->mainId,
  696. 'refundOrderId' => $orderId,
  697. 'refundOrderSn' => $orderSn,
  698. 'remark' => '',
  699. ];
  700. BalancePayChangeClass::add($payChange, true);
  701. $order->payBalanceTk = bcadd($order->payBalanceTk, $buyAmount, 2);
  702. if ($order->payBalanceTk < 0) {
  703. util::fail('不能小于0,编号88673');
  704. }
  705. $order->save();
  706. }
  707. if ($mustUseGiveAmount > 0) {
  708. $customBalanceGive = bcadd($custom->balanceGive, $mustUseGiveAmount, 2);
  709. $custom->balanceGive = $customBalanceGive;
  710. $custom->save();
  711. $hdBalanceGive = bcadd($hd->balanceGive, $mustUseGiveAmount, 2);
  712. $hd->balanceGive = $hdBalanceGive;
  713. $hd->save();
  714. $giveChange = [
  715. 'customId' => $customId,
  716. 'customName' => $customName,
  717. 'relateId' => $relateId,
  718. 'relateSn' => $refundSn,
  719. 'onlinePay' => $onlinePay,
  720. 'capitalType' => $capitalType,
  721. 'amount' => $mustUseGiveAmount,
  722. 'balance' => $hdBalanceGive,
  723. 'io' => 1,
  724. 'side' => $side,
  725. 'payWay' => $payWay,
  726. 'event' => $event,
  727. 'staffId' => $staffId,
  728. 'staffName' => $staffName,
  729. 'shopId' => $refund->shopId,
  730. 'mainId' => $refund->mainId,
  731. 'refundOrderId' => $orderId,
  732. 'refundOrderSn' => $orderSn,
  733. 'remark' => '',
  734. ];
  735. BalanceGiveChangeClass::add($giveChange, true);
  736. $order->giveBalanceTk = bcadd($order->giveBalanceTk, $mustUseGiveAmount, 2);
  737. if ($order->giveBalanceTk < 0) {
  738. util::fail('不能小于0,编号88675');
  739. }
  740. $order->save();
  741. }
  742. }
  743. //添加客户 ssh 2021.2.28
  744. public static function addCustom($data)
  745. {
  746. return self::add($data, true);
  747. }
  748. //创建或获取客户信息 ssh 2021.3.19
  749. public static function buildRelation($shop, $user)
  750. {
  751. if (empty($user)) {
  752. return false;
  753. }
  754. if (empty($shop)) {
  755. return false;
  756. }
  757. if ($shop->ptStyle == 2) {
  758. util::fail('有问题,批发店状态');
  759. }
  760. $salt = stringUtil::charsShuffleLowerCase(8);
  761. $isNewCustom = 0;
  762. $userId = $user->id;
  763. $name = $user->name ?? '';
  764. $mobile = $user->mobile ?? '';
  765. $shopId = $shop->id ?? 0;
  766. $hdAvatar = $shop->avatar ?? '';
  767. $sjId = $shop->sjId ?? 0;
  768. $sjName = $shop->merchantName ?? '';
  769. $shopName = $shop->shopName ?? '';
  770. $hdMobile = $shop->mobile ?? '';
  771. $hdName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  772. $hdPy = stringUtil::py($hdName);
  773. $custom = self::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
  774. if (empty($custom)) {
  775. $py = stringUtil::py($name);
  776. $visitTime = date("Y-m-d H:i:s");
  777. $data = [
  778. 'userId' => $userId,
  779. 'sjId' => $sjId,
  780. 'shopId' => $shopId,
  781. 'name' => $name,
  782. 'originName' => $name,
  783. 'py' => $py,
  784. 'mobile' => $mobile,
  785. 'visitTime' => $visitTime,
  786. 'salt' => $salt,
  787. ];
  788. $birthdaySet = $user->birthdaySet ?? 0;
  789. if ($birthdaySet == 1) {
  790. //如果用户自己设置过了生日,则生日给传过去
  791. $lunar = $user->lunar ?? 0;
  792. $birthday = $user->birthday ?? '';
  793. $birthdayTime = $user->birthdayTime ?? 0;
  794. $birthdayMonth = $user->birthdayMonth ?? 0;
  795. $birthdayDate = $user->birthdayDate ?? 0;
  796. //如果当年的生日时间没有更新,则需要给更新
  797. $year = date("Y", strtotime($birthday));
  798. $thisYear = date("Y");
  799. if ($year != $thisYear) {
  800. if ($lunar == 1) {
  801. try {
  802. //农历转阳历,有多处使用,需要同步修改,关键词change_my_birthday
  803. $calendar = new Calendar();
  804. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  805. $month = $result['gregorian_month'] ?? 1;
  806. $date = $result['gregorian_day'] ?? 1;
  807. $birthday = date("Y") . '-' . $month . '-' . $date;
  808. $user->birthday = $birthday;
  809. $user->save();
  810. CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
  811. } catch (\InvalidArgumentException $e) {
  812. $msg = $e->getMessage();
  813. noticeUtil::push("同步生日出错了,需要检查确认,userId:" . $userId . ' 月份:' . $birthdayMonth . ' 日期:' . $birthdayDate . ' ' . $msg, '15280215347');
  814. $birthday = '0000-00-00 00:00:00';
  815. }
  816. } else {
  817. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  818. $user->birthday = $birthday;
  819. $user->save();
  820. CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
  821. }
  822. }
  823. $data['birthday'] = $birthday;
  824. $data['birthdayTime'] = $birthdayTime;
  825. $data['birthdayMonth'] = $birthdayMonth;
  826. $data['birthdayDate'] = $birthdayDate;
  827. $data['lunar'] = $lunar;
  828. }
  829. // 处理并发插入:尝试插入,如果遇到唯一键冲突则重新查询
  830. try {
  831. $custom = self::addCustom($data);
  832. $isNewCustom = 1;
  833. } catch (\yii\db\IntegrityException $e) {
  834. // 唯一键冲突,说明其他并发请求已经插入,重新查询
  835. if (strpos($e->getMessage(), 'Duplicate entry') !== false) {
  836. $custom = self::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
  837. if (empty($custom)) {
  838. // 如果仍然查不到,则抛出异常
  839. throw $e;
  840. }
  841. $isNewCustom = 0;
  842. } else {
  843. throw $e;
  844. }
  845. }
  846. }
  847. $customId = $custom->id ?? 0;
  848. $hd = HdClass::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
  849. if (empty($hd)) {
  850. $hdData = [
  851. 'shopId' => $shopId,
  852. 'userId' => $userId,
  853. 'name' => $hdName,
  854. 'sjId' => $sjId,
  855. 'customId' => $customId,
  856. 'avatar' => $hdAvatar,
  857. 'py' => $hdPy,
  858. 'mobile' => $hdMobile,
  859. 'province' => $shop->province ?? '',
  860. 'city' => $shop->city ?? '',
  861. 'dist' => $shop->dist ?? '',
  862. 'address' => $shop->address ?? '',
  863. 'floor' => $shop->floor ?? '',
  864. 'fullAddress' => $shop->fullAddress ?? '',
  865. 'showAddress' => $shop->showAddress ?? '',
  866. 'lat' => $shop->lat ?? '',
  867. 'long' => $shop->long ?? '',
  868. 'salt' => $salt,
  869. ];
  870. // 处理并发插入:尝试插入,如果遇到唯一键冲突则重新查询
  871. try {
  872. $hd = HdClass::addData($hdData);
  873. } catch (\yii\db\IntegrityException $e) {
  874. // 唯一键冲突,说明其他并发请求已经插入,重新查询
  875. if (strpos($e->getMessage(), 'Duplicate entry') !== false) {
  876. $hd = HdClass::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
  877. if (empty($hd)) {
  878. // 如果仍然查不到,则抛出异常
  879. throw $e;
  880. }
  881. } else {
  882. throw $e;
  883. }
  884. }
  885. $isNewCustom = 1;
  886. }
  887. $hdId = $hd->id ?? 0;
  888. $custom->hdId = $hdId;
  889. $custom->save();
  890. if ($isNewCustom == 1) {
  891. WxMessageClass::newHdCustomInform($shop, $custom);
  892. }
  893. return ['custom' => $custom, 'hd' => $hd];
  894. }
  895. //获取客户信息
  896. public static function getCustom($data)
  897. {
  898. $custom = self::getByCondition($data);
  899. if (empty($custom)) {
  900. $custom = self::add($data);
  901. }
  902. return $custom;
  903. }
  904. //获取客户信息 ssh 2021.2.1
  905. public static function getCustomInfo($id)
  906. {
  907. $custom = self::getById($id);
  908. if (empty($custom)) {
  909. return $custom;
  910. }
  911. $list = self::groupBaseInfo([$custom]);
  912. return current($list);
  913. }
  914. //获取客户信息 ssh 2021.3.19
  915. public static function getCustomByIds($ids)
  916. {
  917. $list = self::getByIds($ids);
  918. $list = self::groupBaseInfo($list);
  919. return $list;
  920. }
  921. public static function valid($info, $shopId)
  922. {
  923. if (isset($info['shopId']) && $info['shopId'] == $shopId) {
  924. return true;
  925. }
  926. util::fail('客户信息无效');
  927. }
  928. //整合基本信息 ssh 2021.1.31
  929. public static function groupBaseInfo($list)
  930. {
  931. if (empty($list)) {
  932. return $list;
  933. }
  934. foreach ($list as $key => $val) {
  935. $shortAvatar = $val['avatar'] ?? '';
  936. $avatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_800,w_800";
  937. $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  938. $bigAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  939. $list[$key]['shortAvatar'] = $shortAvatar;
  940. $list[$key]['avatar'] = $avatar;
  941. $list[$key]['smallAvatar'] = $smallAvatar;
  942. $list[$key]['bigAvatar'] = $bigAvatar;
  943. //超时未下单
  944. $recentExpend = $val['recentExpend'] ?? '';
  945. $list[$key]['overTimeUnExpend'] = 0;
  946. if ($recentExpend == '0000-00-00 00:00:00') {
  947. $list[$key]['overTimeUnExpend'] = 10000;
  948. } else {
  949. $recentExpendTime = strtotime($recentExpend);
  950. $seven = bcmul(86400, 7);
  951. $sevenTime = bcadd($recentExpendTime, $seven);
  952. if ($sevenTime < time()) {
  953. $list[$key]['overTimeUnExpend'] = 7;
  954. }
  955. $month = bcmul(86400, 30);
  956. $monthTime = bcadd($recentExpendTime, $month);
  957. if ($monthTime < time()) {
  958. $list[$key]['overTimeUnExpend'] = 30;
  959. }
  960. $halfYear = bcmul(86400, 180);
  961. $halfYearTime = bcadd($recentExpendTime, $halfYear);
  962. if ($halfYearTime < time()) {
  963. $list[$key]['overTimeUnExpend'] = 180;
  964. }
  965. $year = bcmul(86400, 365);
  966. $yearTime = bcmul($recentExpendTime, $year);
  967. if ($yearTime < time()) {
  968. $list[$key]['overTimeUnExpend'] = 365;
  969. }
  970. }
  971. }
  972. return $list;
  973. }
  974. // 客户消费等级批量变更
  975. public static function updateCustomExpenseLevel($data)
  976. {
  977. $shopId = $data['shopId'];
  978. $mainId = $data['mainId'];
  979. $customs = self::getAllByCondition(['shopId' => $shopId], null, 'id, name, hdId, userId, buyAmount, growth, member, memberName, discount', null, true);
  980. $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount, discount');
  981. if (!empty($customs) && !empty($levelDatas)) {
  982. foreach ($customs as $c) {
  983. // $buyAmount = 0;
  984. // // 获取所有零售订单,计算消费金额
  985. // $orderList = \bizHd\order\classes\OrderClass::getAllByCondition(['customId' => $c->id], null, 'id, actPrice');
  986. // if (!empty($orderList)) {
  987. // foreach ($orderList as $order) {
  988. // $buyAmount = bcadd($buyAmount, $order['actPrice'], 2);
  989. // }
  990. // }
  991. // $c->buyAmount = $buyAmount;
  992. // 根据 growth 设置等级
  993. $memberData = self::getCustomExpenseLevel($c->growth, $mainId, $levelDatas); // 设置等级多处需要同步修改的,请搜索:getCustomExpenseLevel
  994. if ($c->member != $memberData['level'] || $c->memberName != $memberData['name']) {
  995. $hdId = $c->hdId;
  996. $hd = HdClass::getById($hdId, true);
  997. $change = [
  998. 'customId' => $c->id,
  999. 'customName' => $c->name,
  1000. 'hdId' => $hd->id,
  1001. 'hdName' => $hd->name,
  1002. 'mainId' => $mainId,
  1003. 'shopId' => $shopId,
  1004. 'targetId' => 0,
  1005. 'rechargeAmount' => 0,
  1006. 'days' => 0,
  1007. 'daysBefore' => '0000-00-00 00:00:00',
  1008. 'daysAfter' => '0000-00-00 00:00:00',
  1009. 'beforeLevel' => $c->member,
  1010. 'afterLevel' => $memberData['level'],
  1011. 'beforeName' => $c->memberName,
  1012. 'afterName' => $memberData['name'],
  1013. 'style' => 1, //手动修改会员,脚本执行
  1014. 'event' => '系统脚本修改',
  1015. 'staffId' => 0,
  1016. 'staffName' => '',
  1017. ];
  1018. MemberChangeClass::add($change);
  1019. }
  1020. $c->member = $memberData['level'];
  1021. $c->memberName = $memberData['name'];
  1022. $c->discount = bcdiv(floatval($memberData['discount']), 10, 2);
  1023. $c->save();
  1024. if (!empty($c->hdId)) {
  1025. $hd = HdClass::getById($c->hdId, true);
  1026. if (!empty($hd)) {
  1027. $hd->member = $memberData['level'];
  1028. $hd->memberName = $memberData['name'];
  1029. $hd->discount = $c->discount;
  1030. $hd->save();
  1031. }
  1032. }
  1033. // 更新客户所对应花店的消费金额
  1034. // HdClass::updateByCondition(['shopId'=>$shopId, 'userId'=>$c->userId], ['expendAmount'=>$buyAmount]);
  1035. }
  1036. }
  1037. return true;
  1038. }
  1039. // 获取客户消费等级
  1040. public static function getCustomExpenseLevel($growth, $mainId, $levelDatas=[])
  1041. {
  1042. if (empty($levelDatas)) {
  1043. $levelDatas = MemberLevelClass::getAllByCondition(['mainId' => $mainId], 'amount DESC', 'id, name, level, amount, discount');
  1044. }
  1045. foreach ($levelDatas as $data) {
  1046. if ($growth >= $data['amount']) {
  1047. return $data;
  1048. }
  1049. }
  1050. return ['level'=>0, 'name'=>'', 'discount'=>10];
  1051. }
  1052. }