CustomClass.php 34 KB

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