CustomClass.php 42 KB

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