CustomClass.php 43 KB

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