CustomClass.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. namespace bizHd\custom\classes;
  3. use biz\wx\classes\WxMessageClass;
  4. use bizGhs\shop\classes\TotalDebtChangeClass;
  5. use bizHd\member\classes\MemberChangeClass;
  6. use bizHd\member\classes\MemberWealClass;
  7. use bizHd\shop\classes\MainClass;
  8. use bizMall\user\classes\UserClass;
  9. use common\components\dict;
  10. use common\components\imgUtil;
  11. use common\components\noticeUtil;
  12. use common\components\stringUtil;
  13. use common\components\util;
  14. use Yii;
  15. use bizHd\base\classes\BaseClass;
  16. class CustomClass extends BaseClass
  17. {
  18. public static $baseFile = '\bizHd\custom\models\Custom';
  19. //用余额支付,客户余额减少 ssh 20250410
  20. public static function payByChangeBalance($order)
  21. {
  22. $hdId = $order->hdId ?? 0;
  23. $hd = HdClass::getLockById($hdId);
  24. $customId = $order->customId ?? 0;
  25. $custom = self::getLockById($customId);
  26. }
  27. //会员等级变化,包括手动修改、自动降会员和充值升会员等 ssh 20250324
  28. public static function levelChange($oldLevel, $newLevel, $shop, $custom, $hd, $params)
  29. {
  30. $staffId = $params['staffId'] ?? 0;
  31. $staffName = $params['staffName'] ?? '';
  32. $style = $params['style'] ?? 0;
  33. $target = $params['target'] ?? [];
  34. $onlinePay = $params['onlinePay'] ?? 1;
  35. $rechargeAmount = $params['rechargeAmount'] ?? 0;
  36. $targetId = $target->id ?? 0;
  37. $mainId = $shop->mainId;
  38. $shopId = $shop->id;
  39. $weal = MemberWealClass::getAllByCondition(['mainId' => $mainId], null, '*', 'level');
  40. $current = $weal[$newLevel] ?? [];
  41. $before = $weal[$oldLevel] ?? [];
  42. $beforeName = $before['name'] ?? '';
  43. $discount = $current['discount'] ?? 1;
  44. $memberName = $current['name'] ?? '';
  45. $custom->member = $newLevel;
  46. $custom->discount = $discount;
  47. $custom->memberName = $memberName;
  48. $custom->save();
  49. $hd->member = $newLevel;
  50. $hd->discount = $discount;
  51. $hd->memberName = $memberName;
  52. $hd->save();
  53. $event = '手动修改';
  54. if ($style == 0) {
  55. if ($onlinePay == 2) {
  56. $event = '线上充值' . $rechargeAmount . '元';
  57. } else {
  58. $event = '线下充值' . $rechargeAmount . '元';
  59. }
  60. }
  61. $change = [
  62. 'customId' => $custom->id,
  63. 'customName' => $custom->name,
  64. 'hdId' => $hd->id,
  65. 'hdName' => $hd->name,
  66. 'mainId' => $mainId,
  67. 'shopId' => $shopId,
  68. 'targetId' => $targetId,
  69. 'rechargeAmount' => $rechargeAmount,
  70. 'days' => 0,
  71. 'daysBefore' => '0000-00-00 00:00:00',
  72. 'daysAfter' => '0000-00-00 00:00:00',
  73. 'beforeLevel' => $oldLevel,
  74. 'afterLevel' => $newLevel,
  75. 'beforeName' => $beforeName,
  76. 'afterName' => $memberName,
  77. 'style' => $style,
  78. 'event' => $event,
  79. 'staffId' => $staffId,
  80. 'staffName' => $staffName,
  81. ];
  82. MemberChangeClass::add($change);
  83. }
  84. //客户采购欠款金额增加 ssh 20220908
  85. public static function cgDebtAmountAdd($custom, $order)
  86. {
  87. $amount = $order->remainDebtPrice ?? 0;
  88. $custom->isDebt = 1;
  89. $lastDebt = bcadd($custom->debtAmount, $amount, 2);
  90. $custom->debtAmount = $lastDebt;
  91. $custom->save();
  92. $relateId = $order->id ?? 0;
  93. $customId = $custom->id ?? 0;
  94. $orderSn = $order->orderSn ?? '';
  95. $capitalType = dict::getDict('capitalType', 'xhOrder', 'id');
  96. $event = '购买花材,订单号:' . $orderSn;
  97. $change = [
  98. 'relateId' => $relateId,
  99. 'customId' => $customId,
  100. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  101. 'capitalType' => $capitalType,
  102. 'amount' => $amount,
  103. 'balance' => $lastDebt,
  104. 'io' => 1,
  105. 'payWay' => $order->payWay,
  106. 'event' => $event,
  107. 'sjId' => $order->sjId,
  108. 'shopId' => $order->id,
  109. 'mainId' => $order->mainId,
  110. ];
  111. CustomDebtRecordClass::addChange($change);
  112. $mainId = $order->mainId ?? 0;
  113. $main = MainClass::getLockById($mainId);
  114. if (empty($main)) {
  115. util::fail('没有main信息38');
  116. }
  117. $debt = $main->debt ?? 0;
  118. $remain = bcadd($debt, $amount, 2);
  119. $main->debt = $remain;
  120. $main->save();
  121. $event = $custom->name . ' 购买花材,订单号:' . $orderSn;
  122. $change['balance'] = $remain;
  123. $change['event'] = $event;
  124. TotalDebtChangeClass::addChange($change);
  125. }
  126. //散客申请退款,零售门店欠款金额减少
  127. public static function refundDebtAmountReduce($custom, $amount, $refund)
  128. {
  129. $debtAmount = $custom->debtAmount ?? 0.00;
  130. $remain = bcsub($debtAmount, $amount, 2);
  131. if ($remain < 0) {
  132. noticeUtil::push("零售客户欠款总金额:{$custom->debtAmount} 本次退款金额:{$amount} 客户ID:{$custom->id} 超出了", '15280215347');
  133. util::fail('操作失败,欠款金额会出现负数');
  134. }
  135. $custom->isDebt = $remain <= 0 ? 0 : 1;
  136. $custom->debtAmount = $remain;
  137. $custom->save();
  138. $relateId = $refund->id ?? 0;
  139. $customId = $custom->id ?? 0;
  140. $capitalType = dict::getDict('capitalType', 'hdRefund', 'id');
  141. $refundSn = $refund->refundSn ?? '';
  142. $change = [
  143. 'relateId' => $relateId,
  144. 'customId' => $customId,
  145. 'ptStyle' => dict::getDict('ptStyle', 'hd'),
  146. 'capitalType' => $capitalType,
  147. 'amount' => $amount,
  148. 'balance' => $remain,
  149. 'io' => 0,
  150. 'payWay' => 0,
  151. 'event' => "客户申请退款,退款单号:{$refundSn}",
  152. 'sjId' => $refund->sjId ?? 0,
  153. 'shopId' => $refund->shopId ?? 0,
  154. 'mainId' => $refund->mainId ?? 0,
  155. ];
  156. CustomDebtRecordClass::addChange($change);
  157. $mainId = $refund->mainId ?? 0;
  158. $main = MainClass::getLockById($mainId);
  159. if (empty($main)) {
  160. util::fail('没有main信息39');
  161. }
  162. $debt = $main->debt ?? 0;
  163. $totalRemain = bcsub($debt, $amount, 2);
  164. $main->debt = $totalRemain;
  165. $main->save();
  166. $event = $custom->name . "申请退款,退款单号:{$refundSn}";
  167. $change['balance'] = $totalRemain;
  168. $change['event'] = $event;
  169. TotalDebtChangeClass::addChange($change);
  170. }
  171. //客户结账欠款金额减少 ssh 20220908
  172. public static function clearDebtAmountReduce($custom, $amount, $clear)
  173. {
  174. $debtAmount = $custom->debtAmount ?? 0.00;
  175. $remain = bcsub($debtAmount, $amount, 2);
  176. if ($remain < 0) {
  177. noticeUtil::push("零售客户欠款总金额:{$custom->debtAmount} 本次结账金额:{$amount} 客户ID:{$custom->id} 超出了", '15280215347');
  178. util::fail('操作失败,结账后欠款金额会出现负数');
  179. }
  180. $custom->isDebt = $remain <= 0 ? 0 : 1;
  181. $custom->debtAmount = $remain;
  182. $custom->save();
  183. $relateId = $clear->id ?? 0;
  184. $customId = $custom->id ?? 0;
  185. $capitalType = dict::getDict('capitalType', 'hdXsClear', 'id');
  186. $orderSn = $clear->orderSn ?? '';
  187. $change = [
  188. 'relateId' => $relateId,
  189. 'customId' => $customId,
  190. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  191. 'capitalType' => $capitalType,
  192. 'amount' => $amount,
  193. 'balance' => $remain,
  194. 'io' => 0,
  195. 'payWay' => 0,
  196. 'event' => "结账,单号:{$orderSn}",
  197. 'sjId' => $clear->sjId,
  198. 'shopId' => $clear->id,
  199. 'mainId' => $clear->mainId,
  200. ];
  201. CustomDebtRecordClass::addChange($change);
  202. $mainId = $clear->mainId ?? 0;
  203. $main = MainClass::getLockById($mainId);
  204. if (empty($main)) {
  205. util::fail('没有main信息40');
  206. }
  207. $debt = $main->debt ?? 0;
  208. $totalRemain = bcsub($debt, $amount, 2);
  209. $main->debt = $totalRemain;
  210. $main->save();
  211. $event = $custom->name . " 结账,单号:{$orderSn}";
  212. $change['balance'] = $totalRemain;
  213. $change['event'] = $event;
  214. TotalDebtChangeClass::addChange($change);
  215. }
  216. public static function getCustomCacheKey($sjId, $shopId, $userId)
  217. {
  218. return 'hd_custom_key_' . $sjId . '_' . $shopId . '_' . $userId;
  219. }
  220. //添加客户 ssh 2021.2.28
  221. public static function addCustom($data)
  222. {
  223. return self::add($data, true);
  224. }
  225. //创建或获取客户信息 ssh 2021.3.19
  226. public static function buildRelation($shop, $user)
  227. {
  228. if (empty($user)) {
  229. return false;
  230. }
  231. if (empty($shop)) {
  232. return false;
  233. }
  234. $salt = stringUtil::charsShuffleLowerCase(8);
  235. $isNewCustom = 0;
  236. $userId = $user->id;
  237. $name = $user->name ?? '';
  238. $mobile = $user->mobile ?? '';
  239. $shopId = $shop->id ?? 0;
  240. $hdAvatar = $shop->avatar ?? '';
  241. $sjId = $shop->sjId ?? 0;
  242. $sjName = $shop->merchantName ?? '';
  243. $shopName = $shop->shopName ?? '';
  244. $hdMobile = $shop->mobile ?? '';
  245. $hdName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  246. $hdPy = stringUtil::py($hdName);
  247. $custom = self::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
  248. if (empty($custom)) {
  249. $py = stringUtil::py($name);
  250. $visitTime = date("Y-m-d H:i:s");
  251. $data = [
  252. 'userId' => $userId,
  253. 'sjId' => $sjId,
  254. 'shopId' => $shopId,
  255. 'name' => $name,
  256. 'originName' => $name,
  257. 'py' => $py,
  258. 'mobile' => $mobile,
  259. 'visitTime' => $visitTime,
  260. 'salt' => $salt,
  261. ];
  262. $custom = self::addCustom($data);
  263. $isNewCustom = 1;
  264. }
  265. $customId = $custom->id ?? 0;
  266. $hd = HdClass::getByCondition(['shopId' => $shopId, 'userId' => $userId], true);
  267. if (empty($hd)) {
  268. $hdData = [
  269. 'shopId' => $shopId,
  270. 'userId' => $userId,
  271. 'name' => $hdName,
  272. 'sjId' => $sjId,
  273. 'customId' => $customId,
  274. 'avatar' => $hdAvatar,
  275. 'py' => $hdPy,
  276. 'mobile' => $hdMobile,
  277. 'province' => $shop->province ?? '',
  278. 'city' => $shop->city ?? '',
  279. 'dist' => $shop->dist ?? '',
  280. 'address' => $shop->address ?? '',
  281. 'floor' => $shop->floor ?? '',
  282. 'fullAddress' => $shop->fullAddress ?? '',
  283. 'showAddress' => $shop->showAddress ?? '',
  284. 'lat' => $shop->lat ?? '',
  285. 'long' => $shop->long ?? '',
  286. 'salt' => $salt,
  287. ];
  288. $hd = HdClass::addData($hdData);
  289. $isNewCustom = 1;
  290. }
  291. $hdId = $hd->id ?? 0;
  292. $custom->hdId = $hdId;
  293. $custom->save();
  294. if ($isNewCustom == 1) {
  295. WxMessageClass::newHdCustomInform($shop, $custom);
  296. }
  297. return ['custom' => $custom, 'hd' => $hd];
  298. }
  299. //获取客户信息
  300. public static function getCustom($data)
  301. {
  302. $custom = self::getByCondition($data);
  303. if (empty($custom)) {
  304. $custom = self::add($data);
  305. }
  306. return $custom;
  307. }
  308. //获取客户信息 ssh 2021.2.1
  309. public static function getCustomInfo($id)
  310. {
  311. $custom = self::getById($id);
  312. if (empty($custom)) {
  313. return $custom;
  314. }
  315. $list = self::groupBaseInfo([$custom]);
  316. return current($list);
  317. }
  318. //获取客户信息 ssh 2021.3.19
  319. public static function getCustomByIds($ids)
  320. {
  321. $list = self::getByIds($ids);
  322. $list = self::groupBaseInfo($list);
  323. return $list;
  324. }
  325. public static function valid($info, $shopId)
  326. {
  327. if (isset($info['shopId']) && $info['shopId'] == $shopId) {
  328. return true;
  329. }
  330. util::fail('客户信息无效');
  331. }
  332. //整合基本信息 ssh 2021.1.31
  333. public static function groupBaseInfo($list)
  334. {
  335. if (empty($list)) {
  336. return $list;
  337. }
  338. foreach ($list as $key => $val) {
  339. $list[$key]['avatar'] = imgUtil::groupImg($val['avatar']);
  340. }
  341. return $list;
  342. }
  343. //更新客户信息 ssh 2021.3.23
  344. public static function updateCustomInfo($custom, $data)
  345. {
  346. $userId = $custom['userId'];
  347. $userData = [];
  348. if (isset($data['mobile']) && !empty($data['mobile'])) {
  349. $userData['mobile'] = $data['mobile'];
  350. }
  351. if (!empty($userData)) {
  352. UserClass::updateById($userId, $userData);
  353. }
  354. $id = $custom['id'];
  355. self::updateById($id, $data);
  356. }
  357. }