CustomClass.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. namespace bizGhs\custom\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\sj\classes\MerchantClass;
  5. use bizGhs\custom\models\Custom;
  6. use bizGhs\ghs\classes\GhsBalanceChangeClass;
  7. use bizGhs\shop\classes\MainClass;
  8. use bizGhs\shop\classes\TotalDebtChangeClass;
  9. use bizGhs\ghs\classes\GhsBalanceClass;
  10. use bizGhs\ghs\classes\GhsRechargeClass;
  11. use bizGhs\shop\classes\ShopClass;
  12. use common\components\business;
  13. use common\components\dict;
  14. use common\components\imgUtil;
  15. use common\components\noticeUtil;
  16. use common\components\util;
  17. use common\components\orderSn;
  18. use bizHd\base\classes\BaseClass;
  19. class CustomClass extends BaseClass
  20. {
  21. public static $baseFile = '\bizGhs\custom\models\Custom';
  22. const IS_DEBT_NO = 0;
  23. const IS_DEBT_YES = 1;
  24. const NOT_BLACK = 1;
  25. const IS_BLACK = 2;
  26. const LEVEL_SK = 0; //散客
  27. const LEVEL_PT = 1; //花店
  28. const LEVEL_HJ = 2; //同行
  29. const LEVEL_ZS = 3; //金店
  30. public static $levelMap = [
  31. self::LEVEL_SK => '散客',
  32. self::LEVEL_PT => '花店',
  33. self::LEVEL_HJ => '同行',
  34. self::LEVEL_ZS => '金店',
  35. ];
  36. public static $nickNameMap = [
  37. self::LEVEL_SK => '零售',
  38. self::LEVEL_PT => '批发',
  39. self::LEVEL_HJ => '同行',
  40. self::LEVEL_ZS => '金店',
  41. ];
  42. public static $levelPriceKeyMap = [
  43. self::LEVEL_SK => 'skPrice',
  44. self::LEVEL_PT => 'price',
  45. self::LEVEL_HJ => 'hjPrice',
  46. self::LEVEL_ZS => 'zsPrice',
  47. ];
  48. public static $levelAddPriceKeyMap = [
  49. self::LEVEL_SK => 'skMore',
  50. self::LEVEL_PT => 'addPrice',
  51. self::LEVEL_HJ => 'hjAddPrice',
  52. self::LEVEL_ZS => 'zsAddPrice',
  53. ];
  54. //消费余额 ssh 20240311
  55. public static function clearConsumeBalance($amount, $custom, $ghs, $shop, $staff, $clear)
  56. {
  57. $customId = $custom->id ?? 0;
  58. $ghsId = $ghs->id ?? 0;
  59. $relateId = $clear->id ?? 0;
  60. $staffId = $staff->id ?? 0;
  61. $staffName = $staff->name ?? '';
  62. $clearSn = $clear->orderSn;
  63. $mainId = $shop->mainId ?? 0;
  64. $shopId = $shop->id ?? 0;
  65. $sjId = $shop->sjId ?? 0;
  66. $newGhsBalance = bcsub($ghs->balance, $amount, 2);
  67. $ghs->balance = $newGhsBalance;
  68. $ghs->save();
  69. $newCustomBalance = bcsub($custom->balance, $amount, 2);
  70. $custom->balance = $newCustomBalance;
  71. $custom->save();
  72. if ($newGhsBalance < 0) {
  73. util::fail('余额有问题哦..');
  74. }
  75. if (floatval($newGhsBalance) != floatval($newCustomBalance)) {
  76. util::fail('余额有问题...');
  77. }
  78. $capitalType = dict::getDict('capitalType', 'ghsHelpCustomUseBalanceClear', 'id');
  79. $fromType = dict::getDict('fromType', 'shop');
  80. $cbData = [
  81. 'customId' => $customId,
  82. 'relateId' => $relateId,
  83. 'onlinePay' => 1,
  84. 'ptStyle' => 2,
  85. 'capitalType' => $capitalType,
  86. 'amount' => $amount,
  87. 'balance' => $newCustomBalance,
  88. 'staffId' => $staffId,
  89. 'staffName' => $staffName,
  90. 'io' => 0,
  91. 'side' => 0,
  92. 'payWay' => 0,
  93. 'fromType' => $fromType,
  94. 'event' => '结账 ' . $clearSn,
  95. 'mainId' => $mainId,
  96. 'shopId' => $shopId,
  97. 'sjId' => $sjId,
  98. 'remark' => '',
  99. ];
  100. $cbc = CustomBalanceChangeClass::add($cbData, true);
  101. $customShopId = $custom->shopId ?? 0;
  102. $customShop = ShopClass::getById($customShopId, true);
  103. if (empty($customShop)) {
  104. util::fail('没有找到客户门店呢..');
  105. }
  106. $customShopSjId = $customShop->sjId ?? 0;
  107. $customShopMainId = $customShop->mainId ?? 0;
  108. $capitalType = dict::getDict('capitalType', 'customAskGhsUseBalanceClear', 'id');
  109. $gbData = [
  110. 'ghsId' => $ghsId,
  111. 'relateId' => $relateId,
  112. 'ptStyle' => 2,
  113. 'capitalType' => $capitalType,
  114. 'amount' => $amount,
  115. 'balance' => $newGhsBalance,
  116. 'io' => 0,
  117. 'side' => 0,
  118. 'onlinePay' => 1,
  119. 'payWay' => 0,
  120. 'fromType' => $fromType,
  121. 'event' => '结账 ' . $clearSn,
  122. 'sjId' => $customShopSjId,
  123. 'mainId' => $customShopMainId,
  124. 'shopId' => $customShopId,
  125. 'remark' => '',
  126. ];
  127. $gbc = GhsBalanceChangeClass::add($gbData, true);
  128. $clear->customBalanceChangeId = $cbc->id ?? 0;
  129. $clear->ghsBalanceChangeId = $gbc->id ?? 0;
  130. $clear->save();
  131. }
  132. //充值金额,有多个地方要同步修改,请搜索关键词custom_ghs_recharge ssh 20240310
  133. public static function rechargeBalance($custom, $amount, $shop, $staff, $payWay, $params = [])
  134. {
  135. $ghsId = $custom->ghsId ?? 0;
  136. $ghsInfo = GhsClass::getLockById($ghsId);
  137. if (empty($ghsInfo)) {
  138. util::fail('没有找到供货商');
  139. }
  140. $ghsShopId = $ghsInfo->shopId ?? 0;
  141. $ghsName = $ghsInfo->name ?? '';
  142. $customId = $custom->id ?? 0;
  143. $customShopId = $custom->shopId ?? 0;
  144. $customName = $custom->name ?? '';
  145. $customShop = ShopClass::getById($customShopId, true);
  146. if (empty($customShop)) {
  147. util::fail('没有找到客户门店');
  148. }
  149. $payTime = date("Y-m-d H:i:s");
  150. $customShopSjId = $customShop->sjId ?? 0;
  151. $customShopMainId = $customShop->mainId ?? 0;
  152. $payMap = dict::getDict('payWayName');
  153. $payName = $payMap[$payWay] ?? '';
  154. $rechargeEvent = $payName . '充值';
  155. $custom->balance = bcadd($custom->balance, $amount, 2);
  156. $custom->save();
  157. $orderSn = orderSn::getGhsCustomRechargeSn();
  158. $shopId = $shop->id ?? 0;
  159. $sjId = $shop->sjId ?? 0;
  160. $mainId = $shop->mainId ?? 0;
  161. $staffId = $staff->id ?? 0;
  162. $staffName = $staff->name ?? '';
  163. $remark = $params['remark'] ?? '';
  164. $cData = [
  165. 'orderSn' => $orderSn,
  166. 'onlinePay' => 1,
  167. 'payWay' => $payWay,
  168. 'shopId' => $shopId,
  169. 'mainId' => $mainId,
  170. 'amount' => $amount,
  171. 'balance' => $custom->balance,
  172. 'side' => 0,
  173. 'staffId' => $staffId,
  174. 'staffName' => $staffName,
  175. 'payStatus' => 1,
  176. 'status' => 1,
  177. 'remark' => '',
  178. 'ghsId' => $ghsId,
  179. 'ghsShopId' => $ghsShopId,
  180. 'ghsName' => $ghsName,
  181. 'customId' => $customId,
  182. 'customShopId' => $customShopId,
  183. 'customName' => $customName,
  184. 'remark' => $remark,
  185. 'payTime' => $payTime,
  186. ];
  187. $cRecharge = CustomRechargeClass::add($cData, true);
  188. $cRechargeId = $cRecharge->id ?? 0;
  189. $capitalType = dict::getDict('capitalType', 'ghsHelpCustomRecharge', 'id');
  190. $fromType = dict::getDict('fromType', 'shop');
  191. $cbData = [
  192. 'customId' => $customId,
  193. 'relateId' => $cRechargeId,
  194. 'onlinePay' => 1,
  195. 'ptStyle' => 2,
  196. 'capitalType' => $capitalType,
  197. 'amount' => $amount,
  198. 'balance' => $custom->balance,
  199. 'staffId' => $staffId,
  200. 'staffName' => $staffName,
  201. 'io' => 1,
  202. 'side' => 0,
  203. 'payWay' => $payWay,
  204. 'fromType' => $fromType,
  205. 'event' => $rechargeEvent,
  206. 'mainId' => $mainId,
  207. 'shopId' => $shopId,
  208. 'sjId' => $sjId,
  209. 'remark' => $remark,
  210. ];
  211. CustomBalanceChangeClass::add($cbData, true);
  212. $ghsInfo->balance = bcadd($ghsInfo->balance, $amount, 2);
  213. $ghsInfo->save();
  214. $orderSn = orderSn::getGhsRechargeSn();
  215. $customMainId = $custom->mainId ?? 0;
  216. $gData = [
  217. 'orderSn' => $orderSn,
  218. 'onlinePay' => 1,
  219. 'payWay' => $payWay,
  220. 'shopId' => $customShopId,
  221. 'mainId' => $customMainId,
  222. 'amount' => $amount,
  223. 'balance' => $ghsInfo->balance,
  224. 'side' => 0,
  225. 'staffId' => 0,
  226. 'staffName' => '',
  227. 'payStatus' => 1,
  228. 'status' => 1,
  229. 'remark' => '',
  230. 'ghsId' => $ghsId,
  231. 'ghsShopId' => $ghsShopId,
  232. 'ghsName' => $ghsName,
  233. 'customId' => $customId,
  234. 'customShopId' => $customShopId,
  235. 'customName' => $customName,
  236. 'remark' => $remark,
  237. 'payTime' => $payTime,
  238. ];
  239. $gRecharge = GhsRechargeClass::add($gData, true);
  240. $gRechargeId = $gRecharge->id ?? 0;
  241. $capitalType = dict::getDict('capitalType', 'customAskGhsRecharge', 'id');
  242. $gbData = [
  243. 'ghsId' => $ghsId,
  244. 'relateId' => $gRechargeId,
  245. 'ptStyle' => 2,
  246. 'capitalType' => $capitalType,
  247. 'amount' => $amount,
  248. 'balance' => $ghsInfo->balance,
  249. 'io' => 1,
  250. 'side' => 0,
  251. 'onlinePay' => 1,
  252. 'payWay' => $payWay,
  253. 'fromType' => $fromType,
  254. 'event' => $rechargeEvent,
  255. 'sjId' => $customShopSjId,
  256. 'mainId' => $customShopMainId,
  257. 'shopId' => $customShopId,
  258. 'remark' => $remark,
  259. ];
  260. GhsBalanceChangeClass::add($gbData, true);
  261. return ['custom' => $custom, 'ghs' => $ghsInfo, 'customRecharge' => $cRecharge, 'ghsRecharge' => $gRecharge];
  262. }
  263. public static function createSeatSn($mainId)
  264. {
  265. $max = Custom::find(['mainId' => $mainId])->max('seatSn');
  266. $new = bcadd($max, 1);
  267. return $new;
  268. }
  269. //建立客户和供货商关系
  270. public static function build($ghsShopId, $hdShopId, $changeName = '')
  271. {
  272. return GhsClass::build($ghsShopId, $hdShopId, 2, $changeName);
  273. }
  274. //添加客户 ssh 2021.2.24
  275. public static function addCustom($data)
  276. {
  277. return self::add($data);
  278. }
  279. //获取客户信息 ssh 2021.2.4
  280. public static function getCustom($id)
  281. {
  282. $info = self::getById($id);
  283. if (empty($info)) {
  284. return $info;
  285. }
  286. $respond = self::groupBaseInfo([$info]);
  287. return current($respond);
  288. }
  289. public static function getCustomByIds($ids)
  290. {
  291. $list = self::getByIds($ids, null, 'id');
  292. return self::groupBaseInfo($list);
  293. }
  294. //整合头像等信息 ssh 2021.1.8
  295. public static function groupBaseInfo($list)
  296. {
  297. if (empty($list)) {
  298. return $list;
  299. }
  300. //获取客户信息
  301. $customSjIdList = array_unique(array_filter(array_column($list, 'sjId')));
  302. $sjInfo = MerchantClass::getByIds($customSjIdList, null, 'id');
  303. $levelMap = self::$levelMap;
  304. foreach ($list as $key => $val) {
  305. $customSjId = $val['sjId'];
  306. $currentInfo = isset($sjInfo[$customSjId]) ? $sjInfo[$customSjId] : [];
  307. $currentInfo = business::formatMerchantLogo($currentInfo);
  308. $smallAvatar = !empty($currentInfo['smallLogoUrl']) ? $currentInfo['smallLogoUrl'] : imgUtil::getPrefix() . '/hhb_small.png';
  309. $avatar = !empty($currentInfo['logoUrl']) ? $currentInfo['logoUrl'] : imgUtil::getPrefix() . '/hhb_small.png';
  310. $list[$key]['smallAvatar'] = $smallAvatar;
  311. $list[$key]['shortSmallAvatar'] = $currentInfo['shortSmallLogoUrl'];
  312. $list[$key]['avatar'] = $avatar;
  313. $list[$key]['shortAvatar'] = $currentInfo['shortLogoUrl'];
  314. $list[$key]['sn'] = $val['id'];
  315. $level = $val['level'] ?? 1;
  316. $levelName = $levelMap[$level] ?? '';
  317. $list[$key]['levelName'] = $levelName;
  318. $debtAmount = $val['debtAmount'] ?? 0;
  319. $balance = $val['balance'] ?? 0;
  320. $remainDebtAmount = bcsub($debtAmount, $balance, 2);
  321. $list[$key]['remainDebtAmount'] = $remainDebtAmount;
  322. $recentExpend = $val['recentExpend'] ?? '';
  323. $recentExpend = strtotime($recentExpend);
  324. $seven = bcmul(86400, 7);
  325. $addTime = bcadd($recentExpend, $seven);
  326. $list[$key]['overSevenUnExpend'] = 0;
  327. if ($addTime < time()) {
  328. //超过7天未下单
  329. $list[$key]['overSevenUnExpend'] = 1;
  330. }
  331. }
  332. return $list;
  333. }
  334. //欠款权限开关 ssh 2021.1.8
  335. public static function debt($custom, $debt)
  336. {
  337. $id = isset($custom['id']) ? $custom['id'] : 0;
  338. self::updateById($id, ['debt' => $debt]);
  339. return true;
  340. }
  341. //采购查看权限的黑白名单设置 shizhongqi 2021.08.08
  342. public static function black($custom, $black)
  343. {
  344. $id = isset($custom['id']) ? $custom['id'] : 0;
  345. self::updateById($id, ['black' => $black]);
  346. return true;
  347. }
  348. //权限判断 ssh 2021.1.8
  349. public static function valid($custom, $shopId)
  350. {
  351. if (empty($custom)) {
  352. util::fail('没有找到客户');
  353. }
  354. if ($custom['ownShopId'] != $shopId) {
  355. util::fail('您无法操作此客户');
  356. }
  357. }
  358. //客户采购欠款金额增加 ssh 20220306
  359. public static function cgDebtAmountAdd($custom, $order, $bookOrder = false)
  360. {
  361. $amount = $order->remainDebtPrice ?? 0;
  362. $custom->isDebt = CustomClass::IS_DEBT_YES;
  363. $debtLimit = $custom->debtLimit ?? 0;
  364. if ($bookOrder == false) {
  365. if (isset($order->shopAdminId) && $order->shopAdminId == 0) {
  366. //客户自己下单要受额度限制
  367. if ($custom->debtAmount > $debtLimit) {
  368. util::fail("已欠款{$custom->debtAmount},请先结账哦");
  369. }
  370. }
  371. }
  372. $balance = bcadd($custom->debtAmount, $amount, 2);
  373. $mustNotice = false;
  374. if (getenv('YII_ENV') == 'production') {
  375. //王品牛排 黄先生
  376. if ($custom->id == 11726) {
  377. $mustNotice = true;
  378. }
  379. } else {
  380. if ($custom->id == 577) {
  381. $mustNotice = true;
  382. }
  383. }
  384. if (getenv('YII_ENV', 'local') == 'production') {
  385. if ($mustNotice) {
  386. noticeUtil::push("【开单动作 | 客户】变化前欠款:{$custom->debtAmount} 变化金额:{$amount} 变化后欠款:{$balance}", '15280215347');
  387. }
  388. }
  389. $custom->debtAmount = $balance;
  390. $custom->save();
  391. $relateId = $order->id ?? 0;
  392. $customId = $custom->id ?? 0;
  393. $orderSn = $order->orderSn ?? '';
  394. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  395. $event = '购买花材,单号:' . $orderSn;
  396. if ($bookOrder) {
  397. $event = '预订单多退少补,单号:' . $orderSn;
  398. }
  399. $change = [
  400. 'relateId' => $relateId,
  401. 'customId' => $customId,
  402. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  403. 'capitalType' => $capitalType,
  404. 'amount' => $amount,
  405. 'balance' => $balance,
  406. 'io' => 1,
  407. 'payWay' => $order->payWay,
  408. 'event' => $event,
  409. 'sjId' => $order->sjId,
  410. 'shopId' => $order->id,
  411. 'mainId' => $order->mainId,
  412. ];
  413. CustomDebtChangeClass::addChange($change);
  414. $mainId = $order->mainId ?? 0;
  415. $main = MainClass::getLockById($mainId);
  416. if (empty($main)) {
  417. util::fail('没有main信息');
  418. }
  419. $debt = $main->debt ?? 0;
  420. $remain = bcadd($debt, $amount, 2);
  421. if (getenv('YII_ENV', 'local') == 'production') {
  422. if ($mustNotice) {
  423. noticeUtil::push("【开单动作 | 总欠款】变化前欠款:{$debt} 变化金额:{$amount} 变化后欠款:{$remain}", '15280215347');
  424. }
  425. }
  426. $main->debt = $remain;
  427. $main->save();
  428. $event = $custom->name . ' 购买花材,单号:' . $orderSn;
  429. $change['balance'] = $remain;
  430. $change['event'] = $event;
  431. TotalDebtChangeClass::addChange($change);
  432. }
  433. //客户结账欠款金额减少 ssh 20220306
  434. public static function clearDebtAmountReduce($custom, $ghs, $amount, $clear)
  435. {
  436. $debtAmount = $custom->debtAmount ?? 0.00;
  437. $remain = bcsub($debtAmount, $amount, 2);
  438. if ($remain < 0) {
  439. noticeUtil::push("客户欠款总金额:{$custom->debtAmount} 本次结账金额:{$amount} 客户ID:{$custom->id} 【里外金额有问题】", '15280215347');
  440. util::fail('收款失败,结账后欠款金额会出现负数');
  441. }
  442. $custom->isDebt = $remain <= 0 ? 0 : 1;
  443. $custom->debtAmount = $remain;
  444. $custom->save();
  445. $relateId = $clear->id ?? 0;
  446. $customId = $custom->id ?? 0;
  447. $capitalType = dict::getDict('capitalType', 'ghsXsClear', 'id');
  448. $orderSn = $clear->orderSn ?? '';
  449. $change = [
  450. 'relateId' => $relateId,
  451. 'customId' => $customId,
  452. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  453. 'capitalType' => $capitalType,
  454. 'amount' => $amount,
  455. 'balance' => $remain,
  456. 'io' => 0,
  457. 'payWay' => 0,
  458. 'event' => "结账,单号:{$orderSn}",
  459. 'sjId' => $ghs->sjId,
  460. 'shopId' => $ghs->id,
  461. 'mainId' => $ghs->mainId,
  462. ];
  463. CustomDebtChangeClass::addChange($change);
  464. $mainId = $ghs->mainId ?? 0;
  465. $main = MainClass::getLockById($mainId);
  466. if (empty($main)) {
  467. util::fail('没有main信息');
  468. }
  469. $debt = $main->debt ?? 0;
  470. $remain = bcsub($debt, $amount, 2);
  471. $main->debt = $remain;
  472. $main->save();
  473. $event = $custom->name . " 结账,单号:{$orderSn}";
  474. $change['balance'] = $remain;
  475. $change['event'] = $event;
  476. TotalDebtChangeClass::addChange($change);
  477. }
  478. //客户退款欠款金额减少 ssh 20220306
  479. public static function refundDebtAmountReduce($custom, $refund, $order)
  480. {
  481. $refundPrice = $refund->refundPrice ?? 0;
  482. $balance = bcsub($custom->debtAmount, $refundPrice, 2);
  483. $custom->debtAmount = $balance;
  484. $custom->save();
  485. $relateId = $refund->id ?? 0;
  486. $customId = $custom->id ?? 0;
  487. $saleOrderSn = $order->orderSn ?? '';
  488. $capitalType = dict::getDict('capitalType', 'saleRefund', 'id');
  489. $change = [
  490. 'relateId' => $relateId,
  491. 'customId' => $customId,
  492. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  493. 'capitalType' => $capitalType,
  494. 'amount' => $refundPrice,
  495. 'balance' => $balance,
  496. 'io' => 0,
  497. 'payWay' => 0,
  498. 'event' => "订单号:{$saleOrderSn} 退款",
  499. 'sjId' => $refund->sjId,
  500. 'shopId' => $refund->id,
  501. 'mainId' => $order->mainId,
  502. ];
  503. CustomDebtChangeClass::addChange($change);
  504. $mainId = $order->mainId ?? 0;
  505. $main = MainClass::getLockById($mainId);
  506. if (empty($main)) {
  507. util::fail('没有main信息');
  508. }
  509. $debt = $main->debt ?? 0;
  510. $remain = bcsub($debt, $refundPrice, 2);
  511. $main->debt = $remain;
  512. $main->save();
  513. $event = $custom->name . " 订单号:{$saleOrderSn} 退款";
  514. $change['balance'] = $remain;
  515. $change['event'] = $event;
  516. TotalDebtChangeClass::addChange($change);
  517. }
  518. }