CustomClass.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. namespace bizHd\custom\classes;
  3. use biz\wx\classes\WxMessageClass;
  4. use bizGhs\shop\classes\TotalDebtChangeClass;
  5. use bizHd\balance\classes\BalanceChangeClass;
  6. use bizHd\member\classes\MemberChangeClass;
  7. use bizHd\member\classes\MemberWealClass;
  8. use bizHd\order\classes\SettleClass;
  9. use bizHd\shop\classes\MainClass;
  10. use bizHd\shop\classes\ShopClass;
  11. use bizMall\user\classes\UserClass;
  12. use common\components\dict;
  13. use common\components\imgUtil;
  14. use common\components\noticeUtil;
  15. use common\components\stringUtil;
  16. use common\components\util;
  17. use Yii;
  18. use bizHd\base\classes\BaseClass;
  19. class CustomClass extends BaseClass
  20. {
  21. public static $baseFile = '\bizHd\custom\models\Custom';
  22. //用余额支付,客户余额减少 ssh 20250410
  23. public static function payToChangeBalance($order)
  24. {
  25. $actPrice = $order->actPrice ?? 0;
  26. $hdId = $order->hdId ?? 0;
  27. $hd = HdClass::getLockById($hdId);
  28. $balance = $hd->balance ?? 0;
  29. if ($actPrice > $balance) {
  30. util::fail('余额不足');
  31. }
  32. $remain = bcsub($balance, $actPrice, 2);
  33. $hd->balance = $remain;
  34. $hd->save();
  35. $hdName = $hd->name;
  36. $customId = $order->customId ?? 0;
  37. $custom = self::getLockById($customId);
  38. $balance = $custom->balance ?? 0;
  39. if ($actPrice > $balance) {
  40. util::fail('余额不足哦');
  41. }
  42. $remain = bcsub($balance, $actPrice, 2);
  43. $custom->balance = $remain;
  44. $custom->save();
  45. $customName = $custom->name;
  46. if ($custom->balance != $hd->balance) {
  47. util::fail('余额有问题哦');
  48. }
  49. $relateId = $order->id ?? 0;
  50. //0 花店操作 1 客户操作
  51. $payWay = $order->payWay;
  52. $side = 1;
  53. $shopId = $order->shopId;
  54. $mainId = $order->mainId;
  55. $staffId = $order->shopAdminId;
  56. $staffName = $order->shopAdminName;
  57. $orderSn = $order->orderSn;
  58. $event = '买花,单号:' . $orderSn;
  59. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  60. $change = [
  61. 'hdId' => $hdId,
  62. 'hdName' => $hdName,
  63. 'customId' => $customId,
  64. 'customName' => $customName,
  65. 'relateId' => $relateId,
  66. 'onlinePay' => 1,
  67. 'capitalType' => $capitalType,
  68. 'amount' => $actPrice,
  69. 'balance' => $hd->balance,
  70. 'io' => 0,
  71. 'side' => $side,
  72. 'payWay' => $payWay,
  73. 'event' => $event,
  74. 'staffId' => $staffId,
  75. 'staffName' => $staffName,
  76. 'shopId' => $shopId,
  77. 'mainId' => $mainId,
  78. 'remark' => '',
  79. ];
  80. return BalanceChangeClass::add($change, true);
  81. }
  82. //会员等级变化,包括手动修改、自动降会员和充值升会员等 ssh 20250324
  83. public static function levelChange($oldLevel, $newLevel, $shop, $custom, $hd, $params)
  84. {
  85. $staffId = $params['staffId'] ?? 0;
  86. $staffName = $params['staffName'] ?? '';
  87. $style = $params['style'] ?? 0;
  88. $target = $params['target'] ?? [];
  89. $onlinePay = $params['onlinePay'] ?? 1;
  90. $rechargeAmount = $params['rechargeAmount'] ?? 0;
  91. $targetId = $target->id ?? 0;
  92. $mainId = $shop->mainId;
  93. $shopId = $shop->id;
  94. $weal = MemberWealClass::getAllByCondition(['mainId' => $mainId], null, '*', 'level');
  95. $current = $weal[$newLevel] ?? [];
  96. $before = $weal[$oldLevel] ?? [];
  97. $beforeName = $before['name'] ?? '普通客户';
  98. $discount = $current['discount'] ?? 1;
  99. $memberName = $current['name'] ?? '';
  100. $custom->member = $newLevel;
  101. $custom->discount = $discount;
  102. $custom->memberName = $memberName;
  103. $custom->save();
  104. $hd->member = $newLevel;
  105. $hd->discount = $discount;
  106. $hd->memberName = $memberName;
  107. $hd->save();
  108. $event = '手动修改';
  109. if ($style == 0) {
  110. if ($onlinePay == 2) {
  111. $event = '线上充值' . $rechargeAmount . '元';
  112. } else {
  113. $event = '线下充值' . $rechargeAmount . '元';
  114. }
  115. }
  116. $change = [
  117. 'customId' => $custom->id,
  118. 'customName' => $custom->name,
  119. 'hdId' => $hd->id,
  120. 'hdName' => $hd->name,
  121. 'mainId' => $mainId,
  122. 'shopId' => $shopId,
  123. 'targetId' => $targetId,
  124. 'rechargeAmount' => $rechargeAmount,
  125. 'days' => 0,
  126. 'daysBefore' => '0000-00-00 00:00:00',
  127. 'daysAfter' => '0000-00-00 00:00:00',
  128. 'beforeLevel' => $oldLevel,
  129. 'afterLevel' => $newLevel,
  130. 'beforeName' => $beforeName,
  131. 'afterName' => $memberName,
  132. 'style' => $style,
  133. 'event' => $event,
  134. 'staffId' => $staffId,
  135. 'staffName' => $staffName,
  136. ];
  137. MemberChangeClass::add($change);
  138. }
  139. //客户采购欠款金额增加,如果有余额时,要进行欠款销账 ssh 20250715
  140. public static function cgDebtAmountAdd($custom, $order)
  141. {
  142. $clearAmount = 0;
  143. $amount = $order->remainDebtPrice ?? 0;
  144. $staffId = $order->shopAdminId ?? 0;
  145. $staffName = $order->shopAdminName ?? '';
  146. $orderId = $order->id;
  147. $orderSn = $order->orderSn ?? '';
  148. $shopId = $order->shopId;
  149. $settleNo = '';
  150. $settleId = 0;
  151. $settleAmount = 0;
  152. $hdId = $custom->hdId ?? 0;
  153. $hd = HdClass::getLockById($hdId);
  154. if (empty($hd)) {
  155. util::fail('花店信息缺失,编号6331');
  156. }
  157. if ($custom->balance > 0) {
  158. if ($amount >= $custom->balance) {
  159. $clearAmount = $custom->balance;
  160. } else {
  161. $clearAmount = $amount;
  162. }
  163. }
  164. if ($clearAmount > 0) {
  165. $shop = ShopClass::getById($shopId, true);
  166. $needClearIdMap[] = ['orderId' => $orderId, 'clearAmount' => $clearAmount, 'orderSn' => $orderSn];
  167. $setParams = ['staffId' => $staffId, 'staffName' => $staffName];
  168. $set = SettleClass::addSettle($needClearIdMap, $shop, $custom, $hd, $setParams);
  169. $ret = SettleClass::clearSettle($set);
  170. $settleAmount = $ret['totalClearAmount'] ?? 0;
  171. $settleNo = $set->orderSn ?? '';
  172. $settleId = $set->id;
  173. }
  174. $lastBalance = bcsub($custom->balance, $amount, 2);
  175. $custom->balance = $lastBalance;
  176. if ($custom->balance < 0) {
  177. $custom->isDebt = 1;
  178. }
  179. $custom->save();
  180. $remain = bcsub($hd->balance, $amount, 2);
  181. $hd->balance = $remain;
  182. if ($hd->balance < 0) {
  183. $hd->debt = 2;
  184. }
  185. $hd->save();
  186. $relateId = $order->id ?? 0;
  187. $customId = $custom->id ?? 0;
  188. $orderSn = $order->orderSn ?? '';
  189. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  190. $event = '购买花材,单号:' . $orderSn;
  191. $customName = $custom->name ?? '';
  192. $onlinePay = 1;
  193. $side = 0;
  194. $payWay = 0;
  195. $change = [
  196. 'customId' => $customId,
  197. 'customName' => $customName,
  198. 'relateId' => $relateId,
  199. 'onlinePay' => $onlinePay,
  200. 'capitalType' => $capitalType,
  201. 'amount' => $amount,
  202. 'balance' => $lastBalance,
  203. 'io' => 0,
  204. 'side' => $side,
  205. 'payWay' => $payWay,
  206. 'event' => $event,
  207. 'staffId' => $staffId,
  208. 'staffName' => $staffName,
  209. 'shopId' => $order->id,
  210. 'mainId' => $order->mainId,
  211. 'settleId' => $settleId,
  212. 'settleNo' => $settleNo,
  213. 'settleAmount' => $settleAmount,
  214. 'remark' => '',
  215. ];
  216. BalanceChangeClass::add($change, true);
  217. }
  218. //散客申请退款,零售门店欠款金额减少
  219. public static function refundDebtAmountReduce($custom, $amount, $refund)
  220. {
  221. $debtAmount = $custom->debtAmount ?? 0.00;
  222. $remain = bcsub($debtAmount, $amount, 2);
  223. if ($remain < 0) {
  224. noticeUtil::push("零售客户欠款总金额:{$custom->debtAmount} 本次退款金额:{$amount} 客户ID:{$custom->id} 超出了,编号66379", '15280215347');
  225. util::fail('操作失败,欠款金额会出现负数');
  226. }
  227. $custom->isDebt = $remain <= 0 ? 0 : 1;
  228. $custom->debtAmount = $remain;
  229. $custom->save();
  230. $relateId = $refund->id ?? 0;
  231. $customId = $custom->id ?? 0;
  232. $capitalType = dict::getDict('capitalType', 'hdRefund', 'id');
  233. $refundSn = $refund->refundSn ?? '';
  234. $change = [
  235. 'relateId' => $relateId,
  236. 'customId' => $customId,
  237. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  238. 'capitalType' => $capitalType,
  239. 'amount' => $amount,
  240. 'balance' => $remain,
  241. 'io' => 0,
  242. 'payWay' => 0,
  243. 'event' => "客户申请退款,退款单号:{$refundSn}",
  244. 'sjId' => $refund->sjId ?? 0,
  245. 'shopId' => $refund->shopId ?? 0,
  246. 'mainId' => $refund->mainId ?? 0,
  247. ];
  248. CustomDebtRecordClass::addChange($change);
  249. $mainId = $refund->mainId ?? 0;
  250. $main = MainClass::getLockById($mainId);
  251. if (empty($main)) {
  252. util::fail('没有main信息39');
  253. }
  254. $debt = $main->debt ?? 0;
  255. $totalRemain = bcsub($debt, $amount, 2);
  256. $main->debt = $totalRemain;
  257. $main->save();
  258. $event = $custom->name . "申请退款,退款单号:{$refundSn}";
  259. $change['balance'] = $totalRemain;
  260. $change['event'] = $event;
  261. TotalDebtChangeClass::addChange($change);
  262. }
  263. //客户结账欠款金额减少 ssh 20250627
  264. public static function clearDebtAmountReduce($custom, $amount, $clear)
  265. {
  266. $balance = $custom->balance ?? 0.00;
  267. $remainBalance = bcsub($balance, $amount, 2);
  268. if ($remainBalance < 0) {
  269. noticeUtil::push("零售客户欠款总金额:{$balance} 本次结账金额:{$amount} 客户ID:{$custom->id} 超出了,编号788965", '15280215347');
  270. util::fail('操作失败,结账后欠款金额会出现负数');
  271. }
  272. $custom->balance = $remainBalance;
  273. $custom->save();
  274. $hdId = $custom->hdId ?? 0;
  275. $hd = HdClass::getLockById($hdId);
  276. if (empty($hd)) {
  277. util::fail('店铺信息异常');
  278. }
  279. $hdName = $hd->name ?? '';
  280. $hdBalance = $hd->balance ?? 0;
  281. $hdRemainBalance = bcsub($hdBalance, $amount, 2);
  282. $hd->balance = $hdRemainBalance;
  283. $hd->save();
  284. if (floatval($hdRemainBalance) != floatval($remainBalance)) {
  285. util::fail('余额有问题');
  286. }
  287. $relateId = $clear->id ?? 0;
  288. $customId = $custom->id ?? 0;
  289. $capitalType = dict::getDict('capitalType', 'hdXsClear', 'id');
  290. $orderSn = $clear->orderSn ?? '';
  291. $customName = $custom->name ?? '';
  292. $event = "结账,单号:{$orderSn}";
  293. $staffId = $clear->hdShopAdminId ?? 0;
  294. $staffName = $clear->hdShopAdminName ?? '';
  295. $shopId = $clear->hdShopId ?? 0;
  296. $mainId = $clear->mainId ?? 0;
  297. $remark = '';
  298. $payWay = $clear->payWay ?? 0;
  299. $change = [
  300. 'customId' => $customId,
  301. 'customName' => $customName,
  302. 'hdId' => $hdId,
  303. 'hdName' => $hdName,
  304. 'relateId' => $relateId,
  305. 'onlinePay' => 1,
  306. 'capitalType' => $capitalType,
  307. 'amount' => $amount,
  308. 'balance' => $hdRemainBalance,
  309. 'io' => 0,
  310. 'side' => 0,
  311. 'payWay' => $payWay,
  312. 'event' => $event,
  313. 'staffId' => $staffId,
  314. 'staffName' => $staffName,
  315. 'shopId' => $shopId,
  316. 'mainId' => $mainId,
  317. 'remark' => $remark,
  318. ];
  319. BalanceChangeClass::add($change, true);
  320. }
  321. public static function getCustomCacheKey($sjId, $shopId, $userId)
  322. {
  323. return 'hd_custom_key_' . $sjId . '_' . $shopId . '_' . $userId;
  324. }
  325. //添加客户 ssh 2021.2.28
  326. public static function addCustom($data)
  327. {
  328. return self::add($data, true);
  329. }
  330. //创建或获取客户信息 ssh 2021.3.19
  331. public static function buildRelation($shop, $user)
  332. {
  333. if (empty($user)) {
  334. return false;
  335. }
  336. if (empty($shop)) {
  337. return false;
  338. }
  339. if ($shop->ptStyle == 2) {
  340. util::fail('有问题,批发店状态');
  341. }
  342. $salt = stringUtil::charsShuffleLowerCase(8);
  343. $isNewCustom = 0;
  344. $userId = $user->id;
  345. $name = $user->name ?? '';
  346. $mobile = $user->mobile ?? '';
  347. $shopId = $shop->id ?? 0;
  348. $hdAvatar = $shop->avatar ?? '';
  349. $sjId = $shop->sjId ?? 0;
  350. $sjName = $shop->merchantName ?? '';
  351. $shopName = $shop->shopName ?? '';
  352. $hdMobile = $shop->mobile ?? '';
  353. $hdName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  354. $hdPy = stringUtil::py($hdName);
  355. $custom = self::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
  356. if (empty($custom)) {
  357. $py = stringUtil::py($name);
  358. $visitTime = date("Y-m-d H:i:s");
  359. $data = [
  360. 'userId' => $userId,
  361. 'sjId' => $sjId,
  362. 'shopId' => $shopId,
  363. 'name' => $name,
  364. 'originName' => $name,
  365. 'py' => $py,
  366. 'mobile' => $mobile,
  367. 'visitTime' => $visitTime,
  368. 'salt' => $salt,
  369. ];
  370. $custom = self::addCustom($data);
  371. $isNewCustom = 1;
  372. }
  373. $customId = $custom->id ?? 0;
  374. $hd = HdClass::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
  375. if (empty($hd)) {
  376. $hdData = [
  377. 'shopId' => $shopId,
  378. 'userId' => $userId,
  379. 'name' => $hdName,
  380. 'sjId' => $sjId,
  381. 'customId' => $customId,
  382. 'avatar' => $hdAvatar,
  383. 'py' => $hdPy,
  384. 'mobile' => $hdMobile,
  385. 'province' => $shop->province ?? '',
  386. 'city' => $shop->city ?? '',
  387. 'dist' => $shop->dist ?? '',
  388. 'address' => $shop->address ?? '',
  389. 'floor' => $shop->floor ?? '',
  390. 'fullAddress' => $shop->fullAddress ?? '',
  391. 'showAddress' => $shop->showAddress ?? '',
  392. 'lat' => $shop->lat ?? '',
  393. 'long' => $shop->long ?? '',
  394. 'salt' => $salt,
  395. ];
  396. $hd = HdClass::addData($hdData);
  397. $isNewCustom = 1;
  398. }
  399. $hdId = $hd->id ?? 0;
  400. $custom->hdId = $hdId;
  401. $custom->save();
  402. if ($isNewCustom == 1) {
  403. WxMessageClass::newHdCustomInform($shop, $custom);
  404. }
  405. return ['custom' => $custom, 'hd' => $hd];
  406. }
  407. //获取客户信息
  408. public static function getCustom($data)
  409. {
  410. $custom = self::getByCondition($data);
  411. if (empty($custom)) {
  412. $custom = self::add($data);
  413. }
  414. return $custom;
  415. }
  416. //获取客户信息 ssh 2021.2.1
  417. public static function getCustomInfo($id)
  418. {
  419. $custom = self::getById($id);
  420. if (empty($custom)) {
  421. return $custom;
  422. }
  423. $list = self::groupBaseInfo([$custom]);
  424. return current($list);
  425. }
  426. //获取客户信息 ssh 2021.3.19
  427. public static function getCustomByIds($ids)
  428. {
  429. $list = self::getByIds($ids);
  430. $list = self::groupBaseInfo($list);
  431. return $list;
  432. }
  433. public static function valid($info, $shopId)
  434. {
  435. if (isset($info['shopId']) && $info['shopId'] == $shopId) {
  436. return true;
  437. }
  438. util::fail('客户信息无效');
  439. }
  440. //整合基本信息 ssh 2021.1.31
  441. public static function groupBaseInfo($list)
  442. {
  443. if (empty($list)) {
  444. return $list;
  445. }
  446. foreach ($list as $key => $val) {
  447. $list[$key]['avatar'] = imgUtil::groupImg($val['avatar']);
  448. }
  449. return $list;
  450. }
  451. //更新客户信息 ssh 2021.3.23
  452. public static function updateCustomInfo($custom, $data)
  453. {
  454. $userId = $custom['userId'];
  455. $userData = [];
  456. if (!empty($data['mobile'])) {
  457. $userData['mobile'] = $data['mobile'];
  458. }
  459. if (!empty($userData)) {
  460. UserClass::updateById($userId, $userData);
  461. }
  462. $id = $custom['id'];
  463. self::updateById($id, $data);
  464. }
  465. }