CustomClass.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. $shopId = $order->shopId;
  148. $settleNo = '';
  149. $settleId = 0;
  150. $settleAmount = 0;
  151. if ($custom->balance > 0) {
  152. if ($amount >= $custom->balance) {
  153. $clearAmount = $custom->balance;
  154. } else {
  155. $clearAmount = $amount;
  156. }
  157. }
  158. if ($clearAmount > 0) {
  159. $shop = ShopClass::getById($shopId, true);
  160. $needClearIdMap[] = ['orderId' => $orderId, 'clearAmount' => $clearAmount];
  161. $setParams = ['staffId' => $staffId, 'staffName' => $staffName];
  162. $set = SettleClass::addSettle($needClearIdMap, $shop, $custom, $hd, $setParams);
  163. $ret = SettleClass::clearSettle($set);
  164. $settleAmount = $ret['totalClearAmount'] ?? 0;
  165. $settleNo = $set->orderSn ?? '';
  166. $settleId = $set->id;
  167. }
  168. $lastBalance = bcsub($custom->balance, $amount, 2);
  169. $custom->balance = $lastBalance;
  170. if ($custom->balance < 0) {
  171. $custom->isDebt = 1;
  172. }
  173. $custom->save();
  174. $hdId = $custom->hdId ?? 0;
  175. $hd = HdClass::getLockById($hdId);
  176. if (empty($hd)) {
  177. util::fail('花店信息缺失,编号6331');
  178. }
  179. $remain = bcsub($hd->balance, $amount, 2);
  180. $hd->balance = $remain;
  181. if ($hd->balance < 0) {
  182. $hd->debt = 2;
  183. }
  184. $hd->save();
  185. $relateId = $order->id ?? 0;
  186. $customId = $custom->id ?? 0;
  187. $orderSn = $order->orderSn ?? '';
  188. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  189. $event = '购买花材,单号:' . $orderSn;
  190. $customName = $custom->name ?? '';
  191. $onlinePay = 1;
  192. $side = 0;
  193. $payWay = 0;
  194. $change = [
  195. 'customId' => $customId,
  196. 'customName' => $customName,
  197. 'relateId' => $relateId,
  198. 'onlinePay' => $onlinePay,
  199. 'capitalType' => $capitalType,
  200. 'amount' => $amount,
  201. 'balance' => $lastBalance,
  202. 'io' => 0,
  203. 'side' => $side,
  204. 'payWay' => $payWay,
  205. 'event' => $event,
  206. 'staffId' => $staffId,
  207. 'staffName' => $staffName,
  208. 'shopId' => $order->id,
  209. 'mainId' => $order->mainId,
  210. 'settleId' => $settleId,
  211. 'settleNo' => $settleNo,
  212. 'settleAmount' => $settleAmount,
  213. 'remark' => '',
  214. ];
  215. BalanceChangeClass::add($change, true);
  216. }
  217. //散客申请退款,零售门店欠款金额减少
  218. public static function refundDebtAmountReduce($custom, $amount, $refund)
  219. {
  220. $debtAmount = $custom->debtAmount ?? 0.00;
  221. $remain = bcsub($debtAmount, $amount, 2);
  222. if ($remain < 0) {
  223. noticeUtil::push("零售客户欠款总金额:{$custom->debtAmount} 本次退款金额:{$amount} 客户ID:{$custom->id} 超出了,编号66379", '15280215347');
  224. util::fail('操作失败,欠款金额会出现负数');
  225. }
  226. $custom->isDebt = $remain <= 0 ? 0 : 1;
  227. $custom->debtAmount = $remain;
  228. $custom->save();
  229. $relateId = $refund->id ?? 0;
  230. $customId = $custom->id ?? 0;
  231. $capitalType = dict::getDict('capitalType', 'hdRefund', 'id');
  232. $refundSn = $refund->refundSn ?? '';
  233. $change = [
  234. 'relateId' => $relateId,
  235. 'customId' => $customId,
  236. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  237. 'capitalType' => $capitalType,
  238. 'amount' => $amount,
  239. 'balance' => $remain,
  240. 'io' => 0,
  241. 'payWay' => 0,
  242. 'event' => "客户申请退款,退款单号:{$refundSn}",
  243. 'sjId' => $refund->sjId ?? 0,
  244. 'shopId' => $refund->shopId ?? 0,
  245. 'mainId' => $refund->mainId ?? 0,
  246. ];
  247. CustomDebtRecordClass::addChange($change);
  248. $mainId = $refund->mainId ?? 0;
  249. $main = MainClass::getLockById($mainId);
  250. if (empty($main)) {
  251. util::fail('没有main信息39');
  252. }
  253. $debt = $main->debt ?? 0;
  254. $totalRemain = bcsub($debt, $amount, 2);
  255. $main->debt = $totalRemain;
  256. $main->save();
  257. $event = $custom->name . "申请退款,退款单号:{$refundSn}";
  258. $change['balance'] = $totalRemain;
  259. $change['event'] = $event;
  260. TotalDebtChangeClass::addChange($change);
  261. }
  262. //客户结账欠款金额减少 ssh 20250627
  263. public static function clearDebtAmountReduce($custom, $amount, $clear)
  264. {
  265. $balance = $custom->balance ?? 0.00;
  266. $remainBalance = bcsub($balance, $amount, 2);
  267. if ($remainBalance < 0) {
  268. noticeUtil::push("零售客户欠款总金额:{$balance} 本次结账金额:{$amount} 客户ID:{$custom->id} 超出了,编号788965", '15280215347');
  269. util::fail('操作失败,结账后欠款金额会出现负数');
  270. }
  271. $custom->balance = $remainBalance;
  272. $custom->save();
  273. $hdId = $custom->hdId ?? 0;
  274. $hd = HdClass::getLockById($hdId);
  275. if (empty($hd)) {
  276. util::fail('店铺信息异常');
  277. }
  278. $hdName = $hd->name ?? '';
  279. $hdBalance = $hd->balance ?? 0;
  280. $hdRemainBalance = bcsub($hdBalance, $amount, 2);
  281. $hd->balance = $hdRemainBalance;
  282. $hd->save();
  283. if (floatval($hdRemainBalance) != floatval($remainBalance)) {
  284. util::fail('余额有问题');
  285. }
  286. $relateId = $clear->id ?? 0;
  287. $customId = $custom->id ?? 0;
  288. $capitalType = dict::getDict('capitalType', 'hdXsClear', 'id');
  289. $orderSn = $clear->orderSn ?? '';
  290. $customName = $custom->name ?? '';
  291. $event = "结账,单号:{$orderSn}";
  292. $staffId = $clear->hdShopAdminId ?? 0;
  293. $staffName = $clear->hdShopAdminName ?? '';
  294. $shopId = $clear->hdShopId ?? 0;
  295. $mainId = $clear->mainId ?? 0;
  296. $remark = '';
  297. $payWay = $clear->payWay ?? 0;
  298. $change = [
  299. 'customId' => $customId,
  300. 'customName' => $customName,
  301. 'hdId' => $hdId,
  302. 'hdName' => $hdName,
  303. 'relateId' => $relateId,
  304. 'onlinePay' => 1,
  305. 'capitalType' => $capitalType,
  306. 'amount' => $amount,
  307. 'balance' => $hdRemainBalance,
  308. 'io' => 0,
  309. 'side' => 0,
  310. 'payWay' => $payWay,
  311. 'event' => $event,
  312. 'staffId' => $staffId,
  313. 'staffName' => $staffName,
  314. 'shopId' => $shopId,
  315. 'mainId' => $mainId,
  316. 'remark' => $remark,
  317. ];
  318. BalanceChangeClass::add($change, true);
  319. }
  320. public static function getCustomCacheKey($sjId, $shopId, $userId)
  321. {
  322. return 'hd_custom_key_' . $sjId . '_' . $shopId . '_' . $userId;
  323. }
  324. //添加客户 ssh 2021.2.28
  325. public static function addCustom($data)
  326. {
  327. return self::add($data, true);
  328. }
  329. //创建或获取客户信息 ssh 2021.3.19
  330. public static function buildRelation($shop, $user)
  331. {
  332. if (empty($user)) {
  333. return false;
  334. }
  335. if (empty($shop)) {
  336. return false;
  337. }
  338. if ($shop->ptStyle == 2) {
  339. util::fail('有问题,批发店状态');
  340. }
  341. $salt = stringUtil::charsShuffleLowerCase(8);
  342. $isNewCustom = 0;
  343. $userId = $user->id;
  344. $name = $user->name ?? '';
  345. $mobile = $user->mobile ?? '';
  346. $shopId = $shop->id ?? 0;
  347. $hdAvatar = $shop->avatar ?? '';
  348. $sjId = $shop->sjId ?? 0;
  349. $sjName = $shop->merchantName ?? '';
  350. $shopName = $shop->shopName ?? '';
  351. $hdMobile = $shop->mobile ?? '';
  352. $hdName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  353. $hdPy = stringUtil::py($hdName);
  354. $custom = self::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
  355. if (empty($custom)) {
  356. $py = stringUtil::py($name);
  357. $visitTime = date("Y-m-d H:i:s");
  358. $data = [
  359. 'userId' => $userId,
  360. 'sjId' => $sjId,
  361. 'shopId' => $shopId,
  362. 'name' => $name,
  363. 'originName' => $name,
  364. 'py' => $py,
  365. 'mobile' => $mobile,
  366. 'visitTime' => $visitTime,
  367. 'salt' => $salt,
  368. ];
  369. $custom = self::addCustom($data);
  370. $isNewCustom = 1;
  371. }
  372. $customId = $custom->id ?? 0;
  373. $hd = HdClass::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
  374. if (empty($hd)) {
  375. $hdData = [
  376. 'shopId' => $shopId,
  377. 'userId' => $userId,
  378. 'name' => $hdName,
  379. 'sjId' => $sjId,
  380. 'customId' => $customId,
  381. 'avatar' => $hdAvatar,
  382. 'py' => $hdPy,
  383. 'mobile' => $hdMobile,
  384. 'province' => $shop->province ?? '',
  385. 'city' => $shop->city ?? '',
  386. 'dist' => $shop->dist ?? '',
  387. 'address' => $shop->address ?? '',
  388. 'floor' => $shop->floor ?? '',
  389. 'fullAddress' => $shop->fullAddress ?? '',
  390. 'showAddress' => $shop->showAddress ?? '',
  391. 'lat' => $shop->lat ?? '',
  392. 'long' => $shop->long ?? '',
  393. 'salt' => $salt,
  394. ];
  395. $hd = HdClass::addData($hdData);
  396. $isNewCustom = 1;
  397. }
  398. $hdId = $hd->id ?? 0;
  399. $custom->hdId = $hdId;
  400. $custom->save();
  401. if ($isNewCustom == 1) {
  402. WxMessageClass::newHdCustomInform($shop, $custom);
  403. }
  404. return ['custom' => $custom, 'hd' => $hd];
  405. }
  406. //获取客户信息
  407. public static function getCustom($data)
  408. {
  409. $custom = self::getByCondition($data);
  410. if (empty($custom)) {
  411. $custom = self::add($data);
  412. }
  413. return $custom;
  414. }
  415. //获取客户信息 ssh 2021.2.1
  416. public static function getCustomInfo($id)
  417. {
  418. $custom = self::getById($id);
  419. if (empty($custom)) {
  420. return $custom;
  421. }
  422. $list = self::groupBaseInfo([$custom]);
  423. return current($list);
  424. }
  425. //获取客户信息 ssh 2021.3.19
  426. public static function getCustomByIds($ids)
  427. {
  428. $list = self::getByIds($ids);
  429. $list = self::groupBaseInfo($list);
  430. return $list;
  431. }
  432. public static function valid($info, $shopId)
  433. {
  434. if (isset($info['shopId']) && $info['shopId'] == $shopId) {
  435. return true;
  436. }
  437. util::fail('客户信息无效');
  438. }
  439. //整合基本信息 ssh 2021.1.31
  440. public static function groupBaseInfo($list)
  441. {
  442. if (empty($list)) {
  443. return $list;
  444. }
  445. foreach ($list as $key => $val) {
  446. $list[$key]['avatar'] = imgUtil::groupImg($val['avatar']);
  447. }
  448. return $list;
  449. }
  450. //更新客户信息 ssh 2021.3.23
  451. public static function updateCustomInfo($custom, $data)
  452. {
  453. $userId = $custom['userId'];
  454. $userData = [];
  455. if (!empty($data['mobile'])) {
  456. $userData['mobile'] = $data['mobile'];
  457. }
  458. if (!empty($userData)) {
  459. UserClass::updateById($userId, $userData);
  460. }
  461. $id = $custom['id'];
  462. self::updateById($id, $data);
  463. }
  464. }