CustomClass.php 42 KB

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