CustomClass.php 42 KB

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