ShopClass.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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', 'ghs')) {
  156. //如果是批发店,更新供货商信息
  157. GhsClass::updateByCondition($where, $shopUpdate);
  158. }
  159. if ($ptStyle == 1) {
  160. //如果是花店,更新客户端信息
  161. $city = $data['city'] ?? '';
  162. $dist = $data['dist'] ?? '';
  163. $address = $data['address'] ?? '';
  164. $floor = $data['floor'] ?? '';
  165. $fullAddress = $city . $dist . $address . $floor;
  166. $showAddress = $data['showAddress'] ?? '';
  167. $lat = $data['lat'] ?? '';
  168. $long = $data['long'] ?? '';
  169. $upData = ['city' => $city,
  170. 'dist' => $dist,
  171. 'address' => $address,
  172. 'floor' => $floor,
  173. 'fullAddress' => $fullAddress,
  174. 'showAddress' => $showAddress,
  175. 'lat' => $lat,
  176. 'long' => $long
  177. ];
  178. CustomClass::updateByCondition(['shopId' => $id], $upData);
  179. }
  180. return $respond;
  181. }
  182. //取商家所有门店 ssh 2020.2.30
  183. public static function getAllShop($sjId)
  184. {
  185. return self::getAllByCondition(['sjId' => $sjId], null, "*");
  186. }
  187. //验证所属权限 ssh 2020.2.29
  188. public static function valid($shop, $sjId)
  189. {
  190. if (empty($shop)) {
  191. util::fail('门店无效');
  192. }
  193. if (isset($shop['sjId']) == false || $shop['sjId'] != $sjId) {
  194. util::fail('您无法访问');
  195. }
  196. }
  197. //删除门店 ssh 2020.3.1
  198. public static function deleteShop($shop)
  199. {
  200. if (isset($shop['default']) && $shop['default'] == 1) {
  201. util::fail('默认门店不允许删除');
  202. }
  203. $id = $shop['id'];
  204. self::updateById($id, ['delStatus' => 1]);
  205. }
  206. //生成收款码 ssh 2020.3.9
  207. public static function generateGatheringCode($sjId, $shopId)
  208. {
  209. $url = Yii::$app->params['mallDomain'] . "/#/pages/pay/index?account=" . $shopId;
  210. //强制转成https
  211. $url = httpUtil::becomeHttps($url);
  212. Yii::info('收款码url:' . $url);
  213. $gatheringCode = qrCodeUtil::generateGatheringQrCode($url, $sjId, $shopId, '', 16);
  214. Yii::info('gatheringCode:' . $gatheringCode);
  215. $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';
  216. return $imgUrl;
  217. }
  218. //客户充值增加余额 ssh 20240508
  219. public static function customRechargeAddBalance($main, $shop, $order)
  220. {
  221. $mainId = $shop->mainId ?? 0;
  222. $balance = bcadd($main->balance, $order->amount, 2);
  223. $txBalance = bcadd($main->txBalance, $order->amount, 2);
  224. $main->balance = $balance;
  225. $main->txBalance = $txBalance;
  226. $main->save();
  227. $rechargeId = $order->id ?? 0;
  228. $tx = dict::getDict('yeTx', 'can');
  229. $capitalType = dict::getDict('capitalType', 'customRechargeToGhs', 'id');
  230. $custom = '客户';
  231. if (isset($order->customName) && !empty($order->customName)) {
  232. $custom = $order->customName;
  233. }
  234. $change = [
  235. 'relateId' => $rechargeId,
  236. 'capitalType' => $capitalType,
  237. 'amount' => $order->amount,
  238. 'balance' => $balance,
  239. 'txBalance' => $txBalance,
  240. 'io' => 1,
  241. 'payWay' => $order->payWay,
  242. 'event' => $custom . "充值,单号:{$order->orderSn}",
  243. 'sjId' => $shop->sjId,
  244. 'shopId' => $shop->id,
  245. 'mainId' => $shop->mainId,
  246. 'tx' => $tx,
  247. 'ptStyle' => $shop->ptStyle,
  248. 'mainId' => $mainId,
  249. ];
  250. ShopYeChangeClass::addChange($change, true);
  251. //平台余额增加
  252. PtAssetClass::customRechargeAddBalance($shop, $order, $capitalType, $tx);
  253. }
  254. public static function skRechargeAddBalance($main, $shop, $order)
  255. {
  256. $mainId = $main->id;
  257. $balance = bcadd($main->balance, $order->amount, 2);
  258. $txBalance = bcadd($main->txBalance, $order->amount, 2);
  259. $main->balance = $balance;
  260. $main->txBalance = $txBalance;
  261. $main->save();
  262. $rechargeId = $order->id ?? 0;
  263. $tx = dict::getDict('yeTx', 'can');
  264. $capitalType = dict::getDict('capitalType', 'xhRecharge', 'id');
  265. $custom = !empty($order->customName) ? $order->customName : '客户';
  266. $change = [
  267. 'relateId' => $rechargeId,
  268. 'capitalType' => $capitalType,
  269. 'amount' => $order->amount,
  270. 'balance' => $balance,
  271. 'txBalance' => $txBalance,
  272. 'io' => 1,
  273. 'payWay' => $order->payWay,
  274. 'event' => $custom . "充值,单号:{$order->orderSn}",
  275. 'sjId' => $shop->sjId,
  276. 'shopId' => $shop->id,
  277. 'mainId' => $mainId,
  278. 'tx' => $tx,
  279. 'ptStyle' => $shop->ptStyle,
  280. ];
  281. ShopYeChangeClass::addChange($change, true);
  282. }
  283. //客户结帐增加余额 ssh 20210729
  284. public static function customClearAddBalance($main, $shop, $order)
  285. {
  286. $mainId = $shop->mainId ?? 0;
  287. $balance = bcadd($main->balance, $order->actPrice, 2);
  288. $txBalance = bcadd($main->txBalance, $order->actPrice, 2);
  289. $main->balance = $balance;
  290. $main->txBalance = $txBalance;
  291. $main->save();
  292. $clearId = $order->id ?? 0;
  293. $tx = dict::getDict('yeTx', 'can');
  294. $capitalType = dict::getDict('capitalType', 'hdPurchaseClear', 'id');
  295. $custom = '客户';
  296. if (isset($order->customName) && !empty($order->customName)) {
  297. $custom = $order->customName;
  298. }
  299. $change = [
  300. 'relateId' => $clearId,
  301. 'capitalType' => $capitalType,
  302. 'amount' => $order->actPrice,
  303. 'balance' => $balance,
  304. 'txBalance' => $txBalance,
  305. 'io' => 1,
  306. 'payWay' => $order->payWay,
  307. 'event' => $custom . "结帐(单号:{$order->orderSn})",
  308. 'sjId' => $shop->sjId,
  309. 'shopId' => $shop->id,
  310. 'mainId' => $shop->mainId,
  311. 'tx' => $tx,
  312. 'ptStyle' => $shop->ptStyle,
  313. 'mainId' => $mainId,
  314. ];
  315. ShopYeChangeClass::addChange($change, true);
  316. //平台余额增加
  317. PtAssetClass::customClearAddBalance($shop, $order, $capitalType, $tx);
  318. }
  319. //客户使用优惠券增加余额 ssh 20210601
  320. public static function customKdUseCouponAddBalance($shop, $order, $capitalType)
  321. {
  322. //门店余额增加
  323. $amount = $order->discountAmount;
  324. $currentBalance = bcadd($shop->balance, $amount, 2);
  325. $shop->balance = $currentBalance;
  326. $shop->totalRecharge = bcadd($shop->totalRecharge, $amount, 2);
  327. //可提现余额同步增加
  328. $currentTx = bcadd($shop->txBalance, $amount, 2);
  329. $shop->txBalance = $currentTx;
  330. $shop->save();
  331. //增加余额变动记录
  332. $id = $order->id;
  333. $event = "客户下单(使用平台优惠券{$amount}元)";
  334. $tx = dict::getDict('yeTx', 'can');
  335. $mainId = $shop->mainId ?? 0;
  336. $change = [
  337. 'relateId' => $id,
  338. 'capitalType' => $capitalType,
  339. 'amount' => $amount,
  340. 'balance' => $currentBalance,
  341. 'txBalance' => $currentTx,
  342. 'io' => 1,
  343. 'payWay' => $order->payWay,
  344. 'event' => $event,
  345. 'mainId' => $mainId,
  346. 'tx' => $tx,
  347. 'ptStyle' => $shop->ptStyle,
  348. ];
  349. ShopYeChangeClass::addChange($change, true);
  350. //平台余额增加
  351. PtAssetClass::customKdUseCouponAddBalance($shop, $order, $capitalType, $tx);
  352. }
  353. //客户下单引起的余额增加 ssh 20210520
  354. public static function customKdAddBalance($main, $shop, $amount, $order, $capitalType)
  355. {
  356. //门店余额增加
  357. $mainId = $shop->mainId ?? 0;
  358. $currentBalance = bcadd($main->balance, $amount, 2);
  359. $main->balance = $currentBalance;
  360. //可提现余额同步增加
  361. $currentTx = bcadd($main->txBalance, $amount, 2);
  362. $main->txBalance = $currentTx;
  363. $main->save();
  364. $ptStyle = $shop->ptStyle ?? dict::getDict('ptStyle', 'hd');
  365. $customName = '客户';
  366. if ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  367. $customId = $order->customId ?? 0;
  368. $custom = CustomClass::getById($customId, true);
  369. if (isset($custom->name) && !empty($custom->name)) {
  370. $customName = $custom->name;
  371. }
  372. }
  373. //增加余额变动记录
  374. $id = $order->id;
  375. $event = $customName . "下单" . floatval($amount) . "元";
  376. $tx = dict::getDict('yeTx', 'can');
  377. $fromType = $order->fromType ?? 1;
  378. $change = [
  379. 'relateId' => $id,
  380. 'capitalType' => $capitalType,
  381. 'amount' => $amount,
  382. 'balance' => $currentBalance,
  383. 'txBalance' => $currentTx,
  384. 'io' => 1,
  385. 'payWay' => $order->payWay,
  386. 'event' => $event,
  387. 'mainId' => $mainId,
  388. 'tx' => $tx,
  389. 'ptStyle' => $shop->ptStyle,
  390. 'fromType' => $fromType,
  391. ];
  392. ShopYeChangeClass::addChange($change, true);
  393. //平台余额增加
  394. PtAssetClass::customKdAddBalance($shop, $amount, $order, $capitalType, $tx);
  395. }
  396. //门店充值增加余额 ssh 2021.2.21
  397. //$fix = true 余额只能在指定门店使用
  398. public static function rechargeAddBalance($shop, $amount, $order, $tx, $capitalType, $fix = false)
  399. {
  400. $mainId = $shop->mainId ?? 0;
  401. $main = MainClass::getLockById($mainId);
  402. if (empty($main)) {
  403. util::fail('没有找到资产信息');
  404. }
  405. //门店余额增加
  406. $currentBalance = bcadd($main->balance, $amount, 2);
  407. $main->balance = $currentBalance;
  408. $currentTotalRecharge = bcadd($shop->totalRecharge, $amount, 2);
  409. $main->totalRecharge = $currentTotalRecharge;
  410. //定向消费余额
  411. if ($fix) {
  412. $currentFix = bcadd($main->fixBalance, $amount, 2);
  413. $main->fixBalance = $currentFix;
  414. }
  415. $currentTx = $main->txBalance;
  416. //提现余额
  417. if ($tx == dict::getDict('yeTx', 'can')) {
  418. $currentTx = bcadd($main->txBalance, $amount, 2);
  419. $main->txBalance = $currentTx;
  420. }
  421. $main->save();
  422. $order->totalRecharge = $currentTotalRecharge;
  423. $order->save();
  424. //增加余额变动记录
  425. $id = $order->id;
  426. $event = "充值{$amount}元";
  427. if ($order->shopAdminId == 0) {
  428. $event = "系统充值{$amount}元";
  429. }
  430. $change = [
  431. 'relateId' => $id,
  432. 'capitalType' => $capitalType,
  433. 'amount' => $amount,
  434. 'balance' => $currentBalance,
  435. 'txBalance' => $currentTx,
  436. 'io' => 1,
  437. 'payWay' => $order->payWay,
  438. 'event' => $event,
  439. 'sjId' => $order->sjId,
  440. 'shopId' => $order->shopId,
  441. 'mainId' => $mainId,
  442. 'tx' => $tx,
  443. 'ptStyle' => $shop->ptStyle,
  444. ];
  445. ShopYeChangeClass::addChange($change, true);
  446. //平台余额增加
  447. PtAssetClass::rechargeAddBalance($shop, $amount, $order, $capitalType, $tx);
  448. }
  449. //采购余额付款,减少余额 ssh 20210519
  450. public static function purchaseReduceBalance($main, $shop, $amount, $order)
  451. {
  452. if (bccomp($amount, $main->balance, 2) == 1) {
  453. util::fail('余额不足');
  454. }
  455. $sjId = $order->sjId;
  456. $shopId = $order->shopId;
  457. $mainId = $order->mainId ?? 0;
  458. $currentBalance = bcsub($main->balance, $amount, 2);
  459. $main->balance = $currentBalance;
  460. $tx = dict::getDict('yeTx', 'canNot');
  461. //还有可提现余额也要先用掉
  462. $currentTxBalance = $main->txBalance;
  463. if ($currentTxBalance > 0) {
  464. $reduceTxBalance = $currentTxBalance > $amount ? $amount : $currentTxBalance;
  465. $main->txBalance = bcsub($currentTxBalance, $reduceTxBalance, 2);
  466. $tx = dict::getDict('yeTx', 'can');
  467. }
  468. $main->save();
  469. $capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
  470. //增加余额变动记录
  471. $id = $order->id;
  472. $change = [
  473. 'relateId' => $id,
  474. 'capitalType' => $capitalType,
  475. 'amount' => $amount,
  476. 'balance' => $currentBalance,
  477. 'txBalance' => $currentTxBalance,
  478. 'io' => 0,
  479. 'payWay' => $order->payWay,
  480. 'event' => "采购余额付款{$amount}元",
  481. 'sjId' => $sjId,
  482. 'shopId' => $shopId,
  483. 'mainId' => $mainId,
  484. 'tx' => $tx,
  485. 'ptStyle' => $shop->ptStyle,
  486. ];
  487. ShopYeChangeClass::addChange($change, true);
  488. //平台余额增加
  489. PtAssetClass::reduceBalance($shop, $amount, $order, $capitalType, $tx);
  490. }
  491. //零售采购退款
  492. public static function cgRefundAddBalance($main, $shop, $refundPrice, $cgRefund, $ghs)
  493. {
  494. //客户余额增加
  495. $currentBalance = bcadd($main->balance, $refundPrice, 2);
  496. $main->balance = $currentBalance;
  497. $main->save();
  498. $currentTxBalance = $main->txBalance;
  499. $currentType = dict::getDict('capitalType', 'cgRefund', 'id');
  500. $id = $cgRefund->id;
  501. $ptStyle = $shop->ptStyle;
  502. $sjId = $shop->sjId;
  503. $shopId = $shop->id;
  504. $mainId = $shop->mainId;
  505. $tx = dict::getDict('yeTx', 'canNot');
  506. $ghsName = $ghs->name ?? '';
  507. $change = [
  508. 'relateId' => $id,
  509. 'capitalType' => $currentType,
  510. 'amount' => $refundPrice,
  511. 'balance' => $currentBalance,
  512. 'txBalance' => $currentTxBalance,
  513. 'io' => 1,
  514. 'payWay' => 0,
  515. 'event' => "采购退款({$ghsName})",
  516. 'sjId' => $sjId,
  517. 'shopId' => $shopId,
  518. 'mainId' => $mainId,
  519. 'tx' => $tx,
  520. 'ptStyle' => $ptStyle,
  521. ];
  522. ShopYeChangeClass::addChange($change, true);
  523. PtAssetClass::cgRefundAddBalance($shop, $refundPrice, $cgRefund, $currentType, $tx);
  524. }
  525. //供货商预订被多付了退款减少余额 ssh 20220324
  526. public static function ghsBookOrderRefundReduceBalance($main, $shop, $refund, $refundPrice)
  527. {
  528. $currentBalance = bcsub($main->balance, $refundPrice, 2);
  529. if ($currentBalance < 0) {
  530. util::fail('余额不足,请先充值哦');
  531. }
  532. $main->balance = $currentBalance;
  533. $currentTx = $main->txBalance;
  534. if ($currentTx > 0) {
  535. if ($refundPrice > $currentTx) {
  536. $currentTx = 0;
  537. } else {
  538. $currentTx = bcsub($currentTx, $refundPrice, 2);
  539. }
  540. }
  541. $main->txBalance = $currentTx;
  542. $main->save();
  543. $currentType = dict::getDict('capitalType', 'ghsBookRefund');
  544. $id = $refund->id;
  545. $event = "预订单被多付退给客户(退款单 {$refund->orderSn} 订单{$refund->relateOrderSn})";
  546. $relateOrderSn = $refund->relateOrderSn ?? '';
  547. if (!empty($relateOrderSn)) {
  548. $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  549. $customName = $relateOrder->customName ?? '';
  550. if (!empty($customName)) {
  551. $event = "预订单{$customName}多付退款(退款单 {$refund->orderSn} 订单{$refund->relateOrderSn})";
  552. }
  553. }
  554. $tx = dict::getDict('yeTx', 'can');
  555. $change = [
  556. 'relateId' => $id,
  557. 'capitalType' => $currentType,
  558. 'amount' => $refundPrice,
  559. 'balance' => $currentBalance,
  560. 'txBalance' => $currentTx,
  561. 'io' => 0,
  562. 'payWay' => 0,
  563. 'event' => $event,
  564. 'sjId' => $shop->sjId,
  565. 'shopId' => $shop->id,
  566. 'mainId' => $shop->mainId,
  567. 'tx' => $tx,
  568. 'ptStyle' => $shop->ptStyle,
  569. ];
  570. ShopYeChangeClass::addChange($change, true);
  571. //平台余额增加
  572. PtAssetClass::ghsBookOrderRefundReduceBalance($shop, $refund, $refundPrice);
  573. }
  574. //花店销售单退款减少余额
  575. public static function hdSaleRefundReduceBalance($main, $shop, $refund, $refundPrice)
  576. {
  577. //门店余额减少
  578. $currentBalance = bcsub($main->balance, $refundPrice, 2);
  579. if ($currentBalance < 0) {
  580. util::fail('余额不足,请先充值吧');
  581. }
  582. $main->balance = $currentBalance;
  583. $currentTx = $main->txBalance;
  584. if ($currentTx > 0) {
  585. if ($refundPrice > $currentTx) {
  586. $currentTx = 0;
  587. } else {
  588. $currentTx = bcsub($currentTx, $refundPrice, 2);
  589. }
  590. }
  591. $main->txBalance = $currentTx;
  592. $main->save();
  593. $currentType = dict::getDict('capitalType', 'hdOrderRefund');
  594. $id = $refund->id;
  595. $event = "退款给客户(退款单 {$refund->refundSn})";
  596. $relateOrderSn = $refund->orderSn ?? '';
  597. if (!empty($relateOrderSn)) {
  598. $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  599. $customName = $relateOrder->customName ?? '';
  600. if (!empty($customName)) {
  601. $event = "退款给{$customName}(退款单 {$refund->refundSn})";
  602. }
  603. }
  604. $tx = dict::getDict('yeTx', 'can');
  605. $change = [
  606. 'relateId' => $id,
  607. 'capitalType' => $currentType,
  608. 'amount' => $refundPrice,
  609. 'balance' => $currentBalance,
  610. 'txBalance' => $currentTx,
  611. 'io' => 0,
  612. 'payWay' => 0,
  613. 'event' => $event,
  614. 'sjId' => $shop->sjId,
  615. 'shopId' => $shop->id,
  616. 'mainId' => $shop->mainId,
  617. 'tx' => $tx,
  618. 'ptStyle' => $shop->ptStyle,
  619. ];
  620. ShopYeChangeClass::addChange($change, true);
  621. //平台余额增加
  622. PtAssetClass::hdSaleRefundReduceBalance($shop, $refund, $refundPrice);
  623. }
  624. //销售单退款减少余额 ssh 2021.2.21
  625. public static function saleRefundReduceBalance($main, $shop, $refund, $refundPrice)
  626. {
  627. //门店余额减少
  628. $currentBalance = bcsub($main->balance, $refundPrice, 2);
  629. if ($currentBalance < 0) {
  630. util::fail('余额不足,请稍后再操作');
  631. }
  632. $main->balance = $currentBalance;
  633. $currentTx = $main->txBalance;
  634. if ($currentTx > 0) {
  635. if ($refundPrice > $currentTx) {
  636. $currentTx = 0;
  637. } else {
  638. $currentTx = bcsub($currentTx, $refundPrice, 2);
  639. }
  640. }
  641. $main->txBalance = $currentTx;
  642. $main->save();
  643. $currentType = dict::getDict('capitalType', 'saleRefund');
  644. $id = $refund->id;
  645. $event = "退款给客户(退款单 {$refund->orderSn})";
  646. $relateOrderSn = $refund->relateOrderSn ?? '';
  647. if (!empty($relateOrderSn)) {
  648. $relateOrder = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  649. $customName = $relateOrder->customName ?? '';
  650. if (!empty($customName)) {
  651. $event = "退款给{$customName}(退款单 {$refund->orderSn})";
  652. }
  653. }
  654. $tx = dict::getDict('yeTx', 'can');
  655. $change = [
  656. 'relateId' => $id,
  657. 'capitalType' => $currentType,
  658. 'amount' => $refundPrice,
  659. 'balance' => $currentBalance,
  660. 'txBalance' => $currentTx,
  661. 'io' => 0,
  662. 'payWay' => 0,
  663. 'event' => $event,
  664. 'sjId' => $shop->sjId,
  665. 'shopId' => $shop->id,
  666. 'mainId' => $shop->mainId,
  667. 'tx' => $tx,
  668. 'ptStyle' => $shop->ptStyle,
  669. ];
  670. ShopYeChangeClass::addChange($change, true);
  671. //平台余额增加
  672. PtAssetClass::saleRefundReduceBalance($shop, $refund, $refundPrice);
  673. }
  674. //获取商家所有的门店 ssh 2021.2.26
  675. public static function getSjShopList($sjId)
  676. {
  677. return self::getAllByCondition(['sjId' => $sjId, 'delStatus' => 0], null, ['id']);
  678. }
  679. //取门店长信息 ssh 2021.1.17
  680. public static function getSuper($id)
  681. {
  682. $shop = self::getById($id);
  683. if (empty($shop)) {
  684. return [];
  685. }
  686. $adminId = $shop['adminId'] ?? 0;
  687. return \bizHd\admin\classes\AdminClass::getDetail($adminId);
  688. }
  689. //佣金引起的余额增加 lqh 2021211
  690. /**
  691. * @param $shop
  692. * @param $amount 提现金额
  693. * @param $cashId 提现主键ID
  694. * @param $capitalType
  695. */
  696. public static function brokerageAddBalance($shop, $cash, $capitalType)
  697. {
  698. $amount = $cash->amount ?? 0;
  699. $amount = floatval($amount);
  700. $cashId = $cash->id ?? 0;
  701. $customerSjName = $cash->sjName ?? '';
  702. $customerName = $cash->shopName ?? '';
  703. //获取的佣金 万分之三 暂时先写死,后续可走配置
  704. $brokerage = bcmul($amount, 0.0003, 2);
  705. //门店余额增加
  706. $currentBalance = bcadd($shop->balance, $brokerage, 2);
  707. $shop->balance = $currentBalance;
  708. $shop->totalRecharge = bcadd($shop->totalRecharge, $brokerage, 2);
  709. //可提现余额同步增加
  710. $currentTx = bcadd($shop->txBalance, $brokerage, 2);
  711. $shop->txBalance = $currentTx;
  712. $shop->save();
  713. //增加余额变动记录
  714. $tx = dict::getDict('yeTx', 'can');
  715. $change = [
  716. 'relateId' => $cashId,
  717. 'capitalType' => $capitalType,
  718. 'amount' => $brokerage,
  719. 'balance' => $currentBalance,
  720. 'txBalance' => $currentTx,
  721. 'io' => 1,
  722. 'payWay' => 0,
  723. 'event' => $customerSjName . '-' . $customerName . "提现" . $amount . "元获得佣金",
  724. 'sjId' => $shop->sjId,
  725. 'shopId' => $shop->id,
  726. 'mainId' => $shop->mainId,
  727. 'tx' => $tx,
  728. 'ptStyle' => $shop->ptStyle,
  729. ];
  730. ShopYeChangeClass::addChange($change, true);
  731. //平台余额增加
  732. $ptStyle = $shop->ptStyle;
  733. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  734. $asset = PtAssetClass::getHdBalance();
  735. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  736. $asset = PtAssetClass::getGhsBalance();
  737. } else {
  738. util::fail('没有找到平台');
  739. }
  740. //增加平台可余额
  741. $currentPtBalance = bcadd($asset->amount, $brokerage, 2);
  742. $asset->amount = $currentPtBalance;
  743. $asset->save();
  744. if ($ptStyle == dict::getDict('ptStyle', 'hd')) {
  745. $txAsset = PtAssetClass::getHdTxBalance();
  746. } elseif ($ptStyle == dict::getDict('ptStyle', 'ghs')) {
  747. $txAsset = PtAssetClass::getGhsTxBalance();
  748. } else {
  749. util::fail('没有找到平台');
  750. }
  751. //增加平台可提现余额
  752. $currentPtTxBalance = $txAsset->amount;
  753. $currentPtTxBalance = bcadd($currentPtTxBalance, $brokerage, 2);
  754. $txAsset->amount = $currentPtTxBalance;
  755. $txAsset->save();
  756. $change = [
  757. 'relateId' => $cashId,
  758. 'capitalType' => $capitalType,
  759. 'amount' => $brokerage,
  760. 'balance' => $currentPtBalance,
  761. 'txBalance' => $currentPtTxBalance,
  762. 'io' => 1,
  763. 'payWay' => 0,
  764. 'event' => $customerSjName . '-' . $customerName . "提现" . $amount . "元,代理商【{$shop->merchantName}-{$shop->shopName}】获得佣金",
  765. 'sjId' => $shop->sjId,
  766. 'shopId' => $shop->id,
  767. 'tx' => $tx,
  768. 'ptStyle' => $ptStyle,
  769. 'shopName' => $shop->shopName,
  770. 'sjName' => $shop->merchantName,
  771. ];
  772. PtYeChangeClass::addChange($change, true);
  773. }
  774. }