CustomClass.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. }
  323. return $list;
  324. }
  325. //欠款权限开关 ssh 2021.1.8
  326. public static function debt($custom, $debt)
  327. {
  328. $id = isset($custom['id']) ? $custom['id'] : 0;
  329. self::updateById($id, ['debt' => $debt]);
  330. return true;
  331. }
  332. //采购查看权限的黑白名单设置 shizhongqi 2021.08.08
  333. public static function black($custom, $black)
  334. {
  335. $id = isset($custom['id']) ? $custom['id'] : 0;
  336. self::updateById($id, ['black' => $black]);
  337. return true;
  338. }
  339. //权限判断 ssh 2021.1.8
  340. public static function valid($custom, $shopId)
  341. {
  342. if (empty($custom)) {
  343. util::fail('没有找到客户');
  344. }
  345. if ($custom['ownShopId'] != $shopId) {
  346. util::fail('您无法操作此客户');
  347. }
  348. }
  349. //客户采购欠款金额增加 ssh 20220306
  350. public static function cgDebtAmountAdd($custom, $order, $bookOrder = false)
  351. {
  352. $amount = $order->remainDebtPrice ?? 0;
  353. $custom->isDebt = CustomClass::IS_DEBT_YES;
  354. $debtLimit = $custom->debtLimit ?? 0;
  355. if ($bookOrder == false) {
  356. if (isset($order->shopAdminId) && $order->shopAdminId == 0) {
  357. //客户自己下单要受额度限制
  358. if ($custom->debtAmount > $debtLimit) {
  359. util::fail("已欠款{$custom->debtAmount},请先结账哦");
  360. }
  361. }
  362. }
  363. $balance = bcadd($custom->debtAmount, $amount, 2);
  364. $mustNotice = false;
  365. if (getenv('YII_ENV') == 'production') {
  366. //王品牛排 黄先生
  367. if ($custom->id == 11726) {
  368. $mustNotice = true;
  369. }
  370. } else {
  371. if ($custom->id == 577) {
  372. $mustNotice = true;
  373. }
  374. }
  375. if (getenv('YII_ENV', 'local') == 'production') {
  376. if ($mustNotice) {
  377. noticeUtil::push("【开单动作 | 客户】变化前欠款:{$custom->debtAmount} 变化金额:{$amount} 变化后欠款:{$balance}", '15280215347');
  378. }
  379. }
  380. $custom->debtAmount = $balance;
  381. $custom->save();
  382. $relateId = $order->id ?? 0;
  383. $customId = $custom->id ?? 0;
  384. $orderSn = $order->orderSn ?? '';
  385. $capitalType = dict::getDict('capitalType', 'xhGhsOrder', 'id');
  386. $event = '购买花材,单号:' . $orderSn;
  387. if ($bookOrder) {
  388. $event = '预订单多退少补,单号:' . $orderSn;
  389. }
  390. $change = [
  391. 'relateId' => $relateId,
  392. 'customId' => $customId,
  393. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  394. 'capitalType' => $capitalType,
  395. 'amount' => $amount,
  396. 'balance' => $balance,
  397. 'io' => 1,
  398. 'payWay' => $order->payWay,
  399. 'event' => $event,
  400. 'sjId' => $order->sjId,
  401. 'shopId' => $order->id,
  402. 'mainId' => $order->mainId,
  403. ];
  404. CustomDebtChangeClass::addChange($change);
  405. $mainId = $order->mainId ?? 0;
  406. $main = MainClass::getLockById($mainId);
  407. if (empty($main)) {
  408. util::fail('没有main信息');
  409. }
  410. $debt = $main->debt ?? 0;
  411. $remain = bcadd($debt, $amount, 2);
  412. if (getenv('YII_ENV', 'local') == 'production') {
  413. if ($mustNotice) {
  414. noticeUtil::push("【开单动作 | 总欠款】变化前欠款:{$debt} 变化金额:{$amount} 变化后欠款:{$remain}", '15280215347');
  415. }
  416. }
  417. $main->debt = $remain;
  418. $main->save();
  419. $event = $custom->name . ' 购买花材,单号:' . $orderSn;
  420. $change['balance'] = $remain;
  421. $change['event'] = $event;
  422. TotalDebtChangeClass::addChange($change);
  423. }
  424. //客户结账欠款金额减少 ssh 20220306
  425. public static function clearDebtAmountReduce($custom, $ghs, $amount, $clear)
  426. {
  427. $debtAmount = $custom->debtAmount ?? 0.00;
  428. $remain = bcsub($debtAmount, $amount, 2);
  429. if ($remain < 0) {
  430. noticeUtil::push("客户欠款总金额:{$custom->debtAmount} 本次结账金额:{$amount} 客户ID:{$custom->id} 【里外金额有问题】", '15280215347');
  431. util::fail('收款失败,结账后欠款金额会出现负数');
  432. }
  433. $custom->isDebt = $remain <= 0 ? 0 : 1;
  434. $custom->debtAmount = $remain;
  435. $custom->save();
  436. $relateId = $clear->id ?? 0;
  437. $customId = $custom->id ?? 0;
  438. $capitalType = dict::getDict('capitalType', 'ghsXsClear', 'id');
  439. $orderSn = $clear->orderSn ?? '';
  440. $change = [
  441. 'relateId' => $relateId,
  442. 'customId' => $customId,
  443. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  444. 'capitalType' => $capitalType,
  445. 'amount' => $amount,
  446. 'balance' => $remain,
  447. 'io' => 0,
  448. 'payWay' => 0,
  449. 'event' => "结账,单号:{$orderSn}",
  450. 'sjId' => $ghs->sjId,
  451. 'shopId' => $ghs->id,
  452. 'mainId' => $ghs->mainId,
  453. ];
  454. CustomDebtChangeClass::addChange($change);
  455. $mainId = $ghs->mainId ?? 0;
  456. $main = MainClass::getLockById($mainId);
  457. if (empty($main)) {
  458. util::fail('没有main信息');
  459. }
  460. $debt = $main->debt ?? 0;
  461. $remain = bcsub($debt, $amount, 2);
  462. $main->debt = $remain;
  463. $main->save();
  464. $event = $custom->name . " 结账,单号:{$orderSn}";
  465. $change['balance'] = $remain;
  466. $change['event'] = $event;
  467. TotalDebtChangeClass::addChange($change);
  468. }
  469. //客户退款欠款金额减少 ssh 20220306
  470. public static function refundDebtAmountReduce($custom, $refund, $order)
  471. {
  472. $refundPrice = $refund->refundPrice ?? 0;
  473. $balance = bcsub($custom->debtAmount, $refundPrice, 2);
  474. $custom->debtAmount = $balance;
  475. $custom->save();
  476. $relateId = $refund->id ?? 0;
  477. $customId = $custom->id ?? 0;
  478. $saleOrderSn = $order->orderSn ?? '';
  479. $capitalType = dict::getDict('capitalType', 'saleRefund', 'id');
  480. $change = [
  481. 'relateId' => $relateId,
  482. 'customId' => $customId,
  483. 'ptStyle' => dict::getDict('ptStyle', 'ghs'),
  484. 'capitalType' => $capitalType,
  485. 'amount' => $refundPrice,
  486. 'balance' => $balance,
  487. 'io' => 0,
  488. 'payWay' => 0,
  489. 'event' => "订单号:{$saleOrderSn} 退款",
  490. 'sjId' => $refund->sjId,
  491. 'shopId' => $refund->id,
  492. 'mainId' => $order->mainId,
  493. ];
  494. CustomDebtChangeClass::addChange($change);
  495. $mainId = $order->mainId ?? 0;
  496. $main = MainClass::getLockById($mainId);
  497. if (empty($main)) {
  498. util::fail('没有main信息');
  499. }
  500. $debt = $main->debt ?? 0;
  501. $remain = bcsub($debt, $refundPrice, 2);
  502. $main->debt = $remain;
  503. $main->save();
  504. $event = $custom->name . " 订单号:{$saleOrderSn} 退款";
  505. $change['balance'] = $remain;
  506. $change['event'] = $event;
  507. TotalDebtChangeClass::addChange($change);
  508. }
  509. }