ShopClass.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. <?php
  2. namespace biz\shop\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\pt\classes\PtAssetClass;
  5. use biz\pt\classes\PtYeChangeClass;
  6. use biz\sj\classes\SjClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizGhs\product\classes\ProductClass;
  10. use bizHd\ad\classes\AdClass;
  11. use bizHd\goods\classes\CategoryClass;
  12. use bizHd\goods\classes\GoodsSettingClass;
  13. use bizHd\saas\classes\ApplyClass;
  14. use common\components\dict;
  15. use common\components\httpUtil;
  16. use common\components\imgUtil;
  17. use common\components\qrCodeUtil;
  18. use common\components\stringUtil;
  19. use common\components\util;
  20. use Yii;
  21. use biz\base\classes\BaseClass;
  22. class ShopClass extends BaseClass
  23. {
  24. public static $baseFile = '\biz\shop\models\Shop';
  25. //营业状态
  26. public static function isOpen($shop)
  27. {
  28. if (isset($shop['open']) == false || $shop['open'] == 0) {
  29. return 0;
  30. }
  31. $openStartTime = $shop['openStartTime'] ?? '';
  32. if (empty($openStartTime)) {
  33. return 1;
  34. }
  35. $openEndTime = $shop['openEndTime'] ?? '';
  36. $start = strtotime(date("Y-m-d") . ' ' . $openStartTime);
  37. $end = strtotime(date("Y-m-d") . ' ' . $openEndTime);
  38. $time = time();
  39. if ($time > $start && $time < $end) {
  40. return 1;
  41. }
  42. return 0;
  43. }
  44. //获取图片信息 ssh 2021.3.14
  45. public static function getShopInfo($id)
  46. {
  47. $info = self::getById($id);
  48. if (empty($info)) {
  49. return $info;
  50. }
  51. $list = self::groupShopInfo([$info]);
  52. return current($list);
  53. }
  54. public static function getShopList($where)
  55. {
  56. $list = self::getList('*', $where, 'addTime DESC');
  57. return $list;
  58. }
  59. //查看店铺 ssh 2021.3.1
  60. public static function getShopByIds($ids)
  61. {
  62. $list = self::getByIds($ids);
  63. return self::groupShopInfo($list);
  64. }
  65. //门店 ssh 2021.3.1
  66. public static function groupShopInfo($list)
  67. {
  68. if (empty($list)) {
  69. return $list;
  70. }
  71. foreach ($list as $key => $val) {
  72. $shortAvatar = $val['avatar'] ?? '';
  73. $avatar = imgUtil::groupImg($val['avatar']);
  74. $list[$key]['avatar'] = $avatar;
  75. $list[$key]['shortAvatar'] = $shortAvatar;
  76. //店长微信
  77. $superWx = $val['superWx'] ?? '';
  78. $list[$key]['superWx'] = imgUtil::groupImg($superWx) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  79. }
  80. return $list;
  81. }
  82. //获取商家默认的门店ID ssh 2020.1.19
  83. public static function getDefaultShopId($merchant)
  84. {
  85. return isset($merchant['defaultShopId']) ? $merchant['defaultShopId'] : 0;
  86. }
  87. //更新门店 ssh 2020.2.29
  88. public static function updateShop($shop, $data)
  89. {
  90. $id = $shop['id'] ?? 0;
  91. $shopName = isset($data['shopName']) ? $data['shopName'] : '';
  92. if (stringUtil::getWordNum($shopName) > 8) {
  93. util::fail('门店名称不能超过8个汉字,2个字母顶一个汉字');
  94. }
  95. $sjId = $data['sjId'];
  96. $findShop = self::getByCondition(['sjId' => $sjId, 'shopName' => $shopName]);
  97. if (!empty($findShop) && $findShop['id'] != $id) {
  98. util::fail('门店名称已经存在');
  99. }
  100. $dist = $data['dist'] ?? '';
  101. $floor = $data['floor'] ?? '';
  102. $address = $data['address'] ?? '';
  103. $data['fullAddress'] = $data['city'] . $dist . $address . $floor;
  104. // 营业时间设置逻辑
  105. if ($data['openType'] == 0) { // openType: 0. 表示 24小时营业 1. 表示 xx ~ xx
  106. $data['openStartTime'] = ''; // openStartTime 营业开始时间,空表示24小时
  107. $data['openEndTime'] = '';
  108. }
  109. $cutStyle = $data['cutStyle'] ?? 0;
  110. $cutAmount = $data['cutAmount'] ?? 0;
  111. $meetAmount = $data['meetAmount'] ?? 0;
  112. if ($cutStyle == 1) {
  113. if ($cutAmount >= 1) {
  114. util::fail('折扣要小于1');
  115. }
  116. } else {
  117. if ($meetAmount > 0 && $cutAmount > 0) {
  118. if ($cutAmount >= $meetAmount) {
  119. util::fail('优惠金额过大');
  120. }
  121. }
  122. }
  123. //默认门店的开启和关闭
  124. $preDefault = $shop['default'] ?? 0;
  125. $default = $data['default'] ?? 0;
  126. if ($preDefault != $default) {
  127. if ($default == 0) {
  128. util::fail('总店地位无法修改,至少要有一个总店');
  129. }
  130. $sjId = $shop['sjId'] ?? 0;
  131. self::updateByCondition(['sjId' => $sjId], ['default' => 0]);
  132. }
  133. $data['packCost'] = isset($data['packCost']) && $data['packCost'] > 0 ? $data['packCost'] : 0;
  134. $data['cgUnitType'] = isset($data['cgUnitType']) ? $data['cgUnitType'] : 0;
  135. $respond = self::updateById($id, $data);
  136. $ptStyle = $shop['ptStyle'] ?? dict::getDict('ptStyle', 'hd');
  137. $default = $shop['default'] ?? 0;
  138. $sjName = $shop['merchantName'] ?? '';
  139. //默认门店只显示商家名称即可
  140. $name = $default == 1 && $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  141. $shopUpdate = [
  142. 'name' => $name,
  143. 'mobile' => $data['mobile'] ?? '',
  144. 'province' => $data['province'] ?? '',
  145. 'city' => $data['city'] ?? '',
  146. 'dist' => $data['dist'] ?? '',
  147. 'address' => $data['address'] ?? '',
  148. 'floor' => $data['floor'] ?? '',
  149. 'fullAddress' => $data['fullAddress'] ?? '',
  150. 'showAddress' => $data['showAddress'] ?? '',
  151. 'lat' => $data['lat'] ?? '',
  152. 'long' => $data['long'] ?? '',
  153. ];
  154. $where = ['sjId' => $sjId, 'shopId' => $id];
  155. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  156. CustomClass::updateByCondition($where, $shopUpdate);
  157. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  158. GhsClass::updateByCondition($where, $shopUpdate);
  159. }
  160. return $respond;
  161. }
  162. //取商家所有门店 ssh 2020.2.29
  163. public static function getAllShop($sjId)
  164. {
  165. return self::getAllByCondition(['sjId' => $sjId], null, "*");
  166. }
  167. //验证所属权限 ssh 2020.2.29
  168. public static function valid($shop, $sjId)
  169. {
  170. if (empty($shop)) {
  171. util::fail('门店无效');
  172. }
  173. if (isset($shop['sjId']) == false || $shop['sjId'] != $sjId) {
  174. util::fail('您无法访问');
  175. }
  176. }
  177. //删除门店 ssh 2020.3.1
  178. public static function deleteShop($shop)
  179. {
  180. if (isset($shop['default']) && $shop['default'] == 1) {
  181. util::fail('默认门店不允许删除');
  182. }
  183. $id = $shop['id'];
  184. self::updateById($id, ['delStatus' => 1]);
  185. }
  186. //生成收款码 ssh 2020.3.9
  187. public static function generateGatheringCode($sjId, $shopId)
  188. {
  189. $url = Yii::$app->params['mallDomain'] . "/#/pages/pay/index?account=" . $shopId;
  190. //强制转成https
  191. $url = httpUtil::becomeHttps($url);
  192. Yii::info('收款码url:' . $url);
  193. $gatheringCode = qrCodeUtil::generateGatheringQrCode($url, $sjId, $shopId, '', 16);
  194. Yii::info('gatheringCode:' . $gatheringCode);
  195. $imgUrl = Yii::$app->params['hdImgHost'] . 'gathering/1.jpg?x-oss-process=image/watermark,image_' . base64_encode($gatheringCode) . ',g_nw,x_276,y_463/watermark,image_' . base64_encode('gathering/logo_big.png?x-oss-process=image/resize,P_12') . ',g_nw,x_555,y_728';
  196. return $imgUrl;
  197. }
  198. //客户充值增加余额 ssh 20240508
  199. public static function customRechargeAddBalance($main, $shop, $order)
  200. {
  201. $mainId = $shop->mainId ?? 0;
  202. $balance = bcadd($main->balance, $order->amount, 2);
  203. $txBalance = bcadd($main->txBalance, $order->amount, 2);
  204. $main->balance = $balance;
  205. $main->txBalance = $txBalance;
  206. $main->save();
  207. $rechargeId = $order->id ?? 0;
  208. $tx = dict::getDict('yeTx', 'can');
  209. $capitalType = dict::getDict('capitalType', 'customRechargeToGhs', 'id');
  210. $custom = '客户';
  211. if (isset($order->customName) && !empty($order->customName)) {
  212. $custom = $order->customName;
  213. }
  214. $change = [
  215. 'relateId' => $rechargeId,
  216. 'capitalType' => $capitalType,
  217. 'amount' => $order->amount,
  218. 'balance' => $balance,
  219. 'txBalance' => $txBalance,
  220. 'io' => 1,
  221. 'payWay' => $order->payWay,
  222. 'event' => $custom . "充值(单号:{$order->orderSn})",
  223. 'sjId' => $shop->sjId,
  224. 'shopId' => $shop->id,
  225. 'mainId' => $shop->mainId,
  226. 'tx' => $tx,
  227. 'ptStyle' => $shop->ptStyle,
  228. 'mainId' => $mainId,
  229. ];
  230. ShopYeChangeClass::addChange($change, true);
  231. //平台余额增加
  232. PtAssetClass::customRechargeAddBalance($shop, $order, $capitalType, $tx);
  233. }
  234. //客户结帐增加余额 ssh 20210729
  235. public static function customClearAddBalance($main, $shop, $order)
  236. {
  237. $mainId = $shop->mainId ?? 0;
  238. $balance = bcadd($main->balance, $order->actPrice, 2);
  239. $txBalance = bcadd($main->txBalance, $order->actPrice, 2);
  240. $main->balance = $balance;
  241. $main->txBalance = $txBalance;
  242. $main->save();
  243. $clearId = $order->id ?? 0;
  244. $tx = dict::getDict('yeTx', 'can');
  245. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  246. $custom = '客户';
  247. if (isset($order->customName) && !empty($order->customName)) {
  248. $custom = $order->customName;
  249. }
  250. $change = [
  251. 'relateId' => $clearId,
  252. 'capitalType' => $capitalType,
  253. 'amount' => $order->actPrice,
  254. 'balance' => $balance,
  255. 'txBalance' => $txBalance,
  256. 'io' => 1,
  257. 'payWay' => $order->payWay,
  258. 'event' => $custom . "结帐(单号:{$order->orderSn})",
  259. 'sjId' => $shop->sjId,
  260. 'shopId' => $shop->id,
  261. 'mainId' => $shop->mainId,
  262. 'tx' => $tx,
  263. 'ptStyle' => $shop->ptStyle,
  264. 'mainId' => $mainId,
  265. ];
  266. ShopYeChangeClass::addChange($change, true);
  267. //平台余额增加
  268. PtAssetClass::customClearAddBalance($shop, $order, $capitalType, $tx);
  269. }
  270. //客户使用优惠券增加余额 ssh 20210601
  271. public static function customKdUseCouponAddBalance($shop, $order, $capitalType)
  272. {
  273. //门店余额增加
  274. $amount = $order->discountAmount;
  275. $currentBalance = bcadd($shop->balance, $amount, 2);
  276. $shop->balance = $currentBalance;
  277. $shop->totalRecharge = bcadd($shop->totalRecharge, $amount, 2);
  278. //可提现余额同步增加
  279. $currentTx = bcadd($shop->txBalance, $amount, 2);
  280. $shop->txBalance = $currentTx;
  281. $shop->save();
  282. //增加余额变动记录
  283. $id = $order->id;
  284. $event = "客户下单(使用平台优惠券{$amount}元)";
  285. $tx = dict::getDict('yeTx', 'can');
  286. $mainId = $shop->mainId ?? 0;
  287. $change = [
  288. 'relateId' => $id,
  289. 'capitalType' => $capitalType,
  290. 'amount' => $amount,
  291. 'balance' => $currentBalance,
  292. 'txBalance' => $currentTx,
  293. 'io' => 1,
  294. 'payWay' => $order->payWay,
  295. 'event' => $event,
  296. 'mainId' => $mainId,
  297. 'tx' => $tx,
  298. 'ptStyle' => $shop->ptStyle,
  299. ];
  300. ShopYeChangeClass::addChange($change, true);
  301. //平台余额增加
  302. PtAssetClass::customKdUseCouponAddBalance($shop, $order, $capitalType, $tx);
  303. }
  304. //客户下单引起的余额增加 ssh 20210520
  305. public static function customKdAddBalance($main, $shop, $amount, $order, $capitalType)
  306. {
  307. //门店余额增加
  308. $mainId = $shop->mainId ?? 0;
  309. $currentBalance = bcadd($main->balance, $amount, 2);
  310. $main->balance = $currentBalance;
  311. //可提现余额同步增加
  312. $currentTx = bcadd($main->txBalance, $amount, 2);
  313. $main->txBalance = $currentTx;
  314. $main->save();
  315. $ptStyle = $shop->ptStyle ?? dict::getDict('ptStyle', 'hd');
  316. $customName = '客户';
  317. if ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  318. $customId = $order->customId ?? 0;
  319. $custom = CustomClass::getById($customId, true);
  320. if (isset($custom->name) && !empty($custom->name)) {
  321. $customName = $custom->name;
  322. }
  323. }
  324. //增加余额变动记录
  325. $id = $order->id;
  326. $event = $customName . "下单" . floatval($amount) . "元";
  327. $tx = dict::getDict('yeTx', 'can');
  328. $fromType = $order->fromType ?? 1;
  329. $change = [
  330. 'relateId' => $id,
  331. 'capitalType' => $capitalType,
  332. 'amount' => $amount,
  333. 'balance' => $currentBalance,
  334. 'txBalance' => $currentTx,
  335. 'io' => 1,
  336. 'payWay' => $order->payWay,
  337. 'event' => $event,
  338. 'mainId' => $mainId,
  339. 'tx' => $tx,
  340. 'ptStyle' => $shop->ptStyle,
  341. 'fromType' => $fromType,
  342. ];
  343. ShopYeChangeClass::addChange($change, true);
  344. //平台余额增加
  345. PtAssetClass::customKdAddBalance($shop, $amount, $order, $capitalType, $tx);
  346. }
  347. //门店充值增加余额 ssh 2021.2.21
  348. //$fix = true 余额只能在指定门店使用
  349. public static function rechargeAddBalance($shop, $amount, $order, $tx, $capitalType, $fix = false)
  350. {
  351. $mainId = $shop->mainId ?? 0;
  352. $main = MainClass::getLockById($mainId);
  353. if (empty($main)) {
  354. util::fail('没有找到资产信息');
  355. }
  356. //门店余额增加
  357. $currentBalance = bcadd($main->balance, $amount, 2);
  358. $main->balance = $currentBalance;
  359. $currentTotalRecharge = bcadd($shop->totalRecharge, $amount, 2);
  360. $main->totalRecharge = $currentTotalRecharge;
  361. //定向消费余额
  362. if ($fix) {
  363. $currentFix = bcadd($main->fixBalance, $amount, 2);
  364. $main->fixBalance = $currentFix;
  365. }
  366. $currentTx = $main->txBalance;
  367. //提现余额
  368. if ($tx == dict::getDict('yeTx', 'can')) {
  369. $currentTx = bcadd($main->txBalance, $amount, 2);
  370. $main->txBalance = $currentTx;
  371. }
  372. $main->save();
  373. $order->totalRecharge = $currentTotalRecharge;
  374. $order->save();
  375. //增加余额变动记录
  376. $id = $order->id;
  377. $event = "充值{$amount}元";
  378. if ($order->shopAdminId == 0) {
  379. $event = "系统充值{$amount}元";
  380. }
  381. $change = [
  382. 'relateId' => $id,
  383. 'capitalType' => $capitalType,
  384. 'amount' => $amount,
  385. 'balance' => $currentBalance,
  386. 'txBalance' => $currentTx,
  387. 'io' => 1,
  388. 'payWay' => $order->payWay,
  389. 'event' => $event,
  390. 'sjId' => $order->sjId,
  391. 'shopId' => $order->shopId,
  392. 'mainId' => $mainId,
  393. 'tx' => $tx,
  394. 'ptStyle' => $shop->ptStyle,
  395. ];
  396. ShopYeChangeClass::addChange($change, true);
  397. //平台余额增加
  398. PtAssetClass::rechargeAddBalance($shop, $amount, $order, $capitalType, $tx);
  399. }
  400. //采购余额付款,减少余额 ssh 20210519
  401. public static function purchaseReduceBalance($main, $shop, $amount, $order)
  402. {
  403. if (bccomp($amount, $main->balance, 2) == 1) {
  404. util::fail('余额不足');
  405. }
  406. $sjId = $order->sjId;
  407. $shopId = $order->shopId;
  408. $mainId = $order->mainId ?? 0;
  409. $currentBalance = bcsub($main->balance, $amount, 2);
  410. $main->balance = $currentBalance;
  411. $tx = dict::getDict('yeTx', 'canNot');
  412. //还有可提现余额也要先用掉
  413. $currentTxBalance = $main->txBalance;
  414. if ($currentTxBalance > 0) {
  415. $reduceTxBalance = $currentTxBalance > $amount ? $amount : $currentTxBalance;
  416. $main->txBalance = bcsub($currentTxBalance, $reduceTxBalance, 2);
  417. $tx = dict::getDict('yeTx', 'can');
  418. }
  419. $main->save();
  420. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  421. //增加余额变动记录
  422. $id = $order->id;
  423. $change = [
  424. 'relateId' => $id,
  425. 'capitalType' => $capitalType,
  426. 'amount' => $amount,
  427. 'balance' => $currentBalance,
  428. 'txBalance' => $currentTxBalance,
  429. 'io' => 0,
  430. 'payWay' => $order->payWay,
  431. 'event' => "采购余额付款{$amount}元",
  432. 'sjId' => $sjId,
  433. 'shopId' => $shopId,
  434. 'mainId' => $mainId,
  435. 'tx' => $tx,
  436. 'ptStyle' => $shop->ptStyle,
  437. ];
  438. ShopYeChangeClass::addChange($change, true);
  439. //平台余额增加
  440. PtAssetClass::reduceBalance($shop, $amount, $order, $capitalType, $tx);
  441. }
  442. //零售采购退款
  443. public static function cgRefundAddBalance($main, $shop, $refundPrice, $cgRefund, $ghs)
  444. {
  445. //客户余额增加
  446. $currentBalance = bcadd($main->balance, $refundPrice, 2);
  447. $main->balance = $currentBalance;
  448. $main->save();
  449. $currentTxBalance = $main->txBalance;
  450. $currentType = dict::getDict('capitalType', 'cgRefund', 'id');
  451. $id = $cgRefund->id;
  452. $ptStyle = $shop->ptStyle;
  453. $sjId = $shop->sjId;
  454. $shopId = $shop->id;
  455. $mainId = $shop->mainId;
  456. $tx = dict::getDict('yeTx', 'canNot');
  457. $ghsName = $ghs->name ?? '';
  458. $change = [
  459. 'relateId' => $id,
  460. 'capitalType' => $currentType,
  461. 'amount' => $refundPrice,
  462. 'balance' => $currentBalance,
  463. 'txBalance' => $currentTxBalance,
  464. 'io' => 1,
  465. 'payWay' => 0,
  466. 'event' => "采购退款({$ghsName})",
  467. 'sjId' => $sjId,
  468. 'shopId' => $shopId,
  469. 'mainId' => $mainId,
  470. 'tx' => $tx,
  471. 'ptStyle' => $ptStyle,
  472. ];
  473. ShopYeChangeClass::addChange($change, true);
  474. PtAssetClass::cgRefundAddBalance($shop, $refundPrice, $cgRefund, $currentType, $tx);
  475. }
  476. //供货商预订被多付了退款减少余额 ssh 20220324
  477. public static function ghsBookOrderRefundReduceBalance($main, $shop, $refund, $refundPrice)
  478. {
  479. $currentBalance = bcsub($main->balance, $refundPrice, 2);
  480. if ($currentBalance < 0) {
  481. util::fail('余额不足,请先充值哦');
  482. }
  483. $main->balance = $currentBalance;
  484. $currentTx = $main->txBalance;
  485. if ($currentTx > 0) {
  486. if ($refundPrice > $currentTx) {
  487. $currentTx = 0;
  488. } else {
  489. $currentTx = bcsub($currentTx, $refundPrice, 2);
  490. }
  491. }
  492. $main->txBalance = $currentTx;
  493. $main->save();
  494. $currentType = dict::getDict('capitalType', 'ghsBookRefund');
  495. $id = $refund->id;
  496. $event = "预订单被多付退给客户(退款单 {$refund->orderSn} 订单{$refund->relateOrderSn})";
  497. $relateOrderSn = $refund->relateOrderSn ?? '';
  498. if (!empty($relateOrderSn)) {
  499. $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  500. $customName = $relateOrder->customName ?? '';
  501. if (!empty($customName)) {
  502. $event = "预订单{$customName}多付退款(退款单 {$refund->orderSn} 订单{$refund->relateOrderSn})";
  503. }
  504. }
  505. $tx = dict::getDict('yeTx', 'can');
  506. $change = [
  507. 'relateId' => $id,
  508. 'capitalType' => $currentType,
  509. 'amount' => $refundPrice,
  510. 'balance' => $currentBalance,
  511. 'txBalance' => $currentTx,
  512. 'io' => 0,
  513. 'payWay' => 0,
  514. 'event' => $event,
  515. 'sjId' => $shop->sjId,
  516. 'shopId' => $shop->id,
  517. 'mainId' => $shop->mainId,
  518. 'tx' => $tx,
  519. 'ptStyle' => $shop->ptStyle,
  520. ];
  521. ShopYeChangeClass::addChange($change, true);
  522. //平台余额增加
  523. PtAssetClass::ghsBookOrderRefundReduceBalance($shop, $refund, $refundPrice);
  524. }
  525. //花店销售单退款减少余额
  526. public static function hdSaleRefundReduceBalance($main, $shop, $refund, $refundPrice)
  527. {
  528. //门店余额减少
  529. $currentBalance = bcsub($main->balance, $refundPrice, 2);
  530. if ($currentBalance < 0) {
  531. util::fail('余额不足,请先充值吧');
  532. }
  533. $main->balance = $currentBalance;
  534. $currentTx = $main->txBalance;
  535. if ($currentTx > 0) {
  536. if ($refundPrice > $currentTx) {
  537. $currentTx = 0;
  538. } else {
  539. $currentTx = bcsub($currentTx, $refundPrice, 2);
  540. }
  541. }
  542. $main->txBalance = $currentTx;
  543. $main->save();
  544. $currentType = dict::getDict('capitalType', 'hdOrderRefund');
  545. $id = $refund->id;
  546. $event = "退款给客户(退款单 {$refund->refundSn})";
  547. $relateOrderSn = $refund->orderSn ?? '';
  548. if (!empty($relateOrderSn)) {
  549. $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  550. $customName = $relateOrder->customName ?? '';
  551. if (!empty($customName)) {
  552. $event = "退款给{$customName}(退款单 {$refund->refundSn})";
  553. }
  554. }
  555. $tx = dict::getDict('yeTx', 'can');
  556. $change = [
  557. 'relateId' => $id,
  558. 'capitalType' => $currentType,
  559. 'amount' => $refundPrice,
  560. 'balance' => $currentBalance,
  561. 'txBalance' => $currentTx,
  562. 'io' => 0,
  563. 'payWay' => 0,
  564. 'event' => $event,
  565. 'sjId' => $shop->sjId,
  566. 'shopId' => $shop->id,
  567. 'mainId' => $shop->mainId,
  568. 'tx' => $tx,
  569. 'ptStyle' => $shop->ptStyle,
  570. ];
  571. ShopYeChangeClass::addChange($change, true);
  572. //平台余额增加
  573. PtAssetClass::hdSaleRefundReduceBalance($shop, $refund, $refundPrice);
  574. }
  575. //销售单退款减少余额 ssh 2021.2.21
  576. public static function saleRefundReduceBalance($main, $shop, $refund, $refundPrice)
  577. {
  578. //门店余额减少
  579. $currentBalance = bcsub($main->balance, $refundPrice, 2);
  580. if ($currentBalance < 0) {
  581. util::fail('余额不足,请稍后再操作');
  582. }
  583. $main->balance = $currentBalance;
  584. $currentTx = $main->txBalance;
  585. if ($currentTx > 0) {
  586. if ($refundPrice > $currentTx) {
  587. $currentTx = 0;
  588. } else {
  589. $currentTx = bcsub($currentTx, $refundPrice, 2);
  590. }
  591. }
  592. $main->txBalance = $currentTx;
  593. $main->save();
  594. $currentType = dict::getDict('capitalType', 'saleRefund');
  595. $id = $refund->id;
  596. $event = "退款给客户(退款单 {$refund->orderSn})";
  597. $relateOrderSn = $refund->relateOrderSn ?? '';
  598. if (!empty($relateOrderSn)) {
  599. $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  600. $customName = $relateOrder->customName ?? '';
  601. if (!empty($customName)) {
  602. $event = "退款给{$customName}(退款单 {$refund->orderSn})";
  603. }
  604. }
  605. $tx = dict::getDict('yeTx', 'can');
  606. $change = [
  607. 'relateId' => $id,
  608. 'capitalType' => $currentType,
  609. 'amount' => $refundPrice,
  610. 'balance' => $currentBalance,
  611. 'txBalance' => $currentTx,
  612. 'io' => 0,
  613. 'payWay' => 0,
  614. 'event' => $event,
  615. 'sjId' => $shop->sjId,
  616. 'shopId' => $shop->id,
  617. 'mainId' => $shop->mainId,
  618. 'tx' => $tx,
  619. 'ptStyle' => $shop->ptStyle,
  620. ];
  621. ShopYeChangeClass::addChange($change, true);
  622. //平台余额增加
  623. PtAssetClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
  624. }
  625. //获取商家所有的门店 ssh 2021.2.26
  626. public static function getSjShopList($sjId)
  627. {
  628. return self::getAllByCondition(['sjId' => $sjId, 'delStatus' => 0], null, ['id']);
  629. }
  630. //取门店长信息 ssh 2021.1.17
  631. public static function getSuper($id)
  632. {
  633. $shop = self::getById($id);
  634. if (empty($shop)) {
  635. return [];
  636. }
  637. $adminId = $shop['adminId'] ?? 0;
  638. return \bizHd\admin\classes\AdminClass::getDetail($adminId);
  639. }
  640. //佣金引起的余额增加 lqh 2021211
  641. /**
  642. * @param $shop
  643. * @param $amount 提现金额
  644. * @param $cashId 提现主键ID
  645. * @param $capitalType
  646. */
  647. public static function brokerageAddBalance($shop, $cash, $capitalType)
  648. {
  649. $amount = $cash->amount ?? 0;
  650. $amount = floatval($amount);
  651. $cashId = $cash->id ?? 0;
  652. $customerSjName = $cash->sjName ?? '';
  653. $customerName = $cash->shopName ?? '';
  654. //获取的佣金 万分之三 暂时先写死,后续可走配置
  655. $brokerage = bcmul($amount, 0.0003, 2);
  656. //门店余额增加
  657. $currentBalance = bcadd($shop->balance, $brokerage, 2);
  658. $shop->balance = $currentBalance;
  659. $shop->totalRecharge = bcadd($shop->totalRecharge, $brokerage, 2);
  660. //可提现余额同步增加
  661. $currentTx = bcadd($shop->txBalance, $brokerage, 2);
  662. $shop->txBalance = $currentTx;
  663. $shop->save();
  664. //增加余额变动记录
  665. $tx = dict::getDict('yeTx', 'can');
  666. $change = [
  667. 'relateId' => $cashId,
  668. 'capitalType' => $capitalType,
  669. 'amount' => $brokerage,
  670. 'balance' => $currentBalance,
  671. 'txBalance' => $currentTx,
  672. 'io' => 1,
  673. 'payWay' => 0,
  674. 'event' => $customerSjName . '-' . $customerName . "提现" . $amount . "元获得佣金",
  675. 'sjId' => $shop->sjId,
  676. 'shopId' => $shop->id,
  677. 'mainId' => $shop->mainId,
  678. 'tx' => $tx,
  679. 'ptStyle' => $shop->ptStyle,
  680. ];
  681. ShopYeChangeClass::addChange($change, true);
  682. //平台余额增加
  683. $ptStyle = $shop->ptStyle;
  684. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  685. $asset = PtAssetClass::getHdBalance();
  686. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  687. $asset = PtAssetClass::getGhsBalance();
  688. } else {
  689. util::fail('没有找到平台');
  690. }
  691. //增加平台可余额
  692. $currentPtBalance = bcadd($asset->amount, $brokerage, 2);
  693. $asset->amount = $currentPtBalance;
  694. $asset->save();
  695. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  696. $txAsset = PtAssetClass::getHdTxBalance();
  697. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  698. $txAsset = PtAssetClass::getGhsTxBalance();
  699. } else {
  700. util::fail('没有找到平台');
  701. }
  702. //增加平台可提现余额
  703. $currentPtTxBalance = $txAsset->amount;
  704. $currentPtTxBalance = bcadd($currentPtTxBalance, $brokerage, 2);
  705. $txAsset->amount = $currentPtTxBalance;
  706. $txAsset->save();
  707. $change = [
  708. 'relateId' => $cashId,
  709. 'capitalType' => $capitalType,
  710. 'amount' => $brokerage,
  711. 'balance' => $currentPtBalance,
  712. 'txBalance' => $currentPtTxBalance,
  713. 'io' => 1,
  714. 'payWay' => 0,
  715. 'event' => $customerSjName . '-' . $customerName . "提现" . $amount . "元,代理商【{$shop->merchantName}-{$shop->shopName}】获得佣金",
  716. 'sjId' => $shop->sjId,
  717. 'shopId' => $shop->id,
  718. 'tx' => $tx,
  719. 'ptStyle' => $ptStyle,
  720. 'shopName' => $shop->shopName,
  721. 'sjName' => $shop->merchantName,
  722. ];
  723. PtYeChangeClass::addChange($change, true);
  724. }
  725. }