CustomClass.php 38 KB

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